cfallin / waffle

Apache License 2.0
56 stars 5 forks source link

WAFFLE: Wasm Analysis Framework for Lightweight Experimentation

Synopsis: an SSA IR compiler framework for Wasm-to-Wasm transforms, in Rust.

Status: working for Wasm MVP; roundtrips complex modules successfully

The transforms from Wasm to IR and from IR to Wasm work well, and has been fuzzed in various ways. In particular, waffle is fuzzed by roundtripping Wasm through SSA IR and back, and differentially executing the original and roundtripped Wasm under Wasmtime (with limits on execution time). At this time, no correctness bugs have been found.

Waffle is able to roundtrip (convert to IR, then compile back to Wasm) complex modules such as the SpiderMonkey JS engine compiled to Wasm.

Waffle has some basic mid-end optimizations working, such as GVN and constant propagation. Much more could be done on this.

There are various ways in which the generated Wasm bytecode could be improved; work is ongoing on this.

Architecture

The IR is a CFG of blocks, containing operators that correspond 1-to-1 to Wasm operators. Dataflow is via SSA, and blocks have blockparams (rather than phi-nodes). Wasm locals are not used in the IR (they are converted to SSA).

The frontend converts Wasm into this IR by building SSA as it goes, inserting blockparams when it discovers multiple reaching definitions for a local. Multivalue Wasm (parameters and results for every control-flow block) is fully supported, and converted to SSA. This process more or less works like Cranelift's does, except that memory, table, etc. operations remain at the Wasm abstraction layer (are not lowered into implementation details), and arithmetic operators mirror Wasm's exactly.

The backend operates in three stages:

Comparisons / Related Work