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

            Bug ID: 124914
           Summary: [LoopVectorize][AArch64] GCC fails to partially
                    vectorize loop with non-unit stride, multiple
                    conditions, and early exit that Clang vectorizes
           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 a loop with non-unit stride (i += 3), multiple
conditional branches, and an early exit (break) on AArch64, while Clang
successfully performs partial vectorization under the same architecture flags
(-march=armv9-a+sve).

The loop contains:

Stride of 3 (i += 3), requiring interleaved memory access patterns.

A conditional block: if (i > 10) { mac_sum += a[i] * b[i]; }

An early exit: if (i > n/2) { break; }

GCC's vectorizer reports unsupported control flow in loop and aborts
vectorization entirely, generating fully scalar code. In contrast, Clang
partially vectorizes the loop: it vectorizes the steady-state middle region
(where both i > 10 and i <= n/2 hold) with vectorization width 4 and
interleaved count 2, while handling the prologue and epilogue regions with
scalar code.

**Key observations:**

The failure is not due to a single unsupported feature, but the combination of
non-unit stride, multiple conditionals, and early exit.

Clang demonstrates that partial vectorization is feasible and beneficial for
such complex loops.

GCC appears to lack the capability to isolate and vectorize the "safe middle
region" of loops with complex control flow boundaries.

**Test case :**
```c
#include <stdint.h>
#include <stddef.h>
int foo(
    int * __restrict__ a,
    int * __restrict__ b,
    int n
) {
    int mac_sum = 0;
    for (int i = 0; i < n; i += 3)
    {
        if (i > 10) {
        mac_sum += (int)a[i] * (int)b[i];
        }
        if (i > n/2) {
            break;
        }
    }
    return (int)mac_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
```

**The compiler outputs :**
```
<source>:9:23: missed: couldn't vectorize loop
<source>:9:23: missed: not vectorized: unsupported control flow in loop.
<source>:3:5: note: vectorized 0 loops in function.
<source>:12:17: note: ***** Analysis failed with vector mode VNx4SI
<source>:12:17: note: ***** The result for vector mode VNx16QI would be the
same
<source>:12:17: note: ***** The result for vector mode VNx8QI would be the same
<source>:12:17: note: ***** The result for vector mode VNx4QI would be the same
<source>:12:17: note: ***** Re-trying analysis with vector mode VNx2QI
<source>:12:17: note: ***** Analysis failed with vector mode VNx2QI
<source>:12:17: note: ***** Re-trying analysis with vector mode V16QI
<source>:12:17: note: ***** Analysis failed with vector mode V16QI
<source>:12:17: note: ***** The result for vector mode V8QI would be the same
<source>:12:17: note: ***** The result for vector mode V4HI would be the same
<source>:12:17: note: ***** Re-trying analysis with vector mode V2SI
<source>:12:17: note: ***** Analysis failed with vector mode V2SI
```

Also reproducible on Godbolt: 
https://godbolt.org/z/6GKj4Yf1j

**However, Clang vectorizes it.** Clang version 21.1.1.

**clang version:**
```
clang version 21.1.1
Target: unknown
Thread model: posix
Build config: +unoptimized, +assertions
```

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

**The result of Clang:**
```
<source>:12:25: remark: Recipe with invalid costs prevented vectorization at
VF=(vscale x 1): load [-Rpass-analysis=loop-vectorize]
   12 |         mac_sum += (int)a[i] * (int)b[i];
      |                         ^
<source>:12:37: remark: Recipe with invalid costs prevented vectorization at
VF=(vscale x 1): load [-Rpass-analysis=loop-vectorize]
   12 |         mac_sum += (int)a[i] * (int)b[i];
      |                                     ^
<source>:12:30: remark: Recipe with invalid costs prevented vectorization at
VF=(vscale x 1): mul [-Rpass-analysis=loop-vectorize]
   12 |         mac_sum += (int)a[i] * (int)b[i];
      |                              ^
<source>:12:17: remark: Recipe with invalid costs prevented vectorization at
VF=(vscale x 1): add [-Rpass-analysis=loop-vectorize]
   12 |         mac_sum += (int)a[i] * (int)b[i];
      |                 ^
<source>:9:5: remark: vectorized loop (vectorization width: 4, interleaved
count: 2) [-Rpass=loop-vectorize]
    9 |     for (int i = 0; i < n; i += 3)
      |     ^
<source>:12:37: remark: Cannot SLP vectorize list: vectorization was impossible
with available vectorization factors [-Rpass-missed=slp-vectorizer]
   12 |         mac_sum += (int)a[i] * (int)b[i];
      |                                     ^
<source>:3:5: remark: Cannot SLP vectorize list: vectorization was impossible
with available vectorization factors [-Rpass-missed=slp-vectorizer]
    3 | int foo(
      |     ^
```

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

Reply via email to