runtime icon indicating copy to clipboard operation
runtime copied to clipboard

[RyuJit/ARM32] NYI_ARM: Lowering for cast from small type to float

Open hqueue opened this issue 9 years ago • 11 comments

With COMPlus_AltJIT=* COMPlus_AltJitAssertOnNYI=0 options, following 78 assertions are observed from JIT/Methodical.

     78 lowerarmarch.cpp:279 - NYI_ARM: Lowering for cast from small type to float

Example

With COMPlus_AltJit=Func_0_1_1_1_1_1_1_1_1_1_1_1_1_1_1_1_1_1 option, following 4 tests raise same NYI assertion.

JIT/Methodical/fp/exgen/10w250d_cs_d
JIT/Methodical/fp/exgen/10w250d_cs_do
JIT/Methodical/fp/exgen/10w250d_cs_r
JIT/Methodical/fp/exgen/10w250d_cs_ro
=======================
     Test Results
=======================
# CoreCLR Bin Dir  : 
# Tests Discovered : 4
# Passed           : 0
# Failed           : 4
# Skipped          : 0
=======================
FAILED   - [   0][   6s]JIT/Methodical/fp/exgen/10w250d_cs_d/10w250d_cs_d.sh
               BEGIN EXECUTION
               /mnt/albireo/mywork/device/rpi3/unittests/coreoverlays/coreoverlay.test20170515/corerun 10w250d_cs_d.exe

               Assert failure(PID 17660 [0x000044fc], Thread: 17660 [0x44fc]): Assertion failed 'NYI_ARM: Lowering for cast from small type to float' in 'testout1:Func_0_1_1_1_1_1_1_1_1_1_1_1_1_1_1_1_1_1():double' (IL size 131)

                   File: /opt/code/github/hqueue/coreclr/src/jit/lowerarmarch.cpp Line: 279
                   Image: /mnt/albireo/mywork/device/rpi3/unittests/coreoverlays/coreoverlay.test20170515/corerun

               ./10w250d_cs_d.sh: line 240: 17660 Aborted                 (core dumped) $_DebuggerFullPath "$CORE_ROOT/corerun" $ExePath $CLRTestExecutionArguments
               Expected: 100
               Actual: 134
               END EXECUTION - FAILED

hqueue avatar May 15 '17 11:05 hqueue

Edited on May 16: After analysis, following issue is not related to above ryujit/arm32 issue. Followings will be tracked in other issue https://github.com/dotnet/coreclr/issues/11640.

I've tried to enable casting from small type to float.

Then, all above four 10w250d are passed, but an exception is raised from following two 10w5d tests.

COMPlus_AltJit=Func_0_5_6_2_5

JIT/Methodical/fp/exgen/10w5d_cs_do
JIT/Methodical/fp/exgen/10w5d_cs_ro
               Unhandled Exception: System.OverflowException: Value was either too large or too small for an Int32.
                  at System.Convert.ThrowInt32OverflowException()
                  at System.Convert.ToInt32(UInt64 value)
                  at testout1.Func_0_3_6_2()
                  at testout1.Func_0_3_6()
                  at testout1.Func_0_3()
                  at testout1.Func_0()
                  at testout1.Main()
               ./10w5d_cs_ro.sh: line 240: 17196 Aborted                 (core dumped) $_DebuggerFullPath "$CORE_ROOT/corerun" $ExePath $CLRTestExecutionArguments
               Expected: 100
               Actual: 134
               END EXECUTION - FAILED

hqueue avatar May 15 '17 11:05 hqueue

Then, all above four 10w250d are passed, but an exception is raised from following two 10w5d tests.

AFAIR those tests contain a ton of autogenerated code that does a lot of things. What usually happens is that you fix a NYI and that allows the AltJit to compiler a method that was previously compiled by the legacy JIT. And then it turns out that the AltJit has a bug that is triggered by the newly compiled method. Most likely the AltJit has a bug in its handling of the integer casts with overflow checks.

mikedn avatar May 15 '17 13:05 mikedn

On current master without any additional changes:

$ COMPlus_AltJitLimit=2 \
COMPlus_AltJit=System.Convert:ToUInt32 \
clr-checked/corerun tests-release/JIT/Methodical/fp/exgen/200w1d-02_cs_r/200w1d-02_cs_r.exe

Unhandled Exception: System.OverflowException: Value was either too large or too small for an Int32.
   at System.Convert.ThrowInt32OverflowException()
   at System.Convert.ToInt32(Int64 value)
   at testout1.Func_0()
   at testout1.Main()
Aborted

After debugging with a help of the COMPlus_JitFunctionTrace=1 option and a conditional breakpoint at compiler.cpp:4331 if Compiler::jitTotalMethodCompiled == 2, the function of interest appears to be System.Convert:ToUInt32(double):int: it returns 0xfe666d80 under the legacy jit and 0xfe666d81 under the altjit. The double argument in both cases is the same, (double)0xfe666d80.

mskvortsov avatar May 15 '17 14:05 mskvortsov

Further investigation showed the problem is incorrect RA. The method mentioned contains this fragment generated by the AltJit:

00003E  EEBC 0BC0      vcvt.d2u s0, d0            ; lower half of d0 gets trashed
000042  EE10 0A10      vmov.f2i r0, s0
000046  EE04 0A10      vmov.i2f s8, r0
00004A  EEB8 4B44      vcvt.u2d d8, s8
00004E  EE30 4B44      vsub    d8, d0, d8         ; trashed d0 is used again
000052  2200           movs    r2, 0
000054  F240 0300      movw    r3, 0x00
000058  F6C3 73E0      movt    r3, 0x3fe0
00005C  EC43 2B15      vmov.i2d d10, r2, r3
000060  EEB4 4B45      vcmp    d8, d10
000064  EEF1 FA10      vmrs    APSR, FPSCR
000068  DC0F           bgt     SHORT G_M2415_IG03 ; as a result,
                                                  ; this branch is taken under altjit,
                                                  ; but passed through on legacy

...

G_M2415_IG03:
00008A  1C40           adds    r0, r0, 1

G_M2415_IG04:
00008C  E8BD 8818      pop     {r3,r4,r11,pc}

mskvortsov avatar May 15 '17 15:05 mskvortsov

@mikedn @mskvortsov Thank you for the explanation and analysis :)

@mskvortsov I will look into register allocation why d0 is used even after trashed by s0.

hqueue avatar May 16 '17 04:05 hqueue

I found that this unhanded exception is also raised in LegacyJIT in Debug build and not in Release build. I'm afraid this might not be a Ryujit issue.

I've opened new issue dotnet/coreclr#11640 to track the issue in legacy jit.

hqueue avatar May 16 '17 07:05 hqueue

@hqueue Regarding RA. I think I found the place in the codegen, which causes failure of 200w1d-02_cs_r described above. codegenarm.cpp#L1860:

void CodeGen::genFloatToIntCast(GenTreePtr treeNode)
{
    ...

    genConsumeOperands(treeNode->AsOp());

    assert(insVcvt != INS_invalid);
    getEmitter()->emitIns_R_R(insVcvt, dstSize, op1->gtRegNum, op1->gtRegNum);
    getEmitter()->emitIns_R_R(INS_vmov_f2i, dstSize, treeNode->gtRegNum, op1->gtRegNum);

    genProduceReg(treeNode);
}

mskvortsov avatar May 16 '17 07:05 mskvortsov

@mskvortsov I found similar logic in int to float casting where vcvt use a same dst reg to generate vcvt instruction. However I think this is not a problem because a dst reg is being newly defined.

https://github.com/dotnet/coreclr/blob/094d8301dc238abe470f67342c8b17217585c798/src/jit/codegenarm.cpp#L1785

But for the case where you pointed out (float to int), I think we have to reserve a temporary float register and use a temporary reg as a dst reg for vcvt.

For example,

00003E  EEBC 0BC0      vcvt.d2u tmp_float_reg, d0            ; lower half of d0 gets trashed
000042  EE10 0A10      vmov.f2i r0, tmp_float_reg

What do you think of it?

If this approach seems to be right, then I think we have better make a new issue to address this issue seperately, because we didn't make use of internalFloatCount yet in arm32 and never verified it yet, we only make use of internalFloatCount in Lowering::TreeNodeInfoInit.

hqueue avatar May 16 '17 08:05 hqueue

(deleted) sorry...

hqueue avatar May 16 '17 08:05 hqueue

I found similar logic in int to float casting where vcvt use same a single dst reg to generate vcvt instruction. However I think this is not a problem because a dst reg is being newly defined.

Yes, genIntToFloatCast() doesn't have this problem. Only genFloatToIntCast() has.

But for the case where you pointed out (float to int), I think we have to reserve a temporary float register and use a temporary reg as a dst reg for vcvt.

This will work. Actually, I have a patch almost ready, I'll make a PR later today.

mskvortsov avatar May 16 '17 08:05 mskvortsov

This will work. Actually, I have a patch almost ready, I'll make a PR later today.

@mskvortsov Cool! Thanks :)

hqueue avatar May 16 '17 08:05 hqueue