From 671939f3e54e8ff670f41d07d6bf985d505af474 Mon Sep 17 00:00:00 2001 From: brainbudt <32467201+brainbudt@users.noreply.github.com> Date: Thu, 26 Nov 2026 19:12:49 +0100 Subject: [PATCH] fix(shader): account for generated loop-cap returns in barrier and half-wave analyses --- crates/victory-shader/src/emit/lower.rs | 30 +- .../victory-shader/src/emit/lower/barriers.rs | 240 ++++++- .../victory-shader/src/emit/lower/halves.rs | 41 +- crates/victory-shader/src/emit/lower/tests.rs | 677 +++++++++++++++++- 4 files changed, 961 insertions(+), 27 deletions(-) diff --git a/crates/victory-shader/src/emit/lower.rs b/crates/victory-shader/src/emit/lower.rs index 3942e51a..98a1a0ea 100644 --- a/crates/victory-shader/src/emit/lower.rs +++ b/crates/victory-shader/src/emit/lower.rs @@ -25,7 +25,7 @@ mod atomics; mod bvh; mod halves; mod mesh; -use barriers::{in_step, split_wave_barriers}; +use barriers::{split_wave_barriers, step_and_cap}; pub(crate) use mesh::KeptOutputs; use mesh::prune_signature; @@ -104,6 +104,10 @@ pub(crate) struct Lowered { /// Barriers added for narrow host waves ([`split_wave_barriers`]). #[cfg(test)] pub split_barriers: usize, + /// Group barriers written (`dx.op.barrier`), for structural tests of + /// the exchanges between 32-lane halves. + #[cfg(test)] + pub barriers: usize, /// The `WaveSize` the program declares. #[cfg(test)] pub wave_size: Option, @@ -340,8 +344,13 @@ struct Lower<'a> { /// [`Lower::relabelled_flat`], once made. relabelled: Option, /// In halves: per IR block, whether every lane of the group runs it - /// together ([`in_step`]), where values can pass between the halves. + /// together ([`step_and_cap`]), where values can pass between the halves. step: Vec, + /// Per IR block, whether a lane may have left the program early at a + /// capped back edge's iteration limit before reaching it + /// ([`step_and_cap`]): an explicit `s_barrier` there would wait for + /// lanes that are gone. + cap_escaped: Vec, /// In halves: the group-shared dwords values pass between the halves /// through, once made ([`Lower::exchange_halves`]). half_words: Option, @@ -829,6 +838,7 @@ impl<'a> Lower<'a> { } } } + let (step, cap_escaped) = step_and_cap(p, x.st, halves); Lower { s, x, @@ -880,7 +890,8 @@ impl<'a> Lower<'a> { halves, halves_refused, relabelled: None, - step: if halves { in_step(p, x.st) } else { Vec::new() }, + step: if halves { step } else { Vec::new() }, + cap_escaped, half_words: None, level: 0, alike: if halves { @@ -1691,6 +1702,8 @@ impl<'a> Lower<'a> { #[cfg(test)] split_barriers: self.split_barriers.len(), #[cfg(test)] + barriers: self.s.m.written_calls("dx.op.barrier"), + #[cfg(test)] wave_size: self.s.wave_size, #[cfg(test)] uses_lane_index: self.s.m.has_function("dx.op.waveGetLaneIndex"), @@ -3948,8 +3961,15 @@ impl<'a> Lower<'a> { Fields::Sop { .. } => self.sop(inst), Fields::Sopp { .. } if name == "s_barrier" && self.no_barrier.contains(&b) => Ok(()), Fields::Sopp { .. } if name == "s_barrier" => { - self.group_barrier(); - Ok(()) + // A lane may have left the program early at a capped back + // edge's iteration limit before this block; a group barrier + // here would wait for lanes that are gone. + if self.cap_escaped.get(b).copied().unwrap_or(false) { + Err("an s_barrier where a lane may have left the program early at a loop's iteration limit") + } else { + self.group_barrier(); + Ok(()) + } } Fields::Sopp { .. } => Ok(()), Fields::Smem { .. } => self.smem(inst, (b, i)), diff --git a/crates/victory-shader/src/emit/lower/barriers.rs b/crates/victory-shader/src/emit/lower/barriers.rs index 70855e14..7ed45d26 100644 --- a/crates/victory-shader/src/emit/lower/barriers.rs +++ b/crates/victory-shader/src/emit/lower/barriers.rs @@ -56,7 +56,7 @@ pub(crate) fn split_wave_barriers(p: &Program, st: &Structured) -> Option Option Vec { - let Some(regions) = halves::branch_regions(st) else { - return once_per_lane(p); - }; +/// +/// The other half is per block whether a lane may have left the program +/// early at a capped back edge's iteration limit before reaching it. Lowering +/// caps an unconditional back edge with an early return at the loop +/// iteration limit (`LOOP_LIMIT`): a lane whose count reaches it leaves the +/// program from the latch. The header's counter is shared by *every* back +/// edge to it, so the cap check holds for every lane only when the latch +/// and the header run together and so does each back edge to the header — a +/// conditional one with a uniform condition. Otherwise one wave's count can +/// reach the limit at the latch while another goes on, and that wave leaves +/// there: no block reachable from that loop is run by every lane of the +/// group, the same number of times, so those blocks are not in step either. +/// The escape reaches past the loop too, along the sibling paths of every +/// divergent branch the latch is reachable from: a lane that took the other +/// arm never passes the latch's cap, so a barrier after that branch can +/// still be reached while a lane in the loop arm has left at the cap. The +/// walk back from the latch stops at a block every lane runs together, +/// though: every lane reconverges there before the cap exit, so a divergent +/// branch earlier than that, whose arms already reconverged there, is no +/// escape. +/// +/// With `need_step` false the step half is left out unless the program has +/// an `s_barrier` after a capped back edge, the only reader of the escape +/// half. +pub(crate) fn step_and_cap( + p: &Program, + st: &Structured, + need_step: bool, +) -> (Vec, Vec) { let n = p.blocks.len(); + // Reverse postorder ranks, as lowering numbers them: a Jump to a block at + // or before the jump is a back edge. + let order = crate::ir::reverse_postorder(&p.blocks); + let mut rpo = vec![usize::MAX; n]; + for (r, &b) in order.iter().enumerate() { + rpo[b] = r; + } + let back = |b: usize, t: usize| rpo[b] != usize::MAX && rpo[t] <= rpo[b]; + // The unconditional back edges, (latch, header): lowering counts the + // loop's iterations and at the limit leaves the program from the latch + // with an early return. + let capped: Vec<(usize, usize)> = (0..n) + .filter_map(|b| match st.exits[b] { + LaneExit::Jump(t) if back(b, t) => Some((b, t)), + _ => None, + }) + .collect(); + if !need_step + && (capped.is_empty() + || !p + .blocks + .iter() + .any(|b| b.insts.iter().any(|i| i.name() == "s_barrier"))) + { + return (Vec::new(), vec![false; n]); + } + let Some(regions) = halves::branch_regions(p, st) else { + return ( + if need_step { + once_per_lane(p) + } else { + Vec::new() + }, + vec![false; n], + ); + }; + let mut succ: Vec> = vec![Vec::new(); n]; + for (b, e) in st.exits.iter().enumerate() { + match *e { + LaneExit::Jump(t) => succ[b].push(t), + LaneExit::Branch { + taken, not_taken, .. + } => { + succ[b].push(taken); + succ[b].push(not_taken); + } + LaneExit::End | LaneExit::Indirect => {} + } + } + // Every back edge to each header: the header's iteration counter is + // shared by all of them. + let mut back_to: Vec> = vec![Vec::new(); n]; + for b in 0..n { + match st.exits[b] { + LaneExit::Jump(t) if back(b, t) => back_to[t].push(b), + LaneExit::Branch { + taken, not_taken, .. + } => { + if back(b, taken) { + back_to[taken].push(b); + } + if back(b, not_taken) { + back_to[not_taken].push(b); + } + } + _ => {} + } + } let scalar = |r: Reg| matches!(r, Reg::S(_) | Reg::Scc); // Not EXEC or VCC: lane masks. let mask = |r: Reg| matches!(r, Reg::S(k) if [EXEC, EXEC + 1, VCC, VCC + 1].contains(&k)); @@ -154,6 +247,12 @@ pub(crate) fn in_step(p: &Program, st: &Structured) -> Vec { _ => true, } }; + let together = |when: Bool, alike: &Set| { + st.bools + .support(when) + .iter() + .all(|a| matches!(a, Atom::Uniform(v) if alike.contains(v))) + }; let mut alike: Set = p .inputs .iter() @@ -177,17 +276,134 @@ pub(crate) fn in_step(p: &Program, st: &Structured) -> Vec { let LaneExit::Branch { when, .. } = st.exits[*d] else { continue; }; - let together = st - .bools - .support(when) - .iter() - .all(|a| matches!(a, Atom::Uniform(v) if alike.contains(v))); - if !together { + if !together(when, &alike) { for (b, &r) in region.iter().enumerate() { step[b] &= !r; } } } + // A capped back edge's cap check holds for every lane only when the + // latch and the header run together and so does every back edge to + // the header (a conditional one with a uniform condition): one the + // lanes do not run together can put one wave's count over the limit + // at the latch while another goes on, and that wave leaves the + // program there — so no block reachable from the loop is run by + // every lane of the group, the same number of times. + let cap_uniform = |latch: usize, header: usize, step: &[bool], alike: &Set| { + step[latch] + && step[header] + && back_to[header].iter().all(|&b| { + if !step[b] { + return false; + } + match st.exits[b] { + LaneExit::Jump(t) => t == header, + LaneExit::Branch { + when, + taken, + not_taken, + } => { + (taken == header || not_taken == header) + && together(when, alike) + } + _ => false, + } + }) + }; + let mut escaped = vec![false; n]; + // Reverse adjacency, to walk back from each latch to the branches + // that can reach it. + let mut preds: Vec> = vec![Vec::new(); n]; + for (b, ss) in succ.iter().enumerate() { + for &s in ss { + preds[s].push(b); + } + } + // Mark every block forward-reachable from `from`. + let mark = |from: usize, escaped: &mut Vec| { + let mut seen = vec![false; n]; + let mut stack = vec![from]; + seen[from] = true; + while let Some(b) = stack.pop() { + escaped[b] = true; + for &s in &succ[b] { + if !seen[s] { + seen[s] = true; + stack.push(s); + } + } + } + }; + loop { + let mut changed = false; + for &(latch, header) in &capped { + if cap_uniform(latch, header, &step, &alike) { + continue; + } + // Whether the latch is in step, before the walk from the + // header below clears it. + let latch_in_step = step[latch]; + let mut seen = vec![false; n]; + let mut stack = vec![header]; + seen[header] = true; + while let Some(b) = stack.pop() { + escaped[b] = true; + if std::mem::replace(&mut step[b], false) { + changed = true; + } + for &s in &succ[b] { + if !seen[s] { + seen[s] = true; + stack.push(s); + } + } + } + // The sibling paths of every divergent branch the latch is + // reachable from: a lane that took another arm of such a + // branch never passes the latch's cap, so it can still reach + // a barrier after the branch while a lane in the loop arm + // has left the program at the cap. (The branch's own block + // every lane runs, and a uniform branch sends every lane the + // same way, so neither is marked here.) + let arms = |d: usize, escaped: &mut Vec| { + if let LaneExit::Branch { when, .. } = st.exits[d] + && !together(when, &alike) + { + for &s in &succ[d] { + mark(s, escaped); + } + } + }; + let mut seen = vec![false; n]; + let mut stack = vec![latch]; + if latch_in_step { + // The latch is in step, but a back edge to the header + // the lanes do not run together shares its counter all + // the same: walk back from those sources too. + stack.extend(back_to[header].iter().copied().filter(|&b| !step[b])); + } + while let Some(b) = stack.pop() { + if std::mem::replace(&mut seen[b], true) { + continue; + } + arms(b, &mut escaped); + for &p in &preds[b] { + arms(p, &mut escaped); + // Walk back only through blocks the lanes do not run + // together: at a block every lane runs together they + // all reconverge before the cap exit, so an earlier + // divergent branch whose arms already reconverged + // there cannot strand a lane past the cap. + if !step[p] { + stack.push(p); + } + } + } + } + if !changed { + break; + } + } let mut next: Set = p .inputs .iter() @@ -213,7 +429,7 @@ pub(crate) fn in_step(p: &Program, st: &Structured) -> Vec { } } if next.len() == alike.len() { - return step; + return (step, escaped); } alike = next; } diff --git a/crates/victory-shader/src/emit/lower/halves.rs b/crates/victory-shader/src/emit/lower/halves.rs index 866cb0b1..983edc45 100644 --- a/crates/victory-shader/src/emit/lower/halves.rs +++ b/crates/victory-shader/src/emit/lower/halves.rs @@ -344,12 +344,35 @@ pub(super) const FIRST_ACROSS: Why = "a first-lane read of a value that may diff /// it that do not post-dominate it, which only the lanes taking one side may /// run (the branch's own block among them where a cycle leads back to it). /// None with an indirect exit. -pub(super) fn branch_regions(st: &Structured) -> Option)>> { +/// +/// Lowering caps an unconditional back edge — a `Jump` to a block at or +/// before the jump, by reverse postorder — with an early return at the loop +/// iteration limit: the latch leaves the program there too. That generated +/// exit is block `n` here, a terminal exit just like a `Ret`, so a closed +/// loop with a capped back edge does not look like it has no way out (which +/// would make everything post-dominate its branches). +pub(super) fn branch_regions(p: &Program, st: &Structured) -> Option)>> { let n = st.exits.len(); - let mut succ: Vec> = vec![Vec::new(); n]; + // Reverse postorder ranks, as lowering numbers them. + let order = crate::ir::reverse_postorder(&p.blocks); + let mut rpo = vec![usize::MAX; n]; + for (r, &b) in order.iter().enumerate() { + rpo[b] = r; + } + let capped = |b: usize| { + matches!(st.exits[b], LaneExit::Jump(t) if rpo[b] != usize::MAX && rpo[t] <= rpo[b]) + }; + // Block `n` is the virtual exit of every capped latch. + let m = n + 1; + let mut succ: Vec> = vec![Vec::new(); m]; for (b, e) in st.exits.iter().enumerate() { match *e { - LaneExit::Jump(t) => succ[b].push(t), + LaneExit::Jump(t) => { + succ[b].push(t); + if capped(b) { + succ[b].push(n); + } + } LaneExit::Branch { taken, not_taken, .. } => { @@ -361,9 +384,9 @@ pub(super) fn branch_regions(st: &Structured) -> Option)>> } } // Post-dominators, as bitsets (a virtual exit after every end). - let words = n.div_ceil(64); + let words = m.div_ceil(64); let full = vec![u64::MAX; words]; - let mut pdom: Vec> = (0..n) + let mut pdom: Vec> = (0..m) .map(|b| { if succ[b].is_empty() { let mut v = vec![0; words]; @@ -377,7 +400,7 @@ pub(super) fn branch_regions(st: &Structured) -> Option)>> let mut changed = true; while changed { changed = false; - for b in (0..n).rev() { + for b in (0..m).rev() { if succ[b].is_empty() { continue; } @@ -399,7 +422,7 @@ pub(super) fn branch_regions(st: &Structured) -> Option)>> if !matches!(e, LaneExit::Branch { .. }) { continue; } - let mut seen = vec![false; n]; + let mut seen = vec![false; m]; let mut stack: Vec = succ[d].clone(); while let Some(b) = stack.pop() { // The branch's own block is in its region if a cycle leads back @@ -410,7 +433,7 @@ pub(super) fn branch_regions(st: &Structured) -> Option)>> seen[b] = true; stack.extend(succ[b].iter().copied()); } - regions.push((d, seen)); + regions.push((d, seen[..n].to_vec())); } Some(regions) } @@ -452,7 +475,7 @@ fn wave_tid(x: &Parts) -> Option { pub(super) fn lane_uniform(x: &Parts) -> HashSet { let (p, st) = (x.p, x.st); let wave_tid = wave_tid(x); - let regions = branch_regions(st); + let regions = branch_regions(p, st); let is_mask = |v: Value| st.masks.contains_key(&v) || st.highs.contains_key(&v); let per_lane = |v: Value| x.res.waterfall.reads.contains(&v); let mut same: HashSet = HashSet::new(); diff --git a/crates/victory-shader/src/emit/lower/tests.rs b/crates/victory-shader/src/emit/lower/tests.rs index 276aa3c5..7247d9fa 100644 --- a/crates/victory-shader/src/emit/lower/tests.rs +++ b/crates/victory-shader/src/emit/lower/tests.rs @@ -1358,6 +1358,15 @@ fn split_barriers(words: &[u32], lanes: u32, narrow_waves: bool) -> usize { /// The same program, lowered. fn lowered(words: &[u32], lanes: u32, narrow_waves: bool) -> super::Lowered { + lowered_result(words, lanes, narrow_waves).expect("lower") +} + +/// The same program, lowered, as a result (a refusal is an `Err`). +fn lowered_result( + words: &[u32], + lanes: u32, + narrow_waves: bool, +) -> Result { let code: Vec = words.iter().flat_map(|w| w.to_le_bytes()).collect(); let native = NativeProgram { stage: HwStage::Cs, @@ -1393,7 +1402,6 @@ fn lowered(words: &[u32], lanes: u32, narrow_waves: bool) -> super::Lowered { host_wave32: false, view_marks: false, }) - .expect("lower") } #[test] @@ -1582,6 +1590,673 @@ fn a_read_before_divergent_writes_syncs_where_all_lanes_run() { assert_eq!(barriers_at(&wave64(&w)).unwrap(), []); } +/// Lowers a wave64 compute program of `lanes` threads in halves +/// (`host_wave32`, a host of 32-lane waves): the console wave runs as two +/// host waves, which pass values between them behind group barriers. +fn lowered_halves(words: &[u32], lanes: u32) -> Result { + let code: Vec = words.iter().flat_map(|w| w.to_le_bytes()).collect(); + let native = NativeProgram { + stage: HwStage::Cs, + wave64: true, + vgprs: 256, + layout: vec![], + used: vec![], + io: StageIo::Cs(ComputeIo { + // Two user-data SGPRs, no LDS. + rsrc2: 2 << 1, + threads: [lanes, 1, 1], + }), + code, + }; + let p = ir::build(&native.code, true, 256).unwrap(); + let st = structure::structure(&p).unwrap(); + let res = resource::track(&p, &native.layout, native.stage, Some(&st)).unwrap(); + let io = io::raise(&p, &native).unwrap(); + lower(&Parts { + native: &native, + p: &p, + st: &st, + io: &io, + res: &res, + inputs: vec![], + outputs: vec![], + prims: vec![], + mesh_stride: 0, + atomic_locks: false, + atomic_lock_rounds: false, + atomic_umax: false, + bias_as_gradients: false, + narrow_waves: false, + host_wave32: true, + view_marks: false, + }) +} + +/// A uniform outer loop (on the scalar `s20`) holding a divergent inner +/// loop — entered on `vcc`, the lanes with thread id below 40 — whose latch +/// is an unconditional `s_branch` back to its header: the back edge that +/// lowering caps with an early return at the loop iteration limit. After +/// the inner loop, `v_readlane_b32` of console lane 40, which in halves +/// passes between the 32-lane halves behind group barriers (its result is +/// only read after the outer loop, so no branch in the loop depends on it). +fn capped_back_edge_in_a_divergent_loop() -> Vec { + vec![ + 0xBE94_0380, // s_mov_b32 s20, 0 + 0x7D88_00FF, + 40, // v_cmp_gt_u32 vcc, 40, v0 + 0xBF87_0001, // s_cbranch_vccnz -> the inner loop + 0xBF82_0005, // s_branch -> the join + 0xBE95_0380, // s_mov_b32 s21, 0 + 0x8015_8115, // s_add_u32 s21, s21, 1 + 0xBF06_8315, // s_cmp_eq_u32 s21, 3 + 0xBF85_0001, // s_cbranch_scc1 -> the join + 0xBF82_FFFC, // s_branch -> the inner loop's header (unconditional back edge) + 0xD760_000C, + 0x0001_5100, // v_readlane_b32 s12, v0, 40 + 0x8014_8114, // s_add_u32 s20, s20, 1 + 0xBF0A_8214, // s_cmp_lt_u32 s20, 2 + 0xBF85_FFF2, // s_cbranch_scc1 -> the outer loop's header + 0xBF07_800C, // s_cmp_lg_u32 s12, 0 (after the outer loop) + 0xBF85_0001, // s_cbranch_scc1 -> s_endpgm + 0xBF80_0000, // s_nop + S_ENDPGM, + ] +} + +/// The same outer loop with an inner loop every lane enters together: its +/// unconditional back edge is capped too, but every lane of the group +/// leaves the loop through it together, so nothing after it is skipped. +fn capped_back_edge_in_a_uniform_loop() -> Vec { + vec![ + 0xBE94_0380, // s_mov_b32 s20, 0 + 0xBE95_0380, // s_mov_b32 s21, 0 + 0x8015_8115, // s_add_u32 s21, s21, 1 + 0xBF06_8315, // s_cmp_eq_u32 s21, 3 + 0xBF85_0001, // s_cbranch_scc1 -> the join + 0xBF82_FFFC, // s_branch -> the inner loop's header (unconditional back edge) + 0xD760_000C, + 0x0001_5100, // v_readlane_b32 s12, v0, 40 + 0x8014_8114, // s_add_u32 s20, s20, 1 + 0xBF0A_8214, // s_cmp_lt_u32 s20, 2 + 0xBF85_FFF6, // s_cbranch_scc1 -> the outer loop's header + 0xBF07_800C, // s_cmp_lg_u32 s12, 0 (after the outer loop) + 0xBF85_0001, // s_cbranch_scc1 -> s_endpgm + 0xBF80_0000, // s_nop + S_ENDPGM, + ] +} + +/// The divergent inner loop with a conditional back edge instead: the +/// iteration cap then only ends the loop through its normal exit, no lane +/// leaves the program early. +fn conditional_back_edge_in_a_divergent_loop() -> Vec { + vec![ + 0xBE94_0380, // s_mov_b32 s20, 0 + 0x7D88_00FF, + 40, // v_cmp_gt_u32 vcc, 40, v0 + 0xBF87_0001, // s_cbranch_vccnz -> the inner loop + 0xBF82_0004, // s_branch -> the join + 0xBE95_0380, // s_mov_b32 s21, 0 + 0x8015_8115, // s_add_u32 s21, s21, 1 + 0xBF0A_8315, // s_cmp_lt_u32 s21, 3 + 0xBF85_FFFD, // s_cbranch_scc1 -> the inner loop's header (conditional back edge) + 0xD760_000C, + 0x0001_5100, // v_readlane_b32 s12, v0, 40 + 0x8014_8114, // s_add_u32 s20, s20, 1 + 0xBF0A_8214, // s_cmp_lt_u32 s20, 2 + 0xBF85_FFF3, // s_cbranch_scc1 -> the outer loop's header + 0xBF07_800C, // s_cmp_lg_u32 s12, 0 (after the outer loop) + 0xBF85_0001, // s_cbranch_scc1 -> s_endpgm + 0xBF80_0000, // s_nop + S_ENDPGM, + ] +} + +/// The back-edge programs have the back edges their comments say: one +/// unconditional `s_branch` to a loop header where they say so, and the +/// words decode to the instructions their comments name. +#[test] +fn the_back_edge_programs_are_what_their_comments_say() { + let back_edges = |w: &[u32]| { + let p = wave64(w); + let order = crate::ir::reverse_postorder(&p.blocks); + let mut rpo = vec![usize::MAX; p.blocks.len()]; + for (r, &b) in order.iter().enumerate() { + rpo[b] = r; + } + p.blocks + .iter() + .enumerate() + .filter(|(b, blk)| match blk.exit { + ir::Exit::Jump(t) => rpo[*b] != usize::MAX && rpo[t] <= rpo[*b], + _ => false, + }) + .count() + }; + assert_eq!(back_edges(&capped_back_edge_in_a_divergent_loop()), 1); + assert_eq!(back_edges(&capped_back_edge_in_a_uniform_loop()), 1); + assert_eq!(back_edges(&conditional_back_edge_in_a_divergent_loop()), 0); + for w in [ + capped_back_edge_in_a_divergent_loop(), + capped_back_edge_in_a_uniform_loop(), + conditional_back_edge_in_a_divergent_loop(), + ] { + let code: Vec = w.iter().flat_map(|w| w.to_le_bytes()).collect(); + let all = crate::gfx10::decode(&code) + .unwrap() + .iter() + .map(|i| i.text(true)) + .collect::>() + .join("\n"); + for want in [ + "s_mov_b32 s20, 0", + "s_add_u32 s21, s21, 1", + "v_readlane_b32 s12, v0, 40", + "s_cmp_lg_u32 s12, 0", + "s_add_u32 s20, s20, 1", + "s_cmp_lt_u32 s20, 2", + "s_cbranch_scc1", + "s_branch", + "s_endpgm", + ] { + assert!(all.contains(want), "{want:?} not in:\n{all}"); + } + } + let code: Vec = capped_back_edge_in_a_divergent_loop() + .iter() + .flat_map(|w| w.to_le_bytes()) + .collect(); + let all = crate::gfx10::decode(&code) + .unwrap() + .iter() + .map(|i| i.text(true)) + .collect::>() + .join("\n"); + for want in ["v_cmp_gt_u32 vcc", "s_cbranch_vccnz", "s_cmp_eq_u32 s21, 3"] { + assert!(all.contains(want), "{want:?} not in:\n{all}"); + } +} + +/// A half-wave exchange after a divergent loop whose unconditional back +/// edge lowering caps with an early return: one wave may leave the program +/// at the cap while another reaches the exchange's group barrier with +/// missing participants, so the exchange is refused in halves. As one +/// wave64 there are no halves and it translates. +#[test] +fn an_exchange_after_a_divergent_capped_back_edge_is_refused() { + let w = capped_back_edge_in_a_divergent_loop(); + let why = lowered_halves(&w, 64) + .err() + .expect("refused in halves") + .to_string(); + assert!( + why.contains("a lane read that may cross 32-lane halves"), + "{why}" + ); + // One wave64: no halves, no exchange, and it translates. + let whole = lowered(&w, 64, false); + assert_eq!(whole.wave_size, Some(64)); +} + +/// The same exchange after a loop whose unconditional back edge is capped +/// but which every lane of the group runs together: all lanes leave at the +/// cap together, so the exchange stands in halves. +#[test] +fn an_exchange_after_a_uniform_capped_back_edge_translates() { + let w = capped_back_edge_in_a_uniform_loop(); + let l = lowered_halves(&w, 64).expect("translated in halves"); + assert_eq!(l.wave_size, Some(32)); + assert!(l.wave_ops); + // The exchange really is there: each half's store between two group + // barriers. + assert!(l.barriers > 0, "no dx.op.barrier written"); +} + +/// The same exchange after a divergent loop whose back edge is conditional: +/// the cap then ends the loop through its normal exit, so no lane leaves +/// early and the exchange stands in halves. +#[test] +fn an_exchange_after_a_divergent_conditional_back_edge_translates() { + let w = conditional_back_edge_in_a_divergent_loop(); + let l = lowered_halves(&w, 64).expect("translated in halves"); + assert_eq!(l.wave_size, Some(32)); + assert!(l.wave_ops); + assert!(l.barriers > 0, "no dx.op.barrier written"); +} + +/// A closed loop: its header's conditional back edge branches on `scc` from +/// comparing the loop-carried `s12` (a divergent condition), and the latch's +/// unconditional `s_branch` back to the header is the loop's only other +/// back edge — and the one lowering caps with an early return. Every back +/// edge to the header shares its iteration counter, so a lane whose compare +/// holds runs the conditional back edge and drives the shared count up; it +/// can reach the cap at the latch and leave the program there while another +/// lane goes on. The latch is still run by every lane of the group (it +/// post-dominates the branch), so only the shared-counter check can see the +/// escape: `v_readlane_b32` of console lane 40 in the loop, whose result the +/// header's branch reads, is refused in halves. +fn shared_counter_with_a_divergent_conditional_back_edge() -> Vec { + vec![ + 0xBE94_0380, // s_mov_b32 s20, 0 + 0xBF82_0000, // s_branch -> the loop header + 0xBF07_800C, // s_cmp_lg_u32 s12, 0 (the loop-carried exchange's result) + 0xBF85_FFFE, // s_cbranch_scc1 -> the loop header (conditional back edge) + 0xBF82_0000, // s_branch -> the exchange + 0xD760_000C, + 0x0001_5100, // v_readlane_b32 s12, v0, 40 + 0xBF82_0000, // s_branch -> the latch + 0xBF82_FFF9, // s_branch -> the loop header (unconditional latch, capped) + ] +} + +/// Blocker 1: the loop above shares one counter between its conditional and +/// unconditional back edges. The latch is in step, but the header is not +/// (the divergent branch's region) and the conditional back edge is not +/// uniform, so a lane may return early at the latch's cap while another +/// continues — the exchange in the loop is refused in halves. +#[test] +fn an_exchange_in_a_loop_whose_counter_a_divergent_back_edge_shares_is_refused() { + let w = shared_counter_with_a_divergent_conditional_back_edge(); + // The words decode to what the comments say: one conditional back edge + // to the header on vcc, one unconditional capped one from the latch. + let p = wave64(&w); + let order = crate::ir::reverse_postorder(&p.blocks); + let mut rpo = vec![usize::MAX; p.blocks.len()]; + for (r, &b) in order.iter().enumerate() { + rpo[b] = r; + } + let mut unconditional = 0; + let mut conditional = 0; + for (b, blk) in p.blocks.iter().enumerate() { + let back = |t: usize| rpo[b] != usize::MAX && rpo[t] <= rpo[b]; + match blk.exit { + ir::Exit::Jump(t) if back(t) => unconditional += 1, + ir::Exit::Branch { taken, not_taken } if back(taken) || back(not_taken) => { + conditional += 1; + } + _ => {} + } + } + assert_eq!(unconditional, 1, "one capped unconditional back edge"); + assert_eq!(conditional, 1, "one conditional back edge to the same header"); + assert_eq!( + p.blocks + .iter() + .filter(|b| b.insts.iter().any(|i| i.name() == "s_barrier")) + .count(), + 0 + ); + let why = lowered_halves(&w, 64) + .err() + .expect("refused in halves") + .to_string(); + assert!( + why.contains("a lane read that may cross 32-lane halves"), + "{why}" + ); +} + +/// A lane-dependent branch into a closed self-loop (its only edge is the +/// unconditional `s_branch` back to itself, which lowering caps with an +/// early return) against a sibling path holding `v_readlane_b32` of console +/// lane 40. The lanes in the self-loop never reach the sibling path — they +/// leave at the cap — so the exchange there is refused in halves. +fn closed_self_loop_next_to_a_lane_read() -> Vec { + vec![ + 0x7D88_00FF, + 40, // v_cmp_gt_u32 vcc, 40, v0 (vcc = v0 < 40) + 0xBF87_0001, // s_cbranch_vccnz -> the self-loop + 0xBF82_0001, // s_branch -> the sibling path + 0xBF82_FFFF, // s_branch -> itself: the closed self-loop (capped back edge) + 0xD760_000C, + 0x0001_5100, // v_readlane_b32 s12, v0, 40 + 0xBF07_800C, // s_cmp_lg_u32 s12, 0 + 0xBF85_0001, // s_cbranch_scc1 -> s_endpgm + 0xBF80_0000, // s_nop + S_ENDPGM, + ] +} + +/// Blocker 2: the closed self-loop has no exit in the console's CFG, but +/// lowering's early return at its capped back edge is one, so the sibling +/// path with the lane read does not post-dominate the lane-dependent branch +/// and is not a reconvergence point — the exchange is refused in halves. +#[test] +fn an_exchange_on_the_sibling_of_a_closed_capped_self_loop_is_refused() { + let w = closed_self_loop_next_to_a_lane_read(); + let p = wave64(&w); + let order = crate::ir::reverse_postorder(&p.blocks); + let mut rpo = vec![usize::MAX; p.blocks.len()]; + for (r, &b) in order.iter().enumerate() { + rpo[b] = r; + } + assert_eq!( + p.blocks + .iter() + .enumerate() + .filter(|(b, blk)| match blk.exit { + ir::Exit::Jump(t) => rpo[*b] != usize::MAX && rpo[t] <= rpo[*b], + _ => false, + }) + .count(), + 1, + "one capped unconditional back edge (the self-loop)" + ); + let why = lowered_halves(&w, 64) + .err() + .expect("refused in halves") + .to_string(); + assert!( + why.contains("a lane read that may cross 32-lane halves"), + "{why}" + ); +} + +/// The closed self-loop of [`closed_self_loop_next_to_a_lane_read`], with +/// `s_barrier; s_endpgm` on the sibling path instead of the lane read. The +/// lane read stays in the entry block (its result unused) so that halves +/// really runs. +fn closed_self_loop_next_to_an_s_barrier() -> Vec { + vec![ + 0x7D88_00FF, + 40, // v_cmp_gt_u32 vcc, 40, v0 (vcc = v0 < 40) + 0xD760_000C, + 0x0001_5100, // v_readlane_b32 s12, v0, 40 (unused; activates halves) + 0xBF87_0001, // s_cbranch_vccnz -> the self-loop (word 6) + 0xBF82_0001, // s_branch -> the sibling path (word 7) + 0xBF82_FFFF, // s_branch -> itself: the closed self-loop (capped back edge) + 0xBF8A_0000, // s_barrier on the sibling path + S_ENDPGM, + ] +} + +/// Blocker 3: an explicit `s_barrier` on the sibling path of a closed capped +/// self-loop is refused. The sibling is not reachable from the loop's +/// header, but the lane-dependent branch leads to the latch, so the escape +/// at the cap still reaches the sibling — in halves and as one wave64 alike. +#[test] +fn an_s_barrier_on_the_sibling_of_a_closed_capped_self_loop_is_refused() { + let w = closed_self_loop_next_to_an_s_barrier(); + // The words decode to what the comments say: one capped back edge (the + // self-loop) and the s_barrier on the sibling path. + let p = wave64(&w); + let order = crate::ir::reverse_postorder(&p.blocks); + let mut rpo = vec![usize::MAX; p.blocks.len()]; + for (r, &b) in order.iter().enumerate() { + rpo[b] = r; + } + assert_eq!( + p.blocks + .iter() + .enumerate() + .filter(|(b, blk)| match blk.exit { + ir::Exit::Jump(t) => rpo[*b] != usize::MAX && rpo[t] <= rpo[*b], + _ => false, + }) + .count(), + 1, + "one capped unconditional back edge (the self-loop)" + ); + assert_eq!( + p.blocks + .iter() + .filter(|b| b.insts.iter().any(|i| i.name() == "s_barrier")) + .count(), + 1 + ); + for why in [ + lowered_halves(&w, 64).err().expect("refused in halves"), + lowered_result(&w, 64, false) + .err() + .expect("refused as one wave64"), + ] { + let why = why.to_string(); + assert!( + why.contains("an s_barrier where a lane may have left the program early"), + "{why}" + ); + } +} + +/// The same closed self-loop, but the `s_barrier` sits before the +/// lane-dependent branch: every lane runs that block once before any lane +/// can have left at the cap, so the barrier is kept. +fn an_s_barrier_before_the_branch_of_a_closed_self_loop() -> Vec { + vec![ + 0xBF8A_0000, // s_barrier, before the branch + 0xD760_000C, + 0x0001_5100, // v_readlane_b32 s12, v0, 40 (unused; activates halves) + 0x7D88_00FF, + 40, // v_cmp_gt_u32 vcc, 40, v0 + 0xBF87_0001, // s_cbranch_vccnz -> the self-loop (word 7) + 0xBF82_0001, // s_branch -> the sibling path (word 8) + 0xBF82_FFFF, // s_branch -> itself: the closed self-loop (capped back edge) + S_ENDPGM, // the sibling path + ] +} + +/// An `s_barrier` before the divergent branch that leads to a closed capped +/// self-loop is not marked escaped: it translates, in halves and as one +/// wave64 alike. +#[test] +fn an_s_barrier_before_the_branch_of_a_closed_capped_self_loop_translates() { + let w = an_s_barrier_before_the_branch_of_a_closed_self_loop(); + let l = lowered_halves(&w, 64).expect("translated in halves"); + assert_eq!(l.wave_size, Some(32)); + assert!(l.barriers > 0, "no dx.op.barrier written in halves"); + let whole = lowered(&w, 64, false); + assert_eq!(whole.wave_size, Some(64)); + assert!(whole.barriers > 0, "no dx.op.barrier written as one wave64"); + // The explicit s_barrier is one barrier beyond the same program with an + // s_nop in its place. + let mut nop = w.clone(); + nop[0] = 0xBF80_0000; // s_nop instead of the s_barrier + let without = lowered_halves(&nop, 64).expect("translated in halves"); + assert_eq!( + l.barriers, + without.barriers + 1, + "the s_barrier adds exactly one dx.op.barrier" + ); +} + +/// An earlier divergent branch whose arms reconverge at a join holding an +/// `s_barrier`, then a later divergent branch into a closed capped self-loop +/// (its only edge the unconditional `s_branch` back to itself, which +/// lowering caps with an early return). The lane read stays in the entry +/// block (its result unused) so that halves really runs. +fn an_s_barrier_at_an_earlier_join_before_a_closed_capped_self_loop() -> Vec { + vec![ + 0xD760_000C, + 0x0001_5100, // v_readlane_b32 s12, v0, 40 (unused; activates halves) + 0x7D88_00FF, + 40, // v_cmp_gt_u32 vcc, 40, v0 (the earlier divergent branch) + 0xBF87_0001, // s_cbranch_vccnz -> arm A (word 6) + 0xBF82_0001, // s_branch -> the join (word 7) + 0xBF80_0000, // s_nop, arm A -> the join (word 7) + 0xBF8A_0000, // s_barrier at the join (word 7) + 0x7D88_00FF, + 40, // v_cmp_gt_u32 vcc, 40, v0 (the later divergent branch) + 0xBF87_0001, // s_cbranch_vccnz -> the self-loop (word 12) + 0xBF82_0001, // s_branch -> the sibling path (word 13) + 0xBF82_FFFF, // s_branch -> itself: the closed self-loop (capped back edge) + S_ENDPGM, // the sibling path (word 13) + ] +} + +/// An `s_barrier` at the join of an earlier divergent branch, before a later +/// divergent branch into a closed capped self-loop, is not marked escaped: +/// every lane runs the join before any lane can have left at the self-loop's +/// cap, so the escape at the cap must not reach back past the reconvergence +/// at the join. It translates in halves and as one wave64 alike. +#[test] +fn an_s_barrier_at_an_earlier_join_before_a_closed_capped_self_loop_translates() { + let w = an_s_barrier_at_an_earlier_join_before_a_closed_capped_self_loop(); + // The words give one capped back edge (the self-loop) and one s_barrier + // at the earlier join. + let p = wave64(&w); + let order = crate::ir::reverse_postorder(&p.blocks); + let mut rpo = vec![usize::MAX; p.blocks.len()]; + for (r, &b) in order.iter().enumerate() { + rpo[b] = r; + } + assert_eq!( + p.blocks + .iter() + .enumerate() + .filter(|(b, blk)| match blk.exit { + ir::Exit::Jump(t) => rpo[*b] != usize::MAX && rpo[t] <= rpo[*b], + _ => false, + }) + .count(), + 1, + "one capped unconditional back edge (the self-loop)" + ); + assert_eq!( + p.blocks + .iter() + .filter(|b| b.insts.iter().any(|i| i.name() == "s_barrier")) + .count(), + 1 + ); + let l = lowered_halves(&w, 64).expect("translated in halves"); + assert_eq!(l.wave_size, Some(32)); + assert!(l.barriers > 0, "no dx.op.barrier written in halves"); + let whole = lowered(&w, 64, false); + assert_eq!(whole.wave_size, Some(64)); + assert!(whole.barriers > 0, "no dx.op.barrier written as one wave64"); + // The explicit s_barrier is one barrier beyond the same program with an + // s_nop at the join. + let mut nop = w.clone(); + nop[7] = 0xBF80_0000; // s_nop instead of the s_barrier at the join + let without = lowered_halves(&nop, 64).expect("translated in halves"); + assert_eq!( + l.barriers, + without.barriers + 1, + "the s_barrier adds exactly one dx.op.barrier" + ); +} + +/// The same shape with a uniform branch (on `scc` from comparing the +/// constant `s20`): every lane takes the same arm, so no lane can be at the +/// sibling's barrier while another left at the cap, and the barrier is +/// kept. +fn an_s_barrier_on_the_sibling_behind_a_uniform_branch() -> Vec { + vec![ + 0xBE94_0380, // s_mov_b32 s20, 0 + 0xD760_000C, + 0x0001_5100, // v_readlane_b32 s12, v0, 40 (unused; activates halves) + 0xBF07_8014, // s_cmp_lg_u32 s20, 0 (uniform: s20 is the constant 0) + 0xBF85_0001, // s_cbranch_scc1 -> the self-loop (word 6) + 0xBF82_0001, // s_branch -> the sibling path (word 7) + 0xBF82_FFFF, // s_branch -> itself: the closed self-loop (capped back edge) + 0xBF8A_0000, // s_barrier on the sibling path + S_ENDPGM, + ] +} + +/// An `s_barrier` on the sibling path behind a uniform branch is not marked +/// escaped either: the branch sends every lane the same way. +#[test] +fn an_s_barrier_on_the_sibling_behind_a_uniform_branch_translates() { + let w = an_s_barrier_on_the_sibling_behind_a_uniform_branch(); + let l = lowered_halves(&w, 64).expect("translated in halves"); + assert_eq!(l.wave_size, Some(32)); + assert!(l.barriers > 0, "no dx.op.barrier written in halves"); + let whole = lowered(&w, 64, false); + assert_eq!(whole.wave_size, Some(64)); + assert!(whole.barriers > 0, "no dx.op.barrier written as one wave64"); + // The explicit s_barrier is one barrier beyond the same program with an + // s_nop in its place. + let mut nop = w.clone(); + nop[7] = 0xBF80_0000; // s_nop instead of the s_barrier on the sibling path + let without = lowered_halves(&nop, 64).expect("translated in halves"); + assert_eq!( + l.barriers, + without.barriers + 1, + "the s_barrier adds exactly one dx.op.barrier" + ); +} + +/// The divergent capped loop of [`capped_back_edge_in_a_divergent_loop`], +/// with an explicit `s_barrier` at the join instead of the lane read: a lane +/// may have left the program early at the inner loop's cap, so the barrier +/// is refused even without any exchange. +fn a_divergent_capped_loop_then_an_s_barrier_at_the_join() -> Vec { + vec![ + 0xBE94_0380, // s_mov_b32 s20, 0 + 0x7D88_00FF, + 40, // v_cmp_gt_u32 vcc, 40, v0 + 0xBF87_0001, // s_cbranch_vccnz -> the inner loop + 0xBF82_0005, // s_branch -> the join + 0xBE95_0380, // s_mov_b32 s21, 0 + 0x8015_8115, // s_add_u32 s21, s21, 1 + 0xBF06_8315, // s_cmp_eq_u32 s21, 3 + 0xBF85_0001, // s_cbranch_scc1 -> the join + 0xBF82_FFFC, // s_branch -> the inner loop's header (capped back edge) + 0xBF8A_0000, // s_barrier at the join + 0xBF80_0000, // s_nop + 0x8014_8114, // s_add_u32 s20, s20, 1 + 0xBF0A_8214, // s_cmp_lt_u32 s20, 2 + 0xBF85_FFF2, // s_cbranch_scc1 -> the outer loop's header + 0xBF07_8000, // s_cmp_lg_u32 s0, 0 + 0xD760_000C, + 0x0001_5100, // v_readlane_b32 s12, v0, 40 (unused; activates halves) + S_ENDPGM, + ] +} + +/// Blocker 3: an explicit `s_barrier` at the join after a divergent loop +/// whose capped back edge may have let a lane leave the program early is +/// refused, in halves and as one wave64 alike. +#[test] +fn an_s_barrier_after_a_divergent_capped_loop_is_refused() { + let w = a_divergent_capped_loop_then_an_s_barrier_at_the_join(); + for why in [ + lowered_halves(&w, 64).err().expect("refused in halves"), + lowered_result(&w, 64, false) + .err() + .expect("refused as one wave64"), + ] { + let why = why.to_string(); + assert!( + why.contains("an s_barrier where a lane may have left the program early"), + "{why}" + ); + } +} + +/// The same `s_barrier` where no lane can have left early — a uniform loop +/// with a capped back edge, every lane leaving at the cap together — is +/// kept: the cap analysis does not reject legitimate barriers. The lane +/// read at the join stays (it is what makes halves run), and the explicit +/// `s_barrier` goes just before `s_endpgm`. +#[test] +fn an_s_barrier_after_a_uniform_capped_loop_translates() { + let mut w = capped_back_edge_in_a_uniform_loop(); + // Keep the v_readlane at the join (word 6), so halves really runs, and + // put the explicit s_barrier where the s_nop before s_endpgm was. + w[13] = 0xBF8A_0000; // s_barrier + let l = lowered_halves(&w, 64).expect("translated in halves"); + assert_eq!(l.wave_size, Some(32)); + assert!(l.barriers > 0, "no dx.op.barrier written in halves"); + // And as one wave64, where the barrier is the program's own. + let whole = lowered(&w, 64, false); + assert_eq!(whole.wave_size, Some(64)); + assert!( + whole.barriers > 0, + "no dx.op.barrier written as one wave64" + ); + // The explicit s_barrier is one barrier beyond the same program without + // it (the fixture itself, whose word 13 is the s_nop). + let without = lowered_halves(&capped_back_edge_in_a_uniform_loop(), 64) + .expect("translated in halves"); + assert_eq!( + l.barriers, + without.barriers + 1, + "the s_barrier adds exactly one dx.op.barrier" + ); +} + #[test] fn ds_bpermute_wave64_preserves_32_lane_half() { // Without reading v0 (`thread_id`), only the wave64 half-wave base