Wave64 groups of several console waves on 32-lane hosts: host waves meet per console wave

CS_DefragHeap_ComputeRelocations (vfx_triangleClip, 128 threads: two
console waves, which walk the decal triangle heap each its own way, with
LDS shared inside one wave and no barrier) did not translate on NVIDIA:
"an LDS read and write in one lane-branch region", and the wave-wide
branches (`widen`) were refused for groups of more than one console wave,
because a group barrier inside a branch one console wave takes and the
other skips would wait for threads that never come. Its dispatch was
dropped every frame.

- `lower::with_pair_meetings`: the two host waves of each console wave
  meet on their own (`Lower::meet`): an LDS counter per console wave that
  its 64 lanes count up and wait on, polling at most 4096 times (a partner
  that never comes costs wrong values, not a hung card). Used for the
  LDS meetings, the exchanges of masks, lane reads and wave-wide decisions.
  A meeting starts a new block: exchanges happen where the code is being
  lowered, the masks and SCC an instruction reads as numbers are exchanged
  before its own code (`exchange_first`), and an IR block's last DXIL block
  is taken after its exit's code (phi predecessors were wrong otherwise).
- With it, a branch closing a loop is in its own lane region (a lane that
  left the loop does not run it again), and the title's barriers count
  wave-wide branches as per-lane ones (`fence_only`).
- `widen` takes groups of whole console waves when allowed (the digest
  list or VICTORY_WAVE_WIDE=1); the LANE_MASK and driver-loop paths stay
  as they were. A program whose LDS with the meetings' dwords passes the
  host's 32 KiB is refused.
- Digest list: CS_DefragHeap_ComputeRelocations (128) and
  CS_TransmissionVolumetricsSubmerged (water_shading, 64 threads, the
  underwater volumetric light: widened like its tiled siblings).

CS_CollectTriangles_Meshlets (1024 threads) still does not translate: its
own LDS is the host group's whole 32 KiB, no room for exchange dwords or
meeting counters.

Checked: both translate and validate (dxil.dll); 1,800 other compute
programs translate to the same DXIL as before (VICTORY_HOST_MAX_WAVE=32).
New test: two console waves of one group in loops of 4 and 8 rounds with
LDS shared across their halves, bit-exact on an RTX 4070 SUPER.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
This commit is contained in:
readmet3xt-labandClaude Opus 5.5 committed 2026-11-26 17:38:46 +05:30
1 parent f149e26fea
commit 4f4970974f
3 files changed
+417 -39

No files matched your search

@@ -736,3 +736,165 @@ fn the_lanes_past_a_count_of_threads_do_not_run() {
}
}
}
/// The wave size a lowering of `native` at a host of 32-lane waves asks for, and the shader.
fn lowered_with_wave(
native: &NativeProgram,
p: &ir::Program,
wide: &HashSet<usize>,
) -> (Option<u32>, HostShader) {
let st = structure::structure_wide(p, wide).expect("structure");
let res = resource::track(p, &native.layout, native.stage, Some(&st)).expect("resource::track");
let io = io::raise(p, native).expect("io::raise");
let was = lower::TEST_HOST_WAVE.replace(Some(32));
let lowered = lower::lower(&lower::Parts {
native,
p,
st: &st,
io: &io,
res: &res,
inputs: Vec::new(),
outputs: Vec::new(),
prims: Vec::new(),
mesh_stride: 0,
});
lower::TEST_HOST_WAVE.set(was);
let lowered = lowered.expect("lower");
let shader = HostShader {
stage: HwStage::Cs,
dxil: lowered.dxil,
user_data: lowered.user_data,
samplers: lowered.samplers,
static_samplers: Vec::new(),
root_cbvs: lowered.root_cbvs,
root_srvs: lowered.root_srvs,
root_uavs: lowered.root_uavs,
root_indexed_views: res.root_indexed_views,
};
(lowered.wave_size, shader)
}
/// Two console waves in a group of 128 threads, each run as two host waves of 32. Lane t goes
/// round a per-lane loop (t >> 4) + 1 times: it writes its sum to LDS and adds that of lane
/// t ^ 63 (in the other host wave of its console wave) with no barrier between, as one console
/// wave may; after the loop every lane adds lane 63's sum (`v_readlane_b32`). The loop's back
/// edge is taken as the console takes it, by each console wave as a whole, and the two console
/// waves go round 4 and 8 times: their host waves meet per console wave
/// ([`lower::with_pair_meetings`]). A group barrier in the loop would wait for the threads of
/// the console wave that left it (`CS_DefragHeap_ComputeRelocations`, groups of 128, whose
/// console waves walk a heap each its own way).
#[test]
fn console_waves_of_one_group_meet_on_their_own_in_loops_of_their_own() {
let words = [
0x3402_0082, // v_lshlrev_b32 v1, 2, v0: this lane's LDS dword
0x3A06_00BF, // v_xor_b32 v3, 63, v0
0x3406_0682, // v_lshlrev_b32 v3, 2, v3: the partner lane's (other half, same wave)
0x4A04_0000, // v_add_nc_u32 v2, s0, v0: the sum starts at s0 + t
0x2C0E_0084, // v_lshrrev_b32 v7, 4, v0
0x4A0E_0E81, // v_add_nc_u32 v7, 1, v7: rounds (t >> 4) + 1
0x7E0A_0280, // v_mov_b32 v5, 0
0xBE8E_047E, // s_mov_b64 s[14:15], exec
0xD834_0000,
0x0000_0201, // loop: ds_write_b32 v1, v2
0xD8D8_0000,
0x0400_0003, // ds_read_b32 v4, v3
0xBF8C_C07F, // s_waitcnt lgkmcnt(0)
0x4A04_0902, // v_add_nc_u32 v2, v2, v4
0x4A0A_0A81, // v_add_nc_u32 v5, 1, v5
0x7DA8_0B07, // v_cmpx_gt_u32 exec, v7, v5: the lanes with rounds left
0xBF89_FFF7, // s_cbranch_execnz loop
0xBEFE_040E, // s_mov_b64 exec, s[14:15]
0xD760_0007,
0x0001_7F02, // v_readlane_b32 s7, v2, 63
0x4A04_0407, // v_add_nc_u32 v2, s7, v2
0xE070_1000,
0x8002_0201, // buffer_store_dword v2, v1, s[8:11], 0 offen
];
let mut native = native_of(&words, true, 128);
if let StageIo::Cs(cs) = &mut native.io {
cs.rsrc2 |= 1 << 15; // 512 bytes of LDS
}
let p = ir::build(&native.code, native.wave64, native.vgprs).expect("ir::build");
let back = (0..p.blocks.len())
.find(|&b| p.blocks[b].insts.last().is_some_and(|i| i.name() == "s_cbranch_execnz"))
.expect("the back edge");
// per lane, the loop's LDS write and read stand where not every lane runs: still 64 lanes,
// and the lowering names the loop
lower::take_needs();
let (wave, _) = lower::with_pair_meetings(|| lowered_with_wave(&native, &p, &HashSet::new()));
assert_eq!(wave, Some(64));
let needs = lower::take_needs();
let st = structure::structure(&p).expect("structure");
assert_eq!(lower::with_pair_meetings(|| lower::branches_around(&st, &needs)), Some(vec![back]));
// the back edge taken per console wave: waves of 32, meeting per console wave
let wide = HashSet::from([back]);
let (wave, shader) = lower::with_pair_meetings(|| lowered_with_wave(&native, &p, &wide));
assert_eq!(wave, Some(32));
assert!(lower::take_needs().is_empty());
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");
}
// the console: each wave's lanes in step, writes before reads
let console = |s0: u32| {
let mut out = [0u32; 128];
let mut lds = [0u32; 128];
for w in 0..2u32 {
let lanes = 64 * w..64 * w + 64;
let mut sum: Vec<u32> = lanes.clone().map(|t| s0.wrapping_add(t)).collect();
for round in 0..8 {
let on: Vec<u32> = lanes.clone().filter(|t| round < (t >> 4) + 1).collect();
if on.is_empty() {
break;
}
for &t in &on {
lds[t as usize] = sum[(t - 64 * w) as usize];
}
for &t in &on {
let k = (t - 64 * w) as usize;
sum[k] = sum[k].wrapping_add(lds[(t ^ 63) as usize]);
}
}
let last = sum[63];
for t in lanes {
out[t as usize] = sum[(t - 64 * w) as usize].wrapping_add(last);
}
}
out
};
let _one = super::valu_tests::ONE_AT_A_TIME.lock().unwrap_or_else(|e| e.into_inner());
let Ok(host) = Host::new(false) else {
eprintln!("no SM 6.6 hardware device; not run");
return;
};
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;
}
};
for s0 in [0u32, 1000, 0xFFFF_FF00] {
let mut ud = vec![0u32; shader.user_data_dwords().max(1) as usize];
for u in &shader.user_data {
if let (RootType::Constants, 0, 0) = (u.root, u.slot, u.dword) {
ud[u.index as usize] = s0;
}
}
let out = host
.dispatch(
&root,
&pso,
&Dispatch {
roots: vec![(0, Root::Constants(ud))],
views: &[View::RawUav(128)],
samplers: &[],
groups: [1, 1, 1],
},
)
.unwrap_or_else(|e| panic!("dispatch failed: {e}"));
let want = console(s0);
assert_eq!(&out[0][..128], &want[..], "s0 = {s0:#x}");
}
eprintln!("ran on the hardware device: both console waves' loops, as the console runs them");
}