public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
* [Bug c++/107462] New: Missed optimization of std::atomic::fetch_xxx "null operations"  to std::atomic::load()
@ 2022-10-30  9:29 marko.makela at mariadb dot com
  2022-10-30 11:02 ` [Bug tree-optimization/107462] " pinskia at gcc dot gnu.org
  2023-05-19  3:08 ` pinskia at gcc dot gnu.org
  0 siblings, 2 replies; 3+ messages in thread
From: marko.makela at mariadb dot com @ 2022-10-30  9:29 UTC (permalink / raw)
  To: gcc-bugs

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

            Bug ID: 107462
           Summary: Missed optimization of std::atomic::fetch_xxx "null
                    operations"  to std::atomic::load()
           Product: gcc
           Version: 12.2.0
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: c++
          Assignee: unassigned at gcc dot gnu.org
          Reporter: marko.makela at mariadb dot com
  Target Milestone: ---

In g++-12 -c -O2 targeting x86-64, the fetch_xxx() variants are translated to
LOCK XADD or loops around LOCK CMPXCHG, instead of the simple MOV. I did not
test other targets.

#include <atomic>
uint32_t load(const std::atomic<uint32_t> &a)
{ return a.load(std::memory_order_relaxed); }
uint32_t load_add(std::atomic<uint32_t> &a)
{ return a.fetch_add(0, std::memory_order_relaxed); }
uint32_t load_sub(std::atomic<uint32_t> &a)
{ return a.fetch_sub(0, std::memory_order_relaxed); }
uint32_t load_or(std::atomic<uint32_t> &a)
{ return a.fetch_or(0, std::memory_order_relaxed); }
uint32_t load_xor(std::atomic<uint32_t> &a)
{ return a.fetch_xor(0, std::memory_order_relaxed); }
uint32_t load_and(std::atomic<uint32_t> &a)
{ return a.fetch_and(~uint32_t(0), std::memory_order_relaxed); }

clang++-15 would translate each function to the same x86-64 code:
   0:   8b 07                   mov    (%rdi),%eax
   2:   c3                      ret
When using the default std::memory_order_seq_cst, an MFENCE instruction would
be emitted before the MOV except in load().

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

* [Bug tree-optimization/107462] Missed optimization of std::atomic::fetch_xxx "null operations"  to std::atomic::load()
  2022-10-30  9:29 [Bug c++/107462] New: Missed optimization of std::atomic::fetch_xxx "null operations" to std::atomic::load() marko.makela at mariadb dot com
@ 2022-10-30 11:02 ` pinskia at gcc dot gnu.org
  2023-05-19  3:08 ` pinskia at gcc dot gnu.org
  1 sibling, 0 replies; 3+ messages in thread
From: pinskia at gcc dot gnu.org @ 2022-10-30 11:02 UTC (permalink / raw)
  To: gcc-bugs

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

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

           What    |Removed                     |Added
----------------------------------------------------------------------------
          Component|c++                         |tree-optimization
           Keywords|                            |missed-optimization

--- Comment #1 from Andrew Pinski <pinskia at gcc dot gnu.org> ---
There might be a dup of this bug already but gcc does not try to optimize fetch
and add to just fetch if the operation was going to be a nop.

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

* [Bug tree-optimization/107462] Missed optimization of std::atomic::fetch_xxx "null operations"  to std::atomic::load()
  2022-10-30  9:29 [Bug c++/107462] New: Missed optimization of std::atomic::fetch_xxx "null operations" to std::atomic::load() marko.makela at mariadb dot com
  2022-10-30 11:02 ` [Bug tree-optimization/107462] " pinskia at gcc dot gnu.org
@ 2023-05-19  3:08 ` pinskia at gcc dot gnu.org
  1 sibling, 0 replies; 3+ messages in thread
From: pinskia at gcc dot gnu.org @ 2023-05-19  3:08 UTC (permalink / raw)
  To: gcc-bugs

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

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

           What    |Removed                     |Added
----------------------------------------------------------------------------
           Severity|normal                      |enhancement
             Status|UNCONFIRMED                 |NEW
     Ever confirmed|0                           |1
   Last reconfirmed|                            |2023-05-19

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

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

end of thread, other threads:[~2023-05-19  3:08 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2022-10-30  9:29 [Bug c++/107462] New: Missed optimization of std::atomic::fetch_xxx "null operations" to std::atomic::load() marko.makela at mariadb dot com
2022-10-30 11:02 ` [Bug tree-optimization/107462] " pinskia at gcc dot gnu.org
2023-05-19  3:08 ` 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).