Shadow and trace programs translate, lint clean, ray-tracing write-up

- 59 of the 64 trace programs and the irradiance-cache shading program
  translate; 51 programs leave the approximate wave64 forms.
- The workspace passes the lint with warnings as errors in both feature sets;
  a few style lints are let through in Cargo.toml for this fork.
- README status updated (relay and in-place memory results);
  docs/fork-ray-tracing.md describes what the title does and what is built.

Co-Authored-By: Claude Fable 5.2 <[email protected]>
This commit is contained in:
fxcelessandClaude Fable 5.2 committed 2026-11-24 13:48:06 -05:00
1 parent 6fa814fba5
commit af8f4c68bf
21 files changed
+2350 -62

No files matched your search

+115 -3
View File
@@ -21,6 +21,10 @@ use crate::ir::{EXEC, Inst, Program, Reg, Value};
pub(crate) enum Op {
AddU,
AddF,
/// A sum of 16-bit floats in the low halves (`v_add_f16`), each addition rounded to 16
/// bits: not associative, so only the balanced tree over the lanes in their order is
/// followed (see [`buddies`]), which is how the compiler sums a wave.
AddH,
MinU,
MaxU,
MinI,
@@ -39,6 +43,7 @@ impl Op {
// the high one) is the 32-bit sum of the low dwords
"v_add_nc_u32" | "v_add_co_u32" => Op::AddU,
"v_add_f32" => Op::AddF,
"v_add_f16" => Op::AddH,
"v_min_u32" => Op::MinU,
"v_max_u32" => Op::MaxU,
"v_min_i32" => Op::MinI,
@@ -55,7 +60,7 @@ impl Op {
/// Whether `k` leaves the other operand unchanged.
fn identity(self, k: u32) -> bool {
match self {
Op::AddU | Op::Or | Op::Xor | Op::MaxU => k == 0,
Op::AddU | Op::AddH | Op::Or | Op::Xor | Op::MaxU => k == 0,
Op::AddF => k == 0 || k == 0x8000_0000,
Op::MinU | Op::And => k == u32::MAX,
Op::MinI => k == 0x7FFF_FFFF,
@@ -67,7 +72,26 @@ impl Op {
/// Counting a lane twice changes the result.
fn counts(self) -> bool {
matches!(self, Op::AddU | Op::AddF | Op::Xor)
matches!(self, Op::AddU | Op::AddF | Op::AddH | Op::Xor)
}
}
/// Whether the lane sets `a` and `b` are the two halves of one aligned run of lanes: lanes
/// 2k and 2k+1, then two such pairs side by side, and so on up to the two halves of the
/// wave. Sums joined only that way form the balanced tree over the lanes in their order,
/// whichever of the two a lane adds to the other (the addition is commutative).
fn buddies(a: u64, b: u64) -> bool {
let n = a.count_ones();
if n != b.count_ones() || !n.is_power_of_two() || n == 64 {
return false;
}
let run = |s: u64| {
let first = s.trailing_zeros();
(first.is_multiple_of(n) && s >> first == (1u64 << n) - 1).then_some(first)
};
match (run(a), run(b)) {
(Some(x), Some(y)) => x.min(y) % (2 * n) == 0 && x.abs_diff(y) == n,
_ => false,
}
}
@@ -218,7 +242,8 @@ fn combine(op: Op, a: Sym, b: Sym) -> Sym {
&& op.identity(ident)
&& oa.is_none_or(|o| o == op)
&& ob.is_none_or(|o| o == op)
&& (!op.counts() || la & lb == 0) =>
&& (!op.counts() || la & lb == 0)
&& (op != Op::AddH || buddies(la, lb)) =>
{
Sym::Red {
op: Some(op),
@@ -635,6 +660,32 @@ fn per_lane(lanes: &[Sym], all: u64) -> Option<Shape> {
Some(Shape::Prefix(op, src, mask, ident, inclusive))
}
/// `WaveActiveBitOr(v7)` into s7 as the console compiler expands it: every lane takes part,
/// those outside the saved EXEC (s[4:5]) with 0.
#[cfg(test)]
pub(super) const WAVE_OR_V7_TO_S7: [u32; 20] = [
0xBE84_25C1, // s_or_saveexec_b64 s[4:5], -1
0xD501_0001,
0x0012_0E80, // v_cndmask_b32 v1, 0, v7, s[4:5]
0x3802_02E9,
0xDE54_C101, // v_or_b32 v1, v1, v1 dpp8:[1,0,3,2,5,4,7,6]
0x3802_02E9,
0x9360_1201, // v_or_b32 v1, v1, v1 dpp8:[2,2,0,0,6,6,4,4]
0x3802_02E9,
0x0009_2401, // v_or_b32 v1, v1, v1 dpp8:[4,4,4,4,0,0,0,0]
0x3802_02FA,
0xFF09_4001, // v_or_b32 v1, v1, v1 row_mirror bound_ctrl:1
0xD778_0002,
0x0201_0101, // v_permlanex16_b32 v2, v1, 0, 0
0x3802_0302, // v_or_b32 v1, v2, v1
0xD760_0007,
0x0001_4101, // v_readlane_b32 s7, v1, 32
0x3802_0207, // v_or_b32 v1, s7, v1
0xD760_0007,
0x0001_0101, // v_readlane_b32 s7, v1, 0
0xBEFE_0404, // s_mov_b64 exec, s[4:5]
];
#[cfg(test)]
mod tests {
use super::*;
@@ -859,6 +910,67 @@ mod tests {
}
}
/// `WaveActiveSum` of 16-bit floats, as compiled in
/// `compute_graph_ops:CS_TransposeVecMatMulAlignedOperation` (0x19c on): the tree the
/// lowering reproduces. A tree in another order is not followed.
#[test]
fn a_sum_of_halves_over_the_wave_is_followed_in_the_order_of_its_tree() {
let words = |first: [u32; 2], second: [u32; 2]| {
[
0xBE84_25C1, // s_or_saveexec_b64 s[4:5], -1
0xD501_0001,
0x0012_0E80, // v_cndmask_b32 v1, 0, v7, s[4:5]
first[0],
first[1],
second[0],
second[1],
0x6402_02E9,
0x0009_2401, // v_add_f16 v1, v1, v1 dpp8:[4,4,4,4,0,0,0,0]
0x6402_02FA,
0xFF09_4001, // v_add_f16 v1, v1, v1 row_mirror bound_ctrl:1
0xD778_0002,
0x0201_0101, // v_permlanex16_b32 v2, v1, 0, 0
0x6402_0302, // v_add_f16 v1, v2, v1
0xD760_0007,
0x0001_4101, // v_readlane_b32 s7, v1, 32
0x6402_0207, // v_add_f16 v1, s7, v1
0xD760_0007,
0x0001_0101, // v_readlane_b32 s7, v1, 0
0xBEFE_0404, // s_mov_b64 exec, s[4:5]
]
};
let pairs = [0x6402_02E9, 0xDE54_C101]; // v_add_f16 v1, v1, v1 dpp8:[1,0,3,2,5,4,7,6]
let quads = [0x6402_02E9, 0x9360_1201]; // v_add_f16 v1, v1, v1 dpp8:[2,2,0,0,6,6,4,4]
let shape = |w: &[u32]| {
let p = build(w, true);
let insts = &p.blocks[0].insts;
let saved = insts[0].write(Reg::S(4)).unwrap();
let v7 = p.inputs.iter().find_map(|&(r, v)| (r == Reg::V(7)).then_some(v)).unwrap();
// ..., v_readlane_b32 s7, v1, 0; s_mov_b64 exec, s[4:5]; s_endpgm
let total = insts[insts.len() - 3].write(Reg::S(7)).unwrap();
(sections(&p, 0)[&0].shapes.get(&total).copied(), Shape::Active(Op::AddH, v7, saved, 0))
};
let (found, want) = shape(&words(pairs, quads));
assert_eq!(found, Some(want));
// lanes two apart first, neighbours second: every lane counted once, but
// (x0 + x2) + (x1 + x3) rounds differently from (x0 + x1) + (x2 + x3)
let (found, _) = shape(&words(quads, pairs));
assert_eq!(found, None);
}
#[test]
fn buddies_are_the_two_halves_of_an_aligned_run() {
assert!(buddies(0b01, 0b10));
assert!(buddies(0b1100, 0b0011));
assert!(buddies(0xF0, 0x0F));
assert!(buddies(u64::from(u32::MAX), u64::from(u32::MAX) << 32));
assert!(!buddies(0b010, 0b100)); // lanes 1 and 2: not one pair
assert!(!buddies(0b0011, 0b110000)); // pairs 0 and 2
assert!(!buddies(0b0101, 0b1010)); // not runs
assert!(!buddies(0b01, 0b1100)); // unequal
assert!(!buddies(0b11, 0b11));
}
#[test]
fn wave32_mov_b64_all_lanes_keeps_exec_masked_for_dpp() {
let w = [