public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
* [Bug c++/110124] New: Not inlining comparison operator when using std::tie with -std=c++20
@ 2023-06-05 11:27 vakili at live dot com
  2023-06-05 15:32 ` [Bug c++/110124] " pinskia at gcc dot gnu.org
                   ` (3 more replies)
  0 siblings, 4 replies; 5+ messages in thread
From: vakili at live dot com @ 2023-06-05 11:27 UTC (permalink / raw)
  To: gcc-bugs

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

            Bug ID: 110124
           Summary: Not inlining comparison operator when using std::tie
                    with -std=c++20
           Product: gcc
           Version: 14.0
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: c++
          Assignee: unassigned at gcc dot gnu.org
          Reporter: vakili at live dot com
  Target Milestone: ---

Here is a simple class with a operator<():

struct A {
    int a0;
    int a1;
    int a2;

    friend inline constexpr bool operator<(const A& x, const A& y) {
        return std::tie(x.a0, x.a1, x.a2) < std::tie(y.a0, y.a1, y.a2);
    }
};

When compiling this code with -std=c++17 the operator will be inlined at usage
location. But when compiled with -std=c++20 it will not be inlined.

Here is the compiler explorer snippet showing the situation:
https://godbolt.org/z/4KdzWjvx4

The problem exists from g++ 10.0 to the trunk (14.0).

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

* [Bug c++/110124] Not inlining comparison operator when using std::tie with -std=c++20
  2023-06-05 11:27 [Bug c++/110124] New: Not inlining comparison operator when using std::tie with -std=c++20 vakili at live dot com
@ 2023-06-05 15:32 ` pinskia at gcc dot gnu.org
  2023-06-05 15:34 ` 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-06-05 15:32 UTC (permalink / raw)
  To: gcc-bugs

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

--- Comment #1 from Andrew Pinski <pinskia at gcc dot gnu.org> ---
Created attachment 55263
  --> https://gcc.gnu.org/bugzilla/attachment.cgi?id=55263&action=edit
original testcase

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

* [Bug c++/110124] Not inlining comparison operator when using std::tie with -std=c++20
  2023-06-05 11:27 [Bug c++/110124] New: Not inlining comparison operator when using std::tie with -std=c++20 vakili at live dot com
  2023-06-05 15:32 ` [Bug c++/110124] " pinskia at gcc dot gnu.org
@ 2023-06-05 15:34 ` pinskia at gcc dot gnu.org
  2023-06-05 15:38 ` pinskia at gcc dot gnu.org
  2023-06-05 15:40 ` pinskia at gcc dot gnu.org
  3 siblings, 0 replies; 5+ messages in thread
From: pinskia at gcc dot gnu.org @ 2023-06-05 15:34 UTC (permalink / raw)
  To: gcc-bugs

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

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

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

--- Comment #2 from Andrew Pinski <pinskia at gcc dot gnu.org> ---
The problem is GCC knows that main is only called once so the inlining is done
slightly different there.
If you name the function differently, GCC will do the inlining.
That is:
```
#include <iostream>
#include <tuple>

struct A {
    int a0;
    int a1;
    int a2;

    friend inline constexpr bool operator<(const A& x, const A& y) {
        return std::tie(x.a0, x.a1, x.a2) < std::tie(y.a0, y.a1, y.a2);
    }
};

int h()
{
    A a{15, 30, 45};
    A b{20, 30, 40};

    std::cout << (a < b ? 'T' : 'F');
    return 0;
}
```
You will see it is inlined.

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

* [Bug c++/110124] Not inlining comparison operator when using std::tie with -std=c++20
  2023-06-05 11:27 [Bug c++/110124] New: Not inlining comparison operator when using std::tie with -std=c++20 vakili at live dot com
  2023-06-05 15:32 ` [Bug c++/110124] " pinskia at gcc dot gnu.org
  2023-06-05 15:34 ` pinskia at gcc dot gnu.org
@ 2023-06-05 15:38 ` pinskia at gcc dot gnu.org
  2023-06-05 15:40 ` pinskia at gcc dot gnu.org
  3 siblings, 0 replies; 5+ messages in thread
From: pinskia at gcc dot gnu.org @ 2023-06-05 15:38 UTC (permalink / raw)
  To: gcc-bugs

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

--- Comment #3 from Andrew Pinski <pinskia at gcc dot gnu.org> ---
The reason for the C++20 and C++17 difference is because std::tie creates a
std::tuple and for C++20 has a spaceship operator<=> (instead of operator<) and
that causes needing to another another function after the spaceship operator to
figure out `<`.

The IR for C++20:
              D.70406 = std::operator<=><const int&, const int&, const int&,
const int&, const int&, const int&> (&D.62773, &D.62783);
              D.70405 = std::operator< (D.70406, D.62981);

vs for C++17:
          D.56644 = std::operator< <const int&, const int&, const int&, const
int&, const int&, const int&> (&D.51943, &D.51953);

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

* [Bug c++/110124] Not inlining comparison operator when using std::tie with -std=c++20
  2023-06-05 11:27 [Bug c++/110124] New: Not inlining comparison operator when using std::tie with -std=c++20 vakili at live dot com
                   ` (2 preceding siblings ...)
  2023-06-05 15:38 ` pinskia at gcc dot gnu.org
@ 2023-06-05 15:40 ` pinskia at gcc dot gnu.org
  3 siblings, 0 replies; 5+ messages in thread
From: pinskia at gcc dot gnu.org @ 2023-06-05 15:40 UTC (permalink / raw)
  To: gcc-bugs

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

--- Comment #4 from Andrew Pinski <pinskia at gcc dot gnu.org> ---
So basically more code for the C++20 case causes the less inlining into main.
Overall this is not an bug really because GCC treats main as known to be called
only once and anything main calls is not always inlined as much as what would
happen into other functions.

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

end of thread, other threads:[~2023-06-05 15:40 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2023-06-05 11:27 [Bug c++/110124] New: Not inlining comparison operator when using std::tie with -std=c++20 vakili at live dot com
2023-06-05 15:32 ` [Bug c++/110124] " pinskia at gcc dot gnu.org
2023-06-05 15:34 ` pinskia at gcc dot gnu.org
2023-06-05 15:38 ` pinskia at gcc dot gnu.org
2023-06-05 15:40 ` 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).