Skip to content

JIT: make funclet order correspond to EH clause order#127590

Open
AndyAyersMS wants to merge 6 commits intodotnet:mainfrom
AndyAyersMS:FuncletOrderMatchesReportedEHClauseOrder
Open

JIT: make funclet order correspond to EH clause order#127590
AndyAyersMS wants to merge 6 commits intodotnet:mainfrom
AndyAyersMS:FuncletOrderMatchesReportedEHClauseOrder

Conversation

@AndyAyersMS
Copy link
Copy Markdown
Member

For Wasm the JIT host will need to match up funclets with EH clauses without relying on the offset data in the EH clause.

To allow this, set things up so that the host can infer which funclets are associated with an EH clause by making the ordering of EH clauses and the order of funclets (reported via unwind info) correspond.

Note this is not 1-1 as EH clauses with filters will inspire two funclets. In those cases the JIT will always put the filter funclet before the catch funclet.

For Wasm the JIT host will need to match up funclets with EH clauses without
relying on the offset data in the EH clause.

To allow this, set things up so that the host can infer which funclets are
associated with an EH clause by making the ordering of EH clauses and the
order of funclets (reported via unwind info) correspond.

Note this is not 1-1 as EH clauses with filters will inspire two funclets.
In those cases the JIT will always put the filter funclet before the catch funclet.
Copilot AI review requested due to automatic review settings April 30, 2026 00:33
@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 Apr 30, 2026
@AndyAyersMS
Copy link
Copy Markdown
Member Author

@dotnet/jit-contrib PTAL

A handful of (zero size) diffs for methods where there are mutual protect trys where some catch clauses have EH in them -- those are the cases where the JIT's EH table and the EH info reported to the VM end up differently ordered.

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

Copy link
Copy Markdown
Contributor

Copilot AI left a comment

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 aims to make EH funclet ordering match the order that EH clauses are reported to the VM (specifically, the “VM EH clause order” derived from try-region ordering), so Wasm can associate funclets with EH clauses without depending on EH offset data. The implementation introduces a shared “EH clause order” mapping computed during funclet creation and reuses it during EH reporting and Wasm virtual-IP clause construction.

Changes:

  • Compute and store a VM-EH-clause-order mapping in fgCreateFunclets and publish it via a new Compiler::compEHorderTab.
  • Update EH clause reporting (genReportEH / genReportEHClauses) to use the precomputed clause order instead of sorting at report time.
  • Update Wasm EH virtual-IP clause prefill/update logic to (attempt to) respect VM clause order.

Reviewed changes

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

Show a summary per file
File Description
src/coreclr/jit/flowgraph.cpp Builds and stores the EH-clause ordering map while creating funclets.
src/coreclr/jit/fgwasm.cpp Adjusts Wasm EH virtual-IP clause construction to use the new ordering map.
src/coreclr/jit/fgbasic.cpp Removes comment notes from fgRelocateEHRange header.
src/coreclr/jit/compiler.h Adds compEHorderTab to persist the ordering map.
src/coreclr/jit/codegencommon.cpp Switches EH reporting to use compEHorderTab rather than sorting clauses at report time.

Comment thread src/coreclr/jit/flowgraph.cpp Outdated
Comment thread src/coreclr/jit/codegencommon.cpp Outdated
Comment thread src/coreclr/jit/codegencommon.cpp Outdated
Comment thread src/coreclr/jit/fgwasm.cpp
Comment thread src/coreclr/jit/fgwasm.cpp
Comment thread src/coreclr/jit/fgwasm.cpp
Comment thread src/coreclr/jit/fgwasm.cpp
Comment thread src/coreclr/jit/compiler.h Outdated
@AndyAyersMS
Copy link
Copy Markdown
Member Author

Looks like we should keep both jit->vm and vm->jit mappings. Will revise this.

Copilot AI review requested due to automatic review settings April 30, 2026 02:04
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

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

Comment thread src/coreclr/jit/fgwasm.cpp Outdated
Comment thread src/coreclr/jit/codegencommon.cpp Outdated
Comment thread src/coreclr/jit/flowgraph.cpp Outdated
@AndyAyersMS
Copy link
Copy Markdown
Member Author

@adamperlin want to review this?

@AndyAyersMS AndyAyersMS requested a review from adamperlin April 30, 2026 20:59
Comment thread src/coreclr/jit/flowgraph.cpp Outdated
Comment on lines +3176 to +3177
// This is not the same as the order of entries in the EH table, so build a mapping from
// So, build mappings from vm order to table order and vice versa.
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.

Suggested change
// This is not the same as the order of entries in the EH table, so build a mapping from
// So, build mappings from vm order to table order and vice versa.
// This is not the same as the order of entries in the EH table, so build a mapping from vm order to table order and vice versa.

// Return Value:
// The last block that was relocated, or nullptr on failure.
//
// Notes:
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.

Intentional?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, this method no longer does the bad things the comment said it can do.

vmClauseOrderToEHTabOrder[XTnum] = (unsigned short)XTnum;
}

// The JIT's ordering of EH clauses does not guarantee that clauses covering the same try region are contiguous.
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.

Could we add a comment here about why this is and why the JIT's ordering can't be changed here -- maybe a reference to jiteh.cpp where fgSortEHTable is?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I can add a comment, sure.

There are quite a few places in the JIT that depend on the order of entries in the EH table.

{
// We have two clauses mapped to the same try region.
// Make sure we order the clause with the smaller index first.
return leftIndex < rightIndex;
Copy link
Copy Markdown
Contributor

@adamperlin adamperlin Apr 30, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If two clauses share the same try index, we need to tie break by original eh table index, since i < j in the JIT eh table (with equal try index) implies i more nested than j? And this is then sufficient to be able to determine if we set CORINFO_EH_CLAUSE_SAMETRY , since we're differentiating between:

try { 
    try { } catch (e0) {...}  // SAMETRY = false
} catch(e1) {...} // SAMETRY = false
try { } 
catch(e0) {...} // SAMETRY = true
catch (e1) {...} // SAMETRY = true

?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The JIT table order reflects the order they appeared in the IL, so in your example above we'd make sure to emit the catch(e0) before the catch(e1).

We didn't always do this sorting; see #104531

Copilot AI review requested due to automatic review settings May 1, 2026 01:47
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Copilot reviewed 5 out of 5 changed files in this pull request and generated 1 comment.

Comment on lines +2493 to +2507
for (unsigned XTnum = 0; XTnum < compHndBBtabCount; XTnum++)
{
const unsigned index = ehGetIndex(dsc);
EHblkDsc* const dsc = ehGetDsc(XTnum);

CORINFO_EH_CLAUSE clause;
clause.ClassToken = dsc->HasFilter() ? 0 : dsc->ebdTyp;
clause.Flags = ToCORINFO_EH_CLAUSE_FLAGS(dsc->ebdHandlerType);
clause.TryOffset = 0;
clause.TryLength = 0;
clause.HandlerOffset = 0;
clause.HandlerLength = 0;
clauses[index] = {clause, dsc};

unsigned const vmIndex = compEHTabOrderToVMClauseOrder[XTnum];

clauses[vmIndex] = {clause, dsc};
Copy link

Copilot AI May 1, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fgWasmVirtualIP now stores EHClauseInfo entries in VM clause order (via compEHTabOrderToVMClauseOrder), but the DEBUG dump later in this method still indexes clauses using ehGetIndex(dsc) (EH table order). This will print mismatched Try/Handler ranges for the descriptor being dumped. The dump should translate EH table indices to VM indices before indexing into clauses (e.g., use compEHTabOrderToVMClauseOrder[index]).

Copilot uses AI. Check for mistakes.
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.

3 participants