diff --git a/crates/xcl/src/gpu/recomp/emit.rs b/crates/xcl/src/gpu/recomp/emit.rs index 216240bd2c..d124ca3d13 100644 --- a/crates/xcl/src/gpu/recomp/emit.rs +++ b/crates/xcl/src/gpu/recomp/emit.rs @@ -1733,12 +1733,15 @@ impl Em { self.line(&format!("if (and({act}, {ok})) st_bytes({t}_a, uint4({v}, 0u, 0u, 0u), {n}u);")); } else { let bits = 8 * n; + self.line(&format!("bool {t}_act = {act};")); + self.line(&format!("bool {t}_ok = {ok};")); + self.line(&format!("uint {t}_raw = ld_raw(select(and({t}_act, {t}_ok), {t}_a, 0u)).x;")); let val = if signed { - format!("(uint)(((int)(ld_raw({t}_a).x << {}u)) >> {}u)", 32 - bits, 32 - bits) + format!("(uint)(((int)({t}_raw << {}u)) >> {}u)", 32 - bits, 32 - bits) } else { - format!("(ld_raw({t}_a).x & {}u)", (1u64 << bits) - 1) + format!("({t}_raw & {}u)", (1u64 << bits) - 1) }; - self.line(&format!("if ({act}) {v} = {ok} ? {val} : 0u;")); + self.line(&format!("{v} = select({t}_act, select({t}_ok, {val}, 0u), {v});")); } return Ok(()); } @@ -1777,10 +1780,11 @@ impl Em { BufKind::FormatLoad => { // format from the instruction (tbuffer) or from the V# (dword3 [18:12]); DST_SEL from the V# let fmt = if d.enc.name == "ENC_MTBUF" { format!("{}u", f("FORMAT")) } else { format!("(({d3} >> 12) & 0x7Fu)") }; + self.line(&format!("bool {t}_act = {act};")); self.line(&format!("uint {t}_info = FMT_INFO[min({fmt}, 77u)];")); self.line(&format!("bool {t}_int = ((({t}_info >> 3) & 7u) == 4u) || ((({t}_info >> 3) & 7u) == 5u);")); self.line(&format!("bool {t}_ok = and(({t}_info & 7u) != 0u, {});", in_range(format!("fmt_bytes({t}_info)")))); - self.line(&format!("uint4 {t}_r = ld_raw({t}_a);")); + self.line(&format!("uint4 {t}_r = ld_raw(select(and({t}_act, {t}_ok), {t}_a, 0u));")); self.line(&format!( "uint4 {t}_c = uint4(fmt_comp({t}_r, {t}_info, 0u), fmt_comp({t}_r, {t}_info, 1u), fmt_comp({t}_r, {t}_info, 2u), fmt_comp({t}_r, {t}_info, 3u));" )); @@ -1801,7 +1805,7 @@ impl Em { assigns.push(format!("{v} = {t}_ok ? ({lo} | ({hi} << 16)) : 0u;")); } } - self.line(&format!("if ({act}) {{ {} }}", assigns.join(" "))); + self.line(&format!("if ({t}_act) {{ {} }}", assigns.join(" "))); return Ok(()); } let mut assigns = Vec::new(); @@ -1809,7 +1813,7 @@ impl Em { let v = self.vreg(vdata + i); assigns.push(format!("{v} = select({t}_ok, dst_sel({t}_c, ({d3} >> {}u) & 7u, {t}_int), 0u);", 3 * i)); } - self.line(&format!("if ({act}) {{ {} }}", assigns.join(" "))); + self.line(&format!("if ({t}_act) {{ {} }}", assigns.join(" "))); } BufKind::FormatStore => { let fmt = if d.enc.name == "ENC_MTBUF" { format!("{}u", f("FORMAT")) } else { format!("(({d3} >> 12) & 0x7Fu)") }; @@ -3772,8 +3776,10 @@ impl Em { if store { self.line(&format!("if (and({act}, {ok})) st_bytes({t}_a, uint4({}, 0u, 0u, 0u), {n}u);", d16_data(&v, half))); } else { - let raw = format!("select({ok}, ld_raw({t}_a).x, 0u)"); - self.line(&format!("if ({act}) {v} = {};", d16_merge(&v, &raw, n, signed, half))); + self.line(&format!("bool {t}_act = {act};")); + self.line(&format!("bool {t}_ok = {ok};")); + let raw = format!("select({t}_ok, ld_raw(select(and({t}_act, {t}_ok), {t}_a, 0u)).x, 0u)"); + self.line(&format!("if ({t}_act) {v} = {};", d16_merge(&v, &raw, n, signed, half))); } Ok(()) } diff --git a/crates/xcl/src/gpu/recomp/tests.rs b/crates/xcl/src/gpu/recomp/tests.rs index 5a8d735396..7638725abb 100644 --- a/crates/xcl/src/gpu/recomp/tests.rs +++ b/crates/xcl/src/gpu/recomp/tests.rs @@ -7419,6 +7419,87 @@ fn gpu_safe_s_barrier_multi_warp_and_early_endpgm() { } } +#[test] +fn format_and_subdword_buffer_loads_clamp_out_of_range_offsets() { + let ident = 4 | 5 << 3 | 6 << 6 | 7 << 9; + let code = prog(&[ + // Keep lanes 0..48 active; lanes 48..64 are inactive in EXEC with garbage offsets. + ("V_CMPX_GT_U32", "ENC_VOPC", &[("SRC0", 128 + 48), ("VSRC1", 0)], None), + ("BUFFER_LOAD_FORMAT_XYZW", "ENC_MUBUF", &[("VDATA", 4), ("VADDR", 1), ("SRSRC", 3), ("SOFFSET", 128), ("OFFEN", 1)], None), + ("TBUFFER_LOAD_FORMAT_XYZW", "ENC_MTBUF", &[("VDATA", 8), ("VADDR", 1), ("SRSRC", 3), ("SOFFSET", 128), ("OFFEN", 1), ("FORMAT", 20)], None), + ("BUFFER_LOAD_UBYTE", "ENC_MUBUF", &[("VDATA", 12), ("VADDR", 1), ("SRSRC", 3), ("SOFFSET", 128), ("OFFEN", 1)], None), + ("BUFFER_LOAD_SHORT_D16", "ENC_MUBUF", &[("VDATA", 13), ("VADDR", 1), ("SRSRC", 3), ("SOFFSET", 128), ("OFFEN", 1)], None), + ("BUFFER_LOAD_FORMAT_D16_XY", "ENC_MUBUF", &[("VDATA", 14), ("VADDR", 1), ("SRSRC", 4), ("SOFFSET", 128), ("OFFEN", 1)], None), + ]); + + for windows in [1u32, 4u32] { + let t = translate(&code, &Options { mem_windows: windows, ..Default::default() }).unwrap(); + assert!(t.complete(), "{:?}", t.unsupported_why); + let body = t.hlsl.split("[numthreads").nth(1).expect("entry point"); + let calls: Vec<&str> = body.match_indices("ld_raw(").map(|(i, _)| &body[i..i + 24]).collect(); + assert_eq!(calls.len(), 5, "expected 5 ld_raw calls in body: {calls:?}"); + for c in &calls { + assert!( + c.starts_with("ld_raw(select(and("), + "windows={windows}: unclamped ld_raw address in generated HLSL: {c}" + ); + } + if super::dxc::location().is_ok() { + let _ = super::dxc::compile(&t.hlsl, "main", "cs_6_6", &[]).unwrap_or_else(|e| panic!("{e}\n{}", t.hlsl)); + } + } + + if !gpu_ready() { + return; + } + let mut mem = vec![0u32; 256]; + for i in 0..16usize { + mem[0x100 / 4 + i] = 0xA1B2_0000 | (0x10 + i as u32); + mem[0x200 / 4 + i] = 0x3C00 | (0x4000 << 16); // (1.0, 2.0) in f16 + } + let mut st = KernelState::default(); + // s[12:15]: base 0x100, limit 0x40, format 20 (32_UINT); s[16:19]: base 0x200, limit 0x40, format 29 (16_16_FLOAT) + st.sgpr[12..16].copy_from_slice(&[0x100, 0, 0x40, ident | 20 << 12]); + st.sgpr[16..20].copy_from_slice(&[0x200, 0, 0x40, ident | 29 << 12]); + for (l, r) in st.vgpr.iter_mut().enumerate() { + r[0] = l as u32; + r[1] = if l < 16 { + 4 * l as u32 + } else if l < 32 { + 0x9000_0000 + } else if l < 48 { + 0xFFFF_FFF0 + } else { + 0xDEAD_BEE0 + }; + for reg in 4..=14 { + r[reg] = 0x7777_8888; + } + } + let r = runner::run_code(&code, &st, &mem).unwrap(); + for l in 0..64usize { + let v = &r.vgpr[l]; + if l < 16 { + let word = 0xA1B2_0000 | (0x10 + l as u32); + assert_eq!([v[4], v[5], v[6], v[7]], [word, 0, 0, 1], "lane {l}: buffer_load_format_xyzw"); + assert_eq!([v[8], v[9], v[10], v[11]], [word, 0, 0, 1], "lane {l}: tbuffer_load_format_xyzw"); + assert_eq!(v[12], word & 0xFF, "lane {l}: buffer_load_ubyte"); + assert_eq!(v[13], 0x7777_0000 | (word & 0xFFFF), "lane {l}: buffer_load_ushort_d16"); + assert_eq!(v[14], 0x3C00 | (0x4000 << 16), "lane {l}: buffer_load_format_d16_xy"); + } else if l < 48 { + assert_eq!([v[4], v[5], v[6], v[7]], [0, 0, 0, 0], "lane {l}: OOB format load -> 0"); + assert_eq!([v[8], v[9], v[10], v[11]], [0, 0, 0, 0], "lane {l}: OOB tbuffer format load -> 0"); + assert_eq!(v[12], 0, "lane {l}: OOB ubyte load -> 0"); + assert_eq!(v[13], 0x7777_0000, "lane {l}: OOB ushort_d16 clears low half and keeps high half"); + assert_eq!(v[14], 0, "lane {l}: OOB format_d16_xy -> 0"); + } else { + for reg in 4..=14 { + assert_eq!(v[reg], 0x7777_8888, "lane {l} reg v{reg}: inactive lane must preserve register"); + } + } + } +} + /// `XCL_SAMPLE_FSPX=`: every `*.bin` there that is an FSPX (driver) compute container – /// e.g. the FSR4 pipelines XCL dumps to `\shaders` – translates and compiles. #[test]