llvm / llvm/llvm-project

[AArch64][SVE] Missed vectorization opportunity for loop with non-unit stride compared to GCC

Open
#192,447 3 comments 0 reactions 0 assignees View on GitHub
backend:AArch64 missed-optimization SVE
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 -33 on AArch64 with SVE enabled, while GCC successfully vectorizes the same loop under identical compilation flags.

The loop iterates backwards with a decrement of 33 and contains a conditional path based on n/2. Clang's vectorizer reports that it cannot identify the reduction value and aborts vectorization entirely. In contrast, GCC's vectorizer successfully handles this pattern and generates vectorized code using variable-length SVE vectors after trying multiple vector modes.

**Test case:**
```c
#include
#include

char foo(
const float * __restrict__ a,
const short * __restrict__ b,
int n
) {
char sum = 0;
for (int i = n - 1; i >= 0; i -= 33)
{
int idx = i;
if (i < n/2) {
sum += (char)a[idx] * 1.0;
sum += (char)b[idx] * 1.0;
}
}
return (char)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:10:5: remark: loop not vectorized: value that could not be identified as reduction is used outside the loop [-Rpass-analysis=loop-vectorize]
10 | for (int i = n - 1; i >= 0; i -= 33)
| ^
test.c:10:5: remark: loop not vectorized [-Rpass-missed=loop-vectorize]
```

Also reproducible on Godbolt (https://godbolt.org/z/nEKj7ePzs), as well as with Clang trunk (version 23.0.0git, 326a9fa563).

**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:10:27: missed: couldn't vectorize loop
test.c:14:13: missed: not vectorized: unsupported use in stmt.
test.c:10:27: optimized: loop vectorized using variable length vectors
test.c:4:6: note: vectorized 1 loops in function.
test.c:4:6: note: ***** Analysis failed with vector mode VNx4SF
test.c:4:6: note: ***** The result for vector mode VNx16QI would be the same
test.c:4:6: note: ***** The result for vector mode VNx8QI would be the same
test.c:4:6: note: ***** Re-trying analysis with vector mode VNx4QI
test.c:4:6: note: ***** Analysis failed with vector mode VNx4QI
test.c:4:6: note: ***** Re-trying analysis with vector mode VNx2QI
test.c:4:6: note: ***** Analysis failed with vector mode VNx2QI
test.c:4:6: note: ***** Re-trying analysis with vector mode V16QI
test.c:4:6: note: ***** Analysis failed with vector mode V16QI
test.c:4:6: note: ***** The result for vector mode V8QI would be the same
test.c:4:6: note: ***** Re-trying analysis with vector mode V4HI
test.c:4:6: note: ***** Analysis failed with vector mode V4HI
test.c:4:6: note: ***** Re-trying analysis with vector mode V2SI
test.c:4:6: note: ***** Analysis failed with vector mode V2SI
```
Also reproducible on Godbolt: https://godbolt.org/z/foW9vP63s

Contributor guide

Open the contributing guide

Research direction

Start by compiling the supplied C test case with the listed AArch64 SVE options and inspect the loop-vectorizer remarks. Trace the reduction-analysis path that emits “value that could not be identified as reduction” and compare it with GCC’s result; done means Clang vectorizes this loop for SVE.

Written by the indexing model from the issue text.

Assessment

Tech stack
c
Domain
compilers, performance
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Quiet
Clarity
Mostly clear
Newbie friendliness
45/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.