diff --git a/crates/xcl/src/gpu/dxil/lower_mem.rs b/crates/xcl/src/gpu/dxil/lower_mem.rs index a1d034e740..d74219af42 100644 --- a/crates/xcl/src/gpu/dxil/lower_mem.rs +++ b/crates/xcl/src/gpu/dxil/lower_mem.rs @@ -1172,9 +1172,14 @@ impl Lw { let act = self.act(); let szero = self.cmpc(Pred::Eq, stride, 0); let idx_ok = self.cmp(Pred::ULt, index, d2); + // an offset that wraps past 2^32 is past num_records too (#1893) + let off_ok = self.cmp(Pred::UGe, off, voff); let in_range = |lw: &mut Lw, bytes: Value| -> Value { let e = lw.add(off, bytes); let ok = lw.cmp(Pred::ULe, e, d2); + let no_wrap = lw.cmp(Pred::UGe, e, off); + let ok = lw.land(ok, no_wrap); + let ok = lw.land(ok, off_ok); lw.sel(szero, ok, idx_ok) }; if let Some((n, signed, store, half)) = d16s { @@ -1823,6 +1828,10 @@ impl Lw { let szero = self.cmpc(Pred::Eq, stride, 0); let e = self.addc(off, bytes); let r1 = self.cmp(Pred::ULe, e, d2); + // an offset that wraps past 2^32 is past num_records too (#1893) + let (off_ok, no_wrap) = (self.cmp(Pred::UGe, off, voff), self.cmp(Pred::UGe, e, off)); + let r1 = self.land(r1, off_ok); + let r1 = self.land(r1, no_wrap); let r2 = self.cmp(Pred::ULt, index, d2); let ok = self.sel(szero, r1, r2); let act = self.act(); diff --git a/crates/xcl/src/gpu/recomp/tests.rs b/crates/xcl/src/gpu/recomp/tests.rs index 527dc4e648..ab49bfb3e6 100644 --- a/crates/xcl/src/gpu/recomp/tests.rs +++ b/crates/xcl/src/gpu/recomp/tests.rs @@ -1526,6 +1526,57 @@ fn gpu_grouped_loads_bounds_and_inactive_lanes() { } } +/// Raw buffer accesses (stride 0) whose offset wraps past 2^32 are past num_records: the direct +/// DXIL backend reads 0 and drops the store and the atomic, as for any other offset past the +/// end (#1893). +#[test] +fn gpu_direct_raw_buffer_offsets_that_wrap_are_out_of_range() { + if !gpu_ready() { + return; + } + let code = prog(&[ + ("BUFFER_LOAD_DWORD", "ENC_MUBUF", &[("VDATA", 3), ("VADDR", 0), ("SRSRC", 3), ("SOFFSET", 128), ("OFFSET", 0x20), ("OFFEN", 1)], None), + ("BUFFER_LOAD_DWORD", "ENC_MUBUF", &[("VDATA", 5), ("VADDR", 1), ("SRSRC", 3), ("SOFFSET", 128), ("OFFSET", 0), ("OFFEN", 1)], None), + ("BUFFER_STORE_DWORD", "ENC_MUBUF", &[("VDATA", 4), ("VADDR", 0), ("SRSRC", 3), ("SOFFSET", 128), ("OFFSET", 0x20), ("OFFEN", 1)], None), + ("BUFFER_ATOMIC_ADD", "ENC_MUBUF", &[("VDATA", 6), ("VADDR", 1), ("SRSRC", 3), ("SOFFSET", 128), ("OFFEN", 1)], None), + // the instruction offset is what wraps: voff 0xFFFF_FFF0 + OFFSET 0x28 = 0x18 (returns the old value) + ("BUFFER_ATOMIC_ADD", "ENC_MUBUF", &[("VDATA", 7), ("VADDR", 0), ("SRSRC", 3), ("SOFFSET", 128), ("OFFSET", 0x28), ("OFFEN", 1), ("GLC", 1)], None), + ]); + let mut mem = vec![0u32; 256]; + for (i, m) in mem.iter_mut().enumerate() { + *m = 0x1000 + i as u32; + } + let mut st = KernelState::default(); + st.sgpr[12..16].copy_from_slice(&[0x40, 0, 0x100, 0]); // V#: base 0x40, stride 0, 0x100 bytes + for (l, r) in st.vgpr.iter_mut().enumerate() { + // lane 0 wraps, lane 1 is in range, the others are past the end + r[0] = [0xFFFF_FFF0, 4].get(l).copied().unwrap_or(0x200); + r[1] = [0xFFFF_FFFC, 4].get(l).copied().unwrap_or(0x200); + r[3] = 0xDEAD; + r[4] = 0xC0DE_0000 | l as u32; + r[5] = 0xDEAD; + r[6] = 5; + r[7] = 5; + } + let k = runner::direct_kernel_full(&code, true).unwrap(); + let r = runner::run(&k.dxil, &st, &mem).unwrap(); + // lane 0: 32-bit sums that wrap to 0x10 (+ 0x40 = 0x50) and to 0x3C + assert_eq!(r.vgpr[0][3], 0, "load at voff 0xFFFF_FFF0 + OFFSET 0x20"); + assert_eq!(r.vgpr[0][5], 0, "load at voff 0xFFFF_FFFC"); + assert_eq!(r.mem[0x50 / 4], 0x1000 + 0x50 / 4, "store at voff 0xFFFF_FFF0 + OFFSET 0x20 dropped"); + assert_eq!(r.mem[0x3C / 4], 0x1000 + 0x3C / 4, "atomic at voff 0xFFFF_FFFC dropped"); + // lane 1 is in range + assert_eq!(r.vgpr[1][3], 0x1000 + (0x40 + 4 + 0x20) / 4, "lane 1 load"); + assert_eq!(r.mem[(0x40 + 4 + 0x20) / 4], 0xC0DE_0001, "lane 1 store"); + assert_eq!(r.mem[(0x40 + 4) / 4], 0x1000 + (0x40 + 4) / 4 + 5, "lane 1 atomic"); + // the atomic whose instruction offset wraps (voff + OFFSET): dropped, and it returns 0 + assert_eq!(r.mem[0x58 / 4], 0x1000 + 0x58 / 4, "atomic at voff 0xFFFF_FFF0 + OFFSET 0x28 dropped"); + assert_eq!(r.vgpr[0][7], 0, "dropped atomic returns 0"); + // lane 1 is in range: it adds and returns the old value + assert_eq!(r.mem[0x6C / 4], 0x1000 + 0x6C / 4 + 5, "lane 1 atomic with OFFSET"); + assert_eq!(r.vgpr[1][7], 0x1000 + 0x6C / 4, "lane 1 atomic returns the old value"); +} + /// A real console vertex shader from the retail pack, run on the PC GPU /// (needs XCL_SAMPLE_RPF=…\shaders.rpf). #[test]