Skip to content

JIT: check reachability before simplifying dominated relop - #138624

Open
AndyAyersMS wants to merge 1 commit into
dotnet:mainfrom
AndyAyersMS:Fix133980
Open

AndyAyersMS wants to merge 1 commit into
dotnet:mainfrom
AndyAyersMS:Fix133980

Conversation

@AndyAyersMS

@AndyAyersMS AndyAyersMS commented Dec 2, 2026 •

Copy link
Copy Markdown
Member

The optimization

Redundant branch opts can eliminate a dominating branch D when one of its
successors is a side-effect-free conditional block B and the other is a
successor X shared with B. The original optimization, introduced in
#126587, removes D's branch when the predicate taking B
away from X implies the predicate taking D to B.

#127181 extended this to cases where the implication does not
hold. It combines those predicates with AND and, if that simplifies to a
relop over the same operands, removes D's branch and rewrites B's relop.
For example, (x != y) && (x <= y) simplifies to x < y.

The bug

D dominating B does not mean that every execution of B follows the
D-to-B edge. In the regression test, X can reach B without revisiting
D:

             D: x != y?
             /         \
           yes         no
            v           v
       B: x <= y? --no--> X: SideEffect()
            |             | counter <= 1000 -> B
           yes            | counter > 1000  -> return -1
            v
         return 1

For x == y, the original flow is D -> X -> B -> return 1. The rewrite
instead makes D branch unconditionally to B and changes B's test to
x < y. Execution then cycles through B and X until it returns -1.
The conjunction is not valid at B because x != y need not hold there.

The fix

Before rewriting B's relop, check whether X can reach B without passing
through D. Start at D's actual shared successor so the search also
considers EH flow from skipped blocks. Allow the rewrite only when B is
unreachable; otherwise, or if the shared per-phase reachability budget is
exhausted, leave the branches unchanged. Add regression coverage for equal,
less-than, and greater-than inputs.

Validation

  • .\src\tests\build.cmd -Test JIT\Regression_ro_2\Regression_ro_2.csproj x64 Checked: passed.
  • .\Regression_ro_2.cmd -coreroot C:\repos\runtime4\artifacts\tests\coreclr\windows.x64.Checked\Tests\Core_Root
    from the built test directory: 527 passed, 7 skipped, including all three
    regression cases.
  • Windows x64 SuperPMI asm diffs with Checked JITs: no diffs in 2,849,559
    compared contexts; 52,955 contexts were missing for both JITs. The baseline
    was built at 5339d5292c9, whose JIT sources match the parent commit.
  • SuperPMI TP diff on benchmarks.run_pgo using Release non-PGO JITs:
    98,518 contexts compared, 408 missing for both; JIT instruction count
    decreased by 0.0085%.

Resolves #133980

Note

This PR description was generated with GitHub Copilot.

Redundant branch opts can rewrite a dominated relop to (relop AND
dominating relop), which is only valid if the dominating relop holds
whenever the block is reached. Verify the block is not reachable from
the dominating block's shared successor, using a per-phase budget.

Fixes dotnet#133980.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
@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 Dec 2, 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.

@AndyAyersMS

Copy link
Copy Markdown
Member Author

@jakobbotsch PTAL
fyi @dotnet/jit-contrib

Note for normal flow this seems to require irreducible flow which likely explains why we didn't spot this sooner.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area-CodeGen-coreclr CLR JIT compiler in src/coreclr/src/jit and related components such as SuperPMI

Projects

None yet

Development

Successfully merging this pull request may close these issues.

JIT: (bug) optRedundantDominatingBranch relop simplification strengthens a compare in a block that has other predecessors

2 participants