[llvm] r332918 - [LKH] Add a replacement RTDyldLayer.

Lang Hames via llvm-commits llvm-commits at lists.llvm.org
Mon May 21 16:45:40 PDT 2018


Author: lhames
Date: Mon May 21 16:45:40 2018
New Revision: 332918

URL: http://llvm.org/viewvc/llvm-project?rev=332918&view=rev
Log:
[LKH] Add a replacement RTDyldLayer.

Added:
    llvm/trunk/lib/ExecutionEngine/Orc/RTDyldObjectLinkingLayer.cpp
Modified:
    llvm/trunk/include/llvm/ExecutionEngine/Orc/RTDyldObjectLinkingLayer.h
    llvm/trunk/lib/ExecutionEngine/Orc/CMakeLists.txt

Modified: llvm/trunk/include/llvm/ExecutionEngine/Orc/RTDyldObjectLinkingLayer.h
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/include/llvm/ExecutionEngine/Orc/RTDyldObjectLinkingLayer.h?rev=332918&r1=332917&r2=332918&view=diff
==============================================================================
--- llvm/trunk/include/llvm/ExecutionEngine/Orc/RTDyldObjectLinkingLayer.h (original)
+++ llvm/trunk/include/llvm/ExecutionEngine/Orc/RTDyldObjectLinkingLayer.h Mon May 21 16:45:40 2018
@@ -19,6 +19,7 @@
 #include "llvm/ADT/StringRef.h"
 #include "llvm/ExecutionEngine/JITSymbol.h"
 #include "llvm/ExecutionEngine/Orc/Core.h"
+#include "llvm/ExecutionEngine/Orc/Layer.h"
 #include "llvm/ExecutionEngine/Orc/Legacy.h"
 #include "llvm/ExecutionEngine/RuntimeDyld.h"
 #include "llvm/Object/ObjectFile.h"
@@ -35,6 +36,59 @@
 namespace llvm {
 namespace orc {
 
+class RTDyldObjectLinkingLayer2 : public ObjectLayer {
+public:
+  /// Functor for receiving object-loaded notifications.
+  using NotifyLoadedFunction =
+      std::function<void(VModuleKey, const object::ObjectFile &Obj,
+                         const RuntimeDyld::LoadedObjectInfo &)>;
+
+  /// Functor for receiving finalization notifications.
+  using NotifyFinalizedFunction = std::function<void(VModuleKey)>;
+
+  struct Resources {
+    std::shared_ptr<RuntimeDyld::MemoryManager> MemMgr;
+    std::shared_ptr<SymbolResolver> Resolver;
+  };
+
+  using ResourcesGetterFunction = std::function<Resources(VModuleKey)>;
+
+  /// Construct an ObjectLinkingLayer with the given NotifyLoaded,
+  ///        and NotifyFinalized functors.
+  RTDyldObjectLinkingLayer2(
+      ExecutionSession &ES, ResourcesGetterFunction GetResources,
+      NotifyLoadedFunction NotifyLoaded = NotifyLoadedFunction(),
+      NotifyFinalizedFunction NotifyFinalized = NotifyFinalizedFunction());
+
+  /// Emit the object.
+  void emit(MaterializationResponsibility R, VModuleKey K,
+            std::unique_ptr<MemoryBuffer> O) override;
+
+  /// Map section addresses for the object associated with the
+  ///        VModuleKey K.
+  void mapSectionAddress(VModuleKey K, const void *LocalAddress,
+                         JITTargetAddress TargetAddr) const;
+
+  /// Set the 'ProcessAllSections' flag.
+  ///
+  /// If set to true, all sections in each object file will be allocated using
+  /// the memory manager, rather than just the sections required for execution.
+  ///
+  /// This is kludgy, and may be removed in the future.
+  void setProcessAllSections(bool ProcessAllSections) {
+    this->ProcessAllSections = ProcessAllSections;
+  }
+
+private:
+  mutable std::mutex RTDyldLayerMutex;
+  ResourcesGetterFunction GetResources;
+  NotifyLoadedFunction NotifyLoaded;
+  NotifyFinalizedFunction NotifyFinalized;
+  bool ProcessAllSections;
+  std::map<VModuleKey, RuntimeDyld *> ActiveRTDylds;
+  std::map<VModuleKey, std::shared_ptr<RuntimeDyld::MemoryManager>> MemMgrs;
+};
+
 class RTDyldObjectLinkingLayerBase {
 public:
   using ObjectPtr = std::unique_ptr<MemoryBuffer>;

Modified: llvm/trunk/lib/ExecutionEngine/Orc/CMakeLists.txt
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/ExecutionEngine/Orc/CMakeLists.txt?rev=332918&r1=332917&r2=332918&view=diff
==============================================================================
--- llvm/trunk/lib/ExecutionEngine/Orc/CMakeLists.txt (original)
+++ llvm/trunk/lib/ExecutionEngine/Orc/CMakeLists.txt Mon May 21 16:45:40 2018
@@ -10,6 +10,7 @@ add_llvm_library(LLVMOrcJIT
   OrcError.cpp
   OrcMCJITReplacement.cpp
   RPCUtils.cpp
+  RTDyldObjectLinkingLayer.cpp
 
   ADDITIONAL_HEADER_DIRS
   ${LLVM_MAIN_INCLUDE_DIR}/llvm/ExecutionEngine/Orc

Added: llvm/trunk/lib/ExecutionEngine/Orc/RTDyldObjectLinkingLayer.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/ExecutionEngine/Orc/RTDyldObjectLinkingLayer.cpp?rev=332918&view=auto
==============================================================================
--- llvm/trunk/lib/ExecutionEngine/Orc/RTDyldObjectLinkingLayer.cpp (added)
+++ llvm/trunk/lib/ExecutionEngine/Orc/RTDyldObjectLinkingLayer.cpp Mon May 21 16:45:40 2018
@@ -0,0 +1,96 @@
+//===-- RTDyldObjectLinkingLayer.cpp - RuntimeDyld backed ORC ObjectLayer -===//
+//
+//                     The LLVM Compiler Infrastructure
+//
+// This file is distributed under the University of Illinois Open Source
+// License. See LICENSE.TXT for details.
+//
+//===----------------------------------------------------------------------===//
+
+#include "llvm/ExecutionEngine/Orc/RTDyldObjectLinkingLayer.h"
+
+namespace llvm {
+namespace orc {
+
+RTDyldObjectLinkingLayer2::RTDyldObjectLinkingLayer2(
+    ExecutionSession &ES, ResourcesGetterFunction GetResources,
+    NotifyLoadedFunction NotifyLoaded, NotifyFinalizedFunction NotifyFinalized)
+    : ObjectLayer(ES), GetResources(std::move(GetResources)),
+      NotifyLoaded(std::move(NotifyLoaded)),
+      NotifyFinalized(std::move(NotifyFinalized)), ProcessAllSections(false) {}
+
+void RTDyldObjectLinkingLayer2::emit(MaterializationResponsibility R,
+                                     VModuleKey K,
+                                     std::unique_ptr<MemoryBuffer> O) {
+  assert(O && "Object has already been materialized");
+
+  auto &ES = getExecutionSession();
+
+  auto ObjFile = object::ObjectFile::createObjectFile(*O);
+  if (!ObjFile) {
+    getExecutionSession().reportError(ObjFile.takeError());
+    R.failMaterialization();
+  }
+
+  auto Resources = GetResources(K);
+
+  JITSymbolResolverAdapter ResolverAdapter(ES, *Resources.Resolver, &R);
+  auto RTDyld =
+      llvm::make_unique<RuntimeDyld>(*Resources.MemMgr, ResolverAdapter);
+  RTDyld->setProcessAllSections(ProcessAllSections);
+
+  {
+    std::lock_guard<std::mutex> Lock(RTDyldLayerMutex);
+
+    assert(!ActiveRTDylds.count(K) &&
+           "An active RTDyld already exists for this key?");
+    ActiveRTDylds[K] = RTDyld.get();
+
+    assert(!MemMgrs.count(K) &&
+           "A memory manager already exists for this key?");
+    MemMgrs[K] = Resources.MemMgr;
+  }
+
+  auto Info = RTDyld->loadObject(**ObjFile);
+
+  {
+    SymbolMap Symbols;
+    for (auto &KV : RTDyld->getSymbolTable())
+      Symbols[ES.getSymbolStringPool().intern(KV.first)] = KV.second;
+    R.resolve(Symbols);
+  }
+
+  if (NotifyLoaded)
+    NotifyLoaded(K, **ObjFile, *Info);
+
+  RTDyld->finalizeWithMemoryManagerLocking();
+
+  {
+    std::lock_guard<std::mutex> Lock(RTDyldLayerMutex);
+    ActiveRTDylds.erase(K);
+  }
+
+  if (RTDyld->hasError()) {
+    ES.reportError(make_error<StringError>(RTDyld->getErrorString(),
+                                           inconvertibleErrorCode()));
+    R.failMaterialization();
+  }
+
+  R.finalize();
+
+  if (NotifyFinalized)
+    NotifyFinalized(K);
+}
+
+void RTDyldObjectLinkingLayer2::mapSectionAddress(
+    VModuleKey K, const void *LocalAddress, JITTargetAddress TargetAddr) const {
+  std::lock_guard<std::mutex> Lock(RTDyldLayerMutex);
+  auto ActiveRTDyldItr = ActiveRTDylds.find(K);
+
+  assert(ActiveRTDyldItr != ActiveRTDylds.end() &&
+         "No active RTDyld instance found for key");
+  ActiveRTDyldItr->second->mapSectionAddress(LocalAddress, TargetAddr);
+}
+
+} // End namespace orc.
+} // End namespace llvm.




More information about the llvm-commits mailing list