Skip to content

JIT: add fgRemoveUnreachableTry phase - #129655

Merged
AndyAyersMS merged 2 commits into
dotnet:mainfrom
AndyAyersMS:jit-remove-unreachable-try
Jun 22, 2026
Merged

AndyAyersMS merged 2 commits into
dotnet:mainfrom
AndyAyersMS:jit-remove-unreachable-try

Conversation

@AndyAyersMS

@AndyAyersMS AndyAyersMS commented Jun 20, 2026 •

Copy link
Copy Markdown
Member

Removes EH regions whose try entry has become unreachable. Runs once, in the last set of EH cleanup passes (just before funclet creation). Not needed if not optimizing, as unreachable trys are removed at the end of importation and the other trys then remain reachable. Fixes an issue for wasm control flow, which is not tolerant of unreachable EH.

Note

This PR description was edited with assistance from GitHub Copilot.

Removes EH regions whose try entry has become unreachable. Run after
each empty-EH-cleanup pass. Fixes an issue for wasm control flow, which
is not tolerant of unreachable EH.

Co-authored-by: Copilot <[email protected]>
Copilot AI review requested due to automatic review settings June 20, 2026 14:05
@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 Jun 20, 2026
@AndyAyersMS

Copy link
Copy Markdown
Member Author

This only runs once, in the last group of EH cleanups. Not needed if not optimizing, as unreachable trys are removed at the end of importation, and the other trys then remain reachable.

@EgorBo PTAL
fyi @dotnet/jit-contrib

Only impacts two methods in SPMI. Needed for Wasm.

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.

Pull request overview

This PR introduces a new JIT phase that detects EH clauses whose try entry block is no longer reachable from method entry and removes those EH regions (including associated dead blocks), to avoid carrying unreachable EH into later compilation stages.

Changes:

  • Add a new flowgraph/EH cleanup phase fgRemoveUnreachableTry and wire it into the main compilation pipeline.
  • Add a new phase ID (PHASE_REMOVE_UNREACHABLE_TRY) and a DEBUG-only config knob (JitEnableRemoveUnreachableTry) consistent with existing EH cleanup toggles.
  • Implement logic to retarget callfinally pairs for dead finally handlers, remove EH table entries, rebuild DFS, and delete newly-unreachable blocks.

Reviewed changes

Copilot reviewed 5 out of 5 changed files in this pull request and generated 2 comments.

Show a summary per file
File Description
src/coreclr/jit/jitconfigvalues.h Adds JitEnableRemoveUnreachableTry config switch (DEBUG override pattern).
src/coreclr/jit/fgehopt.cpp Implements Compiler::fgRemoveUnreachableTry EH cleanup logic.
src/coreclr/jit/compphases.h Adds PHASE_REMOVE_UNREACHABLE_TRY phase name.
src/coreclr/jit/compiler.h Declares fgRemoveUnreachableTry on Compiler.
src/coreclr/jit/compiler.cpp Invokes the new phase once before funclet creation.

Comment thread src/coreclr/jit/fgehopt.cpp Resolved
Comment thread src/coreclr/jit/compiler.cpp Resolved
Update the Notes section to describe the actual PASS ordering: EH
table entries are dropped first, then the DFS is rebuilt and blocks
are removed.

Co-authored-by: Copilot <[email protected]>
@AndyAyersMS
AndyAyersMS requested a review from EgorBo June 21, 2026 14:04
Comment thread src/coreclr/jit/fgehopt.cpp Resolved

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

I assume this is needed for WASM correctness? Othwerwise having a new pass with DFS compute call for a few contexts sounds a bit too much

@AndyAyersMS

AndyAyersMS commented Jun 22, 2026 •

Copy link
Copy Markdown
Member Author

I assume this is needed for WASM correctness? Othwerwise having a new pass with DFS compute call for a few contexts sounds a bit too much

Yes, Wasm control flow has a hard time figuring out what to do with an unreachable try, and it has to figure something out or else it can't emit valid Wasm. Easiest thing is to try and ensure it never happens. I may be able to fix this in the control flow phase, in time.

@AndyAyersMS
AndyAyersMS merged commit f937da2 into dotnet:main Jun 22, 2026
138 checks passed
@dotnet-milestone-bot dotnet-milestone-bot Bot added this to the 11.0-preview6 milestone Jun 23, 2026
eiriktsarpalis pushed a commit that referenced this pull request Jul 15, 2026
Removes EH regions whose try entry has become unreachable. Runs once, in
the last set of EH cleanup passes (just before funclet creation). Not
needed if not optimizing, as unreachable trys are removed at the end of
importation and the other trys then remain reachable. Fixes an issue for
wasm control flow, which is not tolerant of unreachable EH.

> [!NOTE]
> This PR description was edited with assistance from GitHub Copilot.

---------

Co-authored-by: Copilot <[email protected]>
@github-actions github-actions Bot locked and limited conversation to collaborators Jul 24, 2026
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

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.

3 participants