Shader forms: sizes of views nothing reads, ordered sections, wave sums off the beaten path, counters

- An image or structured buffer whose descriptor is read for its size
  alone (the puddle passes, a water simulation step).
- The record count of a structured buffer is asked of the raw view the
  host makes of it: as a structured view's it was in bytes on AMD.
- Primitive-ordered pixel shading becomes rasterizer ordered views (the
  hair cards' layers).
- A 16-bit wave sum where not every lane runs, on 64-lane waves, bit for
  bit as the console's tree.
- "Some lane is left" of a mask operation as a number; scalars parked in a
  VGPR used before; a one-channel store into a counter texture (the
  upscaler).
- translate --list prints a hash of every emitted module.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
This commit is contained in:
localandClaude Opus 5.5 committed 2026-11-24 22:02:52 -06:00
1 parent c897fa28ff
commit 389db9efb2
10 files changed
+941 -31

No files matched your search

@@ -199,6 +199,162 @@ fn a_sum_of_halves_over_the_wave_is_the_consoles_tree_bit_for_bit() {
eprintln!("ran on the hardware device: the sum {:#x} of the console's tree", tree[0]);
}
/// "Some lane is left" of a mask operation as a number: an upscaler's programs wait for
/// other groups in a loop that ends when every lane saw its flag, or after enough rounds:
/// both tests become -1 or 0 and SCC of `timeout | ~waiting` ends it.
#[test]
fn some_lane_of_a_mask_is_a_number_the_whole_wave_agrees_on() {
let native = native(&[
0xD765_0001,
0x0001_00C1, // v_mbcnt_lo_u32_b32 v1, -1, 0
0xD766_0001,
0x0002_02C1, // v_mbcnt_hi_u32_b32 v1, -1, v1
0x7D84_0200, // v_cmp_eq_u32 vcc, s0, v1
0x8792_7E6A, // s_and_b64 s[18:19], vcc, exec
0x8592_80C1, // s_cselect_b64 s[18:19], -1, 0
0xBF09_8501, // s_cmp_ge_u32 s1, 5
0x8594_80C1, // s_cselect_b64 s[20:21], -1, 0
0x8B92_1214, // s_orn2_b64 s[18:19], s[20:21], s[18:19]
0x8506_8081, // s_cselect_b32 s6, 1, 0
0x7E04_0206, // v_mov_b32 v2, s6
0xE070_0000,
0x8002_0200, // buffer_store_dword v2, off, s[8:11], 0
]);
let p = ir::build(&native.code, native.wave64, native.vgprs).expect("ir::build");
let shader = on_64_lanes(&native, &p);
// (lane s0 waits, if there is one; s1 rounds)
for (wait, rounds, done) in [(0, 0, 0), (37, 4, 0), (63, 4, 0), (64, 0, 1), (37, 5, 1), (64, 9, 1)] {
let Some(out) = dispatched(&shader, &[wait, rounds]) else {
return;
};
assert_eq!(out, done, "lane {wait} after {rounds} rounds");
}
}
/// `native` for a host of 64-lane waves.
fn on_64_lanes(native: &NativeProgram, p: &ir::Program) -> HostShader {
let st = structure::structure(p).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(64));
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");
assert_eq!(lowered.wave_size, Some(64));
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,
};
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");
}
shader
}
/// Dword 0 of the UAV after `shader` ran with root constants `constants`; none without a
/// device that takes it.
fn dispatched(shader: &HostShader, constants: &[u32]) -> Option<u32> {
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 None;
};
let (root, pso) = match host.compute_pipeline(shader) {
Ok(x) => x,
Err(e) => {
eprintln!("the hardware device does not take this program (waves of 64?): {e}; not run");
return None;
}
};
let mut ud = vec![0u32; shader.user_data_dwords().max(1) as usize];
for u in &shader.user_data {
if u.root == RootType::Constants {
ud[u.index as usize] = constants.get(u.dword as usize).copied().unwrap_or(0);
}
}
let out = host
.dispatch(
&root,
&pso,
&Dispatch {
roots: vec![(0, Root::Constants(ud))],
views: &[View::RawUav(1)],
samplers: &[],
groups: [1, 1, 1],
},
)
.unwrap_or_else(|e| panic!("dispatch failed: {e}"));
Some(out[0][0])
}
/// The same sum inside `if (t >= s0) { ... }` on a host of 64-lane waves: the lanes below s0
/// are elsewhere, and count as +0 in the console's tree.
#[test]
fn a_sum_of_halves_where_not_every_lane_runs_on_a_wave_of_64() {
let mut words = vec![
0x340C_0085, // v_lshlrev_b32 v6, 5, v0
0x340E_0082, // v_lshlrev_b32 v7, 2, v0
0x4A0E_0F06, // v_add_nc_u32 v7, v6, v7
0x4A0E_0F00, // v_add_nc_u32 v7, v0, v7
0x4A0E_0EFF,
0x0000_3C00, // v_add_nc_u32 v7, 0x3c00, v7
0x7D86_0000, // v_cmp_le_u32 vcc, s0, v0
0xBE8C_246A, // s_and_saveexec_b64 s[12:13], vcc
0xBF88_0000 | 23, // s_cbranch_execz to the restore of EXEC
];
words.extend(super::wave::WAVE_OR_V7_TO_S7.iter().map(|&w| if w >> 25 == 28 { w & 0x01FF_FFFF | 50 << 25 } else { w }));
words.extend_from_slice(&[
0x7E00_0207, // v_mov_b32 v0, s7
0x38 << 26 | 0x1C << 18,
0x80 << 24 | 2 << 16, // buffer_store_dword v0, off, s[8:11], 0
0xBEFE_040C, // s_mov_b64 exec, s[12:13]
]);
let native = native(&words);
let p = ir::build(&native.code, native.wave64, native.vgprs).expect("ir::build");
let shader = on_64_lanes(&native, &p);
let wide = |h: u16| match h {
0 => 0.0,
h => f32::from_bits((u32::from(h >> 10) + 112) << 23 | u32::from(h & 0x3FF) << 13),
};
let narrow = |f: f32| {
if f == 0.0 {
return 0;
}
let b = f.to_bits();
let h = ((b >> 23) - 112) << 10 | (b >> 13 & 0x3FF);
let rest = b & 0x1FFF;
(h + u32::from(rest > 0x1000 || rest == 0x1000 && h & 1 == 1)) as u16
};
for from in [0u32, 1, 5, 31, 32, 37, 63] {
let mut tree: Vec<u16> = (0..64u16).map(|t| if u32::from(t) < from { 0 } else { 0x3C00 + 37 * t }).collect();
while tree.len() > 1 {
tree = tree.chunks(2).map(|c| narrow(wide(c[0]) + wide(c[1]))).collect();
}
let Some(out) = dispatched(&shader, &[from]) else {
return;
};
assert_eq!(out, u32::from(tree[0]), "lanes from {from}");
}
}
/// One `v_add_f16` whose exact result lies between two halves: 1 + 3/1024 (0x3C03) plus 2
/// (0x4000) is 3 + 1.5 steps of that range, which the console rounds up to 0x4202. The
/// host's `f32tof16` may give 0x4201 (toward zero); with `VICTORY_F16_NEAREST=1` the