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

            Bug ID: 84471
           Summary: Instruction reordering happens in lambdas even with
                    -O0
           Product: gcc
           Version: 7.3.0
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: c++
          Assignee: unassigned at gcc dot gnu.org
          Reporter: jengelh at inai dot de
  Target Milestone: ---

Expected behavior:

Objects built with -O0 ought not cause gdb to spuriously "go backwards" in
source (for reasons other than the end of a loop).


Observed behavior:

Despite being explicitly built with no optimizations, I observe that gdb jumps
around inside C++ programs' lambdas that capture something.

#include <cstring>
#include <utility>
#define N 256
int main()
{
        const char key[] = "keykeykeykeykeykey";
        [&]() {
                unsigned char S[N];
                int len = strlen(key);
                int j = 0;
                for (int i = 0; i < N; ++i)
                        j = (j + S[i] + key[i%len]) % N;
        }();
}

Using built-in specs.
COLLECT_GCC=g++-7
COLLECT_LTO_WRAPPER=/usr/lib64/gcc/x86_64-suse-linux/7/lto-wrapper
OFFLOAD_TARGET_NAMES=hsa:nvptx-none
Target: x86_64-suse-linux
Configured with: ../configure --prefix=/usr --infodir=/usr/share/info
--mandir=/usr/share/man --libdir=/usr/lib64 --libexecdir=/usr/lib64
--enable-languages=c,c++,objc,fortran,obj-c++,ada,go
--enable-offload-targets=hsa,nvptx-none=/usr/nvptx-none, --without-cuda-driver
--enable-checking=release --disable-werror
--with-gxx-include-dir=/usr/include/c++/7 --enable-ssp --disable-libssp
--disable-libvtv --disable-libcc1 --enable-plugin
--with-bugurl=http://bugs.opensuse.org/ --with-pkgversion='SUSE Linux'
--with-slibdir=/lib64 --with-system-zlib --enable-__cxa_atexit
--enable-libstdcxx-allocator=new --disable-libstdcxx-pch
--enable-version-specific-runtime-libs --with-gcc-major-version-only
--enable-linker-build-id --enable-linux-futex --enable-gnu-indirect-function
--program-suffix=-7 --without-system-libunwind --enable-multilib
--with-arch-32=x86-64 --with-tune=generic --build=x86_64-suse-linux
--host=x86_64-suse-linux
Thread model: posix
gcc version 7.3.0 (SUSE Linux) 

$ g++-7 x.cpp -O0 -ggdb3 -fno-reorder-blocks -fno-reorder-blocks-and-partition
-fno-reorder-functions 

$ gdb a.out 
GNU gdb (GDB; openSUSE Tumbleweed) 8.0.1
(gdb) b main
Breakpoint 1 at 0x4005c0: file x.cpp, line 6.
(gdb) r

Breakpoint 1, main () at x.cpp:6
6               const char key[] = "keykeykeykeykeykey";
(gdb) s
13              }();
(gdb) 
<lambda()>::operator()(void) const (__closure=0x7fffffffdd98) at x.cpp:9
9                       int len = strlen(key);
(gdb) n
10                      int j = 0;
(gdb) 
11                      for (int i = 0; i < N; ++i)
(gdb) 
12                              j = (j + S[i] + key[i%len]) % N;
(gdb) 
9                       int len = strlen(key);
(gdb) 
12                              j = (j + S[i] + key[i%len]) % N;

Expected behavior:

Never reach line 9 again.

Reply via email to