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

            Bug ID: 128626
           Summary: wrong code at -O2/-O3: vectorizer ignores
                    anti-dependence between int read and float store to
                    malloc storage
           Product: gcc
           Version: 17.0
            Status: UNCONFIRMED
          Keywords: alias, wrong-code
          Severity: normal
          Priority: P3
         Component: tree-optimization
          Assignee: unassigned at gcc dot gnu.org
          Reporter: jeffchen006 at gmail dot com
  Target Milestone: ---

int printf(const char *, ...);
void *malloc(unsigned long);
int n = 4;
int main(void) {
  int *p = malloc(4 * (4 + 1));
  for (int i = 0; i <= 4; i++) p[i] = i;
  unsigned s = 0;
  for (int k = 0; k < n; k++) {
    ((float *)p)[k] = 1;
    s += p[k + 1];
  }
  printf("%u\n", s);
}

$ gcc -O0 t.c && ./a.out
10
$ gcc -O3 t.c && ./a.out
3196059652

Expected: 10. Each iteration first stores a float to element k and then reads
the int at element k+1, which is not yet overwritten. The sum is 1+2+3+4 = 10.
-O3 reads the float bits of 1.0f.

Suspected cause: vect_analyze_data_ref_dependence: the positive-distance
anti-dependence is dropped because TBAA says int and float do not alias (rule
from PR60012, 2014). The storage is malloc storage, so the dynamic type changes
by the store.

Avoided by: -fno-tree-loop-vectorize, -fno-strict-aliasing.

Known to fail: 8.5.0, 10.5.0, 11.4.0, 12.5.0, 13.4.0, 14.3.0, 15.2.0, 16.2.0,
17.0.

-O0 -fsanitize=undefined,address is clean.

gcc version 17.0.0 20261111 (experimental) (a2a11abfad3), x86_64-pc-linux-gnu
Configured with: --enable-languages=c,c++ --disable-multilib
--disable-bootstrap --enable-checking=release

Reply via email to