Skip to content

[RyuJIT] Drop redundant static initalizations #48358

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
EgorBo opened this issue Feb 16, 2021 · 1 comment
Closed

[RyuJIT] Drop redundant static initalizations #48358

EgorBo opened this issue Feb 16, 2021 · 1 comment
Assignees
Labels
area-CodeGen-coreclr CLR JIT compiler in src/coreclr/src/jit and related components such as SuperPMI tenet-performance Performance related issue
Milestone

Comments

@EgorBo
Copy link
Member

EgorBo commented Feb 16, 2021

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

@EgorBo EgorBo added the tenet-performance Performance related issue label Feb 16, 2021
@ghost ghost added area-CodeGen-coreclr CLR JIT compiler in src/coreclr/src/jit and related components such as SuperPMI untriaged New issue has not been triaged by the area owner labels Feb 16, 2021
@EgorBo EgorBo added this to the 6.0.0 milestone Feb 16, 2021
@EgorBo EgorBo removed the untriaged New issue has not been triaged by the area owner label Feb 16, 2021
@EgorBo EgorBo self-assigned this Feb 16, 2021
@EgorBo EgorBo added the preview label Mar 22, 2021
@EgorBo
Copy link
Member Author

EgorBo commented May 24, 2021

Implemented in #50446

@EgorBo EgorBo closed this as completed Jul 8, 2021
@ghost ghost locked as resolved and limited conversation to collaborators Aug 7, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
area-CodeGen-coreclr CLR JIT compiler in src/coreclr/src/jit and related components such as SuperPMI tenet-performance Performance related issue
Projects
Archived in project
Development

No branches or pull requests

1 participant