Skip to content

JIT: validate RBO relop rewrite operands - #133621

Merged
AndyAyersMS merged 1 commit into
dotnet:mainfrom
AndyAyersMS:fix-rbo-relop-rewrite
Sep 11, 2026
Merged

AndyAyersMS merged 1 commit into
dotnet:mainfrom
AndyAyersMS:fix-rbo-relop-rewrite

Conversation

@AndyAyersMS

Copy link
Copy Markdown
Member

Ensure dominating RBO only rewrites the remaining predicate when the VN relop operands match the concrete tree operands.

Add coverage for materialized predicates in both relational directions.

Fixes #133558.

Validation:

  • JIT.opt RedundantBranchSimplify: 35 passed
  • SuperPMI: no asm diffs in 987,559 Windows x64 Checked contexts; 908 identical misses

Note

This PR description was generated with GitHub Copilot CLI.

In optRedundantDominatingBranch, ensure the remaining predicate can be rewritten safely.

Fixes dotnet#133558.

Co-authored-by: Copilot <[email protected]>
Copilot AI lite review requested due to automatic review settings September 10, 2026 18:12
@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 Sep 10, 2026
@azure-pipelines

Copy link
Copy Markdown
Azure Pipelines:
Successfully started running 5 pipeline(s).
11 pipeline(s) were filtered out due to trigger conditions.
There may be pipelines that require an authorized user to comment /azp run to run.

@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
AndyAyersMS requested a review from EgorBo September 10, 2026 18:14
@AndyAyersMS

Copy link
Copy Markdown
Member Author

@EgorBo PTAL
fyi @dotnet/jit-contrib

Fixes new opt in .NET 11. No diffs expected.

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🔵 Needs a closer look

It changes CoreCLR JIT branch-optimization correctness in a subtle VN-dependent area that warrants maintainer validation despite the targeted fix and added tests.

Pull request overview

This PR hardens the JIT’s redundant dominating branch optimization by ensuring it only rewrites a dominated relop when the VN relop operands correspond to the dominated compare’s actual tree operands, preventing incorrect simplifications when liberal VN “looks through” a materialized predicate. It also adds targeted JIT opt tests covering the materialized-predicate pattern in both relational directions.

Changes:

  • Add an operand-VN equivalence check before rewriting the dominated relop operator during RBO simplification.
  • Add new xUnit coverage for the “materialized a != b predicate + dominating relational compare” pattern for both < and > directions.
File summaries
File Description
src/coreclr/jit/redundantbranchopts.cpp Adds a guard to prevent relop-only rewrites when VN operands don’t match the compare node’s operand VNs (avoids miscompiles with materialized predicates).
src/tests/JIT/opt/RedundantBranch/RedundantBranchSimplify.cs Adds regression tests that exercise the materialized-predicate scenario in both relational directions.
Review details
  • Files reviewed: 2/2 changed files
  • Comments generated: 0
  • Review effort level: Lite

@EgorBo EgorBo left a comment •

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think I ran into similar problem in global AP some time ago. (relop's op1vn and op2vn didn't match tree op1 and op2). I guess it's expected, just curious if when it goes off the rails, we should update VNs of relop.

@AndyAyersMS

Copy link
Copy Markdown
Member Author

I think I ran into similar problem in global AP some time ago. (relop's op1vn and op2vn didn't match tree op1 and op2). I guess it's expected, just curious if when it goes off the rails, we should update VNs of relop.

I don't quite understand what you're suggesting.

When going from VN space back to IR we have to be careful. Ideally this would be simpler and less error prone...

@AndyAyersMS

Copy link
Copy Markdown
Member Author

Failures are #133702

no diffs

@AndyAyersMS

Copy link
Copy Markdown
Member Author

/ba-g known mono test failures

@AndyAyersMS
AndyAyersMS merged commit 217339b into dotnet:main Sep 11, 2026
141 of 144 checks passed
@dotnet-milestone-bot dotnet-milestone-bot Bot added this to the 12.0-preview1 milestone Sep 14, 2026
@AndyAyersMS

Copy link
Copy Markdown
Member Author

/backport to release/11.0

@github-actions

Copy link
Copy Markdown
Contributor

Started backporting to release/11.0 (link to workflow run)

JulieLeeMSFT pushed a commit that referenced this pull request Sep 21, 2026
Backport of #133621 to release/11.0

/cc @AndyAyersMS

## Customer Impact

- [ ] Customer reported
- [x] Found internally

The JIT can rewrite a materialized Boolean predicate using value-number
operands that do not correspond to the actual tree operands, producing
wrong code. The reported case returns `0` unconditionally when the
correct result is `1`. This reproduces on .NET 11 RC2; .NET 8, 9, and 10
are correct. See #133558.

## Regression

- [x] Yes
- [ ] No

Introduced during the .NET 11 cycle by the redundant-branch relop
simplification added in #127181.

## Testing

Added regression coverage for materialized predicates in both relational
directions. All 35 `JIT.opt` `RedundantBranchSimplify` tests passed.
Windows x64 Checked SuperPMI produced no assembly differences in 987,559
successful compilations, with 908 identical misses.

## Risk

Low. The change adds a validation check before performing the existing
rewrite. When the value-number operands do not match the concrete tree
operands, the JIT conservatively skips the optimization. SuperPMI found
no code-generation changes outside the new regression cases.

Co-authored-by: Andy Ayers <[email protected]>
Co-authored-by: Copilot <[email protected]>
@github-actions github-actions Bot locked and limited conversation to collaborators Oct 22, 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

Projects

None yet

Development

Successfully merging this pull request may close these issues.

JIT: (bug) a dominating branch makes a later if on an unrelated bool return the wrong answer

3 participants