James Kuszmaul | cf32412 | 2023-01-14 14:07:17 -0800 | [diff] [blame^] | 1 | From bc86b62f72cbb76a0911996f4b1c6ce476cd1fac Mon Sep 17 00:00:00 2001 |
| 2 | From: PJ Reiniger <pj.reiniger@gmail.com> |
| 3 | Date: Sat, 7 May 2022 22:17:19 -0400 |
| 4 | Subject: [PATCH 05/28] Threading updates |
| 5 | |
| 6 | - Remove guards for threads and exception |
| 7 | - Prefer scope gaurd over lock gaurd |
| 8 | --- |
| 9 | llvm/include/llvm/Support/Compiler.h | 6 ----- |
| 10 | llvm/lib/Support/ErrorHandling.cpp | 38 +++++----------------------- |
| 11 | llvm/lib/Support/ManagedStatic.cpp | 10 ++++---- |
| 12 | 3 files changed, 11 insertions(+), 43 deletions(-) |
| 13 | |
| 14 | diff --git a/llvm/include/llvm/Support/Compiler.h b/llvm/include/llvm/Support/Compiler.h |
| 15 | index f5d726ec8..ede1cb172 100644 |
| 16 | --- a/llvm/include/llvm/Support/Compiler.h |
| 17 | +++ b/llvm/include/llvm/Support/Compiler.h |
| 18 | @@ -540,7 +540,6 @@ void AnnotateIgnoreWritesEnd(const char *file, int line); |
| 19 | /// initialize to some constant value. In almost all circumstances this is most |
| 20 | /// appropriate for use with a pointer, integer, or small aggregation of |
| 21 | /// pointers and integers. |
| 22 | -#if LLVM_ENABLE_THREADS |
| 23 | #if __has_feature(cxx_thread_local) || defined(_MSC_VER) |
| 24 | #define LLVM_THREAD_LOCAL thread_local |
| 25 | #else |
| 26 | @@ -548,11 +547,6 @@ void AnnotateIgnoreWritesEnd(const char *file, int line); |
| 27 | // we only need the restricted functionality that provides. |
| 28 | #define LLVM_THREAD_LOCAL __thread |
| 29 | #endif |
| 30 | -#else // !LLVM_ENABLE_THREADS |
| 31 | -// If threading is disabled entirely, this compiles to nothing and you get |
| 32 | -// a normal global variable. |
| 33 | -#define LLVM_THREAD_LOCAL |
| 34 | -#endif |
| 35 | |
| 36 | /// \macro LLVM_ENABLE_EXCEPTIONS |
| 37 | /// Whether LLVM is built with exception support. |
| 38 | diff --git a/llvm/lib/Support/ErrorHandling.cpp b/llvm/lib/Support/ErrorHandling.cpp |
| 39 | index 8ae8fb8b4..89440b5ab 100644 |
| 40 | --- a/llvm/lib/Support/ErrorHandling.cpp |
| 41 | +++ b/llvm/lib/Support/ErrorHandling.cpp |
| 42 | @@ -44,7 +44,6 @@ static void *ErrorHandlerUserData = nullptr; |
| 43 | static fatal_error_handler_t BadAllocErrorHandler = nullptr; |
| 44 | static void *BadAllocErrorHandlerUserData = nullptr; |
| 45 | |
| 46 | -#if LLVM_ENABLE_THREADS == 1 |
| 47 | // Mutexes to synchronize installing error handlers and calling error handlers. |
| 48 | // Do not use ManagedStatic, or that may allocate memory while attempting to |
| 49 | // report an OOM. |
| 50 | @@ -58,22 +57,17 @@ static void *BadAllocErrorHandlerUserData = nullptr; |
| 51 | // builds. We can remove these ifdefs if that script goes away. |
| 52 | static std::mutex ErrorHandlerMutex; |
| 53 | static std::mutex BadAllocErrorHandlerMutex; |
| 54 | -#endif |
| 55 | |
| 56 | void llvm::install_fatal_error_handler(fatal_error_handler_t handler, |
| 57 | void *user_data) { |
| 58 | -#if LLVM_ENABLE_THREADS == 1 |
| 59 | - std::lock_guard<std::mutex> Lock(ErrorHandlerMutex); |
| 60 | -#endif |
| 61 | + std::scoped_lock Lock(ErrorHandlerMutex); |
| 62 | assert(!ErrorHandler && "Error handler already registered!\n"); |
| 63 | ErrorHandler = handler; |
| 64 | ErrorHandlerUserData = user_data; |
| 65 | } |
| 66 | |
| 67 | void llvm::remove_fatal_error_handler() { |
| 68 | -#if LLVM_ENABLE_THREADS == 1 |
| 69 | - std::lock_guard<std::mutex> Lock(ErrorHandlerMutex); |
| 70 | -#endif |
| 71 | + std::scoped_lock Lock(ErrorHandlerMutex); |
| 72 | ErrorHandler = nullptr; |
| 73 | ErrorHandlerUserData = nullptr; |
| 74 | } |
| 75 | @@ -92,9 +86,7 @@ void llvm::report_fatal_error(std::string_view Reason, bool GenCrashDiag) { |
| 76 | { |
| 77 | // Only acquire the mutex while reading the handler, so as not to invoke a |
| 78 | // user-supplied callback under a lock. |
| 79 | -#if LLVM_ENABLE_THREADS == 1 |
| 80 | - std::lock_guard<std::mutex> Lock(ErrorHandlerMutex); |
| 81 | -#endif |
| 82 | + std::scoped_lock Lock(ErrorHandlerMutex); |
| 83 | handler = ErrorHandler; |
| 84 | handlerData = ErrorHandlerUserData; |
| 85 | } |
| 86 | @@ -123,18 +115,14 @@ void llvm::report_fatal_error(std::string_view Reason, bool GenCrashDiag) { |
| 87 | |
| 88 | void llvm::install_bad_alloc_error_handler(fatal_error_handler_t handler, |
| 89 | void *user_data) { |
| 90 | -#if LLVM_ENABLE_THREADS == 1 |
| 91 | - std::lock_guard<std::mutex> Lock(BadAllocErrorHandlerMutex); |
| 92 | -#endif |
| 93 | + std::scoped_lock Lock(BadAllocErrorHandlerMutex); |
| 94 | assert(!ErrorHandler && "Bad alloc error handler already registered!\n"); |
| 95 | BadAllocErrorHandler = handler; |
| 96 | BadAllocErrorHandlerUserData = user_data; |
| 97 | } |
| 98 | |
| 99 | void llvm::remove_bad_alloc_error_handler() { |
| 100 | -#if LLVM_ENABLE_THREADS == 1 |
| 101 | - std::lock_guard<std::mutex> Lock(BadAllocErrorHandlerMutex); |
| 102 | -#endif |
| 103 | + std::scoped_lock Lock(BadAllocErrorHandlerMutex); |
| 104 | BadAllocErrorHandler = nullptr; |
| 105 | BadAllocErrorHandlerUserData = nullptr; |
| 106 | } |
| 107 | @@ -145,9 +133,7 @@ void llvm::report_bad_alloc_error(const char *Reason, bool GenCrashDiag) { |
| 108 | { |
| 109 | // Only acquire the mutex while reading the handler, so as not to invoke a |
| 110 | // user-supplied callback under a lock. |
| 111 | -#if LLVM_ENABLE_THREADS == 1 |
| 112 | - std::lock_guard<std::mutex> Lock(BadAllocErrorHandlerMutex); |
| 113 | -#endif |
| 114 | + std::scoped_lock Lock(BadAllocErrorHandlerMutex); |
| 115 | Handler = BadAllocErrorHandler; |
| 116 | HandlerData = BadAllocErrorHandlerUserData; |
| 117 | } |
| 118 | @@ -157,10 +143,6 @@ void llvm::report_bad_alloc_error(const char *Reason, bool GenCrashDiag) { |
| 119 | llvm_unreachable("bad alloc handler should not return"); |
| 120 | } |
| 121 | |
| 122 | -#ifdef LLVM_ENABLE_EXCEPTIONS |
| 123 | - // If exceptions are enabled, make OOM in malloc look like OOM in new. |
| 124 | - throw std::bad_alloc(); |
| 125 | -#else |
| 126 | // Don't call the normal error handler. It may allocate memory. Directly write |
| 127 | // an OOM to stderr and abort. |
| 128 | const char *OOMMessage = "LLVM ERROR: out of memory\n"; |
| 129 | @@ -169,15 +151,8 @@ void llvm::report_bad_alloc_error(const char *Reason, bool GenCrashDiag) { |
| 130 | (void)!::write(2, Reason, strlen(Reason)); |
| 131 | (void)!::write(2, Newline, strlen(Newline)); |
| 132 | abort(); |
| 133 | -#endif |
| 134 | } |
| 135 | |
| 136 | -#ifdef LLVM_ENABLE_EXCEPTIONS |
| 137 | -// Do not set custom new handler if exceptions are enabled. In this case OOM |
| 138 | -// errors are handled by throwing 'std::bad_alloc'. |
| 139 | -void llvm::install_out_of_memory_new_handler() { |
| 140 | -} |
| 141 | -#else |
| 142 | // Causes crash on allocation failure. It is called prior to the handler set by |
| 143 | // 'install_bad_alloc_error_handler'. |
| 144 | static void out_of_memory_new_handler() { |
| 145 | @@ -192,7 +167,6 @@ void llvm::install_out_of_memory_new_handler() { |
| 146 | assert((old == nullptr || old == out_of_memory_new_handler) && |
| 147 | "new-handler already installed"); |
| 148 | } |
| 149 | -#endif |
| 150 | |
| 151 | void llvm::llvm_unreachable_internal(const char *msg, const char *file, |
| 152 | unsigned line) { |
| 153 | diff --git a/llvm/lib/Support/ManagedStatic.cpp b/llvm/lib/Support/ManagedStatic.cpp |
| 154 | index a6ae67066..fc798b7ec 100644 |
| 155 | --- a/llvm/lib/Support/ManagedStatic.cpp |
| 156 | +++ b/llvm/lib/Support/ManagedStatic.cpp |
| 157 | @@ -12,23 +12,23 @@ |
| 158 | |
| 159 | #include "llvm/Support/ManagedStatic.h" |
| 160 | #include "llvm/Config/config.h" |
| 161 | -#include "llvm/Support/Threading.h" |
| 162 | +#include "llvm/Support/mutex.h" |
| 163 | #include <cassert> |
| 164 | #include <mutex> |
| 165 | using namespace llvm; |
| 166 | |
| 167 | static const ManagedStaticBase *StaticList = nullptr; |
| 168 | |
| 169 | -static std::recursive_mutex *getManagedStaticMutex() { |
| 170 | - static std::recursive_mutex m; |
| 171 | +static llvm::mutex *getManagedStaticMutex() { |
| 172 | + static llvm::mutex m; |
| 173 | return &m; |
| 174 | } |
| 175 | |
| 176 | void ManagedStaticBase::RegisterManagedStatic(void *(*Creator)(), |
| 177 | void (*Deleter)(void*)) const { |
| 178 | assert(Creator); |
| 179 | - if (llvm_is_multithreaded()) { |
| 180 | - std::lock_guard<std::recursive_mutex> Lock(*getManagedStaticMutex()); |
| 181 | + if (1) { |
| 182 | + std::scoped_lock Lock(*getManagedStaticMutex()); |
| 183 | |
| 184 | if (!Ptr.load(std::memory_order_relaxed)) { |
| 185 | void *Tmp = Creator(); |