Skip to content

JIT: don't let RBO speculate nodes that depend on a removed check - #138238

Merged
AndyAyersMS merged 1 commit into
dotnet:mainfrom
AndyAyersMS:fix-135811-rbo-nonfaulting
Nov 25, 2026
Merged

AndyAyersMS merged 1 commit into
dotnet:mainfrom
AndyAyersMS:fix-135811-rbo-nonfaulting

Conversation

@AndyAyersMS

Copy link
Copy Markdown
Member

optRedundantDominatingBranch removes a dominating branch when the dominated block's predicate implies it, so the dominated block (and blocks along the paths to and from it) now execute where the dominating branch used to bypass them. It only rejected blocks containing GTF_SIDE_EFFECT, but nodes made safe by a dominating check carry only GTF_ORDER_SIDEEFF. In the issue's repro, morph's non-null assertion prop marks obj.Value non-faulting because of the obj != null guard; RBO then infers that guard from notNull & (obj.Value > 0), removes it, and the load runs with a null object.

Replace BasicBlock::hasSideEffects (only used by RBO) with isSafeToSpeculate, which also rejects GTF_ORDER_SIDEEFF, and use it at all four RBO speculation checks. This matches the FoldNeverNegativeRangeTest fix for #134202. Regression from #126587 (.NET 11).

The check is conservative: it can't tell which dominating check an ordering-dependent node relies on, so it also blocks some safe cases (e.g. x != 0 dominating x > 0 where the load of x depends on an earlier, unrelated null check).

Testing

  • Added NullCheck_00 to JIT/opt/RedundantBranch/RedundantBranchDominating. With a Checked Core_Root and JITs built from the same commit, the base JIT fails it with NullReferenceException (7 passed / 1 failed); the fixed JIT passes all 8.
  • Issue repro: base JIT throws NullReferenceException; fixed JIT prints the expected results and keeps the null check.
  • SuperPMI asmdiffs, windows-x64, all collections: no replay failures; 236 of 2,860,277 contexts with diffs, +2,120 bytes total, all FullOpts.
Collection Contexts with diffs Bytes
libraries_tests_no_tiered_compilation 122 +918
libraries_tests 56 +599
aspnet2 28 +283
benchmarks.run_pgo 17 +219
6 others 2-3 each +10 to +28

Other platforms were not run locally.

Resolves #135811

Note

This PR description was drafted with GitHub Copilot.

optRedundantDominatingBranch makes the dominated block (and blocks on the
paths to and from it) execute where the dominating branch used to bypass
them. It only rejected blocks with GTF_SIDE_EFFECT, but nodes made safe by
a dominating check, e.g. an indirection marked non-faulting by morph's
non-null assertion prop, carry only GTF_ORDER_SIDEEFF. Removing the null
check then lets the load run with a null object.

Replace BasicBlock::hasSideEffects (only used by RBO) with
isSafeToSpeculate, which also rejects GTF_ORDER_SIDEEFF, matching the
FoldNeverNegativeRangeTest fix for dotnet#134202.

Fixes dotnet#135811

Co-authored-by: Copilot <[email protected]>
Copilot-Session: 550ec089-5e39-4794-a1f9-d4a3ad44f268
@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 Nov 24, 2026
@azure-pipelines

Copy link
Copy Markdown
Azure Pipelines:
Successfully started running 5 pipeline(s).
10 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

Copy link
Copy Markdown
Member Author

Would be nice if we knew the origin of the constraints, but we don't.

@jakobbotsch PTAL
fyi @dotnet/jit-contrib

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.

🟢 Approval recommended

No active review issues remain for the JIT speculation-safety change.

No open issues 🎉

What changed in this PR

This PR fixes unsafe speculation in the JIT’s redundant dominating-branch optimization. It prevents branch removal from exposing operations whose safety depends on a dominating check, including field loads previously proven non-null.

Changes:

  • Replace hasSideEffects with isSafeToSpeculate, rejecting both ordinary and ordering side effects.
  • Apply the stronger safety check at all four speculation checks in the optimization.
  • Add regression coverage for null objects and objects with zero or positive field values.
File Description
src/​tests/​JIT/​opt/​RedundantBranch/​RedundantBranchDominating.cs Adds a regression test for preserving a required null guard.
src/​coreclr/​jit/​redundantbranchopts.cpp Uses the stronger speculation check at all four locations.
src/​coreclr/​jit/​block.h Declares and documents isSafeToSpeculate.
src/​coreclr/​jit/​block.cpp Implements the safety check for tree-form and linear intermediate representations.

Review effort: Balanced

@JulieLeeMSFT JulieLeeMSFT 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.

LGTM.
isSafeToSpeculate() checks both GTF_SIDE_EFFECT and GTF_ORDER_SIDEEFF on statement roots and LIR nodes, without target-, debug-, or optimization-mode guards. RBO uses this stricter check at its four existing gates: the candidate block, skipped unconditional blocks, intervening dominators, and continuation of the dominator walk.
No new IR or control-flow update paths are added. Successful branch folding retains the existing morph call.

@AndyAyersMS
AndyAyersMS merged commit 9899e0b into dotnet:main Nov 25, 2026
128 of 130 checks passed
@AndyAyersMS
AndyAyersMS deleted the fix-135811-rbo-nonfaulting branch November 25, 2026 17:35
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) redundant branch opts remove a null check that a non-faulting load depends on (spurious NullReferenceException)

4 participants