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

imachug at gmail dot com gcc-bugzilla@gcc.gnu.org
Mon Dec 13 20:30:13 GMT 2021


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
...
0000000000000000 <_ZZ5solvevENKUlvE_clEv.constprop.0>:
   0:   8b 05 00 00 00 00       mov    0x0(%rip),%eax        # 6
<_ZZ5solvevENKUlvE_clEv.constprop.0+0x6>
   6:   c3                      retq   
   7:   66 0f 1f 84 00 00 00    nopw   0x0(%rax,%rax,1)
   e:   00 00 
...
0000000000000020 <_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.


More information about the Gcc-bugs mailing list