public inbox for gcc-bugs@sourceware.org help / color / mirror / Atom feed
From: "david at westcontrol dot com" <gcc-bugzilla@gcc.gnu.org> To: gcc-bugs@gcc.gnu.org Subject: [Bug libstdc++/109631] New: Simple std::optional types returned on stack, not registers Date: Wed, 26 Apr 2023 07:22:08 +0000 [thread overview] Message-ID: <bug-109631-4@http.gcc.gnu.org/bugzilla/> (raw) https://gcc.gnu.org/bugzilla/show_bug.cgi?id=109631 Bug ID: 109631 Summary: Simple std::optional types returned on stack, not registers Product: gcc Version: unknown Status: UNCONFIRMED Severity: normal Priority: P3 Component: libstdc++ Assignee: unassigned at gcc dot gnu.org Reporter: david at westcontrol dot com Target Milestone: --- A std::optional<T> is fundamentally like a std::pair<T, bool>, but with nice access features, type safety, etc. But for simple functions it is significantly less efficient, because in gcc it is returned on the stack even when T fits in a single register. <https://godbolt.org/z/6hx6vxaG7> On targets such as x86-64 and AARCH64, simple structs and pairs that fit in two registers, are returned in two registers - there is no need for the caller to reserve stack space and pass a hidden pointer to the callee function. On clang with its standard library, this also applies to std::optional<T> for suitable types T such as "int", but on gcc and its standard C++ library, the stack is used for returning the std::optional<int>. Since both clang and gcc follow the same calling conventions, this must (I believe) be a result of different implementations of std::optional<> in the C++ libraries. (I note that code for std::pair<bool, T> is more efficient here than using std::pair<T, bool>. But perhaps the details of std::optional<> require that the contained element comes first.) #include <optional> #include <utility> using A = std::optional<int>; using B = std::pair<int, bool>; using C = std::pair<bool, int>; A foo_A(int x) { return x; } B foo_b(int x) { B y { x, true }; return y; } C foo_c(int x) { C y { true, x }; return y; }
next reply other threads:[~2023-04-26 7:22 UTC|newest] Thread overview: 4+ messages / expand[flat|nested] mbox.gz Atom feed top 2023-04-26 7:22 david at westcontrol dot com [this message] 2023-04-26 9:44 ` [Bug libstdc++/109631] " pinskia at gcc dot gnu.org 2023-04-26 9:46 ` pinskia at gcc dot gnu.org 2023-04-26 9:48 ` [Bug middle-end/109631] " pinskia at gcc dot gnu.org
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-109631-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).