[llvm] [SLP]Emit loop-carried horizontal reductions as loop vector accumulator (PR #221598)

Nikita Popov via llvm-commits llvm-commits at lists.llvm.org
Tue Sep 22 11:55:50 PDT 2026


nikic wrote:

AI flagged a miscompile introduced by this change (https://llvm.godbolt.org/z/6neqbdGeT):
```
target triple = "x86_64-unknown-linux-gnu"

define double @f(ptr %p, double %y, i1 %c) {
entry:
  br i1 %c, label %loop, label %exit

loop:
  %iv = phi i64 [ 0, %entry ], [ %iv.next, %loop ]
  %acc = phi double [ 0.0, %entry ], [ %r, %loop ]
  %p0 = getelementptr double, ptr %p, i64 %iv
  %p1 = getelementptr double, ptr %p0, i64 1
  %p2 = getelementptr double, ptr %p0, i64 2
  %p3 = getelementptr double, ptr %p0, i64 3
  %a0 = load double, ptr %p0
  %a1 = load double, ptr %p1
  %a2 = load double, ptr %p2
  %a3 = load double, ptr %p3
  %s0 = fadd fast double %acc, %a0
  %s1 = fadd fast double %s0, %a1
  %s2 = fadd fast double %s1, %a2
  %r = fadd fast double %s2, %a3
  %iv.next = add i64 %iv, 4
  %cmp = icmp eq i64 %iv.next, 8
  br i1 %cmp, label %exit, label %loop

exit:
  %res = phi double [ %r, %loop ], [ %y, %entry ]
  ret double %res
}
```

The select between the in-loop/out-of-loop value gets the `fast` FMF applied, which is incorrect if the value bypassing the loop is used (and it's Inf/NaN). Should not propagate FMF to the select.

https://github.com/llvm/llvm-project/pull/221598


More information about the llvm-commits mailing list