From e1273c289f461e860b02a6f02e2e139364fe808f Mon Sep 17 00:00:00 2001 From: Natalia Date: Sun, 29 Nov 2026 18:39:47 +0000 Subject: [PATCH] Recompiler: a mask a per-lane loop wrote is unknown after the loop (#3440) lane_masks follows each lane on its own: a lane that leaves a loop through a per-lane branch stops running it. On the console the wave runs on until every lane has left, and every later round writes the loop's masks again (a VALU compare writes 0 for lanes no longer in EXEC). After the loop such a mask holds the last round's bits, not each lane's own last value, so taking it as the lane's bit dropped needed guards. On an edge that leaves loops through a per-lane branch, every SGPR mask and the SCC whose function depends on an atom born inside those loops becomes a new atom (Atom::Exit); the SCC's kind becomes Unknown. EXEC is kept: the wave leaves when it is empty, which is each lane's own bit as it leaves. Masks set before the loop keep their value. Tests: the issue's program (failed with `left: []`, now [28]) and a control where a mask saved before the loop needs no guard. Co-Authored-By: Claude Opus 5.5 --- crates/xcl/src/gpu/recomp/lane_masks.rs | 109 ++++++++++++++++++++++-- 1 file changed, 103 insertions(+), 6 deletions(-) diff --git a/crates/xcl/src/gpu/recomp/lane_masks.rs b/crates/xcl/src/gpu/recomp/lane_masks.rs index 44b1e8a81a..2a933a0779 100644 --- a/crates/xcl/src/gpu/recomp/lane_masks.rs +++ b/crates/xcl/src/gpu/recomp/lane_masks.rs @@ -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) -> R { if a <= TRUE { @@ -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, } @@ -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 { + let s = self.succ[b][k].0; + let left: Vec = (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) } @@ -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