Skip to content
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

JIT: Optimize boxing(x) == boxing(y) to false. #13822

Closed
EgorBo opened this issue Nov 23, 2019 · 3 comments
Closed

JIT: Optimize boxing(x) == boxing(y) to false. #13822

EgorBo opened this issue Nov 23, 2019 · 3 comments
Labels
area-CodeGen-coreclr CLR JIT compiler in src/coreclr/src/jit and related components such as SuperPMI optimization
Milestone

Comments

@EgorBo
Copy link
Member

EgorBo commented Nov 23, 2019

static bool Generic<T1, T2>(T1 a, T2 b)
{
    return (object)a == (object)b;
}

static bool Test()
{
    return Generic(42, 3.14);
}

Codegen for Test:

; Method Program:Test():bool
G_M11853_IG01:
       push     rsi
       sub      rsp, 32
       vzeroupper 
G_M11853_IG02:
       mov      rcx, 0xD1FFAB1E
       call     CORINFO_HELP_NEWSFAST
       mov      rsi, rax
       mov      dword ptr [rsi+8], 42
       mov      rcx, 0xD1FFAB1E
       call     CORINFO_HELP_NEWSFAST
       vmovsd   xmm0, qword ptr [reloc @RWD00]
       vmovsd   qword ptr [rax+8], xmm0
       cmp      rsi, rax
       sete     al
       movzx    rax, al
G_M11853_IG03:
       add      rsp, 32
       pop      rsi
       ret      
RWD00  dq	40091EB851EB851Fh
\--*  EQ\NE     int   
   +--*  BOX       ref   
   |  \--*  LCL_VAR   ref    V03 tmp1   
   \--*  BOX       ref   
      \--*  LCL_VAR   ref    V04 tmp2   

Expected codegen:

; Method Program:Test():bool
       xor      eax, eax  ; return false 
       ret     

Not sure if people actually use this pattern though. But should be a simple fix because CoreCLR already optimizes boxing for similar cases (e.g. boxing(x) == null to false etc, see mono/mono#17858 for links).

category:cq
theme:optimization
skill-level:intermediate
cost:medium

@jkotas
Copy link
Member

jkotas commented Nov 24, 2019

Not sure if people actually use this pattern though

There is no point for adding optimizations for patterns than never show up in real world code....

@EgorBo
Copy link
Member Author

EgorBo commented Nov 24, 2019

@jkotas I made a quick prototype and the jit-diff found a few kb diff for BCL, e.g. https://github.com/dotnet/corefx/blob/master/src/System.Data.Common/src/System/Data/RbTree.cs#L1581

UPD: well, it seems it's pmi was ddosing it and won't happen in real world.

@msftgits msftgits transferred this issue from dotnet/coreclr Jan 31, 2020
@msftgits msftgits added this to the Future milestone Jan 31, 2020
@BruceForstall BruceForstall added the JitUntriaged CLR JIT issues needing additional triage label Oct 28, 2020
@BruceForstall BruceForstall removed the JitUntriaged CLR JIT issues needing additional triage label Nov 25, 2020
@BruceForstall
Copy link
Member

Given the expectation that this doesn't occur in real-world code, I'm going to close this until we see a real-world motivating example.

@dotnet dotnet locked as resolved and limited conversation to collaborators Dec 25, 2020
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 optimization
Projects
None yet
Development

No branches or pull requests

4 participants