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

            Bug ID: 129045
           Summary: [vect] wrong code with -march=x86-64-v3: after
                    alignment peeling exactly VF iterations remain, but
                    the vector loop is not skipped although peeling for
                    gaps needs one more scalar iteration
           Product: gcc
           Version: 17.0
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: c
          Assignee: unassigned at gcc dot gnu.org
          Reporter: 0599jiangyc at gmail dot com
  Target Milestone: ---

The following code:

```c
/* GCC trunk (also 12.x-15.x): wrong code at -O3 -march=x86-64-v3
-fno-vect-cost-model
 * (also -O2 -march=x86-64-v3 -fopenmp-simd -DSIMD, no cost-model flag);
correct at -O0, -O1,
 * -O3 without -march, with the default cost model, and with clang.
 * 11 iterations (i = 5..15). Alignment peeling for B takes 3 of them, leaving
exactly VF = 8,
 * but the gap in A[3*i+3] needs one more scalar iteration, so the vector loop
must be skipped.
 * It runs once anyway (B[8..15]) and the scalar epilogue then restarts at i =
8.
 * Godbolt: x86-64 gcc (trunk), flags: -O3 -march=x86-64-v3
-fno-vect-cost-model
 * EXPECTED: prints 1, exit 0.   BUGGY: prints 2, exit 1.
 */
#include <stdio.h>

float A[200], B[200];

__attribute__((noinline)) static void f(long n, long s) {
#ifdef SIMD
#pragma omp simd
#endif
  for (unsigned i = s; i < n; i++) {
    A[3 * i + 3] = A[3 * i + 3] * 3;
    B[i] = B[i] + 1;
  }
}

int main(void) {
  f(16, 5);
  printf("%g\n", B[8]);
  for (int i = 0; i < 200; i++)
    if (B[i] != (i >= 5 && i < 16))
      return 1;
  return 0;
}
```

Resulted in this output:
```
$ gcc -O3 -march=x86-64-v3 -fno-vect-cost-model repro.c && ./a.out
2
(exit 1; also gcc -O2 -march=x86-64-v3 -fopenmp-simd -DSIMD; gcc -O0 and clang
print 1)
```

Trunk: https://godbolt.org/z/6Gs3xKK4f (x86-64 gcc trunk, -O3 -march=x86-64-v3
-fno-vect-cost-model, executed; reproduces as of 2026-11-12; also GCC 12.2 and
15.2)

Reply via email to