Skip to content

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

Cranelift: reuse `regalloc2::Output` across function compilations (#13927)

Nick Fitzgerald2c72afc5ccd3
diff --git a/cranelift/codegen/src/isa/aarch64/mod.rs b/cranelift/codegen/src/isa/aarch64/mod.rs
index 2cf17cfa72..666d305838 100644
--- a/cranelift/codegen/src/isa/aarch64/mod.rs
+++ b/cranelift/codegen/src/isa/aarch64/mod.rs
@@ -57,7 +57,7 @@ impl AArch64Backend {
domtree: &DominatorTree,
regalloc_ctx: &mut regalloc2::Ctx,
ctrl_plane: &mut ControlPlane,
- ) -> CodegenResult<(VCode<inst::Inst>, regalloc2::Output)> {
+ ) -> CodegenResult<VCode<inst::Inst>> {
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)?;
@@ -83,10 +83,9 @@ impl TargetIsa for AArch64Backend {
want_disasm: bool,
ctrl_plane: &mut ControlPlane,
) -> CodegenResult<CompiledCodeStencil> {
- let (vcode, regalloc_result) =
- self.compile_vcode(func, domtree, regalloc_ctx, ctrl_plane)?;
+ let vcode = self.compile_vcode(func, domtree, regalloc_ctx, ctrl_plane)?;
- let emit_result = vcode.emit(&regalloc_result, want_disasm, &self.flags, ctrl_plane)?;
+ let emit_result = vcode.emit(&regalloc_ctx.output, want_disasm, &self.flags, ctrl_plane)?;
let value_labels_ranges = emit_result.value_labels_ranges;
let buffer = emit_result.buffer;
diff --git a/cranelift/codegen/src/isa/pulley_shared/mod.rs b/cranelift/codegen/src/isa/pulley_shared/mod.rs
index f273b31751..349f10363c 100644
--- a/cranelift/codegen/src/isa/pulley_shared/mod.rs
+++ b/cranelift/codegen/src/isa/pulley_shared/mod.rs
@@ -124,7 +124,7 @@ where
domtree: &DominatorTree,
regalloc_ctx: &mut regalloc2::Ctx,
ctrl_plane: &mut ControlPlane,
- ) -> CodegenResult<(VCode<inst::InstAndKind<P>>, regalloc2::Output)> {
+ ) -> CodegenResult<VCode<inst::InstAndKind<P>>> {
let emit_info = EmitInfo::new(
func.signature.call_conv,
self.flags.clone(),
@@ -186,12 +186,11 @@ where
want_disasm: bool,
ctrl_plane: &mut cranelift_control::ControlPlane,
) -> CodegenResult<CompiledCodeStencil> {
- let (vcode, regalloc_result) =
- self.compile_vcode(func, domtree, regalloc_ctx, ctrl_plane)?;
+ let vcode = self.compile_vcode(func, domtree, regalloc_ctx, ctrl_plane)?;
let want_disasm =
want_disasm || (cfg!(feature = "trace-log") && log::log_enabled!(log::Level::Debug));
- let emit_result = vcode.emit(&regalloc_result, want_disasm, &self.flags, ctrl_plane)?;
+ let emit_result = vcode.emit(&regalloc_ctx.output, want_disasm, &self.flags, ctrl_plane)?;
let value_labels_ranges = emit_result.value_labels_ranges;
let buffer = emit_result.buffer;
diff --git a/cranelift/codegen/src/isa/riscv64/mod.rs b/cranelift/codegen/src/isa/riscv64/mod.rs
index e533fb738d..96ec6b81c7 100644
--- a/cranelift/codegen/src/isa/riscv64/mod.rs
+++ b/cranelift/codegen/src/isa/riscv64/mod.rs
@@ -58,7 +58,7 @@ impl Riscv64Backend {
domtree: &DominatorTree,
regalloc_ctx: &mut regalloc2::Ctx,
ctrl_plane: &mut ControlPlane,
- ) -> CodegenResult<(VCode<inst::Inst>, regalloc2::Output)> {
+ ) -> CodegenResult<VCode<inst::Inst>> {
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)?;
@@ -84,11 +84,10 @@ impl TargetIsa for Riscv64Backend {
want_disasm: bool,
ctrl_plane: &mut ControlPlane,
) -> CodegenResult<CompiledCodeStencil> {
- let (vcode, regalloc_result) =
- self.compile_vcode(func, domtree, regalloc_ctx, ctrl_plane)?;
+ let vcode = 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(&regalloc_result, want_disasm, &self.flags, ctrl_plane)?;
+ let emit_result = vcode.emit(&regalloc_ctx.output, want_disasm, &self.flags, ctrl_plane)?;
let value_labels_ranges = emit_result.value_labels_ranges;
let buffer = emit_result.buffer;
diff --git a/cranelift/codegen/src/isa/s390x/mod.rs b/cranelift/codegen/src/isa/s390x/mod.rs
index d9c14ce213..8323bd50c7 100644
--- a/cranelift/codegen/src/isa/s390x/mod.rs
+++ b/cranelift/codegen/src/isa/s390x/mod.rs
@@ -55,7 +55,7 @@ impl S390xBackend {
domtree: &DominatorTree,
regalloc_ctx: &mut regalloc2::Ctx,
ctrl_plane: &mut ControlPlane,
- ) -> CodegenResult<(VCode<inst::Inst>, regalloc2::Output)> {
+ ) -> CodegenResult<VCode<inst::Inst>> {
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)?;
@@ -82,10 +82,9 @@ impl TargetIsa for S390xBackend {
ctrl_plane: &mut ControlPlane,
) -> CodegenResult<CompiledCodeStencil> {
let flags = self.flags();
- let (vcode, regalloc_result) =
- self.compile_vcode(func, domtree, regalloc_ctx, ctrl_plane)?;
+ let vcode = self.compile_vcode(func, domtree, regalloc_ctx, ctrl_plane)?;
- let emit_result = vcode.emit(&regalloc_result, want_disasm, flags, ctrl_plane)?;
+ let emit_result = vcode.emit(&regalloc_ctx.output, want_disasm, flags, ctrl_plane)?;
let value_labels_ranges = emit_result.value_labels_ranges;
let buffer = emit_result.buffer;
diff --git a/cranelift/codegen/src/isa/x64/mod.rs b/cranelift/codegen/src/isa/x64/mod.rs
index 138a896cbd..ae2f5daecd 100644
--- a/cranelift/codegen/src/isa/x64/mod.rs
+++ b/cranelift/codegen/src/isa/x64/mod.rs
@@ -63,7 +63,7 @@ impl X64Backend {
domtree: &DominatorTree,
regalloc_ctx: &mut regalloc2::Ctx,
ctrl_plane: &mut ControlPlane,
- ) -> CodegenResult<(VCode<inst::Inst>, regalloc2::Output)> {
+ ) -> CodegenResult<VCode<inst::Inst>> {
// This performs lowering to VCode, register-allocates the code, computes
// block layout and finalizes branches. The result is ready for binary emission.
let emit_info = EmitInfo::new(self.flags.clone(), self.x64_flags.clone());
@@ -91,10 +91,9 @@ impl TargetIsa for X64Backend {
want_disasm: bool,
ctrl_plane: &mut ControlPlane,
) -> CodegenResult<CompiledCodeStencil> {
- let (vcode, regalloc_result) =
- self.compile_vcode(func, domtree, regalloc_ctx, ctrl_plane)?;
+ let vcode = self.compile_vcode(func, domtree, regalloc_ctx, ctrl_plane)?;
- let emit_result = vcode.emit(&regalloc_result, want_disasm, &self.flags, ctrl_plane)?;
+ let emit_result = vcode.emit(&regalloc_ctx.output, want_disasm, &self.flags, ctrl_plane)?;
let value_labels_ranges = emit_result.value_labels_ranges;
let buffer = emit_result.buffer;
diff --git a/cranelift/codegen/src/machinst/compile.rs b/cranelift/codegen/src/machinst/compile.rs
index 26633d19da..72109af1a4 100644
--- a/cranelift/codegen/src/machinst/compile.rs
+++ b/cranelift/codegen/src/machinst/compile.rs
@@ -21,7 +21,7 @@ pub fn compile<B: LowerBackend + TargetIsa>(
emit_info: <B::MInst as MachInstEmit>::Info,
sigs: SigSet,
ctrl_plane: &mut ControlPlane,
-) -> CodegenResult<(VCode<B::MInst>, regalloc2::Output)> {
+) -> CodegenResult<VCode<B::MInst>> {
// Compute lowered block order.
let block_order = BlockLoweringOrder::new(f, domtree, ctrl_plane);
@@ -49,7 +49,7 @@ pub fn compile<B: LowerBackend + TargetIsa>(
trace!("vcode from lowering: \n{:?}", vcode);
// Perform register allocation.
- let regalloc_result = {
+ {
let _tt = timing::regalloc();
let mut options = RegallocOptions::default();
options.verbose_log = b.flags().regalloc_verbose_logs();
@@ -63,26 +63,25 @@ pub fn compile<B: LowerBackend + TargetIsa>(
RegallocAlgorithm::SinglePass => Algorithm::Fastalloc,
};
- // 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:?}",
+ "Register allocation error for vcode\n\
+ {vcode:?}\n\
+ Error: {err:?}\n\
+ CLIF for error:\n\
+ {f:?}",
);
err
})
.expect("register allocation");
- core::mem::take(&mut regalloc_ctx.output)
- };
+ }
// Run the regalloc checker, if requested.
if b.flags().regalloc_checker() {
let _tt = timing::regalloc_checker();
let mut checker = regalloc2::checker::Checker::new(&vcode, &vcode.abi.machine_env());
- checker.prepare(&regalloc_result);
+ checker.prepare(&regalloc_ctx.output);
checker
.run()
.map_err(|err| {
@@ -92,5 +91,5 @@ pub fn compile<B: LowerBackend + TargetIsa>(
.expect("register allocation checker");
}
- Ok((vcode, regalloc_result))
+ Ok(vcode)
}