[llvm] 4cf0a9d - ManagedStatic: remove from Interpreter/ExternalFunctions

Nicolai Hähnle via llvm-commits llvm-commits at lists.llvm.org
Wed Aug 3 04:29:36 PDT 2022


Author: Nicolai Hähnle
Date: 2022-08-03T13:29:24+02:00
New Revision: 4cf0a9d4ae76f46ee2833f8479bb712b072673f3

URL: https://github.com/llvm/llvm-project/commit/4cf0a9d4ae76f46ee2833f8479bb712b072673f3
DIFF: https://github.com/llvm/llvm-project/commit/4cf0a9d4ae76f46ee2833f8479bb712b072673f3.diff

LOG: ManagedStatic: remove from Interpreter/ExternalFunctions

Differential Revision: https://reviews.llvm.org/D129124

Added: 
    

Modified: 
    llvm/lib/ExecutionEngine/Interpreter/ExternalFunctions.cpp

Removed: 
    


################################################################################
diff  --git a/llvm/lib/ExecutionEngine/Interpreter/ExternalFunctions.cpp b/llvm/lib/ExecutionEngine/Interpreter/ExternalFunctions.cpp
index c3ba5ebb36fb9..29f481a1e4e82 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 <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;
+typedef void (*RawFunc)();
 
+struct Functions {
+  sys::Mutex Lock;
+  std::map<const Function *, ExFunc> ExportedFunctions;
+  std::map<std::string, ExFunc> FuncNames;
 #ifdef USE_LIBFFI
-typedef void (*RawFunc)();
-static ManagedStatic<std::map<const Function *, RawFunc> > RawFunctions;
+  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;
-
-  (*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;
 }


        


More information about the llvm-commits mailing list