http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47817
Summary: incorrectly working code generated with -O3 Product: gcc Version: 4.4.4 Status: UNCONFIRMED Severity: critical Priority: P3 Component: c++ AssignedTo: unassig...@gcc.gnu.org ReportedBy: andrew.aksyon...@gmail.com Created attachment 23406 --> http://gcc.gnu.org/bugzilla/attachment.cgi?id=23406 sample code that works with -O2 but fails with -O3 The attached sample behaves differently when built with -O2 and -O3 respectively. It's essentially a sorting routine implementation. The expected behavior is to sort the data in ascending order and there is a check that verifies that. It works as expected with gcc -O2 (also with a few other compilers): shodan@dev1:~/gccbug$ g++ -O2 tests.cpp shodan@dev1:~/gccbug$ ./a.out DEBUG: sorting 57 entries, stride 3 However, with -O3 the sanity check fails. (Two rows are ending up in the wrong order.) shodan@dev1:~/gccbug$ g++ -O3 tests.cpp shodan@dev1:~/gccbug$ ./a.out DEBUG: sorting 57 entries, stride 3 DEBUG: !!! sorting2 failed The issue is present in two versions of gcc, 4.4.3 as packaged in Ubuntu 10.04 LTS, and 4.4.4 as packaged in RedHat 6.0, both running on x86_64 platform. Here's gcc -v output for both Ubuntu 10.04 x64 gcc -v Using built-in specs. Target: x86_64-linux-gnu Configured with: ../src/configure -v --with-pkgversion='Ubuntu 4.4.3-4ubuntu5' --with-bugurl=file:///usr/share/doc/gcc-4.4/README.Bugs --enable-languages=c,c++,fortran,objc,obj-c++ --prefix=/usr --enable-shared --enable-multiarch --enable-linker-build-id --with-system-zlib --libexecdir=/usr/lib --without-included-gettext --enable-threads=posix --with-gxx-include-dir=/usr/include/c++/4.4 --program-suffix=-4.4 --enable-nls --enable-clocale=gnu --enable-libstdcxx-debug --enable-plugin --enable-objc-gc --disable-werror --with-arch-32=i486 --with-tune=generic --enable-checking=release --build=x86_64-linux-gnu --host=x86_64-linux-gnu --target=x86_64-linux-gnu Thread model: posix gcc version 4.4.3 (Ubuntu 4.4.3-4ubuntu5) RedHat 6.0 x64 gcc -v Using built-in specs. Target: x86_64-redhat-linux Configured with: ../configure --prefix=/usr --mandir=/usr/share/man --infodir=/usr/share/info --with-bugurl=http://bugzilla.redhat.com/bugzilla --enable-bootstrap --enable-shared --enable-threads=posix --enable-checking=release --with-system-zlib --enable-__cxa_atexit --disable-libunwind-exceptions --enable-gnu-unique-object --enable-languages=c,c++,objc,obj-c++,java,fortran,ada --enable-java-awt=gtk --disable-dssi --with-java-home=/usr/lib/jvm/java-1.5.0-gcj-1.5.0.0/jre --enable-libgcj-multifile --enable-java-maintainer-mode --with-ecj-jar=/usr/share/java/eclipse-ecj.jar --disable-libjava-multilib --with-ppl --with-cloog --with-tune=generic --with-arch_32=i686 --build=x86_64-redhat-linux Thread model: posix gcc version 4.4.4 20100726 (Red Hat 4.4.4-13) (GCC) Not sure if the following helps, but anyway. This trimmed down sample starts to work if I remove unused (!) CSphString from SortDocinfos() function. That was *not* the case on a larger codebase this example was extracted from. Original SortDocinfos() function only consisted of two lines, one declaring DocinfoSort_fn functor and the other one invoking sphSort(), but that was failing nevertheless. Last but not least, any insight and/or workarounds I could implement in my code to prevent this bug from happening on affected gcc versions would be really appreciated. Our application depends on sorting a lot... Thanks.