Skip to content

[Wasm RyuJit] eh flow#125594

Merged
AndyAyersMS merged 33 commits intodotnet:mainfrom
AndyAyersMS:WasmEHFlow
Mar 20, 2026
Merged

[Wasm RyuJit] eh flow#125594
AndyAyersMS merged 33 commits intodotnet:mainfrom
AndyAyersMS:WasmEHFlow

Conversation

@AndyAyersMS
Copy link
Member

@AndyAyersMS AndyAyersMS commented Mar 16, 2026

Implements much of the control flow needed to support resuming control in a method after an exception is caught.

See #121178 (comment) for a sketch of the approach implemented here.

Overview of the changes:

  • a new phase fgWasmEhFlow that runs before Lower that makes the "continuation flow" for try/catch explicit in the method flow graph and IR.
  • a "lightweight" abstraction over the try regions in the method that enables walking the try region body in RPO
  • updates to the core layout algorithm to keep try regions that will inspire Wasm try_table protection contiguous in the block order. For cases where a try region begins in a loop but extends outside the loop, we extend the end of the Wasm loop to encompass the try.
  • codegen for try_table, still a bit incomplete; it currently catches all exceptions and does not do proper rethrow.
  • introduction of a control variable to steer execution to the proper continuation on catch resumption. This will likely evolve into the Virtual IP in some way. It is also ultimately going to be settable by the runtime to signal that particular stack frames will not catch the current exception.

Copilot AI review requested due to automatic review settings March 16, 2026 00:28
@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 Mar 16, 2026
Copy link
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 extends the WASM JIT pipeline to start modeling WebAssembly exception-handling (EH) control flow explicitly, including new wasm instruction/value types and new IR/control-flow structures needed to emit try_table/catch_ref patterns.

Changes:

  • Add wasm EH surface area: exnref value type, try_table/catch_ref/throw_ref instructions, and emitter support for encoding try_table and catch clauses.
  • Introduce wasm-specific IR nodes/flags (GT_WASM_IF_EXCEPT, GT_WASM_THROW_REF, GTF_JTRUE_WASM_EH) and integrate them into core GenTree helpers.
  • Add a new WASM phase (PHASE_WASM_EH_FLOW) plus try-region enumeration and a try-aware+loop-aware RPO walk to support structured wasm codegen.

Reviewed changes

Copilot reviewed 20 out of 20 changed files in this pull request and generated 9 comments.

Show a summary per file
File Description
src/coreclr/jit/registeropswasm.h / .cpp Add ExnRef wasm value type + name mapping
src/coreclr/jit/instrswasm.h Add wasm EH opcodes (try_table, catch_ref, throw_ref)
src/coreclr/jit/emitfmtswasm.h Add new instruction formats for TRY_TABLE / CATCH_DECL
src/coreclr/jit/emit.h Add instrDescValTypeImm and format query for try_table encoding
src/coreclr/jit/emitwasm.h / .cpp Add emitIns_Ty_I and encoder support for try_table + catch decls
src/coreclr/jit/gtlist.h Add wasm-specific IR nodes
src/coreclr/jit/gentree.h / .cpp Add wasm JTRUE flag + plumb new nodes through GenTree infrastructure
src/coreclr/jit/jiteh.cpp Add helper to find outermost mutual-protect try region
src/coreclr/jit/flowgraph.cpp Adjust throw-helper placement for wasm; add try-region data structures
src/coreclr/jit/compiler.h / compiler.hpp Declare try-region APIs + add try-aware loop-aware RPO visitor
src/coreclr/jit/fgwasm.h / .cpp Add try intervals and new fgWasmEhFlow() phase building explicit post-catch dispatch
src/coreclr/jit/compiler.cpp / compphases.h Wire in the new WASM EH flow phase
src/coreclr/jit/codegenwasm.cpp Emit try_table/catch_ref at block start; suppress codegen for wasm-EH-marked JTRUE
src/coreclr/jit/fgopt.cpp Minor preprocessor consistency tweak

You can also share your feedback on Copilot code review. Take the survey.

@jkotas jkotas added the arch-wasm WebAssembly architecture label Mar 16, 2026
Copilot AI review requested due to automatic review settings March 17, 2026 21:12
Copy link
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 adds initial infrastructure for explicit exception-handling (EH) continuation flow in the Wasm JIT pipeline, including new IR nodes and Wasm instruction/emitter support to enable try_table-based dispatch for catch continuations.

Changes:

  • Introduces Wasm-specific IR nodes (GT_WASM_JEXCEPT, GT_WASM_THROW_REF) and wires them into liveness/IR utilities.
  • Adds a new Wasm EH flow phase (PHASE_WASM_EH_FLOW) that materializes post-catch continuation dispatch via conditional entry blocks, switches, and rethrow blocks.
  • Extends Wasm codegen/emitter/instruction metadata with try_table, catch_ref, throw_ref, and exnref support.

Reviewed changes

Copilot reviewed 21 out of 21 changed files in this pull request and generated 11 comments.

Show a summary per file
File Description
src/coreclr/jit/registeropswasm.h Adds WasmValueType::ExnRef.
src/coreclr/jit/registeropswasm.cpp Adds "exnref" name mapping.
src/coreclr/jit/liveness.cpp Treats GT_WASM_JEXCEPT as always side-effecting (non-removable).
src/coreclr/jit/instrswasm.h Adds Wasm EH-related pseudo/real instructions (catch_ref, try_table, throw_ref).
src/coreclr/jit/gtlist.h Adds new Wasm-specific GT node kinds.
src/coreclr/jit/gentree.h Treats GT_WASM_JEXCEPT as a conditional-jump-like node.
src/coreclr/jit/gentree.cpp Wires new GT nodes into comparison/use/exception/clone/dump paths.
src/coreclr/jit/flowgraph.cpp Alters throw-helper placement for Wasm; adds FlowGraphTryRegions implementation.
src/coreclr/jit/fgwasm.h Extends WasmInterval to support Try intervals (in addition to Block/Loop).
src/coreclr/jit/fgwasm.cpp Adds fgWasmEhFlow + try-aware traversal and interval creation for try regions.
src/coreclr/jit/fgstmt.cpp Marks GT_WASM_JEXCEPT as control-flow.
src/coreclr/jit/fgopt.cpp Minor preprocessor consistency fix for Wasm switch assertion.
src/coreclr/jit/emitwasm.h Adds emitter APIs for (type, imm) encoding used by try_table.
src/coreclr/jit/emitwasm.cpp Implements encoding/size/printing for try_table and catch declarations; adds exnref type code.
src/coreclr/jit/emitfmtswasm.h Adds new Wasm instruction formats (IF_TRY_TABLE, IF_CATCH_DECL).
src/coreclr/jit/emit.h Adds a new instrDesc type to carry (valtype, imm) for try_table.
src/coreclr/jit/compphases.h Adds new phase name PHASE_WASM_EH_FLOW.
src/coreclr/jit/compiler.hpp Adds try-aware loop-aware RPO walker and try-region block visitor.
src/coreclr/jit/compiler.h Declares FlowGraphTryRegions APIs and Wasm EH flow entry points.
src/coreclr/jit/compiler.cpp Inserts Wasm EH flow phase before unreachable removal and SCC transforms.
src/coreclr/jit/codegenwasm.cpp Emits try_table/catch_ref for Try intervals; adds handling stubs for new GT nodes.

You can also share your feedback on Copilot code review. Take the survey.

@AndyAyersMS AndyAyersMS marked this pull request as ready for review March 17, 2026 21:57
Copilot AI review requested due to automatic review settings March 17, 2026 21:57
@AndyAyersMS
Copy link
Member Author

AndyAyersMS commented Mar 17, 2026

@dotnet/jit-contrib FYI

Not sure will wants to review this one. Any volunteers?

This runs through a cross-crossgen of an x86 crossgen SPMI without asserting but is otherwise difficult to test (eg the Wasm cannot be validated yet as funclet codegen is mixed in with the main function).

@jakobbotsch
Copy link
Member

Not sure will wants to review this one. Any volunteers?

I can take a look.

Copy link
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

Implements the initial end-to-end plumbing for Wasm exception resumption control flow in RyuJIT by making post-catch continuation dispatch explicit in the CFG/IR, and extending Wasm control-flow interval/layout + emission to support try_table-based protection/dispatch.

Changes:

  • Add fgWasmEhFlow phase to materialize catch-resumption dispatch (control var + per-try switch/rethrow blocks + special GT_WASM_JEXCEPT/GT_WASM_THROW_REF nodes).
  • Introduce FlowGraphTryRegions / FlowGraphTryRegion and use them to build try-aware + loop-aware RPO and to create Wasm Try intervals.
  • Extend Wasm emitter/instruction tables/value types and codegen to start emitting try_table + catch_ref scaffolding.

Reviewed changes

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

Show a summary per file
File Description
src/coreclr/jit/registeropswasm.h Add WasmValueType::ExnRef.
src/coreclr/jit/registeropswasm.cpp Add name mapping for exnref.
src/coreclr/jit/liveness.cpp Treat GT_WASM_JEXCEPT as non-removable side-effecting node.
src/coreclr/jit/instrswasm.h Add catch_ref, throw_ref, and try_table pseudo/real instructions.
src/coreclr/jit/gtlist.h Introduce GT_WASM_JEXCEPT and GT_WASM_THROW_REF nodes.
src/coreclr/jit/gentree.h Treat GT_WASM_JEXCEPT as a conditional-jump operator.
src/coreclr/jit/gentree.cpp Wire new Wasm nodes into comparison/use-iteration/exceptions/cloning/printing helpers.
src/coreclr/jit/flowgraph.cpp Wasm-specific throw-helper placement tweak; add FlowGraphTryRegions implementation.
src/coreclr/jit/fgwasm.h Extend Wasm intervals with Kind::{Block,Loop,Try}.
src/coreclr/jit/fgwasm.cpp Add fgWasmEhFlow; build try regions; create Try intervals; try-aware loop-aware RPO.
src/coreclr/jit/fgstmt.cpp Treat GT_WASM_JEXCEPT as control-flow for statement removal logic.
src/coreclr/jit/fgopt.cpp Minor wasm-specific assert guard cleanup.
src/coreclr/jit/emitwasm.h Add emitter APIs for <valtype, imm> encoding (for try_table).
src/coreclr/jit/emitwasm.cpp Implement try_table/catch_ref sizing + output + disasm plumbing; add exnref type code.
src/coreclr/jit/emitfmtswasm.h Add TRY_TABLE and CATCH_DECL formats.
src/coreclr/jit/emit.h Add instrDescValTypeImm and helper predicate for wasm try_table descriptor.
src/coreclr/jit/compphases.h Add PHASE_WASM_EH_FLOW.
src/coreclr/jit/compiler.hpp Implement try-aware loop-aware RPO; implement VisitTryRegionBlocksReversePostOrder.
src/coreclr/jit/compiler.h Declare try-region types + new wasm phase helpers.
src/coreclr/jit/compiler.cpp Insert PHASE_WASM_EH_FLOW before wasm DFS cleanup/SCC transform.
src/coreclr/jit/codegenwasm.cpp Begin emitting try_table/catch_ref for Try intervals; stub wasm EH catchret; add node handling.

You can also share your feedback on Copilot code review. Take the survey.

Copy link
Member

@jakobbotsch jakobbotsch left a comment

Choose a reason for hiding this comment

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

This looks good to me on a first pass.... I'll probably give it another look over on Friday to try to internalize some of the EH concepts a little bit more.

Copilot AI review requested due to automatic review settings March 18, 2026 17:01
Copy link
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

Adds initial Wasm-specific exception-handling continuation control-flow support in the CoreCLR JIT, making catch-resumption paths explicit in the CFG/IR and extending Wasm control-flow layout + emission to begin representing EH using try_table.

Changes:

  • Introduces fgWasmEhFlow to materialize post-catch resumption dispatch via a control variable, switch blocks, and a Wasm-specific conditional jump marker node.
  • Adds FlowGraphTryRegions and integrates a try-aware + loop-aware RPO visitation, plus interval kinds for Try in Wasm CFG lowering.
  • Extends Wasm emitter/instruction set to support try_table / catch declarations and updates Wasm codegen to emit them.

Reviewed changes

Copilot reviewed 21 out of 21 changed files in this pull request and generated 4 comments.

Show a summary per file
File Description
src/coreclr/jit/registeropswasm.h Adds ExnRef value type for Wasm emission.
src/coreclr/jit/registeropswasm.cpp Adds string name mapping for exnref.
src/coreclr/jit/liveness.cpp Marks GT_WASM_JEXCEPT as side-effecting to prevent removal.
src/coreclr/jit/instrswasm.h Adds Wasm instructions/pseudo-instructions for try_table, catch decls, and throw_ref.
src/coreclr/jit/gtlist.h Introduces Wasm-specific IR nodes (GT_WASM_JEXCEPT, GT_WASM_THROW_REF).
src/coreclr/jit/gentree.h Treats GT_WASM_JEXCEPT as a conditional jump opcode.
src/coreclr/jit/gentree.cpp Hooks new Wasm IR nodes into core GenTree utilities (compare/clone/iterator/exceptions/display).
src/coreclr/jit/flowgraph.cpp Adjusts throw-helper placement for Wasm; adds FlowGraphTryRegions implementation.
src/coreclr/jit/fgwasm.h Extends WasmInterval to include Try kind and related helpers.
src/coreclr/jit/fgwasm.cpp Adds try-aware layout/interval handling and implements fgWasmEhFlow + dispatch block generation.
src/coreclr/jit/fgstmt.cpp Treats GT_WASM_JEXCEPT as control flow.
src/coreclr/jit/fgopt.cpp Fixes TARGET_WASM preprocessor usage for switch assertion.
src/coreclr/jit/emitwasm.h Adds emitter APIs for value-type+immediate instruction descriptors.
src/coreclr/jit/emitwasm.cpp Implements encoding/size/printing for try_table and catch decl pseudo-instructions.
src/coreclr/jit/emitfmtswasm.h Adds new Wasm instruction formats for TRY_TABLE and CATCH_DECL.
src/coreclr/jit/emit.h Adds instrDescValTypeImm and helpers for Wasm emission.
src/coreclr/jit/compphases.h Adds the new PHASE_WASM_EH_FLOW phase.
src/coreclr/jit/compiler.hpp Adds try-aware+loop-aware RPO visitor and try-region block visitation helper.
src/coreclr/jit/compiler.h Declares try-region types and Wasm EH flow entry points.
src/coreclr/jit/compiler.cpp Wires fgWasmEhFlow into the Wasm compilation pipeline before unreachable-block removal.
src/coreclr/jit/codegenwasm.cpp Emits try_table/catch decls on interval start; adds Wasm EH IR handling and no-op catchret codegen.

You can also share your feedback on Copilot code review. Take the survey.

Copy link
Member

@jakobbotsch jakobbotsch left a comment

Choose a reason for hiding this comment

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

LGTM again. Simplification really helps.
A few clarifying questions.

@AndyAyersMS
Copy link
Member Author

/ba-g unrelated build failures

@AndyAyersMS AndyAyersMS merged commit 16c2ce3 into dotnet:main Mar 20, 2026
127 of 135 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

arch-wasm WebAssembly architecture 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.

4 participants