llvm / llvm/llvm-project

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

Open
#192,446 1 comment 0 reactions 0 assignees View on GitHub
backend:AArch64 missed-optimization SVE vectorizers
Dominant language
LLVM
Stars
40.5k
Forks
18.7k
PR merge metrics
PR metrics pending

Description

**Description:**

Clang fails to auto-vectorize a loop that accesses multiple struct members with a stride of -18 on AArch64 with SVE enabled, while GCC successfully vectorizes the same loop under identical compilation flags.

The loop iterates backwards with a decrement of 18 and performs reduction operations on multiple fields from two different struct types. Clang's vectorizer reports that it cannot identify the reduction value and aborts vectorization entirely. In contrast, GCC's vectorizer successfully handles this interleaved struct access pattern and generates vectorized code using variable-length SVE vectors after trying multiple vector modes.

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

typedef struct {
int m0;
int m1;
} element_0;

typedef struct {
int m0;
int m1;
int m2;
} element_1;

float foo(
const element_0 * __restrict__ a,
const element_1 * __restrict__ b,
int n
) {
float sum = 0;
for (int i = n - 1; i >= 0; i -= 18)
{
int idx = i;
if (i < n/2) {
sum += a[idx].m0 * 1.0;
sum += a[idx].m1 * 1.0;
sum += b[idx].m0 * 1.0;
sum += b[idx].m1 * 1.0;
sum += b[idx].m2 * 1.0;
}
}
return 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:21:5: remark: loop not vectorized: value that could not be identified as reduction is used outside the loop [-Rpass-analysis=loop-vectorize]
21 | for (int i = n - 1; i >= 0; i -= 18)
| ^
test.c:21:5: remark: loop not vectorized [-Rpass-missed=loop-vectorize]
```
Also reproducible on Godbolt (https://godbolt.org/z/KoqcKbTj9), 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:21:27: missed: couldn't vectorize loop
test.c:26:17: missed: not vectorized: unsupported use in stmt.
test.c:21:27: optimized: loop vectorized using variable length vectors
test.c:15:7: note: vectorized 1 loops in function.
test.c:15:7: note: ***** Analysis failed with vector mode VNx4SI
test.c:15:7: note: ***** The result for vector mode VNx16QI would be the same
test.c:15:7: note: ***** The result for vector mode VNx8QI would be the same
test.c:15:7: note: ***** The result for vector mode VNx4QI would be the same
test.c:15:7: note: ***** Re-trying analysis with vector mode VNx2QI
test.c:15:7: note: ***** Analysis failed with vector mode VNx2QI
test.c:15:7: note: ***** Re-trying analysis with vector mode V16QI
test.c:15:7: note: ***** Analysis failed with vector mode V16QI
test.c:15:7: note: ***** The result for vector mode V8QI would be the same
test.c:15:7: note: ***** The result for vector mode V4HI would be the same
test.c:15:7: note: ***** Re-trying analysis with vector mode V2SI
test.c:15:7: note: ***** Analysis failed with vector mode V2SI
```

Also reproducible on Godbolt: https://godbolt.org/z/vKezoWedh

Contributor guide

Open the contributing guide

Research direction

Start by compiling the provided C test case with Clang's AArch64 SVE options and vectorization remarks, then compare the loop-vectorizer reduction analysis with the GCC and Godbolt results. No repository file or test path is named; done means Clang reports the loop as vectorized for this case and the behavior is covered by an appropriate regression test.

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
48/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.