JIT: fix stale VN in redundant branch opts dominating-branch simplification#129914
Open
EgorBo wants to merge 2 commits into
Open
JIT: fix stale VN in redundant branch opts dominating-branch simplification#129914EgorBo wants to merge 2 commits into
EgorBo wants to merge 2 commits into
Conversation
) optRedundantDominatingBranch walks up the dominator tree and may simplify an intermediate dominating relop, rewriting the start block's relop via tree->SetOper(newRelop) + fgValueNumberTree(tree). The cached treeNormVN was not refreshed afterwards, so optimizing a higher dominating branch reasoned about a stale value number and could unsoundly fold a guard. This caused `if (x != -1) ThrowIfNegativeOrZero(x)` (as in StreamReader's buffer-size validation) to throw for x == -1. Refresh treeNormVN after the relop is rewritten. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Contributor
|
Tagging subscribers to this area: @JulieLeeMSFT, @jakobbotsch |
Contributor
There was a problem hiding this comment.
Pull request overview
This PR fixes a correctness issue in CoreCLR RyuJIT’s redundant-branch optimization (optRedundantDominatingBranch) where a cached value number for the current block’s condition could become stale after in-loop relop simplification, leading to unsound folding of higher dominating guards.
Changes:
- Update
optRedundantDominatingBranchto refresh the cached normalized VN (treeNormVN) immediately after rewriting and re-value-numbering the start block’s relop. - Add a new JIT regression test (
Runtime_128062) that exercises thebufferSize != -1sentinel pattern to ensure-1is accepted and not miscompiled into a throwing path.
Reviewed changes
Copilot reviewed 3 out of 3 changed files in this pull request and generated no comments.
| File | Description |
|---|---|
| src/coreclr/jit/redundantbranchopts.cpp | Refreshes the start-block condition VN after relop rewrites so subsequent dominator-walk reasoning uses the updated VN. |
| src/tests/JIT/Regression/JitBlue/Runtime_128062/Runtime_128062.cs | Adds a focused regression test reproducing the stale-VN-driven unsound fold scenario. |
| src/tests/JIT/Regression/JitBlue/Runtime_128062/Runtime_128062.csproj | Adds the project file for the new regression test with optimizations enabled. |
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Fixes #128062.
Root cause
Compiler::optRedundantDominatingBranch(Redundant Branch Opts) walks up the dominator tree from a conditional block, trying to fold dominating branches whose outcome is implied by the current block's condition. While doing so it can simplify an intermediate dominating relop, rewriting the start block's relop in place:However,
treeNormVN(the start block's condition value number) was cached once asconstbefore the dominator walk. After the relop is rewritten, the loop keeps reasoning about the stale VN on the next iteration, which can unsoundly fold a higher dominating guard.Concrete miscompile
StreamReader.ValidateArgsAndOpenPathcontains:which lowers to three conditional blocks:
RBO starts at
BB06, simplifiesBB02'sx < 0away and rewritesBB06's relop tox > 0. It then walks up toBB01but still uses the staleNE(x, 0)VN, concludes thex == -1guard is redundant, and folds it. The result isif (x > 0) return; else throw, sox == -1now throwsArgumentOutOfRangeExceptioninstead of being accepted.This is exposed in CI on the
libraries-pgo/jitosr_stress_randomlegs (which disable R2R, so framework code is JIT-compiled at optimized tiers), but it reproduces at plain FullOpts — no PGO/OSR needed.Regression introduced by #127181.
Fix
Refresh
treeNormVNright after the start block's relop is re-value-numbered, so subsequent dominator iterations reason about the block's current condition.Testing
Runtime_128062(fails on the pre-fix JIT, passes after).asmdiffsonbenchmarks.run(+pgo, +pgo_optrepeat) andlibraries.pmi: ~530K contexts, no asserts, zero asm diffs — the change only suppresses the rare unsound fold.