public inbox for gcc-bugs@sourceware.org help / color / mirror / Atom feed
* [Bug other/115581] New: missed argument forwarding in lambda @ 2024-06-21 13:39 pobrn at protonmail dot com 2024-06-21 17:20 ` [Bug middle-end/115581] could remove copy of struct if original otherwise not used pinskia at gcc dot gnu.org ` (4 more replies) 0 siblings, 5 replies; 6+ messages in thread From: pobrn at protonmail dot com @ 2024-06-21 13:39 UTC (permalink / raw) To: gcc-bugs https://gcc.gnu.org/bugzilla/show_bug.cgi?id=115581 Bug ID: 115581 Summary: missed argument forwarding in lambda Product: gcc Version: 15.0 Status: UNCONFIRMED Severity: normal Priority: P3 Component: other Assignee: unassigned at gcc dot gnu.org Reporter: pobrn at protonmail dot com Target Milestone: --- Consider the following piece of C++ code: ``` struct thing { int x[64]; }; void g(const thing &); void f1(thing x) { g(x); } auto *f2 = +[](thing x) { g(x); }; struct f3 { void operator()(thing); }; void f3::operator()(thing x) { g(x); } ``` Checking the generated code on an x86-64 linux system at -O2, note that `f1()` and `f3::operator()()` simply "forward" their by-value arguments to `g()`: ``` f1(thing): sub rsp, 8 lea rdi, [rsp+16] call g(thing const&) add rsp, 8 ret ``` Notice that this is not done in the case of `f2`, the generated function from the lambda makes a copy of `x`: ``` f2::{lambda(thing)#1}::_FUN(thing): sub rsp, 264 movdqu xmm0, XMMWORD PTR [rsp+272] mov rdi, rsp movaps XMMWORD PTR [rsp], xmm0 movdqu xmm0, XMMWORD PTR [rsp+288] [...] movdqu xmm0, XMMWORD PTR [rsp+512] movaps XMMWORD PTR [rsp+240], xmm0 call g(thing const&) add rsp, 264 ret ``` See https://gcc.godbolt.org/z/n43a6MPjY, clang does the same copy, so maybe there is a rule in C++ that prevents this optimization and that I failed to consider? ^ permalink raw reply [flat|nested] 6+ messages in thread
* [Bug middle-end/115581] could remove copy of struct if original otherwise not used 2024-06-21 13:39 [Bug other/115581] New: missed argument forwarding in lambda pobrn at protonmail dot com @ 2024-06-21 17:20 ` pinskia at gcc dot gnu.org 2024-06-21 17:20 ` pinskia at gcc dot gnu.org ` (3 subsequent siblings) 4 siblings, 0 replies; 6+ messages in thread From: pinskia at gcc dot gnu.org @ 2024-06-21 17:20 UTC (permalink / raw) To: gcc-bugs https://gcc.gnu.org/bugzilla/show_bug.cgi?id=115581 Andrew Pinski <pinskia at gcc dot gnu.org> changed: What |Removed |Added ---------------------------------------------------------------------------- Status|UNCONFIRMED |NEW Component|other |middle-end Keywords| |missed-optimization Summary|missed argument forwarding |could remove copy of struct |in lambda |if original otherwise not | |used Ever confirmed|0 |1 Last reconfirmed| |2024-06-21 --- Comment #1 from Andrew Pinski <pinskia at gcc dot gnu.org> --- Reduced testcase: ``` struct thing { int x[64]; }; void g(const thing &); static inline void f1(thing x) { g(x); } void f9(thing x) { f1(x); } ``` Confirmed. ^ permalink raw reply [flat|nested] 6+ messages in thread
* [Bug middle-end/115581] could remove copy of struct if original otherwise not used 2024-06-21 13:39 [Bug other/115581] New: missed argument forwarding in lambda pobrn at protonmail dot com 2024-06-21 17:20 ` [Bug middle-end/115581] could remove copy of struct if original otherwise not used pinskia at gcc dot gnu.org @ 2024-06-21 17:20 ` pinskia at gcc dot gnu.org 2024-06-21 17:22 ` pinskia at gcc dot gnu.org ` (2 subsequent siblings) 4 siblings, 0 replies; 6+ messages in thread From: pinskia at gcc dot gnu.org @ 2024-06-21 17:20 UTC (permalink / raw) To: gcc-bugs https://gcc.gnu.org/bugzilla/show_bug.cgi?id=115581 Andrew Pinski <pinskia at gcc dot gnu.org> changed: What |Removed |Added ---------------------------------------------------------------------------- Severity|normal |enhancement ^ permalink raw reply [flat|nested] 6+ messages in thread
* [Bug middle-end/115581] could remove copy of struct if original otherwise not used 2024-06-21 13:39 [Bug other/115581] New: missed argument forwarding in lambda pobrn at protonmail dot com 2024-06-21 17:20 ` [Bug middle-end/115581] could remove copy of struct if original otherwise not used pinskia at gcc dot gnu.org 2024-06-21 17:20 ` pinskia at gcc dot gnu.org @ 2024-06-21 17:22 ` pinskia at gcc dot gnu.org 2024-06-21 18:01 ` pobrn at protonmail dot com 2024-06-21 18:15 ` pinskia at gcc dot gnu.org 4 siblings, 0 replies; 6+ messages in thread From: pinskia at gcc dot gnu.org @ 2024-06-21 17:22 UTC (permalink / raw) To: gcc-bugs https://gcc.gnu.org/bugzilla/show_bug.cgi?id=115581 --- Comment #2 from Andrew Pinski <pinskia at gcc dot gnu.org> --- Note it looks like clang does some front-end optimization for converting the lambda into a function pointer because my example in comment #1 has the copy still there for clang/LLVM. ^ permalink raw reply [flat|nested] 6+ messages in thread
* [Bug middle-end/115581] could remove copy of struct if original otherwise not used 2024-06-21 13:39 [Bug other/115581] New: missed argument forwarding in lambda pobrn at protonmail dot com ` (2 preceding siblings ...) 2024-06-21 17:22 ` pinskia at gcc dot gnu.org @ 2024-06-21 18:01 ` pobrn at protonmail dot com 2024-06-21 18:15 ` pinskia at gcc dot gnu.org 4 siblings, 0 replies; 6+ messages in thread From: pobrn at protonmail dot com @ 2024-06-21 18:01 UTC (permalink / raw) To: gcc-bugs https://gcc.gnu.org/bugzilla/show_bug.cgi?id=115581 --- Comment #3 from Barnabás Pőcze <pobrn at protonmail dot com> --- Isn't your testcase a bit different? I guess my question is, why does gcc feel the need to make a local copy of a by-value argument when calling a function with a reference to it, but seemingly only in a function generated from a lambda? ^ permalink raw reply [flat|nested] 6+ messages in thread
* [Bug middle-end/115581] could remove copy of struct if original otherwise not used 2024-06-21 13:39 [Bug other/115581] New: missed argument forwarding in lambda pobrn at protonmail dot com ` (3 preceding siblings ...) 2024-06-21 18:01 ` pobrn at protonmail dot com @ 2024-06-21 18:15 ` pinskia at gcc dot gnu.org 4 siblings, 0 replies; 6+ messages in thread From: pinskia at gcc dot gnu.org @ 2024-06-21 18:15 UTC (permalink / raw) To: gcc-bugs https://gcc.gnu.org/bugzilla/show_bug.cgi?id=115581 --- Comment #4 from Andrew Pinski <pinskia at gcc dot gnu.org> --- (In reply to Barnabás Pőcze from comment #3) > Isn't your testcase a bit different? I guess my question is, why does gcc > feel the need to make a local copy of a by-value argument when calling a > function with a reference to it, but seemingly only in a function generated > from a lambda? No my testcase is exactly what is done when converting a lambda to a function pointer. That is it creates a function which then calls the lamdba. Just like this: ``` struct thing { int x[64]; }; void f10(thing x) { auto l = [](thing x) { g(x); }; l(x); } auto *f13 = f10; ``` Which has still has the extra copy even for clang. That is why I mentioned that clang seems to have a front-end optimization which removes the copy when converting the lambda to a function pointer. ^ permalink raw reply [flat|nested] 6+ messages in thread
end of thread, other threads:[~2024-06-21 18:15 UTC | newest] Thread overview: 6+ messages (download: mbox.gz / follow: Atom feed) -- links below jump to the message on this page -- 2024-06-21 13:39 [Bug other/115581] New: missed argument forwarding in lambda pobrn at protonmail dot com 2024-06-21 17:20 ` [Bug middle-end/115581] could remove copy of struct if original otherwise not used pinskia at gcc dot gnu.org 2024-06-21 17:20 ` pinskia at gcc dot gnu.org 2024-06-21 17:22 ` pinskia at gcc dot gnu.org 2024-06-21 18:01 ` pobrn at protonmail dot com 2024-06-21 18:15 ` 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).