Add a cranelift compile-time feature to wasmtime (#3206)

* Remove unnecessary into_iter/map

Forgotten from a previous refactoring, this variable was already of the
right type!

* Move `wasmtime_jit::Compiler` into `wasmtime`

This `Compiler` struct is mostly a historical artifact at this point and
wasn't necessarily pulling much weight any more. This organization also
doesn't lend itself super well to compiling out `cranelift` when the
`Compiler` here is used for both parallel iteration configuration
settings as well as compilation.

The movement into `wasmtime` is relatively small, with
`Module::build_artifacts` being the main function added here which is a
merging of the previous functions removed from the `wasmtime-jit` crate.

* Add a `cranelift` compile-time feature to `wasmtime`

This commit concludes the saga of refactoring Wasmtime and making
Cranelift an optional dependency by adding a new Cargo feature to the
`wasmtime` crate called `cranelift`, which is enabled by default.

This feature is implemented by having a new cfg for `wasmtime` itself,
`cfg(compiler)`, which is used wherever compilation is necessary. This
bubbles up to disable APIs such as `Module::new`, `Func::new`,
`Engine::precompile_module`, and a number of `Config` methods affecting
compiler configuration. Checks are added to CI that when built in this
mode Wasmtime continues to successfully build. It's hoped that although
this is effectively "sprinkle `#[cfg]` until things compile" this won't
be too too bad to maintain over time since it's also an use case we're
interested in supporting.

With `cranelift` disabled the only way to create a `Module` is with the
`Module::deserialize` method, which requires some form of precompiled
artifact.

Two consequences of this change are:

* `Module::serialize` is also disabled in this mode. The reason for this
  is that serialized modules contain ISA/shared flags encoded in them
  which were used to produce the compiled code. There's no storage for
  this if compilation is disabled. This could probably be re-enabled in
  the future if necessary, but it may not end up being all that necessary.

* Deserialized modules are not checked to ensure that their ISA/shared
  flags are compatible with the host CPU. This is actually already the
  case, though, with normal modules. We'll likely want to fix this in
  the future using a shared implementation for both these locations.

Documentation should be updated to indicate that `cranelift` can be
disabled, although it's not really the most prominent documentation
because this is expected to be a somewhat niche use case (albeit
important, just not too common).

* Always enable cranelift for the C API

* Fix doc example builds

* Fix check tests on GitHub Actions
This commit is contained in:
Alex Crichton
2021-08-18 16:47:47 -05:00
committed by GitHub
parent 7a19b8fe2c
commit ddfadaeb38
22 changed files with 418 additions and 436 deletions

View File

@@ -19,6 +19,7 @@ wasmtime-jit = { path = "../jit", version = "0.29.0" }
wasmtime-cache = { path = "../cache", version = "0.29.0", optional = true }
wasmtime-profiling = { path = "../profiling", version = "0.29.0" }
wasmtime-fiber = { path = "../fiber", version = "0.29.0", optional = true }
wasmtime-cranelift = { path = "../cranelift", version = "0.29.0", optional = true }
target-lexicon = { version = "0.12.0", default-features = false }
wasmparser = "0.80"
anyhow = "1.0.19"
@@ -37,6 +38,7 @@ indexmap = "1.6"
paste = "1.0.3"
psm = "0.1.11"
lazy_static = "1.4"
rayon = { version = "1.0", optional = true }
[target.'cfg(target_os = "windows")'.dependencies]
winapi = "0.3.7"
@@ -50,12 +52,16 @@ wasi-cap-std-sync = { path = "../wasi-common/cap-std-sync" }
maintenance = { status = "actively-developed" }
[features]
default = ['async', 'cache', 'wat', 'jitdump', 'parallel-compilation']
default = ['async', 'cache', 'wat', 'jitdump', 'parallel-compilation', 'cranelift']
# Enables experimental support for the lightbeam codegen backend, an alternative
# to cranelift. Requires Nightly Rust currently, and this is not enabled by
# default.
lightbeam = ["wasmtime-jit/lightbeam"]
# An on-by-default feature enabling runtime compilation of WebAssembly modules
# with the Cranelift compiler. Cranelift is the default compilation backend of
# Wasmtime. If disabled then WebAssembly modules can only be created from
# precompiled WebAssembly modules.
cranelift = ["wasmtime-cranelift"]
# Deprecated, does not actually do anything any more.
lightbeam = []
# Enables support for the `perf` jitdump profiler
jitdump = ["wasmtime-jit/jitdump"]
@@ -63,14 +69,14 @@ jitdump = ["wasmtime-jit/jitdump"]
# Enables support for the `VTune` profiler
vtune = ["wasmtime-jit/vtune"]
# Enables parallel compilation of WebAssembly code
parallel-compilation = ["wasmtime-jit/parallel-compilation"]
# Enables parallel compilation of WebAssembly code.
parallel-compilation = ["rayon"]
# Enables support for automatic cache configuration to be enabled in `Config`.
cache = ["wasmtime-cache"]
# Use the old x86 backend.
old-x86-backend = ["wasmtime-jit/old-x86-backend"]
# Use Cranelift's old x86 backend.
old-x86-backend = ["wasmtime-cranelift/old-x86-backend"]
# Enables support for "async stores" as well as defining host functions as
# `async fn` and calling functions asynchronously.
@@ -79,8 +85,10 @@ async = ["wasmtime-fiber", "wasmtime-runtime/async"]
# Enables userfaultfd support in the runtime's pooling allocator when building on Linux
uffd = ["wasmtime-runtime/uffd"]
# Enables support for all architectures in JIT and the `wasmtime compile` CLI command.
all-arch = ["wasmtime-jit/all-arch"]
# Enables support for all architectures in Cranelift, allowing
# cross-compilation using the `wasmtime` crate's API, notably the
# `Engine::precompile_module` function.
all-arch = ["wasmtime-cranelift/all-arch"]
# Enables trap handling using POSIX signals instead of Mach exceptions on MacOS.
# It is useful for applications that do not bind their own exception ports and