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

            Bug ID: 129256
           Summary: [14/15/16/17 Regression] wrong code at -O2: outer-loop
                    vectorization ignores the dependence between stores
                    p[i + j] of different outer iterations, since r12-4240
           Product: gcc
           Version: 17.0
            Status: UNCONFIRMED
          Keywords: wrong-code
          Severity: normal
          Priority: P3
         Component: tree-optimization
          Assignee: unassigned at gcc dot gnu.org
          Reporter: haoxintu at gmail dot com
  Target Milestone: ---

Hi,

$cat repro.c

#include <stdio.h>

unsigned B[19];

__attribute__((noinline)) void
f (unsigned *p, unsigned k)
{
  for (int i = 0; i < 8; i++)
    for (int j = 0; j < 12; j++)
      p[i + j] = k * i;
}

int
main (void)
{
  f (B, 1);
  for (int n = 0; n < 19; n++)
    printf ("%u ", B[n]);
  printf ("\n");
  fflush (stdout);
  /* the last store to p[n] comes from the largest i with i <= n and n - i <
12, so p[n] = min (n, 7) */
  for (int n = 0; n < 19; n++)
    if (B[n] != (n < 7 ? n : 7))
      __builtin_abort ();
  return 0;
}

$ gcc -O0 repro.c && ./a.out        # also -O1, -O3, -Os
0 1 2 3 4 5 6 7 7 7 7 7 7 7 7 7 7 7 7
$ gcc -O2 repro.c && ./a.out
0 0 0 0 4 4 4 4 4 4 4 4 4 4 4 4 5 6 7
Aborted
$ gcc -O2 -fno-tree-loop-vectorize repro.c && ./a.out
0 1 2 3 4 5 6 7 7 7 7 7 7 7 7 7 7 7 7

The program should have no UB (one 19-element array written at indices 0..18;
clean under -fsanitize=address,undefined).

Godbolt (gcc trunk, -O2 aborts, -O0 not): https://godbolt.org/z/TK73Ynaaj

## Regression point

r12-4240 (2b8453c401b, "Enable auto-vectorization at O2 with very-cheap cost
model.", liuhongt, 2021-09-06):
https://gcc.gnu.org/git/?p=gcc.git;a=commit;h=2b8453c401b699ed93c085d0413ab4b5030bcdb8

That is where the default -O2 starts to fail; the outer-loop vectorization
error itself is much older: GCC 11.4 and older fail the same way with `-O2
-ftree-vectorize` (also with `-fvect-cost-model=very-cheap` or `cheap`), see
below. No bisect was run for the underlying error; the -O2 regression point
follows from GCC 11 (-O2 ok, -O2 -ftree-vectorize wrong) vs GCC 12.1 (-O2
wrong).

## Checked versions

- gcc trunk 17.0.0 20261202 (1db3b7e4162, --enable-checking=yes): -O2 wrong;
-O0, -O1, -O3, -Os ok.
- gcc 16.0.1 and 15.2 (local): -O2 wrong, -O3 ok.
- Godbolt gcc 16.1, 15.2, 15.1, 14.4, 14.3, 14.1, 13.4, 13.1, 12.5, 12.1: -O2
aborts; -O3 ok.
- Godbolt gcc 11.4 and local gcc 11.4: -O2 ok; `-O2 -ftree-vectorize` aborts.
- Godbolt gcc 10.5, 9.5, 8.5, 7.5, 6.5: `-O2 -ftree-vectorize` aborts.
- clang trunk -O2: ok.

Thanks.

Reply via email to