binaryen icon indicating copy to clipboard operation
binaryen copied to clipboard

Lower WASM exceptions to MVP (or maybe GC)

Open gkgoat1 opened this issue 1 year ago • 4 comments

Many C++ libraries require exceptions, like libriscv and Binaryen itself. Compiling them to WASI would work (as wasi-sdk, iirc, supports WASM exceptions), but some WASM parsers (like my fork of waffle) and WASM compilers (like w2c2) do not.

For compatibility with those compilers and parsers, we would need a WASM exception lowering pass. Generically, the only sound option is lowering to GC, however MVP lowering might be required for some usages, where we would redirect exception data to either a seperate memory or an unused segment of the existing one.

As one of the few WASM analysis tools that support exceptions, Binaryen seems fit to contain some exception lowering pass, even if it would be unnecessary for most pure-Binaryen workloads.

gkgoat1 avatar Dec 24 '24 22:12 gkgoat1

So far this hasn't come up because e.g. C++ users can recompile to switch between wasm exceptions and not, but if there are toolchains that only emit exceptions then I agree a lowering pass could make sense here. A contribution of such a pass sounds good in general, as Binaryen does have lowering passes for several other features.

kripken avatar Dec 30 '24 16:12 kripken

Is there a lowering pass for the multi-value extension? I guess lowering extensions like reference types or GC are impossible?

turbolent avatar Feb 05 '25 19:02 turbolent

Is there a lowering pass for the multi-value extension? I guess lowering extensions like reference types or GC are impossible?

There have been attempts to lower GC in Binaryen. Multi-value is probably also lowerable, but no one has made an attempt yet. (same as exceptions)

gkgoat1 avatar Feb 05 '25 19:02 gkgoat1

There isn't a full multivalue lowering pass, but TupleOptimization will remove unneeded uses of multivalue inside functions. That leaves multivalue uses between functions, multivalue for exceptions, and a few other things.

Lowering GC is possible, but it would just be a lot of work.

kripken avatar Feb 05 '25 20:02 kripken