Skip to content

Commit

Permalink
refactor(cranelift-codegen): remove SpinLock
Browse files Browse the repository at this point in the history
This removes the dependency on `std::sync::SpinLock` by lifting the state out of a static and into the `Callee` struct.
  • Loading branch information
JonasKruckenberg committed Apr 26, 2024
1 parent 4822904 commit eab337c
Show file tree
Hide file tree
Showing 5 changed files with 14 additions and 28 deletions.
11 changes: 2 additions & 9 deletions cranelift/codegen/src/isa/aarch64/abi.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ use alloc::boxed::Box;
use alloc::vec::Vec;
use regalloc2::{MachineEnv, PReg, PRegSet};
use smallvec::{smallvec, SmallVec};
use std::sync::OnceLock;

// We use a generic implementation that factors out AArch64 and x64 ABI commonalities, because
// these ABIs are very similar.
Expand Down Expand Up @@ -1154,14 +1153,8 @@ impl ABIMachineSpec for AArch64MachineDeps {
s.nominal_sp_to_fp
}

fn get_machine_env(flags: &settings::Flags, _call_conv: isa::CallConv) -> &MachineEnv {
if flags.enable_pinned_reg() {
static MACHINE_ENV: OnceLock<MachineEnv> = OnceLock::new();
MACHINE_ENV.get_or_init(|| create_reg_env(true))
} else {
static MACHINE_ENV: OnceLock<MachineEnv> = OnceLock::new();
MACHINE_ENV.get_or_init(|| create_reg_env(false))
}
fn get_machine_env(flags: &settings::Flags) -> MachineEnv {
create_reg_env(flags.enable_pinned_reg())
}

fn get_regs_clobbered_by_call(_call_conv: isa::CallConv) -> PRegSet {
Expand Down
6 changes: 2 additions & 4 deletions cranelift/codegen/src/isa/riscv64/abi.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ use alloc::vec::Vec;
use regalloc2::{MachineEnv, PReg, PRegSet};

use smallvec::{smallvec, SmallVec};
use std::sync::OnceLock;

/// Support for the Riscv64 ABI from the callee side (within a function body).
pub(crate) type Riscv64Callee = Callee<Riscv64MachineDeps>;
Expand Down Expand Up @@ -704,9 +703,8 @@ impl ABIMachineSpec for Riscv64MachineDeps {
s.nominal_sp_to_fp
}

fn get_machine_env(_flags: &settings::Flags, _call_conv: isa::CallConv) -> &MachineEnv {
static MACHINE_ENV: OnceLock<MachineEnv> = OnceLock::new();
MACHINE_ENV.get_or_init(create_reg_enviroment)
fn get_machine_env(_flags: &settings::Flags) -> MachineEnv {
create_reg_enviroment()
}

fn get_regs_clobbered_by_call(_call_conv_of_callee: isa::CallConv) -> PRegSet {
Expand Down
6 changes: 2 additions & 4 deletions cranelift/codegen/src/isa/s390x/abi.rs
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,6 @@ use crate::{CodegenError, CodegenResult};
use alloc::vec::Vec;
use regalloc2::{MachineEnv, PRegSet};
use smallvec::{smallvec, SmallVec};
use std::sync::OnceLock;

// We use a generic implementation that factors out ABI commonalities.

Expand Down Expand Up @@ -786,9 +785,8 @@ impl ABIMachineSpec for S390xMachineDeps {
s.initial_sp_offset
}

fn get_machine_env(_flags: &settings::Flags, _call_conv: isa::CallConv) -> &MachineEnv {
static MACHINE_ENV: OnceLock<MachineEnv> = OnceLock::new();
MACHINE_ENV.get_or_init(create_machine_env)
fn get_machine_env(_flags: &settings::Flags) -> MachineEnv {
create_machine_env()
}

fn get_regs_clobbered_by_call(_call_conv_of_callee: isa::CallConv) -> PRegSet {
Expand Down
11 changes: 2 additions & 9 deletions cranelift/codegen/src/isa/x64/abi.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ use alloc::vec::Vec;
use args::*;
use regalloc2::{MachineEnv, PReg, PRegSet};
use smallvec::{smallvec, SmallVec};
use std::sync::OnceLock;

/// This is the limit for the size of argument and return-value areas on the
/// stack. We place a reasonable limit here to avoid integer overflow issues
Expand Down Expand Up @@ -936,14 +935,8 @@ impl ABIMachineSpec for X64ABIMachineSpec {
s.nominal_sp_to_fp()
}

fn get_machine_env(flags: &settings::Flags, _call_conv: isa::CallConv) -> &MachineEnv {
if flags.enable_pinned_reg() {
static MACHINE_ENV: OnceLock<MachineEnv> = OnceLock::new();
MACHINE_ENV.get_or_init(|| create_reg_env_systemv(true))
} else {
static MACHINE_ENV: OnceLock<MachineEnv> = OnceLock::new();
MACHINE_ENV.get_or_init(|| create_reg_env_systemv(false))
}
fn get_machine_env(flags: &settings::Flags) -> MachineEnv {
create_reg_env_systemv(flags.enable_pinned_reg())
}

fn get_regs_clobbered_by_call(call_conv_of_callee: isa::CallConv) -> PRegSet {
Expand Down
8 changes: 6 additions & 2 deletions cranelift/codegen/src/machinst/abi.rs
Original file line number Diff line number Diff line change
Expand Up @@ -626,7 +626,7 @@ pub trait ABIMachineSpec {
fn get_nominal_sp_to_fp(s: &<Self::I as MachInstEmit>::State) -> i64;

/// Get the ABI-dependent MachineEnv for managing register allocation.
fn get_machine_env(flags: &settings::Flags, call_conv: isa::CallConv) -> &MachineEnv;
fn get_machine_env(flags: &settings::Flags) -> MachineEnv;

/// Get all caller-save registers, that is, registers that we expect
/// not to be saved across a call to a callee with the given ABI.
Expand Down Expand Up @@ -1118,6 +1118,7 @@ pub struct Callee<M: ABIMachineSpec> {
/// manually register-allocated and carefully only use caller-saved
/// registers and keep nothing live after this sequence of instructions.
stack_limit: Option<(Reg, SmallInstVec<M::I>)>,
machine_env: MachineEnv,

_mach: PhantomData<M>,
}
Expand Down Expand Up @@ -1227,6 +1228,8 @@ impl<M: ABIMachineSpec> Callee<M> {

let tail_args_size = sigs[sig].sized_stack_arg_space;

let machine_env = M::get_machine_env(&flags);

Ok(Self {
ir_sig: ensure_struct_return_ptr_is_returned(&f.signature),
sig,
Expand All @@ -1244,6 +1247,7 @@ impl<M: ABIMachineSpec> Callee<M> {
isa_flags: isa_flags.clone(),
is_leaf: f.is_leaf(),
stack_limit,
machine_env,
_mach: PhantomData,
})
}
Expand Down Expand Up @@ -1450,7 +1454,7 @@ impl<M: ABIMachineSpec> Callee<M> {

/// Get the ABI-dependent MachineEnv for managing register allocation.
pub fn machine_env(&self, sigs: &SigSet) -> &MachineEnv {
M::get_machine_env(&self.flags, self.call_conv(sigs))
&self.machine_env
}

/// The offsets of all sized stack slots (not spill slots) for debuginfo purposes.
Expand Down

0 comments on commit eab337c

Please sign in to comment.