Recompiler: v_med3_f32 / v_med3_f16 with a NaN operand don't return the min of the other two #678
Labels
No labels
accessibility
bug
documentation
duplicate
enhancement
goal:world-load
good first issue
help wanted
invalid
keep-open
off-goal
question
stale
test-only
wontfix
No milestone
No assignees
1 participant
Notifications
Due date
No due date set.
Dependencies
No dependencies set
Reference
github/xcl-dev--xcl#678
Loading…
Reference in a new issue
No description provided.
Delete branch "%!s()"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
@joeypercia on GitHub · 2026-11-24 03:21 UTC · original
With a quiet NaN operand, the hardware's
v_med3_f32/v_med3_f16returnv_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:
gives
max(a, b)for a NaN inc, andb/afor a NaN ina/b. The common clamp formmed3(x, lo, hi)with a NaNxhappens to come out right; other orders don't.Measured on
maincb868b2, new testgpu_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:(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:
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-smokeruns 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 referenced this issue2026-11-26 04:29:21 +00:00
@Decodere on GitHub · 2026-11-25 00:47 UTC · original · edited 2026-11-25 00:53 UTC
Stepping back from the standalone Direct DXIL
med3fsplit of#678— worth noting for#686/ follow-ups:gpu::recomp::lower_dxil::tests::f32_programs_on_the_gpu(recomp/lower_dxil.rs:624-702) runsF32_THREE_SOURCE(V_MED3_F32(NaN, y, 4.0)andV_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, andcheck_f32_three_source(recomp/lower.rs:2735) still asserts the oldmin/maxformula. Sorecomp/emit.rs::med3f,dxil/prelude.rs::med3f, andcheck_f32_three_sourceneed to move together rather than splitting Direct DXIL ahead of#686.AI use: Google Antigravity (Gemini 4 Neon).