public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
* [Bug target/103696] New: Lambda functions are not inlined under certain optimization pragmas
@ 2021-12-13 20:30 imachug at gmail dot com
  2021-12-13 20:33 ` [Bug target/103696] " imachug at gmail dot com
                   ` (6 more replies)
  0 siblings, 7 replies; 8+ messages in thread
From: imachug at gmail dot com @ 2021-12-13 20:30 UTC (permalink / raw)
  To: 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
...
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.

^ permalink raw reply	[flat|nested] 8+ messages in thread

end of thread, other threads:[~2024-04-17  7:12 UTC | newest]

Thread overview: 8+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2021-12-13 20:30 [Bug target/103696] New: Lambda functions are not inlined under certain optimization pragmas imachug at gmail dot com
2021-12-13 20:33 ` [Bug target/103696] " imachug at gmail dot com
2021-12-14  1:14 ` [Bug c++/103696] pragma optimization is not applying to Lambdas pinskia at gcc dot gnu.org
2021-12-14 16:59 ` marxin at gcc dot gnu.org
2021-12-16 11:08 ` marxin at gcc dot gnu.org
2021-12-16 16:52 ` cvs-commit at gcc dot gnu.org
2021-12-16 16:53 ` marxin at gcc dot gnu.org
2024-04-17  7:12 ` pinskia at gcc dot gnu.org

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).