From c2ecb2c220d6fe4f57095b07b5947850219751d7 Mon Sep 17 00:00:00 2001 From: Chris Fallin Date: Mon, 25 Aug 2025 11:23:44 -0700 Subject: [PATCH 1/3] Revert "Cranelift/Wasmtime: disable fastalloc (single-pass) allocator for now. (#10554)" This reverts commit d52e23b09191185996792b8ef18e5fca2865ca43. --- cranelift/codegen/meta/src/shared/settings.rs | 8 +++----- cranelift/codegen/src/machinst/compile.rs | 3 +-- crates/cli-flags/src/lib.rs | 1 + crates/cli-flags/src/opt.rs | 2 ++ crates/fuzzing/src/generators/config.rs | 8 +------- crates/wasmtime/src/config.rs | 14 +++++++++++++- tests/all/cli_tests.rs | 4 ++++ tests/all/stack_overflow.rs | 1 + 8 files changed, 26 insertions(+), 15 deletions(-) diff --git a/cranelift/codegen/meta/src/shared/settings.rs b/cranelift/codegen/meta/src/shared/settings.rs index ff0a0e0700bc..e7ea9af00961 100644 --- a/cranelift/codegen/meta/src/shared/settings.rs +++ b/cranelift/codegen/meta/src/shared/settings.rs @@ -38,12 +38,10 @@ pub(crate) fn define() -> SettingGroup { - `backtracking`: A backtracking allocator with range splitting; more expensive but generates better code. - - Note that the `single_pass` option is currently disabled because it does not - have adequate support for the kinds of allocations required by exception - handling (https://github.com/bytecodealliance/regalloc2/issues/217). + - `single_pass`: A single-pass algorithm that yields quick compilation but + results in code with more register spills and moves. "#, - vec!["backtracking"], + vec!["backtracking", "single_pass"], ); settings.add_enum( diff --git a/cranelift/codegen/src/machinst/compile.rs b/cranelift/codegen/src/machinst/compile.rs index 507ac126a3d1..cd2e7e9d6ac5 100644 --- a/cranelift/codegen/src/machinst/compile.rs +++ b/cranelift/codegen/src/machinst/compile.rs @@ -66,8 +66,7 @@ pub fn compile( options.algorithm = match b.flags().regalloc_algorithm() { RegallocAlgorithm::Backtracking => Algorithm::Ion, - // Note: single-pass is currently disabled - // (https://github.com/bytecodealliance/regalloc2/issues/217). + RegallocAlgorithm::SinglePass => Algorithm::Fastalloc, }; regalloc2::run(&vcode, vcode.abi.machine_env(), &options) diff --git a/crates/cli-flags/src/lib.rs b/crates/cli-flags/src/lib.rs index 90f571c3627e..f0cea981c550 100644 --- a/crates/cli-flags/src/lib.rs +++ b/crates/cli-flags/src/lib.rs @@ -1134,6 +1134,7 @@ mod tests { // Regalloc algorithm for (regalloc_value, expected) in [ ("\"backtracking\"", Some(RegallocAlgorithm::Backtracking)), + ("\"single-pass\"", Some(RegallocAlgorithm::SinglePass)), ("\"hello\"", None), // should fail ("3", None), // should fail ("true", None), // should fail diff --git a/crates/cli-flags/src/opt.rs b/crates/cli-flags/src/opt.rs index 9de6a9d8f66e..2a19f32a590e 100644 --- a/crates/cli-flags/src/opt.rs +++ b/crates/cli-flags/src/opt.rs @@ -485,6 +485,7 @@ impl WasmtimeOptionValue for wasmtime::RegallocAlgorithm { fn parse(val: Option<&str>) -> Result { match String::parse(val)?.as_str() { "backtracking" => Ok(wasmtime::RegallocAlgorithm::Backtracking), + "single-pass" => Ok(wasmtime::RegallocAlgorithm::SinglePass), other => bail!( "unknown regalloc algorithm`{}`, only backtracking,single-pass accepted", other @@ -495,6 +496,7 @@ impl WasmtimeOptionValue for wasmtime::RegallocAlgorithm { fn display(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { match *self { wasmtime::RegallocAlgorithm::Backtracking => f.write_str("backtracking"), + wasmtime::RegallocAlgorithm::SinglePass => f.write_str("single-pass"), _ => unreachable!(), } } diff --git a/crates/fuzzing/src/generators/config.rs b/crates/fuzzing/src/generators/config.rs index 72b9c0fc4630..cf780a8b67b0 100644 --- a/crates/fuzzing/src/generators/config.rs +++ b/crates/fuzzing/src/generators/config.rs @@ -840,13 +840,7 @@ impl RegallocAlgorithm { fn to_wasmtime(&self) -> wasmtime::RegallocAlgorithm { match self { RegallocAlgorithm::Backtracking => wasmtime::RegallocAlgorithm::Backtracking, - // Note: we have disabled `single_pass` for now because of - // its limitations w.r.t. exception handling - // (https://github.com/bytecodealliance/regalloc2/issues/217). To - // avoid breaking all existing fuzzbugs by changing the - // `arbitrary` mappings, we keep the `RegallocAlgorithm` - // enum as it is and remap here to `Backtracking`. - RegallocAlgorithm::SinglePass => wasmtime::RegallocAlgorithm::Backtracking, + RegallocAlgorithm::SinglePass => wasmtime::RegallocAlgorithm::SinglePass, } } } diff --git a/crates/wasmtime/src/config.rs b/crates/wasmtime/src/config.rs index f85504142a6e..a96c7f808d12 100644 --- a/crates/wasmtime/src/config.rs +++ b/crates/wasmtime/src/config.rs @@ -279,9 +279,10 @@ impl Config { // When running under MIRI try to optimize for compile time of wasm // code itself as much as possible. Disable optimizations by - // default. + // default and use the fastest regalloc available to us. if cfg!(miri) { ret.cranelift_opt_level(OptLevel::None); + ret.cranelift_regalloc_algorithm(RegallocAlgorithm::SinglePass); } } @@ -1299,6 +1300,7 @@ impl Config { pub fn cranelift_regalloc_algorithm(&mut self, algo: RegallocAlgorithm) -> &mut Self { let val = match algo { RegallocAlgorithm::Backtracking => "backtracking", + RegallocAlgorithm::SinglePass => "single_pass", }; self.compiler_config .settings @@ -2934,6 +2936,16 @@ pub enum RegallocAlgorithm { /// results in better register utilization, producing fewer spills /// and moves, but can cause super-linear compile runtime. Backtracking, + /// Generates acceptable code very quickly. + /// + /// This algorithm performs a single pass through the code, + /// guaranteed to work in linear time. (Note that the rest of + /// Cranelift is not necessarily guaranteed to run in linear time, + /// however.) It cannot undo earlier decisions, however, and it + /// cannot foresee constraints or issues that may occur further + /// ahead in the code, so the code may have more spills and moves as + /// a result. + SinglePass, } /// Select which profiling technique to support. diff --git a/tests/all/cli_tests.rs b/tests/all/cli_tests.rs index 81b8947ecf5f..24b755569139 100644 --- a/tests/all/cli_tests.rs +++ b/tests/all/cli_tests.rs @@ -2239,11 +2239,15 @@ fn config_cli_flag() -> Result<()> { br#" [optimize] opt-level = 2 + regalloc-algorithm = "single-pass" signals-based-traps = false [codegen] collector = "null" + [debug] + debug-info = true + [wasm] max-wasm-stack = 65536 diff --git a/tests/all/stack_overflow.rs b/tests/all/stack_overflow.rs index 40296916c43f..06381622fb72 100644 --- a/tests/all/stack_overflow.rs +++ b/tests/all/stack_overflow.rs @@ -152,6 +152,7 @@ fn big_stack_works_ok(config: &mut Config) -> Result<()> { // Disable cranelift optimizations to ensure that this test doesn't take too // long in debug mode due to the large size of its code. config.cranelift_opt_level(OptLevel::None); + config.cranelift_regalloc_algorithm(RegallocAlgorithm::SinglePass); let engine = Engine::new(config)?; let mut store = Store::new(&engine, ()); From a5841d685b3490d0a2317e33a0468f286fdcda83 Mon Sep 17 00:00:00 2001 From: Chris Fallin Date: Mon, 25 Aug 2025 11:44:36 -0700 Subject: [PATCH 2/3] Upgrade to regalloc2 0.13.1. Pulls in bytecodealliance/regalloc2#233 to update fastalloc to support the looser constraints needed by exception-related changes. --- Cargo.lock | 4 ++-- Cargo.toml | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index d79ac1d110a3..ca01a7d598d1 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -2995,9 +2995,9 @@ dependencies = [ [[package]] name = "regalloc2" -version = "0.12.2" +version = "0.13.1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "5216b1837de2149f8bc8e6d5f88a9326b63b8c836ed58ce4a0a29ec736a59734" +checksum = "68e18e1ef763167dc6718c28a5585e62f907590a21028b8e87be1318f19ef1cb" dependencies = [ "allocator-api2", "bumpalo", diff --git a/Cargo.toml b/Cargo.toml index bfdce00b0668..e31b38b3dce5 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -309,7 +309,7 @@ component-async-tests = { path = "crates/misc/component-async-tests" } # Bytecode Alliance maintained dependencies: # --------------------------- -regalloc2 = "0.12.2" +regalloc2 = "0.13.1" # cap-std family: target-lexicon = "0.13.0" From 22727034dc7e185c6a935a661ad15d873fad81d8 Mon Sep 17 00:00:00 2001 From: Chris Fallin Date: Mon, 25 Aug 2025 11:54:31 -0700 Subject: [PATCH 3/3] cargo-vet update. --- supply-chain/imports.lock | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/supply-chain/imports.lock b/supply-chain/imports.lock index 5bba1dcf79ee..ebe895f5b23d 100644 --- a/supply-chain/imports.lock +++ b/supply-chain/imports.lock @@ -788,8 +788,8 @@ user-login = "dtolnay" user-name = "David Tolnay" [[publisher.regalloc2]] -version = "0.12.2" -when = "2025-05-07" +version = "0.13.1" +when = "2025-08-25" user-id = 3726 user-login = "cfallin" user-name = "Chris Fallin"