[AArch64][SVE] Missed vectorization opportunity for loop with non-unit stride and early break condition compared to GCC
- Dominant language
- LLVM
- Stars
- 40.5k
- Forks
- 18.7k
- PR merge metrics
- PR metrics pending
Description
**Description:**
Clang fails to auto-vectorize a loop with a stride of -18 and an early break condition on AArch64 with SVE enabled, while GCC successfully vectorizes the same loop under identical compilation flags.
The loop in question iterates backwards with a decrement of 18 and contains a conditional `break` when `i % 10 == 0`. GCC's vectorizer is able to handle this pattern and generates vectorized code using 16-byte vectors after loop versioning for alignment. In contrast, Clang reports "Cannot vectorize uncountable loop" and aborts vectorization entirely.
**Test case:**
```c
#include
#include
double foo(
const int * _restrict_ a,
const int * _restrict_ b,
const float * _restrict_ c,
int n
) {
int sum = 0;
for (int i = n - 1; i >= 0; i -= 18)
{
int idx = i;
sum += (int)a[idx];
sum += (int)b[idx];
sum += (int)c[idx];
if (i % 10 == 0) {
break;
}
}
return (double)sum;
}
```
**clang version:**
```
clang version 21.1.1
Target: unknown
Thread model: posix
Build config: +unoptimized, +assertions
```
**Clang options:**
```
-S -O3 -ftree-vectorize -ftree-slp-vectorize --target=aarch64-linux-gnu -march=armv8-a+sve -Rpass=.*vectorize.* -Rpass-missed=.*vectorize.* -Rpass-analysis=.*vectorize.*
```
**The result of Clang:**
```
test.c:11:5: remark: loop not vectorized: Cannot vectorize uncountable loop [-Rpass-analysis=loop-vectorize]
11 | for (int i = n - 1; i >= 0; i -= 18)
| ^
test.c:11:5: remark: loop not vectorized [-Rpass-missed=loop-vectorize]
```
Also reproducible on Godbolt : https://godbolt.org/z/n3T3qWdT3
**However, GCC vectorizes it.** GCC version 15.2.0.
**gcc version:**
```
aarch64-linux-gnu-gcc (GCC) 15.2.0
Copyright (C) 2025 Free Software Foundation, Inc.
This is free software; see the source for copying conditions. There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
```
**GCC options:**
```
-S -march=armv8-a+sve -ftree-vectorize -O3 -fopt-info-vec-all
```
**The result of GCC:**
```
test.c:11:27: optimized: loop vectorized using 16 byte vectors
test.c:11:27: optimized: loop versioned for vectorization to enhance alignment
test.c:4:8: note: vectorized 1 loops in function.
test.c:16:13: note: ***** Analysis failed with vector mode VNx4SI
test.c:16:13: note: ***** The result for vector mode VNx16QI would be the same
test.c:16:13: note: ***** The result for vector mode VNx8QI would be the same
test.c:16:13: note: ***** The result for vector mode VNx4QI would be the same
test.c:16:13: note: ***** Re-trying analysis with vector mode VNx2QI
test.c:16:13: note: ***** Analysis failed with vector mode VNx2QI
test.c:16:13: note: ***** Re-trying analysis with vector mode V16QI
test.c:16:22: note: ***** Analysis failed with vector mode V16QI
test.c:16:22: note: ***** The result for vector mode V8QI would be the same
test.c:16:22: note: ***** The result for vector mode V4HI would be the same
test.c:16:22: note: ***** Re-trying analysis with vector mode V2SI
test.c:16:22: note: ***** Analysis failed with vector mode V2SI
```
Also reproducible on Godbolt : https://godbolt.org/z/1dcb14z77
Contributor guide
Research direction
Start by reproducing the supplied C test case with Clang's AArch64 SVE options and vectorization remarks, then trace the loop-vectorizer analysis that reports "Cannot vectorize uncountable loop." Compare the result with GCC's vectorized output; done means Clang can handle this loop pattern or the limitation is documented with a clear diagnostic.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- c
- Domain
- compilers
- Issue type
- Bug
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Activity status
- Quiet
- Clarity
- Mostly clear
- Newbie friendliness
- 48/100