public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
* [Bug target/102566] New: [i386] GCC should emit LOCK BTS for simple bit-test-and-set operations with std::atomic
@ 2021-10-02 16:08 thiago at kde dot org
  2021-10-02 20:30 ` [Bug target/102566] " pinskia at gcc dot gnu.org
                   ` (38 more replies)
  0 siblings, 39 replies; 40+ messages in thread
From: thiago at kde dot org @ 2021-10-02 16:08 UTC (permalink / raw)
  To: gcc-bugs

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

            Bug ID: 102566
           Summary: [i386] GCC should emit LOCK BTS for simple
                    bit-test-and-set operations with std::atomic
           Product: gcc
           Version: unknown
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: target
          Assignee: unassigned at gcc dot gnu.org
          Reporter: thiago at kde dot org
  Target Milestone: ---

Simple test:

$ cat test.cpp
#include <atomic>
bool tbit(std::atomic<int> &i)
{
    return i.fetch_or(1, std::memory_order_relaxed) & 1;
}

The sequence x.fetch_or(singlebit_constant) & singlebit_constant can be
implemented by a LOCK BTS sequence. The above should emit:

    lock bts $1, (%rdi)
    setb %al
    ret

But instead it emits a cmpxchg loop - see https://gcc.godbolt.org/z/99enKaffa.

This was found reviewing MariaDB lightweight-mutex code, which uses the sign
bit to indicate a contended mutex. See this commit[1] by one of their
maintainers for the removal of fetch_or because it emits an extra loop.

Bonus: LOCK BTR can be used in the sequence x.fetch_and(~single_bit_constant) &
single_bit_constant

[1]
https://github.com/dr-m/atomic_sync/commit/d5e22b2d42cdbac7a15d242bf1446377555c4041

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

end of thread, other threads:[~2023-01-18 22:30 UTC | newest]

Thread overview: 40+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2021-10-02 16:08 [Bug target/102566] New: [i386] GCC should emit LOCK BTS for simple bit-test-and-set operations with std::atomic thiago at kde dot org
2021-10-02 20:30 ` [Bug target/102566] " pinskia at gcc dot gnu.org
2021-10-03 12:56 ` hjl.tools at gmail dot com
2021-10-03 13:06 ` hjl.tools at gmail dot com
2021-10-03 13:19 ` [Bug tree-optimization/102566] " hjl.tools at gmail dot com
2021-10-03 15:10 ` hjl.tools at gmail dot com
2021-10-03 16:45 ` [Bug middle-end/102566] " hjl.tools at gmail dot com
2021-10-03 22:36 ` hjl.tools at gmail dot com
2021-10-04 15:58 ` thiago at kde dot org
2021-10-04 16:10 ` thiago at kde dot org
2021-10-04 16:13 ` thiago at kde dot org
2021-10-04 21:46 ` hjl.tools at gmail dot com
2021-10-04 23:25 ` thiago at kde dot org
2021-10-04 23:26 ` thiago at kde dot org
2021-10-05  4:40 ` hjl.tools at gmail dot com
2021-10-05 15:23 ` hjl.tools at gmail dot com
2021-10-05 15:57 ` thiago at kde dot org
2021-10-05 16:02 ` pinskia at gcc dot gnu.org
2021-10-05 19:26 ` hjl.tools at gmail dot com
2021-10-05 19:30 ` hjl.tools at gmail dot com
2021-10-05 19:36 ` thiago at kde dot org
2021-10-06  8:00 ` jakub at gcc dot gnu.org
2021-10-06 15:40 ` thiago at kde dot org
2021-10-06 15:47 ` hjl.tools at gmail dot com
2021-10-06 15:54 ` thiago at kde dot org
2021-10-06 16:05 ` hjl.tools at gmail dot com
2021-10-07 15:18 ` thiago at kde dot org
2021-10-07 15:19 ` hjl.tools at gmail dot com
2021-10-07 15:35 ` thiago at kde dot org
2021-10-10 13:51 ` hjl.tools at gmail dot com
2021-10-22  3:31 ` crazylht at gmail dot com
2021-11-04 21:24 ` thiago at kde dot org
2021-11-10  9:17 ` cvs-commit at gcc dot gnu.org
2022-10-29 10:53 ` marko.makela at mariadb dot com
2022-10-31  2:42 ` crazylht at gmail dot com
2022-11-01  8:41 ` marko.makela at mariadb dot com
2022-11-01 16:46 ` hjl.tools at gmail dot com
2022-11-07 19:20 ` cvs-commit at gcc dot gnu.org
2023-01-18 18:49 ` hjl.tools at gmail dot com
2023-01-18 22:30 ` hjl.tools at gmail dot com

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