<html><head><meta http-equiv="content-type" content="text/html; charset=utf-8"></head><body dir="auto"><div><span></span></div><div><meta http-equiv="content-type" content="text/html; charset=utf-8"><div>Hi Dave,</div><div><br></div><div><blockquote type="cite"><div dir="ltr"><div class="gmail_extra"><div class="gmail_quote"><blockquote class="gmail_quote" style="margin: 0px 0px 0px 0.8ex; border-left-width: 1px; border-left-color: rgb(204, 204, 204); padding-left: 1ex;"><font color="#000000"><span style="background-color: rgba(255, 255, 255, 0);"> ; CHECK: Hello<br>-; CHECK: [ {{.*}}main$orc_body ]<br>+; CHECK: [ {{.*}}main ]<br></span></font></blockquote><div><font color="#000000"><span style="background-color: rgba(255, 255, 255, 0);"><br>What does this change represent?<br></span></font></div></div></div></div></blockquote><div><br></div><div>Nothing especially significant. The older partitioner used to append '$orc_body' to the end of function body names to avoid clashes. The new partitioner achieves the same effect by marking function bodies as hidden, but leaving their name unchanged.</div><br><blockquote type="cite"><div dir="ltr"><div class="gmail_extra"><div class="gmail_quote"><div><font color="#000000"><span style="background-color: rgba(255, 255, 255, 0);">(& I imagine a bunch of those utils you wrote (& the layer itself) could be well unit tested - checking that the split module doesn't include uninteresting decls, etc) </span></font></div></div></div></div></blockquote><div><br></div><div>Yep. I would like unit tests for all this, but I'm short on time at the moment, so regression tests will have to do for a little longer. Patches welcome of course. ;)</div><div><br></div><div>Cheers,</div><div>Lang.</div></div><div><br>On May 4, 2015, at 3:25 PM, David Blaikie <<a href="mailto:dblaikie@gmail.com">dblaikie@gmail.com</a>> wrote:<br><br></div><blockquote type="cite"><div><div dir="ltr"><br><div class="gmail_extra"><br><div class="gmail_quote">On Mon, May 4, 2015 at 3:03 PM, Lang Hames <span dir="ltr"><<a href="mailto:lhames@gmail.com" target="_blank">lhames@gmail.com</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">Author: lhames<br>
Date: Mon May  4 17:03:10 2015<br>
New Revision: 236465<br>
<br>
URL: <a href="http://llvm.org/viewvc/llvm-project?rev=236465&view=rev" target="_blank">http://llvm.org/viewvc/llvm-project?rev=236465&view=rev</a><br>
Log:<br>
[Orc] Refactor the compile-on-demand layer to make module partitioning lazy,<br>
and avoid cloning unused decls into every partition.<br>
<br>
Module partitioning showed up as a source of significant overhead when I<br>
profiled some trivial test cases. Avoiding the overhead of partitionging<br>
for uncalled functions helps to mitigate this.<br>
<br>
This change also means that it is no longer necessary to have a<br>
LazyEmittingLayer underneath the CompileOnDemand layer, since the<br>
CompileOnDemandLayer will not extract or emit function bodies until they are<br>
called.<br>
<br>
<br>
Removed:<br>
    llvm/trunk/include/llvm/ExecutionEngine/Orc/CloneSubModule.h<br>
    llvm/trunk/lib/ExecutionEngine/Orc/CloneSubModule.cpp<br>
Modified:<br>
    llvm/trunk/examples/Kaleidoscope/Orc/fully_lazy/toy.cpp<br>
    llvm/trunk/include/llvm/ExecutionEngine/Orc/CompileOnDemandLayer.h<br>
    llvm/trunk/include/llvm/ExecutionEngine/Orc/IndirectionUtils.h<br>
    llvm/trunk/lib/ExecutionEngine/Orc/CMakeLists.txt<br>
    llvm/trunk/lib/ExecutionEngine/Orc/IndirectionUtils.cpp<br>
    llvm/trunk/test/ExecutionEngine/OrcLazy/hello.ll<br>
    llvm/trunk/tools/lli/OrcLazyJIT.h<br>
<br>
Modified: llvm/trunk/examples/Kaleidoscope/Orc/fully_lazy/toy.cpp<br>
URL: <a href="http://llvm.org/viewvc/llvm-project/llvm/trunk/examples/Kaleidoscope/Orc/fully_lazy/toy.cpp?rev=236465&r1=236464&r2=236465&view=diff" target="_blank">http://llvm.org/viewvc/llvm-project/llvm/trunk/examples/Kaleidoscope/Orc/fully_lazy/toy.cpp?rev=236465&r1=236464&r2=236465&view=diff</a><br>
==============================================================================<br>
--- llvm/trunk/examples/Kaleidoscope/Orc/fully_lazy/toy.cpp (original)<br>
+++ llvm/trunk/examples/Kaleidoscope/Orc/fully_lazy/toy.cpp Mon May  4 17:03:10 2015<br>
@@ -1214,11 +1214,11 @@ public:<br>
   void removeModule(ModuleHandleT H) { LazyEmitLayer.removeModuleSet(H); }<br>
<br>
   JITSymbol findSymbol(const std::string &Name) {<br>
-    return LazyEmitLayer.findSymbol(Name, true);<br>
+    return LazyEmitLayer.findSymbol(Name, false);<br>
   }<br>
<br>
   JITSymbol findSymbolIn(ModuleHandleT H, const std::string &Name) {<br>
-    return LazyEmitLayer.findSymbolIn(H, Name, true);<br>
+    return LazyEmitLayer.findSymbolIn(H, Name, false);<br>
   }<br>
<br>
   JITSymbol findUnmangledSymbol(const std::string &Name) {<br>
@@ -1276,7 +1276,7 @@ private:<br>
     makeStub(*F, *FunctionBodyPointer);<br>
<br>
     // Step 4) Add the module containing the stub to the JIT.<br>
-    auto H = addModule(C.takeM());<br>
+    auto StubH = addModule(C.takeM());<br>
<br>
     // Step 5) Set the compile and update actions.<br>
     //<br>
@@ -1289,14 +1289,20 @@ private:<br>
     //   The update action will update FunctionBodyPointer to point at the newly<br>
     // compiled function.<br>
     std::shared_ptr<FunctionAST> Fn = std::move(FnAST);<br>
-    CallbackInfo.setCompileAction([this, Fn]() {<br>
+    CallbackInfo.setCompileAction([this, Fn, BodyPtrName, StubH]() {<br>
       auto H = addModule(IRGen(Session, *Fn));<br>
-      return findUnmangledSymbolIn(H, Fn->Proto->Name).getAddress();<br>
+      auto BodySym = findUnmangledSymbolIn(H, Fn->Proto->Name);<br>
+      auto BodyPtrSym = findUnmangledSymbolIn(StubH, BodyPtrName);<br>
+      assert(BodySym && "Missing function body.");<br>
+      assert(BodyPtrSym && "Missing function pointer.");<br>
+      auto BodyAddr = BodySym.getAddress();<br>
+      auto BodyPtr = reinterpret_cast<void*>(<br>
+                       static_cast<uintptr_t>(BodyPtrSym.getAddress()));<br>
+      memcpy(BodyPtr, &BodyAddr, sizeof(uintptr_t));<br>
+      return BodyAddr;<br>
     });<br>
-    CallbackInfo.setUpdateAction(<br>
-        getLocalFPUpdater(LazyEmitLayer, H, mangle(BodyPtrName)));<br>
<br>
-    return H;<br>
+    return StubH;<br>
   }<br>
<br>
   SessionContext &Session;<br>
<br>
Removed: llvm/trunk/include/llvm/ExecutionEngine/Orc/CloneSubModule.h<br>
URL: <a href="http://llvm.org/viewvc/llvm-project/llvm/trunk/include/llvm/ExecutionEngine/Orc/CloneSubModule.h?rev=236464&view=auto" target="_blank">http://llvm.org/viewvc/llvm-project/llvm/trunk/include/llvm/ExecutionEngine/Orc/CloneSubModule.h?rev=236464&view=auto</a><br>
==============================================================================<br>
--- llvm/trunk/include/llvm/ExecutionEngine/Orc/CloneSubModule.h (original)<br>
+++ llvm/trunk/include/llvm/ExecutionEngine/Orc/CloneSubModule.h (removed)<br>
@@ -1,60 +0,0 @@<br>
-//===-- CloneSubModule.h - Utilities for extracting sub-modules -*- C++ -*-===//<br>
-//<br>
-//                     The LLVM Compiler Infrastructure<br>
-//<br>
-// This file is distributed under the University of Illinois Open Source<br>
-// License. See LICENSE.TXT for details.<br>
-//<br>
-//===----------------------------------------------------------------------===//<br>
-//<br>
-// Contains utilities for extracting sub-modules. Useful for breaking up modules<br>
-// for lazy jitting.<br>
-//<br>
-//===----------------------------------------------------------------------===//<br>
-<br>
-#ifndef LLVM_EXECUTIONENGINE_ORC_CLONESUBMODULE_H<br>
-#define LLVM_EXECUTIONENGINE_ORC_CLONESUBMODULE_H<br>
-<br>
-#include "llvm/ADT/DenseSet.h"<br>
-#include "llvm/Transforms/Utils/ValueMapper.h"<br>
-#include <functional><br>
-<br>
-namespace llvm {<br>
-<br>
-class Function;<br>
-class GlobalVariable;<br>
-class Module;<br>
-<br>
-namespace orc {<br>
-<br>
-/// @brief Functor type for describing how CloneSubModule should mutate a<br>
-///        GlobalVariable.<br>
-typedef std::function<void(GlobalVariable &, const GlobalVariable &,<br>
-                           ValueToValueMapTy &)> HandleGlobalVariableFtor;<br>
-<br>
-/// @brief Functor type for describing how CloneSubModule should mutate a<br>
-///        Function.<br>
-typedef std::function<void(Function &, const Function &, ValueToValueMapTy &)><br>
-    HandleFunctionFtor;<br>
-<br>
-/// @brief Copies the initializer from Orig to New.<br>
-///<br>
-///   Type is suitable for implicit conversion to a HandleGlobalVariableFtor.<br>
-void copyGVInitializer(GlobalVariable &New, const GlobalVariable &Orig,<br>
-                       ValueToValueMapTy &VMap);<br>
-<br>
-/// @brief Copies the body of Orig to New.<br>
-///<br>
-///   Type is suitable for implicit conversion to a HandleFunctionFtor.<br>
-void copyFunctionBody(Function &New, const Function &Orig,<br>
-                      ValueToValueMapTy &VMap);<br>
-<br>
-/// @brief Clone a subset of the module Src into Dst.<br>
-void CloneSubModule(Module &Dst, const Module &Src,<br>
-                    HandleGlobalVariableFtor HandleGlobalVariable,<br>
-                    HandleFunctionFtor HandleFunction, bool KeepInlineAsm);<br>
-<br>
-} // End namespace orc.<br>
-} // End namespace llvm.<br>
-<br>
-#endif // LLVM_EXECUTIONENGINE_ORC_CLONESUBMODULE_H<br>
<br>
Modified: llvm/trunk/include/llvm/ExecutionEngine/Orc/CompileOnDemandLayer.h<br>
URL: <a href="http://llvm.org/viewvc/llvm-project/llvm/trunk/include/llvm/ExecutionEngine/Orc/CompileOnDemandLayer.h?rev=236465&r1=236464&r2=236465&view=diff" target="_blank">http://llvm.org/viewvc/llvm-project/llvm/trunk/include/llvm/ExecutionEngine/Orc/CompileOnDemandLayer.h?rev=236465&r1=236464&r2=236465&view=diff</a><br>
==============================================================================<br>
--- llvm/trunk/include/llvm/ExecutionEngine/Orc/CompileOnDemandLayer.h (original)<br>
+++ llvm/trunk/include/llvm/ExecutionEngine/Orc/CompileOnDemandLayer.h Mon May  4 17:03:10 2015<br>
@@ -15,110 +15,180 @@<br>
 #ifndef LLVM_EXECUTIONENGINE_ORC_COMPILEONDEMANDLAYER_H<br>
 #define LLVM_EXECUTIONENGINE_ORC_COMPILEONDEMANDLAYER_H<br>
<br>
+//#include "CloneSubModule.h"<br>
 #include "IndirectionUtils.h"<br>
 #include "LambdaResolver.h"<br>
 #include "llvm/ADT/STLExtras.h"<br>
 #include "llvm/ExecutionEngine/SectionMemoryManager.h"<br>
+#include "llvm/Transforms/Utils/Cloning.h"<br>
 #include <list><br>
+#include <set><br>
+<br>
+#include "llvm/Support/Debug.h"<br>
<br>
 namespace llvm {<br>
 namespace orc {<br>
<br>
 /// @brief Compile-on-demand layer.<br>
 ///<br>
-///   Modules added to this layer have their calls indirected, and are then<br>
-/// broken up into a set of single-function modules, each of which is added<br>
-/// to the layer below in a singleton set. The lower layer can be any layer that<br>
-/// accepts IR module sets.<br>
-///<br>
-/// It is expected that this layer will frequently be used on top of a<br>
-/// LazyEmittingLayer. The combination of the two ensures that each function is<br>
-/// compiled only when it is first called.<br>
+///   When a module is added to this layer a stub is created for each of its<br>
+/// function definitions. The stubs and other global values are immediately<br>
+/// added to the layer below. When a stub is called it triggers the extraction<br>
+/// of the function body from the original module. The extracted body is then<br>
+/// compiled and executed.<br>
 template <typename BaseLayerT, typename CompileCallbackMgrT><br>
 class CompileOnDemandLayer {<br>
 private:<br>
-  /// @brief Lookup helper that provides compatibility with the classic<br>
-  ///        static-compilation symbol resolution process.<br>
-  ///<br>
-  ///   The CompileOnDemand (COD) layer splits modules up into multiple<br>
-  /// sub-modules, each held in its own llvm::Module instance, in order to<br>
-  /// support lazy compilation. When a module that contains private symbols is<br>
-  /// broken up symbol linkage changes may be required to enable access to<br>
-  /// "private" data that now resides in a different llvm::Module instance. To<br>
-  /// retain expected symbol resolution behavior for clients of the COD layer,<br>
-  /// the CODScopedLookup class uses a two-tiered lookup system to resolve<br>
-  /// symbols. Lookup first scans sibling modules that were split from the same<br>
-  /// original module (logical-module scoped lookup), then scans all other<br>
-  /// modules that have been added to the lookup scope (logical-dylib scoped<br>
-  /// lookup).<br>
-  class CODScopedLookup {<br>
+<br>
+  // Utility class for MapValue. Only materializes declarations for global<br>
+  // variables.<br>
+  class GlobalDeclMaterializer : public ValueMaterializer {<br>
+  public:<br>
+    GlobalDeclMaterializer(Module &Dst) : Dst(Dst) {}<br>
+    Value* materializeValueFor(Value *V) final {<br>
+      if (auto *GV = dyn_cast<GlobalVariable>(V))<br>
+        return cloneGlobalVariableDecl(Dst, *GV);<br>
+      else if (auto *F = dyn_cast<Function>(V))<br>
+        return cloneFunctionDecl(Dst, *F);<br>
+      // Else.<br>
+      return nullptr;<br>
+    }<br>
   private:<br>
-    typedef typename BaseLayerT::ModuleSetHandleT BaseLayerModuleSetHandleT;<br>
-    typedef std::vector<BaseLayerModuleSetHandleT> SiblingHandlesList;<br>
-    typedef std::list<SiblingHandlesList> PseudoDylibModuleSetHandlesList;<br>
+    Module &Dst;<br>
+  };<br>
+<br>
+  typedef typename BaseLayerT::ModuleSetHandleT BaseLayerModuleSetHandleT;<br>
+  class UncompiledPartition;<br>
<br>
+  // Logical module.<br>
+  //<br>
+  //   This struct contains the handles for the global values and stubs (which<br>
+  // cover the external symbols of the original module), plus the handes for<br>
+  // each of the extracted partitions. These handleds are used for lookup (only<br>
+  // the globals/stubs module is searched) and memory management. The actual<br>
+  // searching and resource management are handled by the LogicalDylib that owns<br>
+  // the LogicalModule.<br>
+  struct LogicalModule {<br>
+    std::unique_ptr<Module> SrcM;<br>
+    BaseLayerModuleSetHandleT GVsAndStubsHandle;<br>
+    std::vector<BaseLayerModuleSetHandleT> ImplHandles;<br>
+  };<br>
+<br>
+  // Logical dylib.<br>
+  //<br>
+  //   This class handles symbol resolution and resource management for a set of<br>
+  // modules that were added together as a logical dylib.<br>
+  //<br>
+  //   A logical dylib contains one-or-more LogicalModules plus a set of<br>
+  // UncompiledPartitions. LogicalModules support symbol resolution and resource<br>
+  // management for for code that has already been emitted. UncompiledPartitions<br>
+  // represent code that has not yet been compiled.<br>
+  class LogicalDylib {<br>
+  private:<br>
+    friend class UncompiledPartition;<br>
+    typedef std::list<LogicalModule> LogicalModuleList;<br>
   public:<br>
-    /// @brief Handle for a logical module.<br>
-    typedef typename PseudoDylibModuleSetHandlesList::iterator LMHandle;<br>
<br>
-    /// @brief Construct a scoped lookup.<br>
-    CODScopedLookup(BaseLayerT &BaseLayer) : BaseLayer(BaseLayer) {}<br>
+    typedef unsigned UncompiledPartitionID;<br>
+    typedef typename LogicalModuleList::iterator LMHandle;<br>
<br>
-    virtual ~CODScopedLookup() {}<br>
+    // Construct a logical dylib.<br>
+    LogicalDylib(CompileOnDemandLayer &CODLayer) : CODLayer(CODLayer) { }<br>
+<br>
+    // Delete this logical dylib, release logical module resources.<br>
+    virtual ~LogicalDylib() {<br>
+      releaseLogicalModuleResources();<br>
+    }<br>
<br>
-    /// @brief Start a new context for a single logical module.<br>
+    // Get a reference to the containing layer.<br>
+    CompileOnDemandLayer& getCODLayer() { return CODLayer; }<br>
+<br>
+    // Get a reference to the base layer.<br>
+    BaseLayerT& getBaseLayer() { return CODLayer.BaseLayer; }<br>
+<br>
+    // Start a new context for a single logical module.<br>
     LMHandle createLogicalModule() {<br>
-      Handles.push_back(SiblingHandlesList());<br>
-      return std::prev(Handles.end());<br>
+      LogicalModules.push_back(LogicalModule());<br>
+      return std::prev(LogicalModules.end());<br>
     }<br>
<br>
-    /// @brief Add a concrete Module's handle to the given logical Module's<br>
-    ///        lookup scope.<br>
-    void addToLogicalModule(LMHandle LMH, BaseLayerModuleSetHandleT H) {<br>
-      LMH->push_back(H);<br>
+    // Set the global-values-and-stubs module handle for this logical module.<br>
+    void setGVsAndStubsHandle(LMHandle LMH, BaseLayerModuleSetHandleT H) {<br>
+      LMH->GVsAndStubsHandle = H;<br>
     }<br>
<br>
-    /// @brief Remove a logical Module from the CODScopedLookup entirely.<br>
-    void removeLogicalModule(LMHandle LMH) { Handles.erase(LMH); }<br>
+    // Return the global-values-and-stubs module handle for this logical module.<br>
+    BaseLayerModuleSetHandleT getGVsAndStubsHandle(LMHandle LMH) {<br>
+      return LMH->GVsAndStubsHandle;<br>
+    }<br>
<br>
-    /// @brief Look up a symbol in this context.<br>
-    JITSymbol findSymbol(LMHandle LMH, const std::string &Name) {<br>
-      if (auto Symbol = findSymbolIn(LMH, Name))<br>
+    //   Add a handle to a module containing lazy function bodies to the given<br>
+    // logical module.<br>
+    void addToLogicalModule(LMHandle LMH, BaseLayerModuleSetHandleT H) {<br>
+      LMH->ImplHandles.push_back(H);<br>
+    }<br>
+<br>
+    // Create an UncompiledPartition attached to this LogicalDylib.<br>
+    UncompiledPartition& createUncompiledPartition(LMHandle LMH,<br>
+                                                   std::shared_ptr<Module> SrcM);<br>
+<br>
+    // Take ownership of the given UncompiledPartition from the logical dylib.<br>
+    std::unique_ptr<UncompiledPartition><br>
+    takeUPOwnership(UncompiledPartitionID ID);<br>
+<br>
+    // Look up a symbol in this context.<br>
+    JITSymbol findSymbolInternally(LMHandle LMH, const std::string &Name) {<br>
+      if (auto Symbol = getBaseLayer().findSymbolIn(LMH->GVsAndStubsHandle,<br>
+                                                    Name, false))<br>
         return Symbol;<br>
<br>
-      for (auto I = Handles.begin(), E = Handles.end(); I != E; ++I)<br>
+      for (auto I = LogicalModules.begin(), E = LogicalModules.end(); I != E;<br>
+           ++I)<br>
         if (I != LMH)<br>
-          if (auto Symbol = findSymbolIn(I, Name))<br>
+          if (auto Symbol = getBaseLayer().findSymbolIn(I->GVsAndStubsHandle,<br>
+                                                        Name, false))<br>
             return Symbol;<br>
<br>
       return nullptr;<br>
     }<br>
<br>
-    /// @brief Find an external symbol (via the user supplied SymbolResolver).<br>
+    JITSymbol findSymbol(const std::string &Name, bool ExportedSymbolsOnly) {<br>
+      for (auto &LM : LogicalModules)<br>
+        if (auto Symbol = getBaseLayer().findSymbolIn(LM.GVsAndStubsHandle,<br>
+                                                      Name,<br>
+                                                      ExportedSymbolsOnly))<br>
+          return Symbol;<br>
+      return nullptr;<br>
+    }<br>
+<br>
+    // Find an external symbol (via the user supplied SymbolResolver).<br>
     virtual RuntimeDyld::SymbolInfo<br>
-    externalLookup(const std::string &Name) const = 0;<br>
+    findSymbolExternally(const std::string &Name) const = 0;<br>
<br>
   private:<br>
<br>
-    JITSymbol findSymbolIn(LMHandle LMH, const std::string &Name) {<br>
-      for (auto H : *LMH)<br>
-        if (auto Symbol = BaseLayer.findSymbolIn(H, Name, false))<br>
-          return Symbol;<br>
-      return nullptr;<br>
+    void releaseLogicalModuleResources() {<br>
+      for (auto I = LogicalModules.begin(), E = LogicalModules.end(); I != E;<br>
+           ++I) {<br>
+        getBaseLayer().removeModuleSet(I->GVsAndStubsHandle);<br>
+        for (auto H : I->ImplHandles)<br>
+          getBaseLayer().removeModuleSet(H);<br>
+      }<br>
     }<br>
<br>
-    BaseLayerT &BaseLayer;<br>
-    PseudoDylibModuleSetHandlesList Handles;<br>
+    CompileOnDemandLayer &CODLayer;<br>
+    LogicalModuleList LogicalModules;<br>
+    std::vector<std::unique_ptr<UncompiledPartition>> UncompiledPartitions;<br>
   };<br>
<br>
   template <typename ResolverPtrT><br>
-  class CODScopedLookupImpl : public CODScopedLookup {<br>
+  class LogicalDylibImpl : public LogicalDylib  {<br>
   public:<br>
-    CODScopedLookupImpl(BaseLayerT &BaseLayer, ResolverPtrT Resolver)<br>
-      : CODScopedLookup(BaseLayer), Resolver(std::move(Resolver)) {}<br>
+    LogicalDylibImpl(CompileOnDemandLayer &CODLayer, ResolverPtrT Resolver)<br>
+      : LogicalDylib(CODLayer), Resolver(std::move(Resolver)) {}<br>
<br>
     RuntimeDyld::SymbolInfo<br>
-    externalLookup(const std::string &Name) const override {<br>
+    findSymbolExternally(const std::string &Name) const override {<br>
       return Resolver->findSymbol(Name);<br>
     }<br>
<br>
@@ -127,44 +197,169 @@ private:<br>
   };<br>
<br>
   template <typename ResolverPtrT><br>
-  static std::shared_ptr<CODScopedLookup><br>
-  createCODScopedLookup(BaseLayerT &BaseLayer,<br>
-                        ResolverPtrT Resolver) {<br>
-    typedef CODScopedLookupImpl<ResolverPtrT> Impl;<br>
-    return std::make_shared<Impl>(BaseLayer, std::move(Resolver));<br>
+  static std::unique_ptr<LogicalDylib><br>
+  createLogicalDylib(CompileOnDemandLayer &CODLayer,<br>
+                     ResolverPtrT Resolver) {<br>
+    typedef LogicalDylibImpl<ResolverPtrT> Impl;<br>
+    return llvm::make_unique<Impl>(CODLayer, std::move(Resolver));<br>
   }<br>
<br>
-  typedef typename BaseLayerT::ModuleSetHandleT BaseLayerModuleSetHandleT;<br>
-  typedef std::vector<BaseLayerModuleSetHandleT> BaseLayerModuleSetHandleListT;<br>
+  // Uncompiled partition.<br>
+  //<br>
+  // Represents one as-yet uncompiled portion of a module.<br>
+  class UncompiledPartition {<br>
+  public:<br>
+<br>
+    struct PartitionEntry {<br>
+      PartitionEntry(Function *F, TargetAddress CallbackID)<br>
+          : F(F), CallbackID(CallbackID) {}<br>
+      Function *F;<br>
+      TargetAddress CallbackID;<br>
+    };<br>
+<br>
+    typedef std::vector<PartitionEntry> PartitionEntryList;<br>
+<br>
+    // Creates an uncompiled partition with the list of functions that make up<br>
+    // this partition.<br>
+    UncompiledPartition(LogicalDylib &LD, typename LogicalDylib::LMHandle LMH,<br>
+                        std::shared_ptr<Module> SrcM)<br>
+        : LD(LD), LMH(LMH), SrcM(std::move(SrcM)), ID(~0U) {}<br>
+<br>
+    ~UncompiledPartition() {<br>
+      // FIXME: When we want to support threaded lazy compilation we'll need to<br>
+      //        lock the callback manager here.<br>
+      auto &CCMgr = LD.getCODLayer().CompileCallbackMgr;<br>
+      for (auto PEntry : PartitionEntries)<br>
+        CCMgr.releaseCompileCallback(PEntry.CallbackID);<br>
+    }<br>
<br>
-  struct ModuleSetInfo {<br>
-    // Symbol lookup - just one for the whole module set.<br>
-    std::shared_ptr<CODScopedLookup> Lookup;<br>
-<br>
-    // Logical module handles.<br>
-    std::vector<typename CODScopedLookup::LMHandle> LMHandles;<br>
-<br>
-    // List of vectors of module set handles:<br>
-    // One vector per logical module - each vector holds the handles for the<br>
-    // exploded modules for that logical module in the base layer.<br>
-    BaseLayerModuleSetHandleListT BaseLayerModuleSetHandles;<br>
-<br>
-    ModuleSetInfo(std::shared_ptr<CODScopedLookup> Lookup)<br>
-        : Lookup(std::move(Lookup)) {}<br>
-<br>
-    void releaseResources(BaseLayerT &BaseLayer) {<br>
-      for (auto LMH : LMHandles)<br>
-        Lookup->removeLogicalModule(LMH);<br>
-      for (auto H : BaseLayerModuleSetHandles)<br>
-        BaseLayer.removeModuleSet(H);<br>
+    // Set the ID for this partition.<br>
+    void setID(typename LogicalDylib::UncompiledPartitionID ID) {<br>
+      this->ID = ID;<br>
     }<br>
+<br>
+    // Set the function set and callbacks for this partition.<br>
+    void setPartitionEntries(PartitionEntryList PartitionEntries) {<br>
+      this->PartitionEntries = std::move(PartitionEntries);<br>
+    }<br>
+<br>
+    // Handle a compile callback for the function at index FnIdx.<br>
+    TargetAddress compile(unsigned FnIdx) {<br>
+      // Take ownership of self. This will ensure we delete the partition and<br>
+      // free all its resources once we're done compiling.<br>
+      std::unique_ptr<UncompiledPartition> This = LD.takeUPOwnership(ID);<br>
+<br>
+      // Release all other compile callbacks for this partition.<br>
+      // We skip the callback for this function because that's the one that<br>
+      // called us, and the callback manager will already have removed it.<br>
+      auto &CCMgr = LD.getCODLayer().CompileCallbackMgr;<br>
+      for (unsigned I = 0; I < PartitionEntries.size(); ++I)<br>
+        if (I != FnIdx)<br>
+          CCMgr.releaseCompileCallback(PartitionEntries[I].CallbackID);<br>
+<br>
+      // Grab the name of the function being called here.<br>
+      Function *F = PartitionEntries[FnIdx].F;<br>
+      std::string CalledFnName = Mangle(F->getName(), SrcM->getDataLayout());<br>
+<br>
+      // Extract the function and add it to the base layer.<br>
+      auto PartitionImplH = emitPartition();<br>
+      LD.addToLogicalModule(LMH, PartitionImplH);<br>
+<br>
+      // Update body pointers.<br>
+      // FIXME: When we start supporting remote lazy jitting this will need to<br>
+      //        be replaced with a user-supplied callback for updating the<br>
+      //        remote pointers.<br>
+      TargetAddress CalledAddr = 0;<br>
+      for (unsigned I = 0; I < PartitionEntries.size(); ++I) {<br>
+        auto F = PartitionEntries[I].F;<br>
+        std::string FName(F->getName());<br>
+        auto FnBodySym =<br>
+          LD.getBaseLayer().findSymbolIn(PartitionImplH,<br>
+                                         Mangle(FName, SrcM->getDataLayout()),<br>
+                                         false);<br>
+        auto FnPtrSym =<br>
+          LD.getBaseLayer().findSymbolIn(LD.getGVsAndStubsHandle(LMH),<br>
+                                         Mangle(FName + "$orc_addr",<br>
+                                                SrcM->getDataLayout()),<br>
+                                         false);<br>
+        assert(FnBodySym && "Couldn't find function body.");<br>
+        assert(FnPtrSym && "Couldn't find function body pointer.");<br>
+<br>
+        auto FnBodyAddr = FnBodySym.getAddress();<br>
+        void *FnPtrAddr = reinterpret_cast<void*>(<br>
+                            static_cast<uintptr_t>(FnPtrSym.getAddress()));<br>
+<br>
+        // If this is the function we're calling record the address so we can<br>
+        // return it from this function.<br>
+        if (I == FnIdx)<br>
+          CalledAddr = FnBodyAddr;<br>
+<br>
+        memcpy(FnPtrAddr, &FnBodyAddr, sizeof(uintptr_t));<br>
+      }<br>
+<br>
+      // Finally, clear the partition structure so we don't try to<br>
+      // double-release the callbacks in the UncompiledPartition destructor.<br>
+      PartitionEntries.clear();<br>
+<br>
+      return CalledAddr;<br>
+    }<br>
+<br>
+  private:<br>
+<br>
+    BaseLayerModuleSetHandleT emitPartition() {<br>
+      // Create the module.<br>
+      std::string NewName(SrcM->getName());<br>
+      for (auto &PEntry : PartitionEntries) {<br>
+        NewName += ".";<br>
+        NewName += PEntry.F->getName();<br>
+      }<br>
+      auto PM = llvm::make_unique<Module>(NewName, SrcM->getContext());<br>
+      PM->setDataLayout(SrcM->getDataLayout());<br>
+      ValueToValueMapTy VMap;<br>
+      GlobalDeclMaterializer GDM(*PM);<br>
+<br>
+      // Create decls in the new module.<br>
+      for (auto &PEntry : PartitionEntries)<br>
+        cloneFunctionDecl(*PM, *PEntry.F, &VMap);<br>
+<br>
+      // Move the function bodies.<br>
+      for (auto &PEntry : PartitionEntries)<br>
+        moveFunctionBody(*PEntry.F, VMap);<br>
+<br>
+      // Create memory manager and symbol resolver.<br>
+      auto MemMgr = llvm::make_unique<SectionMemoryManager>();<br>
+      auto Resolver = createLambdaResolver(<br>
+          [this](const std::string &Name) {<br>
+            if (auto Symbol = LD.findSymbolInternally(LMH, Name))<br>
+              return RuntimeDyld::SymbolInfo(Symbol.getAddress(),<br>
+                                             Symbol.getFlags());<br>
+            return LD.findSymbolExternally(Name);<br>
+          },<br>
+          [this](const std::string &Name) {<br>
+            if (auto Symbol = LD.findSymbolInternally(LMH, Name))<br>
+              return RuntimeDyld::SymbolInfo(Symbol.getAddress(),<br>
+                                             Symbol.getFlags());<br>
+            return RuntimeDyld::SymbolInfo(nullptr);<br>
+          });<br>
+      std::vector<std::unique_ptr<Module>> PartMSet;<br>
+      PartMSet.push_back(std::move(PM));<br>
+      return LD.getBaseLayer().addModuleSet(std::move(PartMSet),<br>
+                                            std::move(MemMgr),<br>
+                                            std::move(Resolver));<br>
+    }<br>
+<br>
+    LogicalDylib &LD;<br>
+    typename LogicalDylib::LMHandle LMH;<br>
+    std::shared_ptr<Module> SrcM;<br>
+    typename LogicalDylib::UncompiledPartitionID ID;<br>
+    PartitionEntryList PartitionEntries;<br>
   };<br>
<br>
-  typedef std::list<ModuleSetInfo> ModuleSetInfoListT;<br>
+  typedef std::list<std::unique_ptr<LogicalDylib>> LogicalDylibList;<br>
<br>
 public:<br>
   /// @brief Handle to a set of loaded modules.<br>
-  typedef typename ModuleSetInfoListT::iterator ModuleSetHandleT;<br>
+  typedef typename LogicalDylibList::iterator ModuleSetHandleT;<br>
<br>
   /// @brief Construct a compile-on-demand layer instance.<br>
   CompileOnDemandLayer(BaseLayerT &BaseLayer, CompileCallbackMgrT &CallbackMgr)<br>
@@ -180,19 +375,23 @@ public:<br>
     assert(MemMgr == nullptr &&<br>
            "User supplied memory managers not supported with COD yet.");<br>
<br>
-    // Create a lookup context and ModuleSetInfo for this module set.<br>
-    // For the purposes of symbol resolution the set Ms will be treated as if<br>
-    // the modules it contained had been linked together as a dylib.<br>
-    auto DylibLookup = createCODScopedLookup(BaseLayer, std::move(Resolver));<br>
-    ModuleSetHandleT H =<br>
-        ModuleSetInfos.insert(ModuleSetInfos.end(), ModuleSetInfo(DylibLookup));<br>
-    ModuleSetInfo &MSI = ModuleSetInfos.back();<br>
+    LogicalDylibs.push_back(createLogicalDylib(*this, std::move(Resolver)));<br>
<br>
     // Process each of the modules in this module set.<br>
-    for (auto &M : Ms)<br>
-      partitionAndAdd(*M, MSI);<br>
+    for (auto &M : Ms) {<br>
+      std::vector<std::vector<Function*>> Partitioning;<br>
+      for (auto &F : *M) {<br>
+        if (F.isDeclaration())<br>
+          continue;<br>
+        Partitioning.push_back(std::vector<Function*>());<br>
+        Partitioning.back().push_back(&F);<br>
+      }<br>
+      addLogicalModule(*LogicalDylibs.back(),<br>
+                       std::shared_ptr<Module>(std::move(M)),<br>
+                       std::move(Partitioning));<br>
+    }<br>
<br>
-    return H;<br>
+    return std::prev(LogicalDylibs.end());<br>
   }<br>
<br>
   /// @brief Remove the module represented by the given handle.<br>
@@ -200,8 +399,7 @@ public:<br>
   ///   This will remove all modules in the layers below that were derived from<br>
   /// the module represented by H.<br>
   void removeModuleSet(ModuleSetHandleT H) {<br>
-    H->releaseResources(BaseLayer);<br>
-    ModuleSetInfos.erase(H);<br>
+    LogicalDylibs.erase(H);<br>
   }<br>
<br>
   /// @brief Search for the given named symbol.<br>
@@ -216,149 +414,85 @@ public:<br>
   ///        below this one.<br>
   JITSymbol findSymbolIn(ModuleSetHandleT H, const std::string &Name,<br>
                          bool ExportedSymbolsOnly) {<br>
-<br>
-    for (auto &BH : H->BaseLayerModuleSetHandles) {<br>
-      if (auto Symbol = BaseLayer.findSymbolIn(BH, Name, ExportedSymbolsOnly))<br>
-        return Symbol;<br>
-    }<br>
-    return nullptr;<br>
+    return (*H)->findSymbol(Name, ExportedSymbolsOnly);<br>
   }<br>
<br>
 private:<br>
<br>
-  void partitionAndAdd(Module &M, ModuleSetInfo &MSI) {<br>
-    const char *AddrSuffix = "$orc_addr";<br>
-    const char *BodySuffix = "$orc_body";<br>
-<br>
-    // We're going to break M up into a bunch of sub-modules, but we want<br>
-    // internal linkage symbols to still resolve sensibly. CODScopedLookup<br>
-    // provides the "logical module" concept to make this work, so create a<br>
-    // new logical module for M.<br>
-    auto DylibLookup = MSI.Lookup;<br>
-    auto LogicalModule = DylibLookup->createLogicalModule();<br>
-    MSI.LMHandles.push_back(LogicalModule);<br>
-<br>
-    // Partition M into a "globals and stubs" module, a "common symbols" module,<br>
-    // and a list of single-function modules.<br>
-    auto PartitionedModule = fullyPartition(M);<br>
-    auto StubsModule = std::move(PartitionedModule.GlobalVars);<br>
-    auto CommonsModule = std::move(PartitionedModule.Commons);<br>
-    auto FunctionModules = std::move(PartitionedModule.Functions);<br>
-<br>
-    // Emit the commons stright away.<br>
-    auto CommonHandle = addModule(std::move(CommonsModule), MSI, LogicalModule);<br>
-    BaseLayer.emitAndFinalize(CommonHandle);<br>
-<br>
-    // Map of definition names to callback-info data structures. We'll use<br>
-    // this to build the compile actions for the stubs below.<br>
-    typedef std::map<std::string,<br>
-                     typename CompileCallbackMgrT::CompileCallbackInfo><br>
-      StubInfoMap;<br>
-    StubInfoMap StubInfos;<br>
-<br>
-    // Now we need to take each of the extracted Modules and add them to<br>
-    // base layer. Each Module will be added individually to make sure they<br>
-    // can be compiled separately, and each will get its own lookaside<br>
-    // memory manager that will resolve within this logical module first.<br>
-    for (auto &SubM : FunctionModules) {<br>
-<br>
-      // Keep track of the stubs we create for this module so that we can set<br>
-      // their compile actions.<br>
-      std::vector<typename StubInfoMap::iterator> NewStubInfos;<br>
-<br>
-      // Search for function definitions and insert stubs into the stubs<br>
-      // module.<br>
-      for (auto &F : *SubM) {<br>
-        if (F.isDeclaration())<br>
-          continue;<br>
-<br>
-        std::string Name = F.getName();<br>
-        Function *Proto = StubsModule->getFunction(Name);<br>
-        assert(Proto && "Failed to clone function decl into stubs module.");<br>
-        auto CallbackInfo =<br>
-          CompileCallbackMgr.getCompileCallback(Proto->getContext());<br>
-        GlobalVariable *FunctionBodyPointer =<br>
-          createImplPointer(*Proto->getType(), *Proto->getParent(),<br>
-                            Name + AddrSuffix,<br>
-                            createIRTypedAddress(*Proto->getFunctionType(),<br>
-                                                 CallbackInfo.getAddress()));<br>
-        makeStub(*Proto, *FunctionBodyPointer);<br>
+  void addLogicalModule(LogicalDylib &LD, std::shared_ptr<Module> SrcM,<br>
+                        std::vector<std::vector<Function*>> Partitions) {<br>
<br>
-        F.setName(Name + BodySuffix);<br>
-        F.setVisibility(GlobalValue::HiddenVisibility);<br>
-<br>
-        auto KV = std::make_pair(std::move(Name), std::move(CallbackInfo));<br>
-        NewStubInfos.push_back(StubInfos.insert(StubInfos.begin(), KV));<br>
+    // Bump the linkage and rename any anonymous/privote members in SrcM to<br>
+    // ensure that everything will resolve properly after we partition SrcM.<br>
+    makeAllSymbolsExternallyAccessible(*SrcM);<br>
+<br>
+    // Create a logical module handle for SrcM within the logical dylib.<br>
+    auto LMH = LD.createLogicalModule();<br>
+<br>
+    // Create the GVs-and-stubs module.<br>
+    auto GVsAndStubsM = llvm::make_unique<Module>(<br>
+                          (SrcM->getName() + ".globals_and_stubs").str(),<br>
+                          SrcM->getContext());<br>
+    GVsAndStubsM->setDataLayout(SrcM->getDataLayout());<br>
+    ValueToValueMapTy VMap;<br>
+<br>
+    // Process partitions and create stubs.<br>
+    // We create the stubs before copying the global variables as we know the<br>
+    // stubs won't refer to any globals (they only refer to their implementation<br>
+    // pointer) so there's no ordering/value-mapping issues.<br>
+    for (auto& Partition : Partitions) {<br>
+      auto &UP = LD.createUncompiledPartition(LMH, SrcM);<br>
+      typename UncompiledPartition::PartitionEntryList PartitionEntries;<br>
+      for (auto &F : Partition) {<br>
+        assert(!F->isDeclaration() &&<br>
+               "Partition should only contain definitions");<br>
+        unsigned FnIdx = PartitionEntries.size();<br>
+        auto CCI = CompileCallbackMgr.getCompileCallback(SrcM->getContext());<br>
+        PartitionEntries.push_back(<br>
+          typename UncompiledPartition::PartitionEntry(F, CCI.getAddress()));<br>
+        Function *StubF = cloneFunctionDecl(*GVsAndStubsM, *F, &VMap);<br>
+        GlobalVariable *FnBodyPtr =<br>
+          createImplPointer(*StubF->getType(), *StubF->getParent(),<br>
+                            StubF->getName() + "$orc_addr",<br>
+                            createIRTypedAddress(*StubF->getFunctionType(),<br>
+                                                 CCI.getAddress()));<br>
+        makeStub(*StubF, *FnBodyPtr);<br>
+        CCI.setCompileAction([&UP, FnIdx]() { return UP.compile(FnIdx); });<br>
       }<br>
<br>
-      auto H = addModule(std::move(SubM), MSI, LogicalModule);<br>
-<br>
-      // Set the compile actions for this module:<br>
-      for (auto &KVPair : NewStubInfos) {<br>
-        std::string BodyName = Mangle(KVPair->first + BodySuffix,<br>
-                                      M.getDataLayout());<br>
-        auto &CCInfo = KVPair->second;<br>
-        CCInfo.setCompileAction(<br>
-          [=](){<br>
-            return BaseLayer.findSymbolIn(H, BodyName, false).getAddress();<br>
-          });<br>
-      }<br>
-<br>
-    }<br>
-<br>
-    // Ok - we've processed all the partitioned modules. Now add the<br>
-    // stubs/globals module and set the update actions.<br>
-    auto StubsH =<br>
-      addModule(std::move(StubsModule), MSI, LogicalModule);<br>
-<br>
-    for (auto &KVPair : StubInfos) {<br>
-      std::string AddrName = Mangle(KVPair.first + AddrSuffix,<br>
-                                    M.getDataLayout());<br>
-      auto &CCInfo = KVPair.second;<br>
-      CCInfo.setUpdateAction(<br>
-        getLocalFPUpdater(BaseLayer, StubsH, AddrName));<br>
+      UP.setPartitionEntries(std::move(PartitionEntries));<br>
     }<br>
-  }<br>
<br>
-  // Add the given Module to the base layer using a memory manager that will<br>
-  // perform the appropriate scoped lookup (i.e. will look first with in the<br>
-  // module from which it was extracted, then into the set to which that module<br>
-  // belonged, and finally externally).<br>
-  BaseLayerModuleSetHandleT addModule(<br>
-                               std::unique_ptr<Module> M,<br>
-                               ModuleSetInfo &MSI,<br>
-                               typename CODScopedLookup::LMHandle LogicalModule) {<br>
-<br>
-    // Add this module to the JIT with a memory manager that uses the<br>
-    // DylibLookup to resolve symbols.<br>
-    std::vector<std::unique_ptr<Module>> MSet;<br>
-    MSet.push_back(std::move(M));<br>
-<br>
-    auto DylibLookup = MSI.Lookup;<br>
-    auto Resolver =<br>
-      createLambdaResolver(<br>
-        [=](const std::string &Name) {<br>
-          if (auto Symbol = DylibLookup->findSymbol(LogicalModule, Name))<br>
+    // Now clone the global variable declarations.<br>
+    GlobalDeclMaterializer GDMat(*GVsAndStubsM);<br>
+    for (auto &GV : SrcM->globals())<br>
+      if (!GV.isDeclaration())<br>
+        cloneGlobalVariableDecl(*GVsAndStubsM, GV, &VMap);<br>
+<br>
+    // Then clone the initializers.<br>
+    for (auto &GV : SrcM->globals())<br>
+      if (!GV.isDeclaration())<br>
+        moveGlobalVariableInitializer(GV, VMap, &GDMat);<br>
+<br>
+    // Build a resolver for the stubs module and add it to the base layer.<br>
+    auto GVsAndStubsResolver = createLambdaResolver(<br>
+        [&LD](const std::string &Name) {<br>
+          if (auto Symbol = LD.findSymbol(Name, false))<br>
             return RuntimeDyld::SymbolInfo(Symbol.getAddress(),<br>
                                            Symbol.getFlags());<br>
-          return DylibLookup->externalLookup(Name);<br>
+          return LD.findSymbolExternally(Name);<br>
         },<br>
-        [=](const std::string &Name) -> RuntimeDyld::SymbolInfo {<br>
-          if (auto Symbol = DylibLookup->findSymbol(LogicalModule, Name))<br>
-            return RuntimeDyld::SymbolInfo(Symbol.getAddress(),<br>
-                                           Symbol.getFlags());<br>
-          return nullptr;<br>
+        [&LD](const std::string &Name) {<br>
+          return RuntimeDyld::SymbolInfo(nullptr);<br>
         });<br>
<br>
-    BaseLayerModuleSetHandleT H =<br>
-      BaseLayer.addModuleSet(std::move(MSet),<br>
-                             make_unique<SectionMemoryManager>(),<br>
-                             std::move(Resolver));<br>
-    // Add this module to the logical module lookup.<br>
-    DylibLookup->addToLogicalModule(LogicalModule, H);<br>
-    MSI.BaseLayerModuleSetHandles.push_back(H);<br>
-<br>
-    return H;<br>
+    std::vector<std::unique_ptr<Module>> GVsAndStubsMSet;<br>
+    GVsAndStubsMSet.push_back(std::move(GVsAndStubsM));<br>
+    auto GVsAndStubsH =<br>
+      BaseLayer.addModuleSet(std::move(GVsAndStubsMSet),<br>
+                             llvm::make_unique<SectionMemoryManager>(),<br>
+                             std::move(GVsAndStubsResolver));<br>
+    LD.setGVsAndStubsHandle(LMH, GVsAndStubsH);<br>
   }<br>
<br>
   static std::string Mangle(StringRef Name, const DataLayout &DL) {<br>
@@ -373,9 +507,33 @@ private:<br>
<br>
   BaseLayerT &BaseLayer;<br>
   CompileCallbackMgrT &CompileCallbackMgr;<br>
-  ModuleSetInfoListT ModuleSetInfos;<br>
+  LogicalDylibList LogicalDylibs;<br>
 };<br>
<br>
+template <typename BaseLayerT, typename CompileCallbackMgrT><br>
+typename CompileOnDemandLayer<BaseLayerT, CompileCallbackMgrT>::<br>
+           UncompiledPartition&<br>
+CompileOnDemandLayer<BaseLayerT, CompileCallbackMgrT>::LogicalDylib::<br>
+  createUncompiledPartition(LMHandle LMH, std::shared_ptr<Module> SrcM) {<br>
+  UncompiledPartitions.push_back(<br>
+      llvm::make_unique<UncompiledPartition>(*this, LMH, std::move(SrcM)));<br>
+  UncompiledPartitions.back()->setID(UncompiledPartitions.size() - 1);<br>
+  return *UncompiledPartitions.back();<br>
+}<br>
+<br>
+template <typename BaseLayerT, typename CompileCallbackMgrT><br>
+std::unique_ptr<typename CompileOnDemandLayer<BaseLayerT, CompileCallbackMgrT>::<br>
+                  UncompiledPartition><br>
+CompileOnDemandLayer<BaseLayerT, CompileCallbackMgrT>::LogicalDylib::<br>
+  takeUPOwnership(UncompiledPartitionID ID) {<br>
+<br>
+  std::swap(UncompiledPartitions[ID], UncompiledPartitions.back());<br>
+  UncompiledPartitions[ID]->setID(ID);<br>
+  auto UP = std::move(UncompiledPartitions.back());<br>
+  UncompiledPartitions.pop_back();<br>
+  return UP;<br>
+}<br>
+<br>
 } // End namespace orc.<br>
 } // End namespace llvm.<br>
<br>
<br>
Modified: llvm/trunk/include/llvm/ExecutionEngine/Orc/IndirectionUtils.h<br>
URL: <a href="http://llvm.org/viewvc/llvm-project/llvm/trunk/include/llvm/ExecutionEngine/Orc/IndirectionUtils.h?rev=236465&r1=236464&r2=236465&view=diff" target="_blank">http://llvm.org/viewvc/llvm-project/llvm/trunk/include/llvm/ExecutionEngine/Orc/IndirectionUtils.h?rev=236465&r1=236464&r2=236465&view=diff</a><br>
==============================================================================<br>
--- llvm/trunk/include/llvm/ExecutionEngine/Orc/IndirectionUtils.h (original)<br>
+++ llvm/trunk/include/llvm/ExecutionEngine/Orc/IndirectionUtils.h Mon May  4 17:03:10 2015<br>
@@ -21,6 +21,7 @@<br>
 #include "llvm/IR/IRBuilder.h"<br>
 #include "llvm/IR/Mangler.h"<br>
 #include "llvm/IR/Module.h"<br>
+#include "llvm/Transforms/Utils/ValueMapper.h"<br>
 #include <sstream><br>
<br>
 namespace llvm {<br>
@@ -32,28 +33,22 @@ class JITCompileCallbackManagerBase {<br>
 public:<br>
<br>
   typedef std::function<TargetAddress()> CompileFtor;<br>
-  typedef std::function<void(TargetAddress)> UpdateFtor;<br>
<br>
   /// @brief Handle to a newly created compile callback. Can be used to get an<br>
   ///        IR constant representing the address of the trampoline, and to set<br>
-  ///        the compile and update actions for the callback.<br>
+  ///        the compile action for the callback.<br>
   class CompileCallbackInfo {<br>
   public:<br>
-    CompileCallbackInfo(TargetAddress Addr, CompileFtor &Compile,<br>
-                        UpdateFtor &Update)<br>
-      : Addr(Addr), Compile(Compile), Update(Update) {}<br>
+    CompileCallbackInfo(TargetAddress Addr, CompileFtor &Compile)<br>
+      : Addr(Addr), Compile(Compile) {}<br>
<br>
     TargetAddress getAddress() const { return Addr; }<br>
     void setCompileAction(CompileFtor Compile) {<br>
       this->Compile = std::move(Compile);<br>
     }<br>
-    void setUpdateAction(UpdateFtor Update) {<br>
-      this->Update = std::move(Update);<br>
-    }<br>
   private:<br>
     TargetAddress Addr;<br>
     CompileFtor &Compile;<br>
-    UpdateFtor &Update;<br>
   };<br>
<br>
   /// @brief Construct a JITCompileCallbackManagerBase.<br>
@@ -71,8 +66,8 @@ public:<br>
<br>
   /// @brief Execute the callback for the given trampoline id. Called by the JIT<br>
   ///        to compile functions on demand.<br>
-  TargetAddress executeCompileCallback(TargetAddress TrampolineID) {<br>
-    TrampolineMapT::iterator I = ActiveTrampolines.find(TrampolineID);<br>
+  TargetAddress executeCompileCallback(TargetAddress TrampolineAddr) {<br>
+    auto I = ActiveTrampolines.find(TrampolineAddr);<br>
     // FIXME: Also raise an error in the Orc error-handler when we finally have<br>
     //        one.<br>
     if (I == ActiveTrampolines.end())<br>
@@ -84,31 +79,43 @@ public:<br>
     // Moving the trampoline ID back to the available list first means there's at<br>
     // least one available trampoline if the compile action triggers a request for<br>
     // a new one.<br>
-    AvailableTrampolines.push_back(I->first);<br>
-    auto CallbackHandler = std::move(I->second);<br>
+    auto Compile = std::move(I->second);<br>
     ActiveTrampolines.erase(I);<br>
+    AvailableTrampolines.push_back(TrampolineAddr);<br>
<br>
-    if (auto Addr = CallbackHandler.Compile()) {<br>
-      CallbackHandler.Update(Addr);<br>
+    if (auto Addr = Compile())<br>
       return Addr;<br>
-    }<br>
+<br>
     return ErrorHandlerAddress;<br>
   }<br>
<br>
-  /// @brief Get/create a compile callback with the given signature.<br>
+  /// @brief Reserve a compile callback.<br>
   virtual CompileCallbackInfo getCompileCallback(LLVMContext &Context) = 0;<br>
<br>
-protected:<br>
+  /// @brief Get a CompileCallbackInfo for an existing callback.<br>
+  CompileCallbackInfo getCompileCallbackInfo(TargetAddress TrampolineAddr) {<br>
+    auto I = ActiveTrampolines.find(TrampolineAddr);<br>
+    assert(I != ActiveTrampolines.end() && "Not an active trampoline.");<br>
+    return CompileCallbackInfo(I->first, I->second);<br>
+  }<br>
<br>
-  struct CallbackHandler {<br>
-    CompileFtor Compile;<br>
-    UpdateFtor Update;<br>
-  };<br>
+  /// @brief Release a compile callback.<br>
+  ///<br>
+  ///   Note: Callbacks are auto-released after they execute. This method should<br>
+  /// only be called to manually release a callback that is not going to<br>
+  /// execute.<br>
+  void releaseCompileCallback(TargetAddress TrampolineAddr) {<br>
+    auto I = ActiveTrampolines.find(TrampolineAddr);<br>
+    assert(I != ActiveTrampolines.end() && "Not an active trampoline.");<br>
+    ActiveTrampolines.erase(I);<br>
+    AvailableTrampolines.push_back(TrampolineAddr);<br>
+  }<br>
<br>
+protected:<br>
   TargetAddress ErrorHandlerAddress;<br>
   unsigned NumTrampolinesPerBlock;<br>
<br>
-  typedef std::map<TargetAddress, CallbackHandler> TrampolineMapT;<br>
+  typedef std::map<TargetAddress, CompileFtor> TrampolineMapT;<br>
   TrampolineMapT ActiveTrampolines;<br>
   std::vector<TargetAddress> AvailableTrampolines;<br>
 };<br>
@@ -140,11 +147,8 @@ public:<br>
   /// @brief Get/create a compile callback with the given signature.<br>
   CompileCallbackInfo getCompileCallback(LLVMContext &Context) final {<br>
     TargetAddress TrampolineAddr = getAvailableTrampolineAddr(Context);<br>
-    auto &CallbackHandler =<br>
-      this->ActiveTrampolines[TrampolineAddr];<br>
-<br>
-    return CompileCallbackInfo(TrampolineAddr, CallbackHandler.Compile,<br>
-                               CallbackHandler.Update);<br>
+    auto &Compile = this->ActiveTrampolines[TrampolineAddr];<br>
+    return CompileCallbackInfo(TrampolineAddr, Compile);<br>
   }<br>
<br>
 private:<br>
@@ -218,22 +222,6 @@ private:<br>
   TargetAddress ResolverBlockAddr;<br>
 };<br>
<br>
-/// @brief Get an update functor that updates the value of a named function<br>
-///        pointer.<br>
-template <typename JITLayerT><br>
-JITCompileCallbackManagerBase::UpdateFtor<br>
-getLocalFPUpdater(JITLayerT &JIT, typename JITLayerT::ModuleSetHandleT H,<br>
-                  std::string Name) {<br>
-    // FIXME: Move-capture Name once we can use C++14.<br>
-    return [=,&JIT](TargetAddress Addr) {<br>
-      auto FPSym = JIT.findSymbolIn(H, Name, true);<br>
-      assert(FPSym && "Cannot find function pointer to update.");<br>
-      void *FPAddr = reinterpret_cast<void*>(<br>
-                       static_cast<uintptr_t>(FPSym.getAddress()));<br>
-      memcpy(FPAddr, &Addr, sizeof(uintptr_t));<br>
-    };<br>
-  }<br>
-<br>
 /// @brief Build a function pointer of FunctionType with the given constant<br>
 ///        address.<br>
 ///<br>
@@ -250,27 +238,56 @@ GlobalVariable* createImplPointer(Pointe<br>
 ///        indirect call using the given function pointer.<br>
 void makeStub(Function &F, GlobalVariable &ImplPointer);<br>
<br>
-typedef std::map<Module*, DenseSet<const GlobalValue*>> ModulePartitionMap;<br>
+/// @brief Raise linkage types and rename as necessary to ensure that all<br>
+///        symbols are accessible for other modules.<br>
+///<br>
+///   This should be called before partitioning a module to ensure that the<br>
+/// partitions retain access to each other's symbols.<br>
+void makeAllSymbolsExternallyAccessible(Module &M);<br>
<br>
-/// @brief Extract subsections of a Module into the given Module according to<br>
-///        the given ModulePartitionMap.<br>
-void partition(Module &M, const ModulePartitionMap &PMap);<br>
+/// @brief Clone a function declaration into a new module.<br>
+///<br>
+///   This function can be used as the first step towards creating a callback<br>
+/// stub (see makeStub), or moving a function body (see moveFunctionBody).<br>
+///<br>
+///   If the VMap argument is non-null, a mapping will be added between F and<br>
+/// the new declaration, and between each of F's arguments and the new<br>
+/// declaration's arguments. This map can then be passed in to moveFunction to<br>
+/// move the function body if required. Note: When moving functions between<br>
+/// modules with these utilities, all decls should be cloned (and added to a<br>
+/// single VMap) before any bodies are moved. This will ensure that references<br>
+/// between functions all refer to the versions in the new module.<br>
+Function* cloneFunctionDecl(Module &Dst, const Function &F,<br>
+                            ValueToValueMapTy *VMap = nullptr);<br>
<br>
-/// @brief Struct for trivial "complete" partitioning of a module.<br>
-class FullyPartitionedModule {<br>
-public:<br>
-  std::unique_ptr<Module> GlobalVars;<br>
-  std::unique_ptr<Module> Commons;<br>
-  std::vector<std::unique_ptr<Module>> Functions;<br>
-<br>
-  FullyPartitionedModule() = default;<br>
-  FullyPartitionedModule(FullyPartitionedModule &&S)<br>
-      : GlobalVars(std::move(S.GlobalVars)), Commons(std::move(S.Commons)),<br>
-        Functions(std::move(S.Functions)) {}<br>
-};<br>
+/// @brief Move the body of function 'F' to a cloned function declaration in a<br>
+///        different module (See related cloneFunctionDecl).<br>
+///<br>
+///   If the target function declaration is not supplied via the NewF parameter<br>
+/// then it will be looked up via the VMap.<br>
+///<br>
+///   This will delete the body of function 'F' from its original parent module,<br>
+/// but leave its declaration.<br>
+void moveFunctionBody(Function &OrigF, ValueToValueMapTy &VMap,<br>
+                      ValueMaterializer *Materializer = nullptr,<br>
+                      Function *NewF = nullptr);<br>
+<br>
+/// @brief Clone a global variable declaration into a new module.<br>
+GlobalVariable* cloneGlobalVariableDecl(Module &Dst, const GlobalVariable &GV,<br>
+                                        ValueToValueMapTy *VMap = nullptr);<br>
<br>
-/// @brief Extract every function in M into a separate module.<br>
-FullyPartitionedModule fullyPartition(Module &M);<br>
+/// @brief Move global variable GV from its parent module to cloned global<br>
+///        declaration in a different module.<br>
+///<br>
+///   If the target global declaration is not supplied via the NewGV parameter<br>
+/// then it will be looked up via the VMap.<br>
+///<br>
+///   This will delete the initializer of GV from its original parent module,<br>
+/// but leave its declaration.<br>
+void moveGlobalVariableInitializer(GlobalVariable &OrigGV,<br>
+                                   ValueToValueMapTy &VMap,<br>
+                                   ValueMaterializer *Materializer = nullptr,<br>
+                                   GlobalVariable *NewGV = nullptr);<br>
<br>
 } // End namespace orc.<br>
 } // End namespace llvm.<br>
<br>
Modified: llvm/trunk/lib/ExecutionEngine/Orc/CMakeLists.txt<br>
URL: <a href="http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/ExecutionEngine/Orc/CMakeLists.txt?rev=236465&r1=236464&r2=236465&view=diff" target="_blank">http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/ExecutionEngine/Orc/CMakeLists.txt?rev=236465&r1=236464&r2=236465&view=diff</a><br>
==============================================================================<br>
--- llvm/trunk/lib/ExecutionEngine/Orc/CMakeLists.txt (original)<br>
+++ llvm/trunk/lib/ExecutionEngine/Orc/CMakeLists.txt Mon May  4 17:03:10 2015<br>
@@ -1,5 +1,4 @@<br>
 add_llvm_library(LLVMOrcJIT<br>
-  CloneSubModule.cpp<br>
   ExecutionUtils.cpp<br>
   IndirectionUtils.cpp<br>
   OrcMCJITReplacement.cpp<br>
<br>
Removed: llvm/trunk/lib/ExecutionEngine/Orc/CloneSubModule.cpp<br>
URL: <a href="http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/ExecutionEngine/Orc/CloneSubModule.cpp?rev=236464&view=auto" target="_blank">http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/ExecutionEngine/Orc/CloneSubModule.cpp?rev=236464&view=auto</a><br>
==============================================================================<br>
--- llvm/trunk/lib/ExecutionEngine/Orc/CloneSubModule.cpp (original)<br>
+++ llvm/trunk/lib/ExecutionEngine/Orc/CloneSubModule.cpp (removed)<br>
@@ -1,106 +0,0 @@<br>
-#include "llvm/ExecutionEngine/Orc/CloneSubModule.h"<br>
-#include "llvm/IR/Function.h"<br>
-#include "llvm/IR/GlobalVariable.h"<br>
-#include "llvm/IR/Module.h"<br>
-#include "llvm/Transforms/Utils/Cloning.h"<br>
-<br>
-namespace llvm {<br>
-namespace orc {<br>
-<br>
-void copyGVInitializer(GlobalVariable &New, const GlobalVariable &Orig,<br>
-                             ValueToValueMapTy &VMap) {<br>
-  if (Orig.hasInitializer())<br>
-    New.setInitializer(MapValue(Orig.getInitializer(), VMap));<br>
-}<br>
-<br>
-void copyFunctionBody(Function &New, const Function &Orig,<br>
-                            ValueToValueMapTy &VMap) {<br>
-  if (!Orig.isDeclaration()) {<br>
-    Function::arg_iterator DestI = New.arg_begin();<br>
-    for (Function::const_arg_iterator J = Orig.arg_begin(); J != Orig.arg_end();<br>
-         ++J) {<br>
-      DestI->setName(J->getName());<br>
-      VMap[J] = DestI++;<br>
-    }<br>
-<br>
-    SmallVector<ReturnInst *, 8> Returns; // Ignore returns cloned.<br>
-    CloneFunctionInto(&New, &Orig, VMap, /*ModuleLevelChanges=*/true, Returns);<br>
-  }<br>
-}<br>
-<br>
-void CloneSubModule(llvm::Module &Dst, const Module &Src,<br>
-                    HandleGlobalVariableFtor HandleGlobalVariable,<br>
-                    HandleFunctionFtor HandleFunction, bool CloneInlineAsm) {<br>
-<br>
-  ValueToValueMapTy VMap;<br>
-<br>
-  if (CloneInlineAsm)<br>
-    Dst.appendModuleInlineAsm(Src.getModuleInlineAsm());<br>
-<br>
-  // Copy global variables (but not initializers, yet).<br>
-  for (Module::const_global_iterator I = Src.global_begin(), E = Src.global_end();<br>
-       I != E; ++I) {<br>
-    GlobalVariable *GV = new GlobalVariable(<br>
-        Dst, I->getType()->getElementType(), I->isConstant(), I->getLinkage(),<br>
-        (Constant *)nullptr, I->getName(), (GlobalVariable *)nullptr,<br>
-        I->getThreadLocalMode(), I->getType()->getAddressSpace());<br>
-    GV->copyAttributesFrom(I);<br>
-    VMap[I] = GV;<br>
-  }<br>
-<br>
-  // Loop over the functions in the module, making external functions as before<br>
-  for (Module::const_iterator I = Src.begin(), E = Src.end(); I != E; ++I) {<br>
-    Function *NF =<br>
-        Function::Create(cast<FunctionType>(I->getType()->getElementType()),<br>
-                         I->getLinkage(), I->getName(), &Dst);<br>
-    NF->copyAttributesFrom(I);<br>
-    VMap[I] = NF;<br>
-  }<br>
-<br>
-  // Loop over the aliases in the module<br>
-  for (Module::const_alias_iterator I = Src.alias_begin(), E = Src.alias_end();<br>
-       I != E; ++I) {<br>
-    auto *PTy = cast<PointerType>(I->getType());<br>
-    auto *GA = GlobalAlias::create(PTy, I->getLinkage(), I->getName(), &Dst);<br>
-    GA->copyAttributesFrom(I);<br>
-    VMap[I] = GA;<br>
-  }<br>
-<br>
-  // Now that all of the things that global variable initializer can refer to<br>
-  // have been created, loop through and copy the global variable referrers<br>
-  // over...  We also set the attributes on the global now.<br>
-  for (Module::const_global_iterator I = Src.global_begin(), E = Src.global_end();<br>
-       I != E; ++I) {<br>
-    GlobalVariable &GV = *cast<GlobalVariable>(VMap[I]);<br>
-    HandleGlobalVariable(GV, *I, VMap);<br>
-  }<br>
-<br>
-  // Similarly, copy over function bodies now...<br>
-  //<br>
-  for (Module::const_iterator I = Src.begin(), E = Src.end(); I != E; ++I) {<br>
-    Function &F = *cast<Function>(VMap[I]);<br>
-    HandleFunction(F, *I, VMap);<br>
-  }<br>
-<br>
-  // And aliases<br>
-  for (Module::const_alias_iterator I = Src.alias_begin(), E = Src.alias_end();<br>
-       I != E; ++I) {<br>
-    GlobalAlias *GA = cast<GlobalAlias>(VMap[I]);<br>
-    if (const Constant *C = I->getAliasee())<br>
-      GA->setAliasee(MapValue(C, VMap));<br>
-  }<br>
-<br>
-  // And named metadata....<br>
-  for (Module::const_named_metadata_iterator I = Src.named_metadata_begin(),<br>
-                                             E = Src.named_metadata_end();<br>
-       I != E; ++I) {<br>
-    const NamedMDNode &NMD = *I;<br>
-    NamedMDNode *NewNMD = Dst.getOrInsertNamedMetadata(NMD.getName());<br>
-    for (unsigned i = 0, e = NMD.getNumOperands(); i != e; ++i)<br>
-      NewNMD->addOperand(MapMetadata(NMD.getOperand(i), VMap));<br>
-  }<br>
-<br>
-}<br>
-<br>
-} // End namespace orc.<br>
-} // End namespace llvm.<br>
<br>
Modified: llvm/trunk/lib/ExecutionEngine/Orc/IndirectionUtils.cpp<br>
URL: <a href="http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/ExecutionEngine/Orc/IndirectionUtils.cpp?rev=236465&r1=236464&r2=236465&view=diff" target="_blank">http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/ExecutionEngine/Orc/IndirectionUtils.cpp?rev=236465&r1=236464&r2=236465&view=diff</a><br>
==============================================================================<br>
--- llvm/trunk/lib/ExecutionEngine/Orc/IndirectionUtils.cpp (original)<br>
+++ llvm/trunk/lib/ExecutionEngine/Orc/IndirectionUtils.cpp Mon May  4 17:03:10 2015<br>
@@ -9,10 +9,10 @@<br>
<br>
 #include "llvm/ADT/STLExtras.h"<br>
 #include "llvm/ADT/Triple.h"<br>
-#include "llvm/ExecutionEngine/Orc/CloneSubModule.h"<br>
 #include "llvm/ExecutionEngine/Orc/IndirectionUtils.h"<br>
 #include "llvm/IR/CallSite.h"<br>
 #include "llvm/IR/IRBuilder.h"<br>
+#include "llvm/Transforms/Utils/Cloning.h"<br>
 #include <set><br>
 #include <sstream><br>
<br>
@@ -32,9 +32,11 @@ GlobalVariable* createImplPointer(Pointe<br>
                                   const Twine &Name, Constant *Initializer) {<br>
   if (!Initializer)<br>
     Initializer = Constant::getNullValue(&PT);<br>
-  return new GlobalVariable(M, &PT, false, GlobalValue::ExternalLinkage,<br>
-                            Initializer, Name, nullptr,<br>
-                            GlobalValue::NotThreadLocal, 0, true);<br>
+  auto IP = new GlobalVariable(M, &PT, false, GlobalValue::ExternalLinkage,<br>
+                               Initializer, Name, nullptr,<br>
+                               GlobalValue::NotThreadLocal, 0, true);<br>
+  IP->setVisibility(GlobalValue::HiddenVisibility);<br>
+  return IP;<br>
 }<br>
<br>
 void makeStub(Function &F, GlobalVariable &ImplPointer) {<br>
@@ -50,7 +52,10 @@ void makeStub(Function &F, GlobalVariabl<br>
   CallInst *Call = Builder.CreateCall(ImplAddr, CallArgs);<br>
   Call->setTailCall();<br>
   Call->setAttributes(F.getAttributes());<br>
-  Builder.CreateRet(Call);<br>
+  if (F.getReturnType()->isVoidTy())<br>
+    Builder.CreateRetVoid();<br>
+  else<br>
+    Builder.CreateRet(Call);<br>
 }<br>
<br>
 // Utility class for renaming global values and functions during partitioning.<br>
@@ -84,83 +89,94 @@ private:<br>
   DenseMap<const Value*, std::string> Names;<br>
 };<br>
<br>
-void partition(Module &M, const ModulePartitionMap &PMap) {<br>
+static void raiseVisibilityOnValue(GlobalValue &V, GlobalRenamer &R) {<br>
+  if (V.hasLocalLinkage()) {<br>
+    if (R.needsRenaming(V))<br>
+      V.setName(R.getRename(V));<br>
+    V.setLinkage(GlobalValue::ExternalLinkage);<br>
+    V.setVisibility(GlobalValue::HiddenVisibility);<br>
+  }<br>
+  V.setUnnamedAddr(false);<br>
+  assert(!R.needsRenaming(V) && "Invalid global name.");<br>
+}<br>
<br>
+void makeAllSymbolsExternallyAccessible(Module &M) {<br>
   GlobalRenamer Renamer;<br>
<br>
-  for (auto &KVPair : PMap) {<br>
+  for (auto &F : M)<br>
+    raiseVisibilityOnValue(F, Renamer);<br>
<br>
-    auto ExtractGlobalVars =<br>
-      [&](GlobalVariable &New, const GlobalVariable &Orig,<br>
-          ValueToValueMapTy &VMap) {<br>
-        if (KVPair.second.count(&Orig)) {<br>
-          copyGVInitializer(New, Orig, VMap);<br>
-        }<br>
-        if (New.hasLocalLinkage()) {<br>
-          if (Renamer.needsRenaming(New))<br>
-            New.setName(Renamer.getRename(Orig));<br>
-          New.setLinkage(GlobalValue::ExternalLinkage);<br>
-          New.setVisibility(GlobalValue::HiddenVisibility);<br>
-        }<br>
-        assert(!Renamer.needsRenaming(New) && "Invalid global name.");<br>
-      };<br>
-<br>
-    auto ExtractFunctions =<br>
-      [&](Function &New, const Function &Orig, ValueToValueMapTy &VMap) {<br>
-        if (KVPair.second.count(&Orig))<br>
-          copyFunctionBody(New, Orig, VMap);<br>
-        if (New.hasLocalLinkage()) {<br>
-          if (Renamer.needsRenaming(New))<br>
-            New.setName(Renamer.getRename(Orig));<br>
-          New.setLinkage(GlobalValue::ExternalLinkage);<br>
-          New.setVisibility(GlobalValue::HiddenVisibility);<br>
-        }<br>
-        assert(!Renamer.needsRenaming(New) && "Invalid function name.");<br>
-      };<br>
-<br>
-    CloneSubModule(*KVPair.first, M, ExtractGlobalVars, ExtractFunctions,<br>
-                   false);<br>
-  }<br>
+  for (auto &GV : M.globals())<br>
+    raiseVisibilityOnValue(GV, Renamer);<br>
 }<br>
<br>
-FullyPartitionedModule fullyPartition(Module &M) {<br>
-  FullyPartitionedModule MP;<br>
-<br>
-  ModulePartitionMap PMap;<br>
-<br>
-  for (auto &F : M) {<br>
+Function* cloneFunctionDecl(Module &Dst, const Function &F,<br>
+                            ValueToValueMapTy *VMap) {<br>
+  assert(F.getParent() != &Dst && "Can't copy decl over existing function.");<br>
+  Function *NewF =<br>
+    Function::Create(cast<FunctionType>(F.getType()->getElementType()),<br>
+                     F.getLinkage(), F.getName(), &Dst);<br>
+  NewF->copyAttributesFrom(&F);<br>
+<br>
+  if (VMap) {<br>
+    (*VMap)[&F] = NewF;<br>
+    auto NewArgI = NewF->arg_begin();<br>
+    for (auto ArgI = F.arg_begin(), ArgE = F.arg_end(); ArgI != ArgE;<br>
+         ++ArgI, ++NewArgI)<br>
+      (*VMap)[ArgI] = NewArgI;<br>
+  }<br>
<br>
-    if (F.isDeclaration())<br>
-      continue;<br>
+  return NewF;<br>
+}<br>
<br>
-    std::string NewModuleName = (M.getName() + "." + F.getName()).str();<br>
-    MP.Functions.push_back(<br>
-      llvm::make_unique<Module>(NewModuleName, M.getContext()));<br>
-    MP.Functions.back()->setDataLayout(M.getDataLayout());<br>
-    PMap[MP.Functions.back().get()].insert(&F);<br>
-  }<br>
+void moveFunctionBody(Function &OrigF, ValueToValueMapTy &VMap,<br>
+                      ValueMaterializer *Materializer,<br>
+                      Function *NewF) {<br>
+  assert(!OrigF.isDeclaration() && "Nothing to move");<br>
+  if (!NewF)<br>
+    NewF = cast<Function>(VMap[&OrigF]);<br>
+  else<br>
+    assert(VMap[&OrigF] == NewF && "Incorrect function mapping in VMap.");<br>
+  assert(NewF && "Function mapping missing from VMap.");<br>
+  assert(NewF->getParent() != OrigF.getParent() &&<br>
+         "moveFunctionBody should only be used to move bodies between "<br>
+         "modules.");<br>
+<br>
+  SmallVector<ReturnInst *, 8> Returns; // Ignore returns cloned.<br>
+  CloneFunctionInto(NewF, &OrigF, VMap, /*ModuleLevelChanges=*/true, Returns,<br>
+                    "", nullptr, nullptr, Materializer);<br>
+  OrigF.deleteBody();<br>
+}<br>
<br>
-  MP.GlobalVars =<br>
-    llvm::make_unique<Module>((M.getName() + ".globals_and_stubs").str(),<br>
-                              M.getContext());<br>
-  MP.GlobalVars->setDataLayout(M.getDataLayout());<br>
-<br>
-  MP.Commons =<br>
-    llvm::make_unique<Module>((M.getName() + ".commons").str(), M.getContext());<br>
-  MP.Commons->setDataLayout(M.getDataLayout());<br>
-<br>
-  // Make sure there's at least an empty set for the stubs map or we'll fail<br>
-  // to clone anything for it (including the decls).<br>
-  PMap[MP.GlobalVars.get()] = ModulePartitionMap::mapped_type();<br>
-  for (auto &GV : M.globals())<br>
-    if (GV.getLinkage() == GlobalValue::CommonLinkage)<br>
-      PMap[MP.Commons.get()].insert(&GV);<br>
-    else<br>
-      PMap[MP.GlobalVars.get()].insert(&GV);<br>
+GlobalVariable* cloneGlobalVariableDecl(Module &Dst, const GlobalVariable &GV,<br>
+                                        ValueToValueMapTy *VMap) {<br>
+  assert(GV.getParent() != &Dst && "Can't copy decl over existing global var.");<br>
+  GlobalVariable *NewGV = new GlobalVariable(<br>
+      Dst, GV.getType()->getElementType(), GV.isConstant(),<br>
+      GV.getLinkage(), nullptr, GV.getName(), nullptr,<br>
+      GV.getThreadLocalMode(), GV.getType()->getAddressSpace());<br>
+  NewGV->copyAttributesFrom(&GV);<br>
+  if (VMap)<br>
+    (*VMap)[&GV] = NewGV;<br>
+  return NewGV;<br>
+}<br>
<br>
-  partition(M, PMap);<br>
+void moveGlobalVariableInitializer(GlobalVariable &OrigGV,<br>
+                                   ValueToValueMapTy &VMap,<br>
+                                   ValueMaterializer *Materializer,<br>
+                                   GlobalVariable *NewGV) {<br>
+  assert(OrigGV.hasInitializer() && "Nothing to move");<br>
+  if (!NewGV)<br>
+    NewGV = cast<GlobalVariable>(VMap[&OrigGV]);<br>
+  else<br>
+    assert(VMap[&OrigGV] == NewGV &&<br>
+           "Incorrect global variable mapping in VMap.");<br>
+  assert(NewGV->getParent() != OrigGV.getParent() &&<br>
+         "moveGlobalVariable should only be used to move initializers between "<br>
+         "modules");<br>
<br>
-  return MP;<br>
+  NewGV->setInitializer(MapValue(OrigGV.getInitializer(), VMap, RF_None,<br>
+                                 nullptr, Materializer));<br>
 }<br>
<br>
 } // End namespace orc.<br>
<br>
Modified: llvm/trunk/test/ExecutionEngine/OrcLazy/hello.ll<br>
URL: <a href="http://llvm.org/viewvc/llvm-project/llvm/trunk/test/ExecutionEngine/OrcLazy/hello.ll?rev=236465&r1=236464&r2=236465&view=diff" target="_blank">http://llvm.org/viewvc/llvm-project/llvm/trunk/test/ExecutionEngine/OrcLazy/hello.ll?rev=236465&r1=236464&r2=236465&view=diff</a><br>
==============================================================================<br>
--- llvm/trunk/test/ExecutionEngine/OrcLazy/hello.ll (original)<br>
+++ llvm/trunk/test/ExecutionEngine/OrcLazy/hello.ll Mon May  4 17:03:10 2015<br>
@@ -1,7 +1,7 @@<br>
 ; RUN: lli -jit-kind=orc-lazy -orc-lazy-debug=funcs-to-stdout %s | FileCheck %s<br>
 ;<br>
 ; CHECK: Hello<br>
-; CHECK: [ {{.*}}main$orc_body ]<br>
+; CHECK: [ {{.*}}main ]<br></blockquote><div><br>What does this change represent?<br><br>(& I imagine a bunch of those utils you wrote (& the layer itself) could be well unit tested - checking that the split module doesn't include uninteresting decls, etc)<br> </div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
 ; CHECK: Goodbye<br>
<br>
 %class.Foo = type { i8 }<br>
<br>
Modified: llvm/trunk/tools/lli/OrcLazyJIT.h<br>
URL: <a href="http://llvm.org/viewvc/llvm-project/llvm/trunk/tools/lli/OrcLazyJIT.h?rev=236465&r1=236464&r2=236465&view=diff" target="_blank">http://llvm.org/viewvc/llvm-project/llvm/trunk/tools/lli/OrcLazyJIT.h?rev=236465&r1=236464&r2=236465&view=diff</a><br>
==============================================================================<br>
--- llvm/trunk/tools/lli/OrcLazyJIT.h (original)<br>
+++ llvm/trunk/tools/lli/OrcLazyJIT.h Mon May  4 17:03:10 2015<br>
@@ -21,7 +21,6 @@<br>
 #include "llvm/ExecutionEngine/Orc/ExecutionUtils.h"<br>
 #include "llvm/ExecutionEngine/Orc/IRCompileLayer.h"<br>
 #include "llvm/ExecutionEngine/Orc/IRTransformLayer.h"<br>
-#include "llvm/ExecutionEngine/Orc/LazyEmittingLayer.h"<br>
 #include "llvm/ExecutionEngine/Orc/ObjectLinkingLayer.h"<br>
 #include "llvm/ExecutionEngine/RTDyldMemoryManager.h"<br>
 #include "llvm/IR/LLVMContext.h"<br>
@@ -37,9 +36,7 @@ public:<br>
   typedef std::function<std::unique_ptr<Module>(std::unique_ptr<Module>)><br>
     TransformFtor;<br>
   typedef orc::IRTransformLayer<CompileLayerT, TransformFtor> IRDumpLayerT;<br>
-  typedef orc::LazyEmittingLayer<IRDumpLayerT> LazyEmitLayerT;<br>
-  typedef orc::CompileOnDemandLayer<LazyEmitLayerT,<br>
-                                    CompileCallbackMgr> CODLayerT;<br>
+  typedef orc::CompileOnDemandLayer<IRDumpLayerT, CompileCallbackMgr> CODLayerT;<br>
   typedef CODLayerT::ModuleSetHandleT ModuleHandleT;<br>
<br>
   typedef std::function<<br>
@@ -57,9 +54,8 @@ public:<br>
       ObjectLayer(),<br>
       CompileLayer(ObjectLayer, orc::SimpleCompiler(*this->TM)),<br>
       IRDumpLayer(CompileLayer, createDebugDumper()),<br>
-      LazyEmitLayer(IRDumpLayer),<br>
       CCMgr(BuildCallbackMgr(IRDumpLayer, CCMgrMemMgr, Context)),<br>
-      CODLayer(LazyEmitLayer, *CCMgr),<br>
+      CODLayer(IRDumpLayer, *CCMgr),<br>
       CXXRuntimeOverrides([this](const std::string &S) { return mangle(S); }) {}<br>
<br>
   ~OrcLazyJIT() {<br>
@@ -154,7 +150,6 @@ private:<br>
   ObjLayerT ObjectLayer;<br>
   CompileLayerT CompileLayer;<br>
   IRDumpLayerT IRDumpLayer;<br>
-  LazyEmitLayerT LazyEmitLayer;<br>
   std::unique_ptr<CompileCallbackMgr> CCMgr;<br>
   CODLayerT CODLayer;<br>
<br>
<br>
<br>
_______________________________________________<br>
llvm-commits mailing list<br>
<a href="mailto:llvm-commits@cs.uiuc.edu">llvm-commits@cs.uiuc.edu</a><br>
<a href="http://lists.cs.uiuc.edu/mailman/listinfo/llvm-commits" target="_blank">http://lists.cs.uiuc.edu/mailman/listinfo/llvm-commits</a><br>
</blockquote></div><br></div></div>
</div></blockquote></div></body></html>