public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
* [Bug libstdc++/109631] New: Simple std::optional types returned on stack, not registers
@ 2023-04-26  7:22 david at westcontrol dot com
  2023-04-26  9:44 ` [Bug libstdc++/109631] " pinskia at gcc dot gnu.org
                   ` (2 more replies)
  0 siblings, 3 replies; 4+ messages in thread
From: david at westcontrol dot com @ 2023-04-26  7:22 UTC (permalink / raw)
  To: gcc-bugs

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;
}

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

* [Bug libstdc++/109631] Simple std::optional types returned on stack, not registers
  2023-04-26  7:22 [Bug libstdc++/109631] New: Simple std::optional types returned on stack, not registers david at westcontrol dot com
@ 2023-04-26  9:44 ` 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
  2 siblings, 0 replies; 4+ messages in thread
From: pinskia at gcc dot gnu.org @ 2023-04-26  9:44 UTC (permalink / raw)
  To: gcc-bugs

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=109631

Andrew Pinski <pinskia at gcc dot gnu.org> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
           Severity|normal                      |enhancement

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

* [Bug libstdc++/109631] Simple std::optional types returned on stack, not registers
  2023-04-26  7:22 [Bug libstdc++/109631] New: Simple std::optional types returned on stack, not registers david at westcontrol dot com
  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
  2 siblings, 0 replies; 4+ messages in thread
From: pinskia at gcc dot gnu.org @ 2023-04-26  9:46 UTC (permalink / raw)
  To: gcc-bugs

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=109631

Andrew Pinski <pinskia at gcc dot gnu.org> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
             Status|UNCONFIRMED                 |RESOLVED
         Resolution|---                         |DUPLICATE

--- Comment #1 from Andrew Pinski <pinskia at gcc dot gnu.org> ---
Dup of bug 101326.

*** This bug has been marked as a duplicate of bug 101326 ***

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

* [Bug middle-end/109631] Simple std::optional types returned on stack, not registers
  2023-04-26  7:22 [Bug libstdc++/109631] New: Simple std::optional types returned on stack, not registers david at westcontrol dot com
  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 ` pinskia at gcc dot gnu.org
  2 siblings, 0 replies; 4+ messages in thread
From: pinskia at gcc dot gnu.org @ 2023-04-26  9:48 UTC (permalink / raw)
  To: gcc-bugs

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=109631

Andrew Pinski <pinskia at gcc dot gnu.org> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
          Component|libstdc++                   |middle-end
           Keywords|ABI                         |missed-optimization

--- Comment #2 from Andrew Pinski <pinskia at gcc dot gnu.org> ---
Note this is not a libstdc++ issue as your clang invocation is even still using
libstdc++ :).

Anyways this is a dup of the other bug has some analysis to it.

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

end of thread, other threads:[~2023-04-26  9:48 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2023-04-26  7:22 [Bug libstdc++/109631] New: Simple std::optional types returned on stack, not registers david at westcontrol dot com
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

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).