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

            Bug ID: 128743
           Summary: wrong code at -O2: dependence analysis treats fields
                    of same-type struct objects at different bases as
                    independent under strict aliasing, losing an output
                    dependence, since r8-2390
           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

#include <cstdio>
#include <new>
struct Q { float x; int y; };
int n = 4;
static void f(unsigned char *b) {
  for (int k = 0; k < n; k++) {
    new (b + 4 + k * 8) Q{1.0f, k};
    new (b + k * 8) Q{2.0f, 7};
  }
}
int main() {
  unsigned char *b = new unsigned char[64];
  f(b);
  Q *q = std::launder((Q *)(b + 8));
  printf("%d %d\n", (int)q->x, q->y);
}

$ g++ -O0 t.cpp && ./a.out
2 7
$ g++ -O2 t.cpp && ./a.out
0 7

int printf(const char *, ...);
void *malloc(unsigned long);
struct Q { float x; int y; };
int n = 4;
static void f(unsigned char *b) {
  for (int k = 0; k < n; k++) {
    *(struct Q *)(b + 4 + k * 8) = (struct Q){1.0f, k};
    *(struct Q *)(b + k * 8) = (struct Q){2.0f, 7};
  }
}
int main(void) {
  unsigned char *b = malloc(64);
  f(b);
  struct Q *q = (struct Q *)(b + 8);
  printf("%d %d\n", (int)q->x, q->y);
}

$ gcc -O0 t.c && ./a.out
2 7
$ gcc -O2 t.c && ./a.out
0 7

-O2 (since 15) and -O3 (since 8) fail; -fno-strict-aliasing and
-fno-tree-loop-vectorize give 2 7. With a constant trip count (k < 4) the
result is right.

Expected 2 7: bytes b+8..b+11 get Q1.y = 0 in iteration 0 and then Q2.x = 2.0f
in iteration 1 (a new Q object at b+8 that ends the old one). The vectorized
loop does the int stores of Q1.y after the float stores of the next iteration,
so 0 survives.

Cause: initialize_data_dependence_relation (tree-data-ref.cc). The bases
MEM[(struct Q *)b + 4 + 8k] and MEM[(struct Q *)b + 8k] differ (!same_base_p),
and the code assumes:

      /* Partial overlap is possible for different bases when strict aliasing
         is not in effect.  It's also possible if either base involves a union
         access; ...  */
      if (!flag_strict_aliasing
          || ref_contains_union_access_p (full_seq.object_a)
          || ref_contains_union_access_p (full_seq.object_b))

So under -fstrict-aliasing the field subscripts (.y at 32 bits vs .x at 0) give
a ZIV "no dependence". That is not valid for output (and anti) dependences: a
store can end an object and start a new one of the same type at an overlapping
address (placement new in C++, a new effective type in malloc memory in C).
This is the same principle as in PR128626 (TBAA is wrong for the
positive-distance case), but here both refs are stores and the fault is
earlier, in the common data-ref analysis. With struct Q { int x; int y; } the
stores are grouped and the result is right.

The different-base struct_seq path and this premise came with
r8-2390-gdfbddbeb1ca ("Handle data dependence relations with different bases",
2017); 7.5 is correct, 8.5 fails.

-O0 -fsanitize=undefined,address is clean (C and C++).

gcc -Wall -Wextra gives no warnings. -fno-strict-aliasing hides it, as expected
for a TBAA bug. The program is still valid: in C every store is an lvalue of
struct Q into malloc memory, which gives those bytes a new effective type (C11
6.5p6), and in C++ each placement new ends the lifetime of the object it
overlaps; the reads use the type of the last store.

Possible fix: One possible fix, which we tested locally (not bootstrapped), is
to return an unknown dependence on this different-base path of
initialize_data_dependence_relation when either reference is a write. It fixes
this test, but it costs vectorization (gcc.dg/vect/vect-alias-check-3.c goes
from 12 to 1 vectorized loop in our run, and vect-alias-check-5.c keeps an
alias check), so a real fix should keep the cases where whole-object identity
or disjoint storage is proven.

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/128743] N... jeffchen006 at gmail dot com via Gcc-bugs

Reply via email to