Skip to content
Open
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
150 changes: 136 additions & 14 deletions crates/xcl/src/gpu/dxil/lower_mem.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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<Value> = (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);
Expand All @@ -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]]);
}
Expand Down Expand Up @@ -1821,19 +1936,26 @@ 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");
let bytes = if x2 { 8 } else { 4 };
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);
Expand Down
80 changes: 63 additions & 17 deletions crates/xcl/src/gpu/recomp/emit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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_") {
Expand Down Expand Up @@ -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 {
Expand All @@ -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;
Expand Down Expand Up @@ -2164,7 +2202,7 @@ impl Em {
let oks: Vec<String> = (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::<Vec<_>>().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];
Expand All @@ -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(" ")));
}
Expand Down Expand Up @@ -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();
Expand Down Expand Up @@ -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 {
Expand All @@ -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})")
Expand Down
Loading
Loading