Merge upstream main (8e79c80) into shader/per-lane-phi

main now has CI (clippy with -D warnings, then the workspace's tests) and
three new tests that call `resource::track`, which this branch gives a
fourth argument. No textual conflict, but the test target did not build:
those three calls (emit/lower.rs, twice in emit/mod.rs) now structure the
program first and pass it, as this branch's other callers do. Both of
CI's clippy commands are clean on the result.

Co-Authored-By: Claude Fable 5.2 <[email protected]>
This commit is contained in:
r4.andClaude Fable 5.2 committed 2026-11-24 11:10:23 +02:00
commit f28a9f462e
28 files changed
+1308 -143

No files matched your search

@@ -167,6 +167,11 @@ fn run_cases(
cases: &[[u32; 4]],
mut check: impl FnMut(&str, usize, [u32; 4], u32),
) {
// One test at a time: with these tests on the hardware adapter and WARP
// from several test threads at once, now and then none of their
// dispatches ever completes and the test binary hangs.
static ONE_AT_A_TIME: std::sync::Mutex<()> = std::sync::Mutex::new(());
let _one = ONE_AT_A_TIME.lock().unwrap_or_else(|e| e.into_inner());
let mut ran = 0;
for (label, warp) in [("hardware", false), ("warp", true)] {
let Ok(host) = Host::new(warp) else {
@@ -232,6 +237,27 @@ fn f16_to_f32(bits: u16) -> f32 {
f32::from_bits(f32_bits)
}
#[test]
fn packed_add_selects_halves_of_inline_constants() {
// Add the inline constant 1/(2*pi) to v1 (zero) to read back its two halves.
// The same GFX10 source field (248) has a different bit pattern for a
// packed floating-point and a packed integer instruction.
let cases = [
// v_pk_add_f16 v0, 0x3118, v1
([0xcc0f4000, 0x1a0202f8], 0x0000_3118),
// v_pk_add_f16 v0, 0x3118, v1 op_sel_hi:[0,1] (use low twice)
([0xcc0f4000, 0x120202f8], 0x3118_3118),
// v_pk_add_u16 v0, 0x3e22f983, v1
([0xcc0a4000, 0x1a0202f8], 0x3e22_f983),
];
for (words, expected) in cases {
let shader = build_valu_cs(&words);
run_cases(&shader, &[[0; 4]], |backend, _, _, got| {
assert_eq!(got, expected, "{backend}: {words:08x?}");
});
}
}
#[test]
fn v_med3_f32_matches_rdna2_with_nan() {
// v_med3_f32 v0, v1, v2, v3 (VOP3 opcode 0x157)
@@ -280,6 +306,32 @@ fn v_med3_f32_matches_rdna2_with_nan() {
});
}
/// RDNA2 orders -0 below +0 in its maximums and minimums; DXIL's FMax and
/// FMin compare and select, which can give either zero.
#[test]
fn max_and_min_order_negative_zero_below_positive_zero() {
let (pz, nz) = (0_u32, 0x8000_0000_u32);
// Each case has a -0 and a +0 among its first two operands.
let cases = [
[nz, pz, nz, 0],
[pz, nz, nz, 0],
[nz, pz, pz, 0],
[pz, nz, pz, 0],
];
let ops = [
("v_max_f32", 0x110, pz),
("v_min_f32", 0x10F, nz),
("v_max3_f32", 0x154, pz),
("v_min3_f32", 0x151, nz),
];
for (name, op, want) in ops {
let shader = build_valu_cs(&vop3(op, 0, vgpr(1), vgpr(2), vgpr(3)));
run_cases(&shader, &cases, |backend, _, inp, got| {
assert_eq!(got, want, "{backend}: {name} {inp:x?}");
});
}
}
#[test]
fn v_med3_f16_matches_rdna2_with_nan() {
// v_med3_f16 v0, v1, v2, v3 (VOP3 opcode 0x357)
@@ -321,6 +373,58 @@ fn v_med3_f16_matches_rdna2_with_nan() {
});
}
/// `v_cmp_lg_*` is "less or greater": ordered and not equal, so false when
/// either operand is a NaN (RDNA2 ISA; the same on gfx1036 hardware).
#[test]
fn v_cmp_lg_is_false_for_nan() {
// v_cmp_lg_{f32,f16}_e64 s20, v1, v2 (VOPC 0x005 / 0x0cd in VOP3);
// v_cndmask_b32_e64 v0, 0, 1, s20 (VOP3 0x101)
let nan = f32::NAN.to_bits();
let (inf, ninf) = (f32::INFINITY.to_bits(), f32::NEG_INFINITY.to_bits());
let f = f32::to_bits;
let f32_cases: &[([u32; 4], u32)] = &[
([f(1.0), f(2.0), 0, 0], 1),
([f(2.0), f(1.0), 0, 0], 1),
([f(1.0), f(1.0), 0, 0], 0),
([f(0.0), f(-0.0), 0, 0], 0),
([inf, ninf, 0, 0], 1),
([inf, inf, 0, 0], 0),
([nan, f(1.0), 0, 0], 0),
([f(1.0), nan, 0, 0], 0),
([nan, nan, 0, 0], 0),
([0x7F80_0001, f(0.0), 0, 0], 0), // a signalling NaN
];
let f16_cases: &[([u32; 4], u32)] = &[
([0x3C00, 0x4000, 0, 0], 1),
([0x4000, 0x3C00, 0, 0], 1),
([0x3C00, 0x3C00, 0, 0], 0),
([0x0000, 0x8000, 0, 0], 0),
([0x7C00, 0xFC00, 0, 0], 1),
([0x7E00, 0x3C00, 0, 0], 0),
([0x3C00, 0x7E00, 0, 0], 0),
([0x7E00, 0x7E00, 0, 0], 0),
([0x7C01, 0x0000, 0, 0], 0), // a signalling NaN
([0xABCD_3C00, 0x1234_7E00, 0, 0], 0), // only the low halves are read
];
for (op, what, cases) in [
(0x005u16, "v_cmp_lg_f32", f32_cases),
(0x0cd, "v_cmp_lg_f16", f16_cases),
] {
let mut body = vop3(op, 20, vgpr(1), vgpr(2), 0x80).to_vec();
body.extend_from_slice(&vop3(0x101, 0, 0x80, 0x81, sgpr(20)));
let shader = build_valu_cs(&body);
let inputs: Vec<[u32; 4]> = cases.iter().map(|c| c.0).collect();
run_cases(&shader, &inputs, |backend, i, inp, got| {
let want = cases[i].1;
assert_eq!(
got, want,
"{backend}: {what}({:#010x}, {:#010x}) = {got}, want {want}",
inp[0], inp[1],
);
});
}
}
#[test]
fn v_fract_f32_never_returns_one() {
// v_fract_f32 v0, v1 (VOP1 opcode 0x020)
@@ -421,6 +525,52 @@ fn v_fract_f16_never_returns_one() {
});
}
/// RDNA2's flt16_to_snorm16 / flt16_to_unorm16: clamp, scale by 32767 or
/// 65535, round the exact product to nearest even; NaN gives 0.
fn cvt_norm16_reference(bits: u16, signed: bool) -> u16 {
let x = f16_to_f32(bits);
if x.is_nan() {
return 0;
}
let (lo, scale) = if signed {
(-1.0, 32767.0)
} else {
(0.0, 65535.0)
};
// f64 holds the product of an f16 and a 16-bit scale exactly.
let p = f64::from(x.clamp(lo, 1.0)) * scale;
p.round_ties_even() as i32 as u16
}
#[test]
fn v_cvt_norm_i16_and_u16_f16_round_the_exact_product() {
// Every 251st f16 pattern, plus the values where an f32 product would
// round before the rounding to an integer, ties, and the edges.
let mut bits: Vec<u16> = (0..=u16::MAX).step_by(251).collect();
bits.extend([
0x0000, 0x8000, 0x0001, 0x8001, 0x03FF, 0x0400, 0x3800, 0xB800, 0x3801, 0xB801, 0x37FF,
0x3BFF, 0xBBFF, 0x3C00, 0xBC00, 0x3C01, 0x7BFF, 0x7C00, 0xFC00, 0x7E00, 0xFE00, 0x7C01,
0x3A9B, 0xBA9B, 0x2E66, 0x1400,
]);
for (op, signed, what) in [
(0x063u16, true, "v_cvt_norm_i16_f16"),
(0x064, false, "v_cvt_norm_u16_f16"),
] {
// v_cvt_norm_*16_f16 v0, v1 (VOP1); v0's high half starts at 0.
let shader = build_valu_cs(&[vop1(0x001, 0, 128), vop1(op, 0, vgpr(1))]);
let inputs: Vec<[u32; 4]> = bits.iter().map(|&b| [u32::from(b), 0, 0, 0]).collect();
run_cases(&shader, &inputs, |backend, i, _, got| {
let want = cvt_norm16_reference(bits[i], signed);
assert_eq!(
got,
u32::from(want),
"{backend}: {what}({:#06x}) = {got:#010x}, want {want:#06x}",
bits[i],
);
});
}
}
#[test]
fn v_sin_and_v_cos_f32_reduce_large_turns() {
let sin_shader = build_valu_cs(&[vop1(0x035, 0, vgpr(1))]);
@@ -521,6 +671,46 @@ fn v_sin_and_v_cos_f32_reduce_large_turns() {
});
}
#[test]
fn v_cvt_u16_and_i16_f16_saturate() {
// v_mov_b32 v0, 0; v_cvt_u16_f16 / v_cvt_i16_f16 v0, v1 (VOP1 0x052 / 0x053).
let cases: &[(u16, u16, u16)] = &[
// (f16 input, u16 result, i16 result)
(0x0000, 0, 0), // 0
(0x8000, 0, 0), // -0
(0x3E00, 1, 1), // 1.5 -> 1 (toward zero)
(0xBE00, 0, 0xFFFF), // -1.5 -> 0 / -1
(0x57D0, 125, 125), // 125.0
(0x7800, 32768, 32767), // 32768 saturates i16
(0x77FF, 32752, 32752), // 32752, the largest f16 below 32768
(0xF800, 0, 0x8000), // -32768 fits i16
(0xF801, 0, 0x8000), // -32800 saturates i16
(0x7BFF, 65504, 32767), // max finite f16
(0xFBFF, 0, 0x8000), // -65504
(0x7C00, 0xFFFF, 32767), // +inf
(0xFC00, 0, 0x8000), // -inf
(0x7E00, 0, 0), // NaN
(0xFE00, 0, 0), // -NaN
(0x0001, 0, 0), // smallest subnormal
];
for (op, what, pick) in [
(0x052u16, "v_cvt_u16_f16", 0usize),
(0x053, "v_cvt_i16_f16", 1),
] {
let shader = build_valu_cs(&[vop1(0x001, 0, 128), vop1(op, 0, vgpr(1))]);
let inputs: Vec<[u32; 4]> = cases.iter().map(|c| [u32::from(c.0), 0, 0, 0]).collect();
run_cases(&shader, &inputs, |backend, i, _, got| {
let want = if pick == 0 { cases[i].1 } else { cases[i].2 };
assert_eq!(
got,
u32::from(want),
"{backend}: {what}({:#06x}) = {got:#010x}, want {want:#06x}",
cases[i].0,
);
});
}
}
#[test]
fn v_sin_and_v_cos_f16_reduce_large_turns() {
let sin_shader = build_valu_cs(&[vop1(0x060, 0, vgpr(1))]);