[llvm] 0d944e0 - [ORC] Refactor TrampolinePool to reduce virtual function calls.

Lang Hames via llvm-commits llvm-commits at lists.llvm.org
Sun Jul 19 22:42:36 PDT 2020


Author: Lang Hames
Date: 2020-07-19T22:38:41-07:00
New Revision: 0d944e00ea148a3f16bc7074827b79f6e7f84053

URL: https://github.com/llvm/llvm-project/commit/0d944e00ea148a3f16bc7074827b79f6e7f84053
DIFF: https://github.com/llvm/llvm-project/commit/0d944e00ea148a3f16bc7074827b79f6e7f84053.diff

LOG: [ORC] Refactor TrampolinePool to reduce virtual function calls.

Virtual function calls are now only made when the pool needs to be
grown to accommodate o new request.

Added: 
    

Modified: 
    llvm/include/llvm/ExecutionEngine/Orc/IndirectionUtils.h
    llvm/include/llvm/ExecutionEngine/Orc/OrcRemoteTargetClient.h
    llvm/include/llvm/ExecutionEngine/Orc/TargetProcessControl.h
    llvm/lib/ExecutionEngine/Orc/IndirectionUtils.cpp
    llvm/lib/ExecutionEngine/Orc/TPCIndirectionUtils.cpp
    llvm/unittests/ExecutionEngine/Orc/LegacyCompileOnDemandLayerTest.cpp

Removed: 
    


################################################################################
diff  --git a/llvm/include/llvm/ExecutionEngine/Orc/IndirectionUtils.h b/llvm/include/llvm/ExecutionEngine/Orc/IndirectionUtils.h
index e0cfd8bf2409..78e3ceef50e2 100644
--- a/llvm/include/llvm/ExecutionEngine/Orc/IndirectionUtils.h
+++ b/llvm/include/llvm/ExecutionEngine/Orc/IndirectionUtils.h
@@ -62,14 +62,33 @@ class TrampolinePool {
       JITTargetAddress TrampolineAddr,
       NotifyLandingResolvedFunction OnLandingResolved) const>;
 
-  virtual ~TrampolinePool() {}
+  virtual ~TrampolinePool();
 
   /// Get an available trampoline address.
   /// Returns an error if no trampoline can be created.
-  virtual Expected<JITTargetAddress> getTrampoline() = 0;
+  Expected<JITTargetAddress> getTrampoline() {
+    std::lock_guard<std::mutex> Lock(TPMutex);
+    if (AvailableTrampolines.empty()) {
+      if (auto Err = grow())
+        return std::move(Err);
+    }
+    assert(!AvailableTrampolines.empty() && "Failed to grow trampoline pool");
+    auto TrampolineAddr = AvailableTrampolines.back();
+    AvailableTrampolines.pop_back();
+    return TrampolineAddr;
+  }
 
-private:
-  virtual void anchor();
+  /// Returns the given trampoline to the pool for re-use.
+  void releaseTrampoline(JITTargetAddress TrampolineAddr) {
+    std::lock_guard<std::mutex> Lock(TPMutex);
+    AvailableTrampolines.push_back(TrampolineAddr);
+  }
+
+protected:
+  virtual Error grow() = 0;
+
+  std::mutex TPMutex;
+  std::vector<JITTargetAddress> AvailableTrampolines;
 };
 
 /// A trampoline pool for trampolines within the current process.
@@ -90,26 +109,6 @@ template <typename ORCABI> class LocalTrampolinePool : public TrampolinePool {
     return std::move(LTP);
   }
 
-  /// Get a free trampoline. Returns an error if one can not be provided (e.g.
-  /// because the pool is empty and can not be grown).
-  Expected<JITTargetAddress> getTrampoline() override {
-    std::lock_guard<std::mutex> Lock(LTPMutex);
-    if (AvailableTrampolines.empty()) {
-      if (auto Err = grow())
-        return std::move(Err);
-    }
-    assert(!AvailableTrampolines.empty() && "Failed to grow trampoline pool");
-    auto TrampolineAddr = AvailableTrampolines.back();
-    AvailableTrampolines.pop_back();
-    return TrampolineAddr;
-  }
-
-  /// Returns the given trampoline to the pool for re-use.
-  void releaseTrampoline(JITTargetAddress TrampolineAddr) {
-    std::lock_guard<std::mutex> Lock(LTPMutex);
-    AvailableTrampolines.push_back(TrampolineAddr);
-  }
-
 private:
   static JITTargetAddress reenter(void *TrampolinePoolPtr, void *TrampolineId) {
     LocalTrampolinePool<ORCABI> *TrampolinePool =
@@ -154,8 +153,8 @@ template <typename ORCABI> class LocalTrampolinePool : public TrampolinePool {
     }
   }
 
-  Error grow() {
-    assert(this->AvailableTrampolines.empty() && "Growing prematurely?");
+  Error grow() override {
+    assert(AvailableTrampolines.empty() && "Growing prematurely?");
 
     std::error_code EC;
     auto TrampolineBlock =
@@ -175,7 +174,7 @@ template <typename ORCABI> class LocalTrampolinePool : public TrampolinePool {
         pointerToJITTargetAddress(ResolverBlock.base()), NumTrampolines);
 
     for (unsigned I = 0; I < NumTrampolines; ++I)
-      this->AvailableTrampolines.push_back(pointerToJITTargetAddress(
+      AvailableTrampolines.push_back(pointerToJITTargetAddress(
           TrampolineMem + (I * ORCABI::TrampolineSize)));
 
     if (auto EC = sys::Memory::protectMappedMemory(
@@ -189,10 +188,8 @@ template <typename ORCABI> class LocalTrampolinePool : public TrampolinePool {
 
   ResolveLandingFunction ResolveLanding;
 
-  std::mutex LTPMutex;
   sys::OwningMemoryBlock ResolverBlock;
   std::vector<sys::OwningMemoryBlock> TrampolineBlocks;
-  std::vector<JITTargetAddress> AvailableTrampolines;
 };
 
 /// Target-independent base class for compile callback management.

diff  --git a/llvm/include/llvm/ExecutionEngine/Orc/OrcRemoteTargetClient.h b/llvm/include/llvm/ExecutionEngine/Orc/OrcRemoteTargetClient.h
index 86e8d5df3ad9..809e7cd0e880 100644
--- a/llvm/include/llvm/ExecutionEngine/Orc/OrcRemoteTargetClient.h
+++ b/llvm/include/llvm/ExecutionEngine/Orc/OrcRemoteTargetClient.h
@@ -453,18 +453,6 @@ class OrcRemoteTargetClient
   public:
     RemoteTrampolinePool(OrcRemoteTargetClient &Client) : Client(Client) {}
 
-    Expected<JITTargetAddress> getTrampoline() override {
-      std::lock_guard<std::mutex> Lock(RTPMutex);
-      if (AvailableTrampolines.empty()) {
-        if (auto Err = grow())
-          return std::move(Err);
-      }
-      assert(!AvailableTrampolines.empty() && "Failed to grow trampoline pool");
-      auto TrampolineAddr = AvailableTrampolines.back();
-      AvailableTrampolines.pop_back();
-      return TrampolineAddr;
-    }
-
   private:
     Error grow() {
       JITTargetAddress BlockAddr = 0;
@@ -476,14 +464,12 @@ class OrcRemoteTargetClient
 
       uint32_t TrampolineSize = Client.getTrampolineSize();
       for (unsigned I = 0; I < NumTrampolines; ++I)
-        this->AvailableTrampolines.push_back(BlockAddr + (I * TrampolineSize));
+        AvailableTrampolines.push_back(BlockAddr + (I * TrampolineSize));
 
       return Error::success();
     }
 
-    std::mutex RTPMutex;
     OrcRemoteTargetClient &Client;
-    std::vector<JITTargetAddress> AvailableTrampolines;
   };
 
   /// Remote compile callback manager.

diff  --git a/llvm/include/llvm/ExecutionEngine/Orc/TargetProcessControl.h b/llvm/include/llvm/ExecutionEngine/Orc/TargetProcessControl.h
index facafd883653..cc0016e105d8 100644
--- a/llvm/include/llvm/ExecutionEngine/Orc/TargetProcessControl.h
+++ b/llvm/include/llvm/ExecutionEngine/Orc/TargetProcessControl.h
@@ -119,6 +119,8 @@ class TargetProcessControl {
   /// Return a MemoryAccess object for the target process.
   MemoryAccess &getMemoryAccess() const { return *MemAccess; }
 
+  /// Load the library at the given path.
+
 protected:
   TargetProcessControl(Triple TT, unsigned PageSize);
 

diff  --git a/llvm/lib/ExecutionEngine/Orc/IndirectionUtils.cpp b/llvm/lib/ExecutionEngine/Orc/IndirectionUtils.cpp
index 031b1afefc9d..4f7f6089e68d 100644
--- a/llvm/lib/ExecutionEngine/Orc/IndirectionUtils.cpp
+++ b/llvm/lib/ExecutionEngine/Orc/IndirectionUtils.cpp
@@ -54,8 +54,8 @@ class CompileCallbackMaterializationUnit : public orc::MaterializationUnit {
 namespace llvm {
 namespace orc {
 
+TrampolinePool::~TrampolinePool() {}
 void IndirectStubsManager::anchor() {}
-void TrampolinePool::anchor() {}
 
 Expected<JITTargetAddress>
 JITCompileCallbackManager::getCompileCallback(CompileFunction Compile) {

diff  --git a/llvm/lib/ExecutionEngine/Orc/TPCIndirectionUtils.cpp b/llvm/lib/ExecutionEngine/Orc/TPCIndirectionUtils.cpp
index 150040cd11e5..b4c21c32310c 100644
--- a/llvm/lib/ExecutionEngine/Orc/TPCIndirectionUtils.cpp
+++ b/llvm/lib/ExecutionEngine/Orc/TPCIndirectionUtils.cpp
@@ -37,20 +37,16 @@ class TPCTrampolinePool : public TrampolinePool {
 public:
   TPCTrampolinePool(TPCIndirectionUtils &TPCIU);
   Error deallocatePool();
-  Expected<JITTargetAddress> getTrampoline() override;
-  void releaseTrampoline(JITTargetAddress TrampolineAddr);
 
 protected:
-  Error grow();
+  Error grow() override;
 
   using Allocation = jitlink::JITLinkMemoryManager::Allocation;
 
-  std::mutex TPMutex;
   TPCIndirectionUtils &TPCIU;
   unsigned TrampolineSize = 0;
   unsigned TrampolinesPerPage = 0;
   std::vector<std::unique_ptr<Allocation>> TrampolineBlocks;
-  std::vector<JITTargetAddress> AvailableTrampolines;
 };
 
 class TPCIndirectStubsManager : public IndirectStubsManager,
@@ -96,26 +92,8 @@ Error TPCTrampolinePool::deallocatePool() {
   return Err;
 }
 
-Expected<JITTargetAddress> TPCTrampolinePool::getTrampoline() {
-  std::lock_guard<std::mutex> Lock(TPMutex);
-  if (AvailableTrampolines.empty()) {
-    if (auto Err = grow())
-      return std::move(Err);
-  }
-
-  assert(!AvailableTrampolines.empty() && "Failed to grow trampoline pool");
-  auto TrampolineAddr = AvailableTrampolines.back();
-  AvailableTrampolines.pop_back();
-  return TrampolineAddr;
-}
-
-void TPCTrampolinePool::releaseTrampoline(JITTargetAddress TrampolineAddr) {
-  std::lock_guard<std::mutex> Lock(TPMutex);
-  AvailableTrampolines.push_back(TrampolineAddr);
-}
-
 Error TPCTrampolinePool::grow() {
-  assert(this->AvailableTrampolines.empty() &&
+  assert(AvailableTrampolines.empty() &&
          "Grow called with trampolines still available");
 
   auto ResolverAddress = TPCIU.getResolverBlockAddress();
@@ -144,7 +122,7 @@ Error TPCTrampolinePool::grow() {
 
   auto TargetAddr = (*Alloc)->getTargetMemory(TrampolinePagePermissions);
   for (unsigned I = 0; I < NumTrampolines; ++I)
-    this->AvailableTrampolines.push_back(TargetAddr + (I * TrampolineSize));
+    AvailableTrampolines.push_back(TargetAddr + (I * TrampolineSize));
 
   if (auto Err = (*Alloc)->finalize())
     return Err;

diff  --git a/llvm/unittests/ExecutionEngine/Orc/LegacyCompileOnDemandLayerTest.cpp b/llvm/unittests/ExecutionEngine/Orc/LegacyCompileOnDemandLayerTest.cpp
index a13d8bdeeeb3..543c164e67c4 100644
--- a/llvm/unittests/ExecutionEngine/Orc/LegacyCompileOnDemandLayerTest.cpp
+++ b/llvm/unittests/ExecutionEngine/Orc/LegacyCompileOnDemandLayerTest.cpp
@@ -16,10 +16,8 @@ using namespace llvm::orc;
 namespace {
 
 class DummyTrampolinePool : public orc::TrampolinePool {
-public:
-  Expected<JITTargetAddress> getTrampoline() override {
-    llvm_unreachable("Unimplemented");
-  }
+protected:
+  Error grow() override { llvm_unreachable("Unimplemented"); }
 };
 
 class DummyCallbackManager : public JITCompileCallbackManager {


        


More information about the llvm-commits mailing list