Recompiler: v_med3_f32 / v_med3_f16 with a NaN operand don't return the min of the other two #678

Open
opened 2026-11-26 04:29:09 +00:00 by github-bot · 1 comment
Owner

@⁠joeypercia on GitHub · 2026-11-24 03:21 UTC · original

With a quiet NaN operand, the hardware's v_med3_f32 / v_med3_f16 return v_min(v_min(s0, s1), s2), i.e. the smaller of the two other operands, wherever the NaN is. LLVM documents and folds it that way (AMDGPUInstCombineIntrinsic.cpp, amdgcn_fmed3: "s0 qnan: min(s1, s2) · s1 qnan: min(s0, s2) · s2 qnan: min(s0, s1)").

XCL's helper, used by both instructions:

float med3f(float a, float b, float c) { return max(min(a, b), min(max(a, b), c)); }

gives max(a, b) for a NaN in c, and b / a for a NaN in a / b. The common clamp form med3(x, lo, hi) with a NaN x happens to come out right; other orders don't.

Measured on main cb868b2, new test gpu_med3_with_a_nan_operand_is_the_min_of_the_others (f32 and f16), RTX 4070 Ti (616.56, wave32) and AMD RDNA2 iGPU (26.11.2, wave64 + wave32), identical on both: 8 of 20 wrong:

s0, s1, s2 XCL hardware
NaN, 2, 1 2 1
2, NaN, 1 2 1
2, 1, NaN 2 1
1, 2, NaN 2 1

(each for f32 and f16). med3(NaN, 0, 1), med3(-3, NaN, 5), med3(NaN, NaN, 4) and the no-NaN control are already right.

Fix, one line:

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)); }

With it the test passes on both GPUs (wave64 and wave32).

Not tested: signaling NaN (LLVM notes s0/s1 sNaN give s2 with ieee=1; XCL's inputs are quiet in practice), a retail title. I'll open the PR with the test and fix; it needs the xcl-smoke runs from someone with a title.

AI use: Claude Code (Claude Opus 5.5) found the bug, wrote the test, the fix and this issue; the numbers are from the runs described.

**@⁠joeypercia** on GitHub · 2026-11-24 03:21 UTC · [original](https://github.com/xcl-dev/xcl/issues/678) With a quiet NaN operand, the hardware's `v_med3_f32` / `v_med3_f16` return `v_min(v_min(s0, s1), s2)`, i.e. the smaller of the two other operands, wherever the NaN is. LLVM documents and folds it that way ([`AMDGPUInstCombineIntrinsic.cpp`, `amdgcn_fmed3`](https://github.com/llvm/llvm-project/blob/main/llvm/lib/Target/AMDGPU/AMDGPUInstCombineIntrinsic.cpp): "s0 qnan: min(s1, s2) · s1 qnan: min(s0, s2) · s2 qnan: min(s0, s1)"). XCL's helper, used by both instructions: ```hlsl float med3f(float a, float b, float c) { return max(min(a, b), min(max(a, b), c)); } ``` gives `max(a, b)` for a NaN in `c`, and `b` / `a` for a NaN in `a` / `b`. The common clamp form `med3(x, lo, hi)` with a NaN `x` happens to come out right; other orders don't. Measured on `main` `cb868b2`, new test `gpu_med3_with_a_nan_operand_is_the_min_of_the_others` (f32 and f16), RTX 4070 Ti (616.56, wave32) and AMD RDNA2 iGPU (26.11.2, wave64 + wave32), identical on both: 8 of 20 wrong: | s0, s1, s2 | XCL | hardware | |---|---|---| | NaN, 2, 1 | 2 | 1 | | 2, NaN, 1 | 2 | 1 | | 2, 1, NaN | 2 | 1 | | 1, 2, NaN | 2 | 1 | (each for f32 and f16). `med3(NaN, 0, 1)`, `med3(-3, NaN, 5)`, `med3(NaN, NaN, 4)` and the no-NaN control are already right. **Fix**, one line: ```hlsl 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)); } ``` With it the test passes on both GPUs (wave64 and wave32). **Not tested:** signaling NaN (LLVM notes s0/s1 sNaN give s2 with ieee=1; XCL's inputs are quiet in practice), a retail title. I'll open the PR with the test and fix; it needs the `xcl-smoke` runs from someone with a title. AI use: Claude Code (Claude Opus 5.5) found the bug, wrote the test, the fix and this issue; the numbers are from the runs described.
github-bot locked as GitHub sync and limited conversation to collaborators 2026-11-26 04:29:09 +00:00
Author
Owner

@⁠Decodere on GitHub · 2026-11-25 00:47 UTC · original · edited 2026-11-25 00:53 UTC

Stepping back from the standalone Direct DXIL med3f split of #678 — worth noting for #686 / follow-ups:

  • gpu::recomp::lower_dxil::tests::f32_programs_on_the_gpu (recomp/lower_dxil.rs:624-702) runs F32_THREE_SOURCE (V_MED3_F32(NaN, y, 4.0) and V_MED3_F32(|y|, -NaN, 4.0)) through both HLSL (recomp/emit.rs::med3f) and Direct DXIL (dxil/prelude.rs::med3f) asserting bit-for-bit equality, and check_f32_three_source (recomp/lower.rs:2735) still asserts the old min/max formula. So recomp/emit.rs::med3f, dxil/prelude.rs::med3f, and check_f32_three_source need to move together rather than splitting Direct DXIL ahead of #686.

AI use: Google Antigravity (Gemini 4 Neon).

**@⁠Decodere** on GitHub · 2026-11-25 00:47 UTC · [original](https://github.com/xcl-dev/xcl/issues/678#issuecomment-7148875714) · edited 2026-11-25 00:53 UTC Stepping back from the standalone Direct DXIL `med3f` split of `#678` — worth noting for `#686` / follow-ups: - `gpu::recomp::lower_dxil::tests::f32_programs_on_the_gpu` (`recomp/lower_dxil.rs:624-702`) runs `F32_THREE_SOURCE` (`V_MED3_F32(NaN, y, 4.0)` and `V_MED3_F32(|y|, -NaN, 4.0)`) through both HLSL (`recomp/emit.rs::med3f`) and Direct DXIL (`dxil/prelude.rs::med3f`) asserting bit-for-bit equality, and `check_f32_three_source` (`recomp/lower.rs:2735`) still asserts the old `min/max` formula. So `recomp/emit.rs::med3f`, `dxil/prelude.rs::med3f`, and `check_f32_three_source` need to move together rather than splitting Direct DXIL ahead of `#686`. AI use: Google Antigravity (Gemini 4 Neon).
This discussion has been locked. Commenting is limited to contributors.
No description provided.