Skip to content

JIT: we lose profile info for inlinee casts #116223

Closed
@AndyAyersMS

Description

@AndyAyersMS
using System.Runtime.CompilerServices;

int class1 = 0;
int class2 = 0;

BaseClass b1 = new DerivedClass1();
BaseClass b2 = new DerivedClass2();

for (int i = 0; i < 300; i++)
{
    Problem(i);
    Thread.Sleep(15);
}

Console.WriteLine($"class {class1} class2 {class2}");

[MethodImpl(MethodImplOptions.NoInlining)]
void Problem(int i)
{
    BaseClass b = i % 3 == 0 ? b1 : b2;
    if (b is DerivedClass1)
    {
        class1++;
    }
    else if (IsDerivedClass2(b))
    {
        class2++;
    }
}

bool IsDerivedClass2(BaseClass b)
{
    return b is DerivedClass2;
}

class BaseClass { }

class DerivedClass1 : BaseClass { }
class DerivedClass2 : BaseClass { }

The profile lookup done by the late cast expansion is always consulting the root method PGO data. If the cast came from an inlinee we will query this data with an inlinee IL offset. Usually this will just fail.

In the jit dump below, note we only find a profile for the DerivedClass1 cast. The last line is the "failed to find" profile message for the second cast.

*************** Starting PHASE Expand casts
Likely classes for call [000015]
  1) 00007FFF32ECFC30 (DerivedClass2) [likelihood:81%]
  2) 00007FFF32ECFAB0 (DerivedClass1) [likelihood:18%]
Accepting type DerivedClass2 with likelihood 81 as a candidate
Expanding cast helper call in BB04...
N003 ( 18, 18) [000015] --C-G+-----                         *  CALL help ref    CORINFO_HELP_ISINSTANCEOFCLASS $206
N001 (  1,  1) [000013] -----+----- arg1 rdx                +--*  LCL_VAR   ref    V05 tmp1         u:4 $2c0
N002 (  3, 10) [000014] H----+-N--- arg0 rcx                \--*  CNS_INT(h) long   0x7fff32ecfab0 class DerivedClass1 $300

New Basic Block BB09 [0009] created.
BB05 previous predecessor was BB04, now is BB09
BB06 previous predecessor was BB04, now is BB09
setting likelihood of BB04 -> BB09 to 1

lvaGrabTemp returning 6 (V06 rat0) called for replacement local.
New Basic Block BB10 [0010] created.
New Basic Block BB11 [0011] created.
New Basic Block BB12 [0012] created.
setting likelihood of BB10 -> BB09 to 0.5
setting likelihood of BB10 -> BB11 to 0.5
New Basic Block BB13 [0013] created.
setting likelihood of BB13 -> BB09 to 1
Candidate 0: likelihood 0.81 relative likelihood 0.81
setting likelihood of BB11 -> BB13 to 0.81
setting likelihood of BB11 -> BB12 to 0.19
setting likelihood of BB12 -> BB09 to 1

Compacting BB10 into BB04:
*************** In fgDebugCheckBBlist
No likely class or method, sorry

See #113913 (comment)

FYI @EgorBo

Metadata

Metadata

Assignees

Labels

area-CodeGen-coreclrCLR JIT compiler in src/coreclr/src/jit and related components such as SuperPMI

Type

No type

Projects

No projects

Relationships

None yet

Development

No branches or pull requests

Issue actions