Record vreg classes explicitly during liverange pass. (#35)

This resolves an issue seen when the source program uses multiple
regclasses (Int and Float): in some cases, the logic that grabs the
vregs and retains them (with class) in `vreg_regs` missed a register and
we had a class mismatch. This occurred because data structures were
initialized assuming `Int` regclass at first.

This PR instead removes the `vreg_regs` array, stores the class
explicitly as an `Option<RegClass>` in the `VRegData`, and provides a
`Env::vreg()` method that reconstitutes a `VReg` given its index and its
observed class. We "observe" the class of every vreg seen during the
liveness pass (and we assert that every occurrence of the vreg index has
the same class). In this way, we still have a single source-of-truth for
the vreg class (the mention of the vreg itself) and we explicitly
represent the "not observed yet" state (and panic on attempting to use
such a vreg) rather than implicitly taking the wrong class.
This commit is contained in:
Chris Fallin
2022-03-29 14:00:14 -07:00
committed by GitHub
parent 433e8b3776
commit ad41f8a7a5
5 changed files with 53 additions and 23 deletions

View File

@@ -259,6 +259,8 @@ pub struct VRegData {
pub ranges: LiveRangeList,
pub blockparam: Block,
pub is_ref: bool,
// We don't initially know the RegClass until we observe a use of the VReg.
pub class: Option<RegClass>,
}
#[derive(Clone, Debug)]
@@ -340,7 +342,6 @@ pub struct Env<'a, F: Function> {
pub bundles: Vec<LiveBundle>,
pub spillsets: Vec<SpillSet>,
pub vregs: Vec<VRegData>,
pub vreg_regs: Vec<VReg>,
pub pregs: Vec<PRegData>,
pub allocation_queue: PrioQueue,
pub safepoints: Vec<Inst>, // Sorted list of safepoint insts.
@@ -397,6 +398,31 @@ pub struct Env<'a, F: Function> {
pub annotations_enabled: bool,
}
impl<'a, F: Function> Env<'a, F> {
/// Get the VReg (with bundled RegClass) from a vreg index.
#[inline]
pub fn vreg(&self, index: VRegIndex) -> VReg {
let class = self.vregs[index.index()]
.class
.expect("trying to get a VReg before observing its class");
VReg::new(index.index(), class)
}
/// Record the class of a VReg. We learn this only when we observe
/// the VRegs in use.
pub fn observe_vreg_class(&mut self, vreg: VReg) {
let old_class = self.vregs[vreg.vreg()].class.replace(vreg.class());
// We should never observe two different classes for two
// mentions of a VReg in the source program.
debug_assert!(old_class == None || old_class == Some(vreg.class()));
}
/// Is this vreg actually used in the source program?
pub fn is_vreg_used(&self, index: VRegIndex) -> bool {
self.vregs[index.index()].class.is_some()
}
}
#[derive(Clone, Debug)]
pub struct SpillSlotData {
pub ranges: LiveRangeSet,