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
7 changes: 2 additions & 5 deletions crates/xcl/src/gpu/dxil/kernel.rs
Original file line number Diff line number Diff line change
Expand Up @@ -502,8 +502,8 @@ impl Lw {
let Lw { s, ssa, init, .. } = &mut lw;
ssa.expect_preds(s, Block(0), 0, &mut |s, v| init.load(s, v));
}
// LDS and the split form's exchange slot size (16 words for a coherent s_load_dwordx16,
// 32 for ds_bpermute), as the HLSL kernel sizes them
// LDS and the split form's exchange slot size (16 words for a coherent s_load_dwordx16),
// as the HLSL kernel sizes them
// geometry mode always has the LDS (the ES ring and the GS output)
let mut uses_lds = matches!(entry, Entry::Geometry(_));
let mut slot = 1u32;
Expand All @@ -512,9 +512,6 @@ impl Lw {
if d.enc.name == "ENC_DS" && !matches!(n, "DS_SWIZZLE_B32" | "DS_BPERMUTE_B32" | "DS_PERMUTE_B32" | "DS_NOP") {
uses_lds = true;
}
if n == "DS_BPERMUTE_B32" {
slot = slot.max(32);
}
if d.enc.name == "ENC_SMEM" && ["GLC", "DLC"].iter().any(|f| crate::gpu::rdna2::field_value(d.enc, &d.words, f) == Some(1)) {
let w = match n.rsplit('_').next().unwrap_or("") {
"DWORDX2" => 2,
Expand Down
51 changes: 0 additions & 51 deletions crates/xcl/src/gpu/dxil/lanes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -383,57 +383,6 @@ impl Lw {
self.exchange_n(&[v])[0]
}

/// Split form: `v` of any lane `l` of the console wave (`ds_bpermute`): every lane
/// gives its value, 32 words per half.
pub fn read_lane_any(&mut self, v: Value, l: Value) -> Value {
if !self.split {
return self.rla(v, l);
}
if self.partner == Partner::Spin {
let l31 = self.lane31;
let src31 = self.andc(l, 31);
let other = self.spin_exchange(&[(l31, v)], &[src31])[0];
let own = self.s.wave_read_lane_at(v, src31);
let lh = self.lshrc(l, 5);
let h = self.half;
let same = self.cmp(Pred::Eq, lh, h);
return self.sel(same, own, other);
}
if self.partner != Partner::Present {
// no partner half: only this half's lanes (the others read 0)
let _ = self.exchange_n(&[]);
let src31 = self.andc(l, 31);
let own = self.s.wave_read_lane_at(v, src31);
let lh = self.lshrc(l, 5);
let h = self.half;
let same = self.cmp(Pred::Eq, lh, h);
let z = self.c(0);
return self.sel(same, own, z);
}
let (xs, base) = self.xs_storage();
let h = self.half;
let mine = self.shlc(h, 5);
let mine = self.addc(mine, base);
let l31 = self.lane31;
let at = self.add(mine, l31);
let p = self.s.gs_ptr(xs, at);
self.s.m.store(p, v, 4);
self.meet();
let one = self.c(1);
let o = self.xor(h, one);
let theirs = self.shlc(o, 5);
let theirs = self.addc(theirs, base);
let src31 = self.andc(l, 31);
let at = self.add(theirs, src31);
let q = self.s.gs_ptr(xs, at);
let other = self.s.m.load(q, 4);
self.meet();
let own = self.s.wave_read_lane_at(v, src31);
let lh = self.lshrc(l, 5);
let same = self.cmp(Pred::Eq, lh, h);
self.sel(same, own, other)
}

/// The program's ballot of `cond` → (lo, hi) mask words.
pub fn ballot(&mut self, cond: Value) -> (Value, Value) {
let w = self.s.wave_ballot(cond);
Expand Down
12 changes: 9 additions & 3 deletions crates/xcl/src/gpu/dxil/lower_mem.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2019,14 +2019,20 @@ impl Lw {
self.write32(&Dst::V(f("VDST")), t);
return Ok(());
}
// In wave64 the permute stays in the reader's 32-lane half, as ds_swizzle: lane l reads
// (l & 32) | (index & 31) (measured on RDNA2, #772), so the split form needs no exchange.
"ds_bpermute_b32" => {
let (a, v) = (self.vreg(f("ADDR")), self.vreg(f("DATA0")));
let m = if self.wave64 { 63 } else { 31 };
let (el, eh) = self.exec_mask();
let x = self.addc(a, off16);
let x = self.lshrc(x, 2);
let l = self.andc(x, m);
let r = self.read_lane_any(v, l);
let mut l = self.andc(x, 31);
if self.wave64 {
let lane = self.lane;
let up = self.andc(lane, 32);
l = self.or(up, l);
}
let r = self.read_lane_half(v, l);
let on = self.lanebit_at(el, eh, l);
let z = self.c(0);
let t = self.sel(on, r, z);
Expand Down
29 changes: 8 additions & 21 deletions crates/xcl/src/gpu/recomp/emit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ pub(super) struct Em {
/// scratch_* used: declare the per-lane private memory
uses_scratch: bool,
/// split wave64 (`x64`): words one exchange carries (1: masks and lane reads, up to 16:
/// coherent scalar loads, 32: `ds_bpermute`)
/// coherent scalar loads)
x64_words: usize,
/// split wave64 and pixel shaders: pc → SGPRs whose whole value may be read after that
/// instruction (`x64_masks`); a mask written to none of them is only read lane by lane
Expand Down Expand Up @@ -4372,23 +4372,17 @@ impl Em {
self.write32(&dst, &t);
return Ok(());
}
// In wave64 the permute stays in the reader's 32-lane half, as ds_swizzle: lane l reads
// (l & 32) | (index & 31) (measured on RDNA2, #772), so the split form needs no
// exchange and a 32-lane wave of a wave64 VS / PS never reaches lanes 32..63.
"ds_bpermute_b32" => {
let (a, v) = (self.vreg(f("ADDR")), self.vreg(f("DATA0")));
let m = if self.opt.wave64 { 63 } else { 31 };
let (el, eh) = (self.sreg(126), self.sreg(127));
let eh = if self.opt.wave64 { eh } else { "0u".into() };
self.line(&format!("uint {t}_l = (({a} + {off16}u) >> 2) & {m}u;"));
let rla = if self.x64() {
self.x64_words = 32;
"xcl_x_rla_any"
} else {
"rla"
};
let half = if self.opt.wave64 { "(lane_id & 32u) | " } else { "" };
self.line(&format!("uint {t}_l = {half}((({a} + {off16}u) >> 2) & 31u);"));
let rla = self.rla_half_fn();
self.line(&format!("uint {t} = {rla}({v}, {t}_l);"));
// a 32-lane wave of a wave64 VS / PS: lanes 32..63 hold a constant (`upper_lanes`)
if let Some(Read::Const(c)) = self.upper.as_ref().and_then(|u| u.reads.get(&d.pc)).copied() {
self.line(&format!("{t} = select(and(xcl_hw32, {t}_l >= 32u), {c:#010x}u, {t});"));
}
self.line(&format!("{t} = select(lanebit_at({el}, {eh}, {t}_l), {t}, 0u);"));
let dst = Dst::V(f("VDST"));
self.write32(&dst, &t);
Expand Down Expand Up @@ -5949,20 +5943,13 @@ uint xcl_x_rla_uniform(uint v, uint l) {
uint o = xcl_x_meet() ? xcl_x_get(0u) : 0u;
return (l >> 5) == xcl_half ? m : o;
}
// ds_bpermute: any lane of the wave
uint xcl_x_rla_any(uint v, uint l) {
xcl_x_put(WaveGetLaneIndex(), v);
uint o = xcl_x_meet() ? xcl_x_get(l & 31u) : 0u;
uint m = WaveReadLaneAt(v, l & 31u);
return (l >> 5) == xcl_half ? m : o;
}
// a mask only ever read lane by lane (v_cndmask, carry-ins; `x64_masks`): each half's own
// word, the other half's 0 – no exchange
uint2 xcl_x_ballot_local(bool b) {
uint m = WaveActiveBallot(b).x;
return xcl_half != 0u ? uint2(0u, m) : uint2(m, 0u);
}
// DPP16, DPP8, ds_swizzle, v_permlane(x)16: the ISA keeps the source in the reader's half
// DPP16, DPP8, ds_swizzle, ds_bpermute, v_permlane(x)16: the source stays in the reader's half
uint xcl_x_rla_half(uint v, uint l) { return WaveReadLaneAt(v, l & 31u); }
"#;

Expand Down
85 changes: 74 additions & 11 deletions crates/xcl/src/gpu/recomp/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3089,7 +3089,7 @@ fn gpu_format_stores_atomics_global_lds() {
if l < 63 {
assert_eq!(v[25], l as u32 + 1, "lane {l} ds_read2 (second; LDS is not zeroed past the written area)");
}
assert_eq!(v[29], 63 - l as u32, "lane {l} ds_bpermute");
assert_eq!(v[29], l as u32 ^ 31, "lane {l} ds_bpermute (index 63 - l, within its half)");
assert!(v[16] < 164, "lane {l} umax old value");
}
assert_eq!(r.mem[0x400 / 4], 64, "buffer_atomic_add");
Expand Down Expand Up @@ -6573,10 +6573,73 @@ fn mesh_split_wave64_form() {
assert!(super::translate_mesh(&lane, ms, true, true, 1).unwrap().hlsl.contains("xcl_x_rla_uniform("));
}

/// ds_bpermute_b32 in wave64 reads within each 32-lane half: lane l reads lane
/// `(l & 32) | (((addr + offset) >> 2) & 31)`, and 0 where that lane is inactive. Measured on
/// RDNA2 in native wave64: the three full-EXEC indices on an RX 6800M (gfx1031) by
/// luckyteagirl, https://github.com/ivall/xcl/issues/772#issuecomment-7123462950, and the
/// immediate offsets and the partial EXEC on gfx1036 by certified-retart,
/// https://github.com/ivall/xcl/issues/772#issuecomment-7127381205. The same results natively
/// (64-lane waves) and split (32-lane waves only).
#[test]
fn gpu_ds_bpermute_wave64_stays_in_its_half() {
if !gpu_ready() {
return;
}
let code = prog(&[
("DS_BPERMUTE_B32", "ENC_DS", &[("VDST", 10), ("ADDR", 1), ("DATA0", 0)], None), // lane
("DS_BPERMUTE_B32", "ENC_DS", &[("VDST", 11), ("ADDR", 2), ("DATA0", 0)], None), // 63 - lane
("DS_BPERMUTE_B32", "ENC_DS", &[("VDST", 12), ("ADDR", 3), ("DATA0", 0)], None), // (lane + 1) & 63
("DS_BPERMUTE_B32", "ENC_DS", &[("VDST", 13), ("ADDR", 1), ("DATA0", 0), ("OFFSET0", 4)], None),
("DS_BPERMUTE_B32", "ENC_DS", &[("VDST", 14), ("ADDR", 1), ("DATA0", 0), ("OFFSET0", 128)], None),
("DS_BPERMUTE_B32", "ENC_DS", &[("VDST", 15), ("ADDR", 2), ("DATA0", 0), ("OFFSET0", 4)], None),
("S_MOV_B32", "ENC_SOP1", &[("SDST", 126), ("SSRC0", 255)], Some(0x5555_5555)),
("S_MOV_B32", "ENC_SOP1", &[("SDST", 127), ("SSRC0", 255)], Some(0xAAAA_AAAA)),
("DS_BPERMUTE_B32", "ENC_DS", &[("VDST", 16), ("ADDR", 4), ("DATA0", 0)], None), // lane ^ 32
]);
let mut st = KernelState::default();
for (l, r) in st.vgpr.iter_mut().enumerate() {
let l = l as u32;
r[0] = 1000 + l; // a 0 from an inactive lane stays apart from lane 0
r[1] = 4 * l;
r[2] = 4 * (63 - l);
r[3] = 4 * ((l + 1) & 63);
r[4] = 4 * (l ^ 32);
r[16] = 0xCAFE_BABE;
}
let r = runner::run_code(&code, &st, &[]).unwrap();
let half = |l: u32, addr: u32, off: u32| 1000 + ((l & 32) | ((addr + off) >> 2 & 31));
let mut bad = Vec::new();
for l in 0..64u32 {
let v = &r.vgpr[l as usize];
let exec = if l < 32 { 0x5555_5555u32 >> l & 1 } else { 0xAAAA_AAAAu32 >> (l - 32) & 1 };
// the source lane (l & 32) | (l & 31) = l is active exactly when l is
let partial = if exec == 1 { half(l, 4 * (l ^ 32), 0) } else { 0xCAFE_BABE };
for (what, got, want) in [
("4 * lane", v[10], half(l, 4 * l, 0)),
("4 * (63 - lane)", v[11], half(l, 4 * (63 - l), 0)),
("4 * ((lane + 1) & 63)", v[12], half(l, 4 * ((l + 1) & 63), 0)),
("4 * lane, offset:4", v[13], half(l, 4 * l, 4)),
("4 * lane, offset:128", v[14], half(l, 4 * l, 128)),
("4 * (63 - lane), offset:4", v[15], half(l, 4 * (63 - l), 4)),
("4 * (lane ^ 32), exec 0xAAAAAAAA_55555555", v[16], partial),
] {
if got != want {
bad.push(format!("lane {l} {what}: {got}, want {want}"));
}
}
}
assert!(bad.is_empty(), "{} wrong:
{}", bad.len(), bad.join("
"));
// the measured lanes quoted on #772
assert_eq!([r.vgpr[0][11], r.vgpr[32][11], r.vgpr[63][11]], [1031, 1063, 1032], "63 - lane: lanes 0, 32, 63");
assert_eq!([r.vgpr[31][12], r.vgpr[63][12]], [1000, 1032], "(lane + 1) & 63: lanes 31, 63");
}

/// Operations whose result spans both 32-lane halves of a wave64 – what the split form
/// exchanges: masks with bits in either half (counted, scanned, branched on), readlane /
/// readfirstlane of a lane in the other half, ds_bpermute across the halves, a GLC scalar
/// load. The same results natively (64-lane waves) and split (32-lane waves only); split,
/// readfirstlane of a lane in the other half, a GLC scalar load – and ds_bpermute, which
/// stays in its half (#772). The same results natively (64-lane waves) and split (32-lane waves only); split,
/// the runner also checks both halves end with the same SGPRs.
#[test]
fn gpu_split_wave64_cross_half_operations() {
Expand All @@ -6596,7 +6659,7 @@ fn gpu_split_wave64_cross_half_operations() {
("S_CBRANCH_EXECZ", "ENC_SOPP", &[("SIMM16", 1)], None), // not taken: only the upper half is active
("V_MOV_B32", "ENC_VOP1", &[("VDST", 2), ("SRC0", 3)], None),
("S_MOV_B64", "ENC_SOP1", &[("SDST", 126), ("SSRC0", 193)], None),
("DS_BPERMUTE_B32", "ENC_DS", &[("VDST", 6), ("ADDR", 4), ("DATA0", 1)], None), // lane 63 - l
("DS_BPERMUTE_B32", "ENC_DS", &[("VDST", 6), ("ADDR", 4), ("DATA0", 1)], None), // index 63 - l: lane l ^ 31
("V_CMP_EQ_U32", "ENC_VOPC", &[("SRC0", 255), ("VSRC1", 0)], Some(50)), // vcc = lane 50 only
("S_CBRANCH_VCCZ", "ENC_SOPP", &[("SIMM16", 1)], None), // not taken
("S_MOV_B32", "ENC_SOP1", &[("SDST", 5), ("SSRC0", 129)], None),
Expand Down Expand Up @@ -6627,7 +6690,7 @@ fn gpu_split_wave64_cross_half_operations() {
let v = &r.vgpr[l];
assert_eq!(v[5], (l as u32).min(40), "lane {l}: v_mbcnt over vcc");
assert_eq!(v[2], if l >= 48 { 1048 } else { 0x77 }, "lane {l}: write under exec of the upper half");
assert_eq!(v[6], 1000 + 63 - l as u32, "lane {l}: ds_bpermute from lane {}", 63 - l);
assert_eq!(v[6], 1000 + (l as u32 ^ 31), "lane {l}: ds_bpermute of index {} from lane {}", 63 - l, l ^ 31);
}
}

Expand Down Expand Up @@ -6694,7 +6757,7 @@ fn split_wave64_reduction_tail_rules() {
("V_READLANE_B32", "ENC_VOP3", &[("VDST", 12), ("SRC0", V + 2), ("SRC1", 128)], None),
]);
let t = translate(&tail32, &split).unwrap();
assert_eq!(t.hlsl.matches("xcl_x_meet() ?").count(), 1 + 3, "1 folded meet + 3 in prelude:\n{}", t.hlsl);
assert_eq!(t.hlsl.matches("xcl_x_meet() ?").count(), 1 + 2, "1 folded meet + 2 in prelude:\n{}", t.hlsl);
assert_eq!(t.hlsl.matches("= xcl_x_rla_uniform(").count(), 0);

// Native wave64 is unchanged
Expand Down Expand Up @@ -7000,8 +7063,8 @@ fn gpu_split_wave64_local_and_exchanged_masks_in_diverging_waves() {
}

/// A group of four wave64s whose waves take different paths: each loops 2 · (wave + 1)
/// times through a ballot, s_bcnt1 and a readlane, then exchanges lanes across its halves
/// with ds_bpermute. Split (32-lane GPUs) this is the counter-paired form – a group barrier
/// times through a ballot, s_bcnt1 and a readlane, then exchanges lanes with ds_bpermute
/// (index lane ^ 33, which stays in its half: lane ^ 1). Split (32-lane GPUs) this is the counter-paired form – a group barrier
/// per exchange would deadlock or mix the waves up. 3-D groups too: NVIDIA does not form its
/// waves from consecutive thread indices there (8 × 8 × 4), the console does.
#[test]
Expand Down Expand Up @@ -7034,7 +7097,7 @@ fn gpu_split_wave64_waves_diverge() {
("S_CBRANCH_SCC1", "ENC_SOPP", &[("SIMM16", 0xFFF5)], None), // → loop
("V_XOR_B32", "ENC_VOP2", &[("VDST", 3), ("SRC0", 255), ("VSRC1", 2)], Some(33)),
("V_LSHLREV_B32", "ENC_VOP2", &[("VDST", 3), ("SRC0", 130), ("VSRC1", 3)], None),
("DS_BPERMUTE_B32", "ENC_DS", &[("VDST", 6), ("ADDR", 3), ("DATA0", 0)], None), // v0 of lane ^ 33
("DS_BPERMUTE_B32", "ENC_DS", &[("VDST", 6), ("ADDR", 3), ("DATA0", 0)], None), // index lane ^ 33: v0 of lane ^ 1
("V_MOV_B32", "ENC_VOP1", &[("VDST", 5), ("SRC0", 11)], None),
("S_LSHL_B32", "ENC_SOP2", &[("SDST", 16), ("SSRC0", 4), ("SSRC1", 136)], None), // group · 256
("V_ADD_NC_U32", "ENC_VOP2", &[("VDST", 7), ("SRC0", 16), ("VSRC1", 0)], None),
Expand Down Expand Up @@ -7066,7 +7129,7 @@ fn gpu_split_wave64_waves_diverge() {
let want: u32 = (0..2 * (w + 1)).map(|i| (5 * i + 3).min(64) as u32 + (w * 64 + 5 * i + 3) as u32).sum();
let at = (base + (g * 256 + tid) * 8) / 4;
assert_eq!(out[at], want, "{dims:?}, group {g} thread {tid}: loop of wave {w}");
assert_eq!(out[at + 1], (w * 64 + (l ^ 33)) as u32, "{dims:?}, group {g} thread {tid}: ds_bpermute");
assert_eq!(out[at + 1], (w * 64 + (l ^ 1)) as u32, "{dims:?}, group {g} thread {tid}: ds_bpermute");
}
}
}
Expand Down Expand Up @@ -7976,7 +8039,7 @@ fn gpu_ds_swizzle_and_bpermute_zero_inactive_lanes() {
("DS_SWIZZLE_B32", "ENC_DS", &[("VDST", 10), ("ADDR", 0), ("OFFSET0", 0x1F), ("OFFSET1", 1 << 2)], None),
// bpermute targeting odd neighbor -> must be 0
("DS_BPERMUTE_B32", "ENC_DS", &[("VDST", 11), ("ADDR", 1), ("DATA0", 0)], None),
// bpermute targeting cross-half odd lane (lane ^ 33) -> must be 0
// bpermute of index lane ^ 33: lane ^ 1 in wave64 (its half, #772), still odd -> must be 0
("DS_BPERMUTE_B32", "ENC_DS", &[("VDST", 12), ("ADDR", 2), ("DATA0", 0)], None),
// swizzle XOR 2: even lanes read active even lane -> must return its value
("DS_SWIZZLE_B32", "ENC_DS", &[("VDST", 13), ("ADDR", 0), ("OFFSET0", 0x1F), ("OFFSET1", 2 << 2)], None),
Expand Down
Loading
Loading