Shaders: console lanes in groups of several host waves take their threads from a group counter (port of Vice-Tay c125c0c, fxceless #1243)

NVIDIA fills 2-D compute groups from 16 x 2 tiles, so rounding a host wave's
first flat id down to the wave size gives two waves of a 32 x 2 group the
same base. Such programs now take their console threads from an LDS counter
(wave_base), as wave64-in-halves programs already did; lanes are placed by
their rank among the active lanes. VICTORY_THREAD_TICKET=0 restores the old
rounding (part of the shader cache key).

Co-Authored-By: Claude Opus 5.5 <[email protected]>
This commit is contained in:
regiumlepidiandClaude Opus 5.5 committed 2026-11-27 11:22:23 +01:00
1 parent 9341f4316f
commit 854f57ff7a
2 files changed
+48 -9

No files matched your search

+1
View File
@@ -76,6 +76,7 @@ fn build() -> [u8; 32] {
"VICTORY_MESH_OUTPUT_STRICT",
"VICTORY_BVH_FENCES",
"VICTORY_BVH_BOUNDS",
"VICTORY_THREAD_TICKET",
] {
bytes.extend_from_slice(name.as_bytes());
bytes.push(b'=');
+47 -9
View File
@@ -362,7 +362,14 @@ struct Lower<'a> {
/// A console wave64 program in host waves of 32: the LDS dword counting the console threads
/// handed to host waves (`wave_base`), and the base made in the entry block.
halves: Option<u32>,
/// A program with console lanes in a group of several host waves (not `halves`): the LDS
/// dword of `wave_base`'s counter. A host need not give a host wave a wave-aligned run of
/// flat thread ids (NVIDIA fills 2-D groups from 16 x 2 tiles), so the waves take their
/// console threads from a counter as with `halves` (fxceless issue #1243, Vice-Tay c125c0c).
ticket: Option<u32>,
wave_base: Option<V>,
/// With `wave_base`: this thread's place among the active lanes of its host wave.
wave_rank: Option<V>,
/// The host waves of a console wave meet on their own ([`with_pair_meetings`]): the first
/// of the LDS dwords that count each console wave's arrivals, and the dword of this
/// thread's console wave (made in the entry block).
@@ -1388,6 +1395,12 @@ pub(super) fn with_first_half_pairs<R>(translate: impl FnOnce() -> R) -> R {
result
}
/// Whether programs with console lanes in groups of several host waves take their console
/// threads from a group counter (`ticket`). `VICTORY_THREAD_TICKET=0`: as before.
fn ticket_on() -> bool {
std::env::var_os("VICTORY_THREAD_TICKET").is_none_or(|v| v != "0")
}
pub(super) fn host_max_wave() -> u32 {
#[cfg(test)]
if let Some(lanes) = TEST_HOST_WAVE.get() {
@@ -1639,6 +1652,8 @@ impl<'a> Lower<'a> {
let mut exchange: Option<(u32, bool, Vec<bool>)> = None;
// a console wave64 program in host waves of 32: the LDS dword of `wave_base`'s counter
let mut halves: Option<u32> = None;
// console lanes in a group of several host waves: the LDS dword of `wave_base`'s counter
let mut ticket: Option<u32> = None;
// its host waves meet per console wave (`with_pair_meetings`): the first counter's dword
let mut pairs: Option<u32> = None;
let mut pair_waves = 0;
@@ -1782,6 +1797,10 @@ impl<'a> Lower<'a> {
pair_waves = group / 64;
s.lds_dwords += 9 * pair_waves;
}
} else if console_lanes && group > wave && ticket_on() {
// the flat id rounded down is not a host wave's base on every host (#1243)
ticket = Some(s.lds_dwords);
s.lds_dwords += 1;
}
if console_lanes || one_wave && s.lds_dwords > 0 {
s.wave_size = Some(wave);
@@ -1954,7 +1973,9 @@ impl<'a> Lower<'a> {
clane: None,
pair: None,
halves,
ticket,
wave_base: None,
wave_rank: None,
pairs,
meeting: None,
lds_unordered: lds_order.unordered.clone(),
@@ -4348,11 +4369,13 @@ impl<'a> Lower<'a> {
/// zeroes it, the group meets, each wave's first lane adds 32 and the wave takes what it
/// found. Branch-free, in the entry block, which every thread runs.
fn wave_base(&mut self) -> Option<V> {
let slot = self.halves?;
let slot = self.halves.or(self.ticket)?;
if let Some(v) = self.wave_base {
return Some(v);
}
let (e, i32t) = (self.s.entry, self.s.t.i32);
let w = self.s.wave_size.unwrap_or(32);
let group: u32 = self.s.threads.map_or(w, |t| t.iter().product());
let lds = self.s.lds();
let k = self.s.m.i32(slot);
let ptr = self.s.m.element(e, lds, k);
@@ -4373,15 +4396,29 @@ impl<'a> Lower<'a> {
self.s.m.store(e, ptr, zero);
}
self.group_sync(e);
let lane = self.s.op(e, "waveGetLaneIndex", Attr::ReadOnly, i32t, 111, &[]);
let first = self.s.m.cmp(e, pred::EQ, lane, zero);
let k32 = self.s.m.i32(32);
let add = self.s.m.select(e, first, k32, zero);
let one = self.s.m.i32(1);
let (sum, unsigned) = (self.s.m.i8(0), self.s.m.i8(1));
let count = self.s.op(e, "waveActiveOp.i32", Attr::NoUnwind, i32t, 119, &[one, sum, unsigned]);
let rank = self.s.op(e, "wavePrefixOp.i32", Attr::NoUnwind, i32t, 121, &[one, sum, unsigned]);
let kw = self.s.m.i32(w);
let full = self.s.m.cmp(e, pred::EQ, count, kw);
let first = self.s.m.cmp(e, pred::EQ, rank, zero);
let sixteen = self.s.m.i32(16);
let high = self.s.m.bin(e, Bin::Shl, count, sixteen);
let take = self.s.m.select(e, full, kw, high);
let add = self.s.m.select(e, first, take, zero);
let ptr = self.s.m.element(e, lds, k);
let old = self.s.m.atomic(e, Rmw::Add, ptr, add);
let base = self.s.op(e, "waveReadLaneFirst.i32", Attr::NoUnwind, i32t, 118, &[old]);
let found = self.s.op(e, "waveReadLaneFirst.i32", Attr::NoUnwind, i32t, 118, &[old]);
let low_mask = self.s.m.i32(0xffff);
let low = self.s.m.bin(e, Bin::And, found, low_mask);
let past = self.s.m.bin(e, Bin::LShr, found, sixteen);
let blocks = self.s.m.i32(group / w * w);
let tail = self.s.m.bin(e, Bin::Add, blocks, past);
let base = self.s.m.select(e, full, low, tail);
self.s.flags |= flags::WAVE_OPS;
self.wave_base = Some(base);
self.wave_rank = Some(rank);
Some(base)
}
@@ -4389,10 +4426,11 @@ impl<'a> Lower<'a> {
/// (host waves of 32), else the wave's first thread index rounded down plus the lane.
fn console_thread(&mut self) -> V {
let (e, i32t) = (self.s.entry, self.s.t.i32);
let lane = self.s.op(e, "waveGetLaneIndex", Attr::ReadOnly, i32t, 111, &[]);
if let Some(base) = self.wave_base() {
return self.s.m.bin(e, Bin::Add, base, lane);
let rank = self.wave_rank.expect("made with the base");
return self.s.m.bin(e, Bin::Add, base, rank);
}
let lane = self.s.op(e, "waveGetLaneIndex", Attr::ReadOnly, i32t, 111, &[]);
let flat = self.s.op(e, "flattenedThreadIdInGroup.i32", Attr::ReadNone, i32t, 96, &[]);
let first = self.s.op(e, "waveReadLaneFirst.i32", Attr::NoUnwind, i32t, 118, &[flat]);
let wave = self.s.wave_size.unwrap_or(32);
@@ -5012,7 +5050,7 @@ impl<'a> Lower<'a> {
let [x, y, _] = self.s.threads.expect("compute");
let e = self.s.entry;
let i32t = self.s.t.i32;
if self.halves.is_some() {
if self.halves.is_some() || self.ticket.is_some() {
let f = self.console_thread();
self.s.flags |= flags::WAVE_OPS;
let (xk, yk, xy) = (self.s.m.i32(x), self.s.m.i32(y), self.s.m.i32(x * y));