https://gcc.gnu.org/bugzilla/show_bug.cgi?id=124913

            Bug ID: 124913
           Summary: [LoopVectorize][AArch64] GCC fails to vectorize
                    reverse-loop conditional operations with forward
                    offset that Clang vectorizes with SVE
           Product: gcc
           Version: 16.0
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: target
          Assignee: unassigned at gcc dot gnu.org
          Reporter: bug_hunters at yeah dot net
  Target Milestone: ---

**Description:**

GCC fails to auto-vectorize **reverse-iteration loops** containing conditional
operations with forward offset access patterns on AArch64, while Clang
successfully vectorizes them using SVE under the same architecture flags
(`-march=armv9-a+sve`).

**Affected patterns:**
1. **Conditional store** with forward offset
2. **Conditional reduction** with forward offset *(also affected)*

Both patterns fail with `unsupported SLP instances` in GCC, while Clang
vectorizes them successfully.

**Notable observations:**
- The failure is specific to the **reverse loop structure** combined with
**forward offset accesses** and **conditional control flow**.
- If the loop direction is changed to forward iteration (`i = 0; i < n; i++`),
GCC vectorizes both patterns successfully.
- If a `float` type is introduced into the expression (triggering
`-ftrapping-math` concerns), GCC's error changes to `unsupported control flow
in loop`, indicating a separate semantic safety check that fails even earlier.

**Test case 1: Conditional store (SoA layout)**
```c
int foo_store(
    int * __restrict__ a,
    int * __restrict__ b,
    int * __restrict__ c,
    int * __restrict__ out,
    int n
) {
    for (int i = n - 1; i >= 0; i -= 1) {
        if ((a[i] != 1)) {
            out[i] = ((int)a[(i + 20)]) + ((int)b[(i + 20)]) + ((int)c[(i +
20)]);
        }
    }
    return 0;
}
```

**Test case 2: Conditional reduction (AoS layout)**
```c
#include <stdint.h>
#include <stddef.h>

typedef struct {
    int m0;
} element_t_0;

int foo(
    const element_t_0 * __restrict__ a,
    int n
) {
    int sum = 0;
    for (int i = n - 1; i >= 0; i -= 1)
    {
        if ((a[i].m0 != 0)) {
            sum += (int)a[(i + 30)].m0;
        }
    }
    return (int)sum;
}
```

**GCC version:**
```
aarch64-unknown-linux-gnu-gcc (GCC) 16.0.1 20260413 (experimental)
Copyright (C) 2026 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.
```

**Compilation options:**
```
-O3 -S -march=armv9-a+sve -ftree-vectorize -fopt-info-vec-all
```

**GCC output (both test cases):**
```
<source>:11:27: missed: couldn't vectorize loop
<source>:11:27: missed: unsupported SLP instances
<source>:4:5: note: vectorized 0 loops in function.
<source>:14:63: note: ***** Analysis failed with vector mode VNx4SI
...
```

**Clang version:**
```
clang version 21.1.1
```

**Clang options:**
```
-O3 -ftree-vectorize -ftree-slp-vectorize --target=aarch64-linux-gnu
-march=armv9-a+sve -ftree-slp-vectorize -Rpass=.*vectorize.* 
-Rpass-missed=.*vectorize.*  -Rpass-analysis=.*vectorize.* 
```

**Clang output for case 1(successful vectorization, both test cases):**
```
...
<source>:8:5: remark: vectorized loop (vectorization width: vscale x 4,
interleaved count: 2) [-Rpass=loop-vectorize]
    8 |     for (int i = n - 1; i >= 0; i -= 1) {
      |     ^
...
```

**Godbolt links:**
- Test case 1 (GCC): https://godbolt.org/z/nqfbzPfqK
- Test case 1 (Clang): https://godbolt.org/z/aGrvaM9jY
- Test case 2 (GCC): https://godbolt.org/z/KsbEecj4P
- Test case 2 (Clang): https://godbolt.org/z/zr1rWs1dz

**Additional context:**
This also affects conditional reduction loops with the same reverse-iteration +
forward-offset pattern, indicating the issue is structural rather than
operation-specific.

Reply via email to