diff --git a/README.md b/README.md index 19fc5755f..d060fce33 100644 --- a/README.md +++ b/README.md @@ -153,6 +153,8 @@ xcl rpf D:\data\shaders.rpf --extract NAME --out folder `XCL_PSO_TRACE=1` logs every driver compile. A huge compute program the driver refuses is logged with the driver's verdict (`the driver refused the compute PSO of record HASH … with 0x…`): its dispatches are skipped. +- With `XCL_DIRECT_DXIL=1` and `XCL_PER_LANE=1`, eligible wave32 compute loops can use + per-lane control flow even when the next iteration reads the preceding iteration's scalar counter. - With `XCL_DRED=1`, a D3D12 device removal logs GPU breadcrumbs in `xcl.log`. XCL examines up to 4,096 command lists and reports up to 64 unfinished lists, starting with those on which the GPU completed at least one operation. diff --git a/crates/xcl/src/gpu/recomp/lane_masks.rs b/crates/xcl/src/gpu/recomp/lane_masks.rs index 44b1e8a81..f66e98408 100644 --- a/crates/xcl/src/gpu/recomp/lane_masks.rs +++ b/crates/xcl/src/gpu/recomp/lane_masks.rs @@ -256,6 +256,8 @@ pub struct Block { pub preds: Vec, /// it ends in a branch on a lane mask: each lane takes it on its own bit pub lane_branch: bool, + /// WREXEC destination to reset along with SCC on a proven empty wave32 loop exit. + pub(super) empty_loop_wrexec: Option, } /// What the analysis found in a program. @@ -275,6 +277,17 @@ pub struct Summary { /// blocks the entry reaches: every block after its forward predecessors) pub blocks: Vec, pub order: Vec, + /// each block's immediate dominator (the entry's is itself; `usize::MAX`: not reached), and + /// for each block: Some(blocks of its loop) when it heads one + pub idom: Vec, + pub body: Vec>>, +} + +impl Summary { + /// Whether block a dominates block b (b reached from the entry). + pub fn dominates(&self, a: usize, b: usize) -> bool { + dominates(&self.idom, a, b) + } } /// Whether `d` reads SCC as a non-branch input (either implicitly via `OPR_SSRC_SPECIAL_SCC` @@ -291,6 +304,19 @@ fn reads_scc(d: &Decoded) -> bool { .any(|f| rdna2::field_value(d.enc, &d.words, f) == Some(253)) } +fn dominates(idom: &[usize], a: usize, mut b: usize) -> bool { + while idom[b] != usize::MAX { + if b == a { + return true; + } + if idom[b] == b { + break; + } + b = idom[b]; + } + false +} + /// Executed with the lane's EXEC bit (VOPC and the lane reads / writes are mask producers). fn is_vector(d: &Decoded) -> bool { let n = d.inst.name; @@ -313,6 +339,7 @@ struct Lanes<'a> { preds: Vec>, /// reverse postorder, and for each block: Some(blocks of its loop) when it heads one order: Vec, + idom: Vec, body: Vec>>, /// per instruction: SGPRs it overwrites (`x64_masks::uses`), SGPRs it may change, whether /// it writes SCC @@ -410,22 +437,13 @@ impl<'a> Lanes<'a> { break; } } - let dominates = |h: usize, mut b: usize| loop { - if b == h { - return true; - } - if b == 0 { - return false; - } - b = idom[b]; - }; let mut body: Vec>> = vec![None; n]; for &h in &order { for &(p, _) in &preds[h] { if rpo[p] < rpo[h] { continue; } - if !dominates(h, p) { + if !dominates(&idom, h, p) { return Err("irreducible control flow"); } let inside = body[h].get_or_insert_with(|| vec![false; n]); @@ -448,7 +466,7 @@ impl<'a> Lanes<'a> { (written, kill, d.ienc.operands.iter().any(|o| o.output && o.ty == "OPR_SSRC_SPECIAL_SCC")) }) .collect(); - Ok(Lanes { insts, wave64, launch, wave_branches: false, bdd: Bdd::new(), atoms: HashMap::new(), born: Vec::new(), blocks, succ, preds, order, body, effects }) + Ok(Lanes { insts, wave64, launch, wave_branches: false, bdd: Bdd::new(), atoms: HashMap::new(), born: Vec::new(), blocks, succ, preds, order, idom, body, effects }) } fn atom(&mut self, a: Atom, block: usize) -> R { @@ -557,7 +575,8 @@ impl<'a> Lanes<'a> { // A bitwise scalar instruction can mix a tracked mask and an SGPR // holding a number. The lowering extracts each lane's bit of that // number. Its unknown bits must not turn the result's SCC uniform. - if matches!(op, "AND" | "OR" | "XOR" | "ANDN2" | "ORN2" | "ANDN1" | "NAND" | "NOR" | "XNOR") + // ANDN1 and ORN1 occur only in the EXEC forms handled above. + if matches!(op, "AND" | "OR" | "XOR" | "ANDN2" | "ORN2" | "NAND" | "NOR" | "XNOR") && (sd == Some(EXEC) || (s0 < 128 && a.is_some()) || (s1 < 128 && b.is_some())) { a = Some(self.need(st, d, s0, block)?); @@ -802,13 +821,34 @@ impl<'a> Lanes<'a> { Ok((reach, st)) } - fn analyze(mut self) -> R { + /// WREXEC copied the final EXEC to its destination and set SCC to any(EXEC). + /// With this sole predecessor, the whole wave reaches b only once EXEC is empty. + fn empty_loop_wrexec(&self, b: usize) -> Option { + let &[(pred, _)] = self.preds[b].as_slice() else { return None }; + if pred == b || self.succ[pred] != [(pred, Edge::Taken), (b, Edge::NotTaken)] { + return None; + } + let (start, end) = self.blocks[pred]; + let [.., write, branch] = &self.insts[start..end] else { return None }; + if branch.inst.name != "S_CBRANCH_EXECNZ" || !write.inst.name.ends_with("_WREXEC_B32") { + return None; + } + rdna2::field_value(write.enc, &write.words, "SDST").map(|r| r as u8) + } + + fn analyze(mut self, repair_empty_exits: bool) -> R { let n = self.blocks.len(); let mut entry = State { regs: vec![None; 128], scc: None }; entry.regs[EXEC] = Some(match self.launch { LaunchExec::All => TRUE, LaunchExec::Unknown => self.atom(Atom::Launch, 0)?, }); + let mut empty_exits = vec![None; n]; + if repair_empty_exits && !self.wave64 { + for &b in &self.order { + empty_exits[b] = self.empty_loop_wrexec(b); + } + } let mut forced = HashSet::new(); let mut reach = vec![FALSE; n]; let mut ins: Vec> = vec![None; n]; @@ -833,7 +873,13 @@ impl<'a> Lanes<'a> { if !std::mem::take(&mut dirty[b]) { continue; } - let (r, st) = if b == 0 { (TRUE, entry.clone()) } else { self.join(b, &outs, &forced, false)? }; + let (r, mut st) = if b == 0 { (TRUE, entry.clone()) } else { self.join(b, &outs, &forced, false)? }; + if let Some(dst) = empty_exits[b] { + st.scc = Some((FALSE, SccKind::Uniform)); + if dst != 125 { + st.regs[dst as usize] = Some(FALSE); + } + } if ins[b].as_ref() == Some(&st) && reach[b] == r { continue; } @@ -934,9 +980,12 @@ impl<'a> Lanes<'a> { succ: self.succ[b].clone(), preds: self.preds[b].iter().map(|p| p.0).collect(), lane_branch: lane_branch[b], + empty_loop_wrexec: empty_exits[b], }) .collect(); sum.order = self.order.clone(); + sum.idom = self.idom.clone(); + sum.body = self.body.clone(); Ok(sum) } } @@ -948,16 +997,17 @@ pub fn analyze(insts: &[Decoded], wave64: bool) -> R { /// Analyses a decoded program (far jumps resolved) that starts with EXEC as `launch` says. pub fn analyze_launch(insts: &[Decoded], wave64: bool, launch: LaunchExec) -> R { - analyze_branches(insts, wave64, launch, false) + // Without a lowering stage, rely only on facts that hold without exit repairs. + analyze_branches(insts, wave64, launch, false, false) } -pub(super) fn analyze_branches(insts: &[Decoded], wave64: bool, launch: LaunchExec, wave_branches: bool) -> R { +pub(super) fn analyze_branches(insts: &[Decoded], wave64: bool, launch: LaunchExec, wave_branches: bool, repair_empty_exits: bool) -> R { if insts.is_empty() { return Ok(Summary::default()); } let mut lanes = Lanes::new(insts, wave64, launch)?; lanes.wave_branches = wave_branches; - lanes.analyze() + lanes.analyze(repair_empty_exits) } /// Decodes and analyses a program. @@ -1094,6 +1144,15 @@ mod tests { assert_eq!((s.vector, s.guarded.len(), s.lane_branches), (2, 1, 1)); } + #[test] + fn dominance_ends_at_the_entry_even_when_it_is_not_block_zero() { + // 2 is the entry, 0 its child, 1 its grandchild. Block 3 is unreachable. + let idom = [2, 0, 2, usize::MAX]; + assert!(dominates(&idom, 2, 1)); + assert!(!dominates(&idom, 1, 0)); + assert!(!dominates(&idom, 3, 3)); + } + #[test] fn unsupported_control_flow_is_refused() { // a loop entered at two places diff --git a/crates/xcl/src/gpu/recomp/lower.rs b/crates/xcl/src/gpu/recomp/lower.rs index ca82cf429..27d14999c 100644 --- a/crates/xcl/src/gpu/recomp/lower.rs +++ b/crates/xcl/src/gpu/recomp/lower.rs @@ -176,6 +176,9 @@ struct Lower<'a, I: Ir> { /// inside a coalesced guarded run's own block: only lanes with EXEC set enter, so writes /// need no per-instruction guard unguarded: bool, + /// per IR block of the program: its analysis block, and whether it is a coalesced run's + /// block (only the lanes with their EXEC bit enter it) + place: Vec>, /// the IR block each mask bit was written in, and the mask bits read (block, value) def_block: std::collections::HashMap, lane_uses: Vec<(usize, I::V)>, @@ -1033,7 +1036,9 @@ impl<'a, I: Ir> Lower<'a, I> { /// Whether a lane reading mask bit v in block c may see its own stale bit: v (or a phi /// operand reaching it) was written in another block that not every lane passed through. /// The hardware's whole-wave write left the absent lanes' bits 0 there; this lane kept its - /// own older one. EXEC is not concerned: an absent lane's bit is 0 in both. + /// own older one. EXEC is not concerned: an absent lane's bit is 0 in both. Unless that + /// block dominates c and c is in every loop it is in: then every lane reading in c passed + /// the write, in the same pass as the wave's last one. fn stale(&self, c: usize, v: I::V) -> bool { let mut seen = HashSet::new(); let mut work = vec![v]; @@ -1046,7 +1051,7 @@ impl<'a, I: Ir> Lower<'a, I> { Some(ops) => work.extend_from_slice(ops), None => { if let Some(&b) = self.def_block.get(&v) { - if self.def_owner[b] != self.use_owner[c] && self.partial[b] { + if self.def_owner[b] != self.use_owner[c] && self.partial[b] && !self.passed(b, c) { return true; } } @@ -1056,6 +1061,33 @@ impl<'a, I: Ir> Lower<'a, I> { false } + /// Whether a read in c can use a definition from w without seeing an older iteration + /// or a value from a lane that skipped w. Kernel ABI stores have no program block. + fn passed(&self, w: usize, c: usize) -> bool { + let (Some((bw, run)), Some((bc, _))) = (self.place[w], self.place[c]) else { + return false; + }; + if run { + // a coalesced run's block: only the lanes with their EXEC bit pass it + return false; + } + let sum = self.sum; + // Lanes::new rejects irreducible control flow before constructing the loop bodies. + if sum.dominates(bw, bc) { + return sum.body.iter().flatten().all(|inside| !inside[bw] || inside[bc]); + } + // At the header's first execution the latch has never run. Every lane that + // returns to the header passed the sole latch on its preceding iteration. + // An enclosing loop could carry a value of this latch into another entry, + // and a nested loop at the write could leave an earlier iteration's value. + if self.wave64 || self.graphics { + return false; + } + let Some(body) = &sum.body[bc] else { return false }; + body[bw] && sum.blocks[bc].preds.iter().filter(|&&p| body[p]).all(|&p| p == bw) + && sum.body.iter().enumerate().all(|(h, other)| h == bc || !other.as_ref().is_some_and(|inside| inside[bw])) + } + /// Whether v is (or is a phi reaching) `marker`. fn reaches(&self, v: I::V, marker: I::V) -> bool { let mut seen = HashSet::new(); @@ -1175,7 +1207,7 @@ fn lower_with(insts: &[Decoded], stage: Stage, wave64: bool, mut ir: I, s // Ballots reconstruct every bit only when the entire console wave is present. let wave_branches = matches!(stage, Stage::Compute(cs) if cs.threads.iter().product::() % (if wave64 { 64 } else { 32 }) == 0) && !ir.whole_needs_exchange(); - let sum = lane_masks::analyze_branches(insts, wave64, launch, wave_branches).map_err(|e| e.to_string())?; + let sum = lane_masks::analyze_branches(insts, wave64, launch, wave_branches, !graphics).map_err(|e| e.to_string())?; let n = sum.blocks.len(); if n == 0 { return Err("empty program".into()); @@ -1199,8 +1231,10 @@ fn lower_with(insts: &[Decoded], stage: Stage, wave64: bool, mut ir: I, s let mut partial = vec![false; count]; let mut def_owner: Vec = (0..count).collect(); let mut use_owner: Vec = (0..count).collect(); + let mut place = vec![None; count]; for (b, p) in partial_blocks(&sum).into_iter().enumerate() { partial[blk(b)] = p; + place[blk(b)] = Some((b, false)); for &(_, s, c) in &splits[b] { partial[s] = true; partial[c] = p; @@ -1208,6 +1242,8 @@ fn lower_with(insts: &[Decoded], stage: Stage, wave64: bool, mut ir: I, s use_owner[s] = blk(b); def_owner[c] = blk(b); use_owner[c] = blk(b); + place[s] = Some((b, true)); + place[c] = Some((b, false)); } } let ends: Vec = sum.order.iter().copied().filter(|&b| sum.blocks[b].succ.is_empty()).collect(); @@ -1260,6 +1296,7 @@ fn lower_with(insts: &[Decoded], stage: Stage, wave64: bool, mut ir: I, s def_owner, use_owner, unguarded: false, + place, def_block: std::collections::HashMap::new(), lane_uses: Vec::new(), dpp_src: None, @@ -1409,6 +1446,13 @@ fn lower_with(insts: &[Decoded], stage: Stage, wave64: bool, mut ir: I, s if !sealed[ib] { sealed[ib] = seal_ready(&mut l, &done, ib); } + if let Some(r) = sum.blocks[b].empty_loop_wrexec { + let zero = l.ir.boolean(false); + l.write_scc(ib, zero); + if r != 125 { + l.write_bit(ib, r, zero); + } + } if b == 0 && !pixel { // the analysis takes the lanes that start the program as its EXEC (a PS keeps the // launch EXEC from its start: there the analysis does not know it) @@ -2214,6 +2258,25 @@ pub(crate) mod tests { ("V_ADD_NC_U32", "ENC_VOP2", &[("VDST", 2), ("SRC0", 129), ("VSRC1", 1)]), ENDPGM, ]; + /// v_cmp_gt_u32 vcc, 16, v0; s_and_saveexec s0, vcc; s_cbranch_execz L; s_mov s4, 5; + /// v_cmp_gt_u32 vcc, 8, v0; s_and_saveexec s1, vcc; s_cbranch_execz M; v_mov v1, 7; + /// M: exec |= s1; v_cndmask v3, 0, v0, vcc; v_add v2, s4, v3; L: exec |= s0: s4, VCC and s1 + /// are written in the lanes below 16 and read by them past the inner if + pub(crate) const REGION_DIAMOND: &[I] = &[ + ("V_CMP_GT_U32", "ENC_VOPC", &[("SRC0", 144), ("VSRC1", 0)]), + ("S_AND_SAVEEXEC_B32", "ENC_SOP1", &[("SDST", 0), ("SSRC0", 106)]), + ("S_CBRANCH_EXECZ", "ENC_SOPP", &[("SIMM16", 8)]), + ("S_MOV_B32", "ENC_SOP1", &[("SDST", 4), ("SSRC0", 133)]), + ("V_CMP_GT_U32", "ENC_VOPC", &[("SRC0", 136), ("VSRC1", 0)]), + ("S_AND_SAVEEXEC_B32", "ENC_SOP1", &[("SDST", 1), ("SSRC0", 106)]), + ("S_CBRANCH_EXECZ", "ENC_SOPP", &[("SIMM16", 1)]), + ("V_MOV_B32", "ENC_VOP1", &[("VDST", 1), ("SRC0", 135)]), + ("S_OR_B32", "ENC_SOP2", &[("SDST", 126), ("SSRC0", 126), ("SSRC1", 1)]), + ("V_CNDMASK_B32", "ENC_VOP2", &[("VDST", 3), ("SRC0", 128), ("VSRC1", 0)]), + ("V_ADD_NC_U32", "ENC_VOP2", &[("VDST", 2), ("SRC0", 4), ("VSRC1", 3)]), + ("S_OR_B32", "ENC_SOP2", &[("SDST", 126), ("SSRC0", 126), ("SSRC1", 0)]), + ENDPGM, + ]; /// s_mov s0, exec; L: v_add v1, v1, 1; v_cmp_ge_u32 vcc, v1, v0; exec &= ~vcc; /// s_cbranch_execnz L; exec |= s0 → v1 = max(1, lane) pub(crate) const LEAVING_LOOP: &[I] = &[ @@ -2225,6 +2288,34 @@ pub(crate) mod tests { ("S_OR_B32", "ENC_SOP2", &[("SDST", 126), ("SSRC0", 126), ("SSRC1", 0)]), ENDPGM, ]; + // Each surviving lane records the scalar counter before the latch increments it. + pub(crate) const LATCH_COUNTER: &[I] = &[ + ("S_MOV_B32", "ENC_SOP1", &[("SDST", 0), ("SSRC0", 126)]), + ("S_MOV_B32", "ENC_SOP1", &[("SDST", 1), ("SSRC0", 128)]), + ("V_MOV_B32", "ENC_VOP1", &[("VDST", 1), ("SRC0", 1)]), + ("V_CMP_LT_U32", "ENC_VOPC", &[("SRC0", 1), ("VSRC1", 0)]), + ("S_AND_B32", "ENC_SOP2", &[("SDST", 126), ("SSRC0", 126), ("SSRC1", 106)]), + ("S_CBRANCH_EXECZ", "ENC_SOPP", &[("SIMM16", 2)]), + ("S_ADD_U32", "ENC_SOP2", &[("SDST", 1), ("SSRC0", 1), ("SSRC1", 129)]), + ("S_BRANCH", "ENC_SOPP", &[("SIMM16", 0xFFFA)]), + ("S_MOV_B32", "ENC_SOP1", &[("SDST", 126), ("SSRC0", 0)]), + ENDPGM, + ]; + // Save the remaining lanes in s4 and retire a different group each iteration. + // Once the wave exits, the scalar select reads the SCC of its last WREXEC. + pub(crate) const EMPTY_LOOP_SCC: &[I] = &[ + ("S_MOV_B32", "ENC_SOP1", &[("SDST", 0), ("SSRC0", 126)]), + ("S_MOV_B32", "ENC_SOP1", &[("SDST", 4), ("SSRC0", 126)]), + ("V_ADD_NC_U32", "ENC_VOP2", &[("VDST", 1), ("SRC0", 129), ("VSRC1", 1)]), + ("V_CMP_GE_U32", "ENC_VOPC", &[("SRC0", V + 1), ("VSRC1", 0)]), + ("S_MOV_B32", "ENC_SOP1", &[("SDST", 126), ("SSRC0", 106)]), + ("S_ANDN2_WREXEC_B32", "ENC_SOP1", &[("SDST", 4), ("SSRC0", 4)]), + ("S_CBRANCH_EXECNZ", "ENC_SOPP", &[("SIMM16", 0xFFFB)]), + ("S_MOV_B32", "ENC_SOP1", &[("SDST", 126), ("SSRC0", 0)]), + ("S_CSELECT_B32", "ENC_SOP2", &[("SDST", 2), ("SSRC0", 135), ("SSRC1", 139)]), + ("V_MOV_B32", "ENC_VOP1", &[("VDST", 2), ("SRC0", 2)]), + ENDPGM, + ]; /// s1 = 0; L: s1 += 3; v1 += 1; s_cmp_lt_u32 s1, 12; s_cbranch_scc1 L pub(crate) const UNIFORM_LOOP: &[I] = &[ ("S_MOV_B32", "ENC_SOP1", &[("SDST", 1), ("SSRC0", 128)]), @@ -3191,6 +3282,27 @@ pub(crate) mod tests { out } + #[test] + fn complemented_saveexec_replaces_the_previous_uniform_scc() { + // ANDN1 with full EXEC and ORN1 with empty EXEC both produce ~s8. + // Restoring EXEC with MOV leaves that SCC live for all lanes. + const FULL: I = ("S_MOV_B32", "ENC_SOP1", &[("SDST", 126), ("SSRC0", 193)]); + const EMPTY: I = ("S_MOV_B32", "ENC_SOP1", &[("SDST", 126), ("SSRC0", 128)]); + for (op, exec) in [("S_ANDN1_SAVEEXEC_B32", FULL), ("S_ORN1_SAVEEXEC_B32", EMPTY)] { + let insts = decode(&[ + ("S_CMP_EQ_U32", "ENC_SOPC", &[("SSRC0", 128), ("SSRC1", 128)]), + exec, + (op, "ENC_SOP1", &[("SDST", 4), ("SSRC0", 8)]), + ("S_MOV_B32", "ENC_SOP1", &[("SDST", 126), ("SSRC0", 193)]), + ("S_CBRANCH_SCC1", "ENC_SOPP", &[("SIMM16", 1)]), + ("V_MOV_B32", "ENC_VOP1", &[("VDST", 1), ("SRC0", 135)]), + ENDPGM, + ]); + let err = lower_kernel(&insts, false, Interp::default()).err().expect("varying mask"); + assert!(err.contains("any lane of a mask"), "{op}: {err}"); + } + } + /// A branch or a scalar select on whether any lane of a mask is set decides for the whole /// wave on the console: with lanes in EXEC that lack the bit (VCC = lanes 0–15, EXEC = all), /// each lane deciding by its own bit would differ in lanes 16–31, so the program is refused @@ -3477,6 +3589,54 @@ pub(crate) mod tests { assert_eq!((s[0], s[126]), (u32::MAX, u32::MAX), "saved and restored EXEC"); } + #[test] + fn a_value_written_in_a_lane_region_is_read_in_it_past_an_inner_if() { + // the writes dominate the reads and no loop is left between: accepted + let (s, v) = state(32); + let (_, v) = run(REGION_DIAMOND, false, &s, &v); + for (l, regs) in v.iter().enumerate() { + let (v1, v3) = if l < 8 { (7, l as u32) } else { (0, 0) }; + let want = if l < 16 { (v1, 5 + v3, v3) } else { (0, 0, 0) }; + assert_eq!((regs[1], regs[2], regs[3]), want, "lane {l}"); + } + // written in the loop the lanes leave one by one, read after it: refused + let mut lines = LEAVING_LOOP.to_vec(); + lines.insert(6, ("V_CNDMASK_B32", "ENC_VOP2", &[("VDST", 3), ("SRC0", 128), ("VSRC1", 0)])); + let err = lower_kernel(&decode(&lines), false, Interp::default()).err().expect("refused"); + assert!(err.contains("some lanes were elsewhere"), "{err}"); + // written in the arm some lanes skip, read after the join: refused + let mut lines = SKIPPED_IF.to_vec(); + lines[2] = ("S_CBRANCH_EXECZ", "ENC_SOPP", &[("SIMM16", 2)]); + lines.insert(3, ("S_MOV_B32", "ENC_SOP1", &[("SDST", 4), ("SSRC0", 133)])); + lines.insert(6, ("V_ADD_NC_U32", "ENC_VOP2", &[("VDST", 3), ("SRC0", 4), ("VSRC1", 1)])); + let err = lower_kernel(&decode(&lines), false, Interp::default()).err().expect("refused"); + assert!(err.contains("some lanes were elsewhere"), "{err}"); + } + + #[test] + fn empty_wrexec_loop_repairs_the_mask_and_scc_only_on_exhaustion() { + let (s, v) = state(32); + let (s, v) = run(EMPTY_LOOP_SCC, false, &s, &v); + assert_eq!(s[4], 0); + assert!(v.iter().all(|regs| regs[2] == 11)); + + // Falling through EXECZ instead means at least one bit is still set. + let mut lines = EMPTY_LOOP_SCC.to_vec(); + lines[6] = ("S_CBRANCH_EXECZ", "ENC_SOPP", &[("SIMM16", 0xFFFB)]); + let insts = decode(&lines); + let sum = lane_masks::analyze_branches(&insts, false, lane_masks::LaunchExec::All, false, true).unwrap(); + assert!(sum.blocks.iter().all(|b| b.empty_loop_wrexec.is_none())); + assert!(lower_kernel(&insts, false, Interp::default()).is_err()); + } + + #[test] + fn the_latch_counter_can_still_be_stale_after_the_loop() { + let mut lines = LATCH_COUNTER.to_vec(); + lines.insert(lines.len() - 1, ("V_MOV_B32", "ENC_VOP1", &[("VDST", 2), ("SRC0", 1)])); + let err = lower_kernel(&decode(&lines), false, Interp::default()).err().expect("refused"); + assert!(err.contains("some lanes were elsewhere"), "{err}"); + } + #[test] fn lanes_leave_a_loop_one_by_one() { let (s, v) = state(32); @@ -3636,7 +3796,7 @@ pub(crate) mod tests { runner::skip_test(if !gpu_waves() { "no GPU with 32-lane waves" } else { "no DXC" }); return; } - for (name, lines) in [("straight", STRAIGHT), ("skipped if", SKIPPED_IF), ("leaving loop", LEAVING_LOOP), ("uniform loop", UNIFORM_LOOP), ("memory", MEMORY)] { + for (name, lines) in [("straight", STRAIGHT), ("skipped if", SKIPPED_IF), ("region diamond", REGION_DIAMOND), ("leaving loop", LEAVING_LOOP), ("uniform loop", UNIFORM_LOOP), ("memory", MEMORY)] { let bytes: Vec = lines.iter().flat_map(|(n, e, f)| rdna2::encode(n, e, f, None).unwrap()).collect(); let (s, v) = state(32); let mut st = runner::KernelState::default(); diff --git a/crates/xcl/src/gpu/recomp/lower_dxil.rs b/crates/xcl/src/gpu/recomp/lower_dxil.rs index 2c200c788..8ad2c4f05 100644 --- a/crates/xcl/src/gpu/recomp/lower_dxil.rs +++ b/crates/xcl/src/gpu/recomp/lower_dxil.rs @@ -561,6 +561,7 @@ mod tests { for (name, lines) in [ ("straight", STRAIGHT), ("skipped if", SKIPPED_IF), + ("region diamond", REGION_DIAMOND), ("leaving loop", LEAVING_LOOP), ("uniform loop", UNIFORM_LOOP), ("memory", MEMORY), @@ -640,6 +641,35 @@ mod tests { } } + #[test] + fn shrinking_loops_agree_with_the_hlsl_path_on_the_gpu() { + if !gpu() { return; } + if dxc::location().is_err() { + runner::skip_test("no DXC"); + return; + } + let (s, v) = state(32); + let mut st = runner::KernelState::default(); + st.sgpr.copy_from_slice(&s); + for (lane, regs) in v.iter().enumerate() { + st.vgpr[lane][..regs.len()].copy_from_slice(regs); + } + for (lines, output) in [(LATCH_COUNTER, 1), (EMPTY_LOOP_SCC, 2)] { + let bytes: Vec = lines.iter().flat_map(encode_line).collect(); + let hlsl = translate(&bytes, &Options { wave64: false, ..Default::default() }).unwrap(); + assert!(hlsl.complete(), "{:?}", hlsl.unsupported_why); + let reference = dxc::compile(&hlsl.hlsl, "main", "cs_6_6", &[]).unwrap(); + let (dxil, _) = super::kernel(&decode(lines), false).unwrap(); + let reference = runner::run(&reference, &st, &[]).unwrap(); + let direct = runner::run(&dxil, &st, &[]).unwrap(); + for lane in 0..32 { + let expected = if output == 1 { lane as u32 } else { 11 }; + assert_eq!(reference.vgpr[lane][output], expected, "HLSL lane {lane}"); + assert_eq!(direct.vgpr[lane][output], expected, "per-lane DXIL lane {lane}"); + } + } + } + /// The integer programs of `lower`'s tests, as `f32_programs_on_the_gpu`. #[test] fn integer_programs_on_the_gpu() {