llvm-project icon indicating copy to clipboard operation
llvm-project copied to clipboard

[fuzzer-found] [LoopVectorize] In-loop reduction in a tail-folded scalar plan with interleaving adds poison from masked-out iterations

Open ubmaxxer opened this issue 1 week ago • 0 comments

With -tail-folding-policy=must-fold-tail -force-vector-interleave=4 -prefer-inloop-reductions, a plain add reduction is interleaved by 4 with VF 1 and a folded tail. The loads are predicated, but the reduction adds are not, so iterations past the trip count add a poison value into the sum. The function returns poison whenever the trip count is not a multiple of 4.

Reproducer

godbolt: https://godbolt.org/z/K4176qvKr

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

; s = 0; for (i = 0; i < n; i++) s += a[i]; return s;      (n > 0)
define i32 @test(ptr %a, i64 %n) {
entry:
  br label %loop
loop:
  %iv = phi i64 [ 0, %entry ], [ %iv.next, %loop ]
  %sum = phi i32 [ 0, %entry ], [ %sum.next, %loop ]
  %p = getelementptr inbounds i32, ptr %a, i64 %iv
  %v = load i32, ptr %p, align 4
  %sum.next = add i32 %sum, %v
  %iv.next = add nuw nsw i64 %iv, 1
  %again = icmp ult i64 %iv.next, %n
  br i1 %again, label %loop, label %exit
exit:
  ret i32 %sum.next
}

; a = {1, 2, 4}, n = 3: the sum is 7.
define i32 @main() {
  %a = alloca [3 x i32], align 4
  store i32 1, ptr %a, align 4
  %p1 = getelementptr inbounds i32, ptr %a, i64 1
  store i32 2, ptr %p1, align 4
  %p2 = getelementptr inbounds i32, ptr %a, i64 2
  store i32 4, ptr %p2, align 4
  %r = call i32 @test(ptr %a, i64 3)
  %ok = icmp eq i32 %r, 7
  %ret = select i1 %ok, i32 7, i32 9
  ret i32 %ret
}
$ opt -S -passes=loop-vectorize -tail-folding-policy=must-fold-tail -force-vector-interleave=4 -prefer-inloop-reductions repro.ll -o out.ll
$ llubi repro.ll; echo $?
7
$ llubi out.ll
error: Execution of function 'main' resulted in poison return value.

With any one of the three options removed, out.ll still returns 7. The result does not depend on the target triple, and adding -force-vector-width=1 does not change it.

Why the result is poison

out.ll, abridged. %trip.count.minus.1 is 2 for n == 3:

vector.body:
  %vec.phi3 = phi i32 [ 0, %vector.ph ], [ %24, %pred.load.continue9 ]
  ...
  %4 = add i64 %index, 3
  %8 = icmp ule i64 %4, %trip.count.minus.1         ; (1)
  ...
  br i1 %8, label %pred.load.if8, label %pred.load.continue9

pred.load.if8:
  %18 = getelementptr inbounds i32, ptr %a, i64 %4
  %19 = load i32, ptr %18, align 4
  br label %pred.load.continue9

pred.load.continue9:
  %20 = phi i32 [ poison, %pred.load.continue7 ], [ %19, %pred.load.if8 ]   ; (2)
  ...
  %24 = add i32 %vec.phi3, %20                      ; (3)
  %index.next = add i64 %index, 4
  ...

middle.block:
  %bin.rdx = add i32 %22, %21
  %bin.rdx10 = add i32 %23, %bin.rdx
  %bin.rdx11 = add i32 %24, %bin.rdx10              ; (4)

For a trip count of 3:

  1. The fourth part handles index 3. 3 u<= 2 is false, so its load is skipped.
  2. The skipped load's value is poison.
  3. The fourth partial sum adds that value without looking at the mask: 0 + poison.
  4. The partial sums are added together, so the result is poison.

Add (3) should keep %vec.phi3 when %8 is false, as a select on the header mask does for reductions that are not in-loop.

Without hidden options (Arm MVE)

With MVE, in-loop add reductions are preferred and the tail of this loop is folded by default, so the only missing condition is a scalar plan with interleaving, which two pragmas provide:

int test(const int *a, unsigned n) {
  int s = 0;
#pragma clang loop vectorize_width(1) interleave_count(4)
  for (unsigned i = 0; i < n; i++)
    s += a[i];
  return s;
}

clang -O2 --target=thumbv8.1m.main-none-eabi -mcpu=cortex-m55 -S -emit-llvm (trunk on godbolt) gives the same four predicated loads and four unguarded adds. With a main that calls test on 3 elements, llubi returns the expected value for the -O0 IR and reports a poison return value for the -O2 IR. clang 13.0.0, 15.0.0, 17.0.1, 19.1.0, 21.1.0, 22.1.0 and 23.1.0 on godbolt give the same output pattern; 12.0.0 does not. I have not run the generated Arm code.

Notes

  • Reproduces with opt (trunk) on godbolt, and with opt and llubi built at c3b40cbb3379 (main, 2026-11-22) and at 84a7a7d12ccc.
  • Wrong results in machine code on x86-64: the C function above with vectorize_predicate(enable) added to the pragma, built with clang -O2 -mllvm -prefer-inloop-reductions at 84a7a7d12ccc, returns 24 instead of 15 for n == 5 on a = {1, 2, 3, 4, 5}. It is wrong for every n from 1 to 12 that is not a multiple of 4 and correct for 4, 8 and 12.

I have a fix with a regression test and will open a PR for it.

This test case was generated by a fuzzer, and the report was written with LLM assistance.

ubmaxxer avatar Nov 23 '26 05:11 ubmaxxer