Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion crates/xcl/src/gpu/dxil/kernel.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
123 changes: 81 additions & 42 deletions crates/xcl/src/gpu/recomp/ps_masks.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<u64> {
rdna2::field_value(d.enc, &d.words, f)
Expand Down Expand Up @@ -61,27 +65,27 @@ 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 });
}
}
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 {
Expand All @@ -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") {
Expand Down Expand Up @@ -138,7 +143,7 @@ fn subset_after(d: &Decoded, set: u128) -> u128 {
false
};
if ex {
out |= EXEC;
out |= exec;
}
return out;
}
Expand All @@ -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<usize>]) -> Vec<u128> {
fn live_subsets(insts: &[Decoded], succ: &[Vec<usize>], wave64: bool) -> Vec<u128> {
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] {
Expand All @@ -189,7 +195,7 @@ fn live_subsets(insts: &[Decoded], succ: &[Vec<usize>]) -> Vec<u128> {
}
}
}
(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
Expand All @@ -216,12 +222,13 @@ pub enum Form {
Uniform,
}

pub fn forms(insts: &[Decoded]) -> BTreeMap<u64, Form> {
pub fn forms(insts: &[Decoded], wave64: bool) -> BTreeMap<u64, Form> {
let n = insts.len();
let mut out = BTreeMap::new();
if n == 0 {
return out;
}
let exec = exec_mask(wave64);
let pcs: std::collections::BTreeMap<u64, usize> = insts.iter().enumerate().map(|(i, d)| (d.pc, i)).collect();
let mut indirect = vec![false; n];
let succ: Vec<Vec<usize>> = insts
Expand All @@ -248,7 +255,7 @@ pub fn forms(insts: &[Decoded]) -> BTreeMap<u64, Form> {
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<Option<Region>> = (0..n).map(|_| None).collect();
for (b, d) in insts.iter().enumerate() {
Expand All @@ -267,27 +274,27 @@ pub fn forms(insts: &[Decoded]) -> BTreeMap<u64, Form> {
}
}
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;
}
Expand All @@ -299,15 +306,17 @@ pub fn forms(insts: &[Decoded]) -> BTreeMap<u64, Form> {
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, &region, site).is_ok() {
} else if run(insts, &succ, &indirect, &z, &region, site, wave64).is_ok() {
out.insert(d.pc, Form::Local);
}
}
out
}

fn run(insts: &[Decoded], succ: &[Vec<usize>], indirect: &[bool], z: &[u128], region: &[Option<Region>], site: usize) -> Result<(), &'static str> {
fn run(insts: &[Decoded], succ: &[Vec<usize>], indirect: &[bool], z: &[u128], region: &[Option<Region>], 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];
Expand All @@ -322,9 +331,9 @@ fn run(insts: &[Decoded], succ: &[Vec<usize>], 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 {
Expand All @@ -334,14 +343,14 @@ fn run(insts: &[Decoded], succ: &[Vec<usize>], 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 {
Expand All @@ -363,11 +372,11 @@ fn run(insts: &[Decoded], succ: &[Vec<usize>], 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 {
Expand All @@ -385,30 +394,30 @@ fn run(insts: &[Decoded], succ: &[Vec<usize>], 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 {
return Err("SMEM operand");
}
} 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;
}
}
Expand Down Expand Up @@ -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<Option<Form>> {
/// The form of every compare in `prog` (`S_ENDPGM` appended), in order.
fn forms_of_width(prog: &[I], wave64: bool) -> Vec<Option<Form>> {
let mut code: Vec<u8> = 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();
Expand All @@ -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<Option<Form>> {
forms_of_width(prog, true)
}
fn sop1(n: &'static str, d: u64, s: u64) -> I {
(n, "ENC_SOP1", vec![("SDST", d), ("SSRC0", s)])
}
Expand Down Expand Up @@ -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<I> = 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<I> = 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)]);
}
}
8 changes: 7 additions & 1 deletion crates/xcl/src/gpu/recomp/x64_masks.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -544,7 +550,7 @@ pub fn whole_live_out(insts: &[Decoded], end_reads_all: bool) -> Vec<Regs> {
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<Regs> {
pub fn whole_live_out_for_width(insts: &[Decoded], end_reads_all: bool, wave64: bool) -> Vec<Regs> {
let n = insts.len();
if n == 0 {
return Vec::new();
Expand Down
Loading