public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
* [Bug c++/109281] New: use std::optional results in suboptimal code
@ 2023-03-25 11:55 vincenzo.innocente at cern dot ch
  2023-03-25 18:48 ` [Bug tree-optimization/109281] " pinskia at gcc dot gnu.org
                   ` (3 more replies)
  0 siblings, 4 replies; 5+ messages in thread
From: vincenzo.innocente at cern dot ch @ 2023-03-25 11:55 UTC (permalink / raw)
  To: gcc-bugs

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

            Bug ID: 109281
           Summary: use std::optional results in suboptimal code
           Product: gcc
           Version: 12.2.1
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: c++
          Assignee: unassigned at gcc dot gnu.org
          Reporter: vincenzo.innocente at cern dot ch
  Target Milestone: ---

In the following (almost real) code gcc emits suboptimal code if std::optional
is used w/r/t home made one and clang

see https://godbolt.org/z/Pba51Ye7Y


-----

code


#include <optional>

// #define USE_OPTIONAL

#ifdef USE_OPTIONAL
struct SubRingCrossings {
  SubRingCrossings(int ci, int ni, float nd) : closestIndex(ci), nextIndex(ni),
nextDistance(nd) {}

  int closestIndex;
  int nextIndex;
  float nextDistance;
};
#else
struct SubRingCrossings {
  SubRingCrossings() : valid(false) {}
  SubRingCrossings(int ci, int ni, float nd) : valid(true), closestIndex(ci),
nextIndex(ni), nextDistance(nd) {}

  bool valid;
  int closestIndex;
  int nextIndex;
  float nextDistance;
};
#endif

bool condition();

#ifdef USE_OPTIONAL
std::optional<SubRingCrossings> foo() {
    if (condition()) {
        return std::nullopt;
    }
    return SubRingCrossings(1, 2, 3.14);
}
#else
SubRingCrossings foo() {
    if (condition()) {
        return SubRingCrossings();
    }
    return SubRingCrossings(1, 2, 3.14);
}
#endif

int bar() {
    auto tmp = foo();
#ifdef USE_OPTIONAL
    if (tmp) {
        return tmp->closestIndex;
#else
    if (tmp.valid) {
        return tmp.closestIndex;
#endif
    } else {
        return 0;
    }
}

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

* [Bug tree-optimization/109281] use std::optional results in suboptimal code
  2023-03-25 11:55 [Bug c++/109281] New: use std::optional results in suboptimal code vincenzo.innocente at cern dot ch
@ 2023-03-25 18:48 ` pinskia at gcc dot gnu.org
  2023-04-02 17:03 ` pinskia at gcc dot gnu.org
                   ` (2 subsequent siblings)
  3 siblings, 0 replies; 5+ messages in thread
From: pinskia at gcc dot gnu.org @ 2023-03-25 18:48 UTC (permalink / raw)
  To: gcc-bugs

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

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

           What    |Removed                     |Added
----------------------------------------------------------------------------
         Depends on|                            |101326, 95405

--- Comment #1 from Andrew Pinski <pinskia at gcc dot gnu.org> ---
This is same as PR 95405 and PR 101326


Referenced Bugs:

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=95405
[Bug 95405] Unnecessary stores with std::optional
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=101326
[Bug 101326] std::optional returns forced through stack

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

* [Bug tree-optimization/109281] use std::optional results in suboptimal code
  2023-03-25 11:55 [Bug c++/109281] New: use std::optional results in suboptimal code vincenzo.innocente at cern dot ch
  2023-03-25 18:48 ` [Bug tree-optimization/109281] " pinskia at gcc dot gnu.org
@ 2023-04-02 17:03 ` pinskia at gcc dot gnu.org
  2023-07-12 21:35 ` pinskia at gcc dot gnu.org
  2023-07-12 21:35 ` pinskia at gcc dot gnu.org
  3 siblings, 0 replies; 5+ messages in thread
From: pinskia at gcc dot gnu.org @ 2023-04-02 17:03 UTC (permalink / raw)
  To: gcc-bugs

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

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

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

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

* [Bug tree-optimization/109281] use std::optional results in suboptimal code
  2023-03-25 11:55 [Bug c++/109281] New: use std::optional results in suboptimal code vincenzo.innocente at cern dot ch
  2023-03-25 18:48 ` [Bug tree-optimization/109281] " pinskia at gcc dot gnu.org
  2023-04-02 17:03 ` pinskia at gcc dot gnu.org
@ 2023-07-12 21:35 ` pinskia at gcc dot gnu.org
  2023-07-12 21:35 ` pinskia at gcc dot gnu.org
  3 siblings, 0 replies; 5+ messages in thread
From: pinskia at gcc dot gnu.org @ 2023-07-12 21:35 UTC (permalink / raw)
  To: gcc-bugs

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=109281
Bug 109281 depends on bug 101326, which changed state.

Bug 101326 Summary: std::optional returns forced through stack
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=101326

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

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

* [Bug tree-optimization/109281] use std::optional results in suboptimal code
  2023-03-25 11:55 [Bug c++/109281] New: use std::optional results in suboptimal code vincenzo.innocente at cern dot ch
                   ` (2 preceding siblings ...)
  2023-07-12 21:35 ` pinskia at gcc dot gnu.org
@ 2023-07-12 21:35 ` pinskia at gcc dot gnu.org
  3 siblings, 0 replies; 5+ messages in thread
From: pinskia at gcc dot gnu.org @ 2023-07-12 21:35 UTC (permalink / raw)
  To: gcc-bugs

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

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

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

--- Comment #2 from Andrew Pinski <pinskia at gcc dot gnu.org> ---
Dup.

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

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

end of thread, other threads:[~2023-07-12 21:35 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2023-03-25 11:55 [Bug c++/109281] New: use std::optional results in suboptimal code vincenzo.innocente at cern dot ch
2023-03-25 18:48 ` [Bug tree-optimization/109281] " pinskia at gcc dot gnu.org
2023-04-02 17:03 ` pinskia at gcc dot gnu.org
2023-07-12 21:35 ` pinskia at gcc dot gnu.org
2023-07-12 21:35 ` 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).