A lightweight WebAssembly runtime that is fast, secure, and standards-compliant
Cranelift: reuse the regalloc2::Ctx across function compilations (#13912)
Nick Fitzgeraldffee9b1c4ff0
diff --git a/Cargo.lock b/Cargo.lockindex eebb1a434b..489dc0d49c 100644--- a/Cargo.lock+++ b/Cargo.lock@@ -3169,9 +3169,9 @@ dependencies = [[[package]]name = "regalloc2"-version = "0.15.1"+version = "0.15.2"source = "registry+https://github.com/rust-lang/crates.io-index"-checksum = "de2c52737737f8609e94f975dee22854a2d5c125772d4b1cf292120f4d45c186"+checksum = "757712e8e61590d6d4f5d563483755538b5aa13467837a3b41cd9832509a7f85"dependencies = ["allocator-api2","bumpalo",diff --git a/Cargo.toml b/Cargo.tomlindex d9a8799b4f..0a8ef05bc7 100644--- a/Cargo.toml+++ b/Cargo.toml@@ -358,7 +358,7 @@ component-async-tests = { path = "crates/misc/component-async-tests" }# Bytecode Alliance maintained dependencies:# ----------------------------regalloc2 = "0.15.1"+regalloc2 = "0.15.2"wasip1 = { version = "1.0.0", default-features = false }# cap-std family:diff --git a/cranelift/codegen/src/context.rs b/cranelift/codegen/src/context.rsindex a81291bd57..c5b88be7cd 100644--- a/cranelift/codegen/src/context.rs+++ b/cranelift/codegen/src/context.rs@@ -54,6 +54,9 @@ pub struct Context {/// Flag: do we want a disassembly with the CompiledCode?pub want_disasm: bool,++ /// Reused register allocator context.+ pub(crate) regalloc_ctx: regalloc2::Ctx,}impl Context {@@ -77,6 +80,7 @@ impl Context {loop_analysis: LoopAnalysis::new(),compiled_code: None,want_disasm: false,+ regalloc_ctx: regalloc2::Ctx::default(),}}@@ -136,7 +140,13 @@ impl Context {self.verify_if(isa)?;self.optimize(isa, ctrl_plane)?;- result = isa.compile_function(&self.func, &self.domtree, self.want_disasm, ctrl_plane);+ result = isa.compile_function(+ &self.func,+ &self.domtree,+ &mut self.regalloc_ctx,+ self.want_disasm,+ ctrl_plane,+ );}trace!("****** DONE compiling {}\n", self.func.display_spec());resultdiff --git a/cranelift/codegen/src/isa/aarch64/mod.rs b/cranelift/codegen/src/isa/aarch64/mod.rsindex b613446495..2cf17cfa72 100644--- a/cranelift/codegen/src/isa/aarch64/mod.rs+++ b/cranelift/codegen/src/isa/aarch64/mod.rs@@ -55,12 +55,22 @@ impl AArch64Backend {&self,func: &Function,domtree: &DominatorTree,+ regalloc_ctx: &mut regalloc2::Ctx,ctrl_plane: &mut ControlPlane,) -> CodegenResult<(VCode<inst::Inst>, regalloc2::Output)> {let emit_info = EmitInfo::new(self.flags.clone(), self.isa_flags.clone());let sigs = SigSet::new::<abi::AArch64MachineDeps>(func, &self.flags)?;let abi = abi::AArch64Callee::new(func, self, &self.isa_flags, &sigs)?;- compile::compile::<AArch64Backend>(func, domtree, self, abi, emit_info, sigs, ctrl_plane)+ compile::compile::<AArch64Backend>(+ func,+ domtree,+ regalloc_ctx,+ self,+ abi,+ emit_info,+ sigs,+ ctrl_plane,+ )}}@@ -69,10 +79,12 @@ impl TargetIsa for AArch64Backend {&self,func: &Function,domtree: &DominatorTree,+ regalloc_ctx: &mut regalloc2::Ctx,want_disasm: bool,ctrl_plane: &mut ControlPlane,) -> CodegenResult<CompiledCodeStencil> {- let (vcode, regalloc_result) = self.compile_vcode(func, domtree, ctrl_plane)?;+ let (vcode, regalloc_result) =+ self.compile_vcode(func, domtree, regalloc_ctx, ctrl_plane)?;let emit_result = vcode.emit(®alloc_result, want_disasm, &self.flags, ctrl_plane)?;let value_labels_ranges = emit_result.value_labels_ranges;diff --git a/cranelift/codegen/src/isa/mod.rs b/cranelift/codegen/src/isa/mod.rsindex f9e3008272..5f33e632fb 100644--- a/cranelift/codegen/src/isa/mod.rs+++ b/cranelift/codegen/src/isa/mod.rs@@ -310,6 +310,7 @@ pub trait TargetIsa: fmt::Display + Send + Sync {&self,func: &Function,domtree: &DominatorTree,+ regalloc_ctx: &mut regalloc2::Ctx,want_disasm: bool,ctrl_plane: &mut ControlPlane,) -> CodegenResult<CompiledCodeStencil>;diff --git a/cranelift/codegen/src/isa/pulley_shared/mod.rs b/cranelift/codegen/src/isa/pulley_shared/mod.rsindex 69ac4fd5f3..f273b31751 100644--- a/cranelift/codegen/src/isa/pulley_shared/mod.rs+++ b/cranelift/codegen/src/isa/pulley_shared/mod.rs@@ -122,6 +122,7 @@ where&self,func: &ir::Function,domtree: &DominatorTree,+ regalloc_ctx: &mut regalloc2::Ctx,ctrl_plane: &mut ControlPlane,) -> CodegenResult<(VCode<inst::InstAndKind<P>>, regalloc2::Output)> {let emit_info = EmitInfo::new(@@ -131,7 +132,16 @@ where);let sigs = SigSet::new::<abi::PulleyMachineDeps<P>>(func, &self.flags)?;let abi = abi::PulleyCallee::new(func, self, &self.isa_flags, &sigs)?;- machinst::compile::<Self>(func, domtree, self, abi, emit_info, sigs, ctrl_plane)+ machinst::compile::<Self>(+ func,+ domtree,+ regalloc_ctx,+ self,+ abi,+ emit_info,+ sigs,+ ctrl_plane,+ )}}@@ -172,10 +182,12 @@ where&self,func: &ir::Function,domtree: &DominatorTree,+ regalloc_ctx: &mut regalloc2::Ctx,want_disasm: bool,ctrl_plane: &mut cranelift_control::ControlPlane,) -> CodegenResult<CompiledCodeStencil> {- let (vcode, regalloc_result) = self.compile_vcode(func, domtree, ctrl_plane)?;+ let (vcode, regalloc_result) =+ self.compile_vcode(func, domtree, regalloc_ctx, ctrl_plane)?;let want_disasm =want_disasm || (cfg!(feature = "trace-log") && log::log_enabled!(log::Level::Debug));diff --git a/cranelift/codegen/src/isa/riscv64/mod.rs b/cranelift/codegen/src/isa/riscv64/mod.rsindex e8f8ca4774..e533fb738d 100644--- a/cranelift/codegen/src/isa/riscv64/mod.rs+++ b/cranelift/codegen/src/isa/riscv64/mod.rs@@ -56,12 +56,22 @@ impl Riscv64Backend {&self,func: &Function,domtree: &DominatorTree,+ regalloc_ctx: &mut regalloc2::Ctx,ctrl_plane: &mut ControlPlane,) -> CodegenResult<(VCode<inst::Inst>, regalloc2::Output)> {let emit_info = EmitInfo::new(self.flags.clone(), self.isa_flags.clone());let sigs = SigSet::new::<abi::Riscv64MachineDeps>(func, &self.flags)?;let abi = abi::Riscv64Callee::new(func, self, &self.isa_flags, &sigs)?;- compile::compile::<Riscv64Backend>(func, domtree, self, abi, emit_info, sigs, ctrl_plane)+ compile::compile::<Riscv64Backend>(+ func,+ domtree,+ regalloc_ctx,+ self,+ abi,+ emit_info,+ sigs,+ ctrl_plane,+ )}}@@ -70,10 +80,12 @@ impl TargetIsa for Riscv64Backend {&self,func: &Function,domtree: &DominatorTree,+ regalloc_ctx: &mut regalloc2::Ctx,want_disasm: bool,ctrl_plane: &mut ControlPlane,) -> CodegenResult<CompiledCodeStencil> {- let (vcode, regalloc_result) = self.compile_vcode(func, domtree, ctrl_plane)?;+ let (vcode, regalloc_result) =+ self.compile_vcode(func, domtree, regalloc_ctx, ctrl_plane)?;let want_disasm = want_disasm || log::log_enabled!(log::Level::Debug);let emit_result = vcode.emit(®alloc_result, want_disasm, &self.flags, ctrl_plane)?;diff --git a/cranelift/codegen/src/isa/s390x/mod.rs b/cranelift/codegen/src/isa/s390x/mod.rsindex 9d92adb7a9..d9c14ce213 100644--- a/cranelift/codegen/src/isa/s390x/mod.rs+++ b/cranelift/codegen/src/isa/s390x/mod.rs@@ -53,12 +53,22 @@ impl S390xBackend {&self,func: &Function,domtree: &DominatorTree,+ regalloc_ctx: &mut regalloc2::Ctx,ctrl_plane: &mut ControlPlane,) -> CodegenResult<(VCode<inst::Inst>, regalloc2::Output)> {let emit_info = EmitInfo::new(self.isa_flags.clone());let sigs = SigSet::new::<abi::S390xMachineDeps>(func, &self.flags)?;let abi = abi::S390xCallee::new(func, self, &self.isa_flags, &sigs)?;- compile::compile::<S390xBackend>(func, domtree, self, abi, emit_info, sigs, ctrl_plane)+ compile::compile::<S390xBackend>(+ func,+ domtree,+ regalloc_ctx,+ self,+ abi,+ emit_info,+ sigs,+ ctrl_plane,+ )}}@@ -67,11 +77,13 @@ impl TargetIsa for S390xBackend {&self,func: &Function,domtree: &DominatorTree,+ regalloc_ctx: &mut regalloc2::Ctx,want_disasm: bool,ctrl_plane: &mut ControlPlane,) -> CodegenResult<CompiledCodeStencil> {let flags = self.flags();- let (vcode, regalloc_result) = self.compile_vcode(func, domtree, ctrl_plane)?;+ let (vcode, regalloc_result) =+ self.compile_vcode(func, domtree, regalloc_ctx, ctrl_plane)?;let emit_result = vcode.emit(®alloc_result, want_disasm, flags, ctrl_plane)?;let value_labels_ranges = emit_result.value_labels_ranges;diff --git a/cranelift/codegen/src/isa/x64/mod.rs b/cranelift/codegen/src/isa/x64/mod.rsindex 96f6f3b525..138a896cbd 100644--- a/cranelift/codegen/src/isa/x64/mod.rs+++ b/cranelift/codegen/src/isa/x64/mod.rs@@ -61,6 +61,7 @@ impl X64Backend {&self,func: &Function,domtree: &DominatorTree,+ regalloc_ctx: &mut regalloc2::Ctx,ctrl_plane: &mut ControlPlane,) -> CodegenResult<(VCode<inst::Inst>, regalloc2::Output)> {// This performs lowering to VCode, register-allocates the code, computes@@ -68,7 +69,16 @@ impl X64Backend {let emit_info = EmitInfo::new(self.flags.clone(), self.x64_flags.clone());let sigs = SigSet::new::<abi::X64ABIMachineSpec>(func, &self.flags)?;let abi = abi::X64Callee::new(func, self, &self.x64_flags, &sigs)?;- compile::compile::<Self>(func, domtree, self, abi, emit_info, sigs, ctrl_plane)+ compile::compile::<Self>(+ func,+ domtree,+ regalloc_ctx,+ self,+ abi,+ emit_info,+ sigs,+ ctrl_plane,+ )}}@@ -77,10 +87,12 @@ impl TargetIsa for X64Backend {&self,func: &Function,domtree: &DominatorTree,+ regalloc_ctx: &mut regalloc2::Ctx,want_disasm: bool,ctrl_plane: &mut ControlPlane,) -> CodegenResult<CompiledCodeStencil> {- let (vcode, regalloc_result) = self.compile_vcode(func, domtree, ctrl_plane)?;+ let (vcode, regalloc_result) =+ self.compile_vcode(func, domtree, regalloc_ctx, ctrl_plane)?;let emit_result = vcode.emit(®alloc_result, want_disasm, &self.flags, ctrl_plane)?;let value_labels_ranges = emit_result.value_labels_ranges;diff --git a/cranelift/codegen/src/machinst/compile.rs b/cranelift/codegen/src/machinst/compile.rsindex 7747d804ca..26633d19da 100644--- a/cranelift/codegen/src/machinst/compile.rs+++ b/cranelift/codegen/src/machinst/compile.rs@@ -15,6 +15,7 @@ use regalloc2::{Algorithm, RegallocOptions};pub fn compile<B: LowerBackend + TargetIsa>(f: &Function,domtree: &DominatorTree,+ regalloc_ctx: &mut regalloc2::Ctx,b: &B,abi: Callee<<<B as LowerBackend>::MInst as MachInst>::ABIMachineSpec>,emit_info: <B::MInst as MachInstEmit>::Info,@@ -62,14 +63,19 @@ pub fn compile<B: LowerBackend + TargetIsa>(RegallocAlgorithm::SinglePass => Algorithm::Fastalloc,};- regalloc2::run(&vcode, vcode.abi.machine_env(), &options)+ // Run the allocator with the caller's reused context (owned by the+ // `cranelift_codegen::Context`), taking its `Output` back out. This+ // avoids `regalloc2::run` allocating a fresh context -- which owns all+ // of the allocator's growable scratch state -- for every function.+ regalloc2::run_with_ctx(&vcode, vcode.abi.machine_env(), &options, regalloc_ctx).map_err(|err| {log::error!("Register allocation error for vcode\n{vcode:?}\nError: {err:?}\nCLIF for error:\n{f:?}",);err})- .expect("register allocation")+ .expect("register allocation");+ core::mem::take(&mut regalloc_ctx.output)};// Run the regalloc checker, if requested.diff --git a/supply-chain/imports.lock b/supply-chain/imports.lockindex a2c18e875d..9b25bc969d 100644--- a/supply-chain/imports.lock+++ b/supply-chain/imports.lock@@ -1572,8 +1572,8 @@ user-login = "dtolnay"user-name = "David Tolnay"[[publisher.regalloc2]]-version = "0.15.1"-when = "2026-04-15"+version = "0.15.2"+when = "2026-07-20"user-id = 3726user-login = "cfallin"user-name = "Chris Fallin"