Skip to content

A lightweight WebAssembly runtime that is fast, secure, and standards-compliant

Cranelift: update regalloc2 to 0.15.0 to permit more VRegs. (#12611)

Chris Fallinb5d2ff5d08b6
diff --git a/Cargo.lock b/Cargo.lock
index be667f8883..b70d214a66 100644
--- a/Cargo.lock
+++ b/Cargo.lock
@@ -3005,9 +3005,9 @@ dependencies = [
[[package]]
name = "regalloc2"
-version = "0.14.0"
+version = "0.15.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
-checksum = "d56c86aa200e0d597422a1099f82b38577ec52c660f32d3af3a9c4fd4f2891f2"
+checksum = "952ddbfc6f9f64d006c3efd8c9851a6ba2f2b944ba94730db255d55006e0ffda"
dependencies = [
"allocator-api2",
"bumpalo",
diff --git a/Cargo.toml b/Cargo.toml
index 30c846c9a8..29205a84b7 100644
--- a/Cargo.toml
+++ b/Cargo.toml
@@ -322,7 +322,7 @@ component-async-tests = { path = "crates/misc/component-async-tests" }
# Bytecode Alliance maintained dependencies:
# ---------------------------
-regalloc2 = "0.14.0"
+regalloc2 = "0.15.0"
wasip1 = { version = "1.0.0", default-features = false }
# cap-std family:
diff --git a/cranelift/codegen/src/machinst/reg.rs b/cranelift/codegen/src/machinst/reg.rs
index f269e24ca6..0c004483cc 100644
--- a/cranelift/codegen/src/machinst/reg.rs
+++ b/cranelift/codegen/src/machinst/reg.rs
@@ -54,12 +54,28 @@ const REG_SPILLSLOT_MASK: u32 = !REG_SPILLSLOT_BIT;
impl Reg {
/// Const constructor: create a new Reg from a regalloc2 VReg.
pub const fn from_virtual_reg(vreg: regalloc2::VReg) -> Reg {
- Reg(vreg.bits() as u32)
+ let bits = vreg.bits() as u32;
+ debug_assert!(bits <= REG_SPILLSLOT_MASK);
+ Reg(bits)
}
/// Const constructor: create a new Reg from a regalloc2 PReg.
pub const fn from_real_reg(preg: regalloc2::PReg) -> Reg {
- Reg(preg_to_pinned_vreg(preg).bits() as u32)
+ let vreg = preg_to_pinned_vreg(preg);
+ let bits = vreg.bits() as u32;
+ debug_assert!(bits <= REG_SPILLSLOT_MASK);
+ Reg(bits)
+ }
+
+ /// Maybe construct from a `regalloc2::VReg`, checking if the
+ /// index is in-range for our bit-packing.
+ pub fn from_virtual_reg_checked(vreg: regalloc2::VReg) -> Option<Reg> {
+ let bits = vreg.bits() as u32;
+ if bits <= REG_SPILLSLOT_MASK {
+ Some(Reg(bits))
+ } else {
+ None
+ }
}
/// Get the physical register (`RealReg`), if this register is
diff --git a/cranelift/codegen/src/machinst/vcode.rs b/cranelift/codegen/src/machinst/vcode.rs
index b3d0a64193..2e8120a199 100644
--- a/cranelift/codegen/src/machinst/vcode.rs
+++ b/cranelift/codegen/src/machinst/vcode.rs
@@ -1780,13 +1780,23 @@ impl<I: VCodeInst> VRegAllocator<I> {
}
let v = self.vreg_types.len();
let (regclasses, tys) = I::rc_for_type(ty)?;
- if v + regclasses.len() >= VReg::MAX {
+
+ // Check that new indices are in-bounds for regalloc2's
+ // VReg/Operand representation.
+ if v + regclasses.len() > VReg::MAX {
return Err(CodegenError::CodeTooLarge);
}
+ // Check that new indices are in-bounds for our Reg
+ // bit-packing on top of the RA2 types, which represents
+ // spillslots as well.
+ let check = |vreg: regalloc2::VReg| -> CodegenResult<Reg> {
+ Reg::from_virtual_reg_checked(vreg).ok_or(CodegenError::CodeTooLarge)
+ };
+
let regs: ValueRegs<Reg> = match regclasses {
- &[rc0] => ValueRegs::one(VReg::new(v, rc0).into()),
- &[rc0, rc1] => ValueRegs::two(VReg::new(v, rc0).into(), VReg::new(v + 1, rc1).into()),
+ &[rc0] => ValueRegs::one(check(VReg::new(v, rc0))?),
+ &[rc0, rc1] => ValueRegs::two(check(VReg::new(v, rc0))?, check(VReg::new(v + 1, rc1))?),
// We can extend this if/when we support 32-bit targets; e.g.,
// an i128 on a 32-bit machine will need up to four machine regs
// for a `Value`.
diff --git a/supply-chain/imports.lock b/supply-chain/imports.lock
index 7b0fcfd708..d3368a31a0 100644
--- a/supply-chain/imports.lock
+++ b/supply-chain/imports.lock
@@ -1112,8 +1112,8 @@ user-login = "dtolnay"
user-name = "David Tolnay"
[[publisher.regalloc2]]
-version = "0.14.0"
-when = "2026-02-14"
+version = "0.15.0"
+when = "2026-02-17"
user-id = 3726
user-login = "cfallin"
user-name = "Chris Fallin"
diff --git a/tests/all/code_too_large.rs b/tests/all/code_too_large.rs
deleted file mode 100644
index fac1ac6e12..0000000000
--- a/tests/all/code_too_large.rs
+++ /dev/null
@@ -1,69 +0,0 @@
-#![cfg(not(miri))]
-
-use wasm_encoder::Instruction as I;
-use wasmtime::*;
-
-#[test]
-fn code_too_large_without_panic() -> Result<()> {
- // This takes 1m+ in ASAN and isn't too useful to test in ASAN.
- if cfg!(asan) {
- return Ok(());
- }
-
- const N: usize = 80000;
-
- // Build a module with a function whose body will allocate too many
- // temporaries for our current (Cranelift-based) compiler backend to
- // handle. This test ensures that we propagate the failure upward
- // and return it programmatically, rather than panic'ing. If we ever
- // improve our compiler backend to actually handle such a large
- // function body, we'll need to increase the limits here too!
- let mut module = wasm_encoder::Module::default();
-
- let mut types = wasm_encoder::TypeSection::new();
- types.ty().function([], [wasm_encoder::ValType::I32]);
- module.section(&types);
-
- let mut funcs = wasm_encoder::FunctionSection::new();
- funcs.function(0);
- module.section(&funcs);
-
- let mut tables = wasm_encoder::TableSection::new();
- tables.table(wasm_encoder::TableType {
- element_type: wasm_encoder::RefType::FUNCREF,
- table64: false,
- minimum: 1,
- maximum: Some(1),
- shared: false,
- });
- module.section(&tables);
-
- let mut exports = wasm_encoder::ExportSection::new();
- exports.export("", wasm_encoder::ExportKind::Func, 0);
- module.section(&exports);
-
- let mut func = wasm_encoder::Function::new([]);
- func.instruction(&I::I32Const(0));
- for _ in 0..N {
- func.instruction(&I::TableGet(0));
- func.instruction(&I::RefIsNull);
- }
- func.instruction(&I::End);
- let mut code = wasm_encoder::CodeSection::new();
- code.function(&func);
- module.section(&code);
-
- let mut config = Config::new();
- config.cranelift_opt_level(OptLevel::None);
- let engine = Engine::new(&config)?;
-
- let store = Store::new(&engine, ());
- let result = Module::new(store.engine(), &module.finish());
- match result {
- Err(e) => {
- assert!(format!("{e:?}").contains("Compilation error: Code for function is too large"))
- }
- Ok(_) => panic!("Please adjust limits to make the module too large to compile!"),
- }
- Ok(())
-}
diff --git a/tests/all/main.rs b/tests/all/main.rs
index e7c33138e6..abbf9be0a6 100644
--- a/tests/all/main.rs
+++ b/tests/all/main.rs
@@ -6,7 +6,6 @@ mod arrays;
mod async_functions;
mod call_hook;
mod cli_tests;
-mod code_too_large;
mod compile_time_builtins;
mod component_model;
mod coredump;