Merge pull request #30 from regiumlepidi/pr-thread-ticket
Shaders: console threads of 2-D groups on NVIDIA from a group counter (port of Vice-Tay c125c0c, #1243)
This commit is contained in:
3 files changed
+125
-9
No files matched your search
@@ -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'=');
|
||||
|
||||
@@ -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));
|
||||
|
||||
@@ -933,3 +933,80 @@ fn lds_accesses_after_the_last_plain_write_need_no_meeting() {
|
||||
assert_eq!(unstable.iter().map(|x| x.0).collect::<Vec<_>>(), ["ds_read_b32"]);
|
||||
assert!(unordered[0].1 > 0x10, "the read after the barrier");
|
||||
}
|
||||
|
||||
/// A console wave32 program with console lanes (`v_mbcnt`) in a 32 x 2 group: thread (x, y)
|
||||
/// stores 1 at dword 32y + x. Every dword must be written once: the host's relabelled thread
|
||||
/// ids are a bijection of the group. A 32-lane host need not give a host wave a 32-aligned run
|
||||
/// of flat ids (2-D groups are filled from 16 x 2 tiles on NVIDIA and RDNA), so the first flat
|
||||
/// id of a wave rounded down gave two waves the same ids (issue #1243); the waves now take
|
||||
/// their threads from a group counter. With `VICTORY_THREAD_TICKET=0` (the old rounding) the
|
||||
/// count of dwords written is printed, not checked: it depends on the card.
|
||||
#[test]
|
||||
fn the_console_threads_of_a_two_dimensional_group_are_each_given_once() {
|
||||
let words = [
|
||||
0xD765_0003, // v_mbcnt_lo_u32_b32 v3, exec_lo, 0
|
||||
0x0001_007E,
|
||||
0x3408_0285, // v_lshlrev_b32 v4, 5, v1
|
||||
0x4A08_0104, // v_add_nc_u32 v4, v4, v0
|
||||
0x3408_0882, // v_lshlrev_b32 v4, 2, v4
|
||||
0x7E0A_0281, // v_mov_b32 v5, 1
|
||||
0x38 << 26 | 0x1C << 18 | 1 << 12,
|
||||
0x80 << 24 | 2 << 16 | 5 << 8 | 4, // buffer_store_dword v5, v4, s[8:11], 0 offen
|
||||
];
|
||||
let mut native = native_of(&words, false, 64);
|
||||
native.io = StageIo::Cs(ComputeIo {
|
||||
rsrc2: 5 << 1 | 1 << 11, // 5 user SGPRs, thread ids x and y in v0, v1
|
||||
threads: [32, 2, 1],
|
||||
});
|
||||
let p = ir::build(&native.code, native.wave64, native.vgprs).expect("ir::build");
|
||||
let _one = super::valu_tests::ONE_AT_A_TIME.lock().unwrap_or_else(|e| e.into_inner());
|
||||
let lower_with = |ticket: bool| {
|
||||
// SAFETY: tests that read this switch hold ONE_AT_A_TIME
|
||||
unsafe { std::env::set_var("VICTORY_THREAD_TICKET", if ticket { "1" } else { "0" }) };
|
||||
let shader = lowered_on(&native, &p, &HashSet::new(), 32);
|
||||
unsafe { std::env::remove_var("VICTORY_THREAD_TICKET") };
|
||||
shader.expect("lower")
|
||||
};
|
||||
let shader = lower_with(true);
|
||||
let old = lower_with(false);
|
||||
assert_ne!(shader.dxil, old.dxil, "the counter is used for a group of two host waves");
|
||||
if let Ok(mut v) = validator::Validator::load(validator::Validator::default_path().as_deref()) {
|
||||
let mut copy = shader.dxil.clone();
|
||||
v.validate(&mut copy).expect("DXIL validation failed");
|
||||
}
|
||||
let Ok(host) = Host::new(false) else {
|
||||
eprintln!("no SM 6.6 hardware device; not run");
|
||||
return;
|
||||
};
|
||||
let run = |shader: &HostShader| -> Option<Vec<u32>> {
|
||||
let (root, pso) = match host.compute_pipeline(shader) {
|
||||
Ok(x) => x,
|
||||
Err(e) => {
|
||||
eprintln!("the hardware device does not take this program: {e}; not run");
|
||||
return None;
|
||||
}
|
||||
};
|
||||
let ud = vec![0u32; shader.user_data_dwords().max(1) as usize];
|
||||
let out = host
|
||||
.dispatch(
|
||||
&root,
|
||||
&pso,
|
||||
&Dispatch {
|
||||
roots: vec![(0, Root::Constants(ud))],
|
||||
views: &[View::RawUav(64)],
|
||||
samplers: &[],
|
||||
groups: [1, 1, 1],
|
||||
},
|
||||
)
|
||||
.unwrap_or_else(|e| panic!("dispatch failed: {e}"));
|
||||
Some(out[0][..64].to_vec())
|
||||
};
|
||||
let Some(got) = run(&shader) else { return };
|
||||
let missing: Vec<usize> = (0..64).filter(|&i| got[i] != 1).collect();
|
||||
assert!(missing.is_empty(), "dwords not written once: {missing:?}");
|
||||
if let Some(before) = run(&old) {
|
||||
let written = before.iter().filter(|&&d| d == 1).count();
|
||||
eprintln!("the old rounding on this device: {written} of 64 dwords written");
|
||||
}
|
||||
eprintln!("ran on the hardware device: 64 of 64 console threads, each once");
|
||||
}
|
||||
Reference in new issue
Block a user