From 9fb1465afe8d905fa1318b8b0573f28e7c5f6d47 Mon Sep 17 00:00:00 2001 From: joeypercia <81598667+joeypercia@users.noreply.github.com> Date: Mon, 23 Nov 2026 19:22:45 -0800 Subject: [PATCH 1/3] Recompiler: v_med3_f32 / _f16 with a NaN operand give the min of the others (#678) With a quiet NaN operand the hardware's v_med3 returns v_min(v_min(s0, s1), s2), the smaller of the two other operands, as LLVM's amdgcn_fmed3 folding documents. med3f computed max(min(a, b), min(max(a, b), c)), which gives max(a, b) for a NaN in c and b / a for a NaN in a / b (med3(2, 1, NaN) = 2, want 1), on NVIDIA and AMD alike. The clamp form med3(NaN, lo, hi) was already right. Fixes #678 Assisted-by: Claude Code (Claude Opus 5.5) --- crates/xcl/src/gpu/recomp/emit.rs | 3 ++- crates/xcl/src/gpu/recomp/tests.rs | 40 ++++++++++++++++++++++++++++++ 2 files changed, 42 insertions(+), 1 deletion(-) diff --git a/crates/xcl/src/gpu/recomp/emit.rs b/crates/xcl/src/gpu/recomp/emit.rs index bfdb47201..fa28b8b40 100644 --- a/crates/xcl/src/gpu/recomp/emit.rs +++ b/crates/xcl/src/gpu/recomp/emit.rs @@ -6106,7 +6106,8 @@ uint f32_to_f16_rm(float f, uint rm) { return sign | (h + (uint)inc); } float mad_nf_f16(float a, float b, float c, uint rm) { precise float p = a * b; float p16 = f16tof32(f32_to_f16_rm(p, rm)); precise float r = p16 + c; return r; } -float med3f(float a, float b, float c) { return max(min(a, b), min(max(a, b), c)); } +// with a NaN operand the hardware gives v_min(v_min(a, b), c): the smaller of the other two +float med3f(float a, float b, float c) { return (isnan(a) || isnan(b) || isnan(c)) ? min(min(a, b), c) : max(min(a, b), min(max(a, b), c)); } uint med3u(uint a, uint b, uint c) { return max(min(a, b), min(max(a, b), c)); } int med3i(int a, int b, int c) { return max(min(a, b), min(max(a, b), c)); } // x - floor(x) below 1.0 (RDNA: a NaN stays NaN and ±inf gives NaN – min() would return the diff --git a/crates/xcl/src/gpu/recomp/tests.rs b/crates/xcl/src/gpu/recomp/tests.rs index b40953c60..58965cc4f 100644 --- a/crates/xcl/src/gpu/recomp/tests.rs +++ b/crates/xcl/src/gpu/recomp/tests.rs @@ -2742,6 +2742,46 @@ fn gpu_images_a16_d16() { } } +/// v_med3_f32 / v_med3_f16 with a quiet NaN operand: the hardware returns +/// v_min(v_min(s0, s1), s2), the smaller of the other two (LLVM's amdgcn_fmed3 folding, +/// AMDGPUInstCombineIntrinsic.cpp), wherever the NaN is; without a NaN, the median (#678). +#[test] +fn gpu_med3_with_a_nan_operand_is_the_min_of_the_others() { + for wave64 in gpu_waves() { + let p = prog(&[ + ("V_MED3_F32", "ENC_VOP3", &[("VDST", 10), ("SRC0", V), ("SRC1", V + 1), ("SRC2", V + 2)], None), + ("V_MED3_F16", "ENC_VOP3", &[("VDST", 11), ("SRC0", V + 3), ("SRC1", V + 4), ("SRC2", V + 5)], None), + ]); + let (dxil, mem) = kernel_wave(&p, wave64); + let n = f32::NAN; + let h = |x: f32| if x.is_nan() { 0x7E00 } else { half_bits(x) }; + // (s0, s1, s2, want) + let cases: [(f32, f32, f32, f32); 10] = [ + (n, 2.0, 1.0, 1.0), (n, 1.0, 2.0, 1.0), (2.0, n, 1.0, 1.0), (1.0, n, 2.0, 1.0), + (2.0, 1.0, n, 1.0), (1.0, 2.0, n, 1.0), (n, 0.0, 1.0, 0.0), (-3.0, n, 5.0, -3.0), + (3.0, 1.0, 2.0, 2.0), (n, n, 4.0, 4.0), + ]; + let mut st = KernelState::default(); + for (l, r) in st.vgpr.iter_mut().enumerate() { + let (a, b, c, _) = cases[l % cases.len()]; + (r[0], r[1], r[2]) = (a.to_bits(), b.to_bits(), c.to_bits()); + (r[3], r[4], r[5]) = (h(a), h(b), h(c)); + } + let r = runner::run(&dxil, &st, &mem).unwrap(); + let mut bad = Vec::new(); + for (l, (a, b, c, want)) in cases.iter().enumerate() { + let (g32, g16) = (r.vgpr[l][10], r.vgpr[l][11] & 0xFFFF); + if g32 != want.to_bits() { + bad.push(format!("med3_f32({a}, {b}, {c}) = {}, want {want}", f32::from_bits(g32))); + } + if g16 != h(*want) { + bad.push(format!("med3_f16({a}, {b}, {c}) = {g16:#06x}, want {:#06x}", h(*want))); + } + } + assert!(bad.is_empty(), "wave{}: {} wrong\n{}", if wave64 { 64 } else { 32 }, bad.len(), bad.join("\n")); + } +} + /// f32 → IEEE half bits (exact for the small values used in tests). fn half_bits(x: f32) -> u32 { if x == 0.0 { From 597102f28172f77301554b0c1ff6193e0f7c93f9 Mon Sep 17 00:00:00 2001 From: milesah3ad <360542007+milesah3ad@users.noreply.github.com> Date: Tue, 24 Nov 2026 18:52:07 -0600 Subject: [PATCH 2/3] Direct DXIL: v_med3_f32 / _f16 with a NaN operand give the min of the others med3f was max(min(a, b), min(max(a, b), c)) for every input, so a NaN operand did not give what the hardware does: v_min(v_min(a, b), c), the smaller of the other two. With a NaN in any operand it now selects min(min(a, b), c), as #686 does on the HLSL path; other inputs are unchanged. gpu_med3_with_a_nan_operand_is_the_min_of_the_others now passes with XCL_DIRECT_DXIL=1. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_01YPhfiWsdJcDBLYSS8edLyS --- crates/xcl/src/gpu/dxil/prelude.rs | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/crates/xcl/src/gpu/dxil/prelude.rs b/crates/xcl/src/gpu/dxil/prelude.rs index a171fd099..26ab39359 100644 --- a/crates/xcl/src/gpu/dxil/prelude.rs +++ b/crates/xcl/src/gpu/dxil/prelude.rs @@ -186,11 +186,20 @@ impl Lw { let m = self.fmad(a, b, c); self.sel(cnd, c, m) } + /// With a NaN operand the hardware gives v_min(v_min(a, b), c): the smaller of the other + /// two (as #686 does on the HLSL path). pub fn med3f(&mut self, a: Value, b: Value, c: Value) -> Value { let x = self.fmin(a, b); let y = self.fmax(a, b); let y = self.fmin(y, c); - self.fmax(x, y) + let med = self.fmax(x, y); + let na = self.isnan(a); + let nb = self.isnan(b); + let nc = self.isnan(c); + let n = self.lor(na, nb); + let n = self.lor(n, nc); + let lo = self.fmin(x, c); + self.sel(n, lo, med) } /// `div_fixup` pub fn div_fixup(&mut self, q: Value, d: Value, n: Value) -> Value { From 8c6027287752346710624075516b980feb2e77f0 Mon Sep 17 00:00:00 2001 From: milesah3ad <360542007+milesah3ad@users.noreply.github.com> Date: Tue, 24 Nov 2026 18:57:53 -0600 Subject: [PATCH 3/3] Tests: check_f32_three_source expects the NaN rule of v_med3_f32 The three-source program check written for the direct backend computed med3 with Rust's min / max, which ignore a NaN, so with #686's rule (a NaN operand gives the smaller of the other two, on both paths now) lane 0's med3(|y|, NaN, 4.0) came out 4.0 against an expected 16.0. The expected value follows the hardware rule. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_01YPhfiWsdJcDBLYSS8edLyS --- crates/xcl/src/gpu/recomp/lower.rs | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/crates/xcl/src/gpu/recomp/lower.rs b/crates/xcl/src/gpu/recomp/lower.rs index 44dfbb039..aaab886d4 100644 --- a/crates/xcl/src/gpu/recomp/lower.rs +++ b/crates/xcl/src/gpu/recomp/lower.rs @@ -2732,7 +2732,8 @@ pub(crate) mod tests { } pub(crate) fn check_f32_three_source(_: &[u32], v: &[Vec]) { - let med3 = |a: f32, b: f32, c: f32| a.min(b).max(a.max(b).min(c)); + // with a NaN operand v_med3_f32 gives the smaller of the other two (#686) + let med3 = |a: f32, b: f32, c: f32| if a.is_nan() || b.is_nan() || c.is_nan() { a.min(b).min(c) } else { a.min(b).max(a.max(b).min(c)) }; for (l, r) in v.iter().enumerate() { let f = |i: usize| f32::from_bits(r[i]); let (x, y) = (l as f32, l as f32 - 16.0);