https://gcc.gnu.org/bugzilla/show_bug.cgi?id=127711
Bug ID: 127711
Summary: [AArch64][SVE] GCC fails to vectorize large-stride AoS
access due to complicated access pattern
Product: gcc
Version: 16.1.0
Status: UNCONFIRMED
Severity: normal
Priority: P3
Component: tree-optimization
Assignee: unassigned at gcc dot gnu.org
Reporter: bug_hunters at yeah dot net
Target Milestone: ---
GCC fails to vectorize the following loop on AArch64 SVE and reports a
`complicated access pattern`. Clang considers the same loop legal for
vectorization and generates SVE code when vectorization is explicitly enabled.
### Test case
```c
typedef struct {
char a;
char b;
} S;
void foo(const S *__restrict__ x,
char *__restrict__ out,
int n)
{
for (int i = 0; i < n; i += 2049)
out[i] = x[i].a + x[i].b;
}
```
### GCC version
```text
aarch64-unknown-linux-gnu-g++ (GCC) 16.1.0
```
### GCC options
```text
-O3 -march=armv9-a+sve -ftree-vectorize -fopt-info-vec-all
```
The same failure is observed with `-fvect-cost-model=unlimited` and
`-ffast-math`.
### GCC output
```text
<source>:10:23: missed: couldn't vectorize loop
<source>:11:23: missed: not vectorized: complicated access pattern.
<source>:6:6: note: vectorized 0 loops in function.
<source>:11:25: note: ***** Analysis failed with vector mode VNx16QI
<source>:11:25: note: ***** The result for vector mode VNx16QI would be the
same
<source>:11:25: note: ***** Re-trying analysis with vector mode VNx8QI
<source>:11:25: note: ***** Analysis failed with vector mode VNx8QI
<source>:11:25: note: ***** Re-trying analysis with vector mode VNx4QI
<source>:11:25: note: ***** Analysis failed with vector mode VNx4QI
<source>:11:25: note: ***** Re-trying analysis with vector mode VNx2QI
<source>:11:25: note: ***** Analysis failed with vector mode VNx2QI
<source>:11:25: note: ***** Re-trying analysis with vector mode V16QI
<source>:11:25: note: ***** Analysis failed with vector mode V16QI
<source>:11:25: note: ***** Re-trying analysis with vector mode V8QI
<source>:11:25: note: ***** Analysis failed with vector mode V8QI
<source>:11:25: note: ***** The result for vector mode V4HI would be the same
<source>:11:25: note: ***** Re-trying analysis with vector mode V2SI
<source>:11:25: note: ***** Analysis failed with vector mode V2SI
```
GCC therefore rejects the loop during vectorization analysis because it
considers the memory access pattern complicated.
### Clang result
With Clang 22.1.0 and the following options:
```text
-S -O3 -ftree-vectorize -ftree-slp-vectorize --target=aarch64-linux-gnu
-march=armv9-a+sve -Rpass='.*vectorize.*' -Rpass-missed='.*vectorize.*'
-Rpass-analysis='.*vectorize.*'
```
adding
```c
#pragma clang loop vectorize(enable)
```
before the loop causes Clang to vectorize it successfully.
The generated SVE code constructs the large-stride indices and performs indexed
loads from the two structure fields, followed by vector addition and indexed
stores. For example:
```asm
mov w13, #2049
index z0.d, #0, x13
...
lsl z17.d, z0.d, #1
add x13, x0, #1
...
ld1b { z18.d }, p0/z, [x0, z17.d]
...
ld1b { z17.d }, p0/z, [x13, z17.d]
...
add z17.b, z17.b, z18.b
...
st1b { z20.d }, p0, [x1, z0.d]
```
Here, `index z0.d, #0, x13` constructs indices with a step of 2049, while the
two indexed loads access the two adjacent `char` fields of the structure.
This suggests that Clang considers the loop feasible for vectorization and that
the required indexed memory operations are supported by the target.
### Additional observation
The failure is sensitive to the loop stride. With smaller strides, GCC may
follow a different vectorization-analysis path and report `unsupported SLP
instances`, while a sufficiently large stride triggers the `complicated access
pattern` diagnostic.
The test case contains only two adjacent `char` fields and integer operations,
so the failure does not depend on floating-point semantics or type conversions.
This case may be related to instr-dataflow during feasibility analysis.
### Actual behavior
GCC fails to vectorize the loop and reports:
```text
missed: couldn't vectorize loop
missed: not vectorized: complicated access pattern.
```
even though Clang is able to generate SVE code for the same access pattern when
vectorization is explicitly enabled.
Live examples reproducing the behavior are available on Compiler Explorer:
GCC: https://godbolt.org/z/58fcc8fK9
Clang: https://godbolt.org/z/xhx4MfhKW