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

Open
opened 2026-11-24 03:21:46 +00:00 by joeypercia · 1 comment
joeypercia commented 2026-11-24 03:21:46 +00:00 (Migrated from github.com)
Contributor

Imported from ivall/xcl. Originally posted by joeypercia on 2026-11-24T03:21:46Z.


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.

<!-- greyfinch-sync:ivall/xcl:issue:6573877031 --> Imported from [ivall/xcl](https://github.com/xcl-dev/xcl/issues/678). Originally posted by [joeypercia](https://github.com/joeypercia) on 2026-11-24T03:21:46Z. --- 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.
Decodere commented 2026-11-25 01:10:55 +00:00 (Migrated from github.com)
Author
Contributor

Imported from ivall/xcl. Originally posted by Decodere on 2026-11-25T00:47:20Z.


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).

<!-- greyfinch-sync:ivall/xcl:comment:7148875714 --> Imported from [ivall/xcl](https://github.com/xcl-dev/xcl/issues/678#issuecomment-7148875714). Originally posted by [Decodere](https://github.com/Decodere) on 2026-11-25T00:47:20Z. --- 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).
Sign in to join this conversation.
No labels
No milestone
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Reference
xcl-dev/xcl#673
No description provided.