diff --git a/crates/xcl/src/gpu/dxil/kernel.rs b/crates/xcl/src/gpu/dxil/kernel.rs index 94ce0b37d0..8eb2712d8e 100644 --- a/crates/xcl/src/gpu/dxil/kernel.rs +++ b/crates/xcl/src/gpu/dxil/kernel.rs @@ -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; @@ -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, diff --git a/crates/xcl/src/gpu/dxil/lanes.rs b/crates/xcl/src/gpu/dxil/lanes.rs index 04491ba070..34ec4ed01e 100644 --- a/crates/xcl/src/gpu/dxil/lanes.rs +++ b/crates/xcl/src/gpu/dxil/lanes.rs @@ -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); diff --git a/crates/xcl/src/gpu/dxil/lower_mem.rs b/crates/xcl/src/gpu/dxil/lower_mem.rs index a1d034e740..1108a95334 100644 --- a/crates/xcl/src/gpu/dxil/lower_mem.rs +++ b/crates/xcl/src/gpu/dxil/lower_mem.rs @@ -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); diff --git a/crates/xcl/src/gpu/recomp/emit.rs b/crates/xcl/src/gpu/recomp/emit.rs index 8f257a043f..1aa584fa9e 100644 --- a/crates/xcl/src/gpu/recomp/emit.rs +++ b/crates/xcl/src/gpu/recomp/emit.rs @@ -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 @@ -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); @@ -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); } "#; diff --git a/crates/xcl/src/gpu/recomp/tests.rs b/crates/xcl/src/gpu/recomp/tests.rs index 527dc4e648..3fc04a187c 100644 --- a/crates/xcl/src/gpu/recomp/tests.rs +++ b/crates/xcl/src/gpu/recomp/tests.rs @@ -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"); @@ -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() { @@ -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), @@ -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); } } @@ -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 @@ -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] @@ -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), @@ -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"); } } } @@ -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), diff --git a/crates/xcl/src/gpu/recomp/upper_lanes.rs b/crates/xcl/src/gpu/recomp/upper_lanes.rs index 7590f9414e..ac97647a3c 100644 --- a/crates/xcl/src/gpu/recomp/upper_lanes.rs +++ b/crates/xcl/src/gpu/recomp/upper_lanes.rs @@ -7,9 +7,10 @@ //! lanes 32..63 are not populated, as in the last wave of a draw: inactive, but on the console //! their VGPRs exist and can still be written – by `v_writelane`, which ignores EXEC, and by //! whole-wave code (EXEC's upper word set, `s_or_saveexec_b64 s, -1`). Here those lanes do -//! not exist. A read of one – `v_readlane` of a lane ≥ 32 or of a lane from an SGPR, -//! `ds_bpermute` – needs the value the console lane would hold. This forward analysis follows -//! it per VGPR lane: +//! not exist. A read of one – `v_readlane` of a lane ≥ 32 or of a lane from an SGPR, or +//! `ds_permute` – needs the value the console lane would hold (`ds_bpermute` stays in the +//! reader's 32-lane half on RDNA2, #772, so lanes < 32 never reach it). This forward analysis +//! follows it per VGPR lane: //! - *unwritten*: nothing wrote the lane, it holds what an earlier wave left in the register. //! A program cannot depend on that, so any value is as right as the console's; //! - a *constant*: whole-wave code wrote it – the identity a wave reduction puts into the @@ -219,8 +220,7 @@ impl State { /// How the 32-lane form resolves a lane read (`UpperLanes::reads`). #[derive(Clone, Copy, PartialEq, Eq, Debug)] pub enum Read { - /// the read as it is: the lanes ≥ 32 it can reach are unwritten (or, for `ds_bpermute`, - /// inactive, which reads 0 here as on the console) + /// the read as it is: the lanes ≥ 32 it can reach are unwritten Native, /// a lane ≥ 32 holds this constant Const(u32), @@ -248,7 +248,7 @@ impl UpperLanes { out.insert(match (d.inst.name, lane_operand(d)) { ("V_READLANE_B32", Lane::Const(_)) => "v_readlane: lane ≥ 32", ("V_READLANE_B32", _) => "v_readlane: lane number from an SGPR", - _ => "ds_(b)permute: arbitrary lane", + _ => "ds_permute: arbitrary lane", }); } } @@ -260,7 +260,7 @@ impl UpperLanes { self.reads .values() .filter_map(|r| match r { - Read::Native => Some("lanes ≥ 32 never written (or inactive)"), + Read::Native => Some("lanes ≥ 32 never written"), Read::Const(_) => Some("lane ≥ 32 holds a constant (whole-wave identity)"), Read::Shadow => Some("spill slot from its v_writelane shadow"), Read::Unknown => None, @@ -273,7 +273,7 @@ impl UpperLanes { fn reaches_upper(d: &Decoded) -> bool { match d.inst.name { "V_READLANE_B32" => !matches!(lane_operand(d), Lane::Const(l) if l < 32), - "DS_BPERMUTE_B32" | "DS_PERMUTE_B32" => true, + "DS_PERMUTE_B32" => true, _ => false, } } @@ -703,10 +703,6 @@ fn classify(d: &Decoded, st: &State) -> Option<(Read, Option<(u64, u32)>)> { Lane::Sgpr => Some((any_upper(v), None)), } } - "DS_BPERMUTE_B32" => { - // source lanes ≥ 32 inactive: 0, as `Em::ds` gives for them - Some((if st.s[127] == Word::Zero { Read::Native } else { any_upper(fv("DATA0")) }, None)) - } "DS_PERMUTE_B32" => Some((Read::Unknown, None)), _ => None, } @@ -892,9 +888,8 @@ mod tests { ("S_AND_B64", "ENC_SOP2", &[("SDST", 0), ("SSRC0", 72), ("SSRC1", 126)], None), ("S_FF1_I32_B64", "ENC_SOP1", &[("SDST", 0), ("SSRC0", 0)], None), ("V_READLANE_B32", "ENC_VOP3", &[("VDST", 4), ("SRC0", V + 51), ("SRC1", 0)], None), - ("DS_BPERMUTE_B32", "ENC_DS", &[("VDST", 1), ("ADDR", 2), ("DATA0", 51)], None), ]); - assert_eq!(r, vec![Read::Native, Read::Native]); + assert_eq!(r, vec![Read::Native]); // after s_mov_b64 exec, -1 (no saved copy) the same move writes the upper lanes let r = reads(&[ ("S_MOV_B64", "ENC_SOP1", &[("SDST", 126), ("SSRC0", 193)], None), @@ -902,7 +897,8 @@ mod tests { ("V_READLANE_B32", "ENC_VOP3", &[("VDST", 4), ("SRC0", V + 51), ("SRC1", 0)], None), ("DS_BPERMUTE_B32", "ENC_DS", &[("VDST", 1), ("ADDR", 2), ("DATA0", 51)], None), ]); - assert_eq!(r, vec![Read::Unknown, Read::Unknown]); + // ds_bpermute stays in the reader's half (#772): lanes < 32 never read lanes ≥ 32 + assert_eq!(r, vec![Read::Unknown]); } #[test] diff --git a/docs/wave64-on-wave32.md b/docs/wave64-on-wave32.md index c11d203537..171ca979d3 100644 --- a/docs/wave64-on-wave32.md +++ b/docs/wave64-on-wave32.md @@ -40,8 +40,7 @@ whole-wave: |---|---| | `ballot()` (every mask: `v_cmp*`, `v_cmpx*`, carries, EXEC at entry) | one word: each half gives its 32-bit ballot, both get the 64-bit mask – unless the mask is only read lane by lane (below) | | `v_readlane`, `v_readfirstlane` (lane index uniform) | one word: each half gives the value of lane `l & 31`, the half that owns `l` wins | -| `ds_bpermute` (any lane) | 32 words: each half gives its lanes | -| DPP16, DPP8, `ds_swizzle`, `v_permlane(x)16` | none: the ISA keeps them inside a 32-lane half (`WaveReadLaneAt(v, l & 31)`) | +| DPP16, DPP8, `ds_swizzle`, `ds_bpermute`, `v_permlane(x)16` | none: they stay inside a 32-lane half (`WaveReadLaneAt(v, l & 31)`); for `ds_bpermute`, lane l reads `(l & 32) \| (index & 31)`, measured on RDNA2 in native wave64 (#772) | | `s_load*` / `s_buffer_load*` with GLC (or DLC) | up to 16 words: half 0's values for both (memory another wave writes may change between the two halves' loads; without GLC the console's scalar cache is not coherent either, so plain loads are read by both halves) | So both halves hold the full 64-bit EXEC / VCC / masks and identical SGPRs; every branch is @@ -51,14 +50,21 @@ SGPRs of both halves and the tests fail if they ever differ. ### Synchronisation -* **One console wave per group** (≤ 64 threads: kernel mode, most compute, ES+GS): - `GroupMemoryBarrierWithGroupSync` between writing and reading the slot. All hardware - waves of the group are the two halves of the same console wave, which execute the same - sequence of exchanges, so every barrier is reached by both. Portable (no forward-progress - assumption) and cheap. -* **Several console waves per group** (mesh: 128 threads, compute > 64 threads): a group - barrier would also wait for the *other* console waves, whose control flow may differ (wave - index, per-wave data). Each pair instead meets through groupshared arrival counters: +The HLSL translation pairs the halves through arrival counters (below) in every group by +default since #798: a group barrier that the two halves ever reach in different places +hangs the SM until a TDR, the bounded wait does not. `XCL_WAVE64_SPIN=0` brings back the +barrier for groups of one console wave. The direct DXIL backend (`XCL_DIRECT_DXIL=1`) still +uses the barrier there (`Partner::Present` in `dxil/kernel.rs`) and ignores the variable. + +* **One console wave per group** (≤ 64 threads: kernel mode, most compute, ES+GS), with + the barrier: `GroupMemoryBarrierWithGroupSync` between writing and reading the slot. All + hardware waves of the group are the two halves of the same console wave, which execute the + same sequence of exchanges, so every barrier is reached by both. Portable (no + forward-progress assumption) and cheap. +* **Several console waves per group** (mesh: 128 threads, compute > 64 threads), and by + default every group: a group barrier would also wait for the *other* console waves, whose + control flow may differ (wave index, per-wave data). Each pair instead meets through + groupshared arrival counters: every lane stores its slot word(s) and fences (`GroupMemoryBarrier`); the wave's lane count (`WaveActiveCountBits` – a wave operation, so all its lanes have fenced) goes into its half's counter in one atomic; one lane waits until the partner's counter reaches @@ -69,8 +75,7 @@ SGPRs of both halves and the tests fail if they ever differ. pair – should not happen, see hazards), the wave stops exchanging (`xcl_x_dead`) and finishes with half-local values instead of hanging the GPU until a TDR. This relies on co-resident waves of one group making progress, which holds on GPUs (all waves of a group - are resident on one SM / CU) but not necessarily on WARP. `XCL_WAVE64_SPIN=1` forces this - variant everywhere (the test suite passes with it too). + are resident on one SM / CU) but not necessarily on WARP. A console wave whose upper half does not exist (group of 32 or 96 threads) exchanges with nobody: the missing lanes count as inactive (ballot bits 0), exactly as on the console. The @@ -79,7 +84,7 @@ GS phase of ES+GS runs in the lower half only (≤ 21 primitives), likewise with ### Memory The exchange needs groupshared space: per console wave 2 counters + 2 halves × 2 parities × -slot (1 word, 16 with wide GLC scalar loads, 32 with `ds_bpermute`). Without console LDS it +slot (1 word, 16 with wide GLC scalar loads). Without console LDS it has arrays of its own; with LDS (`xcl_lds`, already the full 32 KB D3D12 limit) it lives in the top of the LDS block (as PR #33 did), and the program's LDS accesses are clamped below it. A program that really uses the whole 32 KB of LDS therefore loses its top dwords on a @@ -99,7 +104,8 @@ it. A program that really uses the whole 32 KB of LDS therefore loses its top dw of non-consecutive thread indices for 3-D groups (`gpu_split_wave64_waves_diverge` fails without the remap). 4. **`WaveReadLaneAt` with a per-lane index** – used inside a half as before. -5. **Forward progress** – only the multi-wave variant spins; see above. +5. **Forward progress** – the counter variant spins (by default every group in HLSL, groups + of several console waves in direct DXIL); see above. ### Masks read lane by lane @@ -139,7 +145,7 @@ destination, 0 in every lane), which are the same in both halves either way. A wave64 program pays one exchange per mask write that is read whole (`v_cmp*` feeding `s_and_saveexec`, `s_cbranch_vccz`, `s_bcnt1`, …) and per readlane / readfirstlane / -ds_bpermute / coherent scalar load, nothing per ALU, memory or DPP instruction, nor for +coherent scalar load, nothing per ALU, memory or DPP instruction, nor for masks read lane by lane (selects, 64-bit carry chains). Wave32 programs and everything on GPUs with 64-lane waves are unchanged (the HLSL of every native translation the test suite makes is byte-identical). A wave64 VS / PS pays nothing for a lane ≥ 32 read as a constant (the lane read is skipped) and @@ -179,7 +185,7 @@ the split form (64-bit masks, the threads' start), not measured further. The ben loop mask is exactly the kind the pass keeps local – it shows the exchange gone and what one costs, not how often real shaders gain. -What is left is readlane / readfirstlane, `ds_bpermute`, coherent scalar loads and masks +What is left is readlane / readfirstlane, coherent scalar loads and masks read whole. Not measured on real compute shaders (none on that machine); Minecraft's 273 wave64 programs, translated as compute, have 38 mask writes, all only read lane by lane. @@ -190,8 +196,8 @@ GPU with 32-lane waves only a console wave64 VS / PS runs in waves of 32. Nothin them (no groupshared memory), so such a wave stands for a console wave whose lanes 32..63 are not populated, as in the last, partial wave of a draw. Per-lane code is exact as it is, and so are masks (their upper word is 0: those lanes are inactive), DPP / `ds_swizzle` / -`v_permlane(x)16` (inside a 32-lane half) and `v_readfirstlane`. What is not: a read of a lane -≥ 32 – `v_readlane` with a constant lane ≥ 32 or a lane from an SGPR, `ds_bpermute` – and +`ds_bpermute` / `v_permlane(x)16` (inside a 32-lane half) and `v_readfirstlane`. What is not: +a read of a lane ≥ 32 – `v_readlane` with a constant lane ≥ 32 or a lane from an SGPR – and `v_writelane` to one. On the console those lanes exist, inactive, and hold what `v_writelane` (which ignores EXEC) or whole-wave code (EXEC's upper word set, `s_or_saveexec_b64 s, -1`) put there; here they do not exist, and `WaveReadLaneAt(v, l)` with l ≥ 32 is undefined (NVIDIA