Skip to content

winch: catch exceptions - #14180

Open
macovedj wants to merge 2 commits into
bytecodealliance:mainfrom
macovedj:winch-catch-exceptions
Open

winch: catch exceptions#14180
macovedj wants to merge 2 commits into
bytecodealliance:mainfrom
macovedj:winch-catch-exceptions

Conversation

@macovedj

Copy link
Copy Markdown
Contributor

Adds support for catching exceptions in Winch.
Winch tracks exception handlers in scope, records them in call-site metadata, and emits landing pads when a try_table ends. When Wasmtime’s unwinder selects a handler, its landing pad restores the expected stack state and VMContext.
Support for catch_ref and catch_all_ref is not yet implemented and will be added in follow-up work

@macovedj
macovedj requested review from a team as code owners August 20, 2026 15:29
@macovedj
macovedj requested review from cfallin and removed request for a team August 20, 2026 15:29
@github-actions github-actions Bot added cranelift Issues related to the Cranelift code generator winch Winch issues or pull requests labels Aug 20, 2026
@github-actions

Copy link
Copy Markdown

Subscribe to Label Action

cc @saulecabrera

Details This issue or pull request has been labeled: "cranelift", "winch"

Thus the following users have been cc'd because of the following labels:

  • saulecabrera: winch

To subscribe or unsubscribe from this label, edit the .github/subscribe-to-label.json configuration file.

Learn more.

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

Thanks -- this looks pretty much OK to me (and the handler stack with checkpoints appears to be using the same approach that we have in the Wasm-to-CLIF translator, which is good). A few questions around the libcalls/barriers and register management below. It might also be good to get a second pair of eyes (@saulecabrera maybe?) to ensure that all of that is done properly.

Comment thread winch/codegen/src/codegen/exceptions.rs Outdated
for (field_ty, field_offset) in fields {
let field_base = match object_addr {
Some(reg) => reg,
None => {

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.

It seems that we have logic to re-compute object_addr (field_base) every iteration if invalidated. A few thoughts:

  • Do we need to initialize object_addr above (line 178) if we're going to lazily recompute it if None in each iteration? In other words would None as an initializer work?
  • Zooming out, why are we recomputing it? It seems that below we set it to None when a call (DRC barrier) clobbers registers, but we save the other registers; why do we need to re-derive object_addr from scratch? Does the read barrier have the option to relocate the heap or similar?
  • All of these manual register save/restore sequences make me a little squeamish. Do we not have an abstraction to save active registers and restore them around calls?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Thanks for pointing this out. On revisiting it, I realized the recomputation was there because the barrier was responsible for freeing the storage_base register. I changed the barrier to operate on an already-loaded GC reference, leaving responsibility for freeing the storage_base with the caller. This lets the exception path preserve the already-computed object_addr rather than recomputing it.

I'm not aware of a mechanism available other than the value stack for managing the registers, but these changes simplify the sequence by preserving the already-computed object_addr instead of preserving exception_reg and later reloading the heap metadata to reconstruct the address. If @saulecabrera has any thoughts about some potential helpers that could assist, I'm open to suggestions. Otherwise, the simplified barrier contract may be sufficient.

@saulecabrera

Copy link
Copy Markdown
Member

It might also be good to get a second pair of eyes (@saulecabrera maybe?) to ensure that all of that is done properly.

I will take a look; thanks!

@macovedj
macovedj force-pushed the winch-catch-exceptions branch from 11521d7 to 1c6f09c Compare August 21, 2026 09:13
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

cranelift Issues related to the Cranelift code generator winch Winch issues or pull requests

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants