[Bug target/103696] Lambda functions are not inlined under certain optimization pragmas

2021-12-13 Thread imachug at gmail dot com via Gcc-bugs
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=103696

--- Comment #1 from Ivan Machugovskiy  ---
Obligatory info dump. I managed to reproduce this on G++ 9.3.0 and G++ 10.3.0
locally, and on G++ trunk on Godbolt (see https://godbolt.org/z/Y5Kr3KfjW).
This is probably a longstanding bug.


$ g++ -v
Using built-in specs.
COLLECT_GCC=g++
COLLECT_LTO_WRAPPER=/usr/lib/gcc/x86_64-linux-gnu/9/lto-wrapper
OFFLOAD_TARGET_NAMES=nvptx-none:hsa
OFFLOAD_TARGET_DEFAULT=1
Target: x86_64-linux-gnu
Configured with: ../src/configure -v --with-pkgversion='Ubuntu
9.3.0-17ubuntu1~20.04' --with-bugurl=file:///usr/share/doc/gcc-9/README.Bugs
--enable-languages=c,ada,c++,go,brig,d,fortran,objc,obj-c++,gm2 --prefix=/usr
--with-gcc-major-version-only --program-suffix=-9
--program-prefix=x86_64-linux-gnu- --enable-shared --enable-linker-build-id
--libexecdir=/usr/lib --without-included-gettext --enable-threads=posix
--libdir=/usr/lib --enable-nls --enable-clocale=gnu --enable-libstdcxx-debug
--enable-libstdcxx-time=yes --with-default-libstdcxx-abi=new
--enable-gnu-unique-object --disable-vtable-verify --enable-plugin
--enable-default-pie --with-system-zlib --with-target-system-zlib=auto
--enable-objc-gc=auto --enable-multiarch --disable-werror --with-arch-32=i686
--with-abi=m64 --with-multilib-list=m32,m64,mx32 --enable-multilib
--with-tune=generic
--enable-offload-targets=nvptx-none=/build/gcc-9-HskZEa/gcc-9-9.3.0/debian/tmp-nvptx/usr,hsa
--without-cuda-driver --enable-checking=release --build=x86_64-linux-gnu
--host=x86_64-linux-gnu --target=x86_64-linux-gnu
Thread model: posix
gcc version 9.3.0 (Ubuntu 9.3.0-17ubuntu1~20.04)

$ g++-10 -v
Using built-in specs.
COLLECT_GCC=g++-10
COLLECT_LTO_WRAPPER=/usr/lib/gcc/x86_64-linux-gnu/10/lto-wrapper
OFFLOAD_TARGET_NAMES=nvptx-none:amdgcn-amdhsa:hsa
OFFLOAD_TARGET_DEFAULT=1
Target: x86_64-linux-gnu
Configured with: ../src/configure -v --with-pkgversion='Ubuntu
10.3.0-1ubuntu1~20.04' --with-bugurl=file:///usr/share/doc/gcc-10/README.Bugs
--enable-languages=c,ada,c++,go,brig,d,fortran,objc,obj-c++,m2 --prefix=/usr
--with-gcc-major-version-only --program-suffix=-10
--program-prefix=x86_64-linux-gnu- --enable-shared --enable-linker-build-id
--libexecdir=/usr/lib --without-included-gettext --enable-threads=posix
--libdir=/usr/lib --enable-nls --enable-bootstrap --enable-clocale=gnu
--enable-libstdcxx-debug --enable-libstdcxx-time=yes
--with-default-libstdcxx-abi=new --enable-gnu-unique-object
--disable-vtable-verify --enable-plugin --enable-default-pie --with-system-zlib
--enable-libphobos-checking=release --with-target-system-zlib=auto
--enable-objc-gc=auto --enable-multiarch --disable-werror --with-arch-32=i686
--with-abi=m64 --with-multilib-list=m32,m64,mx32 --enable-multilib
--with-tune=generic
--enable-offload-targets=nvptx-none=/build/gcc-10-S4I5Pr/gcc-10-10.3.0/debian/tmp-nvptx/usr,amdgcn-amdhsa=/build/gcc-10-S4I5Pr/gcc-10-10.3.0/debian/tmp-gcn/usr,hsa
--without-cuda-driver --enable-checking=release --build=x86_64-linux-gnu
--host=x86_64-linux-gnu --target=x86_64-linux-gnu
--with-build-config=bootstrap-lto-lean --enable-link-mutex
Thread model: posix
Supported LTO compression algorithms: zlib zstd
gcc version 10.3.0 (Ubuntu 10.3.0-1ubuntu1~20.04)

[Bug target/103696] New: Lambda functions are not inlined under certain optimization pragmas

2021-12-13 Thread imachug at gmail dot com via Gcc-bugs
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=103696

Bug ID: 103696
   Summary: Lambda functions are not inlined under certain
optimization pragmas
   Product: gcc
   Version: 11.2.1
Status: UNCONFIRMED
  Severity: normal
  Priority: P3
 Component: target
  Assignee: unassigned at gcc dot gnu.org
  Reporter: imachug at gmail dot com
  Target Milestone: ---

This seems like a very weird bug to me and I'm not even sure how to label it,
so please fix the component if needed.

Testcase (C++):


#pragma GCC optimize("finite-math-only")
#pragma GCC target("sse3")

void fn() {
}

int global_var;

int solve() {
auto nested = []() {
return global_var;
};
return nested();
}


When compiling this code via `g++ test.cpp -c -O2 -std=c++17`, I get the
following assembly:


$ objdump -d test.o
...
 <_ZZ5solvevENKUlvE_clEv.constprop.0>:
   0:   8b 05 00 00 00 00   mov0x0(%rip),%eax# 6
<_ZZ5solvevENKUlvE_clEv.constprop.0+0x6>
   6:   c3  retq   
   7:   66 0f 1f 84 00 00 00nopw   0x0(%rax,%rax,1)
   e:   00 00 
...
0020 <_Z5solvev>:
  20:   f3 0f 1e fa endbr64 
  24:   e8 d7 ff ff ff  callq  0 <_ZZ5solvevENKUlvE_clEv.constprop.0>
  29:   c3  retq   


As you can see, the nested() lambda call was not inlined into solve().

However, if I do any of the following, the lambda is inlined as expected:

- Remove `fn` definition
- Move `fn` definition under `solve`
- Replace reading `global_var` with a constant
- Make `nested` a global function
- Remove either of the two pragmas (or both)
- Add -ffinite-math-only or -msse3 or both to the compilation line (regardless
of whether the pragmas are still there)

I have absolutely no idea why a floating point optimization affects inlining or
how a pragma is different from a compilation line option wrt. this bug.