Skip to content
Draft
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
109 changes: 103 additions & 6 deletions crates/xcl/src/gpu/recomp/lane_masks.rs
Original file line number Diff line number Diff line change
Expand Up @@ -176,6 +176,22 @@ impl Bdd {
self.disjoint(a0, b0, seen) && self.disjoint(a1, b1, seen)
}

/// Whether `a` depends on an atom `pick` names.
fn depends(&self, a: B, pick: &dyn Fn(u32) -> bool) -> bool {
let (mut work, mut seen) = (vec![a], HashSet::new());
while let Some(n) = work.pop() {
if n <= TRUE || !seen.insert(n) {
continue;
}
let (v, lo, hi) = self.nodes[n as usize];
if pick(v) {
return true;
}
work.extend([lo, hi]);
}
false
}

/// `a` with the atoms `drop` names quantified away (∃).
fn exists(&mut self, a: B, drop: &dyn Fn(u32) -> bool, memo: &mut HashMap<B, B>) -> R<B> {
if a <= TRUE {
Expand All @@ -202,6 +218,9 @@ enum Atom {
Opaque(u64, usize),
/// an unresolved join of this slot (SGPR, `SCC`) at the start of this block
Join(usize, usize),
/// a slot (SGPR, `SCC`) a loop wrote, on the per-lane exit edge (block, successor index) out
/// of it: the console's value is the last round's, not each lane's own ([`Lanes::exit_state`])
Exit(usize, usize, usize),
/// EXEC at launch, where it need not hold every lane that runs (`LaunchExec::Unknown`)
Launch,
}
Expand Down Expand Up @@ -704,21 +723,60 @@ impl<'a> Lanes<'a> {
let last = &self.insts[end - 1];
let mut out = Vec::new();
for k in 0..self.succ[b].len() {
let fact = match self.succ[b][k].1 {
Edge::Always => reach,
let (fact, lane) = match self.succ[b][k].1 {
Edge::Always => (reach, false),
edge => {
// a fact too large to build is left without the branch's condition (weaker)
let (raw_c, _, negate) = self.cond_bounded(&st, last, b)?;
let (raw_c, kind, negate) = self.cond_bounded(&st, last, b)?;
let take_neg = (edge == Edge::Taken) == negate;
self.bdd
let fact = self
.bdd
.within(|bdd| {
let c = if take_neg { bdd.not(raw_c)? } else { raw_c };
bdd.and(reach, c)
})
.unwrap_or(reach)
.unwrap_or(reach);
(fact, kind != SccKind::Uniform)
}
};
out.push((fact, st.clone()));
let st = if lane { self.exit_state(b, k, &st)? } else { st.clone() };
out.push((fact, st));
}
Ok(out)
}

/// The masks on edge `k` of block `b`, a branch each lane takes on its own bit. Where it
/// leaves loops, lanes leave them in different rounds: here a lane stops running the loop
/// once it is out, while on the console the wave runs on and every later round writes the
/// loop's masks again (a compare with 0 in the lanes no longer in EXEC). After the loop a mask
/// the loop wrote holds the last round's bits, not each lane's own last value, so it becomes
/// an atom (#3440). EXEC is the exception: the wave leaves when it is empty, which is each
/// lane's own bit when it leaves too. A mask set before the loop and not written in it is
/// each lane's on both.
fn exit_state(&mut self, b: usize, k: usize, st: &State) -> R<State> {
let s = self.succ[b][k].0;
let left: Vec<usize> = (0..self.blocks.len())
.filter(|&h| self.body[h].as_ref().is_some_and(|inside| inside[b] && !inside[s]))
.collect();
if left.is_empty() {
return Ok(st.clone());
}
let in_loop = |v: u32, lanes: &Self| {
let at = lanes.born[v as usize];
left.iter().any(|&h| lanes.body[h].as_ref().unwrap()[at])
};
let mut out = st.clone();
for r in (0..128).filter(|&r| r != EXEC) {
if let Some(v) = st.regs[r] {
if self.bdd.depends(v, &|a| in_loop(a, self)) {
out.regs[r] = Some(self.atom(Atom::Exit(b, k, r), s)?);
}
}
}
if let Some((v, _)) = st.scc {
if self.bdd.depends(v, &|a| in_loop(a, self)) {
out.scc = Some((self.atom(Atom::Exit(b, k, SCC), s)?, SccKind::Unknown));
}
}
Ok(out)
}
Expand Down Expand Up @@ -1061,6 +1119,45 @@ mod tests {
assert_eq!((s.vector, s.guarded.len(), s.lane_branches), (2, 0, 1));
}

#[test]
fn a_mask_from_a_per_lane_loop_still_guards_what_follows() {
// As above, then exec = vcc after the loop (#3440). On the console VCC is the last
// round's compare: bits only for the lanes still in the loop then, since the compare
// writes 0 for inactive lanes. Per lane, each lane keeps its own last compare, which was
// set for every lane (it is why the lane left), so the v_mov at pc 28 lost its guard.
let r = run(&[
("S_MOV_B64", "ENC_SOP1", &[("SDST", 0), ("SSRC0", 126)]),
CMP,
("V_ADD_NC_U32", "ENC_VOP2", &[("VDST", 4), ("SRC0", V + 4), ("VSRC1", 5)]),
("S_ANDN2_B64", "ENC_SOP2", &[("SDST", 126), ("SSRC0", 126), ("SSRC1", 106)]),
("S_CBRANCH_EXECNZ", "ENC_SOPP", &[("SIMM16", 0xFFFC)]),
RESTORE,
("S_AND_SAVEEXEC_B64", "ENC_SOP1", &[("SDST", 2), ("SSRC0", 106)]),
MOV3,
ENDPGM,
]);
assert_eq!(r.unwrap().guarded, vec![28], "the v_mov at pc 28 needs its guard");
}

#[test]
fn a_mask_saved_before_a_per_lane_loop_is_still_known_after_it() {
// The same loop, then exec = s[0:1] & exec: s[0:1] was written before the loop, so every
// round leaves it as it was and each lane's bit is the console's; no guard is added.
let s = run(&[
("S_MOV_B64", "ENC_SOP1", &[("SDST", 0), ("SSRC0", 126)]),
CMP,
("V_ADD_NC_U32", "ENC_VOP2", &[("VDST", 4), ("SRC0", V + 4), ("VSRC1", 5)]),
("S_ANDN2_B64", "ENC_SOP2", &[("SDST", 126), ("SSRC0", 126), ("SSRC1", 106)]),
("S_CBRANCH_EXECNZ", "ENC_SOPP", &[("SIMM16", 0xFFFC)]),
RESTORE,
("S_AND_SAVEEXEC_B64", "ENC_SOP1", &[("SDST", 2), ("SSRC0", 0)]),
MOV3,
ENDPGM,
])
.unwrap();
assert!(s.guarded.is_empty(), "{:?}", s.guarded);
}

#[test]
fn a_loop_that_narrows_exec_is_found_on_its_back_edge() {
// s4 = 0; L: v_mov; v_cmp; exec &= vcc; s4 += 1; s_cmp_lt s4, 10; s_cbranch_scc1 L; v_mov
Expand Down
Loading