From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001 From: PJ Reiniger Date: Sat, 7 May 2022 22:17:19 -0400 Subject: [PATCH 04/36] Threading updates - Remove guards for threads and exception - Prefer scope gaurd over lock gaurd --- llvm/include/llvm/Support/Compiler.h | 6 ----- llvm/lib/Support/ErrorHandling.cpp | 39 +++++----------------------- llvm/lib/Support/ManagedStatic.cpp | 10 +++---- 3 files changed, 11 insertions(+), 44 deletions(-) diff --git a/llvm/include/llvm/Support/Compiler.h b/llvm/include/llvm/Support/Compiler.h index 021d1d29368deb330418ff4e08fbf3931e4b7453..586242b53bbf4ceb9bad5c9c8b495f83cb088cda 100644 --- a/llvm/include/llvm/Support/Compiler.h +++ b/llvm/include/llvm/Support/Compiler.h @@ -648,7 +648,6 @@ void AnnotateIgnoreWritesEnd(const char *file, int line); /// initialize to some constant value. In almost all circumstances this is most /// appropriate for use with a pointer, integer, or small aggregation of /// pointers and integers. -#if LLVM_ENABLE_THREADS #if __has_feature(cxx_thread_local) || defined(_MSC_VER) #define LLVM_THREAD_LOCAL thread_local #else @@ -656,11 +655,6 @@ void AnnotateIgnoreWritesEnd(const char *file, int line); // we only need the restricted functionality that provides. #define LLVM_THREAD_LOCAL __thread #endif -#else // !LLVM_ENABLE_THREADS -// If threading is disabled entirely, this compiles to nothing and you get -// a normal global variable. -#define LLVM_THREAD_LOCAL -#endif /// \macro LLVM_ENABLE_EXCEPTIONS /// Whether LLVM is built with exception support. diff --git a/llvm/lib/Support/ErrorHandling.cpp b/llvm/lib/Support/ErrorHandling.cpp index d4ca266f3c1f98337499fba4fd0675e49a7dcf18..4e1dca80a21881bfb8fa886a5921c780ca021bf2 100644 --- a/llvm/lib/Support/ErrorHandling.cpp +++ b/llvm/lib/Support/ErrorHandling.cpp @@ -15,7 +15,6 @@ #include "llvm-c/ErrorHandling.h" #include "llvm/ADT/SmallVector.h" #include "llvm/Config/config.h" -#include "llvm/Config/llvm-config.h" // for LLVM_ENABLE_THREADS #include "llvm/Support/Debug.h" #include "llvm/Support/Errc.h" #include "llvm/Support/Error.h" @@ -45,7 +44,6 @@ static void *ErrorHandlerUserData = nullptr; static fatal_error_handler_t BadAllocErrorHandler = nullptr; static void *BadAllocErrorHandlerUserData = nullptr; -#if LLVM_ENABLE_THREADS == 1 // Mutexes to synchronize installing error handlers and calling error handlers. // Do not use ManagedStatic, or that may allocate memory while attempting to // report an OOM. @@ -59,22 +57,17 @@ static void *BadAllocErrorHandlerUserData = nullptr; // builds. We can remove these ifdefs if that script goes away. static std::mutex ErrorHandlerMutex; static std::mutex BadAllocErrorHandlerMutex; -#endif void llvm::install_fatal_error_handler(fatal_error_handler_t handler, void *user_data) { -#if LLVM_ENABLE_THREADS == 1 - std::lock_guard Lock(ErrorHandlerMutex); -#endif + std::scoped_lock Lock(ErrorHandlerMutex); assert(!ErrorHandler && "Error handler already registered!\n"); ErrorHandler = handler; ErrorHandlerUserData = user_data; } void llvm::remove_fatal_error_handler() { -#if LLVM_ENABLE_THREADS == 1 - std::lock_guard Lock(ErrorHandlerMutex); -#endif + std::scoped_lock Lock(ErrorHandlerMutex); ErrorHandler = nullptr; ErrorHandlerUserData = nullptr; } @@ -93,9 +86,7 @@ void llvm::report_fatal_error(std::string_view Reason, bool GenCrashDiag) { { // Only acquire the mutex while reading the handler, so as not to invoke a // user-supplied callback under a lock. -#if LLVM_ENABLE_THREADS == 1 - std::lock_guard Lock(ErrorHandlerMutex); -#endif + std::scoped_lock Lock(ErrorHandlerMutex); handler = ErrorHandler; handlerData = ErrorHandlerUserData; } @@ -127,9 +118,7 @@ void llvm::report_fatal_error(std::string_view Reason, bool GenCrashDiag) { void llvm::install_bad_alloc_error_handler(fatal_error_handler_t handler, void *user_data) { -#if LLVM_ENABLE_THREADS == 1 - std::lock_guard Lock(BadAllocErrorHandlerMutex); -#endif + std::scoped_lock Lock(BadAllocErrorHandlerMutex); assert(!BadAllocErrorHandler && "Bad alloc error handler already registered!\n"); BadAllocErrorHandler = handler; @@ -137,9 +126,7 @@ void llvm::install_bad_alloc_error_handler(fatal_error_handler_t handler, } void llvm::remove_bad_alloc_error_handler() { -#if LLVM_ENABLE_THREADS == 1 - std::lock_guard Lock(BadAllocErrorHandlerMutex); -#endif + std::scoped_lock Lock(BadAllocErrorHandlerMutex); BadAllocErrorHandler = nullptr; BadAllocErrorHandlerUserData = nullptr; } @@ -150,9 +137,7 @@ void llvm::report_bad_alloc_error(const char *Reason, bool GenCrashDiag) { { // Only acquire the mutex while reading the handler, so as not to invoke a // user-supplied callback under a lock. -#if LLVM_ENABLE_THREADS == 1 - std::lock_guard Lock(BadAllocErrorHandlerMutex); -#endif + std::scoped_lock Lock(BadAllocErrorHandlerMutex); Handler = BadAllocErrorHandler; HandlerData = BadAllocErrorHandlerUserData; } @@ -162,10 +147,6 @@ void llvm::report_bad_alloc_error(const char *Reason, bool GenCrashDiag) { llvm_unreachable("bad alloc handler should not return"); } -#ifdef LLVM_ENABLE_EXCEPTIONS - // If exceptions are enabled, make OOM in malloc look like OOM in new. - throw std::bad_alloc(); -#else // Don't call the normal error handler. It may allocate memory. Directly write // an OOM to stderr and abort. const char *OOMMessage = "LLVM ERROR: out of memory\n"; @@ -174,15 +155,8 @@ void llvm::report_bad_alloc_error(const char *Reason, bool GenCrashDiag) { (void)!::write(2, Reason, strlen(Reason)); (void)!::write(2, Newline, strlen(Newline)); abort(); -#endif } -#ifdef LLVM_ENABLE_EXCEPTIONS -// Do not set custom new handler if exceptions are enabled. In this case OOM -// errors are handled by throwing 'std::bad_alloc'. -void llvm::install_out_of_memory_new_handler() { -} -#else // Causes crash on allocation failure. It is called prior to the handler set by // 'install_bad_alloc_error_handler'. static void out_of_memory_new_handler() { @@ -197,7 +171,6 @@ void llvm::install_out_of_memory_new_handler() { assert((old == nullptr || old == out_of_memory_new_handler) && "new-handler already installed"); } -#endif void llvm::llvm_unreachable_internal(const char *msg, const char *file, unsigned line) { diff --git a/llvm/lib/Support/ManagedStatic.cpp b/llvm/lib/Support/ManagedStatic.cpp index a6ae67066ea0423334e8ee52106f220cd456e25e..fc798b7ec1b788e232c7374b9968dc71d4f506f0 100644 --- a/llvm/lib/Support/ManagedStatic.cpp +++ b/llvm/lib/Support/ManagedStatic.cpp @@ -12,23 +12,23 @@ #include "llvm/Support/ManagedStatic.h" #include "llvm/Config/config.h" -#include "llvm/Support/Threading.h" +#include "llvm/Support/mutex.h" #include #include using namespace llvm; static const ManagedStaticBase *StaticList = nullptr; -static std::recursive_mutex *getManagedStaticMutex() { - static std::recursive_mutex m; +static llvm::mutex *getManagedStaticMutex() { + static llvm::mutex m; return &m; } void ManagedStaticBase::RegisterManagedStatic(void *(*Creator)(), void (*Deleter)(void*)) const { assert(Creator); - if (llvm_is_multithreaded()) { - std::lock_guard Lock(*getManagedStaticMutex()); + if (1) { + std::scoped_lock Lock(*getManagedStaticMutex()); if (!Ptr.load(std::memory_order_relaxed)) { void *Tmp = Creator();