ManagedStatic: remove from Interpreter/ExternalFunctions

Differential Revision: https://reviews.llvm.org/D129124
This commit is contained in:
Nicolai Hähnle 2022-06-29 16:20:03 +02:00
parent 75747e6e11
commit 4cf0a9d4ae
1 changed files with 42 additions and 29 deletions

View File

@ -30,7 +30,6 @@
#include "llvm/Support/Casting.h" #include "llvm/Support/Casting.h"
#include "llvm/Support/DynamicLibrary.h" #include "llvm/Support/DynamicLibrary.h"
#include "llvm/Support/ErrorHandling.h" #include "llvm/Support/ErrorHandling.h"
#include "llvm/Support/ManagedStatic.h"
#include "llvm/Support/Mutex.h" #include "llvm/Support/Mutex.h"
#include "llvm/Support/raw_ostream.h" #include "llvm/Support/raw_ostream.h"
#include <cassert> #include <cassert>
@ -57,16 +56,26 @@
using namespace llvm; using namespace llvm;
static ManagedStatic<sys::Mutex> FunctionsLock; namespace {
typedef GenericValue (*ExFunc)(FunctionType *, ArrayRef<GenericValue>); typedef GenericValue (*ExFunc)(FunctionType *, ArrayRef<GenericValue>);
static ManagedStatic<std::map<const Function *, ExFunc> > ExportedFunctions;
static ManagedStatic<std::map<std::string, ExFunc> > FuncNames;
#ifdef USE_LIBFFI
typedef void (*RawFunc)(); typedef void (*RawFunc)();
static ManagedStatic<std::map<const Function *, RawFunc> > RawFunctions;
struct Functions {
sys::Mutex Lock;
std::map<const Function *, ExFunc> ExportedFunctions;
std::map<std::string, ExFunc> FuncNames;
#ifdef USE_LIBFFI
std::map<const Function *, RawFunc> RawFunctions;
#endif #endif
};
Functions &getFunctions() {
static Functions F;
return F;
}
} // anonymous namespace
static Interpreter *TheInterpreter; static Interpreter *TheInterpreter;
@ -107,15 +116,16 @@ static ExFunc lookupFunction(const Function *F) {
ExtName += getTypeID(T); ExtName += getTypeID(T);
ExtName += ("_" + F->getName()).str(); ExtName += ("_" + F->getName()).str();
sys::ScopedLock Writer(*FunctionsLock); auto &Fns = getFunctions();
ExFunc FnPtr = (*FuncNames)[ExtName]; sys::ScopedLock Writer(Fns.Lock);
ExFunc FnPtr = Fns.FuncNames[ExtName];
if (!FnPtr) 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... if (!FnPtr) // Try calling a generic function... if it exists...
FnPtr = (ExFunc)(intptr_t)sys::DynamicLibrary::SearchForAddressOfSymbol( FnPtr = (ExFunc)(intptr_t)sys::DynamicLibrary::SearchForAddressOfSymbol(
("lle_X_" + F->getName()).str()); ("lle_X_" + F->getName()).str());
if (FnPtr) 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; return FnPtr;
} }
@ -260,27 +270,29 @@ GenericValue Interpreter::callExternalFunction(Function *F,
ArrayRef<GenericValue> ArgVals) { ArrayRef<GenericValue> ArgVals) {
TheInterpreter = this; TheInterpreter = this;
std::unique_lock<sys::Mutex> Guard(*FunctionsLock); auto &Fns = getFunctions();
std::unique_lock<sys::Mutex> Guard(Fns.Lock);
// Do a lookup to see if the function is in our cache... this should just be a // Do a lookup to see if the function is in our cache... this should just be a
// deferred annotation! // deferred annotation!
std::map<const Function *, ExFunc>::iterator FI = ExportedFunctions->find(F); std::map<const Function *, ExFunc>::iterator FI =
if (ExFunc Fn = (FI == ExportedFunctions->end()) ? lookupFunction(F) Fns.ExportedFunctions.find(F);
: FI->second) { if (ExFunc Fn = (FI == Fns.ExportedFunctions.end()) ? lookupFunction(F)
: FI->second) {
Guard.unlock(); Guard.unlock();
return Fn(F->getFunctionType(), ArgVals); return Fn(F->getFunctionType(), ArgVals);
} }
#ifdef USE_LIBFFI #ifdef USE_LIBFFI
std::map<const Function *, RawFunc>::iterator RF = RawFunctions->find(F); std::map<const Function *, RawFunc>::iterator RF = Fns.RawFunctions.find(F);
RawFunc RawFn; RawFunc RawFn;
if (RF == RawFunctions->end()) { if (RF == Fns.RawFunctions.end()) {
RawFn = (RawFunc)(intptr_t) RawFn = (RawFunc)(intptr_t)
sys::DynamicLibrary::SearchForAddressOfSymbol(std::string(F->getName())); sys::DynamicLibrary::SearchForAddressOfSymbol(std::string(F->getName()));
if (!RawFn) if (!RawFn)
RawFn = (RawFunc)(intptr_t)getPointerToGlobalIfAvailable(F); RawFn = (RawFunc)(intptr_t)getPointerToGlobalIfAvailable(F);
if (RawFn != 0) 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 { } else {
RawFn = RF->second; RawFn = RF->second;
} }
@ -496,16 +508,17 @@ static GenericValue lle_X_memcpy(FunctionType *FT,
} }
void Interpreter::initializeExternalFunctions() { void Interpreter::initializeExternalFunctions() {
sys::ScopedLock Writer(*FunctionsLock); auto &Fns = getFunctions();
(*FuncNames)["lle_X_atexit"] = lle_X_atexit; sys::ScopedLock Writer(Fns.Lock);
(*FuncNames)["lle_X_exit"] = lle_X_exit; Fns.FuncNames["lle_X_atexit"] = lle_X_atexit;
(*FuncNames)["lle_X_abort"] = lle_X_abort; Fns.FuncNames["lle_X_exit"] = lle_X_exit;
Fns.FuncNames["lle_X_abort"] = lle_X_abort;
(*FuncNames)["lle_X_printf"] = lle_X_printf; Fns.FuncNames["lle_X_printf"] = lle_X_printf;
(*FuncNames)["lle_X_sprintf"] = lle_X_sprintf; Fns.FuncNames["lle_X_sprintf"] = lle_X_sprintf;
(*FuncNames)["lle_X_sscanf"] = lle_X_sscanf; Fns.FuncNames["lle_X_sscanf"] = lle_X_sscanf;
(*FuncNames)["lle_X_scanf"] = lle_X_scanf; Fns.FuncNames["lle_X_scanf"] = lle_X_scanf;
(*FuncNames)["lle_X_fprintf"] = lle_X_fprintf; Fns.FuncNames["lle_X_fprintf"] = lle_X_fprintf;
(*FuncNames)["lle_X_memset"] = lle_X_memset; Fns.FuncNames["lle_X_memset"] = lle_X_memset;
(*FuncNames)["lle_X_memcpy"] = lle_X_memcpy; Fns.FuncNames["lle_X_memcpy"] = lle_X_memcpy;
} }