Repository navigation
JIT: exclude EH handler/filter entry blocks from GetUniquePred - #137780
Merged
Merged
Conversation
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
|
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. |
Contributor
|
Tagging subscribers to this area: @JulieLeeMSFT, @jakobbotsch |
AndyAyersMS
requested review from
jakobbotsch
and
a balanced review from Copilot
November 17, 2026 22:47
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 |
Contributor
There was a problem hiding this comment.
🟢 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::GetUniquePredreturnnullptrfor 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
This was referenced Nov 18, 2026
jakobbotsch
approved these changes
Nov 19, 2026
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 #137683
Description
BasicBlock::GetUniquePredspecial-cased onlyfgFirstBB(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) andHasZeroTripTest/IsZeroTripTest(loop cloning) both walk backward viaGetUniquePredto 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 ofGetUniquePredalready treats anullresult as "skip this optimization," so the change is conservative-only.Testing
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.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.