blob: 98c1d8ad7353a8fc44275096957d6d839cfd1b8d [file] [log] [blame]
James Kuszmaulb13e13f2023-11-22 20:44:04 -08001From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001
James Kuszmaulcf324122023-01-14 14:07:17 -08002From: PJ Reiniger <pj.reiniger@gmail.com>
3Date: Sat, 7 May 2022 22:17:19 -0400
James Kuszmaulb13e13f2023-11-22 20:44:04 -08004Subject: [PATCH 04/31] Threading updates
James Kuszmaulcf324122023-01-14 14:07:17 -08005
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
14diff --git a/llvm/include/llvm/Support/Compiler.h b/llvm/include/llvm/Support/Compiler.h
James Kuszmaulb13e13f2023-11-22 20:44:04 -080015index 92376629c607461061bc60597a47aed1e535af52..2662839b27bf368cd5da0668099c4b44cbc6435d 100644
James Kuszmaulcf324122023-01-14 14:07:17 -080016--- a/llvm/include/llvm/Support/Compiler.h
17+++ b/llvm/include/llvm/Support/Compiler.h
James Kuszmaulb13e13f2023-11-22 20:44:04 -080018@@ -530,7 +530,6 @@ void AnnotateIgnoreWritesEnd(const char *file, int line);
James Kuszmaulcf324122023-01-14 14:07:17 -080019 /// 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
James Kuszmaulb13e13f2023-11-22 20:44:04 -080026@@ -538,11 +537,6 @@ void AnnotateIgnoreWritesEnd(const char *file, int line);
James Kuszmaulcf324122023-01-14 14:07:17 -080027 // 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.
38diff --git a/llvm/lib/Support/ErrorHandling.cpp b/llvm/lib/Support/ErrorHandling.cpp
James Kuszmaulb13e13f2023-11-22 20:44:04 -080039index 0aa13a0f78eb370b2a673ca4a773f26820575052..637b669a7d0dae69ef4b34955f21a9fb8ba1276e 100644
James Kuszmaulcf324122023-01-14 14:07:17 -080040--- 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 }
James Kuszmaulb13e13f2023-11-22 20:44:04 -080086@@ -126,18 +118,14 @@ void llvm::report_fatal_error(std::string_view Reason, bool GenCrashDiag) {
James Kuszmaulcf324122023-01-14 14:07:17 -080087
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 }
James Kuszmaulb13e13f2023-11-22 20:44:04 -0800107@@ -148,9 +136,7 @@ void llvm::report_bad_alloc_error(const char *Reason, bool GenCrashDiag) {
James Kuszmaulcf324122023-01-14 14:07:17 -0800108 {
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 }
James Kuszmaulb13e13f2023-11-22 20:44:04 -0800118@@ -160,10 +146,6 @@ void llvm::report_bad_alloc_error(const char *Reason, bool GenCrashDiag) {
James Kuszmaulcf324122023-01-14 14:07:17 -0800119 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";
James Kuszmaulb13e13f2023-11-22 20:44:04 -0800129@@ -172,15 +154,8 @@ void llvm::report_bad_alloc_error(const char *Reason, bool GenCrashDiag) {
James Kuszmaulcf324122023-01-14 14:07:17 -0800130 (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() {
James Kuszmaulb13e13f2023-11-22 20:44:04 -0800145@@ -195,7 +170,6 @@ void llvm::install_out_of_memory_new_handler() {
James Kuszmaulcf324122023-01-14 14:07:17 -0800146 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) {
153diff --git a/llvm/lib/Support/ManagedStatic.cpp b/llvm/lib/Support/ManagedStatic.cpp
James Kuszmaulb13e13f2023-11-22 20:44:04 -0800154index a6ae67066ea0423334e8ee52106f220cd456e25e..fc798b7ec1b788e232c7374b9968dc71d4f506f0 100644
James Kuszmaulcf324122023-01-14 14:07:17 -0800155--- 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();