Skip to content

Commit

Permalink
Change how Pulley is defaulted
Browse files Browse the repository at this point in the history
Default to pulley in `build.rs` rather than in `Cargo.toml` to make it
easier to write down the condition and comment what's happening. This
means that the `pulley-interpreter` crate and pulley support in
Cranelift is always compiled in now and cannot be removed. This should
hopefully be ok though as the `pulley-interpreter` crate is still
conditionally used (meaning it can get GC'd) and the code-size of
Cranelift is not as important as the runtime itself.
  • Loading branch information
alexcrichton committed Jan 6, 2025
1 parent c904df2 commit b476a2e
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 38 deletions.
6 changes: 0 additions & 6 deletions crates/cranelift/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -34,12 +34,6 @@ wasmtime-versioned-export-macros = { workspace = true }
itertools = "0.12"
pulley-interpreter = { workspace = true, optional = true }

# On 32-bit platforms where Cranelift currently doesn't have any supported host
# unconditionally enable the `pulley` feature of the Cranelift backend to ensure
# that it's possible to run wasm code by default.
[target.'cfg(target_pointer_width = "32")'.dependencies]
cranelift-codegen = { workspace = true, features = ['pulley'] }

[features]
all-arch = ["cranelift-codegen/all-arch"]
host-arch = ["cranelift-codegen/host-arch"]
Expand Down
23 changes: 10 additions & 13 deletions crates/wasmtime/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -23,15 +23,15 @@ wasmtime-jit-debug = { workspace = true, features = ["gdb_jit_int", "perf_jitdum
wasmtime-jit-icache-coherence = { workspace = true, optional = true }
wasmtime-cache = { workspace = true, optional = true }
wasmtime-fiber = { workspace = true, optional = true }
wasmtime-cranelift = { workspace = true, optional = true }
wasmtime-cranelift = { workspace = true, optional = true, features = ['pulley'] }
wasmtime-winch = { workspace = true, optional = true }
wasmtime-component-macro = { workspace = true, optional = true }
wasmtime-component-util = { workspace = true, optional = true }
wasmtime-slab = { workspace = true, optional = true }
wasmtime-versioned-export-macros = { workspace = true }
wasmtime-wmemcheck = { workspace = true, optional = true }
wasmtime-math = { workspace = true }
pulley-interpreter = { workspace = true, optional = true }
pulley-interpreter = { workspace = true }
target-lexicon = { workspace = true }
wasmparser = { workspace = true }
wasm-encoder = { workspace = true, optional = true }
Expand Down Expand Up @@ -98,14 +98,6 @@ rustix = { workspace = true, optional = true }
[target.'cfg(target_arch = "s390x")'.dependencies]
psm = { workspace = true, optional = true }

# On 32-bit platforms where Cranelift currently doesn't have any supported host
# unconditionally pull in the `pulley-interpreter` crate. Runtime support is
# enabled via `build.rs` which prints `feature="pulley"` by default here and
# compilation support, if enabled via the `cranelift` feature, is handled in the
# `wasmtime-cranelift` crate enabling Pulley on 32-bit platforms.
[target.'cfg(target_pointer_width = "32")'.dependencies]
pulley-interpreter = { workspace = true }

[dev-dependencies]
env_logger = { workspace = true }
proptest = { workspace = true }
Expand Down Expand Up @@ -165,7 +157,12 @@ winch = ["dep:wasmtime-winch", "std"]
# targets will be available. When paired with the `runtime` feature, the Pulley
# interpreter will be built into the runtime and you can interpret WebAssembly
# modules that have been compiled to Pulley bytecode.
pulley = ["dep:pulley-interpreter", "wasmtime-cranelift?/pulley"]
pulley = [
# Note that this is intentionally empty. This feature is dynamically activated
# in `build.rs` as well when the host platform does not have Cranelift support
# for example. That means that dependencies for pulley need to be already
# activated anyway.
]

# Enables support for incremental compilation cache to be enabled in `Config`.
incremental-cache = ["wasmtime-cranelift?/incremental-cache", "std"]
Expand Down Expand Up @@ -257,7 +254,7 @@ runtime = [
"dep:psm",
"dep:rustix",
"rustix/mm",
"pulley-interpreter?/interp",
"pulley-interpreter/interp",
]

# Enable support for garbage collection-related things.
Expand Down Expand Up @@ -325,7 +322,7 @@ std = [
'object/std',
'once_cell',
'wasmtime-fiber?/std',
'pulley-interpreter?/std',
'pulley-interpreter/std',
'wasmtime-math/std',
# technically this isn't necessary but once you have the standard library you
# probably want things to go fast in which case you've probably got signal
Expand Down
27 changes: 19 additions & 8 deletions crates/wasmtime/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,26 @@ fn main() {
#[cfg(feature = "runtime")]
build_c_helpers();

// Flag pulley as enabled unconditionally on 32-bit targets to ensure that
// wasm is runnable by default like it is on other 64-bit native platforms.
// Note that this doesn't actually enable the Cargo feature, it just changes
// the cfg's passed to the crate, so for example care is still taken in
// `Cargo.toml` to handle pulley-specific dependencies on 32-bit platforms.
let target_pointer_width = std::env::var("CARGO_CFG_TARGET_POINTER_WIDTH").unwrap();
if target_pointer_width == "32" {
println!("cargo:rustc-cfg=feature=\"pulley\"");
// Determine whether Pulley will be enabled and used for this target.
match std::env::var("CARGO_CFG_TARGET_ARCH").unwrap().as_str() {
// These targets use Cranelift by default as they have backends in
// Cranelift. Pulley can still be used on an opt-in basis, but it's not
// otherwise explicitly enabled here.
"x86_64" | "riscv64" | "s390x" | "aarch64" => {}

// All other targets at this time use Pulley by default. That means
// that the pulley feature is "enabled" here and the default target is
// pulley. Note that by enabling the feature here it doesn't actually
// enable the Cargo feature, it just passes a cfg to rustc. That means
// that conditional dependencies enabled in `Cargo.toml` (or other
// features) by `pulley` aren't activated, which is why the `pulley`
// feature of this crate depends on nothing else.
_ => {
println!("cargo:rustc-cfg=feature=\"pulley\"");
println!("cargo:rustc-cfg=default_target_pulley");
}
}
println!("cargo:rustc-check-cfg=cfg(default_target_pulley)");
}

#[cfg(feature = "runtime")]
Expand Down
14 changes: 3 additions & 11 deletions crates/wasmtime/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2059,17 +2059,9 @@ impl Config {
return target;
}

// Without an explicitly configured target the goal is then to select
// some default which can reasonably run code on this host. If pulley is
// enabled and the host has no support at all in the cranelift/winch
// backends then pulley becomes the default target. This means, for
// example, that 32-bit platforms will default to running pulley at this
// time.
let any_compiler_support = cfg!(target_arch = "x86_64")
|| cfg!(target_arch = "aarch64")
|| cfg!(target_arch = "riscv64")
|| cfg!(target_arch = "s390x");
if !any_compiler_support && cfg!(feature = "pulley") {
// If the `build.rs` script determined that this platform uses pulley by
// default, then use Pulley.
if cfg!(default_target_pulley) {
return target_lexicon::Triple::pulley_host();
}

Expand Down

0 comments on commit b476a2e

Please sign in to comment.