JIT: make funclet order correspond to EH clause order#127590
JIT: make funclet order correspond to EH clause order#127590AndyAyersMS wants to merge 6 commits intodotnet:mainfrom
Conversation
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.
|
@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. |
|
Tagging subscribers to this area: @JulieLeeMSFT, @jakobbotsch |
There was a problem hiding this comment.
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
fgCreateFuncletsand publish it via a newCompiler::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. |
|
Looks like we should keep both jit->vm and vm->jit mappings. Will revise this. |
|
@adamperlin want to review this? |
| // 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. |
There was a problem hiding this comment.
| // 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: |
There was a problem hiding this comment.
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. |
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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; |
There was a problem hiding this comment.
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 = falsetry { }
catch(e0) {...} // SAMETRY = true
catch (e1) {...} // SAMETRY = true?
There was a problem hiding this comment.
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
| 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}; |
There was a problem hiding this comment.
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]).
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.