https://gcc.gnu.org/bugzilla/show_bug.cgi?id=80631
Bug ID: 80631 Summary: Compiling with -O3 -mavx2 gives wrong code Product: gcc Version: 6.3.1 Status: UNCONFIRMED Severity: normal Priority: P3 Component: c Assignee: unassigned at gcc dot gnu.org Reporter: elias.rudberg at it dot uu.se Target Milestone: --- Created attachment 41319 --> https://gcc.gnu.org/bugzilla/attachment.cgi?id=41319&action=edit Preprocessed source generated by gcc -v -save-temps -O3 -mavx2 thecode.c I ran into a problem with strange results when compiling with -O3 -mavx2 and have been able to reduce it to the following small test code: ======================================== #include <stdio.h> int main() { const int N = 8; int v[N]; for(int k = 0; k < N; k++) v[k] = k; v[0] = 77; int found_index = -1; for(int k = 0; k < N; k++) { if(v[k] == 77) found_index = k; } printf("found_index = %d\n", found_index); } ======================================== If compiled correctly, running this code should give "found_index = 0". When compiling it like this: gcc -O3 -mavx2 thecode.c then running the resulting a.out executable gives: $ ./a.out found_index = -1 which is wrong. The output of "gcc -v -save-temps -O3 -mavx2 thecode.c" looks as follows: ======================================== $ gcc -v -save-temps -O3 -mavx2 thecode.c Using built-in specs. COLLECT_GCC=gcc COLLECT_LTO_WRAPPER=/usr/libexec/gcc/x86_64-redhat-linux/6.3.1/lto-wrapper Target: x86_64-redhat-linux Configured with: ../configure --enable-bootstrap --enable-languages=c,c++,objc,obj-c++,fortran,ada,go,lto --prefix=/usr --mandir=/usr/share/man --infodir=/usr/share/info --with-bugurl=http://bugzilla.redhat.com/bugzilla --enable-shared --enable-threads=posix --enable-checking=release --enable-multilib --with-system-zlib --enable-__cxa_atexit --disable-libunwind-exceptions --enable-gnu-unique-object --enable-linker-build-id --with-linker-hash-style=gnu --enable-plugin --enable-initfini-array --disable-libgcj --with-isl --enable-libmpx --enable-gnu-indirect-function --with-tune=generic --with-arch_32=i686 --build=x86_64-redhat-linux Thread model: posix gcc version 6.3.1 20161221 (Red Hat 6.3.1-1) (GCC) COLLECT_GCC_OPTIONS='-v' '-save-temps' '-O3' '-mavx2' '-mtune=generic' '-march=x86-64' /usr/libexec/gcc/x86_64-redhat-linux/6.3.1/cc1 -E -quiet -v thecode.c -mavx2 -mtune=generic -march=x86-64 -O3 -fpch-preprocess -o thecode.i ignoring nonexistent directory "/usr/lib/gcc/x86_64-redhat-linux/6.3.1/include-fixed" ignoring nonexistent directory "/usr/lib/gcc/x86_64-redhat-linux/6.3.1/../../../../x86_64-redhat-linux/include" #include "..." search starts here: #include <...> search starts here: /usr/lib/gcc/x86_64-redhat-linux/6.3.1/include /usr/local/include /usr/include End of search list. COLLECT_GCC_OPTIONS='-v' '-save-temps' '-O3' '-mavx2' '-mtune=generic' '-march=x86-64' /usr/libexec/gcc/x86_64-redhat-linux/6.3.1/cc1 -fpreprocessed thecode.i -quiet -dumpbase thecode.c -mavx2 -mtune=generic -march=x86-64 -auxbase thecode -O3 -version -o thecode.s GNU C11 (GCC) version 6.3.1 20161221 (Red Hat 6.3.1-1) (x86_64-redhat-linux) compiled by GNU C version 6.3.1 20161221 (Red Hat 6.3.1-1), GMP version 6.1.1, MPFR version 3.1.5, MPC version 1.0.2, isl version 0.14 or 0.13 GGC heuristics: --param ggc-min-expand=100 --param ggc-min-heapsize=131072 GNU C11 (GCC) version 6.3.1 20161221 (Red Hat 6.3.1-1) (x86_64-redhat-linux) compiled by GNU C version 6.3.1 20161221 (Red Hat 6.3.1-1), GMP version 6.1.1, MPFR version 3.1.5, MPC version 1.0.2, isl version 0.14 or 0.13 GGC heuristics: --param ggc-min-expand=100 --param ggc-min-heapsize=131072 Compiler executable checksum: 67626b9d441eed376539391e660a9413 COLLECT_GCC_OPTIONS='-v' '-save-temps' '-O3' '-mavx2' '-mtune=generic' '-march=x86-64' as -v --64 -o thecode.o thecode.s GNU assembler version 2.26.1 (x86_64-redhat-linux) using BFD version version 2.26.1-1.fc25 COMPILER_PATH=/usr/libexec/gcc/x86_64-redhat-linux/6.3.1/:/usr/libexec/gcc/x86_64-redhat-linux/6.3.1/:/usr/libexec/gcc/x86_64-redhat-linux/:/usr/lib/gcc/x86_64-redhat-linux/6.3.1/:/usr/lib/gcc/x86_64-redhat-linux/ LIBRARY_PATH=/usr/lib/gcc/x86_64-redhat-linux/6.3.1/:/usr/lib/gcc/x86_64-redhat-linux/6.3.1/../../../../lib64/:/lib/../lib64/:/usr/lib/../lib64/:/usr/lib/gcc/x86_64-redhat-linux/6.3.1/../../../:/lib/:/usr/lib/ COLLECT_GCC_OPTIONS='-v' '-save-temps' '-O3' '-mavx2' '-mtune=generic' '-march=x86-64' /usr/libexec/gcc/x86_64-redhat-linux/6.3.1/collect2 -plugin /usr/libexec/gcc/x86_64-redhat-linux/6.3.1/liblto_plugin.so -plugin-opt=/usr/libexec/gcc/x86_64-redhat-linux/6.3.1/lto-wrapper -plugin-opt=-fresolution=thecode.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 --no-add-needed --eh-frame-hdr --hash-style=gnu -m elf_x86_64 -dynamic-linker /lib64/ld-linux-x86-64.so.2 /usr/lib/gcc/x86_64-redhat-linux/6.3.1/../../../../lib64/crt1.o /usr/lib/gcc/x86_64-redhat-linux/6.3.1/../../../../lib64/crti.o /usr/lib/gcc/x86_64-redhat-linux/6.3.1/crtbegin.o -L/usr/lib/gcc/x86_64-redhat-linux/6.3.1 -L/usr/lib/gcc/x86_64-redhat-linux/6.3.1/../../../../lib64 -L/lib/../lib64 -L/usr/lib/../lib64 -L/usr/lib/gcc/x86_64-redhat-linux/6.3.1/../../.. thecode.o -lgcc --as-needed -lgcc_s --no-as-needed -lc -lgcc --as-needed -lgcc_s --no-as-needed /usr/lib/gcc/x86_64-redhat-linux/6.3.1/crtend.o /usr/lib/gcc/x86_64-redhat-linux/6.3.1/../../../../lib64/crtn.o COLLECT_GCC_OPTIONS='-v' '-save-temps' '-O3' '-mavx2' '-mtune=generic' '-march=x86-64' ======================================== I have tested this with a few different gcc versions: gcc 4.8.3 --> OK gcc 4.9.4 --> OK gcc 5.3.0 --> OK gcc 5.4.0 --> OK gcc 6.1.0 --> WRONG gcc 6.2.0 --> WRONG gcc 6.3.1 --> WRONG gcc 7.1.0 --> WRONG I don't know what goes wrong but it seems somehow related to the beginning of the list v in the code; if I change v[0]=77 to e.g. v[3]=77 then that gives found_index=3 as it should, it is only v[0] that somehow is missed.