[Orc] Refactor JITCompileCallbackManagerBase and CompileOnDemandLayer to support
authorLang Hames <lhames@gmail.com>
Wed, 25 Mar 2015 02:45:50 +0000 (02:45 +0000)
committerLang Hames <lhames@gmail.com>
Wed, 25 Mar 2015 02:45:50 +0000 (02:45 +0000)
target-independent callback management.

This is a prerequisite for adding orc-based lazy-jitting to lli.

git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@233166 91177308-0d34-0410-b5e6-96231b3b80d8

include/llvm/ExecutionEngine/Orc/CompileOnDemandLayer.h
include/llvm/ExecutionEngine/Orc/IndirectionUtils.h
include/llvm/ExecutionEngine/Orc/OrcTargetSupport.h
lib/ExecutionEngine/Orc/OrcTargetSupport.cpp

index b10a275b12e72031c9cd94da0f7ce287b1cae976..3dd1a37e1ed5f0994f27f547374f41742a53ec8b 100644 (file)
@@ -142,9 +142,8 @@ public:
   typedef std::function<uint64_t(const std::string &)> LookupFtor;
 
   /// @brief Construct a compile-on-demand layer instance.
   typedef std::function<uint64_t(const std::string &)> LookupFtor;
 
   /// @brief Construct a compile-on-demand layer instance.
-  CompileOnDemandLayer(BaseLayerT &BaseLayer, LLVMContext &Context)
-    : BaseLayer(BaseLayer),
-      CompileCallbackMgr(BaseLayer, Context, 0, 64) {}
+  CompileOnDemandLayer(BaseLayerT &BaseLayer, CompileCallbackMgrT &CallbackMgr)
+      : BaseLayer(BaseLayer), CompileCallbackMgr(CallbackMgr) {}
 
   /// @brief Add a module to the compile-on-demand layer.
   template <typename ModuleSetT>
 
   /// @brief Add a module to the compile-on-demand layer.
   template <typename ModuleSetT>
@@ -294,7 +293,7 @@ private:
                                     M.getDataLayout());
       auto &CCInfo = KVPair.second;
       CCInfo.setUpdateAction(
                                     M.getDataLayout());
       auto &CCInfo = KVPair.second;
       CCInfo.setUpdateAction(
-        CompileCallbackMgr.getLocalFPUpdater(StubsH, AddrName));
+        getLocalFPUpdater(BaseLayer, StubsH, AddrName));
     }
   }
 
     }
   }
 
@@ -345,7 +344,7 @@ private:
   }
 
   BaseLayerT &BaseLayer;
   }
 
   BaseLayerT &BaseLayer;
-  CompileCallbackMgrT CompileCallbackMgr;
+  CompileCallbackMgrT &CompileCallbackMgr;
   ModuleSetInfoListT ModuleSetInfos;
 };
 
   ModuleSetInfoListT ModuleSetInfos;
 };
 
index 6168684950ad8a743d1f5fcd4f9a3fc2e6fe9aa5..bfc86147e43fbf2a330f6f4a99f212b9f5a17ace 100644 (file)
@@ -26,10 +26,34 @@ namespace orc {
 
 /// @brief Base class for JITLayer independent aspects of
 ///        JITCompileCallbackManager.
 
 /// @brief Base class for JITLayer independent aspects of
 ///        JITCompileCallbackManager.
-template <typename TargetT>
 class JITCompileCallbackManagerBase {
 public:
 
 class JITCompileCallbackManagerBase {
 public:
 
+  typedef std::function<TargetAddress()> CompileFtor;
+  typedef std::function<void(TargetAddress)> UpdateFtor;
+
+  /// @brief Handle to a newly created compile callback. Can be used to get an
+  ///        IR constant representing the address of the trampoline, and to set
+  ///        the compile and update actions for the callback.
+  class CompileCallbackInfo {
+  public:
+    CompileCallbackInfo(Constant *Addr, CompileFtor &Compile,
+                        UpdateFtor &Update)
+      : Addr(Addr), Compile(Compile), Update(Update) {}
+
+    Constant* getAddress() const { return Addr; }
+    void setCompileAction(CompileFtor Compile) {
+      this->Compile = std::move(Compile);
+    }
+    void setUpdateAction(UpdateFtor Update) {
+      this->Update = std::move(Update);
+    }
+  private:
+    Constant *Addr;
+    CompileFtor &Compile;
+    UpdateFtor &Update;
+  };
+
   /// @brief Construct a JITCompileCallbackManagerBase.
   /// @param ErrorHandlerAddress The address of an error handler in the target
   ///                            process to be used if a compile callback fails.
   /// @brief Construct a JITCompileCallbackManagerBase.
   /// @param ErrorHandlerAddress The address of an error handler in the target
   ///                            process to be used if a compile callback fails.
@@ -41,6 +65,8 @@ public:
     : ErrorHandlerAddress(ErrorHandlerAddress),
       NumTrampolinesPerBlock(NumTrampolinesPerBlock) {}
 
     : ErrorHandlerAddress(ErrorHandlerAddress),
       NumTrampolinesPerBlock(NumTrampolinesPerBlock) {}
 
+  virtual ~JITCompileCallbackManagerBase() {}
+
   /// @brief Execute the callback for the given trampoline id. Called by the JIT
   ///        to compile functions on demand.
   TargetAddress executeCompileCallback(TargetAddress TrampolineID) {
   /// @brief Execute the callback for the given trampoline id. Called by the JIT
   ///        to compile functions on demand.
   TargetAddress executeCompileCallback(TargetAddress TrampolineID) {
@@ -67,14 +93,14 @@ public:
     return ErrorHandlerAddress;
   }
 
     return ErrorHandlerAddress;
   }
 
-protected:
+  /// @brief Get/create a compile callback with the given signature.
+  virtual CompileCallbackInfo getCompileCallback(FunctionType &FT) = 0;
 
 
-  typedef std::function<TargetAddress()> CompileFtorT;
-  typedef std::function<void(TargetAddress)> UpdateFtorT;
+protected:
 
   struct CallbackHandler {
 
   struct CallbackHandler {
-    CompileFtorT Compile;
-    UpdateFtorT Update;
+    CompileFtor Compile;
+    UpdateFtor Update;
   };
 
   TargetAddress ErrorHandlerAddress;
   };
 
   TargetAddress ErrorHandlerAddress;
@@ -87,15 +113,9 @@ protected:
 
 /// @brief Manage compile callbacks.
 template <typename JITLayerT, typename TargetT>
 
 /// @brief Manage compile callbacks.
 template <typename JITLayerT, typename TargetT>
-class JITCompileCallbackManager :
-    public JITCompileCallbackManagerBase<TargetT> {
+class JITCompileCallbackManager : public JITCompileCallbackManagerBase {
 public:
 
 public:
 
-  typedef typename JITCompileCallbackManagerBase<TargetT>::CompileFtorT
-    CompileFtorT;
-  typedef typename JITCompileCallbackManagerBase<TargetT>::UpdateFtorT
-    UpdateFtorT;
-
   /// @brief Construct a JITCompileCallbackManager.
   /// @param JIT JIT layer to emit callback trampolines, etc. into.
   /// @param Context LLVMContext to use for trampoline & resolve block modules.
   /// @brief Construct a JITCompileCallbackManager.
   /// @param JIT JIT layer to emit callback trampolines, etc. into.
   /// @param Context LLVMContext to use for trampoline & resolve block modules.
@@ -108,36 +128,14 @@ public:
   JITCompileCallbackManager(JITLayerT &JIT, LLVMContext &Context,
                             TargetAddress ErrorHandlerAddress,
                             unsigned NumTrampolinesPerBlock)
   JITCompileCallbackManager(JITLayerT &JIT, LLVMContext &Context,
                             TargetAddress ErrorHandlerAddress,
                             unsigned NumTrampolinesPerBlock)
-    : JITCompileCallbackManagerBase<TargetT>(ErrorHandlerAddress,
-                                             NumTrampolinesPerBlock),
+    : JITCompileCallbackManagerBase(ErrorHandlerAddress,
+                                    NumTrampolinesPerBlock),
       JIT(JIT) {
     emitResolverBlock(Context);
   }
 
       JIT(JIT) {
     emitResolverBlock(Context);
   }
 
-  /// @brief Handle to a newly created compile callback. Can be used to get an
-  ///        IR constant representing the address of the trampoline, and to set
-  ///        the compile and update actions for the callback.
-  class CompileCallbackInfo {
-  public:
-    CompileCallbackInfo(Constant *Addr, CompileFtorT &Compile,
-                        UpdateFtorT &Update)
-      : Addr(Addr), Compile(Compile), Update(Update) {}
-
-    Constant* getAddress() const { return Addr; }
-    void setCompileAction(CompileFtorT Compile) {
-      this->Compile = std::move(Compile);
-    }
-    void setUpdateAction(UpdateFtorT Update) {
-      this->Update = std::move(Update);
-    }
-  private:
-    Constant *Addr;
-    CompileFtorT &Compile;
-    UpdateFtorT &Update;
-  };
-
   /// @brief Get/create a compile callback with the given signature.
   /// @brief Get/create a compile callback with the given signature.
-  CompileCallbackInfo getCompileCallback(FunctionType &FT) {
+  CompileCallbackInfo getCompileCallback(FunctionType &FT) final {
     TargetAddress TrampolineAddr = getAvailableTrampolineAddr(FT.getContext());
     auto &CallbackHandler =
       this->ActiveTrampolines[TrampolineAddr];
     TargetAddress TrampolineAddr = getAvailableTrampolineAddr(FT.getContext());
     auto &CallbackHandler =
       this->ActiveTrampolines[TrampolineAddr];
@@ -151,19 +149,6 @@ public:
                                CallbackHandler.Update);
   }
 
                                CallbackHandler.Update);
   }
 
-  /// @brief Get a functor for updating the value of a named function pointer.
-  UpdateFtorT getLocalFPUpdater(typename JITLayerT::ModuleSetHandleT H,
-                                std::string Name) {
-    // FIXME: Move-capture Name once we can use C++14.
-    return [=](TargetAddress Addr) {
-      auto FPSym = JIT.findSymbolIn(H, Name, true);
-      assert(FPSym && "Cannot find function pointer to update.");
-      void *FPAddr = reinterpret_cast<void*>(
-                       static_cast<uintptr_t>(FPSym.getAddress()));
-      memcpy(FPAddr, &Addr, sizeof(uintptr_t));
-    };
-  }
-
 private:
 
   std::vector<std::unique_ptr<Module>>
 private:
 
   std::vector<std::unique_ptr<Module>>
@@ -216,6 +201,22 @@ private:
   TargetAddress ResolverBlockAddr;
 };
 
   TargetAddress ResolverBlockAddr;
 };
 
+/// @brief Get an update functor for updating the value of a named function
+///        pointer.
+template <typename JITLayerT>
+JITCompileCallbackManagerBase::UpdateFtor
+getLocalFPUpdater(JITLayerT &JIT, typename JITLayerT::ModuleSetHandleT H,
+                  std::string Name) {
+    // FIXME: Move-capture Name once we can use C++14.
+    return [=,&JIT](TargetAddress Addr) {
+      auto FPSym = JIT.findSymbolIn(H, Name, true);
+      assert(FPSym && "Cannot find function pointer to update.");
+      void *FPAddr = reinterpret_cast<void*>(
+                       static_cast<uintptr_t>(FPSym.getAddress()));
+      memcpy(FPAddr, &Addr, sizeof(uintptr_t));
+    };
+  }
+
 GlobalVariable* createImplPointer(Function &F, const Twine &Name,
                                   Constant *Initializer);
 
 GlobalVariable* createImplPointer(Function &F, const Twine &Name,
                                   Constant *Initializer);
 
index 045528b865bbfdc6befed4d38b580818ddb75de3..309f5a96090ef313a472a1973db67caae1286917 100644 (file)
@@ -25,9 +25,8 @@ public:
 
   /// @brief Insert module-level inline callback asm into module M for the
   /// symbols managed by JITResolveCallbackHandler J.
 
   /// @brief Insert module-level inline callback asm into module M for the
   /// symbols managed by JITResolveCallbackHandler J.
-  static void insertResolverBlock(
-                                 Module &M,
-                                 JITCompileCallbackManagerBase<OrcX86_64> &JCBM);
+  static void insertResolverBlock(Module &M,
+                                  JITCompileCallbackManagerBase &JCBM);
 
   /// @brief Get a label name from the given index.
   typedef std::function<std::string(unsigned)> LabelNameFtor;
 
   /// @brief Get a label name from the given index.
   typedef std::function<std::string(unsigned)> LabelNameFtor;
index 4932ec16bb0fa246de6dac0138dbde5eab255d15..6fe530176a1982555b110c904185672706db2237 100644 (file)
@@ -39,7 +39,7 @@ template <typename OStream> void restoreX86Regs(OStream &OS) {
 }
 
 template <typename TargetT>
 }
 
 template <typename TargetT>
-uint64_t executeCompileCallback(JITCompileCallbackManagerBase<TargetT> *JCBM,
+uint64_t executeCompileCallback(JITCompileCallbackManagerBase *JCBM,
                                 TargetAddress CallbackID) {
   return JCBM->executeCompileCallback(CallbackID);
 }
                                 TargetAddress CallbackID) {
   return JCBM->executeCompileCallback(CallbackID);
 }
@@ -52,7 +52,7 @@ namespace orc {
 const char* OrcX86_64::ResolverBlockName = "orc_resolver_block";
 
 void OrcX86_64::insertResolverBlock(
 const char* OrcX86_64::ResolverBlockName = "orc_resolver_block";
 
 void OrcX86_64::insertResolverBlock(
-    Module &M, JITCompileCallbackManagerBase<OrcX86_64> &JCBM) {
+    Module &M, JITCompileCallbackManagerBase &JCBM) {
   const unsigned X86_64_TrampolineLength = 6;
   auto CallbackPtr = executeCompileCallback<OrcX86_64>;
   uint64_t CallbackAddr =
   const unsigned X86_64_TrampolineLength = 6;
   auto CallbackPtr = executeCompileCallback<OrcX86_64>;
   uint64_t CallbackAddr =