From 146f9d2333bc0aeed1593dc659b17e65b38a33e7 Mon Sep 17 00:00:00 2001 From: Daan Date: Wed, 24 Apr 2024 19:48:04 -0700 Subject: [PATCH] make TLS slot default instead of __builtin_thread_pointer. Potentially fixes build errors on various platforms (see issue #883) --- include/mimalloc/prim.h | 29 +++++++++++++++-------------- 1 file changed, 15 insertions(+), 14 deletions(-) diff --git a/include/mimalloc/prim.h b/include/mimalloc/prim.h index f8a40323..6063d44a 100644 --- a/include/mimalloc/prim.h +++ b/include/mimalloc/prim.h @@ -203,11 +203,14 @@ static inline void mi_prim_tls_slot_set(size_t slot, void* value) mi_attr_noexce #endif -// Do we have __builtin_thread_pointer? (do not make this a compound test as it fails on older gcc's, see issue #851) -#if defined(__has_builtin) -#if __has_builtin(__builtin_thread_pointer) -#define MI_HAS_BUILTIN_THREAD_POINTER 1 -#endif +// Do we have __builtin_thread_pointer? (do not make this a compound test as that fails on older gcc's, see issue #851) +#ifdef __has_builtin + #if __has_builtin(__builtin_thread_pointer) + #if (!defined(__APPLE__)) && /* on apple (M1) the wrong register is read (tpidr_el0 instead of tpidrro_el0) so fall back to TLS slot assembly ()*/ \ + (!defined(__clang_major__) || __clang_major__ >= 14) // older clang versions emit bad code; fall back to using the TLS slot () + #define MI_HAS_BUILTIN_THREAD_POINTER 1 + #endif + #endif #elif defined(__GNUC__) && (__GNUC__ >= 7) && defined(__aarch64__) // special case aarch64 for older gcc versions (issue #851) #define MI_HAS_BUILTIN_THREAD_POINTER 1 #endif @@ -231,15 +234,6 @@ static inline mi_threadid_t _mi_prim_thread_id(void) mi_attr_noexcept { return (uintptr_t)NtCurrentTeb(); } -#elif MI_HAS_BUILTIN_THREAD_POINTER && \ - (!defined(__APPLE__)) && /* on apple (M1) the wrong register is read (tpidr_el0 instead of tpidrro_el0) so fall back to TLS slot assembly ()*/ \ - (!defined(__clang_major__) || __clang_major__ >= 14) // older clang versions emit bad code; fall back to using the TLS slot () - -static inline mi_threadid_t _mi_prim_thread_id(void) mi_attr_noexcept { - // Works on most Unix based platforms - return (uintptr_t)__builtin_thread_pointer(); -} - #elif defined(MI_HAS_TLS_SLOT) static inline mi_threadid_t _mi_prim_thread_id(void) mi_attr_noexcept { @@ -255,6 +249,13 @@ static inline mi_threadid_t _mi_prim_thread_id(void) mi_attr_noexcept { #endif } +#elif MI_HAS_BUILTIN_THREAD_POINTER + +static inline mi_threadid_t _mi_prim_thread_id(void) mi_attr_noexcept { + // Works on most Unix based platforms + return (uintptr_t)__builtin_thread_pointer(); +} + #else // otherwise use portable C, taking the address of a thread local variable (this is still very fast on most platforms).