-
Notifications
You must be signed in to change notification settings - Fork 13.5k
[compiler-rt][builtins] Switch libatomic locks to pthread_mutex_t. #94374
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
✅ With the latest revision this PR passed the C/C++ code formatter. |
When an uninstrumented libatomic is used with a TSan instrumented memcpy, TSan may report a data race in circumstances where writes are arguably safe. This occurs because __atomic_compare_exchange won't be instrumented in an uninstrumented libatomic, so TSan doesn't know that the subsequent memcpy is race-free. On the other hand, pthread_mutex_(un)lock will be intercepted by TSan, meaning an uninstrumented libatomic will not report this false-positive. pthread_mutexes also may try a number of different strategies to acquire the lock, which may bound the amount of time a thread has to wait for a lock during contention. While pthread_mutex_lock has a larger overhead (due to the function call and some dispatching), a dispatch to libatomic already predicates a lack of performance guarantees.
LGTM. It's perhaps worth mentioning that GCC libatomic also uses pthread_mutex_t. So this change would bring three advantages:
It seems that glibc's |
It seems that we forgot PTHREAD_MUTEX_INITIALIZER (benign issue since it is all zeroes in glibc and musl). Perhaps we should avoid false sharing as well: |
This PR broke the armv8-a builtins build in Android toolchain - which can lower |
This also broke builds for Windows: https://github.com/mstorsjo/llvm-mingw/actions/runs/9376329007/job/25816083980
This sounds like a good idea, but I'd also like to make it dependent on platform. E.g. on Windows, libpthread may exist - there are third party libraries that provide the pthreads API, but we don't want to use it here even if it happens to exist while building compiler-rt. |
Yeah, i agree that users should have a choice to opt-in to pthreads. As it seems like there are a few things to clarify, should this patch be reverted? |
Yes, this should be reverted for now. I can push a revert shortly. |
Reverted now in a4b32c2. |
Perhaps we can keep the current spin locks for |
The breakage in the Android toolchain is not when targeting Android but a baremetal configuration needed in Android. So the above will not work. This target does set |
When an uninstrumented libatomic is used with a TSan instrumented
memcpy, TSan may report a data race in circumstances where writes are
arguably safe.
This occurs because __atomic_compare_exchange won't be instrumented in
an uninstrumented libatomic, so TSan doesn't know that the subsequent
memcpy is race-free.
On the other hand, pthread_mutex_(un)lock will be intercepted by TSan,
meaning an uninstrumented libatomic will not report this false-positive.
pthread_mutexes also may try a number of different strategies to acquire
the lock, which may bound the amount of time a thread has to wait for a
lock during contention.
While pthread_mutex_lock has a larger overhead (due to the function
call and some dispatching), a dispatch to libatomic already predicates
a lack of performance guarantees.