Repository navigation
JIT: don't let RBO speculate nodes that depend on a removed check - #138238
Conversation
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
|
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. |
|
Tagging subscribers to this area: @JulieLeeMSFT, @jakobbotsch |
|
Would be nice if we knew the origin of the constraints, but we don't. @jakobbotsch PTAL |
There was a problem hiding this comment.
🟢 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
hasSideEffectswithisSafeToSpeculate, 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
left a comment
There was a problem hiding this comment.
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.
optRedundantDominatingBranchremoves 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 containingGTF_SIDE_EFFECT, but nodes made safe by a dominating check carry onlyGTF_ORDER_SIDEEFF. In the issue's repro, morph's non-null assertion prop marksobj.Valuenon-faulting because of theobj != nullguard; RBO then infers that guard fromnotNull & (obj.Value > 0), removes it, and the load runs with a null object.Replace
BasicBlock::hasSideEffects(only used by RBO) withisSafeToSpeculate, which also rejectsGTF_ORDER_SIDEEFF, and use it at all four RBO speculation checks. This matches theFoldNeverNegativeRangeTestfix 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 != 0dominatingx > 0where the load ofxdepends on an earlier, unrelated null check).Testing
NullCheck_00toJIT/opt/RedundantBranch/RedundantBranchDominating. With a Checked Core_Root and JITs built from the same commit, the base JIT fails it withNullReferenceException(7 passed / 1 failed); the fixed JIT passes all 8.NullReferenceException; fixed JIT prints the expected results and keeps the null check.Other platforms were not run locally.
Resolves #135811
Note
This PR description was drafted with GitHub Copilot.