public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
* [Bug c++/100322] New: Switching from std=c++17 to std=c++20 causes performance regression in relationals
@ 2021-04-28 17:15 aaron at aarongraham dot com
  2021-04-28 17:49 ` [Bug c++/100322] " redi at gcc dot gnu.org
                   ` (7 more replies)
  0 siblings, 8 replies; 9+ messages in thread
From: aaron at aarongraham dot com @ 2021-04-28 17:15 UTC (permalink / raw)
  To: gcc-bugs

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

            Bug ID: 100322
           Summary: Switching from std=c++17 to std=c++20 causes
                    performance regression in relationals
           Product: gcc
           Version: 12.0
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: c++
          Assignee: unassigned at gcc dot gnu.org
          Reporter: aaron at aarongraham dot com
  Target Milestone: ---

Experiment here: https://godbolt.org/z/PT73cn5e5

#include <chrono>

using clk = std::chrono::steady_clock;
bool compare_count(clk::duration a, clk::duration b) {
    return a.count() > b.count();
}
bool compare(clk::duration a, clk::duration b) {
    return a > b;
}

Compiling with -std=c++17 I get:

_Z13compare_countNSt6chrono8durationIxSt5ratioILx1ELx1000000000EEEES3_:
        cmp     r2, r0
        sbcs    r3, r3, r1
        ite     lt
        movlt   r0, #1
        movge   r0, #0
        bx      lr
_Z7compareNSt6chrono8durationIxSt5ratioILx1ELx1000000000EEEES3_:
        cmp     r2, r0
        sbcs    r3, r3, r1
        ite     lt
        movlt   r0, #1
        movge   r0, #0
        bx      lr

Compiling with -std=c++20 I get:

_Z13compare_countNSt6chrono8durationIxSt5ratioILx1ELx1000000000EEEES3_:
        cmp     r2, r0
        sbcs    r3, r3, r1
        ite     lt
        movlt   r0, #1
        movge   r0, #0
        bx      lr
_Z7compareNSt6chrono8durationIxSt5ratioILx1ELx1000000000EEEES3_:
        cmp     r1, r3
        it      eq
        cmpeq   r0, r2
        beq     .L4
        cmp     r0, r2
        sbcs    r3, r1, r3
        bge     .L5
        mov     r0, #-1
.L3:
        cmp     r0, #0
        ite     le
        movle   r0, #0
        movgt   r0, #1
        bx      lr
.L4:
        movs    r0, #0
        b       .L3
.L5:
        movs    r0, #1
        b       .L3

(Note that clang doesn't have this problem)

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

* [Bug c++/100322] Switching from std=c++17 to std=c++20 causes performance regression in relationals
  2021-04-28 17:15 [Bug c++/100322] New: Switching from std=c++17 to std=c++20 causes performance regression in relationals aaron at aarongraham dot com
@ 2021-04-28 17:49 ` redi at gcc dot gnu.org
  2021-04-28 18:03 ` redi at gcc dot gnu.org
                   ` (6 subsequent siblings)
  7 siblings, 0 replies; 9+ messages in thread
From: redi at gcc dot gnu.org @ 2021-04-28 17:49 UTC (permalink / raw)
  To: gcc-bugs

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

--- Comment #1 from Jonathan Wakely <redi at gcc dot gnu.org> ---
Maybe related to either PR 94006 or PR 94566

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

* [Bug c++/100322] Switching from std=c++17 to std=c++20 causes performance regression in relationals
  2021-04-28 17:15 [Bug c++/100322] New: Switching from std=c++17 to std=c++20 causes performance regression in relationals aaron at aarongraham dot com
  2021-04-28 17:49 ` [Bug c++/100322] " redi at gcc dot gnu.org
@ 2021-04-28 18:03 ` redi at gcc dot gnu.org
  2021-04-28 18:13 ` redi at gcc dot gnu.org
                   ` (5 subsequent siblings)
  7 siblings, 0 replies; 9+ messages in thread
From: redi at gcc dot gnu.org @ 2021-04-28 18:03 UTC (permalink / raw)
  To: gcc-bugs

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

Jonathan Wakely <redi at gcc dot gnu.org> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
             Status|UNCONFIRMED                 |NEW
   Last reconfirmed|                            |2021-04-28
     Ever confirmed|0                           |1

--- Comment #2 from Jonathan Wakely <redi at gcc dot gnu.org> ---
While it's not the cause of the bad codegen for the <=> comparisons, this test
shouldn't even be using <=>. The problem is that the synthesized operator> that
is based on <=> is a better match for non-const durations than the one that
would generate good code:

If you write it like this you get good codegen:

bool compare(const clk::duration& a, const clk::duration& b)
{
    return a > b;
}

In fact you don't even need to pass them by reference, just:

bool compare(const clk::duration a, const clk::duration b)
{
    return a > b;
}

This seems like a defect in the std::lib, but it's separate from the poor
codegen for the <=> comparison.

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

* [Bug c++/100322] Switching from std=c++17 to std=c++20 causes performance regression in relationals
  2021-04-28 17:15 [Bug c++/100322] New: Switching from std=c++17 to std=c++20 causes performance regression in relationals aaron at aarongraham dot com
  2021-04-28 17:49 ` [Bug c++/100322] " redi at gcc dot gnu.org
  2021-04-28 18:03 ` redi at gcc dot gnu.org
@ 2021-04-28 18:13 ` redi at gcc dot gnu.org
  2021-04-28 18:18 ` barry.revzin at gmail dot com
                   ` (4 subsequent siblings)
  7 siblings, 0 replies; 9+ messages in thread
From: redi at gcc dot gnu.org @ 2021-04-28 18:13 UTC (permalink / raw)
  To: gcc-bugs

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

--- Comment #3 from Jonathan Wakely <redi at gcc dot gnu.org> ---
(In reply to Jonathan Wakely from comment #2)
> If you write it like this you get good codegen:

I think I messed up my testing, and it doesn't help. GCC always chooses the
synthesized operator> instead of the intended one, so always produces poor
code.

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

* [Bug c++/100322] Switching from std=c++17 to std=c++20 causes performance regression in relationals
  2021-04-28 17:15 [Bug c++/100322] New: Switching from std=c++17 to std=c++20 causes performance regression in relationals aaron at aarongraham dot com
                   ` (2 preceding siblings ...)
  2021-04-28 18:13 ` redi at gcc dot gnu.org
@ 2021-04-28 18:18 ` barry.revzin at gmail dot com
  2021-04-28 18:19 ` barry.revzin at gmail dot com
                   ` (3 subsequent siblings)
  7 siblings, 0 replies; 9+ messages in thread
From: barry.revzin at gmail dot com @ 2021-04-28 18:18 UTC (permalink / raw)
  To: gcc-bugs

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

Barry Revzin <barry.revzin at gmail dot com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |barry.revzin at gmail dot com

--- Comment #4 from Barry Revzin <barry.revzin at gmail dot com> ---
Reduced to avoid chrono: https://godbolt.org/z/o96oz9d6K

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

* [Bug c++/100322] Switching from std=c++17 to std=c++20 causes performance regression in relationals
  2021-04-28 17:15 [Bug c++/100322] New: Switching from std=c++17 to std=c++20 causes performance regression in relationals aaron at aarongraham dot com
                   ` (3 preceding siblings ...)
  2021-04-28 18:18 ` barry.revzin at gmail dot com
@ 2021-04-28 18:19 ` barry.revzin at gmail dot com
  2021-04-28 18:20 ` redi at gcc dot gnu.org
                   ` (2 subsequent siblings)
  7 siblings, 0 replies; 9+ messages in thread
From: barry.revzin at gmail dot com @ 2021-04-28 18:19 UTC (permalink / raw)
  To: gcc-bugs

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

--- Comment #5 from Barry Revzin <barry.revzin at gmail dot com> ---
Sorry meant to actually copy the reduction:

#include <compare> 

bool compare_count(int a, int b)
{
    return a > b;
}

bool compare(int a, int b)
{
    return (a <=> b) > 0;
}

which generates:

compare_count(int, int):
        cmp     r0, r1
        ite     le
        movle   r0, #0
        movgt   r0, #1
        bx      lr
compare(int, int):
        cmp     r0, r1
        beq     .L4
        blt     .L5
        movs    r0, #1
.L3:
        cmp     r0, #0
        ite     le
        movle   r0, #0
        movgt   r0, #1
        bx      lr
.L4:
        movs    r0, #0
        b       .L3
.L5:
        mov     r0, #-1
        b       .L3

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

* [Bug c++/100322] Switching from std=c++17 to std=c++20 causes performance regression in relationals
  2021-04-28 17:15 [Bug c++/100322] New: Switching from std=c++17 to std=c++20 causes performance regression in relationals aaron at aarongraham dot com
                   ` (4 preceding siblings ...)
  2021-04-28 18:19 ` barry.revzin at gmail dot com
@ 2021-04-28 18:20 ` redi at gcc dot gnu.org
  2021-04-28 22:55 ` glisse at gcc dot gnu.org
  2021-04-29  8:55 ` redi at gcc dot gnu.org
  7 siblings, 0 replies; 9+ messages in thread
From: redi at gcc dot gnu.org @ 2021-04-28 18:20 UTC (permalink / raw)
  To: gcc-bugs

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

--- Comment #6 from Jonathan Wakely <redi at gcc dot gnu.org> ---
So it's just bad codegen for <=> comparisons. Thanks, Barry.

The library part is a red herring, the operator> synthesized from <=> is fine
to use, it just produces bad code (with GCC, but not Clang).

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

* [Bug c++/100322] Switching from std=c++17 to std=c++20 causes performance regression in relationals
  2021-04-28 17:15 [Bug c++/100322] New: Switching from std=c++17 to std=c++20 causes performance regression in relationals aaron at aarongraham dot com
                   ` (5 preceding siblings ...)
  2021-04-28 18:20 ` redi at gcc dot gnu.org
@ 2021-04-28 22:55 ` glisse at gcc dot gnu.org
  2021-04-29  8:55 ` redi at gcc dot gnu.org
  7 siblings, 0 replies; 9+ messages in thread
From: glisse at gcc dot gnu.org @ 2021-04-28 22:55 UTC (permalink / raw)
  To: gcc-bugs

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

--- Comment #7 from Marc Glisse <glisse at gcc dot gnu.org> ---
PR94589 then.

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

* [Bug c++/100322] Switching from std=c++17 to std=c++20 causes performance regression in relationals
  2021-04-28 17:15 [Bug c++/100322] New: Switching from std=c++17 to std=c++20 causes performance regression in relationals aaron at aarongraham dot com
                   ` (6 preceding siblings ...)
  2021-04-28 22:55 ` glisse at gcc dot gnu.org
@ 2021-04-29  8:55 ` redi at gcc dot gnu.org
  7 siblings, 0 replies; 9+ messages in thread
From: redi at gcc dot gnu.org @ 2021-04-29  8:55 UTC (permalink / raw)
  To: gcc-bugs

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

Jonathan Wakely <redi at gcc dot gnu.org> changed:

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

--- Comment #8 from Jonathan Wakely <redi at gcc dot gnu.org> ---
Ah yes, thanks, Marc.

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

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

end of thread, other threads:[~2021-04-29  8:55 UTC | newest]

Thread overview: 9+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2021-04-28 17:15 [Bug c++/100322] New: Switching from std=c++17 to std=c++20 causes performance regression in relationals aaron at aarongraham dot com
2021-04-28 17:49 ` [Bug c++/100322] " redi at gcc dot gnu.org
2021-04-28 18:03 ` redi at gcc dot gnu.org
2021-04-28 18:13 ` redi at gcc dot gnu.org
2021-04-28 18:18 ` barry.revzin at gmail dot com
2021-04-28 18:19 ` barry.revzin at gmail dot com
2021-04-28 18:20 ` redi at gcc dot gnu.org
2021-04-28 22:55 ` glisse at gcc dot gnu.org
2021-04-29  8:55 ` redi 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).