| Issue |
244157
|
| Summary |
[AMDGPU] performFNegCombine treats the fadd/fma's own `nsz` as permission to change the sign of its zero result, so `fneg (fadd nsz a, b)` with another use returns +0.0 and gives that use -0.0
|
| Labels |
backend:AMDGPU,
miscompilation,
floating-point,
generated by fuzzer
|
| Assignees |
|
| Reporter |
YuanchengJiang
|
The following code:
IR-only reproducer. Clang cannot emit it for AMDGPU: `-fno-signed-zeros` also puts nsz on the fneg, and `#pragma float_control` is ignored on this target. Run `llc -mtriple=amdgcn-amd-amdhsa -mcpu=gfx900 -O2 repro.ll`:
```llvm
target triple = "amdgcn-amd-amdhsa"
define float @negadd2(float %a, float %b, float %c, ptr addrspace(1) %p) {
%s = fadd nsz float %a, %b
%m = fmul float %s, %c
store float %m, ptr addrspace(1) %p
%n = fneg float %s
ret float %n
}
```
For a = 1, b = -1 (no zero inputs, so nsz permits nothing), s = +0.0. The function must return -0.0 and store +0.0 * c.
Resulted in this output (no GPU; the emitted code and what it computes):
```
negadd2:
s_waitcnt vmcnt(0) expcnt(0) lgkmcnt(0)
v_sub_f32_e64 v0, -v0, v1 ; -a - b = -1 - (-1) = +0.0 (returned; must be -0.0)
v_mul_f32_e64 v1, -v0, v2 ; (-(+0.0)) * c = -0.0 * c (stored; must be +0.0 * c)
global_store_dword v[3:4], v1, off
s_waitcnt vmcnt(0)
s_setpc_b64 s[30:31]
expected (what the fix emits):
v_add_f32_e32 v0, v0, v1
v_mul_f32_e32 v1, v0, v2
v_xor_b32_e32 v0, 0x80000000, v0
```
Trunk: https://godbolt.org/z/nzqq5Gsr8 (LLVM IR, llc trunk -mtriple=amdgcn-amd-amdhsa -mcpu=gfx900 -O2; same code on 81d345fc0e, 2026-11-02)
Root cause: `AMDGPUTargetLowering::performFNegCombine` (llvm/lib/Target/AMDGPU/AMDGPUISelLowering.cpp) pushes the fneg into an FADD (`-(a+b) -> (-a)+(-b)`) or an FMA/FMAD. If the source has other uses, it RAUWs them with `fneg(new node)`. Both forms differ from the original exactly when the source is a zero produced by cancellation. The guard is
```c++
bool MayIgnoreSignedZeroForAllUses =
N0->getFlags().hasNoSignedZeros() ||
(N0.hasOneUse() && N->getFlags().hasNoSignedZeros());
```
#217573 added the second clause and its comment ("nsz on the fneg does not apply to the fadd/fma other users"). The first clause still treats the fadd's own `nsz` as a license. Since #180906, `nsz` only lets an instruction flip the sign of its *input* zeros, not its result, so it covers neither the fneg nor the other users. The generic `getNegatedExpression` has the same issue (reported separately), but it refuses multi-use nodes, so the multi-use case above is AMDGPU-only.
Suggested fix: Require all users of N0 to ignore the sign of a zero result. That also covers the old one-use `fneg nsz` clause. The check is lazy because N0 may have several results for opcodes that never reach these cases:
```diff
- bool MayIgnoreSignedZeroForAllUses =
- N0->getFlags().hasNoSignedZeros() ||
- (N0.hasOneUse() && N->getFlags().hasNoSignedZeros());
+ // N0's own nsz only covers N0's input zeros. The folds below change the
+ // sign of a zero N0 for every user (N and, via RAUW, the others), so all of
+ // them must ignore it.
+ auto MayIgnoreSignedZeroForAllUses = [&] {
+ return DAG.canIgnoreSignBitOfZeroResult(N0);
+ };
...
- if (!MayIgnoreSignedZeroForAllUses)
+ if (!MayIgnoreSignedZeroForAllUses())
```
(fix.diff applies to 81d345fc0e.) Validated: the output becomes the expected code above, and the AGT check no longer fires on fneg-combines.ll / .new.ll / .f16.ll (25 / 30 / 6 fires before). The tests that change are the `*_nsz` cases in those files, which have nsz on the fadd/fma only.
_______________________________________________
llvm-bugs mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-bugs