From 44eeb0c75acb0b1d55961fbee4c7f6f14e4cf028 Mon Sep 17 00:00:00 2001 From: Kalmarv <59708937+Kalmarv@users.noreply.github.com> Date: Mon, 30 Nov 2026 11:22:45 -0500 Subject: [PATCH] Recompiler: use wave32 register width in direct PS mask analysis PR #2571 (1183cd437) called x64_masks::whole_live_out(insts, false) and x64_masks::uses(d) inside ps_masks::forms(), both of which assume wave64 register pairs. On wave32 pixel shaders (all non-split PS on wave32 NVIDIA GPUs), a compare writing sN was treated as killing s(N+1) in both liveness and forward taint, allowing an earlier compare into s(N+1) that is still read whole (e.g. by s_bcnt1, s_and_saveexec_b32, or a branch/loop) to be misclassified as Form::Own (no ballot) or have its taint cleared. Expose x64_masks::uses_for_width and x64_masks::whole_live_out_for_width, pass o.wave64 from dxil::kernel into ps_masks::forms(), and use single-SGPR widths for VCC/EXEC/per-lane/writes in wave32 mode. Reported by CharlyRien on #2571. --- crates/xcl/src/gpu/dxil/kernel.rs | 2 +- crates/xcl/src/gpu/recomp/ps_masks.rs | 123 ++++++++++++++++--------- crates/xcl/src/gpu/recomp/x64_masks.rs | 8 +- 3 files changed, 89 insertions(+), 44 deletions(-) diff --git a/crates/xcl/src/gpu/dxil/kernel.rs b/crates/xcl/src/gpu/dxil/kernel.rs index f57cf8d7c0..2331541d41 100644 --- a/crates/xcl/src/gpu/dxil/kernel.rs +++ b/crates/xcl/src/gpu/dxil/kernel.rs @@ -810,7 +810,7 @@ impl Lw { lw.whole_live = crate::gpu::recomp::x64_masks::by_pc(insts, kernel); } if matches!(entry, Entry::Pixel(_)) && !split { - lw.ps_masks = crate::gpu::recomp::ps_masks::forms(insts); + lw.ps_masks = crate::gpu::recomp::ps_masks::forms(insts, o.wave64); } // (before the storage: the spin exchange has its own slots and counters) if let (Some(t), true) = (group_threads, split) { diff --git a/crates/xcl/src/gpu/recomp/ps_masks.rs b/crates/xcl/src/gpu/recomp/ps_masks.rs index 28013110a2..4224cc5aff 100644 --- a/crates/xcl/src/gpu/recomp/ps_masks.rs +++ b/crates/xcl/src/gpu/recomp/ps_masks.rs @@ -25,8 +25,12 @@ use crate::gpu::rdna2::{self, Decoded, Variant, F_CONDITIONAL, F_INDIRECT, F_TER use super::x64_masks; use std::collections::BTreeMap; -const VCC: u128 = 3 << 106; -const EXEC: u128 = 3 << 126; +fn vcc_mask(wave64: bool) -> u128 { + if wave64 { 3 << 106 } else { 1 << 106 } +} +fn exec_mask(wave64: bool) -> u128 { + if wave64 { 3 << 126 } else { 1 << 126 } +} fn fv(d: &Decoded, f: &str) -> Option { rdna2::field_value(d.enc, &d.words, f) @@ -61,12 +65,12 @@ fn reads_scc(n: &str) -> bool { n.starts_with("S_CSELECT") || n.starts_with("S_CMOV") || n.starts_with("S_ADDC") || n.starts_with("S_SUBB") } /// every SGPR the instruction writes (also the partial writes `uses` leaves out) -fn writes(d: &Decoded) -> u128 { +fn writes(d: &Decoded, wave64: bool) -> u128 { let n = d.inst.name; if n.starts_with("S_MOVRELD") { return !0; } - let (_, mut w) = x64_masks::uses(d); + let (_, mut w) = x64_masks::uses_for_width(d, wave64); if is_salu(d) { if let Some(s) = fv(d, "SDST") { w |= regs(s, if b64(n) { 2 } else { 1 }); @@ -74,14 +78,14 @@ fn writes(d: &Decoded) -> u128 { } w } -fn per_lane(d: &Decoded) -> u128 { +fn per_lane(d: &Decoded, wave64: bool) -> u128 { let n = d.inst.name; if !(matches!(n, "V_CNDMASK_B32" | "V_ADD_CO_CI_U32" | "V_SUB_CO_CI_U32" | "V_SUBREV_CO_CI_U32") || n.starts_with("V_DIV_FMAS")) { return 0; } match fv(d, "SRC2") { - Some(v) if d.enc.name.starts_with("ENC_VOP3") => regs(v, 2), - _ => VCC, + Some(v) if d.enc.name.starts_with("ENC_VOP3") => regs(v, if wave64 { 2 } else { 1 }), + _ => vcc_mask(wave64), } } fn xlane(d: &Decoded) -> bool { @@ -104,9 +108,10 @@ impl St { /// SGPRs known to be a subset of a reference mask after `d`, from those before it /// (`set`; EXEC's bit 126 in it: EXEC is). AND / ANDN2 keep a bounded first operand's /// bound, OR / XOR / CSELECT need both, a compare's mask is a subset of EXEC. -fn subset_after(d: &Decoded, set: u128) -> u128 { +fn subset_after(d: &Decoded, set: u128, wave64: bool) -> u128 { let n = d.inst.name; - let w = writes(d); + let exec = exec_mask(wave64); + let w = writes(d, wave64); let mut out = set & !w; let e_in = set >> 126 & 1 != 0; if n.starts_with("V_CMP") { @@ -138,7 +143,7 @@ fn subset_after(d: &Decoded, set: u128) -> u128 { false }; if ex { - out |= EXEC; + out |= exec; } return out; } @@ -165,18 +170,19 @@ fn subset_after(d: &Decoded, set: u128) -> u128 { /// SGPRs that are a subset of the live lanes (the entry EXEC) after each instruction: /// their helper-lane bits are 0, so the local form leaves them exact -fn live_subsets(insts: &[Decoded], succ: &[Vec]) -> Vec { +fn live_subsets(insts: &[Decoded], succ: &[Vec], wave64: bool) -> Vec { let n = insts.len(); + let exec = exec_mask(wave64); let mut zin = vec![!0u128; n]; let mut seen = vec![false; n]; - zin[0] = EXEC; + zin[0] = exec; seen[0] = true; let mut work = vec![0usize]; let mut queued = vec![false; n]; queued[0] = true; while let Some(i) = work.pop() { queued[i] = false; - let o = subset_after(&insts[i], zin[i]); + let o = subset_after(&insts[i], zin[i], wave64); for &j in &succ[i] { let nz = if seen[j] { zin[j] & o } else { o }; if !seen[j] || nz != zin[j] { @@ -189,7 +195,7 @@ fn live_subsets(insts: &[Decoded], succ: &[Vec]) -> Vec { } } } - (0..n).map(|i| if seen[i] { subset_after(&insts[i], zin[i]) } else { !0 }).collect() + (0..n).map(|i| if seen[i] { subset_after(&insts[i], zin[i], wave64) } else { !0 }).collect() } /// A conditional branch's region (forward: up to its target; backward: the loop) and what @@ -216,12 +222,13 @@ pub enum Form { Uniform, } -pub fn forms(insts: &[Decoded]) -> BTreeMap { +pub fn forms(insts: &[Decoded], wave64: bool) -> BTreeMap { let n = insts.len(); let mut out = BTreeMap::new(); if n == 0 { return out; } + let exec = exec_mask(wave64); let pcs: std::collections::BTreeMap = insts.iter().enumerate().map(|(i, d)| (d.pc, i)).collect(); let mut indirect = vec![false; n]; let succ: Vec> = insts @@ -248,7 +255,7 @@ pub fn forms(insts: &[Decoded]) -> BTreeMap { v }) .collect(); - let z = live_subsets(insts, &succ); + let z = live_subsets(insts, &succ, wave64); // conditional branches: the region whose execution may differ between copies let mut region: Vec> = (0..n).map(|_| None).collect(); for (b, d) in insts.iter().enumerate() { @@ -267,27 +274,27 @@ pub fn forms(insts: &[Decoded]) -> BTreeMap { } } let (mut w, mut scc, mut bad) = (0u128, false, false); - let mut bound: u128 = EXEC; + let mut bound: u128 = exec; for k in lo..hi.min(n) { let dk = &insts[k]; - w |= writes(dk) & !EXEC; + w |= writes(dk, wave64) & !exec; scc |= writes_scc(dk.inst.name); - bound = subset_after(dk, bound); + bound = subset_after(dk, bound, wave64); let nk = dk.inst.name; - bad |= writes(dk) & EXEC != 0 && bound >> 126 & 1 == 0 + bad |= writes(dk, wave64) & exec != 0 && bound >> 126 & 1 == 0 || nk == "V_WRITELANE_B32" || nk.starts_with("S_SENDMSG") || dk.enc.name == "ENC_SMEM" && (nk.contains("STORE") || nk.contains("ATOMIC")); } region[b] = Some(Region { joins, w, scc, bad }); } - let live = x64_masks::whole_live_out(insts, false); + let live = x64_masks::whole_live_out_for_width(insts, false, wave64); for site in 0..n { let d = &insts[site]; if !is_site(d) { continue; } - let dst = writes(d); + let dst = writes(d, wave64); if dst == 0 { continue; } @@ -299,15 +306,17 @@ pub fn forms(insts: &[Decoded]) -> BTreeMap { let uniform = d.enc.name.starts_with("ENC_VOP3") && fv(d, "SRC0").is_some_and(|v| v < 256) && fv(d, "SRC1").is_some_and(|v| v < 256); if uniform { out.insert(d.pc, Form::Uniform); - } else if run(insts, &succ, &indirect, &z, ®ion, site).is_ok() { + } else if run(insts, &succ, &indirect, &z, ®ion, site, wave64).is_ok() { out.insert(d.pc, Form::Local); } } out } -fn run(insts: &[Decoded], succ: &[Vec], indirect: &[bool], z: &[u128], region: &[Option], site: usize) -> Result<(), &'static str> { +fn run(insts: &[Decoded], succ: &[Vec], indirect: &[bool], z: &[u128], region: &[Option], site: usize, wave64: bool) -> Result<(), &'static str> { let n = insts.len(); + let vcc = vcc_mask(wave64); + let exec = exec_mask(wave64); let mut sin = vec![St::default(); n]; let mut extra = vec![St::default(); n]; let mut div = vec![false; n]; @@ -322,9 +331,9 @@ fn run(insts: &[Decoded], succ: &[Vec], indirect: &[bool], z: &[u128], re } let d = &insts[i]; let nm = d.inst.name; - let (whole, _) = x64_masks::uses(d); - let w = writes(d); - if (whole | per_lane(d)) & st.a != 0 { + let (whole, _) = x64_masks::uses_for_width(d, wave64); + let w = writes(d, wave64); + if (whole | per_lane(d, wave64)) & st.a != 0 { return Err("read of a diverged register"); } if indirect[i] && (st.t | st.a) != 0 { @@ -334,14 +343,14 @@ fn run(insts: &[Decoded], succ: &[Vec], indirect: &[bool], z: &[u128], re let mut branch_div = false; if d.enc.name == "ENC_SOPP" { if nm.starts_with("S_CBRANCH_EXEC") { - branch_div = st.t & EXEC != 0; + branch_div = st.t & exec != 0; } else if nm.starts_with("S_CBRANCH_SCC") { if st.scc == 2 { return Err("branch on SCC of a mask"); } branch_div = st.scc == 1; } else if nm.starts_with("S_CBRANCH_VCC") { - if st.t & VCC != 0 { + if st.t & vcc != 0 { return Err("branch on VCCZ"); } } else if whole & st.t != 0 { @@ -363,11 +372,11 @@ fn run(insts: &[Decoded], succ: &[Vec], indirect: &[bool], z: &[u128], re } match () { _ if nm.contains("_SAVEEXEC_") || nm.contains("_WREXEC") => { - let et = st.t & EXEC != 0; - let srct = whole & !EXEC & st.t != 0; - let sd = w & !EXEC; + let et = st.t & exec != 0; + let srct = whole & !exec & st.t != 0; + let sd = w & !exec; if et || srct { - o.t |= EXEC; + o.t |= exec; } if nm.contains("_WREXEC") { if et || srct { @@ -385,8 +394,8 @@ fn run(insts: &[Decoded], succ: &[Vec], indirect: &[bool], z: &[u128], re } if writes_scc(nm) { // SCC of a bitwise op is its result != 0: exact when the result is live-only - let exact = w & !EXEC & !z[i] == 0 && (w & EXEC == 0 || z[i] >> 126 & 1 != 0); - o.scc = if rt && !exact { if w & EXEC != 0 { 1 } else { 2 } } else { 0 }; + let exact = w & !exec & !z[i] == 0 && (w & exec == 0 || z[i] >> 126 & 1 != 0); + o.scc = if rt && !exact { if w & exec != 0 { 1 } else { 2 } } else { 0 }; } } else if d.enc.name == "ENC_SMEM" { if whole & st.t != 0 { @@ -394,21 +403,21 @@ fn run(insts: &[Decoded], succ: &[Vec], indirect: &[bool], z: &[u128], re } } else { // VALU / VMEM / DS / export - if st.a & EXEC != 0 { + if st.a & exec != 0 { return Err("diverged EXEC"); } let exec_data = d.variant == Variant::Plain && ["SRC0", "SRC1", "SRC2"].iter().any(|f| matches!(fv(d, f), Some(126 | 127))); - if whole & !EXEC & st.t != 0 || exec_data && st.t & EXEC != 0 { + if whole & !exec & st.t != 0 || exec_data && st.t & exec != 0 { return Err("VALU / VMEM scalar operand"); } - if st.t & EXEC != 0 && xlane(d) { + if st.t & exec != 0 && xlane(d) { return Err(match d.variant { Variant::Dpp16 | Variant::Dpp8 => "DPP under the mask", _ if nm == "V_READFIRSTLANE_B32" => "readfirstlane under the mask", _ => "ds_swizzle / permute under the mask", }); } - if is_site(d) && (i == site || st.t & EXEC != 0) { + if is_site(d) && (i == site || st.t & exec != 0) { o.t |= w; } } @@ -454,8 +463,8 @@ mod tests { const V: u64 = 256; type I = (&'static str, &'static str, Vec<(&'static str, u64)>); - /// The form of every compare in `prog` (wave64; `S_ENDPGM` appended), in order. - fn forms_of(prog: &[I]) -> Vec> { + /// The form of every compare in `prog` (`S_ENDPGM` appended), in order. + fn forms_of_width(prog: &[I], wave64: bool) -> Vec> { let mut code: Vec = prog.iter().flat_map(|(n, e, f)| encode(n, e, f, None).unwrap_or_else(|err| panic!("{n}: {err}"))).collect(); code.extend(encode("S_ENDPGM", "ENC_SOPP", &[], None).unwrap()); let mut insts = Vec::new(); @@ -466,9 +475,12 @@ mod tests { insts.push(d); } rdna2::resolve_far_jumps(&mut insts); - let f = forms(&insts); + let f = forms(&insts, wave64); insts.iter().filter(|d| is_site(d)).map(|d| f.get(&d.pc).copied()).collect() } + fn forms_of(prog: &[I]) -> Vec> { + forms_of_width(prog, true) + } fn sop1(n: &'static str, d: u64, s: u64) -> I { (n, "ENC_SOP1", vec![("SDST", d), ("SSRC0", s)]) } @@ -536,4 +548,31 @@ mod tests { ]; assert_eq!(forms_of(&prog), [Some(Form::Local)]); } + + #[test] + fn wave32_compare_does_not_kill_neighbor_sgpr_in_liveness_or_taint() { + // Wave32 PS: `v_cmp -> s21`, then `v_cmp -> s20`, then `s_bcnt1 s21` (CharlyRien's repro). + // Under wave64 liveness/taint, `v_cmp -> s20` wrongly treated `s21` as killed and gave + // the first compare `Form::Own` (or cleared its taint). Under wave32 it must keep the + // per-quad form (`None`) because `s_bcnt1` reads `s21` whole. + let prog: Vec = vec![ + sop1("S_WQM_B32", 126, 126), + ("V_CMP_LT_F32", "ENC_VOP3", vec![("VDST", 21), ("SRC0", 128), ("SRC1", V + 2)]), + ("V_CMP_GT_F32", "ENC_VOP3", vec![("VDST", 20), ("SRC0", 128), ("SRC1", V + 3)]), + sop1("S_BCNT1_I32_B32", 22, 21), + ]; + assert_eq!(forms_of_width(&prog, false), [None, Some(Form::Own)]); + + // Same neighbor-write pattern, where `s21` is instead used by `s_and_saveexec_b32`: + // it must get `Form::Local`, not `Form::Own`. + let prog_local: Vec = vec![ + sop1("S_WQM_B32", 126, 126), + ("V_CMP_LT_F32", "ENC_VOP3", vec![("VDST", 21), ("SRC0", 128), ("SRC1", V + 2)]), + ("V_CMP_GT_F32", "ENC_VOP3", vec![("VDST", 20), ("SRC0", 128), ("SRC1", V + 3)]), + sop1("S_AND_SAVEEXEC_B32", 22, 21), + ("V_MOV_B32", "ENC_VOP1", vec![("VDST", 7), ("SRC0", V + 3)]), + sop1("S_MOV_B32", 126, 22), + ]; + assert_eq!(forms_of_width(&prog_local, false), [Some(Form::Local), Some(Form::Own)]); + } } diff --git a/crates/xcl/src/gpu/recomp/x64_masks.rs b/crates/xcl/src/gpu/recomp/x64_masks.rs index bd523b7a86..491f86d44e 100644 --- a/crates/xcl/src/gpu/recomp/x64_masks.rs +++ b/crates/xcl/src/gpu/recomp/x64_masks.rs @@ -393,6 +393,12 @@ pub fn uses(d: &Decoded) -> (Regs, Regs) { uses_with_table(d, use_plans()) } +/// Width-aware `(read whole, overwritten)` SGPRs of one instruction. +/// In wave32, pixel VALU mask writes and `SAVEEXEC`/`WREXEC` write one dword, not a pair. +pub fn uses_for_width(d: &Decoded, wave64: bool) -> (Regs, Regs) { + liveness_uses(d, use_plans(), wave64) +} + #[derive(Clone, Copy)] enum BlockSucc { Indirect, @@ -544,7 +550,7 @@ pub fn whole_live_out(insts: &[Decoded], end_reads_all: bool) -> Vec { whole_live_out_for_width(insts, end_reads_all, true) } -fn whole_live_out_for_width(insts: &[Decoded], end_reads_all: bool, wave64: bool) -> Vec { +pub fn whole_live_out_for_width(insts: &[Decoded], end_reads_all: bool, wave64: bool) -> Vec { let n = insts.len(); if n == 0 { return Vec::new();