Skip to content

JIT: rebuild SSA and VN after RBO jump threading - #137972

Merged
AndyAyersMS merged 2 commits into
dotnet:mainfrom
AndyAyersMS:rbo-rebuild-ssa-vn
Nov 25, 2026
Merged

AndyAyersMS merged 2 commits into
dotnet:mainfrom
AndyAyersMS:rbo-rebuild-ssa-vn

Conversation

@AndyAyersMS

Copy link
Copy Markdown
Member

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:

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

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 #135813
Resolves #135810
Resolves #135812

Note

This PR description was drafted with the help of GitHub Copilot.

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
@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 20, 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

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
fyi @dotnet/jit-contrib

@AndyAyersMS
AndyAyersMS marked this pull request as ready for review November 23, 2026 16:17
@AndyAyersMS
AndyAyersMS requested review from jakobbotsch and a balanced review from Copilot November 23, 2026 16:17

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.

🟡 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

Comment thread src/coreclr/jit/redundantbranchopts.cpp Resolved
Comment thread src/coreclr/jit/redundantbranchopts.cpp Resolved
Co-authored-by: Copilot <[email protected]>
Copilot-Session: 4feae766-ed53-4bd5-9701-6cf0bf4d999f
@AndyAyersMS

Copy link
Copy Markdown
Member Author

Moved the rebuild into a post-RBO block in the main phase list, so we get proper phase checking and timing and such.

Comment thread src/coreclr/jit/compiler.cpp Resolved
@EgorBo

EgorBo commented Nov 23, 2026

Copy link
Copy Markdown
Member

@AndyAyersMS the RBO still tries to fix SSA e.g. replacementSsaDef->AddUse - I assume it's no longer needed due to full SSA recompute anyway?

@AndyAyersMS

Copy link
Copy Markdown
Member Author

@AndyAyersMS the RBO still tries to fix SSA e.g. replacementSsaDef->AddUse - I assume it's no longer needed due to full SSA recompute anyway?

I am not sure. I need to look at that more closely to make sure RBO itself doesn't need it.

@jakobbotsch

Copy link
Copy Markdown
Member

/azp run runtime-coreclr superpmi-diffs

@azure-pipelines

Copy link
Copy Markdown
Azure Pipelines:
Successfully started running 1 pipeline(s).

@AndyAyersMS

Copy link
Copy Markdown
Member Author

/ba-g build monitor didn't pick up successful re-run of a timed out leg

@AndyAyersMS
AndyAyersMS merged commit bc93180 into dotnet:main Nov 25, 2026
126 of 128 checks passed
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

5 participants