Skip to content

[RyuJIT] Drop redundant static initalizations #48358

@EgorBo

Description

@EgorBo

To track #48279 (comment)

Example:

bool Foo(int x)
{
    var comparer = EqualityComparer<int>.Default;
    for (int i = 0; i < 100; i++)
    {
        if (comparer.Equals(i, x))
            return true;
    }
    return false;
}

With #48279 we now should be able to inline that Equals (because we know the exact type of comparer local) thus, the comparer variable becomes unused and we don't need to statically initialize the EqualityComparer<int>, but we currently do:

*  ASG       ref   
+--*  LCL_VAR   ref    V02 loc0      <--- unused local !
\--*  COMMA     ref   
   +--*  CALL help long   HELPER.CORINFO_HELP_CLASSINIT_SHARED_DYNAMICCLASS  <--- has GTF_CALL_M_HELPER_SPECIAL_DCE flag !
   |  +--*  CNS_INT   long   0x7ff963fb0028
   |  \--*  CNS_INT   int    18
   \--*  IND       ref   
      \--*  CNS_INT(h) long   0xd1ffab1e static Fseq[<Default>k__BackingField]

If we manage to get rid of it in such cases we'll be able to drop workarounds such as this one (and AFAIR a similar is being added to PriorityQueue now in #48346).
Worth noting that in most cases static initialization is not emitted at all because a class can be already inited at the jit time. Unfortunately it happens rarely for methods with loops - such methods are compiled directly to tier1 and aren't re-jitted after.

A smaller repro:

int Foo()
{
    var unusedVar = EqualityComparer<int>.Default;
    return 42;
}

Current codegen:

       mov      rcx, 0xD1FFAB1E
       mov      edx, 18
       call     CORINFO_HELP_CLASSINIT_SHARED_DYNAMICCLASS
       mov      eax, 42
       ret

Expected codegen:

       mov      eax, 42
       ret

/cc @AndyAyersMS

Metadata

Metadata

Assignees

Labels

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

Type

No type

Projects

Status

Done

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions