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: consider optimizing box followed by GetType #8680

Closed
AndyAyersMS opened this issue Aug 3, 2017 · 0 comments
Closed

JIT: consider optimizing box followed by GetType #8680

AndyAyersMS opened this issue Aug 3, 2017 · 0 comments
Labels
area-CodeGen-coreclr CLR JIT compiler in src/coreclr/src/jit and related components such as SuperPMI enhancement Product code improvement that does NOT require public API changes/additions JitThroughput CLR JIT issues regarding speed of JIT itself optimization tenet-performance Performance related issue
Milestone

Comments

@AndyAyersMS
Copy link
Member

Might not be a perf win, but it shows up in exception paths so could help reduce overall code size and jit time.

For instance, inspired by similar code inSystem.ValueTuple:

struct B
{
    public B(double d) { x = d; y = 4.4; }
    double x;
    double y;
    public bool CompareTo(object other)
    {
       if (!(other is B)) throw new Exception("Bad type, expected" + this.GetType().ToString());
       var otherB = (B) other;
       return x == otherB.x && y == otherB.y;
    }
}

the codegen for CompareTo is:

; Assembly listing for method B:CompareTo(ref):bool:this
; Emitting BLENDED_CODE for X64 CPU with AVX
; optimized code
; rsp based frame
; partially interruptible
; Final local variable assignments
;
;  V00 this         [V00,T03] (  6,  3.50)   byref  ->  rdi         this
;  V01 arg1         [V01,T00] (  6,  5.25)     ref  ->  rsi         class-hnd
;* V02 loc0         [V02    ] (  0,  0   )  struct (16) zero-ref
;  V03 tmp0         [V03,T01] (  6,  7.24)     ref  ->  rdx         class-hnd
;  V04 tmp1         [V04,T06] (  4,  0   )     ref  ->  rsi
;  V05 tmp2         [V05,T07] (  3,  0   )     ref  ->  rdi         class-hnd exact
;  V06 tmp3         [V06,T04] (  2,  2   )  double  ->  mm0         V02.x(offs=0x00) P-INDEP
;  V07 tmp4         [V07,T05] (  2,  1.50)  double  ->  mm1         V02.y(offs=0x08) P-INDEP
;  V08 tmp5         [V08,T09] (  2,  0   )     ref  ->  rbx
;  V09 tmp6         [V09,T10] (  2,  0   )     ref  ->  rdx
;  V10 tmp7         [V10,T11] (  2,  0   )     ref  ->  rdx
;  V11 tmp8         [V11,T02] (  3,  6   )   byref  ->  rax
;  V12 OutArgs      [V12    ] (  1,  1   )  lclBlk (32) [rsp+0x00]
;  V13 rat0         [V13,T08] (  3,  0   )     ref  ->  rax
;
; Lcl frame size = 32

G_M6708_IG01:
       57                   push     rdi
       56                   push     rsi
       53                   push     rbx
       4883EC20             sub      rsp, 32
       C5F877               vzeroupper
       488BF9               mov      rdi, rcx
       488BF2               mov      rsi, rdx

G_M6708_IG02:
       488BD6               mov      rdx, rsi
       4885D2               test     rdx, rdx
       7411                 je       SHORT G_M6708_IG03
       48B9105A637FF87F0000 mov      rcx, 0x7FF87F635A10
       48390A               cmp      qword ptr [rdx], rcx
       7402                 je       SHORT G_M6708_IG03
       33D2                 xor      rdx, rdx

G_M6708_IG03:
       4885D2               test     rdx, rdx
       7466                 je       SHORT G_M6708_IG10

G_M6708_IG04:
       48BA105A637FF87F0000 mov      rdx, 0x7FF87F635A10
       483916               cmp      qword ptr [rsi], rdx
       7412                 je       SHORT G_M6708_IG05
       488BD6               mov      rdx, rsi
       48B9105A637FF87F0000 mov      rcx, 0x7FF87F635A10
       E8118C385F           call     CORINFO_HELP_UNBOX

G_M6708_IG05:
       488D4608             lea      rax, bword ptr [rsi+8]
       C4E17B1000           vmovsd   xmm0, qword ptr [rax]
       C4E17B104808         vmovsd   xmm1, qword ptr [rax+8]   ;; seems odd to load this.y here
       C4E17B1017           vmovsd   xmm2, qword ptr [rdi]
       C4E1792ED0           vucomisd xmm2, xmm0
       7A20                 jpe      SHORT G_M6708_IG08
       751E                 jne      SHORT G_M6708_IG08
       C4E17B104708         vmovsd   xmm0, qword ptr [rdi+8]   ;; would expect it to be loaded here
       C4E1792EC1           vucomisd xmm0, xmm1
       0F9BC0               setpo    al
       7A03                 jpe      SHORT G_M6708_IG06
       0F94C0               sete     al

G_M6708_IG06:
       0FB6C0               movzx    rax, al

G_M6708_IG07:
       4883C420             add      rsp, 32
       5B                   pop      rbx
       5E                   pop      rsi
       5F                   pop      rdi
       C3                   ret

G_M6708_IG08:
       33C0                 xor      eax, eax

G_M6708_IG09:
       4883C420             add      rsp, 32
       5B                   pop      rbx
       5E                   pop      rsi
       5F                   pop      rdi
       C3                   ret

G_M6708_IG10:
       48B9105A637FF87F0000 mov      rcx, 0x7FF87F635A10
       E84D6E855F           call     CORINFO_HELP_NEWSFAST   ;; allocate new B on heap
       488BF0               mov      rsi, rax   ;; copy contents to new B
       488D4E08             lea      rcx, bword ptr [rsi+8]
       C4E17A6F07           vmovdqu  xmm0, qword ptr [rdi]
       C4E17A7F01           vmovdqu  qword ptr [rcx], xmm0
       48B9904E59DEF87F0000 mov      rcx, 0x7FF8DE594E90
       E82D6E855F           call     CORINFO_HELP_NEWSFAST
       488BF8               mov      rdi, rax
       B901000000           mov      ecx, 1
       48BA704E637FF87F0000 mov      rdx, 0x7FF87F634E70
       E8862C395F           call     CORINFO_HELP_STRCNS
       488BD8               mov      rbx, rax
       488BCE               mov      rcx, rsi     ;; call GetType on new B on heap, ignore contents
       E8AB5AAC5F           call     System.Object:GetType():ref:this
       488BC8               mov      rcx, rax
       488B00               mov      rax, qword ptr [rax]
       488B4048             mov      rax, qword ptr [rax+72]
       FF10                 call     gword ptr [rax]System.Object:ToString():ref:this
       488BD0               mov      rdx, rax
       488BCB               mov      rcx, rbx
       E8C417A35E           call     System.String:Concat(ref,ref):ref
       488BD0               mov      rdx, rax
       488BCF               mov      rcx, rdi
       E8E9D1A55E           call     System.Exception:.ctor(ref):this
       488BCF               mov      rcx, rdi
       E8B121395F           call     CORINFO_HELP_THROW
       CC                   int3

; Total bytes of code 272, prolog size 10 for method B:CompareTo(ref):bool:this

Also a bit puzzling why we load both x and y from this before doing any compare, seems like we should defer loading y. I suppose that's just how promotion works; all the fields get loaded even if some of the downstream uses are conditional.

AndyAyersMS referenced this issue in AndyAyersMS/coreclr Aug 31, 2017
If the only use of a box is in a call to Type:GetType, we can remove the
box and just construct the type directly.

Also add some logging to the type optimizations done in morph.

Closes #13187.
AndyAyersMS referenced this issue in AndyAyersMS/coreclr Aug 31, 2017
If the only use of a box is in a call to Type:GetType, we can remove the
box and just construct the type directly.

Also add some logging to the type optimizations done in morph.

Closes #13187.
AndyAyersMS referenced this issue in AndyAyersMS/coreclr Sep 12, 2017
If the only use of a box is in a call to Type:GetType, remove the box and
obtain the type directly. Get the handle needed for obtaining the type from
the newobj call in the original box, via a new box removal option.

Also add some logging to the type optimizations done in morph.

Closes #13187.
AndyAyersMS referenced this issue in dotnet/coreclr Sep 12, 2017
If the only use of a box is in a call to Type:GetType, remove the box and
obtain the type directly. Get the handle needed for obtaining the type from
the newobj call in the original box, via a new box removal option.

Also add some logging to the type optimizations done in morph.

Closes #13187.
@msftgits msftgits transferred this issue from dotnet/coreclr Jan 31, 2020
@msftgits msftgits added this to the Future milestone Jan 31, 2020
@ghost ghost locked as resolved and limited conversation to collaborators Dec 21, 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 enhancement Product code improvement that does NOT require public API changes/additions JitThroughput CLR JIT issues regarding speed of JIT itself optimization tenet-performance Performance related issue
Projects
None yet
Development

No branches or pull requests

2 participants