Skip to content

JIT: preserve exception order after hoist rejection - #137118

Merged
AndyAyersMS merged 1 commit into
dotnet:mainfrom
AndyAyersMS:jit/hoisting-exception-order
Nov 12, 2026
Merged

AndyAyersMS merged 1 commit into
dotnet:mainfrom
AndyAyersMS:jit/hoisting-exception-order

Conversation

@AndyAyersMS

Copy link
Copy Markdown
Member

Close the exception-ordering barrier for throwing operations that remain in the loop after hoist rejection. Previously, a later throwing candidate could move ahead of an earlier null check rejected for profitability, changing which exception is observed.

Split bottom-up classification from top-down selection so larger subtrees are considered first. Accepted parents skip descendants; rejected parents allow child hoists before applying the parent's exception barrier. Add regression coverage for division-by-zero and overflow, in separate and shared statements, and distinguish both passes in JIT dumps.

Validation

  • Windows x64 Checked and Release JIT builds passed. The final dump-label-only edit was rebuilt in Checked; Release was not rebuilt for that edit.
  • All four new cases fail with the baseline JIT. Fresh focused runs pass all five cases in both configurations, with matching runtime/JIT/CoreLib layouts.
  • Full Checked Regression_ro_2: 496 passed before the dump-only edits.
  • JIT formatting/clang-tidy passed. Fresh dumps verify the diagnostics; the final two-context codegen comparison against the pre-diagnostic JIT has no differences.
  • SuperPMI before the dump-only edits: 765,476 successful compilations, 327 assembly diffs, no failures or missing data, and 1,018 fewer code bytes.

Performance

Of the 327 diffs, 298 only prune nested child hoists, 16 also gain cheap invariants, and 13 lose existing hoists. Collection-wide static PerfScore geomeans are effectively neutral: benchmarks -0.000827%, libraries PMI -0.000440%, and PGO/crossgen2 unchanged.

BenchmarkDotNet ShortRun on this shared Windows x64 machine, at 1,000 iterations:

Repro-shaped loop Baseline Changed Delta
SeparateStatements 1,474.60 ns 1,742.20 ns +18.15%
QueuedCandidates 1,201.91 ns 1,749.91 ns +45.59%

These loops lose the previously hoisted division to preserve exception order; these are not general workload results. JIT compilation throughput and other architectures were not measured.

Resolves #134937

Note

This PR description was generated with GitHub Copilot.

Close the exception-ordering barrier for unhoisted throwing operations.
Split hoist classification from selection so larger subtrees are considered first.

Resolves dotnet#134937

Co-authored-by: Copilot <[email protected]>
@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 6, 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

The split classification / selection is to mitigate some of the regressions (and is also better in general).

We first try and hoist the largest trees, so that a tree with an invariant, may throw, unprofitable subtree can still be hoisted as a whole, even though we would not just hoist the subtree. These tend to be indirs feeding indirs.

Previously we would consider trees only in execution order, so the invariant, may throw, unprofitable subtree would be rejected and block any subsequent hoisting.

If hoisting the big tree fails, then we try hosting the subtrees.

We can apply this same trick to sibling trees, say a call with two may throw invariant args. If the earlier executing arg is unprofitable and so not hoisted, it will block hoisting of the later arg; to mitigate this we can consider them together as a single hoist candidate. I've left that as a follow-up.

ptal @dotnet/jit-contrib

@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. It prevents hoisting later throwing exception ahead of earlier unhoisted ones.

@AndyAyersMS
AndyAyersMS merged commit 68b16fb into dotnet:main Nov 12, 2026
136 checks passed
@dotnet-milestone-bot dotnet-milestone-bot Bot added this to the 12.0-preview1 milestone Nov 13, 2026
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) loop hoisting reorders exceptions when an earlier throwing candidate is rejected as unprofitable

2 participants