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

            Bug ID: 128753
           Summary: [15/16/17 Regression] wrong code at -O3: vectorizer
                    hoists an invariant load past a store that writes it
                    in an earlier iteration (alias check WAR, step-0
                    segment)
           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: jeffchen006 at gmail dot com
  Target Milestone: ---
            Target: x86_64-pc-linux-gnu

int printf(const char *, ...);
void *calloc(unsigned long, unsigned long);
struct C { float f0, f1; int i, j; };
int x = 1;
int main(void) {
  char *b = (char *)calloc(10, 16) + 16;
  for (int k = 0; k < (x & 3) + 1; k++) {
    *(long *)(b + 16 * (2 - k)) = *(long *)(b + 16 * 3);
    *(struct C *)(b + 16 * (5 + k)) = (struct C){ 0, 0, 0, 0 };
    *(long *)(b + 16 * (3 + k)) = 3;
  }
  printf("%d\n", b[16]);
}

$ gcc -O0 t.c && ./a.out
3
$ gcc -O3 t.c && ./a.out
0

-O3 fails; -O2, -O3 -fno-tree-loop-vectorize, -fno-vect-cost-model and
-fno-tree-ch give 3. -fno-strict-aliasing does not help.

Expected 3: k runs 0 and 1 (b points 16 bytes into a 160-byte calloc block, so
every access is in bounds even for the k values that do not run). At k = 0 the
loop stores 3 at b + 48; at k = 1 the load of b + 48 must see it and copy 3 to
b + 16.

The vect dump (judge3) has a runtime alias check for the pair:

  reference: MEM <unsigned long> [(char * {ref-all})b_22 + 48B] vs. MEM[(long
int *)_12]
  segment length: 0 vs. ((sizetype)((unsigned int)_34 + 1) +
18446744073709551615) * 16
  access size:    8 vs. 48
  flags:          WAR ARBITRARY

The long store's range is merged with the struct C store range, and the
invariant load (segment length 0) is hoisted to the prologue. The pair is
treated as WAR (the load comes before the store in one iteration), but the
store in iteration 0 feeds the load in iteration 1 (a RAW across iterations),
so the invariant load may only be hoisted if no store in the loop can write its
address. The check lets this overlap pass.

Suspected cause (not confirmed in the code): runtime alias versioning in
tree-vect-data-refs.cc (vect_prune_runtime_alias_test_list and the segment /
WAR handling) together with hoisting of the VMAT_INVARIANT load. 14.3 is
correct, 15.2 fails (not bisected). PR126587 (aarch64, VMAT_INVARIANT load
hoisted because no alias check is emitted under the VF == 1 rule) has the same
age and the same hoisting; here a check is emitted but does not catch the
overlap. The maintainer may merge them.

-O0 -fsanitize=undefined,address is clean.

gcc -Wall -Wextra gives no warnings. -fno-strict-aliasing -fwrapv does not
change the output.

Possible fix: We are not sure of the right fix. A minimal guard would be to not
hoist an invariant (VMAT_INVARIANT) load out of the vectorized loop when a
store in the loop may write its address, unless the runtime alias check covers
the full store range of all iterations; or to treat such a pair as RAW, not
WAR, when the store can reach the load address in an earlier iteration.

gcc version 17.0.0 20261114 (experimental), r17-6275-gfceddda853f,
x86_64-pc-linux-gnu
Configured with: --enable-languages=c,c++ --disable-multilib
--disable-bootstrap --enable-checking=release
  • [Bug tree-optimization/128753] N... jeffchen006 at gmail dot com via Gcc-bugs

Reply via email to