News

Cranelift library tackles WebAssembly execution

Mozilla and Fastly Labs have led development of the Cranelift open source library, which translates functions for the WebAssembly portable code format for the web into native machine-code functions, for efficient execution. Cranelift can also be used for running WebAssembly code outside the web. Cranelift is being built under the auspices of Crane Station, the Crane Compiler Organization.

Cranelift reads WebAssembly as input and writes native machine code as output. The library also can ingest its own Crane IR (intermediate representation), providing a broader feature set. With Cranelift, functions are converted into SSA (static single assignment) form and optimizations are performed before being converted into machine code, which can be packaged into native object files (.o files) or allocated in memory and executed directly as a just-in-time (JIT).

The library is still in development, with its APIs not yet stable and platform support still being built. But it does have enough functionality to run programs such as executing WebAssembly MVP (minimum viable product) functions, if used with an external WebAssembly embedding as part of a complete WebAssembly implementation.

Cranelift’s developers say the library is similar to the LLVM compiler project in that both have textual and in-memory forms of their intermediate representation and can cross-compile by default without rebuilding the code generator. Differences include the Cranelift intermediate representation being at a slightly lower level of abstraction, for use all the way during the code-gen process. The Cranelift IR is less friendly for midlevel optimizations but it currently does not perform any. Also, LLVM has a small, well-defined instruction set and a large number of intrinsics while Cranelift has a larger instruction set and no intrinsics.

Work is under way to integrate Cranelift into Mozilla’s Firefox browser. It might be used to generate native code for the Rust compiler as well. Its builders see Cranelift as a library for large products rather as a user-facing product on its own. Work is also under way on an embeddable, lightweight nonbrowser VM for WebAssembly called Wasmtime, which uses Cranelift for compilation and runs WebAssembly outside the web. Wasmtime can be used as a command-line utility or as a library embedded in bigger application.

The most-mature Cranelift-based product is in Fastly’s Terrarium, a multilanguage, browser-based editor and deployment platform launched in late November 2018 that uses Cranelift in its compiler and server.