From 7c2fb08eff26aad1398d073e44b1fdf7e81006eb Mon Sep 17 00:00:00 2001 From: earpjonathan <247878535+earpjonathan@users.noreply.github.com> Date: Fri, 27 Nov 2026 19:11:42 +0000 Subject: [PATCH 1/2] fix(shader): two descriptor refusals in compute programs A buffer descriptor loaded only for its record count (dword 2), with its other dwords never read and the buffer never accessed, was refused as "a descriptor read only as data": the view addresses hold only a buffer's address. The record count now comes from the host view's dimensions, the kind from the used-resource entry of the table slot (structured: elements, raw: bytes), as for a buffer read with scalar loads (`lower/descriptor_records.rs`). A per-lane descriptor moved into VGPRs under an EXEC that a waterfall loop then starts with was refused as "a per-lane descriptor written where some lanes may keep the old one": the loop header's lane conditions forget the EXEC before it. The tracer now first checks, on SSA values, that the access's EXEC holds no lane the move's EXEC did not (phis of such, and lane-mask operations that keep or take out lanes), before asking the lane conditions. Tests: a waterfall over a descriptor moved after `v_cmpx` narrowed EXEC (tracing, and sampling on the host GPU), and a program storing a structured buffer's record count (on the host GPU). Co-Authored-By: Claude Opus 5.5 --- crates/victory-shader/src/emit/lower.rs | 6 +- .../src/emit/lower/descriptor_records.rs | 101 +++++++++++++ .../src/emit/waterfall_tests.rs | 70 +++++++++ crates/victory-shader/src/resource.rs | 138 +++++++++++++++++- 4 files changed, 313 insertions(+), 2 deletions(-) create mode 100644 crates/victory-shader/src/emit/lower/descriptor_records.rs diff --git a/crates/victory-shader/src/emit/lower.rs b/crates/victory-shader/src/emit/lower.rs index 2a4a6713b..33f2c24e7 100644 --- a/crates/victory-shader/src/emit/lower.rs +++ b/crates/victory-shader/src/emit/lower.rs @@ -23,6 +23,7 @@ use crate::structure::{Atom, Bool, LaneExit, Map, Set, Structured}; mod atomics; mod barriers; mod bvh; +mod descriptor_records; mod f64; mod first_lane; /// Format loads that move 16-bit halves without the d16 form, a child @@ -4931,7 +4932,10 @@ impl<'a> Lower<'a> { .find_map(|&(s, _)| use_of(s)) }); let Some((at, first)) = use_at else { - return self.view_addresses(inst, site, code, count); + return match self.unused_buffer(site) { + Some(props) => self.buffer_records(inst, site, code, count, props), + None => self.view_addresses(inst, site, code, count), + }; }; let src = Source::Loaded { load: site, first }; let used = self.used_kind(&src); diff --git a/crates/victory-shader/src/emit/lower/descriptor_records.rs b/crates/victory-shader/src/emit/lower/descriptor_records.rs new file mode 100644 index 000000000..bb6431827 --- /dev/null +++ b/crates/victory-shader/src/emit/lower/descriptor_records.rs @@ -0,0 +1,101 @@ +//! A buffer descriptor the program loads only to read its record count +//! (dword 2): no instruction of the program accesses the buffer, so its +//! kind comes from the used-resource entry of the table slot it is loaded +//! from. + +use super::*; + +impl Lower<'_> { + /// The handle properties of the buffer descriptor that `site` loads + /// whole from a table slot, when the program never accesses it: by its + /// used-resource entry's stride, structured (the record count in + /// elements) or raw (in bytes), as for a buffer read with scalar loads + /// ([`Lower::descriptor_data`]). None for anything else: a load that + /// does not start at a buffer descriptor of the entry, a constant + /// buffer, or an odd stride. + pub(super) fn unused_buffer(&self, site: Site) -> Option<(u32, u32)> { + // Only the record count: a program that also reads the address + // takes it from the view addresses ([`Lower::view_addresses`]). + let inst = &self.x.p.blocks[site.0].insts[site.1]; + let read = |v: Value| { + self.x.p.blocks.iter().any(|b| { + b.phis.iter().any(|f| f.args.contains(&v)) + || b.insts.iter().any(|i| i.reads.iter().any(|r| r.1 == v)) + }) + }; + let Fields::Smem { + data: Some(Operand::Scalar { code, .. }), + .. + } = inst.fields + else { + return None; + }; + let others = inst + .writes + .iter() + .filter(|w| w.0 != Reg::S(code + 2)) + .any(|w| read(w.1)); + if others { + return None; + } + let a = self.scalar.get(&site)?; + let used = self.used_kind(&Source::Loaded { + load: site, + first: 0, + })?; + let dword = a.offset / 4; + if !a.offset.is_multiple_of(4) + || used.dwords != 4 + || !(dword - used.table_offset_dwords).is_multiple_of(4) + { + return None; + } + let u = match used.kind { + ResourceType::Srv => 0, + ResourceType::Uav => 0x1000, + _ => return None, + }; + match used.stride { + 0 | 1 => Some((11 | u, 0)), + s if s.is_multiple_of(4) => Some((12 | u, s)), + _ => None, + } + } + + /// Dword 2 of the buffer descriptor `site` loads ([`Lower::unused_buffer`], + /// `props`): the record count the host view gives; the other dwords + /// stop the program when read, as in [`Lower::descriptor_data`]. + pub(super) fn buffer_records( + &mut self, + inst: &Inst, + site: Site, + code: u8, + count: u32, + props: (u32, u32), + ) -> Result<(), Why> { + let src = Source::Loaded { + load: site, + first: 0, + }; + let h = self.handle(site, &src, false, props)?; + let c = self.cur; + let i32t = self.s.t.i32; + let dims = self.s.m.named_struct("dx.types.Dimensions", vec![i32t; 4]); + let mip = self.s.m.undef(i32t); + let d = self + .s + .op(c, "getDimensions", Attr::ReadOnly, dims, 72, &[h, mip]); + let records = self.s.m.extract(c, d, 0); + for k in 0..count { + if k == 2 { + self.write(inst, Reg::S(code + 2), records, None); + } else if let Some(v) = inst.write(Reg::S(code + k as u8)) { + self.vals.insert( + v, + Val::Missing("a descriptor's address or format read as data"), + ); + } + } + Ok(()) + } +} diff --git a/crates/victory-shader/src/emit/waterfall_tests.rs b/crates/victory-shader/src/emit/waterfall_tests.rs index 98957455f..6233aba66 100644 --- a/crates/victory-shader/src/emit/waterfall_tests.rs +++ b/crates/victory-shader/src/emit/waterfall_tests.rs @@ -269,3 +269,73 @@ fn lanes_sample_their_own_texture_in_a_waterfall_narrowed_by_v_cmpx() { } } } + +#[test] +fn lanes_sample_a_descriptor_moved_under_the_exec_their_waterfall_starts_with() { + let words = crate::resource::tests::waterfall_narrowed_wave32(); + let shader = build(&words, false); + for (label, red) in sampled(&shader) { + // Lanes below 16 moved the first texture's descriptor and sample + // it; the others keep v12's 0. + let want: Vec = (0..32).map(|l| if l < 16 { 1.0 } else { 0.0 }).collect(); + assert_eq!(red, want, "{label}"); + } +} + +/// A buffer descriptor (table entry 0, a structured buffer of dwords) loaded +/// only for its record count, dword 2, which every lane stores at lane * 4 +/// of the buffer at dword 16: no instruction accesses the buffer. +const RECORDS_ONLY_WAVE32: &[u32] = &[ + 0xBE84_0300, // s_mov_b32 s4, s0 + 0xBE85_0380, // s_mov_b32 s5, 0 + 0xF408_0602, + 0xFA00_0000, // s_load_dwordx4 s[24:27], s[4:5], 0x0 + 0xF408_0A02, + 0xFA00_0040, // s_load_dwordx4 s[40:43], s[4:5], 0x40 + 0x3420_0082, // v_lshlrev_b32 v16, 2, v0 + 0xBF8C_C07F, // s_waitcnt lgkmcnt(0) + 0x7E02_021A, // v_mov_b32 v1, s26 + 0xE070_1000, + 0x800A_0110, // buffer_store_dword v1, v16, s[40:43], 0 offen + 0xBF81_0000, // s_endpgm +]; + +#[test] +fn a_buffer_descriptor_read_only_for_its_record_count_gives_the_views_elements() { + let code: Vec = RECORDS_ONLY_WAVE32 + .iter() + .flat_map(|w| w.to_le_bytes()) + .collect(); + let all = crate::gfx10::decode(&code) + .expect("decodes") + .iter() + .map(|i| i.text(false)) + .collect::>() + .join("\n"); + for want in [ + "s_load_dwordx4 s[24:27], s[4:5], 0x000000", + "s_load_dwordx4 s[40:43], s[4:5], 0x000040", + "v_mov_b32 v1, s26", + "buffer_store_dword v1, v16, s[40:43], 0 offen", + ] { + assert!(all.contains(want), "{want:?} not in:\n{all}"); + } + let shader = build(RECORDS_ONLY_WAVE32, true); + let first: Vec = (0..24).collect(); + let second = [0u32; 4]; + let views = [ + View::Buffer { + data: &first, + stride: Stride::Structured(4), + }, + View::Buffer { + data: &second, + stride: Stride::Structured(4), + }, + View::RawUav(32), + ]; + for (label, got) in run(&shader, &views) { + // The console's record count of a structured buffer: its elements. + assert_eq!(got, vec![24; 32], "{label}"); + } +} diff --git a/crates/victory-shader/src/resource.rs b/crates/victory-shader/src/resource.rs index 0154483d8..f7ac06711 100644 --- a/crates/victory-shader/src/resource.rs +++ b/crates/victory-shader/src/resource.rs @@ -341,6 +341,8 @@ struct Tracer<'a> { spill_at: std::collections::HashMap>, /// Address sums already traced, indexed by SSA value. sums: std::cell::RefCell>>, + /// The access whose descriptor is being traced ([`Tracer::runs`]). + access: std::cell::Cell>, } /// What lanes are known to satisfy: each [`Bool`] where its flag is set, @@ -458,6 +460,7 @@ impl<'a> Tracer<'a> { first_spilled: Default::default(), spill_at: Default::default(), sums: Default::default(), + access: Default::default(), }; t.origin = t.propagate_copies(n)?; // Loads from the root signature's memory are found by their address, @@ -1124,7 +1127,10 @@ impl<'a> Tracer<'a> { known.push((guard, true)); } } - self.classify(&words, &mut Vec::new(), &mut known) + self.access.set(Some(at)); + let source = self.classify(&words, &mut Vec::new(), &mut known); + self.access.set(None); + source } /// The source of a descriptor's words; `joins` are the phi blocks @@ -1689,6 +1695,9 @@ impl<'a> Tracer<'a> { let Some(&Some(guard)) = lanes.guards.get(site.0).and_then(|g| g.get(site.1)) else { return Ok(()); }; + if self.access.get().is_some_and(|at| self.ran_under(at, site)) { + return Ok(()); + } let mut skips = known.clone(); skips.extend([(lanes.reach[site.0], true), (guard, false)]); if meet(lanes, &skips) { @@ -1697,6 +1706,86 @@ impl<'a> Tracer<'a> { Ok(()) } + /// Whether every lane that runs the access at `at` ran the copy at + /// `copy` (whose value the access reads, so the copy's block ran on the + /// way): the access's EXEC holds no lane the copy's EXEC did not + /// ([`Tracer::exec_within`]). A waterfall loop's EXEC starts as the + /// EXEC before it and each pass only takes lanes out, which the lane + /// conditions cannot tell where the loop's header forgets them. + fn ran_under(&self, at: Site, copy: Site) -> bool { + let (access, copy) = (self.inst(at), self.inst(copy)); + let halves: &[u8] = if self.p.wave64 { &[0, 1] } else { &[0] }; + halves.iter().all( + |&h| match (access.read(Reg::S(EXEC + h)), copy.read(Reg::S(EXEC + h))) { + (Some(e), Some(x)) => self.exec_within(e, x, h, &mut Vec::new()), + _ => false, + }, + ) + } + + /// Whether lane mask half `v` (low dword 0, high 1: `half`) holds no lane + /// `x` does not: `x` itself, a phi of such, or a lane-mask operation that + /// keeps or takes out lanes of such (`and` of one, `andn2` or + /// `andn2_wrexec` of its first, `or` and `xor` of both, `v_cmpx` of the + /// EXEC it reads, the old EXEC an `_saveexec` saves). Going round a loop + /// back to a value being checked holds: lanes enter it only from the + /// rest. + fn exec_within(&self, v: Value, x: Value, half: u8, seen: &mut Vec) -> bool { + if v == x || seen.contains(&v) { + return true; + } + seen.push(v); + if let Some((b, i)) = self.phis[v.0 as usize] { + let args = self.p.blocks[b].phis[i].args.clone(); + return args.iter().all(|&a| self.exec_within(a, x, half, seen)); + } + let Some(inst) = self.def(v) else { + return false; + }; + let exec_in = inst.read(Reg::S(EXEC + half)); + let within = |l: &Self, w: Option, seen: &mut Vec| { + w.is_some_and(|w| l.exec_within(w, x, half, seen)) + }; + let name = inst.name(); + if name.starts_with("v_cmpx_") { + return inst.write(Reg::S(EXEC + half)) == Some(v) && within(self, exec_in, seen); + } + let Fields::Sop { src, .. } = &inst.fields else { + return false; + }; + let width = if self.p.wave64 { "_b64" } else { "_b32" }; + let Some(op) = name.strip_prefix("s_").and_then(|n| n.strip_suffix(width)) else { + return false; + }; + // A source operand's half, or an inline 0 (no lanes). + let operand = |k: usize| -> Option> { + match *src.get(k)? { + Operand::Scalar { code, .. } if code != NULL => { + Some(Some(inst.read(Reg::S(code + half))?)) + } + Operand::Const(128) => Some(None), + _ => None, + } + }; + let arg = |l: &Self, k: usize, seen: &mut Vec| match operand(k) { + Some(None) => true, + Some(w) => within(l, w, seen), + None => false, + }; + let to_exec = inst.write(Reg::S(EXEC + half)) == Some(v); + match op { + "mov" => arg(self, 0, seen), + "and" => arg(self, 0, seen) || arg(self, 1, seen), + "andn2" => arg(self, 0, seen), + "or" | "xor" => arg(self, 0, seen) && arg(self, 1, seen), + "and_saveexec" if to_exec => arg(self, 0, seen) || within(self, exec_in, seen), + "andn2_saveexec" if to_exec => arg(self, 0, seen), + "andn2_wrexec" => arg(self, 0, seen), + _ if op.ends_with("_saveexec") && !to_exec => within(self, exec_in, seen), + _ => false, + } + } + /// The source of descriptor dwords each lane holds its own of (a /// waterfall loop reads them from the first active lane): the same in /// every lane, loaded per lane from memory a heap address points to, or @@ -3191,6 +3280,37 @@ pub(crate) mod tests { 0xBF81_0000, // s_endpgm ]; + /// The waterfall loop of [`WATERFALL_JOINED_WAVE32`] over one image + /// descriptor that is the same in every lane, moved into v[4:11] after + /// `v_cmpx_gt_u32` narrowed EXEC to the lanes below 16: the lanes above + /// keep their old VGPRs, but the loop starts with that EXEC and each + /// pass only takes lanes out, so no lane that samples skipped the move. + /// v12 starts at 0; the sample goes to the buffer at dword 16 of the + /// table, at lane * 4, in every lane. + pub(crate) fn waterfall_narrowed_wave32() -> Vec { + let joined = WATERFALL_JOINED_WAVE32; + // Up to and including s_waitcnt lgkmcnt(0). + let wait = joined.iter().position(|&w| w == 0xBF8C_C07F).unwrap(); + // From the loop's first v_readfirstlane_b32 on. + let read = joined.iter().position(|&w| w == 0x7E18_0504).unwrap(); + let mut w: Vec = joined[..=wait].to_vec(); + w.extend([ + 0x7E18_0280, // v_mov_b32 v12, 0 + 0xBE97_037E, // s_mov_b32 s23, exec_lo + 0x7DA8_0090, // v_cmpx_gt_u32 16, v0 + 0x7E08_0218, // v_mov_b32 v4, s24 + 0x7E0A_0219, // v_mov_b32 v5, s25 + 0x7E0C_021A, // v_mov_b32 v6, s26 + 0x7E0E_021B, // v_mov_b32 v7, s27 + 0x7E10_021C, // v_mov_b32 v8, s28 + 0x7E12_021D, // v_mov_b32 v9, s29 + 0x7E14_021E, // v_mov_b32 v10, s30 + 0x7E16_021F, // v_mov_b32 v11, s31 + ]); + w.extend(&joined[read..]); + w + } + /// The console compiler's waterfall (Grand Theft Auto VI's ray-tracing /// resolve and others): each lane holds its entry's byte offset in the /// table at s0 (v7 = (lane & 1) * 32); the loop reads the first active @@ -3913,6 +4033,22 @@ pub(crate) mod tests { ); } + #[test] + fn a_per_lane_descriptor_moved_under_the_exec_its_waterfall_starts_with_is_traced() { + let words = waterfall_narrowed_wave32(); + let r = waterfall(&words, false).unwrap(); + let image = r + .descriptors + .iter() + .find(|d| d.role == Role::Resource) + .unwrap(); + let Source::Loaded { load, first: 0 } = image.source else { + panic!("{:?}", image.source) + }; + let a = r.scalar.iter().find(|s| s.0 == load).unwrap().1; + assert_eq!((a.param, a.offset), (0, 0)); + } + #[test] fn a_per_lane_descriptor_some_lanes_do_not_write_fails() { // The else arm of WATERFALL_JOINED_WAVE32 not skipped (its From daa2b6be697e8865306e82afeb73feff1a077646 Mon Sep 17 00:00:00 2001 From: earpjonathan <247878535+earpjonathan@users.noreply.github.com> Date: Fri, 27 Nov 2026 19:19:32 +0000 Subject: [PATCH 2/2] victory-shader: fix the two new waterfall GPU tests Both programs' descriptors are the same in every lane, so their single access is uniform: build_with takes the expected count of nonuniform accesses (0 here, 1 for the other waterfalls). The decoder prints a zero SMEM offset with a null soffset as "null". Co-Authored-By: Claude Opus 5.5 --- .../src/emit/waterfall_tests.rs | 23 +++++++++++++------ 1 file changed, 16 insertions(+), 7 deletions(-) diff --git a/crates/victory-shader/src/emit/waterfall_tests.rs b/crates/victory-shader/src/emit/waterfall_tests.rs index 6233aba66..18193be84 100644 --- a/crates/victory-shader/src/emit/waterfall_tests.rs +++ b/crates/victory-shader/src/emit/waterfall_tests.rs @@ -14,8 +14,15 @@ use crate::{io, ir, resource, structure}; /// Lowers a wave32 compute program of 32 threads with the table of views at /// s0 (textures, or with `buffers` structured buffers of dwords, at entries -/// 0 and 1, a raw UAV at entry 2) and the table of samplers at s1. +/// 0 and 1, a raw UAV at entry 2) and the table of samplers at s1, with one +/// access by each lane's own heap index. fn build(words: &[u32], buffers: bool) -> HostShader { + build_with(words, buffers, 1) +} + +/// `build` for a program with `nonuniform` accesses by each lane's own heap +/// index: 0 where every lane's descriptor is the same. +fn build_with(words: &[u32], buffers: bool, nonuniform: u32) -> HostShader { let table = |user_data: u8| RootParam { root: RootType::Table, table_offset_dwords: 0, @@ -80,8 +87,8 @@ fn build(words: &[u32], buffers: bool) -> HostShader { view_marks: false, }) .expect("lower"); - // One access with each lane's own heap index, no wave operation. - assert_eq!(lowered.nonuniform_handles, 1); + // No wave operation. + assert_eq!(lowered.nonuniform_handles, nonuniform); assert!(!lowered.wave_ops); let shader = HostShader { stage: HwStage::Cs, @@ -273,7 +280,9 @@ fn lanes_sample_their_own_texture_in_a_waterfall_narrowed_by_v_cmpx() { #[test] fn lanes_sample_a_descriptor_moved_under_the_exec_their_waterfall_starts_with() { let words = crate::resource::tests::waterfall_narrowed_wave32(); - let shader = build(&words, false); + // Every lane's descriptor is the first texture's, so the waterfall is + // one uniform access. + let shader = build_with(&words, false, 0); for (label, red) in sampled(&shader) { // Lanes below 16 moved the first texture's descriptor and sample // it; the others keep v12's 0. @@ -289,7 +298,7 @@ const RECORDS_ONLY_WAVE32: &[u32] = &[ 0xBE84_0300, // s_mov_b32 s4, s0 0xBE85_0380, // s_mov_b32 s5, 0 0xF408_0602, - 0xFA00_0000, // s_load_dwordx4 s[24:27], s[4:5], 0x0 + 0xFA00_0000, // s_load_dwordx4 s[24:27], s[4:5], null 0xF408_0A02, 0xFA00_0040, // s_load_dwordx4 s[40:43], s[4:5], 0x40 0x3420_0082, // v_lshlrev_b32 v16, 2, v0 @@ -313,14 +322,14 @@ fn a_buffer_descriptor_read_only_for_its_record_count_gives_the_views_elements() .collect::>() .join("\n"); for want in [ - "s_load_dwordx4 s[24:27], s[4:5], 0x000000", + "s_load_dwordx4 s[24:27], s[4:5], null", "s_load_dwordx4 s[40:43], s[4:5], 0x000040", "v_mov_b32 v1, s26", "buffer_store_dword v1, v16, s[40:43], 0 offen", ] { assert!(all.contains(want), "{want:?} not in:\n{all}"); } - let shader = build(RECORDS_ONLY_WAVE32, true); + let shader = build_with(RECORDS_ONLY_WAVE32, true, 0); let first: Vec = (0..24).collect(); let second = [0u32; 4]; let views = [