Wasmtime: Explanation of scope

Created on 29 Feb 2020  路  1Comment  路  Source: bytecodealliance/wasmtime

The readme says that cretonne is supposed to be a code generator for WebAssembly, but it's a bit unclear what that means, seeing how there doesn't appear to be anything to actually do with wasm in the code.

  • I heard that cretonne is meant to have inter-procedural optimizations as another layer built over top. Is this true, and always going to be the case?
  • Does another tool to translate wasm to cretonne IR need to be built in order to generate code for wasm?
  • Is it in scope for cretonne to output to wasm, or is it only intended to output to native code? (e.g. will I be able to use cretonne as a pure-rust binaryen alternative?)
  • Is cretonne meant to be useful in both JIT and AOT configurations?

Preferably, the readme or documentation should be able to answer these questions.

cranelift documentation

Most helpful comment

Cretonne is still in development, so big pieces are still missing.

Inter-procedural optimizations

Cretonne is meant to be a part of a larger compiler or JIT that prioritizes fast compilation times. In particular, it must be possible to parallelize compilation to make use of multiple cores. This is achieved by making a function in Cretonne IR an independent data structure without external references. When a Cretonne function refers to a global variable or calls another function, it uses the _name_ of the external object instead of a pointer.

A later linking phase depends on the framework embedding Cretonne to resolve these references. Cretonne won't have the global data structures needed to do that itself.

Inter-procedural optimizations necessarily need to work with multiple functions at once, and so they need a closer interaction with the larger compiler framework and how it does concurrency. I consider this out of scope for Cretonne, at least initially.

Cretonne could provide useful functions for an IPO framework, for example "inline this function at this call site".

Translating WebAssembly to Cretonne IR

This hasn't been built yet, by I expect there will be a lib/wasm crate which can translate binary WebAssembly to Cretonne IR. Again, since Cretonne is supposed to support concurrent compilation without imposing a way of managing threads, this crate should support translating wasm functions individually.

Translating Cretonne IR to WebAssembly

This is an interesting idea that I didn't consider initially, but I think it could work.

A subset of the Cretonne instruction set is equivalent to WebAssembly and can be translated easily. A larger subset of the instructions could be transformed into wasm-equivalent instructions using the legalizer framework.

Some instructions are too low-level to be translated to WebAssembly. This includes raw loads and stores (as opposed to heap_load and heap_store) and things like taking the address of a stack slot. When LLVM is translating C/C++ to WebAssembly, it deals with these things by sandboxing. It maps loads and stores to the wasm heap and moves stacks objects to a shadow stack in the heap if their address escapes.

JIT vs AOT compilation

I see three tiers on the AOT-JIT scale:

  1. AOT-compile a program to generate an executable ELF or similar that can be executed later.
  2. Compile an entire WebAssembly module before running its main function.
  3. JIT-compile individual functions on demand just before calling them. Use inline caches and other speculative optimizations that may cause deoptimization.

Cretonne is initially targeting 2. as a second-tier WebAssembly compiler for Firefox. I imagine its scope expanding in both directions.

>All comments

Cretonne is still in development, so big pieces are still missing.

Inter-procedural optimizations

Cretonne is meant to be a part of a larger compiler or JIT that prioritizes fast compilation times. In particular, it must be possible to parallelize compilation to make use of multiple cores. This is achieved by making a function in Cretonne IR an independent data structure without external references. When a Cretonne function refers to a global variable or calls another function, it uses the _name_ of the external object instead of a pointer.

A later linking phase depends on the framework embedding Cretonne to resolve these references. Cretonne won't have the global data structures needed to do that itself.

Inter-procedural optimizations necessarily need to work with multiple functions at once, and so they need a closer interaction with the larger compiler framework and how it does concurrency. I consider this out of scope for Cretonne, at least initially.

Cretonne could provide useful functions for an IPO framework, for example "inline this function at this call site".

Translating WebAssembly to Cretonne IR

This hasn't been built yet, by I expect there will be a lib/wasm crate which can translate binary WebAssembly to Cretonne IR. Again, since Cretonne is supposed to support concurrent compilation without imposing a way of managing threads, this crate should support translating wasm functions individually.

Translating Cretonne IR to WebAssembly

This is an interesting idea that I didn't consider initially, but I think it could work.

A subset of the Cretonne instruction set is equivalent to WebAssembly and can be translated easily. A larger subset of the instructions could be transformed into wasm-equivalent instructions using the legalizer framework.

Some instructions are too low-level to be translated to WebAssembly. This includes raw loads and stores (as opposed to heap_load and heap_store) and things like taking the address of a stack slot. When LLVM is translating C/C++ to WebAssembly, it deals with these things by sandboxing. It maps loads and stores to the wasm heap and moves stacks objects to a shadow stack in the heap if their address escapes.

JIT vs AOT compilation

I see three tiers on the AOT-JIT scale:

  1. AOT-compile a program to generate an executable ELF or similar that can be executed later.
  2. Compile an entire WebAssembly module before running its main function.
  3. JIT-compile individual functions on demand just before calling them. Use inline caches and other speculative optimizations that may cause deoptimization.

Cretonne is initially targeting 2. as a second-tier WebAssembly compiler for Firefox. I imagine its scope expanding in both directions.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

ckaran picture ckaran  路  5Comments

moderation picture moderation  路  4Comments

daubaris picture daubaris  路  3Comments

itowlson picture itowlson  路  5Comments

kubkon picture kubkon  路  5Comments