-
Notifications
You must be signed in to change notification settings - Fork 42
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
refactor: use &'static [PReg]
instead of Vec<PReg>
#208
base: main
Are you sure you want to change the base?
Changes from all commits
732bc75
0d59759
5e0d894
fbeebc3
3058116
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -140,18 +140,20 @@ impl RealFunction { | |
} | ||
} | ||
|
||
fn mach_env(no_of_regs: usize) -> MachineEnv { | ||
fn mach_env(no_of_regs: usize) -> MachineEnv<'static> { | ||
MachineEnv { | ||
preferred_regs_by_class: [ | ||
(0..no_of_regs) | ||
.map(|no| PReg::new(no, RegClass::Int)) | ||
.collect(), | ||
vec![], | ||
vec![], | ||
Vec::leak( | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We definitely can't leak while fuzzing -- can we instead have a few static |
||
(0..no_of_regs) | ||
.map(|no| PReg::new(no, RegClass::Int)) | ||
.collect(), | ||
), | ||
&[], | ||
&[], | ||
], | ||
non_preferred_regs_by_class: [vec![], vec![], vec![]], | ||
non_preferred_regs_by_class: [&[], &[], &[]], | ||
scratch_by_class: [None, None, None], | ||
fixed_stack_slots: vec![], | ||
fixed_stack_slots: &[], | ||
} | ||
} | ||
|
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -624,30 +624,32 @@ impl core::fmt::Debug for Func { | |
} | ||
} | ||
|
||
pub fn machine_env() -> MachineEnv { | ||
fn regs(r: core::ops::Range<usize>, c: RegClass) -> Vec<PReg> { | ||
r.map(|i| PReg::new(i, c)).collect() | ||
pub fn machine_env() -> MachineEnv<'static> { | ||
fn regs(r: core::ops::Range<usize>, c: RegClass) -> &'static [PReg] { | ||
Vec::leak(r.map(|i| PReg::new(i, c)).collect()) | ||
} | ||
let preferred_regs_by_class: [Vec<PReg>; 3] = [ | ||
let preferred_regs_by_class: [&'static [PReg]; 3] = [ | ||
regs(0..24, RegClass::Int), | ||
regs(0..24, RegClass::Float), | ||
regs(0..24, RegClass::Vector), | ||
]; | ||
let non_preferred_regs_by_class: [Vec<PReg>; 3] = [ | ||
let non_preferred_regs_by_class: [&'static [PReg]; 3] = [ | ||
regs(24..32, RegClass::Int), | ||
regs(24..32, RegClass::Float), | ||
regs(24..32, RegClass::Vector), | ||
]; | ||
let scratch_by_class: [Option<PReg>; 3] = [None, None, None]; | ||
let fixed_stack_slots = (32..63) | ||
.flat_map(|i| { | ||
[ | ||
PReg::new(i, RegClass::Int), | ||
PReg::new(i, RegClass::Float), | ||
PReg::new(i, RegClass::Vector), | ||
] | ||
}) | ||
.collect(); | ||
let fixed_stack_slots = Vec::leak( | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think we need to avoid |
||
(32..63) | ||
.flat_map(|i| { | ||
[ | ||
PReg::new(i, RegClass::Int), | ||
PReg::new(i, RegClass::Float), | ||
PReg::new(i, RegClass::Vector), | ||
] | ||
}) | ||
.collect(), | ||
); | ||
// Register 63 is reserved for use as a fixed non-allocatable register. | ||
MachineEnv { | ||
preferred_regs_by_class, | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -334,17 +334,17 @@ impl Iterator for PRegSetIter { | |
} | ||
} | ||
|
||
impl From<&MachineEnv> for PRegSet { | ||
impl From<&MachineEnv<'_>> for PRegSet { | ||
fn from(env: &MachineEnv) -> Self { | ||
let mut res = Self::default(); | ||
|
||
for class in env.preferred_regs_by_class.iter() { | ||
for class in env.preferred_regs_by_class { | ||
for preg in class { | ||
res.add(*preg) | ||
} | ||
} | ||
|
||
for class in env.non_preferred_regs_by_class.iter() { | ||
for class in env.non_preferred_regs_by_class { | ||
for preg in class { | ||
res.add(*preg) | ||
} | ||
|
@@ -1434,15 +1434,20 @@ impl<'a> Iterator for OutputIter<'a> { | |
/// are available to allocate and what register may be used as a | ||
/// scratch register for each class, and some other miscellaneous info | ||
/// as well. | ||
/// | ||
/// Note that `MachineEnv` is designed to be a global configuration struct that programs | ||
/// will have very few of and generally want to keep around for their entire lifetime. | ||
/// In order to make static initialization easier the registers lists are static slices instead | ||
/// of `Vec`s. If your use case depends on dynamically creating the registers lists, consider | ||
/// [`Vec::leak`]. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'd prefer not to give this suggestion in the doc-comment: since it's parameterized on a lifetime (rather than hardcoding There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. ah yeah good catch, that was from before I allowed any lifetimes. This notice doesn't make much sense anymore |
||
#[derive(Clone, Debug)] | ||
#[cfg_attr(feature = "enable-serde", derive(Serialize, Deserialize))] | ||
pub struct MachineEnv { | ||
pub struct MachineEnv<'a> { | ||
/// Preferred physical registers for each class. These are the | ||
/// registers that will be allocated first, if free. | ||
/// | ||
/// If an explicit scratch register is provided in `scratch_by_class` then | ||
/// it must not appear in this list. | ||
pub preferred_regs_by_class: [Vec<PReg>; 3], | ||
pub preferred_regs_by_class: [&'a [PReg]; 3], | ||
|
||
/// Non-preferred physical registers for each class. These are the | ||
/// registers that will be allocated if a preferred register is | ||
|
@@ -1451,7 +1456,7 @@ pub struct MachineEnv { | |
/// | ||
/// If an explicit scratch register is provided in `scratch_by_class` then | ||
/// it must not appear in this list. | ||
pub non_preferred_regs_by_class: [Vec<PReg>; 3], | ||
pub non_preferred_regs_by_class: [&'a [PReg]; 3], | ||
|
||
/// Optional dedicated scratch register per class. This is needed to perform | ||
/// moves between registers when cyclic move patterns occur. The | ||
|
@@ -1476,7 +1481,7 @@ pub struct MachineEnv { | |
/// | ||
/// `PReg`s in this list cannot be used as an allocatable or scratch | ||
/// register. | ||
pub fixed_stack_slots: Vec<PReg>, | ||
pub fixed_stack_slots: &'a [PReg], | ||
} | ||
|
||
/// The output of the register allocator. | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
don't love this, open for better ideas
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Maybe use
.to_owned()
instead ofVec::from
?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
hmm, yeah doesn't really change anything about it but might look nicer