Issue 237542
Summary [DAGCombiner] `fmul (fmul X, C1), C2` is folded to `fmul X, C1*C2` when only the outer fmul has `reassoc`
Labels miscompilation, llvm:SelectionDAG, generated by fuzzer
Assignees
Reporter ChoKyuWon
    ### reproducer
godbolt: https://godbolt.org/z/obsK3PGjW

```llvm
target triple = "x86_64-unknown-linux-gnu"

define float @f(float %x) {
  %a = fmul float %x, 7.0
  %b = fmul reassoc float %a, 10.0
  ret float %b
}

define i32 @main() {
  %r = call float @f(float 0x3FD3333340000000) ; 0.3f
  %bits = bitcast float %r to i32
  %failed = icmp ne i32 %bits, 1101529089
  %exit_code = zext i1 %failed to i32
  ret i32 %exit_code
}
```

```console
$ llc -O3 -mtriple=x86_64 -o - issue-repro.ll
...
.LCPI0_0:
        .long   0x428c0000 # float 70
...
f:                                      # @f
 .cfi_startproc
# %bb.0:
        mulss   .LCPI0_0(%rip), %xmm0
 retq
...

$ cat issue-repro-control.ll
target triple = "x86_64-unknown-linux-gnu"

define float @f(float %x) {
  %a = fmul float %x, 7.0
  %b = fmul float %a, 10.0
  ret float %b
}

define i32 @main() {
  %r = call float @f(float 0x3FD3333340000000) ; 0.3f
  %bits = bitcast float %r to i32
  %failed = icmp ne i32 %bits, 1101529089
 %exit_code = zext i1 %failed to i32
  ret i32 %exit_code
}

$ llc -O3 -mtriple=x86_64 -o - issue-repro-control.ll
...
.LCPI0_0:
        .long 0x40e00000                      # float 7
...
.LCPI0_1:
        .long 0x41200000                      # float 10
...
f: # @f
        .cfi_startproc
# %bb.0:
        mulss .LCPI0_0(%rip), %xmm0
        mulss   .LCPI0_1(%rip), %xmm0
 retq
...
```

### suggested fix
`DAGCombiner::visitFMUL` folds `fmul (fmul X, C1), C2 -> fmul X, C1 * C2` under `Flags.hasAllowReassociation()`, where `Flags` is `N->getFlags()`; the inner `fmul` `N0` is never checked, although LangRef's rewrite-based flags require `reassoc` on every instruction of the rewritten _expression_ (its example is this `fmul` chain).
Also require `N0->getFlags().hasAllowReassociation()` and give the new nodes `Flags & N0->getFlags()`, as `reassociateReduction` already does for its nested-binop pattern.
The `fmul (fadd X, X), C -> fmul X, 2.0 * C` fold in the same block has the same gap (`x = 0x1p127`, `C = 0.25`: `+inf` becomes `0x1p126`).

_______________________________________________
llvm-bugs mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-bugs

Reply via email to