diff --git a/crates/xcl/src/gpu/dxil/lower_mem.rs b/crates/xcl/src/gpu/dxil/lower_mem.rs index fe04b4ac65..ecd8d2cbe9 100644 --- a/crates/xcl/src/gpu/dxil/lower_mem.rs +++ b/crates/xcl/src/gpu/dxil/lower_mem.rs @@ -1145,6 +1145,94 @@ impl Lw { // ------------------------------------------------------------ MUBUF / MTBUF + /// The byte address of a MUBUF / MTBUF access, V# base + SOFFSET + the offset of `index` + /// and `off` (VGPR offset + instruction offset), and its window. Returns the address, + /// whether the access is swizzled and the distance between its dwords in bytes. + /// + /// A swizzled V# (word 1 bit 31 with a nonzero stride) on an access with IDXEN, or with + /// ADD_TID (word 3 bit 23) and no IDXEN, interleaves groups of `step = 8 << INDEX_STRIDE` + /// (word 3 [22:21]) records in dwords: `((index / step) * stride + (off & ~3)) * step + + /// (index % step) * 4 + (off & 3)`; SOFFSET is added unscaled. Without IDXEN each dword of a + /// multi-dword access is swizzled at `off + 4k`, `4 * step` bytes apart; with IDXEN they are + /// contiguous. Anything else is linear, `off + stride * index`. (AnyPS5#21970, measured on + /// gfx1036 with compute loads and stores; ELEMENT_SIZE has no effect. The HLSL path: + /// `recomp::emit::Em::buf_rel`.) + #[allow(clippy::too_many_arguments)] + fn buf_addr(&mut self, bits: [Value; 4], soff: Value, voff: Value, inst_off: u32, stride: Value, index: Value, idxen: bool, write: bool) -> (Value, Value, Value) { + let [d0, d1, d3, off] = bits; + let sh = self.lshrc(d3, 21); + let sh = self.andc(sh, 3); + let sh = self.addc(sh, 3); + let b31 = self.lshrc(d1, 31); + let on = self.cmpc(Pred::Ne, b31, 0); + let snz = self.cmpc(Pred::Ne, stride, 0); + let mut swz = self.land(on, snz); + if !idxen { + let t = self.lshrc(d3, 23); + let t = self.andc(t, 1); + let tid = self.cmpc(Pred::Ne, t, 0); + swz = self.land(swz, tid); + } + let q = self.lshr(index, sh); + let qs = self.mul(q, stride); + let oa = self.andc(off, !3); + let hi = self.add(qs, oa); + let hi = self.shl(hi, sh); + let one = self.c(1); + let m = self.shl(one, sh); + let m = self.sub(m, one); + let r = self.and(index, m); + let r4 = self.shlc(r, 2); + let o3 = self.andc(off, 3); + let lo = self.add(r4, o3); + let sw = self.add(hi, lo); + let si = self.mul(stride, index); + let lin = self.add(off, si); + let rel = self.sel(swz, sw, lin); + let a = self.add(d0, soff); + let a = self.add(a, rel); + if self.windowed() { + // the window comes from the untruncated relative offset, carried in 32-bit halves + // (`rel` is its low dword) as `window_at` does, without i64 + let c = |lw: &mut Self, sum: Value, a: Value| { + let k = lw.cmp(Pred::ULt, sum, a); + lw.u(k) + }; + // off = voff + OFFSET, with its carry + let off_hi = if inst_off == 0 { self.c(0) } else { c(self, off, voff) }; + // linear: off + stride * index + let (si_hi, _) = self.s.umul_wide(stride, index); + let lin_c = c(self, lin, si); + let lin_hi = self.add(si_hi, off_hi); + let lin_hi = self.add(lin_hi, lin_c); + // swizzled: ((q * stride + (off & ~3)) << sh) + lo, sh in 3..=6 + let (qs_hi, _) = self.s.umul_wide(q, stride); + let t_lo = self.add(qs, oa); + let t_c = c(self, t_lo, qs); + let t_hi = self.add(qs_hi, off_hi); + let t_hi = self.add(t_hi, t_c); + let t_hi = self.shl(t_hi, sh); + let k32 = self.c(32); + let rsh = self.sub(k32, sh); + let spill = self.lshr(t_lo, rsh); + let t_hi = self.or(t_hi, spill); + let sw_c = c(self, sw, hi); + let sw_hi = self.add(t_hi, sw_c); + let rel_hi = self.sel(swz, sw_hi, lin_hi); + let base_hi = self.andc(d1, 0xFFFF); + let base_hi = self.add(base_hi, rel_hi); + self.window_at(base_hi, d0, &[Ea::U(soff), Ea::U(rel)], write); + } + let four = self.c(4); + let dstep = if idxen { + four + } else { + let s = self.shl(four, sh); + self.sel(swz, s, four) + }; + (a, swz, dstep) + } + pub fn mubuf(&mut self, d: &Decoded, name: &str) -> LResult<()> { if name.starts_with("buffer_atomic_") { return self.buffer_atomic(d, name); @@ -1174,16 +1262,22 @@ impl Lw { let soff = self.code32(f("SOFFSET"), None, false).ok_or_else(|| format!("{}: op", rdna2::format(d)))?; let (offen, idxen) = (f("OFFEN") != 0, f("IDXEN") != 0); let (d0, d1, d2, d3) = (self.sreg(srsrc), self.sreg(srsrc + 1), self.sreg(srsrc + 2), self.sreg(srsrc + 3)); - let index = if idxen { self.vreg(vaddr) } else { self.c(0) }; + // ADD_TID_ENABLE (V# word 3 bit 23) without IDXEN: the lane is the index (RDNA2, AnyPS5#21970) + let index = if idxen { + self.vreg(vaddr) + } else { + let t = self.lshrc(d3, 23); + let t = self.andc(t, 1); + let on = self.cmpc(Pred::Ne, t, 0); + let z = self.c(0); + let lane = self.lane; + self.sel(on, lane, z) + }; let voff = if offen { self.vreg(vaddr + idxen as u64) } else { self.c(0) }; let stride = self.lshrc(d1, 16); let stride = self.andc(stride, 0x3FFF); let off = self.addc(voff, f("OFFSET") as u32); - let a = self.add(d0, soff); - let a = self.add(a, off); - let si = self.mul(stride, index); - let a = self.add(a, si); - self.buffer_window(d0, d1, &[Ea::U(soff), Ea::U(voff), Ea::K(f("OFFSET") as u32), Ea::Mul(stride, index)], name.contains("store")); + let (a, swz, dstep) = self.buf_addr([d0, d1, d3, off], soff, voff, f("OFFSET") as u32, stride, index, idxen, name.contains("store")); let act = self.act(); let szero = self.cmpc(Pred::Eq, stride, 0); let idx_ok = self.cmp(Pred::ULt, index, d2); @@ -1241,7 +1335,26 @@ impl Lw { in_range(self, b) }) .collect(); - let loaded = self.mem_ld_n(Some(act), a, count as u32); + let loaded = if idxen || count == 1 { + self.mem_ld_n(Some(act), a, count as u32) + } else { + // without IDXEN a swizzled access reads its dwords `dstep` apart; the V# is + // uniform, so one of the two loads runs + let nswz = self.not(swz); + let g_lin = self.land(act, nswz); + let lin = self.mem_ld_n(Some(g_lin), a, count as u32); + let g_swz = self.land(act, swz); + let at: Vec = (0..count as u32) + .map(|i| { + let k = self.c(i); + let o = self.mul(dstep, k); + let x = self.add(a, o); + self.andc(x, !3) + }) + .collect(); + let sw = self.with_mem_i32s(Some(g_swz), count as usize, &mut |lw, m| at.iter().map(|&x| lw.s.raw_load(m, x, 1)[0]).collect()); + (0..count as usize).map(|i| self.sel(swz, sw[i], lin[i])).collect() + }; let z = self.c(0); for i in 0..count { let v = self.sel(oks[i as usize], loaded[i as usize], z); @@ -1258,7 +1371,9 @@ impl Lw { }) .collect(); for i in 0..count as usize { - let at = self.addc(a, 4 * i as u32); + let k = self.c(i as u32); + let o = self.mul(dstep, k); + let at = self.add(a, o); let c = self.land(act, oks[i]); self.mem_st_n(Some(c), at, &[vals[i]]); } @@ -1821,7 +1936,18 @@ impl Lw { let soff = self.code32(f("SOFFSET"), None, false).ok_or_else(|| format!("{}: op", rdna2::format(d)))?; let (offen, idxen) = (f("OFFEN") != 0, f("IDXEN") != 0); let (d0, d1, d2) = (self.sreg(srsrc), self.sreg(srsrc + 1), self.sreg(srsrc + 2)); - let index = if idxen { self.vreg(vaddr) } else { self.c(0) }; + let d3 = self.sreg(srsrc + 3); + // ADD_TID_ENABLE (V# word 3 bit 23) without IDXEN: the lane is the index (RDNA2, AnyPS5#21970) + let index = if idxen { + self.vreg(vaddr) + } else { + let t = self.lshrc(d3, 23); + let t = self.andc(t, 1); + let on = self.cmpc(Pred::Ne, t, 0); + let z = self.c(0); + let lane = self.lane; + self.sel(on, lane, z) + }; let voff = if offen { self.vreg(vaddr + idxen as u64) } else { self.c(0) }; let x2 = op.ends_with("_x2"); let op = op.trim_end_matches("_x2"); @@ -1829,11 +1955,7 @@ impl Lw { let stride = self.lshrc(d1, 16); let stride = self.andc(stride, 0x3FFF); let off = self.addc(voff, f("OFFSET") as u32); - let a = self.add(d0, soff); - let a = self.add(a, off); - let si = self.mul(stride, index); - let a = self.add(a, si); - self.buffer_window(d0, d1, &[Ea::U(soff), Ea::U(voff), Ea::K(f("OFFSET") as u32), Ea::Mul(stride, index)], true); + let (a, _, _) = self.buf_addr([d0, d1, d3, off], soff, voff, f("OFFSET") as u32, stride, index, idxen, true); let a = self.andc(a, !(bytes - 1)); let szero = self.cmpc(Pred::Eq, stride, 0); let e = self.addc(off, bytes); diff --git a/crates/xcl/src/gpu/recomp/emit.rs b/crates/xcl/src/gpu/recomp/emit.rs index 069c8b3f6f..5d3a330df4 100644 --- a/crates/xcl/src/gpu/recomp/emit.rs +++ b/crates/xcl/src/gpu/recomp/emit.rs @@ -2072,6 +2072,40 @@ impl Em { format!("xcl_ss{}", self.opt.static_sampler_base as usize + i) } + /// The byte offset of a MUBUF / MTBUF access from V# base + SOFFSET as `{t}_rel` (uses + /// `{t}_off` and `{t}_stride`), and the distance between its dwords, in dwords. + /// + /// A swizzled V# (word 1 bit 31 with a nonzero stride) on an access with IDXEN, or with + /// ADD_TID (word 3 bit 23) and no IDXEN, interleaves groups of `step = 8 << INDEX_STRIDE` + /// (word 3 [22:21]) records in dwords: `((index / step) * stride + (off & ~3)) * step + + /// (index % step) * 4 + (off & 3)`; SOFFSET is added unscaled. Without IDXEN each dword of a + /// multi-dword access is swizzled at `off + 4k`, `4 * step` bytes apart; with IDXEN they are + /// contiguous. Anything else is linear, `off + stride * index`. (AnyPS5#21970, measured on + /// gfx1036 with compute loads and stores; ELEMENT_SIZE has no effect.) + fn buf_rel(&mut self, t: &str, d1: &str, d3: &str, index: &str, idxen: bool) -> String { + self.line(&format!("uint {t}_sh = 3u + (({d3} >> 21) & 3u);")); + if idxen { + self.line(&format!("bool {t}_swz = and(({d1} >> 31) != 0u, {t}_stride != 0u);")); + } else { + self.line(&format!("bool {t}_swz = and(and(({d1} >> 31) != 0u, {t}_stride != 0u), (({d3} >> 23) & 1u) != 0u);")); + } + let lo = format!("((({index} & ((1u << {t}_sh) - 1u)) << 2u) + ((uint){t}_off & 3u))"); + if self.opt.mem_windows > 1 { + self.line(&format!( + "uint64_t {t}_rel = {t}_swz ? ((((uint64_t)({index} >> {t}_sh) * (uint64_t){t}_stride + ({t}_off & ~(uint64_t)3)) << {t}_sh) + (uint64_t){lo}) : ({t}_off + (uint64_t){t}_stride * (uint64_t){index});" + )); + } else { + self.line(&format!( + "uint {t}_rel = {t}_swz ? ((((({index} >> {t}_sh) * {t}_stride) + ({t}_off & ~3u)) << {t}_sh) + {lo}) : ({t}_off + {t}_stride * {index});" + )); + } + if idxen { + "1u".into() + } else { + format!("({t}_swz ? (1u << {t}_sh) : 1u)") + } + } + /// MUBUF / MTBUF: raw loads and stores, typed (format) loads. fn mubuf(&mut self, d: &Decoded, name: &str) -> Result<(), &'static str> { if name.starts_with("buffer_atomic_") { @@ -2104,16 +2138,19 @@ impl Em { let soff = self.code32(f("SOFFSET"), None, false).ok_or("op")?; let (offen, idxen) = (f("OFFEN") != 0, f("IDXEN") != 0); let (d0, d1, d2, d3) = (self.sreg(srsrc), self.sreg(srsrc + 1), self.sreg(srsrc + 2), self.sreg(srsrc + 3)); - let index = if idxen { self.vreg(vaddr) } else { "0u".into() }; + // ADD_TID_ENABLE (V# word 3 bit 23) without IDXEN: the lane is the index (RDNA2, AnyPS5#21970) + let index = if idxen { self.vreg(vaddr) } else { format!("select((({d3} >> 23) & 1u) != 0u, lane_id, 0u)") }; let voff = if offen { self.vreg(vaddr + idxen as u64) } else { "0u".into() }; let t = self.t(); + self.line(&format!("uint {t}_idx = {index};")); + let index = format!("{t}_idx"); let is_store = small.map_or(matches!(kind, BufKind::Store | BufKind::FormatStore), |(_, _, s)| s); self.line(&format!("uint {t}_stride = ({d1} >> 16) & 0x3FFFu;")); + let dw; 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};" - )); + dw = self.buf_rel(&t, &d1, &d3, &index, idxen); + self.line(&format!("uint64_t {t}_ea = ((((uint64_t)({d1} & 0xFFFFu)) << 32) | (uint64_t){d0}) + (uint64_t){soff} + {t}_rel;")); if is_store { self.window_write(&format!("(uint)({t}_ea >> 32)")); } else { @@ -2122,7 +2159,8 @@ 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};")); + dw = self.buf_rel(&t, &d1, &d3, &index, idxen); + self.line(&format!("uint {t}_a = {d0} + {soff} + {t}_rel;")); } let act = self.act(); let multi = self.opt.mem_windows > 1; @@ -2164,7 +2202,7 @@ impl Em { let oks: Vec = (0..4).map(|i| in_range(format!("{}u", 4 * i.min(count - 1) + 4))).collect(); let o = (0..4).map(|i| format!("{}u", 4 * i.min(count - 1))).collect::>().join(", "); self.line(&format!("bool4 {t}_ok = bool4({});", oks.join(", "))); - self.line(&format!("uint4 {t}_v = xcl_loadx4(select(and({t}_act, {t}_ok), ({t}_a & ~3u) + uint4({o}), 0u));")); + self.line(&format!("uint4 {t}_v = xcl_loadx4(select(and({t}_act, {t}_ok), ({t}_a & ~3u) + uint4({o}) * {dw}, 0u));")); for i in 0..count { let v = self.vreg(vdata + i); let c = ["x", "y", "z", "w"][i as usize]; @@ -2177,7 +2215,7 @@ impl Em { for i in 0..count { let ok = in_range(format!("{}u", 4 * i + 4)); let v = self.vreg(vdata + i); - st.push(format!("if ({ok}) mem.Store(({t}_a + {}u) & ~3u, {v});", 4 * i)); + st.push(format!("if ({ok}) mem.Store(({t}_a + {}u * {dw}) & ~3u, {v});", 4 * i)); } self.line(&format!("if ({act}) {{ {} }}", st.join(" "))); } @@ -4173,24 +4211,28 @@ impl Em { let soff = self.code32(f("SOFFSET"), None, false).ok_or("op")?; let (offen, idxen) = (f("OFFEN") != 0, f("IDXEN") != 0); let (d0, d1, d2) = (self.sreg(srsrc), self.sreg(srsrc + 1), self.sreg(srsrc + 2)); - let index = if idxen { self.vreg(vaddr) } else { "0u".into() }; + let d3 = self.sreg(srsrc + 3); + // ADD_TID_ENABLE (V# word 3 bit 23) without IDXEN: the lane is the index (RDNA2, AnyPS5#21970) + let index = if idxen { self.vreg(vaddr) } else { format!("select((({d3} >> 23) & 1u) != 0u, lane_id, 0u)") }; let voff = if offen { self.vreg(vaddr + idxen as u64) } else { "0u".into() }; let t = self.t(); + self.line(&format!("uint {t}_idx = {index};")); + let index = format!("{t}_idx"); let x2 = op.ends_with("_x2"); let op = op.trim_end_matches("_x2"); let bytes = if x2 { 8 } else { 4 }; self.line(&format!("uint {t}_stride = ({d1} >> 16) & 0x3FFFu;")); 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.buf_rel(&t, &d1, &d3, &index, idxen); + self.line(&format!("uint64_t {t}_ea = ((((uint64_t)({d1} & 0xFFFFu)) << 32) | (uint64_t){d0}) + (uint64_t){soff} + {t}_rel;")); 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.buf_rel(&t, &d1, &d3, &index, idxen); + self.line(&format!("uint {t}_a = ({d0} + {soff} + {t}_rel) & ~{}u;", bytes - 1)); self.line(&format!("bool {t}_ok = select({t}_stride == 0u, {t}_off + {bytes}u <= {d2}, {index} < {d2});")); } let act = self.act(); @@ -4231,15 +4273,18 @@ impl Em { let soff = self.code32(f("SOFFSET"), None, false).ok_or("op")?; let (offen, idxen) = (f("OFFEN") != 0, f("IDXEN") != 0); let (d0, d1, d2) = (self.sreg(srsrc), self.sreg(srsrc + 1), self.sreg(srsrc + 2)); - let index = if idxen { self.vreg(vaddr) } else { "0u".into() }; + let d3 = self.sreg(srsrc + 3); + // ADD_TID_ENABLE (V# word 3 bit 23) without IDXEN: the lane is the index (RDNA2, AnyPS5#21970) + let index = if idxen { self.vreg(vaddr) } else { format!("select((({d3} >> 23) & 1u) != 0u, lane_id, 0u)") }; let voff = if offen { self.vreg(vaddr + idxen as u64) } else { "0u".into() }; let t = self.t(); + self.line(&format!("uint {t}_idx = {index};")); + let index = format!("{t}_idx"); self.line(&format!("uint {t}_stride = ({d1} >> 16) & 0x3FFFu;")); 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.buf_rel(&t, &d1, &d3, &index, idxen); + self.line(&format!("uint64_t {t}_ea = ((((uint64_t)({d1} & 0xFFFFu)) << 32) | (uint64_t){d0}) + (uint64_t){soff} + {t}_rel;")); if store { self.window_write(&format!("(uint)({t}_ea >> 32)")); } else { @@ -4248,7 +4293,8 @@ 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.buf_rel(&t, &d1, &d3, &index, idxen); + self.line(&format!("uint {t}_a = {d0} + {soff} + {t}_rel;")); } let ok = if self.opt.mem_windows > 1 { format!("select({t}_stride == 0u, {t}_off + (uint64_t){n}u <= (uint64_t){d2}, {index} < {d2})") diff --git a/crates/xcl/src/gpu/recomp/tests.rs b/crates/xcl/src/gpu/recomp/tests.rs index 8aa9aa1c01..f8d191fef7 100644 --- a/crates/xcl/src/gpu/recomp/tests.rs +++ b/crates/xcl/src/gpu/recomp/tests.rs @@ -1653,6 +1653,201 @@ fn buffer_format_table_matches_llvm() { assert!(checked >= 10, "{checked}"); } +/// A V# with ADD_TID_ENABLE (word 3 bit 23) and no IDXEN takes the lane as the index: lane l +/// reads base + l * stride (AnyPS5#21970 / #24781, gfx1036). Stride 4, no offsets. +#[test] +fn gpu_buffer_add_tid_indexes_by_lane() { + if !gpu_ready() { + return; + } + let ident = 4 | 5 << 3 | 6 << 6 | 7 << 9; + let code = prog(&[("BUFFER_LOAD_DWORD", "ENC_MUBUF", &[("VDATA", 10), ("SRSRC", 3), ("SOFFSET", 128)], None)]); + let mut mem = vec![0u32; 512]; + for l in 0..64 { + mem[0x100 / 4 + l] = 0xA000 + l as u32; + } + let mut st = KernelState::default(); + // stride 4 in word 1 [29:16], num_records 64, ADD_TID_ENABLE in word 3 bit 23 + st.sgpr[12..16].copy_from_slice(&[0x100, 4 << 16, 64, ident | 20 << 12 | 1 << 23]); + let r = runner::run_code(&code, &st, &mem).unwrap(); + let got: Vec = (0..64).map(|l| r.vgpr[l][10]).collect(); + let want: Vec = (0..64).map(|l| 0xA000 + l as u32).collect(); + assert_eq!(got, want, "lane l reads element l"); +} + +/// Byte offset of a swizzled buffer access (V# word 1 bit 31, stride != 0) from the V# base, as +/// AnyPS5#21970 measured it on gfx1036: `step = 8 << INDEX_STRIDE`, +/// `((idx / step) * stride + (off & ~3)) * step + (idx % step) * 4 + (off & 3)`. +fn swizzled_offset(idx: u32, off: u32, stride: u32, index_stride: u32) -> u32 { + let step = 8 << index_stride; + ((idx / step) * stride + (off & !3)) * step + (idx % step) * 4 + (off & 3) +} + +/// Runs one buffer load over a buffer whose dword i holds `0x5000_0000 | i` at V# base 0x100 and +/// returns, per lane and per loaded dword, the byte offset (from the V# base) it was read from. +fn swizzle_load(op: &str, fields: &[(&str, u64)], count: u64, word1: u32, word3: u32, vgpr0: impl Fn(usize) -> u32) -> Vec> { + let mut f = vec![("VDATA", 10), ("SRSRC", 3), ("SOFFSET", 128)]; + f.extend_from_slice(fields); + let code = prog(&[(op, "ENC_MUBUF", &f, None)]); + let mem: Vec = (0..2048u32).map(|i| 0x5000_0000 | i).collect(); + let mut st = KernelState::default(); + st.sgpr[12..16].copy_from_slice(&[0x100, word1, 64, word3]); + for l in 0..64 { + st.vgpr[l][0] = vgpr0(l); + } + let r = runner::run_code(&code, &st, &mem).unwrap(); + (0..64).map(|l| (0..count).map(|k| ((r.vgpr[l][10 + k as usize] & 0xFFFF) * 4).wrapping_sub(0x100)).collect()).collect() +} + +const SWZ_IDENT: u32 = 4 | 5 << 3 | 6 << 6 | 7 << 9 | 20 << 12; + +/// (a) ADD_TID with SWIZZLE_EN, stride 16, INDEX_STRIDE 0 (step 8), no IDXEN: lane l reads +/// `(l / 8) * 128 + (l % 8) * 4` (lane 1 at +4, where the linear rule reads +16). +#[test] +fn gpu_buffer_swizzle_add_tid() { + if !gpu_ready() { + return; + } + let got = swizzle_load("BUFFER_LOAD_DWORD", &[], 1, 16 << 16 | 1 << 31, SWZ_IDENT | 1 << 23, |_| 0); + let want: Vec> = (0..64).map(|l| vec![(l / 8) * 128 + (l % 8) * 4]).collect(); + assert_eq!(got[1], vec![4], "lane 1"); + assert_eq!(got, want); + // the instruction offset goes into the swizzled element: (l / 8) * 128 + 4 * 8 + (l % 8) * 4 + let got = swizzle_load("BUFFER_LOAD_DWORD", &[("OFFSET", 4)], 1, 16 << 16 | 1 << 31, SWZ_IDENT | 1 << 23, |_| 0); + let want: Vec> = (0..64).map(|l| vec![swizzled_offset(l, 4, 16, 0)]).collect(); + assert_eq!(got, want, "OFFSET 4"); +} + +/// (b) INDEX_STRIDE 3 (step 64): a whole wave64 is one group, lane l reads `4 * l` with stride 16. +#[test] +fn gpu_buffer_swizzle_index_stride() { + if !gpu_ready() { + return; + } + let got = swizzle_load("BUFFER_LOAD_DWORD", &[], 1, 16 << 16 | 1 << 31, SWZ_IDENT | 1 << 23 | 3 << 21, |_| 0); + let want: Vec> = (0..64).map(|l| vec![4 * l]).collect(); + assert_eq!(got, want); +} + +/// (c) IDXEN with SWIZZLE_EN: the VGPR index is swizzled (ADD_TID is ignored) and a two-dword +/// load is contiguous from its first dword's swizzled address. INDEX_STRIDE 1 (step 16), OFFSET 4. +#[test] +fn gpu_buffer_swizzle_idxen() { + if !gpu_ready() { + return; + } + let got = swizzle_load("BUFFER_LOAD_DWORDX2", &[("VADDR", 0), ("IDXEN", 1), ("OFFSET", 4)], 2, 16 << 16 | 1 << 31, SWZ_IDENT | 1 << 23 | 1 << 21, |l| 63 - l as u32); + let want: Vec> = (0..64) + .map(|l| { + let a = swizzled_offset(63 - l, 4, 16, 1); + vec![a, a + 4] + }) + .collect(); + assert_eq!(got, want); +} + +/// (d) A two-dword load with ADD_TID and no IDXEN swizzles each dword at `offset + 4k`: dword k +/// of lane l is at `(l / 8) * 128 + 32k + (l % 8) * 4`, not contiguous. +#[test] +fn gpu_buffer_swizzle_add_tid_x2() { + if !gpu_ready() { + return; + } + let got = swizzle_load("BUFFER_LOAD_DWORDX2", &[], 2, 16 << 16 | 1 << 31, SWZ_IDENT | 1 << 23, |_| 0); + let want: Vec> = (0..64).map(|l| vec![(l / 8) * 128 + (l % 8) * 4, (l / 8) * 128 + 32 + (l % 8) * 4]).collect(); + assert_eq!(got, want); +} + +/// (e) Controls: SWIZZLE_EN clear with ADD_TID stays linear (`16 * l`), and SWIZZLE_EN set with +/// neither IDXEN nor ADD_TID is linear too (every lane reads offset 0). +#[test] +fn gpu_buffer_swizzle_controls_linear() { + if !gpu_ready() { + return; + } + let got = swizzle_load("BUFFER_LOAD_DWORD", &[], 1, 16 << 16, SWZ_IDENT | 1 << 23, |_| 0); + let want: Vec> = (0..64).map(|l| vec![16 * l]).collect(); + assert_eq!(got, want, "SWIZZLE_EN clear"); + let got = swizzle_load("BUFFER_LOAD_DWORD", &[], 1, 16 << 16 | 1 << 31, SWZ_IDENT, |_| 0); + let want: Vec> = (0..64).map(|_| vec![0]).collect(); + assert_eq!(got, want, "SWIZZLE_EN without IDXEN or ADD_TID"); +} + +/// (f) Stores use the same rule: a two-dword ADD_TID store, swizzled per dword. +#[test] +fn gpu_buffer_swizzle_add_tid_store() { + if !gpu_ready() { + return; + } + let code = prog(&[("BUFFER_STORE_DWORDX2", "ENC_MUBUF", &[("VDATA", 10), ("SRSRC", 3), ("SOFFSET", 128)], None)]); + let mem = vec![0u32; 2048]; + let mut st = KernelState::default(); + st.sgpr[12..16].copy_from_slice(&[0x100, 16 << 16 | 1 << 31, 64, SWZ_IDENT | 1 << 23]); + for l in 0..64 { + st.vgpr[l][10] = 0xC000 | l as u32; + st.vgpr[l][11] = 0xD000 | l as u32; + } + let r = runner::run_code(&code, &st, &mem).unwrap(); + for l in 0..64u32 { + for (k, tag) in [(0u32, 0xC000u32), (1, 0xD000)] { + let a = 0x100 + swizzled_offset(l, 4 * k, 16, 0); + assert_eq!(r.mem[a as usize / 4], tag | l, "lane {l} dword {k} at {a:#x}"); + } + } +} + +/// (g) With several memory windows the window comes from the whole swizzled offset, not from +/// `stride * index` or its low dword. Step 8, stride 16 for A: +/// A: ADD_TID, V# base 0xDB6D_C6D8 in the 4 GiB below window 0, VGPR offset V = 0x2492_4928 +/// (7V = 2^32 + 24). Linear, base + V + 16l is window 0 + 0x1000 + 16l. Swizzled, base + 8V + +/// (l/8)*128 + (l%8)*4 carries twice, to window 1 + 0x1018 + (l/8)*128 + (l%8)*4. +/// B and C have V#s at the start of window 0. +/// B: IDXEN, index 0x10_0000 + l, stride 0x1000: `(index / 8) * stride * 8` = 2^32 + (l/8)*0x8000. +/// C: IDXEN, index 0x1000_0000 + l, stride 0x100: `(index / 8) * stride` is past 2^32 before the +/// shift, so the access is 16 windows up, outside UMA, and reads 0. +#[test] +fn gpu_buffer_swizzle_window_from_swizzled_offset() { + if !gpu_ready() { + return; + } + let arena = crate::gpu::uma::Uma::with_windows(2, 1 << 20).unwrap(); + let (w0, w1) = (arena.window(0).0 as u64, arena.window(1).0 as u64); + assert_eq!(w1 - w0, 1 << 32); + let base_hi = (w0 >> 32) as u32; + for i in 0..(1u64 << 18) { + unsafe { + *((w0 + 4 * i) as *mut u32) = 0xA000_0000 | i as u32; + *((w1 + 4 * i) as *mut u32) = 0xB000_0000 | i as u32; + } + } + let p = prog(&[ + ("BUFFER_LOAD_DWORD", "ENC_MUBUF", &[("VDATA", 10), ("VADDR", 0), ("OFFEN", 1), ("SRSRC", 3), ("SOFFSET", 128)], None), + ("BUFFER_LOAD_DWORD", "ENC_MUBUF", &[("VDATA", 11), ("VADDR", 1), ("IDXEN", 1), ("SRSRC", 4), ("SOFFSET", 128)], None), + ("BUFFER_LOAD_DWORD", "ENC_MUBUF", &[("VDATA", 12), ("VADDR", 2), ("IDXEN", 1), ("SRSRC", 5), ("SOFFSET", 128)], None), + ]); + let o = on_gpu(Options { mem_windows: 2, ..Default::default() }); + let t = translate(&p, &o).unwrap(); + let hlsl_dxil = super::dxc::compile(&t.hlsl, "main", "cs_6_6", &[]).map_err(|e| format!("{e}\n{}", t.hlsl)).unwrap(); + let dxil = direct_or(&p, &o, hlsl_dxil); + let hi = base_hi & 0xFFFF; + let mut st = KernelState::default(); + st.sgpr[12..16].copy_from_slice(&[0xDB6D_C6D8, ((base_hi - 1) & 0xFFFF) | 16 << 16 | 1 << 31, 64, SWZ_IDENT | 1 << 23]); + st.sgpr[16..20].copy_from_slice(&[0, hi | 0x1000 << 16 | 1 << 31, u32::MAX, SWZ_IDENT]); + st.sgpr[20..24].copy_from_slice(&[0, hi | 0x100 << 16 | 1 << 31, u32::MAX, SWZ_IDENT]); + for l in 0..64 { + st.vgpr[l][0] = 0x2492_4928; + st.vgpr[l][1] = 0x10_0000 + l as u32; + st.vgpr[l][2] = 0x1000_0000 + l as u32; + } + let r = runner::run_in_arena(&dxil, &st, &arena).unwrap(); + for l in 0..64u32 { + let lane = (l % 8) * 4; + assert_eq!(r.vgpr[l as usize][10], 0xB000_0000 | (0x1018 + (l / 8) * 128 + lane) / 4, "A lane {l}: window 1"); + assert_eq!(r.vgpr[l as usize][11], 0xB000_0000 | ((l / 8) * 0x8000 + lane) / 4, "B lane {l}: window 1"); + assert_eq!(r.vgpr[l as usize][12], 0, "C lane {l}: outside UMA"); + } +} + #[test] fn buffer_format_d16_packs_and_unpacks_halves() { let code = prog(&[