public inbox for gcc-bugs@sourceware.org help / color / mirror / Atom feed
From: "federico.kircheis at gmail dot com" <gcc-bugzilla@gcc.gnu.org> To: gcc-bugs@gcc.gnu.org Subject: [Bug c++/102209] New: NRVO for function parameters Date: Sun, 05 Sep 2021 19:55:09 +0000 [thread overview] Message-ID: <bug-102209-4@http.gcc.gnu.org/bugzilla/> (raw) https://gcc.gnu.org/bugzilla/show_bug.cgi?id=102209 Bug ID: 102209 Summary: NRVO for function parameters Product: gcc Version: 12.0 Status: UNCONFIRMED Severity: normal Priority: P3 Component: c++ Assignee: unassigned at gcc dot gnu.org Reporter: federico.kircheis at gmail dot com Target Milestone: --- I believe this is a missed optimization opportunity. Given following class and functions: ---- struct s{ s(); s(const s&); s(s&&); void doit(); }; s bar0(){ s v = s(); v.doit(); return v; } s bar1(s v = s()){ v.doit(); return v; } void foo0(){ auto v = bar0(); } void foo1(){ auto v = bar1(); } ---- I can see that in bar0, the returned s is copy (and move) elided. But for bar1, this is not the case. I've tried different things, like changing function signature, adding std::forward, change optimization level, disable exceptions, but I was never able to obtain the desired result. As in bar1 `v` is actually built a level higher on the stack, for this code-snippet I would even have expected GCC to have less issue optimizing the copy/move away. For reference: https://godbolt.org/z/oe7W3nvcP foo0(): sub rsp, 24 call construct() lea rdi, [rsp+15] call s::doit() call destroy() add rsp, 24 ret foo1(): sub rsp, 24 call construct() lea rdi, [rsp+15] call s::doit() call move() call destroy() add rsp, 24 jmp destroy() In both cases, bar* has been inlined. It can be easily verified that in the case of bar0 everything is inlined, and no copies are made. In the case of foo1, a temporary is created, passed to bar without moving or copying, but when bar1 returns, a move is made. As s is passed by value, I do not think the move is necessary. So either my assumption is wrong, or GCC is playing safe. I would like to know if it is a missed optimization opportunity on the side of the programmer or compiler (for those classes where even an unnecessary move might be costly).
next reply other threads:[~2021-09-05 19:55 UTC|newest] Thread overview: 4+ messages / expand[flat|nested] mbox.gz Atom feed top 2021-09-05 19:55 federico.kircheis at gmail dot com [this message] 2022-03-07 20:35 ` [Bug c++/102209] " pinskia at gcc dot gnu.org 2022-03-08 12:25 ` [Bug middle-end/102209] " redi at gcc dot gnu.org 2022-03-08 20:23 ` federico.kircheis at gmail dot com
Reply instructions: You may reply publicly to this message via plain-text email using any one of the following methods: * Save the following mbox file, import it into your mail client, and reply-to-all from there: mbox Avoid top-posting and favor interleaved quoting: https://en.wikipedia.org/wiki/Posting_style#Interleaved_style * Reply using the --to, --cc, and --in-reply-to switches of git-send-email(1): git send-email \ --in-reply-to=bug-102209-4@http.gcc.gnu.org/bugzilla/ \ --to=gcc-bugzilla@gcc.gnu.org \ --cc=gcc-bugs@gcc.gnu.org \ /path/to/YOUR_REPLY https://kernel.org/pub/software/scm/git/docs/git-send-email.html * If your mail client supports setting the In-Reply-To header via mailto: links, try the mailto: linkBe sure your reply has a Subject: header at the top and a blank line before the message body.
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).