Issue 245054
Summary [LoopVectorize] Forced vector epilogue is entered with a vector trip count of 0 and never exits
Labels new issue
Assignees
Reporter ubmaxxer
    ### Summary

With `-force-vector-interleave=8 -epilogue-vectorization-force-VF=2`, a loop with exactly 2 iterations enters the epilogue vector loop with a vector trip count of 0. The exit test never succeeds and the loop reads and writes out of bounds.

### Reproducer

godbolt: https://godbolt.org/z/zbsbx4vz7

```llvm
target datalayout = "e-m:e-p270:32:32-p271:32:32-p272:64:64-i64:64-i128:128-f80:128-n8:16:32:64-S128"
target triple = "x86_64-unknown-linux-gnu"

; for (i = 0; i < n; i += 3) b[i] = a[i];
; n == 6 gives two iterations: i = 0 and i = 3.
define void @test(ptr noalias %a, ptr noalias %b, i64 %n) {
entry:
  br label %loop
loop:
  %iv = phi i64 [ 0, %entry ], [ %iv.next, %loop ]
  %pa = getelementptr inbounds i8, ptr %a, i64 %iv
  %v = load i8, ptr %pa, align 1
  %ext = zext i8 %v to i16
  %pb = getelementptr inbounds i16, ptr %b, i64 %iv
  store i16 %ext, ptr %pb, align 2
  %iv.next = add nuw nsw i64 %iv, 3
  %again = icmp ult i64 %iv.next, %n
  br i1 %again, label %loop, label %exit
exit:
  ret void
}

define i32 @main() {
  %a = alloca [6 x i8], align 1
  %b = alloca [6 x i16], align 2
  call void @llvm.memset.p0.i64(ptr %a, i8 5, i64 6, i1 false)
  call void @llvm.memset.p0.i64(ptr %b, i8 0, i64 12, i1 false)
  call void @test(ptr %a, ptr %b, i64 6)
  %p3 = getelementptr inbounds i16, ptr %b, i64 3
  %b0 = load i16, ptr %b, align 2
  %b3 = load i16, ptr %p3, align 2
  %sum = add i16 %b0, %b3
  %ok = icmp eq i16 %sum, 10
  %ret = select i1 %ok, i32 7, i32 9
  ret i32 %ret
}

declare void @llvm.memset.p0.i64(ptr, i8, i64, i1)
```

```console
$ opt -S -passes=loop-vectorize -force-vector-interleave=8 -epilogue-vectorization-force-VF=2 repro.ll -o out.ll
$ llubi repro.ll; echo $?
7
$ llubi out.ll
#0   %54 = load i8, ptr %52, align 1 at @test
#1 call void @test(ptr %a, ptr %b, i64 6) at @main
Immediate UB detected: Memory access is out of bounds. Accessed size: 1, Address: 0xE, Object base: 0x8, Object size: 6.
```

With only one of the two options, or neither, `out.ll` still returns 7.

### Why the epilogue loop never exits

`out.ll`, abridged. `%2` is the trip count. The main plan is scalar with an interleave count of 8, and the epilogue plan has VF 2:

```llvm
iter.check:
  %min.iters.check = icmp ult i64 %2, 2 ; (1)
  br i1 %min.iters.check, label %vec.epilog.scalar.ph, label %vector.main.loop.iter.check

vector.main.loop.iter.check:
 %min.iters.check1 = icmp ult i64 %2, 8         ; (2)
  br i1 %min.iters.check1, label %vec.epilog.ph, label %vector.ph

vec.epilog.ph:
 %vec.epilog.resume.val = phi i64 [ %n.vec, %vec.epilog.iter.check ], [ 0, %vector.main.loop.iter.check ]
  %46 = and i64 %2, 1
  %47 = icmp eq i64 %46, 0
  %48 = select i1 %47, i64 2, i64 %46
  %n.vec2 = sub i64 %2, %48 ; (3)

vec.epilog.vector.body:
  %index.next4 = add nuw i64 %index3, 2
  %63 = icmp eq i64 %index.next4, %n.vec2        ; (4)
  br i1 %63, label %vec.epilog.middle.block, label %vec.epilog.vector.body
```

For a trip count of 2:

1. `2 u< 2` is false, so the vector code is entered.
2. `2 u< 8` is true, so the main vector loop is skipped and the epilogue vector loop starts at index 0.
3. The epilogue plan requires a scalar epilogue (the accesses step by 3, so the interleave group has gaps), so its vector trip count keeps at least one iteration back: `%n.vec2 = 2 - 2 = 0`.
4. After one vector iteration the index is 2. `2 == 0` is false, and it stays false.

Check (1) should be `ule`: with a required scalar epilogue, a trip count equal to the epilogue VF leaves nothing for the epilogue vector loop. The other entry to the epilogue, `vec.epilog.iter.check` after the main vector loop, already uses `ule`.

### Notes

- Reproduces with `opt` (trunk) on godbolt, with `opt` and `llubi` built at 84a7a7d12ccc, and from C with `clang` at 1bd1774757f8 (`-O3 -mllvm -force-vector-interleave=8 -mllvm -epilogue-vectorization-force-VF=2`, SIGSEGV for 2 iterations).
- I have not found a way to reach this without `-epilogue-vectorization-force-VF`.

### Related

#213904 (open) includes a change to check (1): it uses the epilogue plan's `requiresScalarEpilogue()` instead of the main plan's.

- With only that change applied locally, the reproducer returns 7 and `check-llvm` passes.
- I have not built #213904 as a whole.
- The problem is on trunk today, without that PR.

I have a regression test for this case (scalar main plan, epilogue VF 2) and can share it.

This test case was generated by a fuzzer, and the report was written with LLM assistance.
_______________________________________________
llvm-bugs mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-bugs

Reply via email to