From 4cf0a9d4ae76f46ee2833f8479bb712b072673f3 Mon Sep 17 00:00:00 2001 From: =?utf8?q?Nicolai=20H=C3=A4hnle?= Date: Wed, 29 Jun 2022 16:20:03 +0200 Subject: [PATCH] ManagedStatic: remove from Interpreter/ExternalFunctions Differential Revision: https://reviews.llvm.org/D129124 --- .../Interpreter/ExternalFunctions.cpp | 71 +++++++++++++--------- 1 file changed, 42 insertions(+), 29 deletions(-) diff --git a/llvm/lib/ExecutionEngine/Interpreter/ExternalFunctions.cpp b/llvm/lib/ExecutionEngine/Interpreter/ExternalFunctions.cpp index c3ba5eb..29f481a 100644 --- a/llvm/lib/ExecutionEngine/Interpreter/ExternalFunctions.cpp +++ b/llvm/lib/ExecutionEngine/Interpreter/ExternalFunctions.cpp @@ -30,7 +30,6 @@ #include "llvm/Support/Casting.h" #include "llvm/Support/DynamicLibrary.h" #include "llvm/Support/ErrorHandling.h" -#include "llvm/Support/ManagedStatic.h" #include "llvm/Support/Mutex.h" #include "llvm/Support/raw_ostream.h" #include @@ -57,16 +56,26 @@ using namespace llvm; -static ManagedStatic FunctionsLock; +namespace { typedef GenericValue (*ExFunc)(FunctionType *, ArrayRef); -static ManagedStatic > ExportedFunctions; -static ManagedStatic > FuncNames; +typedef void (*RawFunc)(); +struct Functions { + sys::Mutex Lock; + std::map ExportedFunctions; + std::map FuncNames; #ifdef USE_LIBFFI -typedef void (*RawFunc)(); -static ManagedStatic > RawFunctions; + std::map RawFunctions; #endif +}; + +Functions &getFunctions() { + static Functions F; + return F; +} + +} // anonymous namespace static Interpreter *TheInterpreter; @@ -107,15 +116,16 @@ static ExFunc lookupFunction(const Function *F) { ExtName += getTypeID(T); ExtName += ("_" + F->getName()).str(); - sys::ScopedLock Writer(*FunctionsLock); - ExFunc FnPtr = (*FuncNames)[ExtName]; + auto &Fns = getFunctions(); + sys::ScopedLock Writer(Fns.Lock); + ExFunc FnPtr = Fns.FuncNames[ExtName]; if (!FnPtr) - FnPtr = (*FuncNames)[("lle_X_" + F->getName()).str()]; + FnPtr = Fns.FuncNames[("lle_X_" + F->getName()).str()]; if (!FnPtr) // Try calling a generic function... if it exists... FnPtr = (ExFunc)(intptr_t)sys::DynamicLibrary::SearchForAddressOfSymbol( ("lle_X_" + F->getName()).str()); if (FnPtr) - ExportedFunctions->insert(std::make_pair(F, FnPtr)); // Cache for later + Fns.ExportedFunctions.insert(std::make_pair(F, FnPtr)); // Cache for later return FnPtr; } @@ -260,27 +270,29 @@ GenericValue Interpreter::callExternalFunction(Function *F, ArrayRef ArgVals) { TheInterpreter = this; - std::unique_lock Guard(*FunctionsLock); + auto &Fns = getFunctions(); + std::unique_lock Guard(Fns.Lock); // Do a lookup to see if the function is in our cache... this should just be a // deferred annotation! - std::map::iterator FI = ExportedFunctions->find(F); - if (ExFunc Fn = (FI == ExportedFunctions->end()) ? lookupFunction(F) - : FI->second) { + std::map::iterator FI = + Fns.ExportedFunctions.find(F); + if (ExFunc Fn = (FI == Fns.ExportedFunctions.end()) ? lookupFunction(F) + : FI->second) { Guard.unlock(); return Fn(F->getFunctionType(), ArgVals); } #ifdef USE_LIBFFI - std::map::iterator RF = RawFunctions->find(F); + std::map::iterator RF = Fns.RawFunctions.find(F); RawFunc RawFn; - if (RF == RawFunctions->end()) { + if (RF == Fns.RawFunctions.end()) { RawFn = (RawFunc)(intptr_t) sys::DynamicLibrary::SearchForAddressOfSymbol(std::string(F->getName())); if (!RawFn) RawFn = (RawFunc)(intptr_t)getPointerToGlobalIfAvailable(F); if (RawFn != 0) - RawFunctions->insert(std::make_pair(F, RawFn)); // Cache for later + Fns.RawFunctions.insert(std::make_pair(F, RawFn)); // Cache for later } else { RawFn = RF->second; } @@ -496,16 +508,17 @@ static GenericValue lle_X_memcpy(FunctionType *FT, } void Interpreter::initializeExternalFunctions() { - sys::ScopedLock Writer(*FunctionsLock); - (*FuncNames)["lle_X_atexit"] = lle_X_atexit; - (*FuncNames)["lle_X_exit"] = lle_X_exit; - (*FuncNames)["lle_X_abort"] = lle_X_abort; - - (*FuncNames)["lle_X_printf"] = lle_X_printf; - (*FuncNames)["lle_X_sprintf"] = lle_X_sprintf; - (*FuncNames)["lle_X_sscanf"] = lle_X_sscanf; - (*FuncNames)["lle_X_scanf"] = lle_X_scanf; - (*FuncNames)["lle_X_fprintf"] = lle_X_fprintf; - (*FuncNames)["lle_X_memset"] = lle_X_memset; - (*FuncNames)["lle_X_memcpy"] = lle_X_memcpy; + auto &Fns = getFunctions(); + sys::ScopedLock Writer(Fns.Lock); + Fns.FuncNames["lle_X_atexit"] = lle_X_atexit; + Fns.FuncNames["lle_X_exit"] = lle_X_exit; + Fns.FuncNames["lle_X_abort"] = lle_X_abort; + + Fns.FuncNames["lle_X_printf"] = lle_X_printf; + Fns.FuncNames["lle_X_sprintf"] = lle_X_sprintf; + Fns.FuncNames["lle_X_sscanf"] = lle_X_sscanf; + Fns.FuncNames["lle_X_scanf"] = lle_X_scanf; + Fns.FuncNames["lle_X_fprintf"] = lle_X_fprintf; + Fns.FuncNames["lle_X_memset"] = lle_X_memset; + Fns.FuncNames["lle_X_memcpy"] = lle_X_memcpy; } -- 2.7.4