From 9a64b31985f996f011e9f5103601de8322b2ba0e Mon Sep 17 00:00:00 2001 From: fromrivertothesea <345018979+fromrivertothesea@users.noreply.github.com> Date: Fri, 27 Nov 2026 22:48:40 +0100 Subject: [PATCH 1/2] Shaders: CS_CollectTriangles(_Meshlets) on 32-lane hosts (local waves with LDS atomics, no room past a full LDS) vfx_triangleClip:CS_CollectTriangles (#6832a9da15c2e584) and _Meshlets (#6b82bc7d22016cb5), groups of 1024 using all 32 KiB of LDS, stopped with "no wave32 lowering for this wave64 program" on 32-lane hosts, and their dispatches were dropped. Wave-wide branches cannot take them: their meetings need 177 dwords past the program's 8192, which a D3D12 group never has. Their LDS is only shared across the title's own barriers; between them it holds atomics, reads of what the last barrier fixed, and one region thread 0 runs alone. Their wave operations are the allocation idiom, with the reservation on an LDS counter (ds_add_rtn_u32) instead of a buffer. So each host wave can run as a wave of its own: - wave_local takes an LDS atomic of the total's operation (ds_add/min/max/or/ and/xor, with or without _rtn) as a reservation, as it takes buffer atomics. - Local waves compute LdsOrder too, but only for a program whose LDS accesses cannot be ordered without it; every other program keeps its barriers. - tight_lds: with local waves, a one-dimensional group whose own LDS leaves no room makes neither the exchange dwords (local waves never exchange) nor wave_base's counter (the flat id's 32-aligned runs are the host waves). All 26177 programs of GTA VI 1.0.2.1 translated before and after (32-lane host): only these two differ, both from refused to emitted; every other program's DXIL is the same. victory-shader tests: 288 passed. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_01FsQtBz4aDhUTip3jBagKEX --- crates/victory-shader/src/emit/lower.rs | 80 +++++++++++++++++++++++-- 1 file changed, 74 insertions(+), 6 deletions(-) diff --git a/crates/victory-shader/src/emit/lower.rs b/crates/victory-shader/src/emit/lower.rs index 3aa6a72c50..e568d72f70 100644 --- a/crates/victory-shader/src/emit/lower.rs +++ b/crates/victory-shader/src/emit/lower.rs @@ -649,6 +649,37 @@ fn lane_uniform( same } +/// With each host wave a wave of its own ([`with_local_waves`]), a program whose own LDS +/// leaves no room for the translation's dwords (`CS_CollectTriangles_Meshlets`: all 32 KiB): +/// its host waves exchange nothing, and in a one-dimensional group each host wave is a +/// 32-aligned run of flat ids on the hosts of 32-lane waves, so neither the exchange dwords +/// nor `wave_base`'s counter is made. (A two- or three-dimensional group keeps them: a host +/// may give its waves other runs, #1243.) +fn tight_lds(cs: &crate::native::ComputeIo, own: u32) -> bool { + LOCAL_WAVES.get() + && cs.threads[1] == 1 + && cs.threads[2] == 1 + && own + 2 * cs.threads[0].div_ceil(64) + 1 > HOST_LDS_DWORDS +} + +/// The reduction an LDS atomic applies to its counter (with or without the old value +/// returned), for [`wave_local`]'s reservations: `None` for any other DS instruction. +fn lds_atomic(name: &str) -> Option { + use super::wave::Op; + let base = name.strip_prefix("ds_")?.replace("_rtn", ""); + Some(match base.as_str() { + "add_u32" => Op::AddU, + "min_u32" => Op::MinU, + "max_u32" => Op::MaxU, + "min_i32" => Op::MinI, + "max_i32" => Op::MaxI, + "or_b32" => Op::Or, + "and_b32" => Op::And, + "xor_b32" => Op::Xor, + _ => return None, + }) +} + /// What a register value holds, for [`wave_local`]. #[derive(Debug, Clone, Copy, PartialEq, Eq)] enum Wave { @@ -732,6 +763,9 @@ pub(super) fn wave_local(p: &Program, st: &Structured) -> Result<(), (usize, &'s Fields::Buffer { data, .. } if name.contains("_atomic_") || name.contains("_store_") => { data.iter().copied().collect() } + // An LDS atomic's operand: a reservation on a counter the group shares (the + // address stays among what it reads, which must not depend on the wave). + Fields::Ds { data, gds: false, .. } if lds_atomic(name).is_some() => data.clone(), _ => Vec::new(), }; let kinds: Vec = ops.iter().map(|o| of(kind, val(o))).collect(); @@ -820,7 +854,7 @@ pub(super) fn wave_local(p: &Program, st: &Structured) -> Result<(), (usize, &'s if *c == Wave::Base { Wave::Base } else { Wave::Prefix } } // the reservation: the first lane adds its wave's total to a shared counter - (_, [Wave::Total(op)]) if name.contains("_atomic_") => { + (_, [Wave::Total(op)]) if name.contains("_atomic_") || lds_atomic(name).is_some() => { let want = match op { Op::AddU => "buffer_atomic_add", Op::MinU => "buffer_atomic_umin", @@ -832,7 +866,11 @@ pub(super) fn wave_local(p: &Program, st: &Structured) -> Result<(), (usize, &'s Op::Xor => "buffer_atomic_xor", _ => "", }; - if name != want || !first_mask(firsts, inst.read(Reg::S(EXEC))) { + // (or the same operation on an LDS counter: the group's waves share it, so + // each host wave's first lane reserves its own wave's slots as the console's + // reserved the whole wave's, `CS_CollectTriangles_Meshlets`' `ds_add_rtn_u32`) + let lds = lds_atomic(name).is_some_and(|o| o == *op); + if (name != want && !lds) || !first_mask(firsts, inst.read(Reg::S(EXEC))) { return Err(TOTAL); } reserved = true; @@ -1658,7 +1696,29 @@ impl<'a> Lower<'a> { let mut pairs: Option = None; let mut pair_waves = 0; // (and only around the LDS accesses whose order counts) - let lds_order = if PAIR_MEETINGS.get() { lds_order(p, x.st) } else { LdsOrder::default() }; + // With local waves too, but only where the LDS accesses cannot be ordered without it + // (`CS_CollectTriangles(_Meshlets)`): the programs that translate without it keep + // their barriers as they were. + let local_order = || { + let StageIo::Cs(cs) = x.native.io else { return false }; + let ds = |i: &Inst| { + matches!(i.fields, Fields::Ds { .. }) + && !(i.name().starts_with("ds_swizzle") || i.name().contains("permute")) + }; + cs.lds_bytes() > 0 + && cs.threads.iter().product::() > 32 + && all_lane_blocks(x.st).is_some_and(|all| { + let spread = p.blocks.iter().enumerate().any(|(b, blk)| { + !all.get(b).copied().unwrap_or(false) && blk.insts.iter().any(ds) + }); + spread && lds_meetings(p, x.st, &all, &HashSet::new()).is_none() + }) + }; + let lds_order = if PAIR_MEETINGS.get() || LOCAL_WAVES.get() && local_order() { + lds_order(p, x.st) + } else { + LdsOrder::default() + }; if let StageIo::Cs(cs) = x.native.io { s.threads = Some(cs.threads); s.lds_dwords = cs.lds_bytes() / 4; @@ -1743,7 +1803,11 @@ impl<'a> Lower<'a> { // lanes 32..63 are the other host wave: masks, lane reads and lane // numbers go through two exchange dwords past the program's LDS exchange = Some((s.lds_dwords, group <= 32, all)); - s.lds_dwords += 2 * group.div_ceil(64); + // (each host wave on its own never exchanges: no room for it + // where the program's own LDS fills the host's, [`tight_lds`]) + if !tight_lds(&cs, s.lds_dwords) { + s.lds_dwords += 2 * group.div_ceil(64); + } } } } @@ -1789,8 +1853,12 @@ impl<'a> Lower<'a> { } if wave == 32 && x.native.wave64 { // console threads go to host waves in the order the waves take them (`wave_base`) - halves = Some(s.lds_dwords); - s.lds_dwords += 1; + if tight_lds(&cs, s.lds_dwords) && exchange.is_some() { + // the flat id's runs are the host waves' (`thread_id`): no counter + } else { + halves = Some(s.lds_dwords); + s.lds_dwords += 1; + } if PAIR_MEETINGS.get() && group > 64 && group % 64 == 0 && (split_lds || exchange.is_some()) { // a counter per console wave, then two exchange buffers of four dwords each pairs = Some(s.lds_dwords); From 05d7e0a310970aa287a7bb5817f82e50db34f21e Mon Sep 17 00:00:00 2001 From: fromrivertothesea <345018979+fromrivertothesea@users.noreply.github.com> Date: Fri, 27 Nov 2026 23:54:18 +0100 Subject: [PATCH 2/2] Shaders: local waves read a guarded write's value under its own guard `wave_local` took a VGPR that a guarded write left with a wave-dependent value in the lanes outside the guard as mixed for every later read. A read in the same block under the same guard only runs in the lanes that wrote it, so it now reads the new value. Another guarded write over such a value keeps it that way. `CS_CollectTriangles(_Meshlets)` load into the VGPR that held the wave's count (0x3acc / 0x34c4) and read it only under the load's guard: both now pass as the exact `local waves (idiom)` form, with the same DXIL as the approximate form gave them. Every other program of GTA VI 1.0.2.1 is unchanged (VICTORY_HOST_WAVE=32, all outcomes and DXIL hashes identical). Co-Authored-By: Claude Opus 5.5 --- crates/victory-shader/src/emit/lower.rs | 41 +++++++++++++++++++------ 1 file changed, 31 insertions(+), 10 deletions(-) diff --git a/crates/victory-shader/src/emit/lower.rs b/crates/victory-shader/src/emit/lower.rs index e568d72f70..4a4007076a 100644 --- a/crates/victory-shader/src/emit/lower.rs +++ b/crates/victory-shader/src/emit/lower.rs @@ -882,7 +882,9 @@ pub(super) fn wave_local(p: &Program, st: &Structured) -> Result<(), (usize, &'s }; let sections: Vec<_> = (0..p.blocks.len()).map(|b| super::wave::sections(p, b)).collect(); let order = crate::ir::reverse_postorder(&p.blocks); - let pass = |kind: &mut HashMap, firsts: &mut HashSet, check: bool| -> Result<(), (usize, Why)> { + // guarded writes over a wave-dependent value: the block and guard they were written under + type Stale = HashMap; + let pass = |kind: &mut HashMap, firsts: &mut HashSet, stale: &mut Stale, check: bool| -> Result<(), (usize, Why)> { for &b in &order { let block = &p.blocks[b]; for phi in &block.phis { @@ -929,7 +931,21 @@ pub(super) fn wave_local(p: &Program, st: &Structured) -> Result<(), (usize, &'s if name.starts_with("s_cbranch") { continue; // with the block's exit, below } - let (k, reserved) = match step(inst, kind, firsts) { + // Values `stale` under this very guard in this block: the lanes that run it + // are the ones that wrote them, so they read only the new value. + let guard = st.guards[b][i]; + let fresh: Vec<(Value, Wave)> = match guard { + Some(g) => inst + .reads + .iter() + .filter(|&&(_, v)| stale.get(&v) == Some(&(b, g))) + .filter_map(|&(_, v)| kind.remove(&v).map(|k| (v, k))) + .collect(), + None => Vec::new(), + }; + let stepped = step(inst, kind, firsts); + kind.extend(fresh); + let (k, reserved) = match stepped { Ok(r) => r, Err(why) if check => return Err((inst.machine.offset, why)), Err(_) => (Wave::Mixed, false), @@ -946,7 +962,6 @@ pub(super) fn wave_local(p: &Program, st: &Structured) -> Result<(), (usize, &'s if check && effect && !reserved && first_mask(firsts, inst.read(Reg::S(EXEC))) { return Err((inst.machine.offset, EFFECT)); } - let guarded = st.guards[b][i].is_some(); for &(r, w) in &inst.writes { let mut kw = match r { // "some lane is counted" keeps the count's kind; any other SCC is one bit of data @@ -955,13 +970,18 @@ pub(super) fn wave_local(p: &Program, st: &Structured) -> Result<(), (usize, &'s } _ => k, }; - // a guarded write leaves the old value in the lanes outside EXEC - if guarded + // a guarded write leaves the old value in the lanes outside EXEC: the + // value is mixed, but not to a later read under the same guard + if let Some(g) = guard && matches!(r, Reg::V(_)) && kw == Wave::Plain - && matches!(of(kind, inst.read(r)), Wave::First | Wave::Total(_) | Wave::Prefix) + && inst.read(r).is_some_and(|old| { + stale.contains_key(&old) + || matches!(of(kind, Some(old)), Wave::First | Wave::Total(_) | Wave::Prefix) + }) { kw = Wave::Mixed; + stale.insert(w, (b, g)); } put(kind, w, kw); } @@ -985,12 +1005,13 @@ pub(super) fn wave_local(p: &Program, st: &Structured) -> Result<(), (usize, &'s }; let mut kind: HashMap = HashMap::new(); let mut firsts: HashSet = HashSet::new(); + let mut stale: Stale = HashMap::new(); // to a fixpoint (values carried around loops), then once more to say what is wrong let mut settled = false; for _ in 0..16 { - let before = (kind.clone(), firsts.len()); - pass(&mut kind, &mut firsts, false)?; - if (&before.0, before.1) == (&kind, firsts.len()) { + let before = (kind.clone(), firsts.len(), stale.clone()); + pass(&mut kind, &mut firsts, &mut stale, false)?; + if (&before.0, before.1, &before.2) == (&kind, firsts.len(), &stale) { settled = true; break; } @@ -998,7 +1019,7 @@ pub(super) fn wave_local(p: &Program, st: &Structured) -> Result<(), (usize, &'s if !settled { return Err((0, MIXED)); } - pass(&mut kind, &mut firsts, true) + pass(&mut kind, &mut firsts, &mut stale, true) } /// The mode of a group barrier the translation adds (host waves meeting):