[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
gcc-bugzilla@gcc.gnu.org
Sat Oct 2 16:08:46 GMT 2021
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
More information about the Gcc-bugs
mailing list