https://gcc.gnu.org/bugzilla/show_bug.cgi?id=116768
Bug ID: 116768 Summary: Strict aliasing breaks autovectorization with -O3 Product: gcc Version: 14.2.0 Status: UNCONFIRMED Severity: normal Priority: P3 Component: tree-optimization Assignee: unassigned at gcc dot gnu.org Reporter: imachug at gmail dot com Target Milestone: --- This returns 0 (wrong) with strict aliasing enabled and 1 (correct) with strict aliasing disabled. Looks like a bug to me (no casts, sanitizers are silent, the example is a minimization of an std::bitset-based reproducer). -O3 -mavx is required to trigger the bug. I believe this is a bug in TBAA, because defining Parent to Child or replacing `y_child->` with `y->child.` fixes the miscompilation. A quick check with Godbolt shows the code is reduced to 'return 0' by the last tree pass, so I'm tentatively labeling this tree-optimization. This can be reproduced starting with 11.2 up to trunk. https://godbolt.org/z/1v16bPdfv ``` typedef struct { unsigned long words[2]; } Child; typedef struct { Child child; } Parent; Parent my_or(Parent x, const Parent *y) { const Child *y_child = &y->child; for (int i = 0; i < 2; i++) { x.child.words[i] |= y_child->words[i]; } return x; } int main() { Parent bs[4]; __builtin_memset(bs, 0, sizeof(bs)); bs[0].child.words[0] = 1; for (int i = 1; i <= 3; i++) { bs[i] = my_or(bs[i], &bs[i - 1]); } return bs[2].child.words[0]; } ``` Here's -v for my local compiler if you find it useful. Using built-in specs. COLLECT_GCC=gcc COLLECT_LTO_WRAPPER=/usr/lib/gcc/x86_64-pc-linux-gnu/14.2.1/lto-wrapper Target: x86_64-pc-linux-gnu Configured with: /build/gcc/src/gcc/configure --enable-languages=ada,c,c++,d,fortran,go,lto,m2,objc,obj-c++,rust --enable-bootstrap --prefix=/usr --libdir=/usr/lib --libexecdir=/usr/lib --mandir=/usr/share/man --infodir=/usr/share/info --with-bugurl=https://gitlab.archlinux.org/archlinux/packaging/packages/gcc/-/issues --with-build-config=bootstrap-lto --with-linker-hash-style=gnu --with-system-zlib --enable-__cxa_atexit --enable-cet=auto --enable-checking=release --enable-clocale=gnu --enable-default-pie --enable-default-ssp --enable-gnu-indirect-function --enable-gnu-unique-object --enable-libstdcxx-backtrace --enable-link-serialization=1 --enable-linker-build-id --enable-lto --enable-multilib --enable-plugin --enable-shared --enable-threads=posix --disable-libssp --disable-libstdcxx-pch --disable-werror Thread model: posix Supported LTO compression algorithms: zlib zstd gcc version 14.2.1 20240805 (GCC) COLLECT_GCC_OPTIONS='-v' '-save-temps' '-O3' '-mavx' '-mtune=generic' '-march=x86-64' '-dumpdir' 'a-' /usr/lib/gcc/x86_64-pc-linux-gnu/14.2.1/cc1 -E -quiet -v test.c -mavx -mtune=generic -march=x86-64 -O3 -fpch-preprocess -o a-test.i ignoring nonexistent directory "/usr/lib/gcc/x86_64-pc-linux-gnu/14.2.1/../../../../x86_64-pc-linux-gnu/include" #include "..." search starts here: #include <...> search starts here: /usr/lib/gcc/x86_64-pc-linux-gnu/14.2.1/include /usr/local/include /usr/lib/gcc/x86_64-pc-linux-gnu/14.2.1/include-fixed /usr/include End of search list. COLLECT_GCC_OPTIONS='-v' '-save-temps' '-O3' '-mavx' '-mtune=generic' '-march=x86-64' '-dumpdir' 'a-' /usr/lib/gcc/x86_64-pc-linux-gnu/14.2.1/cc1 -fpreprocessed a-test.i -quiet -dumpdir a- -dumpbase test.c -dumpbase-ext .c -mavx -mtune=generic -march=x86-64 -O3 -version -o a-test.s GNU C17 (GCC) version 14.2.1 20240805 (x86_64-pc-linux-gnu) compiled by GNU C version 14.2.1 20240805, GMP version 6.3.0, MPFR version 4.2.1, MPC version 1.3.1, isl version isl-0.26-GMP GGC heuristics: --param ggc-min-expand=100 --param ggc-min-heapsize=131072 Compiler executable checksum: faa3163d33b78b77071c76eebeab3034 COLLECT_GCC_OPTIONS='-v' '-save-temps' '-O3' '-mavx' '-mtune=generic' '-march=x86-64' '-dumpdir' 'a-' as -v --64 -o a-test.o a-test.s GNU assembler version 2.43.0 (x86_64-pc-linux-gnu) using BFD version (GNU Binutils) 2.43.0 COMPILER_PATH=/usr/lib/gcc/x86_64-pc-linux-gnu/14.2.1/:/usr/lib/gcc/x86_64-pc-linux-gnu/14.2.1/:/usr/lib/gcc/x86_64-pc-linux-gnu/:/usr/lib/gcc/x86_64-pc-linux-gnu/14.2.1/:/usr/lib/gcc/x86_64-pc-linux-gnu/ LIBRARY_PATH=/usr/lib/gcc/x86_64-pc-linux-gnu/14.2.1/:/usr/lib/gcc/x86_64-pc-linux-gnu/14.2.1/../../../../lib/:/lib/../lib/:/usr/lib/../lib/:/usr/lib/gcc/x86_64-pc-linux-gnu/14.2.1/../../../:/lib/:/usr/lib/ COLLECT_GCC_OPTIONS='-v' '-save-temps' '-O3' '-mavx' '-mtune=generic' '-march=x86-64' '-dumpdir' 'a.' /usr/lib/gcc/x86_64-pc-linux-gnu/14.2.1/collect2 -plugin /usr/lib/gcc/x86_64-pc-linux-gnu/14.2.1/liblto_plugin.so -plugin-opt=/usr/lib/gcc/x86_64-pc-linux-gnu/14.2.1/lto-wrapper -plugin-opt=-fresolution=a.res -plugin-opt=-pass-through=-lgcc -plugin-opt=-pass-through=-lgcc_s -plugin-opt=-pass-through=-lc -plugin-opt=-pass-through=-lgcc -plugin-opt=-pass-through=-lgcc_s --build-id --eh-frame-hdr --hash-style=gnu -m elf_x86_64 -dynamic-linker /lib64/ld-linux-x86-64.so.2 -pie /usr/lib/gcc/x86_64-pc-linux-gnu/14.2.1/../../../../lib/Scrt1.o /usr/lib/gcc/x86_64-pc-linux-gnu/14.2.1/../../../../lib/crti.o /usr/lib/gcc/x86_64-pc-linux-gnu/14.2.1/crtbeginS.o -L/usr/lib/gcc/x86_64-pc-linux-gnu/14.2.1 -L/usr/lib/gcc/x86_64-pc-linux-gnu/14.2.1/../../../../lib -L/lib/../lib -L/usr/lib/../lib -L/usr/lib/gcc/x86_64-pc-linux-gnu/14.2.1/../../.. a-test.o -lgcc --push-state --as-needed -lgcc_s --pop-state -lc -lgcc --push-state --as-needed -lgcc_s --pop-state /usr/lib/gcc/x86_64-pc-linux-gnu/14.2.1/crtendS.o /usr/lib/gcc/x86_64-pc-linux-gnu/14.2.1/../../../../lib/crtn.o COLLECT_GCC_OPTIONS='-v' '-save-temps' '-O3' '-mavx' '-mtune=generic' '-march=x86-64' '-dumpdir' 'a.'