https://gcc.gnu.org/bugzilla/show_bug.cgi?id=128742
Bug ID: 128742
Summary: wrong code at -O2: data dependence analysis finds
fields of struct objects at p + 4*k (step smaller than
the struct) independent; vectorizer reorders the
stores (since r184789)
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 S { int a; int b; };
void f(unsigned char *p) {
for (int k = 0; k < 4; k++)
new (p + 4 * k) S{1, 2};
}
int main() {
unsigned char *buf = new unsigned char[20];
f(buf);
S *s = std::launder((S *)(buf + 12));
printf("%d %d\n", s->a, s->b);
}
$ g++ -O0 t.cpp && ./a.out
1 2
$ g++ -O2 t.cpp && ./a.out
2 2
int printf(const char *, ...);
void *malloc(unsigned long);
struct S { int a; int b; };
void f(unsigned char *p) {
for (int k = 0; k < 4; k++)
*(struct S *)(p + 4 * k) = (struct S){1, 2};
}
int main(void) {
unsigned char *buf = malloc(20);
f(buf);
struct S *s = (struct S *)(buf + 12);
printf("%d %d\n", s->a, s->b);
}
$ gcc -O0 t.c && ./a.out
1 2
$ gcc -O2 t.c && ./a.out
2 2
-O2 and -O3 fail; -O1 and -O3 -fno-tree-loop-vectorize give 1 2.
-fno-strict-aliasing does not help (all accesses are int).
Expected 1 2: the object at p + 4k overlaps the one at p + 4(k+1), so the .a
store of iteration k+1 overwrites the .b store of iteration k, and the last
object (bytes 12..19) holds {1, 2}. The vectorized loop stores all .a lanes
first and then all .b lanes, so .b of k = 2 (value 2) ends up in bytes 12..15.
The vect dump compares .a and .b of the same base:
(chrec_b = 32)
(analyze_ziv_subscript
)
(overlap_iterations_a = no dependence)
(overlap_iterations_b = no dependence))
) -> no dependence
Cause: dr_analyze_indices (tree-data-ref.cc), MEM_REF case. For MEM[(struct S
*)p_k].a the field offset (0 or 32 bits) becomes one access function and the
pointer evolution {0, +, 4} another. The code "Adjust the offset so it is a
multiple of the access type size and thus we separate bases that can possibly
be used to produce partial overlaps" (from PR64365) only rounds the initial
offset to a multiple of TYPE_SIZE_UNIT (8). The step (4) is not checked.
initialize_data_dependence_relation then accepts the two dimensions as
independent for the same base, and the ZIV test on the field offsets (0 vs 32)
says "no dependence" before the pointer evolutions are compared. But .b of
iteration k and .a of iteration k+1 are the same bytes. The unconstrained_base
mark is only used in dr_may_alias_p.
This is not a TBAA problem: all stores are int, and -fno-strict-aliasing does
not help. The field/pointer split comes from r184789 (c4ddde1bbb8, PR52406,
2012); PR64365 (r5-6421) added the offset rounding but not a step check.
-O0 -fsanitize=undefined,address is clean (C and C++).
gcc -Wall -Wextra gives no warnings. -fno-strict-aliasing -fwrapv does not
change the output.
Possible fix: One possible fix, which we tested locally (not bootstrapped), is
to accept the field/pointer split in dr_analyze_indices only when every
constant pointer step is a multiple of the object size, and otherwise use the
full byte-address analysis (or an unknown dependence). It fixes this test in C
and C++, and pr52406.c, pr64365.c and pr65206.c still pass. A check of only abs
(step) >= sizeof is not enough: the iterations must stay on the whole-object
grid.
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