Skip to content
Closed
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
31 changes: 9 additions & 22 deletions crates/xcl/src/gpu/recomp/emit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1702,8 +1702,8 @@ impl Em {
let t = self.t();
let is_store = small.map_or(matches!(kind, BufKind::Store | BufKind::FormatStore), |(_, _, s)| s);
self.line(&format!("uint {t}_stride = ({d1} >> 16) & 0x3FFFu;"));
self.line(&format!("uint64_t {t}_off = (uint64_t){voff} + (uint64_t){}u;", f("OFFSET")));
if self.opt.mem_windows > 1 {
self.line(&format!("uint64_t {t}_off = (uint64_t){voff} + (uint64_t){}u;", f("OFFSET")));
self.line(&format!(
"uint64_t {t}_ea = ((((uint64_t)({d1} & 0xFFFFu)) << 32) | (uint64_t){d0}) + (uint64_t){soff} + {t}_off + (uint64_t){t}_stride * (uint64_t){index};"
));
Expand All @@ -1714,17 +1714,11 @@ impl Em {
}
self.line(&format!("uint {t}_a = (uint){t}_ea;"));
} else {
self.line(&format!("uint {t}_off = {voff} + {}u;", f("OFFSET")));
self.line(&format!("uint {t}_a = {d0} + {soff} + {t}_off + {t}_stride * {index};"));
self.line(&format!("uint {t}_a = {d0} + {soff} + (uint){t}_off + {t}_stride * {index};"));
}
let act = self.act();
let multi = self.opt.mem_windows > 1;
let in_range = |bytes: String| {
if multi {
format!("select({t}_stride == 0u, {t}_off + (uint64_t)({bytes}) <= (uint64_t){d2}, {index} < {d2})")
} else {
format!("select({t}_stride == 0u, {t}_off + {bytes} <= {d2}, {index} < {d2})")
}
format!("select({t}_stride == 0u, {t}_off + (uint64_t)({bytes}) <= (uint64_t){d2}, {index} < {d2})")
};
if let Some((n, signed, store)) = small {
let ok = in_range(format!("{n}u"));
Expand Down Expand Up @@ -3692,19 +3686,17 @@ impl Em {
let op = op.trim_end_matches("_x2");
let bytes = if x2 { 8 } else { 4 };
self.line(&format!("uint {t}_stride = ({d1} >> 16) & 0x3FFFu;"));
self.line(&format!("uint64_t {t}_off = (uint64_t){voff} + (uint64_t){}u;", f("OFFSET")));
if self.opt.mem_windows > 1 {
self.line(&format!("uint64_t {t}_off = (uint64_t){voff} + (uint64_t){}u;", f("OFFSET")));
self.line(&format!(
"uint64_t {t}_ea = ((((uint64_t)({d1} & 0xFFFFu)) << 32) | (uint64_t){d0}) + (uint64_t){soff} + {t}_off + (uint64_t){t}_stride * (uint64_t){index};"
));
self.window_write(&format!("(uint)({t}_ea >> 32)"));
self.line(&format!("uint {t}_a = (uint){t}_ea & ~{}u;", bytes - 1));
self.line(&format!("bool {t}_ok = select({t}_stride == 0u, {t}_off + (uint64_t){bytes}u <= (uint64_t){d2}, {index} < {d2});"));
} else {
self.line(&format!("uint {t}_off = {voff} + {}u;", f("OFFSET")));
self.line(&format!("uint {t}_a = ({d0} + {soff} + {t}_off + {t}_stride * {index}) & ~{}u;", bytes - 1));
self.line(&format!("bool {t}_ok = select({t}_stride == 0u, {t}_off + {bytes}u <= {d2}, {index} < {d2});"));
self.line(&format!("uint {t}_a = ({d0} + {soff} + (uint){t}_off + {t}_stride * {index}) & ~{}u;", bytes - 1));
}
self.line(&format!("bool {t}_ok = select({t}_stride == 0u, {t}_off + (uint64_t){bytes}u <= (uint64_t){d2}, {index} < {d2});"));
let act = self.act();
if x2 {
return self.atomic64(op, &format!("{t}_a"), vdata, vdata + 2, f("GLC") == 1, vdata, &format!("and({act}, {t}_ok)"));
Expand Down Expand Up @@ -3747,8 +3739,8 @@ impl Em {
let voff = if offen { self.vreg(vaddr + idxen as u64) } else { "0u".into() };
let t = self.t();
self.line(&format!("uint {t}_stride = ({d1} >> 16) & 0x3FFFu;"));
self.line(&format!("uint64_t {t}_off = (uint64_t){voff} + (uint64_t){}u;", f("OFFSET")));
if self.opt.mem_windows > 1 {
self.line(&format!("uint64_t {t}_off = (uint64_t){voff} + (uint64_t){}u;", f("OFFSET")));
self.line(&format!(
"uint64_t {t}_ea = ((((uint64_t)({d1} & 0xFFFFu)) << 32) | (uint64_t){d0}) + (uint64_t){soff} + {t}_off + (uint64_t){t}_stride * (uint64_t){index};"
));
Expand All @@ -3759,14 +3751,9 @@ impl Em {
}
self.line(&format!("uint {t}_a = (uint){t}_ea;"));
} else {
self.line(&format!("uint {t}_off = {voff} + {}u;", f("OFFSET")));
self.line(&format!("uint {t}_a = {d0} + {soff} + {t}_off + {t}_stride * {index};"));
self.line(&format!("uint {t}_a = {d0} + {soff} + (uint){t}_off + {t}_stride * {index};"));
}
let ok = if self.opt.mem_windows > 1 {
format!("select({t}_stride == 0u, {t}_off + (uint64_t){n}u <= (uint64_t){d2}, {index} < {d2})")
} else {
format!("select({t}_stride == 0u, {t}_off + {n}u <= {d2}, {index} < {d2})")
};
let ok = format!("select({t}_stride == 0u, {t}_off + (uint64_t){n}u <= (uint64_t){d2}, {index} < {d2})");
let act = self.act();
let v = self.vreg(vdata);
if store {
Expand Down
46 changes: 45 additions & 1 deletion crates/xcl/src/gpu/recomp/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -391,7 +391,7 @@ fn atomic_fcmpswap_x2_is_a_bitwise_64_bit_cas() {
assert!(t.hlsl.contains("_cmp = (uint64_t)v26 | ((uint64_t)v27 << 32);"), "{}", t.hlsl);
// 8-byte alignment of all three cells; the buffer form's bounds check reads records in dwords
assert_eq!(t.hlsl.matches("& ~7u;").count(), 3, "{}", t.hlsl);
assert!(t.hlsl.contains("_off + 8u <= s10"), "{}", t.hlsl);
assert!(t.hlsl.contains("_off + (uint64_t)8u <= (uint64_t)s10"), "{}", t.hlsl);
// GLC returns the old u64 as a low / high dword pair – in place in VDATA for the buffer
// form, into VDST for flat / global
assert_eq!(t.hlsl.matches("_old >> 32").count(), 3, "{}", t.hlsl);
Expand Down Expand Up @@ -7306,6 +7306,50 @@ fn gpu_sdwa_f16_inline_constants() {
}
}

#[test]
fn gpu_mubuf_raw_bounds_do_not_wrap_32bit() {
if !gpu_ready() {
return;
}
let p = prog(&[
// voff = 0xFFFF_FFFC, OFFSET = 0: t_off + 4 wraps to 0 in 32-bit uint
("BUFFER_LOAD_DWORD", "ENC_MUBUF", &[("VDATA", 10), ("VADDR", 0), ("SRSRC", 4), ("SOFFSET", 128), ("OFFEN", 1), ("OFFSET", 0)], None),
// voff = 0xFFFF_FFF0, OFFSET = 16: voff + OFFSET wraps to 0 in 32-bit uint
("BUFFER_LOAD_DWORD", "ENC_MUBUF", &[("VDATA", 11), ("VADDR", 1), ("SRSRC", 4), ("SOFFSET", 128), ("OFFEN", 1), ("OFFSET", 16)], None),
// wrapping store must not touch mem[base - 4]
("BUFFER_STORE_DWORD", "ENC_MUBUF", &[("VDATA", 4), ("VADDR", 0), ("SRSRC", 4), ("SOFFSET", 128), ("OFFEN", 1), ("OFFSET", 0)], None),
// wrapping atomic must not touch mem[base - 4]
("BUFFER_ATOMIC_ADD", "ENC_MUBUF", &[("VDATA", 5), ("VADDR", 0), ("SRSRC", 4), ("SOFFSET", 128), ("OFFEN", 1), ("OFFSET", 0)], None),
// wrapping d16 load (voff = 0xFFFF_FFFE, bytes = 2) must return 0 in low 16 bits
("BUFFER_LOAD_SHORT_D16", "ENC_MUBUF", &[("VDATA", 12), ("VADDR", 2), ("SRSRC", 4), ("SOFFSET", 128), ("OFFEN", 1), ("OFFSET", 0)], None),
// control: in-bounds load at offset 0 still reads mem[base]
("BUFFER_LOAD_DWORD", "ENC_MUBUF", &[("VDATA", 13), ("VADDR", 3), ("SRSRC", 4), ("SOFFSET", 128), ("OFFEN", 1), ("OFFSET", 0)], None),
]);
let mut st = KernelState::default();
// V# at s[16:19]: base = 0x100, stride = 0 (raw), num_records = 64
st.sgpr[16..20].copy_from_slice(&[0x100, 0, 64, 0]);
for r in st.vgpr.iter_mut() {
r[0] = 0xFFFF_FFFC;
r[1] = 0xFFFF_FFF0;
r[2] = 0xFFFF_FFFE;
r[3] = 0;
r[4] = 0x1111_2222;
r[5] = 1;
r[10] = 0xAAAA_AAAA;
r[11] = 0xBBBB_BBBB;
r[12] = 0x1234_5678;
}
let mut mem = vec![0u32; 0x80];
mem[0xFC / 4] = 0xDEAD_BEEF;
mem[0x100 / 4] = 0xCAFE_BABE;
let r = runner::run_code(&p, &st, &mem).unwrap();
assert_eq!(r.vgpr[0][10], 0, "BUFFER_LOAD_DWORD with voff=0xFFFFFFFC must be OOB (0)");
assert_eq!(r.vgpr[0][11], 0, "BUFFER_LOAD_DWORD with voff+OFFSET wrapping to 0 must be OOB (0)");
assert_eq!(r.vgpr[0][12], 0x1234_0000, "BUFFER_LOAD_USHORT_D16 with voff=0xFFFFFFFE must be OOB (0 in low half)");
assert_eq!(r.vgpr[0][13], 0xCAFE_BABE, "in-bounds control load");
assert_eq!(r.mem[0xFC / 4], 0xDEAD_BEEF, "wrapping store/atomic must not modify mem[base - 4]");
}

#[test]
fn fspx_compute_user_sgprs_from_rsrc2() {
use crate::gpu::xbox::tests::container;
Expand Down
Loading