Skip to content

JIT: Use inline label for fall-though blocks in codegen to give peephole opts a better chance - #128536

Closed
BoyBaykiller wants to merge 5 commits into
dotnet:mainfrom
BoyBaykiller:use-inline-label
Closed

BoyBaykiller wants to merge 5 commits into
dotnet:mainfrom
BoyBaykiller:use-inline-label

Conversation

@BoyBaykiller

@BoyBaykiller BoyBaykiller commented May 24, 2026 •

Copy link
Copy Markdown
Contributor

Example:

static bool MissedReuseCmp(int op1, int op2)
{
    if (op1 < op2) // first cmp
    {
        Console.WriteLine("Hi!");
    }
    else if (op1 == op2) // second cmp
    {
        return true;
    }
    return false;
}
G_M11242_IG01:  ;; offset=0x0000
       sub      rsp, 40
						;; size=4 bbWeight=1 PerfScore 0.25
G_M11242_IG02:  ;; offset=0x0004
       cmp      ecx, edx
       jl       SHORT G_M11242_IG05
						;; size=4 bbWeight=1 PerfScore 1.25
G_M11242_IG03:  ;; offset=0x0008
-      cmp      ecx, edx
       sete     al
       movzx    rax, al
-						;; size=8 bbWeight=0.50 PerfScore 0.50
+                       ;; size=6 bbWeight=0.50 PerfScore 0.38
G_M11242_IG04:  ;; offset=0x000E
       add      rsp, 40
       ret      
						;; size=5 bbWeight=0.50 PerfScore 0.62
G_M11242_IG05:  ;; offset=0x0013
       mov      rcx, 0x2BEC8714F98      ; 'Hi!'
       call     [System.Console:WriteLine(System.String)]
       xor      eax, eax
						;; size=18 bbWeight=0.50 PerfScore 1.75
G_M11242_IG06:  ;; offset=0x0025
       add      rsp, 40
       ret      
						;; size=5 bbWeight=0.50 PerfScore 0.62

Fix #128535

@github-actions github-actions Bot added the area-CodeGen-coreclr CLR JIT compiler in src/coreclr/src/jit and related components such as SuperPMI label May 24, 2026
@dotnet-policy-service dotnet-policy-service Bot added the community-contribution Indicates that the PR has been added by a community member label May 24, 2026
@dotnet-policy-service

Copy link
Copy Markdown
Contributor

Tagging subscribers to this area: @JulieLeeMSFT, @jakobbotsch
See info in area-owners.md if you want to be subscribed.

@EgorBo

EgorBo commented May 24, 2026

Copy link
Copy Markdown
Member

that is definitely not the right fix, it has to be fixed in RBO, @AndyAyersMS recently added a similar optimization where dominated relop effectively makes dominating one redundant.

@BoyBaykiller

Copy link
Copy Markdown
Contributor Author

Which branch are you suggesting RBO to remove?

static bool MissedReuseCmp(byte ch)
{
    if (ch <= 32)
    {
        if (ch != 32)
        {
            Console.WriteLine("Hi!");
            return false;
        }
        return true;
    }
    return false;
}

Even if it was fixable in RBO (which I dont think so?) this is useful either way.

@EgorBo

EgorBo commented May 24, 2026 •

Copy link
Copy Markdown
Member

Which branch are you suggesting RBO to remove?

static bool MissedReuseCmp(byte ch)
{
    if (ch <= 32)
    {
        if (ch != 32)
        {
            Console.WriteLine("Hi!");
            return false;
        }
        return true;
    }
    return false;
}

Even if it was fixable in RBO (which I dont think so?) this is useful either way.

It should be fixed in RBO with jump-threading actually. Console.WriteLine("Hi!"); is accessible with just if (ch < 32)

@BoyBaykiller

BoyBaykiller commented May 24, 2026 •

Copy link
Copy Markdown
Contributor Author

This looks better but still requires two compares:

static bool MissedReuseCmpIdeal(byte ch)
{
    if (ch < 32)
    {
        Console.WriteLine("Hi!");
    }
    else if (ch == 32)
    {
        return true;
    }
    return false;
}
G_M36236_IG02:  ;; offset=0x0004
       movzx    rax, cl
       cmp      eax, 32
       jg       SHORT G_M36236_IG06
						;; size=8 bbWeight=1 PerfScore 1.50
G_M36236_IG03:  ;; offset=0x000C
       cmp      eax, 32 ; <- redundant, re-use flag
       jne      SHORT G_M36236_IG05
       mov      eax, 1

Which this PR removes.


Actually to handle this we need to a peehole variant of IsRedundantCmp that handles cns as op2. Currently it only deals with regs. Should be easy to add in a follow up though

* handle empty IGF_EXTENSION
* small things
@BoyBaykiller

BoyBaykiller commented Jun 9, 2026 •

Copy link
Copy Markdown
Contributor Author

Closing, because even though the diffs seem good, this approach misses cases and has the downside of interfering with GC info.
This approach requires the pred to be a preceding IG in execution order which is an unnecessary restriction.
Instead, I have looked into extending the existing emitPeepholeIterateLastInstrs to visit the unique pred but that also seems messy, because there is no clear notion of "here is a pred IG, which may not be previous in execution order".

Here is an example this PR misses, I would like to handle:

static bool Missed(bool cond, int a, int b)
{
    if (a <= b)
    {
        return a != b && cond;
    }

    return true;
}
G_M60931_IG02:
       cmp      edx, r8d
       jle      SHORT G_M60931_IG05

G_M60931_IG03:
       mov      eax, 1

G_M60931_IG04:
       ret      

G_M60931_IG05:
       cmp      edx, r8d ; <- we don't remove this because it's not right after G_M60931_IG02
       setne    al
       movzx    rax, al
       movzx    rcx, cl
       and      eax, ecx

G_M60931_IG06:
       ret      

I think in order to properly handle this we need a good representation of CPU flags and still have a control flow graph, linking #74867

jakobbotsch pushed a commit that referenced this pull request Jun 10, 2026
…c vars) (#128811)

This random method (and other) tells us at that `igData` points to after
the gc vars.

https://github.com/dotnet/runtime/blob/f2d5042ceaea5642bac6bde8a1a5765afcbf2ef4/src/coreclr/jit/emit.h#L352-L360

More specifically it points to "addr of instruction descriptors".
However the corresponding debug-only `igDataSize` was being assigned
`gs` which is the size of the entire IG (including gc vars). It should
use code size (`sz`) instead.

Fixes this assert I was hitting in
#128536:

https://github.com/dotnet/runtime/blob/f2d5042ceaea5642bac6bde8a1a5765afcbf2ef4/src/coreclr/jit/emit.cpp#L10081
@github-actions github-actions Bot locked and limited conversation to collaborators Jul 9, 2026
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 community-contribution Indicates that the PR has been added by a community member

Projects

None yet

Development

Successfully merging this pull request may close these issues.

JIT: Missed flag re-use over fall-through blocks

2 participants