From 875a63a49d725eee0ec3c565f0474151365da212 Mon Sep 17 00:00:00 2001 From: local Date: Tue, 24 Nov 2026 22:35:28 -0600 Subject: [PATCH] Buffers loaded past the cache are coherent; record counts of buffers picked from the heap A raw UAV buffer the program loads with GLC is globally coherent on the host (the upscaler's waits for other groups, the ped blend shapes): a load in a loop that only waits would be made once otherwise. Ten shaders change by it. Co-Authored-By: Claude Opus 5.5 --- crates/victory-shader/src/emit/lower.rs | 67 +++++++++++++++----- crates/victory-shader/src/emit/size_tests.rs | 32 ++++++++++ 2 files changed, 83 insertions(+), 16 deletions(-) diff --git a/crates/victory-shader/src/emit/lower.rs b/crates/victory-shader/src/emit/lower.rs index b4df3c2..899255f 100644 --- a/crates/victory-shader/src/emit/lower.rs +++ b/crates/victory-shader/src/emit/lower.rs @@ -254,6 +254,11 @@ struct Lower<'a> { stale: HashSet, /// See [`ordered_section`] (pixel programs). ordered: HashSet, + /// Raw UAV buffers (see [`Lower::entry`]) the program loads from past its + /// wave's cache (GLC): other groups write what it waits for. On the host + /// they are globally coherent; a load in a loop that only waits would + /// be made once otherwise. + coherent: HashSet<(usize, u32)>, mask_phis: HashMap, pending_masks: Vec<(V, usize, usize)>, data_phis: Vec<(V, usize, usize)>, @@ -1546,6 +1551,7 @@ impl<'a> Lower<'a> { data_phis: Vec::new(), read_phis: HashSet::new(), stale: HashSet::new(), + coherent: HashSet::new(), ordered: if x.native.stage == HwStage::Ps { ordered_section(p) } else { @@ -2219,6 +2225,7 @@ impl<'a> Lower<'a> { fn run(mut self) -> Result { let p = self.x.p; + self.find_coherent(); let order = crate::ir::reverse_postorder(&p.blocks); for (n, &b) in order.iter().enumerate() { self.rpo[b] = n; @@ -2811,15 +2818,7 @@ impl<'a> Lower<'a> { /// the program has an integer atomic on the same table entry, which /// takes no other view. fn is_counter(&self, src: &Source) -> bool { - let entry = |s: &Source| match s { - Source::Loaded { load, first } => self - .scalar - .get(load) - .filter(|a| a.dynamic.is_none()) - .map(|a| (a.param, a.offset + 4 * u32::from(*first))), - _ => None, - }; - let Some(this) = entry(src) else { + let Some(this) = self.entry(src) else { return false; }; self.x.res.descriptors.iter().any(|d| { @@ -2838,10 +2837,37 @@ impl<'a> Lower<'a> { | "image_atomic_umin" | "image_atomic_umax" ) - && entry(&d.source) == Some(this) + && self.entry(&d.source) == Some(this) }) } + /// Which table entry `src` is loaded from: its root parameter and byte. + fn entry(&self, src: &Source) -> Option<(usize, u32)> { + let Source::Loaded { load, first } = src else { + return None; + }; + let a = self.scalar.get(load).filter(|a| a.dynamic.is_none())?; + Some((a.param, a.offset + 4 * u32::from(*first))) + } + + /// See [`Lower::coherent`]. + fn find_coherent(&mut self) { + let found: Vec<_> = self + .x + .res + .descriptors + .iter() + .filter(|d| { + let i = &self.x.p.blocks[d.site.0].insts[d.site.1]; + matches!(&i.fields, Fields::Buffer { cache, .. } if cache.glc) + && i.name().starts_with("buffer_load_dword") + && self.used_kind(&d.source).is_some_and(|u| u.kind == ResourceType::Uav) + }) + .filter_map(|d| self.entry(&d.source)) + .collect(); + self.coherent.extend(found); + } + /// The kind of a view no instruction uses (in this program, or through /// another load of its address), by what the code reads of the /// descriptor the load `inst` at `site` writes to the SGPRs from `code`: @@ -3281,11 +3307,6 @@ impl<'a> Lower<'a> { if self.exchange.is_some() && !self.local { need(made.unwrap_or(0)); } - if std::env::var_os("VICTORY_DESC_WHY").is_some() { - let by = made.map(|b| &self.x.p.blocks[b]); - let def = by.and_then(|b| b.insts.iter().find(|i| i.writes.iter().any(|w| w.1 == lo))); - eprintln!("MASK-WHY {v:?} made in {made:?} by {:?} at {:x?}; read in block at {:#x}; phi {}", def.map(|i| i.name()), def.map(|i| i.machine.offset), self.x.p.blocks[self.cur_ir].offset, self.phi_at.contains_key(&lo)); - } return Err(MASK_ELSEWHERE); } let (x, y) = match self.ballots.get(&lo) { @@ -6089,9 +6110,20 @@ impl<'a> Lower<'a> { // A raw view counts bytes; the descriptor's record count is in elements. let n = field(self, 0); let stride = used.map_or(0, |u| u.stride); + let counted = inst + .write(Reg::S(code + first + 2)) + .is_some_and(|v| self.read_as_number(v)); let n = if props.0 & 0xFF == 11 && stride > 1 { let k = self.s.m.i32(stride); self.s.m.bin(c, Bin::UDiv, n, k) + } else if props.0 & 0xFF == 11 + && counted + && let Some(stride) = self.slot_stride(&src)? + { + // Picked from the heap: the host's table has the stride. + let (i32t, one) = (self.s.t.i32, self.s.m.i32(1)); + let k = self.s.op(c, "binary.i32", Attr::ReadNone, i32t, 39, &[stride, one]); + self.s.m.bin(c, Bin::UDiv, n, k) } else { n }; @@ -10765,7 +10797,10 @@ impl<'a> Lower<'a> { return Ok(()); } let uav = store || atomic.is_some() || used.is_some_and(|u| u.kind == ResourceType::Uav); - let u = if uav { 0x1000 } else { 0 }; + let mut u = if uav { 0x1000 } else { 0 }; + if uav && !typed && self.entry(src).is_some_and(|e| self.coherent.contains(&e)) { + u |= 0x4000; + } let iu = self.s.m.undef(self.s.t.i32); if let Some((bits, signed)) = narrow { // Below a dword: raw buffers are read and written in dwords on diff --git a/crates/victory-shader/src/emit/size_tests.rs b/crates/victory-shader/src/emit/size_tests.rs index 5c6c40e..a0023b8 100644 --- a/crates/victory-shader/src/emit/size_tests.rs +++ b/crates/victory-shader/src/emit/size_tests.rs @@ -266,3 +266,35 @@ fn a_counter_texture_is_set_by_a_store_of_one_channel() { // A store of two channels says nothing about the view. assert!(program([0xF020_1308, 0x0002_0300], 0x7E06_0289).is_err()); } + +/// A load past the wave's cache (GLC) of a buffer other groups write: its +/// view is globally coherent on the host, as is every access to it. +#[test] +fn a_buffer_loaded_past_the_cache_is_coherent() { + let program = |load: u32| { + let words = [ + 0x7E04_028Bu32, // v_mov_b32 v2, 11 + 0xE070_0008, // buffer_store_dword v2, off, s[20:23], 0 offset:8 + 0x8005_0200, + load, // buffer_load_dword v3, off, s[20:23], 0 offset:8 + 0x8005_0300, + 0x7E08_0503, // v_readfirstlane_b32 s4, v3 + 0xBE85_0380, // s_mov_b32 s5, 0 + ]; + build(&[TABLE, &words, STORE].concat(), (8, 0)).expect("translates") + }; + let (plain, coherent) = (program(0xE030_0008), program(0xE030_C008)); + // the annotations of the store's and the load's handles + assert_ne!(plain.dxil, coherent.dxil); + let texel = [[0.0; 4]; 1]; + let views = [ + View::Texture(1, 1, &texel), + View::Texture(1, 1, &texel), + View::RawUav(3), + ]; + for shader in [&plain, &coherent] { + for (label, got) in run(shader, &views) { + assert_eq!(got[..3], [11, 0, 11], "{label}"); + } + } +}