diff --git a/crates/xcl/src/gpu/rdna2.rs b/crates/xcl/src/gpu/rdna2.rs index 6c516213db..6c4ddfae37 100644 --- a/crates/xcl/src/gpu/rdna2.rs +++ b/crates/xcl/src/gpu/rdna2.rs @@ -540,10 +540,9 @@ pub fn encoding(name: &str) -> Option<&'static Encoding> { ENCODINGS.iter().find(|e| e.name == name) } -/// VGPRs an instruction reads and writes (explicit operands; register ranges by operand -/// size). SDWA/DPP src0 comes from the extension dword. /// SGPRs (codes 0–127 without NULL: s0–s105, VCC, M0, EXEC…) read and written by the -/// explicit operands: (reads, writes). +/// explicit operands: (reads, writes). An SDWA instruction's are in its extension dword too: +/// a source whose S bit is set, and the SGPR pair a compare writes instead of VCC (SD). pub fn sgpr_uses(d: &Decoded) -> (Vec, Vec) { let (mut r, mut w) = (Vec::new(), Vec::new()); for o in d.ienc.operands { @@ -566,9 +565,22 @@ pub fn sgpr_uses(d: &Decoded) -> (Vec, Vec) { let dst = if o.output { &mut w } else { &mut r }; dst.extend(v..(v + n).min(128)); } + if d.variant == Variant::Sdwa { + let ext = d.words.get(1).copied().unwrap_or(0); + let src1 = field_value(d.enc, &d.words, "VSRC1").filter(|_| ext >> 31 & 1 == 1); + r.extend(Some((ext & 0xff) as u64).filter(|_| ext >> 23 & 1 == 1).into_iter().chain(src1).filter(|&v| v <= 127 && v != 125)); + // SDST (bits 8-14 of the dword): a mask, two SGPRs as a VOP3 compare's; v_cmpx writes EXEC + let sdst = (ext >> 8 & 0x7f) as u64; + if d.enc.name == "ENC_VOPC" && ext >> 15 & 1 == 1 && !d.inst.name.starts_with("V_CMPX") && sdst != 125 { + w.extend(sdst..(sdst + 2).min(128)); + } + } (r, w) } +/// VGPRs an instruction reads and writes (explicit operands; register ranges by operand +/// size). SDWA/DPP src0 comes from the extension dword, and an SDWA source whose S bit is set +/// is no VGPR. pub fn vgpr_uses(d: &Decoded) -> (Vec, Vec) { if d.enc.name == "ENC_MIMG" { return mimg_vgpr_uses(d); @@ -581,7 +593,9 @@ pub fn vgpr_uses(d: &Decoded) -> (Vec, Vec) { } let Some(mut v) = field_value(d.enc, &d.words, f) else { continue }; let width = d.enc.field_width(f); - let vgpr = if o.ty.starts_with("OPR_VGPR") && width <= 8 { + let vgpr = if f == "VSRC1" && d.variant == Variant::Sdwa && d.words.get(1).copied().unwrap_or(0) >> 31 & 1 == 1 { + false + } else if o.ty.starts_with("OPR_VGPR") && width <= 8 { true } else if width >= 9 && (o.ty.starts_with("OPR_SRC") || o.ty.starts_with("OPR_VGPR")) && v >= 256 { v -= 256; @@ -1093,6 +1107,29 @@ mod tests { } } + #[test] + fn sdwa_register_uses_come_from_the_extension_dword() { + let uses = |words: [u32; 2]| { + let bytes: Vec = words.iter().flat_map(|w| w.to_le_bytes()).collect(); + let d = decode(&bytes, 0).unwrap(); + assert_eq!(d.variant, Variant::Sdwa); + (sgpr_uses(&d), vgpr_uses(&d)) + }; + // v_cmp_lt_f32_sdwa s5, v1, s2: reads v1 and s2, writes the mask to s[5:6] + assert_eq!(uses([0x7c02_04f9, 0x8606_8501]), ((vec![2], vec![5, 6]), (vec![1], vec![]))); + // v_cmp_lt_f32_sdwa v1, v2 (no SD: VCC, implicit as in the plain form) + assert_eq!(uses([0x7c02_04f9, 0x0606_0001]), ((vec![], vec![]), (vec![1, 2], vec![]))); + // v_cmpx_lt_f32_sdwa v1, v2 with the SD bits set: EXEC, no SGPR + assert_eq!(uses([0x7c22_04f9, 0x0606_8501]), ((vec![], vec![]), (vec![1, 2], vec![]))); + // v_lshlrev_b32_sdwa v5, s1, s2 + assert_eq!(uses([0x340a_04f9, 0x8686_0601]), ((vec![1, 2], vec![]), (vec![], vec![5]))); + // v_lshlrev_b32_sdwa v5, 1.0, v2 (S0 with an inline constant: no register) and v5, v1, v2 + assert_eq!(uses([0x340a_04f9, 0x0686_06f2]), ((vec![], vec![]), (vec![2], vec![5]))); + assert_eq!(uses([0x340a_04f9, 0x0606_0601]), ((vec![], vec![]), (vec![1, 2], vec![5]))); + // v_mov_b32_sdwa v1, s2 + assert_eq!(uses([0x7e02_02f9, 0x0086_0602]), ((vec![2], vec![]), (vec![], vec![1]))); + } + #[test] fn encoder_round_trips() { let b = encode("V_ADD_F32", "ENC_VOP2", &[("VDST", 1), ("SRC0", 256 + 2), ("VSRC1", 3)], None).unwrap(); diff --git a/crates/xcl/src/gpu/recomp/tests.rs b/crates/xcl/src/gpu/recomp/tests.rs index 6777b5a967..a219feb6a4 100644 --- a/crates/xcl/src/gpu/recomp/tests.rs +++ b/crates/xcl/src/gpu/recomp/tests.rs @@ -142,6 +142,39 @@ fn f(x: f32) -> u32 { x.to_bits() } +/// What `rdna2::sgpr_uses` reports for an SDWA compare into an SGPR reaches its callers: the +/// mask it writes is a mask to `geometry_wave32_hazards`, and a constant it overwrites is no +/// longer a word of a static sampler. +#[test] +fn sdwa_compare_destination_is_a_write_to_the_analyses() { + let one = |name: &str, enc: &str, fields: &[(&str, u64)]| encode(name, enc, fields, None).unwrap(); + // v_cmp_lt_f32_sdwa s, v1, v2 + let compare = |sdst: u32| [0x7c02_04f9u32, 0x0606_8001 | sdst << 8].iter().flat_map(|w| w.to_le_bytes()).collect::>(); + + let mut count = compare(10); + count.extend(one("S_BCNT1_I32_B32", "ENC_SOP1", &[("SDST", 0), ("SSRC0", 10)])); + assert!(super::geometry_wave32_hazards(&count).contains("whole-wave mask reduction")); + + // s[8:11] = 1, 2, 3, 4; image_sample with them as its sampler; the same after s9 was written + let mut code = Vec::new(); + for r in 8..12 { + code.extend(one("S_MOVK_I32", "ENC_SOPK", &[("SDST", r), ("SIMM16", r - 7)])); + } + let sample = one("IMAGE_SAMPLE", "ENC_MIMG", &[("DMASK", 15), ("VADDR", 0), ("VDATA", 4), ("SRSRC", 0), ("SSAMP", 2)]); + code.extend(&sample); + code.extend(compare(9)); + code.extend(&sample); + let (mut insts, mut off) = (Vec::new(), 0); + while off < code.len() { + let d = crate::gpu::rdna2::decode(&code[off..], off as u64).unwrap(); + off += d.len; + insts.push(d); + } + assert_eq!((insts[4].inst.name, insts[5].inst.name, insts[6].inst.name), ("IMAGE_SAMPLE", "V_CMP_LT_F32", "IMAGE_SAMPLE")); + let samplers = super::constant_samplers(&insts, &Default::default(), false); + assert_eq!(samplers.into_iter().collect::>(), [(insts[4].pc, [1, 2, 3, 4])]); +} + #[test] fn geometry_wave32_rejects_cross_half_operations() { let one = |name: &str, enc: &str, fields: &[(&str, u64)]| encode(name, enc, fields, None).unwrap();