JIT: Duplicate function epilogs may be generated

Open
#112,392 4 comments 1 reaction 0 assignees View on GitHub

Nobody has claimed this yet.

Assessment

Difficulty
4/5
Estimated time
3-5 days
Newbie friendliness
45/100
Issue type
Bug
Clarity
Mostly clear
Activity status
Stale
Tech stack
csharp
Domain
compilers

Research direction

Start with the linked Godbolt reproduction and compare the generated G_M43597_IG05 and G_M43597_IG07 epilogs in the assembly output. Trace the JIT code-generation path responsible for these return blocks and verify the result on the supplied TwoEpilogs example. Done means equivalent epilogs are shared or otherwise no longer duplicated, with the generated code becoming smaller.

Written by the indexing model from the issue text.

Description

area-CodeGen-coreclr

Following example generates two identical function epilogs. I would expect some overall size improvements from only having a single epilog and jumping to it. https://godbolt.org/z/Mch1hGKfx

static bool TwoEpilogs(in Matrix4x4 a, in Matrix4x4 b)
{
    var c = a * b;

    if (c[0, 0] == 1.0f && c[1, 1] == 1.0f)
    {
        return true;
    }

    return false;
}

G_M43597_IG05 and G_M43597_IG07 are the same

; Method Program.EntryPoint:TwoEpilogs(byref,byref):ubyte (FullOpts)
G_M43597_IG01:  ;; offset=0x0000
       sub      rsp, 152
       vmovaps  xmmword ptr [rsp+0x80], xmm6
       vmovaps  xmmword ptr [rsp+0x70], xmm7
       vmovaps  xmmword ptr [rsp+0x60], xmm8
       vmovaps  xmmword ptr [rsp+0x50], xmm9
       vmovaps  xmmword ptr [rsp+0x40], xmm10
       vxorps   xmm4, xmm4, xmm4
       vmovdqu  ymmword ptr [rsp], ymm4
       vmovdqu  ymmword ptr [rsp+0x20], ymm4
						;; size=55 bbWeight=1 PerfScore 14.58

G_M43597_IG02:  ;; offset=0x0037
       ; bunch of multiplication code
       vucomiss xmm0, dword ptr [reloc @RWD00]
       jp       SHORT G_M43597_IG04
       jne      SHORT G_M43597_IG04
						;; size=302 bbWeight=1 PerfScore 122.00

G_M43597_IG03:  ;; offset=0x0165
       vmovss   xmm0, dword ptr [rsp+0x14]
       vucomiss xmm0, dword ptr [reloc @RWD00]
       jp       SHORT G_M43597_IG04
       je       SHORT G_M43597_IG06
						;; size=18 bbWeight=0.50 PerfScore 4.50

G_M43597_IG04:  ;; offset=0x0177
       xor      eax, eax
						;; size=2 bbWeight=0.50 PerfScore 0.12

G_M43597_IG05:  ;; offset=0x0179
       vmovaps  xmm6, xmmword ptr [rsp+0x80]
       vmovaps  xmm7, xmmword ptr [rsp+0x70]
       vmovaps  xmm8, xmmword ptr [rsp+0x60]
       vmovaps  xmm9, xmmword ptr [rsp+0x50]
       vmovaps  xmm10, xmmword ptr [rsp+0x40]
       add      rsp, 152
       ret      
						;; size=41 bbWeight=0.50 PerfScore 10.62

G_M43597_IG06:  ;; offset=0x01A2
       mov      eax, 1
						;; size=5 bbWeight=0.50 PerfScore 0.12

G_M43597_IG07:  ;; offset=0x01A7
       vmovaps  xmm6, xmmword ptr [rsp+0x80]
       vmovaps  xmm7, xmmword ptr [rsp+0x70]
       vmovaps  xmm8, xmmword ptr [rsp+0x60]
       vmovaps  xmm9, xmmword ptr [rsp+0x50]
       vmovaps  xmm10, xmmword ptr [rsp+0x40]
       add      rsp, 152
       ret      
						;; size=41 bbWeight=0.50 PerfScore 10.62
RWD00  	dd	3F800000h		;         1
; Total bytes of code: 464
Dominant language
C#
Stars
18.3k
Forks
5.6k
PR merge metrics
PR metrics pending

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

More from dotnet/runtime

All issues in dotnet/runtime

Similar issues

More C# issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.