Skip to content

JIT: exclude EH handler/filter entry blocks from GetUniquePred - #137780

Merged
AndyAyersMS merged 1 commit into
dotnet:mainfrom
AndyAyersMS:fix-137683-getuniquepred-eh
Nov 19, 2026
Merged

AndyAyersMS merged 1 commit into
dotnet:mainfrom
AndyAyersMS:fix-137683-getuniquepred-eh

Conversation

@AndyAyersMS

Copy link
Copy Markdown
Member

Fixes #137683

Description

BasicBlock::GetUniquePred special-cased only fgFirstBB (because of the implicit prolog edge), but a handler or filter entry block can also be reached without an explicit flow-graph edge: exception dispatch can invoke a second-pass finally funclet between a throw point and the handler, with no explicit edge recorded for that transfer of control.

FindConstInit (loop unrolling) and HasZeroTripTest/IsZeroTripTest (loop cloning) both walk backward via GetUniquePred to find a constant loop-init value or a zero-trip guard to elide, and could walk across such an implicit EH boundary, picking up a value that a finally funclet run during unwind can still change before the handler executes. This let the JIT propagate a stale constant into a loop guarded by a filter or an unclonable finally, producing wrong results.

Fix

Treat handler/filter entry blocks the same way as fgFirstBB: never report them as having a "unique" predecessor. Every caller of GetUniquePred already treats a null result as "skip this optimization," so the change is conservative-only.

Testing

  • Added a regression test (src/tests/Regressions/coreclr/GitHub_137683) combining both known-reproducing scenarios (filter+finally, and an unclonable finally reached both normally and via an exception). Verified it fails without the fix and passes with it.
  • Ran local SuperPMI asmdiffs (superpmi.py asmdiffs) across all locally cached collections (aspnet2, benchmarks, coreclr_tests, libraries.crossgen2, libraries.pmi, libraries_tests, realworld, smoke_tests.nativeaot) comparing a baseline JIT (rolling build) against a diff JIT with this fix: 0 bytes / 0 PerfScore delta overall. Some contexts are flagged by SuperPMI's near-differ as "has diffs," but these are solely due to debug-info (native variable live-range) reporting granularity differences near EH handler boundaries -- code bytes, GC info, and EH info are identical in every flagged context that was inspected.

Note

This PR description and commit were prepared with GitHub Copilot CLI assistance.

BasicBlock::GetUniquePred special-cased only fgFirstBB (due to the
implicit prolog edge), but a handler or filter entry block can also be
reached without an explicit flow-graph edge: exception dispatch can
invoke a second-pass finally funclet between a throw point and the
handler, with no explicit edge recorded.

FindConstInit (loop unrolling) and HasZeroTripTest/IsZeroTripTest (loop
cloning) both walk backward via GetUniquePred to find a constant loop
init or a zero-trip guard to elide, and could walk across such an
implicit EH boundary, picking up a value that a finally funclet run
during unwind can still change before the handler executes. This let
the JIT propagate a stale constant into a loop guarded by a filter or
an unclonable finally, producing wrong results.

Treat handler/filter entry blocks the same way as fgFirstBB: never
report them as having a "unique" predecessor. Every caller of
GetUniquePred treats a null result as "skip this optimization," so the
change is conservative-only.

Add a regression test combining both known-reproducing scenarios
(filter+finally, and an unclonable finally reached normally and via an
exception).

Co-authored-by: Copilot <[email protected]>

Copilot-Session: 4259cfdf-2ea5-4b3c-9066-fb9efda019c4
@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 17, 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

No code diffs; some debug info diffs. The debug info changes are a bit odd, but they seem harmless enought (either breaking up or coalescing ranges).

@jakobbotsch ptal
fyi @dotnet/jit-contrib

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.

🟢 Approval recommended

No active review issues are reported for the exception-handling predecessor-analysis fix.

No open issues 🎉

What changed in this PR

This PR addresses incorrect JIT loop optimization across exception-handling boundaries in #137683. It prevents predecessor-based analysis from relying on values that exception dispatch or an intervening finally can invalidate.

Changes:

  • Make BasicBlock::GetUniquePred return nullptr for handler and filter entry blocks, and document their implicit control flow.
  • Add regression coverage for filter-plus-finally execution and normal versus exceptional entry into an unclonable finally.
File Description
src/​tests/​Regressions/​coreclr/​GitHub_137683/​test137683.csproj Registers the priority-1 regression test and test-library reference.
src/​tests/​Regressions/​coreclr/​GitHub_137683/​test137683.cs Checks loop results across the affected exception-handling scenarios.
src/​coreclr/​jit/​block.cpp Excludes handler and filter entries from unique-predecessor results.

Review effort: Balanced

@AndyAyersMS
AndyAyersMS merged commit 094fa6a into dotnet:main Nov 19, 2026
128 of 130 checks passed
@dotnet-milestone-bot dotnet-milestone-bot Bot added this to the 12.0-preview1 milestone Nov 23, 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 unrolling takes the constant IV init from an exception filter, ignoring a finally that runs between the filter and the catch

3 participants