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/DynamicLibrary.h"
#include "llvm/Support/ErrorHandling.h"
#include "llvm/Support/ManagedStatic.h"
#include "llvm/Support/Mutex.h"
#include "llvm/Support/raw_ostream.h"
#include <cassert>
@ -57,16 +56,26 @@
using namespace llvm;
static ManagedStatic<sys::Mutex> FunctionsLock;
namespace {
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)();
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
};
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<GenericValue> ArgVals) {
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
// deferred annotation!
std::map<const Function *, ExFunc>::iterator FI = ExportedFunctions->find(F);
if (ExFunc Fn = (FI == ExportedFunctions->end()) ? lookupFunction(F)
: FI->second) {
std::map<const Function *, ExFunc>::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<const Function *, RawFunc>::iterator RF = RawFunctions->find(F);
std::map<const Function *, RawFunc>::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;
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;
(*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;
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;
}