Files
allwpilib/upstream_utils/llvm_patches/0004-Threading-updates.patch
2026-05-26 21:55:50 -07:00

199 lines
7.8 KiB
Diff

From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001
From: PJ Reiniger <pj.reiniger@gmail.com>
Date: Sat, 7 May 2022 22:17:19 -0400
Subject: [PATCH 04/34] Threading updates
- Remove guards for threads and exception
- Replace std::lock_guard with std::scoped_lock
---
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 a160164a33903408d60d6e8a57462a3b8c8623e2..f2b356ef04be557795afe9c356acc4af53fcdf52 100644
--- a/llvm/include/llvm/Support/Compiler.h
+++ b/llvm/include/llvm/Support/Compiler.h
@@ -665,7 +665,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
@@ -673,11 +672,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 fc9d118e85508cdf86553bb63cce4a1c567045b7..7ab8a97bb85929615fc751a1eda1fee62004a26f 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/Errno.h"
@@ -46,7 +45,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.
@@ -60,7 +58,6 @@ static void *BadAllocErrorHandlerUserData = nullptr;
// builds. We can remove these ifdefs if that script goes away.
static std::mutex ErrorHandlerMutex;
static std::mutex BadAllocErrorHandlerMutex;
-#endif
static bool write_retry(int fd, const char *buf, size_t count) {
while (count > 0) {
@@ -75,18 +72,14 @@ static bool write_retry(int fd, const char *buf, size_t count) {
void llvm::install_fatal_error_handler(fatal_error_handler_t handler,
void *user_data) {
-#if LLVM_ENABLE_THREADS == 1
- std::lock_guard<std::mutex> 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<std::mutex> Lock(ErrorHandlerMutex);
-#endif
+ std::scoped_lock Lock(ErrorHandlerMutex);
ErrorHandler = nullptr;
ErrorHandlerUserData = nullptr;
}
@@ -105,9 +98,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<std::mutex> Lock(ErrorHandlerMutex);
-#endif
+ std::scoped_lock Lock(ErrorHandlerMutex);
handler = ErrorHandler;
handlerData = ErrorHandlerUserData;
}
@@ -157,9 +148,7 @@ void llvm::reportFatalUsageError(std::string_view reason) {
void llvm::install_bad_alloc_error_handler(fatal_error_handler_t handler,
void *user_data) {
-#if LLVM_ENABLE_THREADS == 1
- std::lock_guard<std::mutex> Lock(BadAllocErrorHandlerMutex);
-#endif
+ std::scoped_lock Lock(BadAllocErrorHandlerMutex);
assert(!BadAllocErrorHandler &&
"Bad alloc error handler already registered!\n");
BadAllocErrorHandler = handler;
@@ -167,9 +156,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<std::mutex> Lock(BadAllocErrorHandlerMutex);
-#endif
+ std::scoped_lock Lock(BadAllocErrorHandlerMutex);
BadAllocErrorHandler = nullptr;
BadAllocErrorHandlerUserData = nullptr;
}
@@ -180,9 +167,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<std::mutex> Lock(BadAllocErrorHandlerMutex);
-#endif
+ std::scoped_lock Lock(BadAllocErrorHandlerMutex);
Handler = BadAllocErrorHandler;
HandlerData = BadAllocErrorHandlerUserData;
}
@@ -192,10 +177,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";
@@ -204,15 +185,8 @@ void llvm::report_bad_alloc_error(const char *Reason, bool GenCrashDiag) {
write_retry(2, Reason, strlen(Reason));
write_retry(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() {
@@ -227,7 +201,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 <cassert>
#include <mutex>
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<std::recursive_mutex> Lock(*getManagedStaticMutex());
+ if (1) {
+ std::scoped_lock Lock(*getManagedStaticMutex());
if (!Ptr.load(std::memory_order_relaxed)) {
void *Tmp = Creator();