| Issue |
237539
|
| Summary |
[DAGCombiner] `(fmul (fadd x, 1.0), y) -> fma(x, y, y)` requires `ninf` only on the fadd, so `y = +Inf` gives NaN
|
| Labels |
miscompilation,
llvm:SelectionDAG,
generated by fuzzer
|
| Assignees |
|
| Reporter |
ChoKyuWon
|
### reproducer
godbolt: https://godbolt.org/z/nbMn84463
```llvm
target triple = "x86_64-unknown-linux-gnu"
define double @f(double %x, double %y) "target-features"="+fma" {
%a = fadd ninf contract double %x, 1.000000e+00
%m = fmul contract double %a, %y
ret double %m
}
define i32 @main() {
%r = call double @f(double 0.000000e+00, double 0x7FF0000000000000)
%isinf = fcmp oeq double %r, 0x7FF0000000000000
%z = zext i1 %isinf to i32
%exit_code = xor i32 %z, 1
ret i32 %exit_code
}
```
```console
$ llc -O3 -o - issue-repro.ll
...
f: # @f
.cfi_startproc
# %bb.0:
vfmadd213sd %xmm1, %xmm1, %xmm0 # xmm0 = (xmm1 * xmm0) + xmm1
retq
...
$ cat issue-repro-control.ll
target triple = "x86_64-unknown-linux-gnu"
define double @f(double %x, double %y) "target-features"="+fma" {
%a = fadd contract double %x, 1.000000e+00
%m = fmul contract double %a, %y
ret double %m
}
define i32 @main() {
%r = call double @f(double 0.000000e+00, double 0x7FF0000000000000)
%isinf = fcmp oeq double %r, 0x7FF0000000000000
%z = zext i1 %isinf to i32
%exit_code = xor i32 %z, 1
ret i32 %exit_code
}
$ llc -O3 -o - issue-repro-control.ll
...
.LCPI0_0:
.quad 0x3ff0000000000000 # double 1
...
f: # @f
.cfi_startproc
# %bb.0:
vaddsd .LCPI0_0(%rip), %xmm0, %xmm0
vmulsd %xmm1, %xmm0, %xmm0
retq
...
```
### suggested fix
`visitFMULForFMADistributiveCombine` guards all of its folds with `FAdd->getFlags().hasNoInfs()`, where `FAdd` is the fadd operand (or just `N1` when `N0` is not an fadd).
`ninf` on the fadd only rules out an infinite `x` or `x + 1.0`, not an infinite `y`, so `x = 0`, `y = +Inf` (the case the comment above the guard names) still turns `(0 + 1) * Inf = +Inf` into `fma(0, Inf, Inf) = NaN`.
Check `N->getFlags().hasNoInfs()` on the fmul instead, which covers both `y` and the fadd; the `*_ninf` tests in `X86/fma_patterns.ll` already have it, and only `combine_fmul_distributive` (`X86/fma-scalar-combine.ll`) and `fold_fmul_distributive` (`AMDGPU/fma.ll`) rely on the fadd's flag (they would need `ninf` on the fmul).
_______________________________________________
llvm-bugs mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-bugs