Recompiler: v_fma_legacy_f32 / v_fmac_legacy_f32 return -0 for a zero product plus -0 #679

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

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

v_fma_legacy_f32 / v_fmac_legacy_f32 with a zero factor return c itself; the hardware returns c + (+0), which is +0 when c is -0 (the legacy product of ±0 with anything, inf and NaN included, is +0). LLVM spells this case out in its amdgcn_fma_legacy folding (AMDGPUInstCombineIntrinsic.cpp): "It's tempting to just return Op2 here, but that would give the wrong result if Op2 was -0.0."

XCL (emit.rs):

float fma_legacy(float a, float b, float c) { return (a == 0.0f || b == 0.0f) ? c : mad(a, b, c); }

Measured on main cb868b2, new test gpu_fma_legacy_with_a_zero_factor_adds_plus_zero, RTX 4070 Ti (616.56) and AMD RDNA2 iGPU (26.11.2), same on both: 6 of 14 wrong, all -0 (0x80000000) where +0 is right: fma_legacy and fmac_legacy of (0, inf, -0), (-0, 5, -0), (0, NaN, -0). Zero times inf / NaN plus a non-zero c, and the plain case, are already right.

Fix — note the obvious one doesn't work: select(c == 0.0f, 0.0f, c) still returns -0 on both GPUs, because DXC's relaxed float math folds it back to c (+0 == -0). On the bits it holds:

float fma_legacy(float a, float b, float c) { return (a == 0.0f || b == 0.0f) ? asfloat(select(c == 0.0f, 0u, asuint(c))) : mad(a, b, c); }

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

Low impact: only the sign of a zero result changes, which matters only where something later divides by it or takes its sign. 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/679) `v_fma_legacy_f32` / `v_fmac_legacy_f32` with a zero factor return `c` itself; the hardware returns `c + (+0)`, which is `+0` when `c` is `-0` (the legacy product of ±0 with anything, inf and NaN included, is `+0`). LLVM spells this case out in its `amdgcn_fma_legacy` folding ([`AMDGPUInstCombineIntrinsic.cpp`](https://github.com/llvm/llvm-project/blob/main/llvm/lib/Target/AMDGPU/AMDGPUInstCombineIntrinsic.cpp)): "It's tempting to just return Op2 here, but that would give the wrong result if Op2 was -0.0." XCL (`emit.rs`): ```hlsl float fma_legacy(float a, float b, float c) { return (a == 0.0f || b == 0.0f) ? c : mad(a, b, c); } ``` Measured on `main` `cb868b2`, new test `gpu_fma_legacy_with_a_zero_factor_adds_plus_zero`, RTX 4070 Ti (616.56) and AMD RDNA2 iGPU (26.11.2), same on both: 6 of 14 wrong, all `-0` (`0x80000000`) where `+0` is right: `fma_legacy` and `fmac_legacy` of `(0, inf, -0)`, `(-0, 5, -0)`, `(0, NaN, -0)`. Zero times inf / NaN plus a non-zero `c`, and the plain case, are already right. **Fix** — note the obvious one doesn't work: `select(c == 0.0f, 0.0f, c)` still returns `-0` on both GPUs, because DXC's relaxed float math folds it back to `c` (`+0 == -0`). On the bits it holds: ```hlsl float fma_legacy(float a, float b, float c) { return (a == 0.0f || b == 0.0f) ? asfloat(select(c == 0.0f, 0u, asuint(c))) : mad(a, b, c); } ``` With it the test passes on both GPUs (wave64 and wave32). Low impact: only the sign of a zero result changes, which matters only where something later divides by it or takes its sign. 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
This discussion has been locked. Commenting is limited to contributors.
No description provided.