Repository navigation
JIT: rebuild SSA and VN after RBO jump threading - #137972
Conversation
Jump threading in redundant branch opts (RBO) reroutes predecessors around a block that has only PHIs and a side-effect-free branch. It updates SSA and value numbers only partially: PHI-based threading rewrites some uses to new SSA defs, and dominator-based threading ignores PHIs entirely. The result is stale SSA and VN that later phases trust: - **dotnet#135813:** ancestors of rewritten uses keep stale VNs, so CSE reuses a value computed from a different SSA def. - **dotnet#135810:** threaded targets gain predecessors with no PHI args, so range check elimination wrongly removes a bounds check. - **dotnet#135812:** stale PHIs left by threading make strength reduction treat a value as loop-invariant. Instead of trying to repair SSA and VN incrementally, this change rebuilds them. If any jump threading happened during RBO, then at the end of the phase it: 1. resets SSA state and clears all tree VNs; 2. recomputes the DFS tree (removing blocks that became unreachable), dominators and loops; 3. re-runs SSA construction and value numbering. The partial COMMA-parent VN fixup in `optJumpThreadCore` is removed, since the rebuild supersedes it. The rewritten use's own VN is still updated in place, for the remainder of the RBO walk. ### Diffs (windows x64, all SPMI collections) - No replay failures. 262 contexts (0.01%) are missed by the diff JIT, because re-running VN makes JIT/EE queries absent from the collections. - Code size: **−95,219 bytes**. 23,782 contexts with diffs: 9,447 improvements, 5,618 regressions. - PerfScore in diffs improves in every collection (−0.08% to −2.08%). | Collection | Size delta (bytes) | PerfScore in diffs | |---|--:|--:| | aspnet2 | −607 | −0.16% | | benchmarks.run | −336 | −0.13% | | benchmarks.run_pgo | −2,244 | −0.14% | | benchmarks.run_pgo_optrepeat | −336 | −0.13% | | coreclr_tests | −6,377 | −0.80% | | libraries.crossgen2 | −1,085 | −0.08% | | libraries.pmi | −4,462 | −0.84% | | libraries_tests | −62,153 | −0.48% | | libraries_tests_no_tiered_compilation | −16,675 | −2.08% | | realworld | −453 | −0.15% | | smoke_tests.nativeaot | −491 | −0.49% | ### Throughput (windows x64, PIN, Release non-PGO JITs, same toolchain) MinOpts is unchanged. The cost is concentrated in the roughly 1–12% of optimized methods that jump thread. For those, the rebuild adds about 8% (p50), and up to about 37% for the worst method. Within the rebuild, SSA construction and value numbering account for about 94% of the cost. | Collection | Overall | FullOpts | |---|--:|--:| | aspnet2 | +1.62% | +1.77% | | benchmarks.run | +0.50% | +0.50% | | benchmarks.run_pgo | +1.44% | +2.23% | | benchmarks.run_pgo_optrepeat | +0.31% | +0.31% | | coreclr_tests | +0.64% | +1.00% | | libraries.crossgen2 | +0.51% | +0.51% | | libraries.pmi | +1.03% | +1.03% | | libraries_tests | +2.81% | +3.46% | | libraries_tests_no_tiered_compilation | +1.01% | +1.03% | | realworld | +1.09% | +1.10% | | smoke_tests.nativeaot | +0.90% | +0.90% | Alternatives considered: - **Incremental repair** (re-VN ancestors, bail when that isn't possible) lost 2.6–23.5% of threadings and grew code by about 216 KB. - **Rebuilding only when threading provably invalidates SSA** skipped only 13–43% of rebuilds, and correctness depends on listing every invalidation case. - **Reusing the original liveness in the rebuild** was roughly throughput-neutral across collections. The dead-store removal it skips just moves to later phases. - **Skipping the tracked-local sort in SSA liveness** (sort only when over the tracking limit, and have CSE sort its own candidates) saved only 0.04–0.17% FullOpts with the rebuild. CSE's heuristic depends on the sorted order, so the sort mostly moves into CSE. ### Future enhancements These are left for follow-up changes: - **Remove `BBF_NO_CSE_IN`.** PHI-based threading still sets it on threaded targets when the bypassed block has a memory PHI. With SSA and VN rebuilt, the barrier should no longer be needed for correctness. Removing it lets CSEs live across calls, which grew code by about 190 KB in an earlier experiment, so it needs its own tuning. - **Expand PHI-based jump threading.** Today it bails when a PHI has uses outside the block and its immediate successors, when a successor is a join, or for PHIs of promoted fields. Those restrictions exist so uses can be rewritten to keep SSA/VN consistent; with the rebuild, most of them can likely be relaxed. ### Testing - Added `Runtime_135810`, `Runtime_135812` and `Runtime_135813` to `JIT/Regression_ro_2`. All three fail with the current JIT and pass with this change. - `src\tests\build.cmd x64 checked -test JIT\Regression_ro_2\Regression_ro_2.csproj`, then `Regression_ro_2.cmd` with this JIT: 501 total, 477 passed, 0 failed, 24 skipped (as before). - `superpmi.py asmdiffs` over all windows x64 collections: no failures. Resolves dotnet#135813 Resolves dotnet#135810 Resolves dotnet#135812 Co-authored-by: Copilot <[email protected]> Copilot-Session: 4feae766-ed53-4bd5-9701-6cf0bf4d999f
|
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 |
|
I haven't had much luck finding ways to speed up either VN or SSA building. This rebuild costs us ~3% or so. Incremental fixes seem both risky and complicated, in particular dealing with memory VNs. So I don't see a good avenue there either (maybe if we made memory "operands" explicit, but that's a much bigger change). It will let us remove the ugly DONT_CSE hack, however doing so comes with some large size regressions and so I want to do that as a follow-up. It may let us be more ambitious with phi-based jump threading, though in doing so, I worry RBO will now run into the same kinds of issues that this is fixing with downstream consumers of SSA and VN. @jakobbotch ptal |
There was a problem hiding this comment.
🟡 Changes recommended
The nested SSA/VN rebuild misattributes RBO and liveness work in phase-level timing reports, requiring corrected timing boundaries or explicit support for nested passes.
1 open issue
What changed in this PR
This PR rebuilds SSA and value numbers after redundant branch optimization performs jump threading, addressing three JIT miscompilations caused by stale analysis data.
Changes:
- Tracks jump threading and rebuilds flow-graph annotations, SSA, and value numbers before later optimizations.
- Removes the partial COMMA-parent value-number fixup.
- Adds regression tests for incorrect bounds-check removal, strength reduction, and common-subexpression reuse.
| File | Description |
|---|---|
src/tests/JIT/Regression_ro_2/Runtime_135813.cs |
Tests that different SSA values are not incorrectly reused by CSE. |
src/tests/JIT/Regression_ro_2/Runtime_135812.cs |
Tests strength reduction after threading bypasses a PHI definition. |
src/tests/JIT/Regression_ro_2/Runtime_135810.cs |
Tests that an out-of-range array access still throws. |
src/coreclr/jit/redundantbranchopts.cpp |
Rebuilds analysis data after threading and removes partial parent-VN repair. |
src/coreclr/jit/compiler.h |
Adds the flag indicating that RBO requires an SSA/VN rebuild. |
Review effort: Balanced
Co-authored-by: Copilot <[email protected]> Copilot-Session: 4feae766-ed53-4bd5-9701-6cf0bf4d999f
|
Moved the rebuild into a post-RBO block in the main phase list, so we get proper phase checking and timing and such. |
|
@AndyAyersMS the RBO still tries to fix SSA e.g. |
I am not sure. I need to look at that more closely to make sure RBO itself doesn't need it. |
|
/azp run runtime-coreclr superpmi-diffs |
|
Azure Pipelines: Successfully started running 1 pipeline(s). |
|
/ba-g build monitor didn't pick up successful re-run of a timed out leg |

Jump threading in redundant branch opts (RBO) reroutes predecessors around a block that has only PHIs and a side-effect-free branch. It updates SSA and value numbers only partially: PHI-based threading rewrites some uses to new SSA defs, and dominator-based threading ignores PHIs entirely. The result is stale SSA and VN that later phases trust:
optJumpThreadCoreomits target PHI args for threaded edges, causingRangeCheckto unsoundly eliminate bounds checks #135810: threaded targets gain predecessors with no PHI args, so range check elimination wrongly removes a bounds check.Instead of trying to repair SSA and VN incrementally, this change rebuilds them. If any jump threading happened during RBO, then at the end of the phase it:
The partial COMMA-parent VN fixup in
optJumpThreadCoreis removed, since the rebuild supersedes it. The rewritten use's own VN is still updated in place, for the remainder of the RBO walk.Diffs (windows x64, all SPMI collections)
Throughput (windows x64, PIN, Release non-PGO JITs built with the same toolchain)
MinOpts is unchanged. The cost is concentrated in the roughly 1–12% of optimized methods that jump thread. For those, the rebuild adds about 8% (p50), and up to about 37% for the worst method. Within the rebuild, SSA construction and value numbering account for about 94% of the cost.
Alternatives considered:
Future enhancements
These are left for follow-up changes:
BBF_NO_CSE_IN. PHI-based threading still sets it on threaded targets when the bypassed block has a memory PHI. With SSA and VN rebuilt, the barrier should no longer be needed for correctness. Removing it lets CSEs live across calls, which grew code by about 190 KB in an earlier experiment, so it needs its own tuning.Testing
Runtime_135810,Runtime_135812andRuntime_135813toJIT/Regression_ro_2. All three fail with the current JIT and pass with this change.src\tests\build.cmd x64 checked -test JIT\Regression_ro_2\Regression_ro_2.csproj, thenRegression_ro_2.cmdwith this JIT: 501 total, 477 passed, 0 failed, 24 skipped (as before).superpmi.py asmdiffsover all windows x64 collections: no failures.Resolves #135813
Resolves #135810
Resolves #135812
Note
This PR description was drafted with the help of GitHub Copilot.