Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ description = "Backtracking register allocator inspired from IonMonkey"
repository = "https://github.com/bytecodealliance/regalloc2"

[dependencies]
arena-btree = { path = "../arena-btree" }
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

should refer to published crate here

log = { version = "0.4.8", default-features = false }
smallvec = "1.6.1"
fxhash = "0.2.1"
Expand Down
66 changes: 59 additions & 7 deletions src/ion/data_structures.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,10 @@ use crate::{
define_index, Allocation, Block, Edit, Function, Inst, MachineEnv, Operand, PReg, ProgPoint,
RegClass, VReg,
};
use arena_btree::{Arena, BTreeMap};
use smallvec::SmallVec;
use std::cmp::Ordering;
use std::collections::{BTreeMap, HashMap, HashSet};
use std::collections::{HashMap, HashSet};
use std::fmt::Debug;

/// A range from `from` (inclusive) to `to` (exclusive).
Expand Down Expand Up @@ -286,12 +287,25 @@ pub struct VRegData {
pub class: Option<RegClass>,
}

#[derive(Clone, Debug)]
#[derive(Debug)]
pub struct PRegData {
pub allocations: LiveRangeSet,
pub is_stack: bool,
}

impl PRegData {
pub fn clone(&self, arena: &mut Arena<LiveRangeKey, LiveRangeIndex>) -> Self {
PRegData {
allocations: self.allocations.clone(arena),
is_stack: self.is_stack,
}
}

pub fn drop(self, arena: &mut Arena<LiveRangeKey, LiveRangeIndex>) {
self.allocations.drop(arena);
}
}

#[derive(Clone, Debug)]
pub struct MultiFixedRegFixup {
pub pos: ProgPoint,
Expand Down Expand Up @@ -361,7 +375,7 @@ impl BlockparamIn {
}
}

#[derive(Clone, Debug)]
#[derive(Debug)]
pub struct Env<'a, F: Function> {
pub func: &'a F,
pub env: &'a MachineEnv,
Expand All @@ -371,6 +385,7 @@ pub struct Env<'a, F: Function> {
pub blockparam_outs: Vec<BlockparamOut>,
pub blockparam_ins: Vec<BlockparamIn>,

pub arena: Arena<LiveRangeKey, LiveRangeIndex>,
pub ranges: Vec<LiveRange>,
pub bundles: Vec<LiveBundle>,
pub spillsets: Vec<SpillSet>,
Expand Down Expand Up @@ -432,6 +447,17 @@ pub struct Env<'a, F: Function> {
pub annotations_enabled: bool,
}

impl<'a, F: Function> Drop for Env<'a, F> {
fn drop(&mut self) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think we actually need this, given that we know our key and index types are Copy (u32s in fact)?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

(Perhaps in trivial cases the empty drop impl means things optimize away, but I'd be a little skeptical of LLVM's ability to do that here in concert with the use of the drain iters etc)

for preg in self.pregs.drain(..) {
preg.drop(&mut self.arena);
}
for slot in self.spillslots.drain(..) {
slot.drop(&mut self.arena);
}
}
}

impl<'a, F: Function> Env<'a, F> {
/// Get the VReg (with bundled RegClass) from a vreg index.
#[inline]
Expand All @@ -457,13 +483,27 @@ impl<'a, F: Function> Env<'a, F> {
}
}

#[derive(Clone, Debug)]
#[derive(Debug)]
pub struct SpillSlotData {
pub ranges: LiveRangeSet,
pub slots: u32,
pub alloc: Allocation,
}

impl SpillSlotData {
pub fn clone(&self, arena: &mut Arena<LiveRangeKey, LiveRangeIndex>) -> Self {
SpillSlotData {
ranges: self.ranges.clone(arena),
slots: self.slots,
alloc: self.alloc,
}
}

pub fn drop(self, arena: &mut Arena<LiveRangeKey, LiveRangeIndex>) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Likewise here, not needed given knowledge of key/value types?

self.ranges.drop(arena);
}
}

#[derive(Clone, Debug)]
pub struct SpillSlotList {
pub slots: SmallVec<[SpillSlotIndex; 32]>,
Expand Down Expand Up @@ -495,11 +535,23 @@ pub struct PrioQueueEntry {
pub reg_hint: PReg,
}

#[derive(Clone, Debug)]
#[derive(Debug)]
pub struct LiveRangeSet {
pub btree: BTreeMap<LiveRangeKey, LiveRangeIndex>,
}

impl LiveRangeSet {
pub fn clone(&self, arena: &mut Arena<LiveRangeKey, LiveRangeIndex>) -> LiveRangeSet {
LiveRangeSet {
btree: self.btree.clone(arena),
}
}

pub fn drop(self, arena: &mut Arena<LiveRangeKey, LiveRangeIndex>) {
self.btree.drop(arena);
}
}

#[derive(Clone, Copy, Debug)]
pub struct LiveRangeKey {
pub from: u32,
Expand Down Expand Up @@ -588,9 +640,9 @@ impl PrioQueue {
}

impl LiveRangeSet {
pub(crate) fn new() -> Self {
pub(crate) fn new(arena: &Arena<LiveRangeKey, LiveRangeIndex>) -> Self {
Self {
btree: BTreeMap::new(),
btree: BTreeMap::new(arena),
}
}
}
Expand Down
25 changes: 14 additions & 11 deletions src/ion/liveranges.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,12 @@ use crate::{
Allocation, Block, Function, Inst, InstPosition, Operand, OperandConstraint, OperandKind,
OperandPos, PReg, ProgPoint, RegAllocError, VReg,
};
use arena_btree::Arena;
use fxhash::{FxHashMap, FxHashSet};
use slice_group_by::GroupByMut;
use smallvec::{smallvec, SmallVec};
use std::collections::{HashSet, VecDeque};
use std::mem;

/// A spill weight computed for a certain Use.
#[derive(Clone, Copy, Debug)]
Expand Down Expand Up @@ -101,13 +103,13 @@ impl std::ops::Add<SpillWeight> for SpillWeight {
impl<'a, F: Function> Env<'a, F> {
pub fn create_pregs_and_vregs(&mut self) {
// Create PRegs from the env.
self.pregs.resize(
PReg::NUM_INDEX,
PRegData {
allocations: LiveRangeSet::new(),
is_stack: false,
},
);
let arena = mem::replace(&mut self.arena, Arena::new());
self.pregs.resize_with(PReg::NUM_INDEX, || PRegData {
allocations: LiveRangeSet::new(&arena),
is_stack: false,
});
self.arena = arena;

for &preg in &self.env.fixed_stack_slots {
self.pregs[preg.index()].is_stack = true;
}
Expand Down Expand Up @@ -307,10 +309,11 @@ impl<'a, F: Function> Env<'a, F> {
pub fn add_liverange_to_preg(&mut self, range: CodeRange, reg: PReg) {
trace!("adding liverange to preg: {:?} to {}", range, reg);
let preg_idx = PRegIndex::new(reg.index());
self.pregs[preg_idx.index()]
.allocations
.btree
.insert(LiveRangeKey::from_range(&range), LiveRangeIndex::invalid());
self.pregs[preg_idx.index()].allocations.btree.insert(
&mut self.arena,
LiveRangeKey::from_range(&range),
LiveRangeIndex::invalid(),
);
}

pub fn is_live_in(&mut self, block: Block, vreg: VRegIndex) -> bool {
Expand Down
9 changes: 5 additions & 4 deletions src/ion/merge.rs
Original file line number Diff line number Diff line change
Expand Up @@ -250,10 +250,11 @@ impl<'a, F: Function> Env<'a, F> {
if let Some(preg) = self.func.is_pinned_vreg(self.vreg(vreg)) {
for entry in &self.vregs[vreg.index()].ranges {
let key = LiveRangeKey::from_range(&entry.range);
self.pregs[preg.index()]
.allocations
.btree
.insert(key, LiveRangeIndex::invalid());
self.pregs[preg.index()].allocations.btree.insert(
&mut self.arena,
key,
LiveRangeIndex::invalid(),
);
}
continue;
}
Expand Down
14 changes: 9 additions & 5 deletions src/ion/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,9 @@

use crate::cfg::CFGInfo;
use crate::{Function, MachineEnv, Output, PReg, ProgPoint, RegAllocError, RegClass};
use arena_btree::Arena;
use std::collections::HashMap;
use std::mem;

pub(crate) mod data_structures;
pub use data_structures::Stats;
Expand Down Expand Up @@ -55,6 +57,8 @@ impl<'a, F: Function> Env<'a, F> {
blockparam_outs: vec![],
blockparam_ins: vec![],
bundles: Vec::with_capacity(n),

arena: Arena::new(),
ranges: Vec::with_capacity(4 * n),
spillsets: Vec::with_capacity(n),
vregs: Vec::with_capacity(n),
Expand Down Expand Up @@ -133,14 +137,14 @@ pub fn run<F: Function>(
Ok(Output {
edits: env
.edits
.into_iter()
.drain(..)
.map(|(pos_prio, edit)| (pos_prio.pos, edit))
.collect(),
allocs: env.allocs,
inst_alloc_offsets: env.inst_alloc_offsets,
allocs: mem::take(&mut env.allocs),
inst_alloc_offsets: mem::take(&mut env.inst_alloc_offsets),
num_spillslots: env.num_spillslots as usize,
debug_locations: env.debug_locations,
safepoint_slots: env.safepoint_slots,
debug_locations: mem::take(&mut env.debug_locations),
safepoint_slots: mem::take(&mut env.safepoint_slots),
stats: env.stats,
})
}
2 changes: 1 addition & 1 deletion src/ion/moves.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1004,7 +1004,7 @@ impl<'a, F: Function> Env<'a, F> {
if !self.pregs[preg.index()]
.allocations
.btree
.contains_key(&key)
.contains_key(&self.arena, &key)
{
let alloc = Allocation::reg(preg);
if moves
Expand Down
21 changes: 13 additions & 8 deletions src/ion/process.rs
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ impl<'a, F: Function> Env<'a, F> {
let mut preg_range_iter = self.pregs[reg.index()]
.allocations
.btree
.range(from_key..)
.range(&self.arena, from_key..)
.peekable();
trace!(
"alloc map for {:?} in range {:?}..: {:?}",
Expand Down Expand Up @@ -122,7 +122,7 @@ impl<'a, F: Function> Env<'a, F> {
preg_range_iter = self.pregs[reg.index()]
.allocations
.btree
.range(from_key..)
.range(&self.arena, from_key..)
.peekable();
skips = 0;
}
Expand Down Expand Up @@ -198,10 +198,11 @@ impl<'a, F: Function> Env<'a, F> {
trace!(" -> bundle {:?} assigned to preg {:?}", bundle, preg);
self.bundles[bundle.index()].allocation = Allocation::reg(preg);
for entry in &self.bundles[bundle.index()].ranges {
self.pregs[reg.index()]
.allocations
.btree
.insert(LiveRangeKey::from_range(&entry.range), entry.index);
self.pregs[reg.index()].allocations.btree.insert(
&mut self.arena,
LiveRangeKey::from_range(&entry.range),
entry.index,
);
}

AllocRegResult::Allocated(Allocation::reg(preg))
Expand Down Expand Up @@ -230,7 +231,7 @@ impl<'a, F: Function> Env<'a, F> {
self.pregs[preg_idx.index()]
.allocations
.btree
.remove(&LiveRangeKey::from_range(&entry.range));
.remove(&mut self.arena, &LiveRangeKey::from_range(&entry.range));
}
let prio = self.bundles[bundle.index()].prio;
trace!(" -> prio {}; back into queue", prio);
Expand Down Expand Up @@ -1230,7 +1231,11 @@ impl<'a, F: Function> Env<'a, F> {
from: range.from.prev(),
to: range.from.prev(),
});
for (key, lr) in self.pregs[preg.index()].allocations.btree.range(start..) {
for (key, lr) in self.pregs[preg.index()]
.allocations
.btree
.range(&self.arena, start..)
{
let preg_range = key.to_range();
if preg_range.to <= range.from {
continue;
Expand Down
13 changes: 7 additions & 6 deletions src/ion/spill.rs
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ impl<'a, F: Function> Env<'a, F> {
if self.spillslots[spillslot.index()]
.ranges
.btree
.contains_key(&LiveRangeKey::from_range(&entry.range))
.contains_key(&self.arena, &LiveRangeKey::from_range(&entry.range))
{
return false;
}
Expand Down Expand Up @@ -106,10 +106,11 @@ impl<'a, F: Function> Env<'a, F> {
entry.index,
vreg,
);
self.spillslots[spillslot.index()]
.ranges
.btree
.insert(LiveRangeKey::from_range(&entry.range), entry.index);
self.spillslots[spillslot.index()].ranges.btree.insert(
&mut self.arena,
LiveRangeKey::from_range(&entry.range),
entry.index,
);
}
}
}
Expand Down Expand Up @@ -163,7 +164,7 @@ impl<'a, F: Function> Env<'a, F> {
// Allocate a new spillslot.
let spillslot = SpillSlotIndex::new(self.spillslots.len());
self.spillslots.push(SpillSlotData {
ranges: LiveRangeSet::new(),
ranges: LiveRangeSet::new(&self.arena),
alloc: Allocation::none(),
slots: size as u32,
});
Expand Down