From 714d8c7bfece821ecb270b48b19920cdb2373d01 Mon Sep 17 00:00:00 2001 From: Kalmarv <59708937+Kalmarv@users.noreply.github.com> Date: Tue, 24 Nov 2026 16:16:25 -0500 Subject: [PATCH] Recompiler: take native compute mask branches across the whole wave Keep complete native compute waves together across console branches. Reduce mask results at their scalar producers and ballot VCC independently of EXEC. Model each uniform edge separately from lane reach so vector writes retain their EXEC guards. --- README.md | 4 +- crates/xcl/src/gpu/recomp/lane_masks.rs | 20 +++- crates/xcl/src/gpu/recomp/lower.rs | 127 ++++++++++++++++++++++-- crates/xcl/src/gpu/recomp/lower/wave.rs | 3 +- crates/xcl/src/gpu/recomp/lower_dxil.rs | 14 +++ 5 files changed, 155 insertions(+), 13 deletions(-) diff --git a/README.md b/README.md index e35903a1cf..a4ff851780 100644 --- a/README.md +++ b/README.md @@ -105,7 +105,9 @@ next to `xcl.exe`, directly next to `xcl.exe`, in `%LOCALAPPDATA%\XCL\D3D12\`, o (NVIDIA) console wave64 compute, geometry and mesh programs run as two 32-lane halves that exchange masks and lane values ([`docs/wave64-on-wave32.md`](docs/wave64-on-wave32.md)) – slower than 64-lane waves, most for groups of several waves; wave64 vertex and pixel shaders that read lanes 32–63 - can still differ there. Test results on other GPUs: [#42](https://github.com/ivall/xcl/issues/42). + can still differ there. With `XCL_DIRECT_DXIL=1` and `XCL_PER_LANE=1`, compute groups + made up of complete native waves can use the per-lane form for branches on whole-wave masks. + Test results on other GPUs: [#42](https://github.com/ivall/xcl/issues/42). ```powershell cargo build --release --workspace # target\release\xcl.exe diff --git a/crates/xcl/src/gpu/recomp/lane_masks.rs b/crates/xcl/src/gpu/recomp/lane_masks.rs index da823e4ee7..3de2d3a35e 100644 --- a/crates/xcl/src/gpu/recomp/lane_masks.rs +++ b/crates/xcl/src/gpu/recomp/lane_masks.rs @@ -275,6 +275,7 @@ struct Lanes<'a> { insts: &'a [Decoded], wave64: bool, launch: LaunchExec, + wave_branches: bool, bdd: Bdd, atoms: HashMap, /// block each atom is born in @@ -421,7 +422,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, 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, body, effects }) } fn atom(&mut self, a: Atom, block: usize) -> R { @@ -453,7 +454,7 @@ impl<'a> Lanes<'a> { st.regs[EXEC] = Some(self.atom(Atom::Opaque(d.pc, EXEC), block)?); } if writes_scc { - st.scc = Some((self.atom(Atom::Scc(d.pc), block)?, true)); + st.scc = Some((self.atom(Atom::Scc(d.pc), block)?, !self.wave_branches)); } Ok(()) } @@ -581,7 +582,7 @@ impl<'a> Lanes<'a> { st.regs[EXEC] = Some(self.atom(Atom::Opaque(d.pc, EXEC), block)?); } if writes_scc { - st.scc = match scc { + st.scc = match scc.filter(|_| !self.wave_branches) { Some(s) => Some(s), None => Some((self.atom(Atom::Scc(d.pc), block)?, false)), }; @@ -626,6 +627,11 @@ impl<'a> Lanes<'a> { /// lane mask's. fn cond(&mut self, st: &State, d: &Decoded, block: usize) -> R<(B, bool)> { let e = st.regs[EXEC].expect("EXEC"); + // All hardware lanes take the same edge. A wave decision carries no fact about + // this lane's mask bit, even when the wave took the nonzero edge. + if self.wave_branches { + return Ok((self.atom(Atom::Scc(d.pc), block)?, false)); + } let (c, lane, negate) = match d.inst.name { n @ ("S_CBRANCH_SCC0" | "S_CBRANCH_SCC1") => { let (c, lane) = match st.scc { @@ -889,10 +895,16 @@ 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) +} + +pub(super) fn analyze_branches(insts: &[Decoded], wave64: bool, launch: LaunchExec, wave_branches: bool) -> R { if insts.is_empty() { return Ok(Summary::default()); } - Lanes::new(insts, wave64, launch)?.analyze() + let mut lanes = Lanes::new(insts, wave64, launch)?; + lanes.wave_branches = wave_branches; + lanes.analyze() } /// Decodes and analyses a program. diff --git a/crates/xcl/src/gpu/recomp/lower.rs b/crates/xcl/src/gpu/recomp/lower.rs index 7904becbca..58a7f1b2f4 100644 --- a/crates/xcl/src/gpu/recomp/lower.rs +++ b/crates/xcl/src/gpu/recomp/lower.rs @@ -154,6 +154,7 @@ struct Lower<'a, I: Ir> { ssa: Ssa, sum: &'a Summary, guarded: HashSet, + wave_branches: bool, wave64: bool, /// the markers: a number where a mask was written, a bit where a number was num_marker: I::V, @@ -509,6 +510,18 @@ impl<'a, I: Ir> Lower<'a, I> { }) } + /// A mask-producing scalar operation sets SCC from its full result. In the native + /// wave CFG every lane reaches scalar instructions, including lanes outside EXEC. + fn mask_nonzero(&mut self, bit: I::V) -> I::V { + if !self.wave_branches { + return bit; + } + let [lo, hi] = self.ir.ballot(bit); + let word = if self.wave64 { self.ir.bin(Bin::Or, lo, hi) } else { lo }; + let zero = self.ir.int(0); + self.ir.icmp(Cmp::Ne, word, zero) + } + fn salu(&mut self, b: usize, d: &Decoded) -> R<()> { let name = d.inst.name; let fv = |f: &str| rdna2::field_value(d.enc, &d.words, f); @@ -552,7 +565,8 @@ impl<'a, I: Ir> Lower<'a, I> { if sd != 125 { self.write_bit(b, sd, if base.ends_with("_WREXEC") { new } else { old }); } - self.write_scc(b, new); + let scc = self.mask_nonzero(new); + self.write_scc(b, scc); return Ok(()); } if op == "CSELECT" && (sd == EXEC || reads_mask) { @@ -575,7 +589,8 @@ impl<'a, I: Ir> Lower<'a, I> { }; self.write_bit(b, sd, v); if op != "MOV" { - self.write_scc(b, v); + let scc = self.mask_nonzero(v); + self.write_scc(b, scc); } return Ok(()); } @@ -585,6 +600,7 @@ impl<'a, I: Ir> Lower<'a, I> { if cmp64.contains(&name) && [s0, s1].iter().any(|&c| c <= 127 && self.holds_mask(b, c as u8)) { let (a, c) = (self.src_bit(b, s0)?, self.src_bit(b, s1)?); let x = self.ir.bin(Bin::Xor, a, c); + let x = self.mask_nonzero(x); let v = if name == cmp64[0] { self.not(x) } else { x }; self.write_scc(b, v); return Ok(()); @@ -968,18 +984,27 @@ impl<'a, I: Ir> Lower<'a, I> { self.not(s) } n @ ("S_CBRANCH_VCCZ" | "S_CBRANCH_VCCNZ") => { - let (v, e) = (self.read_bit(b, VCC)?, self.read_bit(b, EXEC)?); - let c = self.ir.bin(Bin::And, v, e); + let v = self.read_bit(b, VCC)?; + let c = if self.wave_branches { + self.mask_nonzero(v) + } else { + let e = self.read_bit(b, EXEC)?; + self.ir.bin(Bin::And, v, e) + }; if n == "S_CBRANCH_VCCZ" { self.not(c) } else { c } } - "S_CBRANCH_EXECNZ" => self.read_bit(b, EXEC)?, + "S_CBRANCH_EXECNZ" => { + let e = self.read_bit(b, EXEC)?; + self.mask_nonzero(e) + } "S_CBRANCH_EXECZ" => { let e = self.read_bit(b, EXEC)?; - self.not(e) + let c = self.mask_nonzero(e); + self.not(c) } n => return Err(format!("unsupported branch {n}")), }) @@ -1036,6 +1061,9 @@ impl<'a, I: Ir> Lower<'a, I> { /// counts: the lanes that leave do not come back to it). fn partial_blocks(sum: &Summary) -> Vec { let n = sum.blocks.len(); + if sum.lane_branches == 0 { + return vec![false; n]; + } let words = (n + 1).div_ceil(64); // post-dominators over the blocks the entry reaches, with a virtual exit n let full = vec![!0u64; words]; @@ -1124,7 +1152,10 @@ fn lower_with(insts: &[Decoded], stage: Stage, wave64: bool, mut ir: I, s // vertex invocation being one lane let pixel = matches!(stage, Stage::Pixel); let launch = if pixel { lane_masks::LaunchExec::Unknown } else { lane_masks::LaunchExec::All }; - let sum = lane_masks::analyze_launch(insts, wave64, launch).map_err(|e| e.to_string())?; + // 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 n = sum.blocks.len(); if n == 0 { return Err("empty program".into()); @@ -1197,6 +1228,7 @@ fn lower_with(insts: &[Decoded], stage: Stage, wave64: bool, mut ir: I, s sum: &sum, guarded, + wave_branches, wave64, num_marker, bit_marker, @@ -3184,6 +3216,87 @@ pub(crate) mod tests { assert!(v.iter().all(|r| r[1] == 7)); } + /// Only the last lane has a VCC bit. The branches inspect VCC after EXEC has been + /// narrowed to the other lanes. Each case stores the scalar decision at mem[thread]. + pub(crate) fn wave_branch_cases(wave64: bool) -> Vec<(Vec, u32)> { + let cmp: I = if wave64 { + ("V_CMP_EQ_U32", "ENC_VOPC", &[("SRC0", 191), ("VSRC1", 0)]) + } else { + ("V_CMP_EQ_U32", "ENC_VOPC", &[("SRC0", 159), ("VSRC1", 0)]) + }; + let (narrow, restore, eq, lg) = if wave64 { + ("S_ANDN2_B64", "S_MOV_B64", "S_CMP_EQ_U64", "S_CMP_LG_U64") + } else { + ("S_ANDN2_B32", "S_MOV_B32", "S_CMP_EQ_U32", "S_CMP_LG_U32") + }; + let mut cases: Vec<(Vec, u32)> = Vec::new(); + for (branch, expected) in [("S_CBRANCH_VCCNZ", 5), ("S_CBRANCH_VCCZ", 9), ("S_CBRANCH_EXECNZ", 5), + ("S_CBRANCH_EXECZ", 9), ("S_CBRANCH_SCC1", 5), ("S_CBRANCH_SCC0", 9)] { + cases.push((vec![ + cmp, + (narrow, "ENC_SOP2", &[("SDST", 126), ("SSRC0", 126), ("SSRC1", 106)]), + ("S_MOV_B32", "ENC_SOP1", &[("SDST", 6), ("SSRC0", 133)]), + (branch, "ENC_SOPP", &[("SIMM16", 1)]), + ("S_MOV_B32", "ENC_SOP1", &[("SDST", 6), ("SSRC0", 137)]), + (restore, "ENC_SOP1", &[("SDST", 126), ("SSRC0", 193)]), + ], expected)); + } + for (compare, expected) in [(eq, 9), (lg, 5)] { + cases.push((vec![ + cmp, + (compare, "ENC_SOPC", &[("SSRC0", 106), ("SSRC1", 128)]), + ("S_CSELECT_B32", "ENC_SOP2", &[("SDST", 6), ("SSRC0", 133), ("SSRC1", 137)]), + ], expected)); + } + if !wave64 { + // The second iteration's CSELECT reads the mask-derived SCC on the + // backedge (thehollowedone, #1745). + cases.push((vec![ + ("S_MOV_B32", "ENC_SOP1", &[("SDST", 4), ("SSRC0", 130)]), + ("V_CMP_GT_U32", "ENC_VOPC", &[("SRC0", 144), ("VSRC1", 0)]), + ("S_CMP_GT_U32", "ENC_SOPC", &[("SSRC0", 129), ("SSRC1", 128)]), + ("S_CSELECT_B32", "ENC_SOP2", &[("SDST", 6), ("SSRC0", 133), ("SSRC1", 135)]), + ("V_MOV_B32", "ENC_VOP1", &[("VDST", 1), ("SRC0", 6)]), + ("S_SUB_U32", "ENC_SOP2", &[("SDST", 4), ("SSRC0", 4), ("SSRC1", 129)]), + ("S_CMP_EQ_U32", "ENC_SOPC", &[("SSRC0", 4), ("SSRC1", 128)]), + ("S_CBRANCH_SCC1", "ENC_SOPP", &[("SIMM16", 2)]), + ("S_AND_B32", "ENC_SOP2", &[("SDST", 0), ("SSRC0", 106), ("SSRC1", 126)]), + ("S_BRANCH", "ENC_SOPP", &[("SIMM16", 0xfff9)]), + ], 5)); + // The bitwise op reads both a mask and a number (Pipodi, #1745). + cases.push((vec![ + ("S_MOV_B32", "ENC_SOP1", &[("SDST", 8), ("SSRC0", 133)]), + ("V_CMP_GT_U32", "ENC_VOPC", &[("SRC0", 144), ("VSRC1", 0)]), + ("S_AND_B32", "ENC_SOP2", &[("SDST", 4), ("SSRC0", 106), ("SSRC1", 8)]), + ("S_MOV_B32", "ENC_SOP1", &[("SDST", 6), ("SSRC0", 135)]), + ("S_CBRANCH_SCC0", "ENC_SOPP", &[("SIMM16", 1)]), + ("S_MOV_B32", "ENC_SOP1", &[("SDST", 6), ("SSRC0", 129)]), + ], 1)); + } + for (lines, _) in &mut cases { + lines.extend_from_slice(&[ + ("V_MOV_B32", "ENC_VOP1", &[("VDST", 1), ("SRC0", 6)]), + ("V_LSHLREV_B32", "ENC_VOP2", &[("VDST", 2), ("SRC0", 130), ("VSRC1", 0)]), + ("GLOBAL_STORE_DWORD", "ENC_FLAT_GLBL", &[("ADDR", 2), ("DATA", 1), ("SADDR", 125)]), + ENDPGM, + ]); + } + cases + } + + #[test] + fn native_wave_branches_keep_inactive_lanes_in_scalar_control_flow() { + for wave64 in [false, true] { + let lanes = if wave64 { 64 } else { 32 }; + let mut cs = compute_cs(); + cs.threads = [lanes, 1, 1]; + for (lines, expected) in wave_branch_cases(wave64) { + let got = run_compute_waves(&lines, &cs, wave64, 1, &[], &vec![0; lanes as usize]); + assert!(got.iter().all(|&word| word == expected), "wave{lanes}, {lines:?}: {got:?}"); + } + } + } + /// A cross-lane scalar op on s0 holding a number takes the number path (the check for the /// upper dword of a mask pair at s must not compute r - 1 for s0: nobodyttk, #1269). #[test] diff --git a/crates/xcl/src/gpu/recomp/lower/wave.rs b/crates/xcl/src/gpu/recomp/lower/wave.rs index 7d3ceceb77..9b9cc856eb 100644 --- a/crates/xcl/src/gpu/recomp/lower/wave.rs +++ b/crates/xcl/src/gpu/recomp/lower/wave.rs @@ -431,7 +431,8 @@ impl Lower<'_, I> { let z = self.c(0); let bit = self.ir.icmp(Cmp::Ne, w, z); self.write_bit(b, sd, bit); - self.write_scc(b, bit); + let scc = self.mask_nonzero(bit); + self.write_scc(b, scc); return Ok(true); } let (ones, count) = match name { diff --git a/crates/xcl/src/gpu/recomp/lower_dxil.rs b/crates/xcl/src/gpu/recomp/lower_dxil.rs index 759680d394..7aee23edb6 100644 --- a/crates/xcl/src/gpu/recomp/lower_dxil.rs +++ b/crates/xcl/src/gpu/recomp/lower_dxil.rs @@ -753,6 +753,20 @@ mod tests { programs_on_the_gpu(&[("f64", F64_ARITHMETIC, check_f64, &[])]); } + #[test] + fn native_wave_branches_on_the_gpu() { + if !gpu() { + return; + } + let mut cs = compute_cs(); + cs.threads = [32, 1, 1]; + for (lines, expected) in wave_branch_cases(false) { + let dxil = super::compute(&decode(&lines), &cs, false).unwrap(); + let out = runner::dispatch(&dxil, [1, 1, 1], &[], &[0; 32]).unwrap(); + assert_eq!(&out[..32], &[expected; 32], "{lines:?}"); + } + } + #[test] fn a_per_lane_compute_program_runs_on_the_gpu() { if !gpu() {