diff options
Diffstat (limited to 'include/llvm/ExecutionEngine/Orc')
16 files changed, 472 insertions, 447 deletions
diff --git a/include/llvm/ExecutionEngine/Orc/CompileOnDemandLayer.h b/include/llvm/ExecutionEngine/Orc/CompileOnDemandLayer.h index 2fccf8a0f625e..8ac1b6bca0a7a 100644 --- a/include/llvm/ExecutionEngine/Orc/CompileOnDemandLayer.h +++ b/include/llvm/ExecutionEngine/Orc/CompileOnDemandLayer.h @@ -24,16 +24,20 @@ #include "llvm/ExecutionEngine/Orc/LambdaResolver.h" #include "llvm/ExecutionEngine/RuntimeDyld.h" #include "llvm/IR/Attributes.h" +#include "llvm/IR/Constant.h" #include "llvm/IR/Constants.h" #include "llvm/IR/DataLayout.h" #include "llvm/IR/Function.h" #include "llvm/IR/GlobalAlias.h" #include "llvm/IR/GlobalValue.h" #include "llvm/IR/GlobalVariable.h" +#include "llvm/IR/Instruction.h" #include "llvm/IR/Mangler.h" #include "llvm/IR/Module.h" +#include "llvm/IR/Type.h" #include "llvm/Support/Casting.h" #include "llvm/Support/raw_ostream.h" +#include "llvm/Transforms/Utils/ValueMapper.h" #include <algorithm> #include <cassert> #include <functional> @@ -46,6 +50,9 @@ #include <vector> namespace llvm { + +class Value; + namespace orc { /// @brief Compile-on-demand layer. @@ -77,15 +84,15 @@ private: return LambdaMaterializer<MaterializerFtor>(std::move(M)); } - typedef typename BaseLayerT::ModuleSetHandleT BaseLayerModuleSetHandleT; + using BaseLayerModuleHandleT = typename BaseLayerT::ModuleHandleT; // Provide type-erasure for the Modules and MemoryManagers. template <typename ResourceT> class ResourceOwner { public: ResourceOwner() = default; - ResourceOwner(const ResourceOwner&) = delete; - ResourceOwner& operator=(const ResourceOwner&) = delete; + ResourceOwner(const ResourceOwner &) = delete; + ResourceOwner &operator=(const ResourceOwner &) = delete; virtual ~ResourceOwner() = default; virtual ResourceT& getResource() const = 0; @@ -106,7 +113,7 @@ private: template <typename ResourceT, typename ResourcePtrT> std::unique_ptr<ResourceOwner<ResourceT>> wrapOwnership(ResourcePtrT ResourcePtr) { - typedef ResourceOwnerImpl<ResourceT, ResourcePtrT> RO; + using RO = ResourceOwnerImpl<ResourceT, ResourcePtrT>; return llvm::make_unique<RO>(std::move(ResourcePtr)); } @@ -130,21 +137,21 @@ private: }; struct LogicalDylib { - typedef std::function<JITSymbol(const std::string&)> SymbolResolverFtor; + using SymbolResolverFtor = std::function<JITSymbol(const std::string&)>; - typedef std::function<typename BaseLayerT::ModuleSetHandleT( - BaseLayerT&, - std::unique_ptr<Module>, - std::unique_ptr<JITSymbolResolver>)> - ModuleAdderFtor; + using ModuleAdderFtor = + std::function<typename BaseLayerT::ModuleHandleT( + BaseLayerT&, + std::unique_ptr<Module>, + std::unique_ptr<JITSymbolResolver>)>; struct SourceModuleEntry { std::unique_ptr<ResourceOwner<Module>> SourceMod; std::set<Function*> StubsToClone; }; - typedef std::vector<SourceModuleEntry> SourceModulesList; - typedef typename SourceModulesList::size_type SourceModuleHandle; + using SourceModulesList = std::vector<SourceModuleEntry>; + using SourceModuleHandle = typename SourceModulesList::size_type; SourceModuleHandle addSourceModule(std::unique_ptr<ResourceOwner<Module>> M) { @@ -174,7 +181,7 @@ private: void removeModulesFromBaseLayer(BaseLayerT &BaseLayer) { for (auto &BLH : BaseLayerHandles) - BaseLayer.removeModuleSet(BLH); + BaseLayer.removeModule(BLH); } std::unique_ptr<JITSymbolResolver> ExternalSymbolResolver; @@ -183,21 +190,21 @@ private: StaticGlobalRenamer StaticRenamer; ModuleAdderFtor ModuleAdder; SourceModulesList SourceModules; - std::vector<BaseLayerModuleSetHandleT> BaseLayerHandles; + std::vector<BaseLayerModuleHandleT> BaseLayerHandles; }; - typedef std::list<LogicalDylib> LogicalDylibList; + using LogicalDylibList = std::list<LogicalDylib>; public: - /// @brief Handle to a set of loaded modules. - typedef typename LogicalDylibList::iterator ModuleSetHandleT; + /// @brief Handle to loaded module. + using ModuleHandleT = typename LogicalDylibList::iterator; /// @brief Module partitioning functor. - typedef std::function<std::set<Function*>(Function&)> PartitioningFtor; + using PartitioningFtor = std::function<std::set<Function*>(Function&)>; /// @brief Builder for IndirectStubsManagers. - typedef std::function<std::unique_ptr<IndirectStubsMgrT>()> - IndirectStubsManagerBuilderT; + using IndirectStubsManagerBuilderT = + std::function<std::unique_ptr<IndirectStubsMgrT>()>; /// @brief Construct a compile-on-demand layer instance. CompileOnDemandLayer(BaseLayerT &BaseLayer, PartitioningFtor Partition, @@ -211,15 +218,14 @@ public: ~CompileOnDemandLayer() { while (!LogicalDylibs.empty()) - removeModuleSet(LogicalDylibs.begin()); + removeModule(LogicalDylibs.begin()); } - + /// @brief Add a module to the compile-on-demand layer. - template <typename ModuleSetT, typename MemoryManagerPtrT, - typename SymbolResolverPtrT> - ModuleSetHandleT addModuleSet(ModuleSetT Ms, - MemoryManagerPtrT MemMgr, - SymbolResolverPtrT Resolver) { + template <typename MemoryManagerPtrT, typename SymbolResolverPtrT> + ModuleHandleT addModule(std::shared_ptr<Module> M, + MemoryManagerPtrT MemMgr, + SymbolResolverPtrT Resolver) { LogicalDylibs.push_back(LogicalDylib()); auto &LD = LogicalDylibs.back(); @@ -232,23 +238,25 @@ public: LD.ModuleAdder = [&MemMgrRef](BaseLayerT &B, std::unique_ptr<Module> M, std::unique_ptr<JITSymbolResolver> R) { - std::vector<std::unique_ptr<Module>> Ms; - Ms.push_back(std::move(M)); - return B.addModuleSet(std::move(Ms), &MemMgrRef, std::move(R)); + return B.addModule(std::move(M), &MemMgrRef, std::move(R)); }; // Process each of the modules in this module set. - for (auto &M : Ms) - addLogicalModule(LogicalDylibs.back(), std::move(M)); + addLogicalModule(LogicalDylibs.back(), std::move(M)); return std::prev(LogicalDylibs.end()); } + /// @brief Add extra modules to an existing logical module. + void addExtraModule(ModuleHandleT H, std::shared_ptr<Module> M) { + addLogicalModule(*H, std::move(M)); + } + /// @brief Remove the module represented by the given handle. /// /// This will remove all modules in the layers below that were derived from /// the module represented by H. - void removeModuleSet(ModuleSetHandleT H) { + void removeModule(ModuleHandleT H) { H->removeModulesFromBaseLayer(BaseLayer); LogicalDylibs.erase(H); } @@ -270,7 +278,7 @@ public: /// @brief Get the address of a symbol provided by this layer, or some layer /// below this one. - JITSymbol findSymbolIn(ModuleSetHandleT H, const std::string &Name, + JITSymbol findSymbolIn(ModuleHandleT H, const std::string &Name, bool ExportedSymbolsOnly) { return H->findSymbol(BaseLayer, Name, ExportedSymbolsOnly); } @@ -303,7 +311,6 @@ public: private: template <typename ModulePtrT> void addLogicalModule(LogicalDylib &LD, ModulePtrT SrcMPtr) { - // Rename all static functions / globals to $static.X : // This will unique the names across all modules in the logical dylib, // simplifying symbol lookup. @@ -495,7 +502,7 @@ private: } template <typename PartitionT> - BaseLayerModuleSetHandleT + BaseLayerModuleHandleT emitPartition(LogicalDylib &LD, typename LogicalDylib::SourceModuleHandle LMId, const PartitionT &Part) { @@ -581,6 +588,7 @@ private: }; } // end namespace orc + } // end namespace llvm #endif // LLVM_EXECUTIONENGINE_ORC_COMPILEONDEMANDLAYER_H diff --git a/include/llvm/ExecutionEngine/Orc/CompileUtils.h b/include/llvm/ExecutionEngine/Orc/CompileUtils.h index ce0864fbd9c9a..b9f7d6accc308 100644 --- a/include/llvm/ExecutionEngine/Orc/CompileUtils.h +++ b/include/llvm/ExecutionEngine/Orc/CompileUtils.h @@ -1,4 +1,4 @@ -//===-- CompileUtils.h - Utilities for compiling IR in the JIT --*- C++ -*-===// +//===- CompileUtils.h - Utilities for compiling IR in the JIT ---*- C++ -*-===// // // The LLVM Compiler Infrastructure // @@ -14,24 +14,47 @@ #ifndef LLVM_EXECUTIONENGINE_ORC_COMPILEUTILS_H #define LLVM_EXECUTIONENGINE_ORC_COMPILEUTILS_H +#include "llvm/ADT/SmallVector.h" +#include "llvm/ExecutionEngine/ObjectCache.h" #include "llvm/ExecutionEngine/ObjectMemoryBuffer.h" #include "llvm/IR/LegacyPassManager.h" -#include "llvm/MC/MCContext.h" +#include "llvm/Object/Binary.h" #include "llvm/Object/ObjectFile.h" +#include "llvm/Support/Error.h" +#include "llvm/Support/ErrorHandling.h" +#include "llvm/Support/MemoryBuffer.h" +#include "llvm/Support/raw_ostream.h" #include "llvm/Target/TargetMachine.h" +#include <algorithm> +#include <memory> namespace llvm { + +class MCContext; +class Module; + namespace orc { /// @brief Simple compile functor: Takes a single IR module and returns an /// ObjectFile. class SimpleCompiler { public: + + using CompileResult = object::OwningBinary<object::ObjectFile>; + /// @brief Construct a simple compile functor with the given target. - SimpleCompiler(TargetMachine &TM) : TM(TM) {} + SimpleCompiler(TargetMachine &TM, ObjectCache *ObjCache = nullptr) + : TM(TM), ObjCache(ObjCache) {} + + /// @brief Set an ObjectCache to query before compiling. + void setObjectCache(ObjectCache *NewCache) { ObjCache = NewCache; } /// @brief Compile a Module to an ObjectFile. - object::OwningBinary<object::ObjectFile> operator()(Module &M) const { + CompileResult operator()(Module &M) { + CompileResult CachedObject = tryToLoadFromObjectCache(M); + if (CachedObject.getBinary()) + return CachedObject; + SmallVector<char, 0> ObjBufferSV; raw_svector_ostream ObjStream(ObjBufferSV); @@ -44,19 +67,47 @@ public: new ObjectMemoryBuffer(std::move(ObjBufferSV))); Expected<std::unique_ptr<object::ObjectFile>> Obj = object::ObjectFile::createObjectFile(ObjBuffer->getMemBufferRef()); - typedef object::OwningBinary<object::ObjectFile> OwningObj; - if (Obj) - return OwningObj(std::move(*Obj), std::move(ObjBuffer)); + if (Obj) { + notifyObjectCompiled(M, *ObjBuffer); + return CompileResult(std::move(*Obj), std::move(ObjBuffer)); + } // TODO: Actually report errors helpfully. consumeError(Obj.takeError()); - return OwningObj(nullptr, nullptr); + return CompileResult(nullptr, nullptr); } private: + + CompileResult tryToLoadFromObjectCache(const Module &M) { + if (!ObjCache) + return CompileResult(); + + std::unique_ptr<MemoryBuffer> ObjBuffer = ObjCache->getObject(&M); + if (!ObjBuffer) + return CompileResult(); + + Expected<std::unique_ptr<object::ObjectFile>> Obj = + object::ObjectFile::createObjectFile(ObjBuffer->getMemBufferRef()); + if (!Obj) { + // TODO: Actually report errors helpfully. + consumeError(Obj.takeError()); + return CompileResult(); + } + + return CompileResult(std::move(*Obj), std::move(ObjBuffer)); + } + + void notifyObjectCompiled(const Module &M, const MemoryBuffer &ObjBuffer) { + if (ObjCache) + ObjCache->notifyObjectCompiled(&M, ObjBuffer.getMemBufferRef()); + } + TargetMachine &TM; + ObjectCache *ObjCache = nullptr; }; -} // End namespace orc. -} // End namespace llvm. +} // end namespace orc + +} // end namespace llvm #endif // LLVM_EXECUTIONENGINE_ORC_COMPILEUTILS_H diff --git a/include/llvm/ExecutionEngine/Orc/ExecutionUtils.h b/include/llvm/ExecutionEngine/Orc/ExecutionUtils.h index 71d847c062644..bf8cca406844d 100644 --- a/include/llvm/ExecutionEngine/Orc/ExecutionUtils.h +++ b/include/llvm/ExecutionEngine/Orc/ExecutionUtils.h @@ -1,4 +1,4 @@ -//===-- ExecutionUtils.h - Utilities for executing code in Orc --*- C++ -*-===// +//===- ExecutionUtils.h - Utilities for executing code in Orc ---*- C++ -*-===// // // The LLVM Compiler Infrastructure // @@ -17,8 +17,11 @@ #include "llvm/ADT/StringMap.h" #include "llvm/ADT/iterator_range.h" #include "llvm/ExecutionEngine/JITSymbol.h" -#include "llvm/ExecutionEngine/RuntimeDyld.h" +#include <algorithm> +#include <cstdint> +#include <string> #include <vector> +#include <utility> namespace llvm { @@ -37,7 +40,6 @@ namespace orc { /// getConstructors/getDestructors functions. class CtorDtorIterator { public: - /// @brief Accessor for an element of the global_ctors/global_dtors array. /// /// This class provides a read-only view of the element with any casts on @@ -89,17 +91,16 @@ iterator_range<CtorDtorIterator> getDestructors(const Module &M); template <typename JITLayerT> class CtorDtorRunner { public: - /// @brief Construct a CtorDtorRunner for the given range using the given /// name mangling function. CtorDtorRunner(std::vector<std::string> CtorDtorNames, - typename JITLayerT::ModuleSetHandleT H) + typename JITLayerT::ModuleHandleT H) : CtorDtorNames(std::move(CtorDtorNames)), H(H) {} /// @brief Run the recorded constructors/destructors through the given JIT /// layer. bool runViaLayer(JITLayerT &JITLayer) const { - typedef void (*CtorDtorTy)(); + using CtorDtorTy = void (*)(); bool Error = false; for (const auto &CtorDtorName : CtorDtorNames) @@ -115,7 +116,7 @@ public: private: std::vector<std::string> CtorDtorNames; - typename JITLayerT::ModuleSetHandleT H; + typename JITLayerT::ModuleHandleT H; }; /// @brief Support class for static dtor execution. For hosted (in-process) JITs @@ -135,7 +136,6 @@ private: /// called. class LocalCXXRuntimeOverrides { public: - /// Create a runtime-overrides class. template <typename MangleFtorT> LocalCXXRuntimeOverrides(const MangleFtorT &Mangle) { @@ -156,7 +156,6 @@ public: void runDestructors(); private: - template <typename PtrTy> JITTargetAddress toTargetAddress(PtrTy* P) { return static_cast<JITTargetAddress>(reinterpret_cast<uintptr_t>(P)); @@ -168,15 +167,16 @@ private: StringMap<JITTargetAddress> CXXRuntimeOverrides; - typedef void (*DestructorPtr)(void*); - typedef std::pair<DestructorPtr, void*> CXXDestructorDataPair; - typedef std::vector<CXXDestructorDataPair> CXXDestructorDataPairList; + using DestructorPtr = void (*)(void *); + using CXXDestructorDataPair = std::pair<DestructorPtr, void *>; + using CXXDestructorDataPairList = std::vector<CXXDestructorDataPair>; CXXDestructorDataPairList DSOHandleOverride; static int CXAAtExitOverride(DestructorPtr Destructor, void *Arg, void *DSOHandle); }; -} // End namespace orc. -} // End namespace llvm. +} // end namespace orc + +} // end namespace llvm #endif // LLVM_EXECUTIONENGINE_ORC_EXECUTIONUTILS_H diff --git a/include/llvm/ExecutionEngine/Orc/GlobalMappingLayer.h b/include/llvm/ExecutionEngine/Orc/GlobalMappingLayer.h index 634d1480ae4c0..d582e9a33241f 100644 --- a/include/llvm/ExecutionEngine/Orc/GlobalMappingLayer.h +++ b/include/llvm/ExecutionEngine/Orc/GlobalMappingLayer.h @@ -1,4 +1,4 @@ -//===---- GlobalMappingLayer.h - Run all IR through a functor ---*- C++ -*-===// +//===- GlobalMappingLayer.h - Run all IR through a functor ------*- C++ -*-===// // // The LLVM Compiler Infrastructure // @@ -17,6 +17,7 @@ #include "llvm/ExecutionEngine/JITSymbol.h" #include <map> +#include <string> namespace llvm { namespace orc { @@ -32,7 +33,7 @@ template <typename BaseLayerT> class GlobalMappingLayer { public: /// @brief Handle to a set of added modules. - typedef typename BaseLayerT::ModuleSetHandleT ModuleSetHandleT; + using ModuleSetHandleT = typename BaseLayerT::ModuleSetHandleT; /// @brief Construct an GlobalMappingLayer with the given BaseLayer GlobalMappingLayer(BaseLayerT &BaseLayer) : BaseLayer(BaseLayer) {} @@ -102,7 +103,7 @@ private: std::map<std::string, JITTargetAddress> SymbolTable; }; -} // End namespace orc. -} // End namespace llvm. +} // end namespace orc +} // end namespace llvm #endif // LLVM_EXECUTIONENGINE_ORC_GLOBALMAPPINGLAYER_H diff --git a/include/llvm/ExecutionEngine/Orc/IRCompileLayer.h b/include/llvm/ExecutionEngine/Orc/IRCompileLayer.h index f81d054440fc9..99ccd4d221a5e 100644 --- a/include/llvm/ExecutionEngine/Orc/IRCompileLayer.h +++ b/include/llvm/ExecutionEngine/Orc/IRCompileLayer.h @@ -1,4 +1,4 @@ -//===------ IRCompileLayer.h -- Eagerly compile IR for JIT ------*- C++ -*-===// +//===- IRCompileLayer.h -- Eagerly compile IR for JIT -----------*- C++ -*-===// // // The LLVM Compiler Infrastructure // @@ -14,79 +14,54 @@ #ifndef LLVM_EXECUTIONENGINE_ORC_IRCOMPILELAYER_H #define LLVM_EXECUTIONENGINE_ORC_IRCOMPILELAYER_H +#include "llvm/ADT/STLExtras.h" #include "llvm/ExecutionEngine/JITSymbol.h" -#include "llvm/ExecutionEngine/ObjectCache.h" -#include "llvm/Object/ObjectFile.h" +#include "llvm/Support/Error.h" #include <memory> +#include <string> namespace llvm { + +class Module; + namespace orc { /// @brief Eager IR compiling layer. /// -/// This layer accepts sets of LLVM IR Modules (via addModuleSet). It -/// immediately compiles each IR module to an object file (each IR Module is -/// compiled separately). The resulting set of object files is then added to -/// the layer below, which must implement the object layer concept. -template <typename BaseLayerT> class IRCompileLayer { +/// This layer immediately compiles each IR module added via addModule to an +/// object file and adds this module file to the layer below, which must +/// implement the object layer concept. +template <typename BaseLayerT, typename CompileFtor> +class IRCompileLayer { public: - typedef std::function<object::OwningBinary<object::ObjectFile>(Module &)> - CompileFtor; - -private: - typedef typename BaseLayerT::ObjSetHandleT ObjSetHandleT; -public: - /// @brief Handle to a set of compiled modules. - typedef ObjSetHandleT ModuleSetHandleT; + /// @brief Handle to a compiled module. + using ModuleHandleT = typename BaseLayerT::ObjHandleT; /// @brief Construct an IRCompileLayer with the given BaseLayer, which must /// implement the ObjectLayer concept. IRCompileLayer(BaseLayerT &BaseLayer, CompileFtor Compile) - : BaseLayer(BaseLayer), Compile(std::move(Compile)), ObjCache(nullptr) {} + : BaseLayer(BaseLayer), Compile(std::move(Compile)) {} - /// @brief Set an ObjectCache to query before compiling. - void setObjectCache(ObjectCache *NewCache) { ObjCache = NewCache; } + /// @brief Get a reference to the compiler functor. + CompileFtor& getCompiler() { return Compile; } - /// @brief Compile each module in the given module set, then add the resulting - /// set of objects to the base layer along with the memory manager and - /// symbol resolver. + /// @brief Compile the module, and add the resulting object to the base layer + /// along with the given memory manager and symbol resolver. /// - /// @return A handle for the added modules. - template <typename ModuleSetT, typename MemoryManagerPtrT, - typename SymbolResolverPtrT> - ModuleSetHandleT addModuleSet(ModuleSetT Ms, - MemoryManagerPtrT MemMgr, - SymbolResolverPtrT Resolver) { - std::vector<std::unique_ptr<object::OwningBinary<object::ObjectFile>>> - Objects; - - for (const auto &M : Ms) { - auto Object = - llvm::make_unique<object::OwningBinary<object::ObjectFile>>(); - - if (ObjCache) - *Object = tryToLoadFromObjectCache(*M); - - if (!Object->getBinary()) { - *Object = Compile(*M); - if (ObjCache) - ObjCache->notifyObjectCompiled(&*M, - Object->getBinary()->getMemoryBufferRef()); - } - - Objects.push_back(std::move(Object)); - } - - ModuleSetHandleT H = - BaseLayer.addObjectSet(std::move(Objects), std::move(MemMgr), - std::move(Resolver)); - - return H; + /// @return A handle for the added module. + template <typename MemoryManagerPtrT, typename SymbolResolverPtrT> + ModuleHandleT addModule(std::shared_ptr<Module> M, + MemoryManagerPtrT MemMgr, + SymbolResolverPtrT Resolver) { + using CompileResult = decltype(Compile(*M)); + auto Obj = std::make_shared<CompileResult>(Compile(*M)); + return BaseLayer.addObject(std::move(Obj), std::move(MemMgr), + std::move(Resolver)); } - /// @brief Remove the module set associated with the handle H. - void removeModuleSet(ModuleSetHandleT H) { BaseLayer.removeObjectSet(H); } + /// @brief Remove the module associated with the handle H. + void removeModule(ModuleHandleT H) { BaseLayer.removeObject(H); } /// @brief Search for the given named symbol. /// @param Name The name of the symbol to search for. @@ -96,51 +71,33 @@ public: return BaseLayer.findSymbol(Name, ExportedSymbolsOnly); } - /// @brief Get the address of the given symbol in the context of the set of - /// compiled modules represented by the handle H. This call is - /// forwarded to the base layer's implementation. - /// @param H The handle for the module set to search in. + /// @brief Get the address of the given symbol in compiled module represented + /// by the handle H. This call is forwarded to the base layer's + /// implementation. + /// @param H The handle for the module to search in. /// @param Name The name of the symbol to search for. /// @param ExportedSymbolsOnly If true, search only for exported symbols. /// @return A handle for the given named symbol, if it is found in the - /// given module set. - JITSymbol findSymbolIn(ModuleSetHandleT H, const std::string &Name, + /// given module. + JITSymbol findSymbolIn(ModuleHandleT H, const std::string &Name, bool ExportedSymbolsOnly) { return BaseLayer.findSymbolIn(H, Name, ExportedSymbolsOnly); } - /// @brief Immediately emit and finalize the moduleOB set represented by the - /// given handle. - /// @param H Handle for module set to emit/finalize. - void emitAndFinalize(ModuleSetHandleT H) { + /// @brief Immediately emit and finalize the module represented by the given + /// handle. + /// @param H Handle for module to emit/finalize. + void emitAndFinalize(ModuleHandleT H) { BaseLayer.emitAndFinalize(H); } private: - object::OwningBinary<object::ObjectFile> - tryToLoadFromObjectCache(const Module &M) { - std::unique_ptr<MemoryBuffer> ObjBuffer = ObjCache->getObject(&M); - if (!ObjBuffer) - return object::OwningBinary<object::ObjectFile>(); - - Expected<std::unique_ptr<object::ObjectFile>> Obj = - object::ObjectFile::createObjectFile(ObjBuffer->getMemBufferRef()); - if (!Obj) { - // TODO: Actually report errors helpfully. - consumeError(Obj.takeError()); - return object::OwningBinary<object::ObjectFile>(); - } - - return object::OwningBinary<object::ObjectFile>(std::move(*Obj), - std::move(ObjBuffer)); - } - BaseLayerT &BaseLayer; CompileFtor Compile; - ObjectCache *ObjCache; }; -} // End namespace orc. -} // End namespace llvm. +} // end namespace orc + +} // end namespace llvm #endif // LLVM_EXECUTIONENGINE_ORC_IRCOMPILINGLAYER_H diff --git a/include/llvm/ExecutionEngine/Orc/IRTransformLayer.h b/include/llvm/ExecutionEngine/Orc/IRTransformLayer.h index c67297b111b93..8e9be6b6f4fe4 100644 --- a/include/llvm/ExecutionEngine/Orc/IRTransformLayer.h +++ b/include/llvm/ExecutionEngine/Orc/IRTransformLayer.h @@ -1,4 +1,4 @@ -//===----- IRTransformLayer.h - Run all IR through a functor ----*- C++ -*-===// +//===- IRTransformLayer.h - Run all IR through a functor --------*- C++ -*-===// // // The LLVM Compiler Infrastructure // @@ -15,46 +15,41 @@ #define LLVM_EXECUTIONENGINE_ORC_IRTRANSFORMLAYER_H #include "llvm/ExecutionEngine/JITSymbol.h" +#include <string> namespace llvm { namespace orc { /// @brief IR mutating layer. /// -/// This layer accepts sets of LLVM IR Modules (via addModuleSet). It -/// immediately applies the user supplied functor to each module, then adds -/// the set of transformed modules to the layer below. +/// This layer applies a user supplied transform to each module that is added, +/// then adds the transformed module to the layer below. template <typename BaseLayerT, typename TransformFtor> class IRTransformLayer { public: + /// @brief Handle to a set of added modules. - typedef typename BaseLayerT::ModuleSetHandleT ModuleSetHandleT; + using ModuleHandleT = typename BaseLayerT::ModuleHandleT; /// @brief Construct an IRTransformLayer with the given BaseLayer IRTransformLayer(BaseLayerT &BaseLayer, TransformFtor Transform = TransformFtor()) : BaseLayer(BaseLayer), Transform(std::move(Transform)) {} - /// @brief Apply the transform functor to each module in the module set, then - /// add the resulting set of modules to the base layer, along with the - /// memory manager and symbol resolver. + /// @brief Apply the transform functor to the module, then add the module to + /// the layer below, along with the memory manager and symbol resolver. /// /// @return A handle for the added modules. - template <typename ModuleSetT, typename MemoryManagerPtrT, - typename SymbolResolverPtrT> - ModuleSetHandleT addModuleSet(ModuleSetT Ms, - MemoryManagerPtrT MemMgr, - SymbolResolverPtrT Resolver) { - - for (auto I = Ms.begin(), E = Ms.end(); I != E; ++I) - *I = Transform(std::move(*I)); - - return BaseLayer.addModuleSet(std::move(Ms), std::move(MemMgr), - std::move(Resolver)); + template <typename MemoryManagerPtrT, typename SymbolResolverPtrT> + ModuleHandleT addModule(std::shared_ptr<Module> M, + MemoryManagerPtrT MemMgr, + SymbolResolverPtrT Resolver) { + return BaseLayer.addModule(Transform(std::move(M)), std::move(MemMgr), + std::move(Resolver)); } - /// @brief Remove the module set associated with the handle H. - void removeModuleSet(ModuleSetHandleT H) { BaseLayer.removeModuleSet(H); } + /// @brief Remove the module associated with the handle H. + void removeModule(ModuleHandleT H) { BaseLayer.removeModule(H); } /// @brief Search for the given named symbol. /// @param Name The name of the symbol to search for. @@ -64,23 +59,23 @@ public: return BaseLayer.findSymbol(Name, ExportedSymbolsOnly); } - /// @brief Get the address of the given symbol in the context of the set of - /// modules represented by the handle H. This call is forwarded to the - /// base layer's implementation. - /// @param H The handle for the module set to search in. + /// @brief Get the address of the given symbol in the context of the module + /// represented by the handle H. This call is forwarded to the base + /// layer's implementation. + /// @param H The handle for the module to search in. /// @param Name The name of the symbol to search for. /// @param ExportedSymbolsOnly If true, search only for exported symbols. /// @return A handle for the given named symbol, if it is found in the - /// given module set. - JITSymbol findSymbolIn(ModuleSetHandleT H, const std::string &Name, + /// given module. + JITSymbol findSymbolIn(ModuleHandleT H, const std::string &Name, bool ExportedSymbolsOnly) { return BaseLayer.findSymbolIn(H, Name, ExportedSymbolsOnly); } - /// @brief Immediately emit and finalize the module set represented by the - /// given handle. - /// @param H Handle for module set to emit/finalize. - void emitAndFinalize(ModuleSetHandleT H) { + /// @brief Immediately emit and finalize the module represented by the given + /// handle. + /// @param H Handle for module to emit/finalize. + void emitAndFinalize(ModuleHandleT H) { BaseLayer.emitAndFinalize(H); } @@ -95,7 +90,7 @@ private: TransformFtor Transform; }; -} // End namespace orc. -} // End namespace llvm. +} // end namespace orc +} // end namespace llvm #endif // LLVM_EXECUTIONENGINE_ORC_IRTRANSFORMLAYER_H diff --git a/include/llvm/ExecutionEngine/Orc/IndirectionUtils.h b/include/llvm/ExecutionEngine/Orc/IndirectionUtils.h index 07bbd921dad62..e038093d7628c 100644 --- a/include/llvm/ExecutionEngine/Orc/IndirectionUtils.h +++ b/include/llvm/ExecutionEngine/Orc/IndirectionUtils.h @@ -1,4 +1,4 @@ -//===-- IndirectionUtils.h - Utilities for adding indirections --*- C++ -*-===// +//===- IndirectionUtils.h - Utilities for adding indirections ---*- C++ -*-===// // // The LLVM Compiler Infrastructure // @@ -18,9 +18,6 @@ #include "llvm/ADT/StringRef.h" #include "llvm/ADT/Twine.h" #include "llvm/ExecutionEngine/JITSymbol.h" -#include "llvm/IR/IRBuilder.h" -#include "llvm/IR/Mangler.h" -#include "llvm/IR/Module.h" #include "llvm/Support/Error.h" #include "llvm/Support/Memory.h" #include "llvm/Support/Process.h" @@ -36,12 +33,23 @@ #include <vector> namespace llvm { + +class Constant; +class Function; +class FunctionType; +class GlobalAlias; +class GlobalVariable; +class Module; +class PointerType; +class Triple; +class Value; + namespace orc { /// @brief Target-independent base class for compile callback management. class JITCompileCallbackManager { public: - typedef std::function<JITTargetAddress()> CompileFtor; + using CompileFtor = std::function<JITTargetAddress()>; /// @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 @@ -125,7 +133,7 @@ public: protected: JITTargetAddress ErrorHandlerAddress; - typedef std::map<JITTargetAddress, CompileFtor> TrampolineMapT; + using TrampolineMapT = std::map<JITTargetAddress, CompileFtor>; TrampolineMapT ActiveTrampolines; std::vector<JITTargetAddress> AvailableTrampolines; @@ -155,7 +163,6 @@ public: /// process to be used if a compile callback fails. LocalJITCompileCallbackManager(JITTargetAddress ErrorHandlerAddress) : JITCompileCallbackManager(ErrorHandlerAddress) { - /// Set up the resolver block. std::error_code EC; ResolverBlock = sys::OwningMemoryBlock(sys::Memory::allocateMappedMemory( @@ -220,7 +227,7 @@ private: class IndirectStubsManager { public: /// @brief Map type for initializing the manager. See init. - typedef StringMap<std::pair<JITTargetAddress, JITSymbolFlags>> StubInitsMap; + using StubInitsMap = StringMap<std::pair<JITTargetAddress, JITSymbolFlags>>; virtual ~IndirectStubsManager() = default; @@ -336,7 +343,7 @@ private: } std::vector<typename TargetT::IndirectStubsInfo> IndirectStubsInfos; - typedef std::pair<uint16_t, uint16_t> StubKey; + using StubKey = std::pair<uint16_t, uint16_t>; std::vector<StubKey> FreeStubs; StringMap<std::pair<StubKey, JITSymbolFlags>> StubIndexes; }; @@ -432,6 +439,7 @@ void cloneModuleFlagsMetadata(Module &Dst, const Module &Src, ValueToValueMapTy &VMap); } // end namespace orc + } // end namespace llvm #endif // LLVM_EXECUTIONENGINE_ORC_INDIRECTIONUTILS_H diff --git a/include/llvm/ExecutionEngine/Orc/LambdaResolver.h b/include/llvm/ExecutionEngine/Orc/LambdaResolver.h index cbe2a80edf1c1..6868640d38e80 100644 --- a/include/llvm/ExecutionEngine/Orc/LambdaResolver.h +++ b/include/llvm/ExecutionEngine/Orc/LambdaResolver.h @@ -1,4 +1,4 @@ -//===-- LambdaResolverMM - Redirect symbol lookup via a functor -*- C++ -*-===// +//===- LambdaResolverMM - Redirect symbol lookup via a functor --*- C++ -*-===// // // The LLVM Compiler Infrastructure // @@ -16,7 +16,7 @@ #define LLVM_EXECUTIONENGINE_ORC_LAMBDARESOLVER_H #include "llvm/ADT/STLExtras.h" -#include "llvm/ExecutionEngine/RuntimeDyld.h" +#include "llvm/ExecutionEngine/JITSymbol.h" #include <memory> namespace llvm { @@ -25,7 +25,6 @@ namespace orc { template <typename DylibLookupFtorT, typename ExternalLookupFtorT> class LambdaResolver : public JITSymbolResolver { public: - LambdaResolver(DylibLookupFtorT DylibLookupFtor, ExternalLookupFtorT ExternalLookupFtor) : DylibLookupFtor(DylibLookupFtor), @@ -49,12 +48,12 @@ template <typename DylibLookupFtorT, std::unique_ptr<LambdaResolver<DylibLookupFtorT, ExternalLookupFtorT>> createLambdaResolver(DylibLookupFtorT DylibLookupFtor, ExternalLookupFtorT ExternalLookupFtor) { - typedef LambdaResolver<DylibLookupFtorT, ExternalLookupFtorT> LR; + using LR = LambdaResolver<DylibLookupFtorT, ExternalLookupFtorT>; return make_unique<LR>(std::move(DylibLookupFtor), std::move(ExternalLookupFtor)); } -} // End namespace orc. -} // End namespace llvm. +} // end namespace orc +} // end namespace llvm #endif // LLVM_EXECUTIONENGINE_ORC_LAMBDARESOLVER_H diff --git a/include/llvm/ExecutionEngine/Orc/LazyEmittingLayer.h b/include/llvm/ExecutionEngine/Orc/LazyEmittingLayer.h index 53d4c0cfe5d4d..38769aac12afe 100644 --- a/include/llvm/ExecutionEngine/Orc/LazyEmittingLayer.h +++ b/include/llvm/ExecutionEngine/Orc/LazyEmittingLayer.h @@ -34,19 +34,20 @@ namespace orc { /// @brief Lazy-emitting IR layer. /// -/// This layer accepts sets of LLVM IR Modules (via addModuleSet), but does -/// not immediately emit them the layer below. Instead, emissing to the base -/// layer is deferred until the first time the client requests the address -/// (via JITSymbol::getAddress) for a symbol contained in this layer. +/// This layer accepts LLVM IR Modules (via addModule), but does not +/// immediately emit them the layer below. Instead, emissing to the base layer +/// is deferred until the first time the client requests the address (via +/// JITSymbol::getAddress) for a symbol contained in this layer. template <typename BaseLayerT> class LazyEmittingLayer { public: - typedef typename BaseLayerT::ModuleSetHandleT BaseLayerHandleT; + + using BaseLayerHandleT = typename BaseLayerT::ModuleHandleT; private: - class EmissionDeferredSet { + class EmissionDeferredModule { public: - EmissionDeferredSet() = default; - virtual ~EmissionDeferredSet() = default; + EmissionDeferredModule() = default; + virtual ~EmissionDeferredModule() = default; JITSymbol find(StringRef Name, bool ExportedSymbolsOnly, BaseLayerT &B) { switch (EmitState) { @@ -84,9 +85,9 @@ private: llvm_unreachable("Invalid emit-state."); } - void removeModulesFromBaseLayer(BaseLayerT &BaseLayer) { + void removeModuleFromBaseLayer(BaseLayerT &BaseLayer) { if (EmitState != NotEmitted) - BaseLayer.removeModuleSet(Handle); + BaseLayer.removeModule(Handle); } void emitAndFinalize(BaseLayerT &BaseLayer) { @@ -100,10 +101,9 @@ private: BaseLayer.emitAndFinalize(Handle); } - template <typename ModuleSetT, typename MemoryManagerPtrT, - typename SymbolResolverPtrT> - static std::unique_ptr<EmissionDeferredSet> - create(BaseLayerT &B, ModuleSetT Ms, MemoryManagerPtrT MemMgr, + template <typename MemoryManagerPtrT, typename SymbolResolverPtrT> + static std::unique_ptr<EmissionDeferredModule> + create(BaseLayerT &B, std::shared_ptr<Module> M, MemoryManagerPtrT MemMgr, SymbolResolverPtrT Resolver); protected: @@ -116,14 +116,13 @@ private: BaseLayerHandleT Handle; }; - template <typename ModuleSetT, typename MemoryManagerPtrT, - typename SymbolResolverPtrT> - class EmissionDeferredSetImpl : public EmissionDeferredSet { + template <typename MemoryManagerPtrT, typename SymbolResolverPtrT> + class EmissionDeferredModuleImpl : public EmissionDeferredModule { public: - EmissionDeferredSetImpl(ModuleSetT Ms, - MemoryManagerPtrT MemMgr, - SymbolResolverPtrT Resolver) - : Ms(std::move(Ms)), MemMgr(std::move(MemMgr)), + EmissionDeferredModuleImpl(std::shared_ptr<Module> M, + MemoryManagerPtrT MemMgr, + SymbolResolverPtrT Resolver) + : M(std::move(M)), MemMgr(std::move(MemMgr)), Resolver(std::move(Resolver)) {} protected: @@ -154,8 +153,8 @@ private: // We don't need the mangled names set any more: Once we've emitted this // to the base layer we'll just look for symbols there. MangledSymbols.reset(); - return BaseLayer.addModuleSet(std::move(Ms), std::move(MemMgr), - std::move(Resolver)); + return BaseLayer.addModule(std::move(M), std::move(MemMgr), + std::move(Resolver)); } private: @@ -197,56 +196,54 @@ private: auto Symbols = llvm::make_unique<StringMap<const GlobalValue*>>(); - for (const auto &M : Ms) { - Mangler Mang; + Mangler Mang; - for (const auto &GO : M->global_objects()) + for (const auto &GO : M->global_objects()) if (auto GV = addGlobalValue(*Symbols, GO, Mang, SearchName, ExportedSymbolsOnly)) return GV; - } MangledSymbols = std::move(Symbols); return nullptr; } - ModuleSetT Ms; + std::shared_ptr<Module> M; MemoryManagerPtrT MemMgr; SymbolResolverPtrT Resolver; mutable std::unique_ptr<StringMap<const GlobalValue*>> MangledSymbols; }; - typedef std::list<std::unique_ptr<EmissionDeferredSet>> ModuleSetListT; + using ModuleListT = std::list<std::unique_ptr<EmissionDeferredModule>>; BaseLayerT &BaseLayer; - ModuleSetListT ModuleSetList; + ModuleListT ModuleList; public: - /// @brief Handle to a set of loaded modules. - typedef typename ModuleSetListT::iterator ModuleSetHandleT; + /// @brief Handle to a loaded module. + using ModuleHandleT = typename ModuleListT::iterator; /// @brief Construct a lazy emitting layer. LazyEmittingLayer(BaseLayerT &BaseLayer) : BaseLayer(BaseLayer) {} - /// @brief Add the given set of modules to the lazy emitting layer. - template <typename ModuleSetT, typename MemoryManagerPtrT, - typename SymbolResolverPtrT> - ModuleSetHandleT addModuleSet(ModuleSetT Ms, - MemoryManagerPtrT MemMgr, - SymbolResolverPtrT Resolver) { - return ModuleSetList.insert( - ModuleSetList.end(), - EmissionDeferredSet::create(BaseLayer, std::move(Ms), std::move(MemMgr), - std::move(Resolver))); + /// @brief Add the given module to the lazy emitting layer. + template <typename MemoryManagerPtrT, typename SymbolResolverPtrT> + ModuleHandleT addModule(std::shared_ptr<Module> M, + MemoryManagerPtrT MemMgr, + SymbolResolverPtrT Resolver) { + return ModuleList.insert( + ModuleList.end(), + EmissionDeferredModule::create(BaseLayer, std::move(M), + std::move(MemMgr), + std::move(Resolver))); } - /// @brief Remove the module set represented by the given handle. + /// @brief Remove the module represented by the given handle. /// - /// This method will free the memory associated with the given module set, - /// both in this layer, and the base layer. - void removeModuleSet(ModuleSetHandleT H) { - (*H)->removeModulesFromBaseLayer(BaseLayer); - ModuleSetList.erase(H); + /// This method will free the memory associated with the given module, both + /// in this layer, and the base layer. + void removeModule(ModuleHandleT H) { + (*H)->removeModuleFromBaseLayer(BaseLayer); + ModuleList.erase(H); } /// @brief Search for the given named symbol. @@ -258,42 +255,40 @@ public: if (auto Symbol = BaseLayer.findSymbol(Name, ExportedSymbolsOnly)) return Symbol; - // If not found then search the deferred sets. If any of these contain a + // If not found then search the deferred modules. If any of these contain a // definition of 'Name' then they will return a JITSymbol that will emit // the corresponding module when the symbol address is requested. - for (auto &DeferredSet : ModuleSetList) - if (auto Symbol = DeferredSet->find(Name, ExportedSymbolsOnly, BaseLayer)) + for (auto &DeferredMod : ModuleList) + if (auto Symbol = DeferredMod->find(Name, ExportedSymbolsOnly, BaseLayer)) return Symbol; // If no definition found anywhere return a null symbol. return nullptr; } - /// @brief Get the address of the given symbol in the context of the set of + /// @brief Get the address of the given symbol in the context of the of /// compiled modules represented by the handle H. - JITSymbol findSymbolIn(ModuleSetHandleT H, const std::string &Name, + JITSymbol findSymbolIn(ModuleHandleT H, const std::string &Name, bool ExportedSymbolsOnly) { return (*H)->find(Name, ExportedSymbolsOnly, BaseLayer); } - /// @brief Immediately emit and finalize the moduleOB set represented by the - /// given handle. - /// @param H Handle for module set to emit/finalize. - void emitAndFinalize(ModuleSetHandleT H) { + /// @brief Immediately emit and finalize the module represented by the given + /// handle. + /// @param H Handle for module to emit/finalize. + void emitAndFinalize(ModuleHandleT H) { (*H)->emitAndFinalize(BaseLayer); } }; template <typename BaseLayerT> -template <typename ModuleSetT, typename MemoryManagerPtrT, - typename SymbolResolverPtrT> -std::unique_ptr<typename LazyEmittingLayer<BaseLayerT>::EmissionDeferredSet> -LazyEmittingLayer<BaseLayerT>::EmissionDeferredSet::create( - BaseLayerT &B, ModuleSetT Ms, MemoryManagerPtrT MemMgr, +template <typename MemoryManagerPtrT, typename SymbolResolverPtrT> +std::unique_ptr<typename LazyEmittingLayer<BaseLayerT>::EmissionDeferredModule> +LazyEmittingLayer<BaseLayerT>::EmissionDeferredModule::create( + BaseLayerT &B, std::shared_ptr<Module> M, MemoryManagerPtrT MemMgr, SymbolResolverPtrT Resolver) { - typedef EmissionDeferredSetImpl<ModuleSetT, MemoryManagerPtrT, SymbolResolverPtrT> - EDS; - return llvm::make_unique<EDS>(std::move(Ms), std::move(MemMgr), + using EDS = EmissionDeferredModuleImpl<MemoryManagerPtrT, SymbolResolverPtrT>; + return llvm::make_unique<EDS>(std::move(M), std::move(MemMgr), std::move(Resolver)); } diff --git a/include/llvm/ExecutionEngine/Orc/ObjectTransformLayer.h b/include/llvm/ExecutionEngine/Orc/ObjectTransformLayer.h index 173c106cd3ec9..c41c1233c0d96 100644 --- a/include/llvm/ExecutionEngine/Orc/ObjectTransformLayer.h +++ b/include/llvm/ExecutionEngine/Orc/ObjectTransformLayer.h @@ -15,20 +15,22 @@ #define LLVM_EXECUTIONENGINE_ORC_OBJECTTRANSFORMLAYER_H #include "llvm/ExecutionEngine/JITSymbol.h" +#include <algorithm> +#include <string> namespace llvm { namespace orc { /// @brief Object mutating layer. /// -/// This layer accepts sets of ObjectFiles (via addObjectSet). It +/// This layer accepts sets of ObjectFiles (via addObject). It /// immediately applies the user supplied functor to each object, then adds /// the set of transformed objects to the layer below. template <typename BaseLayerT, typename TransformFtor> class ObjectTransformLayer { public: /// @brief Handle to a set of added objects. - typedef typename BaseLayerT::ObjSetHandleT ObjSetHandleT; + using ObjHandleT = typename BaseLayerT::ObjHandleT; /// @brief Construct an ObjectTransformLayer with the given BaseLayer ObjectTransformLayer(BaseLayerT &BaseLayer, @@ -40,20 +42,16 @@ public: /// memory manager and symbol resolver. /// /// @return A handle for the added objects. - template <typename ObjSetT, typename MemoryManagerPtrT, + template <typename ObjPtrT, typename MemoryManagerPtrT, typename SymbolResolverPtrT> - ObjSetHandleT addObjectSet(ObjSetT Objects, MemoryManagerPtrT MemMgr, - SymbolResolverPtrT Resolver) { - - for (auto I = Objects.begin(), E = Objects.end(); I != E; ++I) - *I = Transform(std::move(*I)); - - return BaseLayer.addObjectSet(std::move(Objects), std::move(MemMgr), - std::move(Resolver)); + ObjHandleT addObject(ObjPtrT Obj, MemoryManagerPtrT MemMgr, + SymbolResolverPtrT Resolver) { + return BaseLayer.addObject(Transform(std::move(Obj)), std::move(MemMgr), + std::move(Resolver)); } /// @brief Remove the object set associated with the handle H. - void removeObjectSet(ObjSetHandleT H) { BaseLayer.removeObjectSet(H); } + void removeObject(ObjHandleT H) { BaseLayer.removeObject(H); } /// @brief Search for the given named symbol. /// @param Name The name of the symbol to search for. @@ -71,7 +69,7 @@ public: /// @param ExportedSymbolsOnly If true, search only for exported symbols. /// @return A handle for the given named symbol, if it is found in the /// given object set. - JITSymbol findSymbolIn(ObjSetHandleT H, const std::string &Name, + JITSymbol findSymbolIn(ObjHandleT H, const std::string &Name, bool ExportedSymbolsOnly) { return BaseLayer.findSymbolIn(H, Name, ExportedSymbolsOnly); } @@ -79,10 +77,10 @@ public: /// @brief Immediately emit and finalize the object set represented by the /// given handle. /// @param H Handle for object set to emit/finalize. - void emitAndFinalize(ObjSetHandleT H) { BaseLayer.emitAndFinalize(H); } + void emitAndFinalize(ObjHandleT H) { BaseLayer.emitAndFinalize(H); } /// @brief Map section addresses for the objects associated with the handle H. - void mapSectionAddress(ObjSetHandleT H, const void *LocalAddress, + void mapSectionAddress(ObjHandleT H, const void *LocalAddress, JITTargetAddress TargetAddr) { BaseLayer.mapSectionAddress(H, LocalAddress, TargetAddr); } @@ -98,7 +96,7 @@ private: TransformFtor Transform; }; -} // End namespace orc. -} // End namespace llvm. +} // end namespace orc +} // end namespace llvm #endif // LLVM_EXECUTIONENGINE_ORC_OBJECTTRANSFORMLAYER_H diff --git a/include/llvm/ExecutionEngine/Orc/OrcABISupport.h b/include/llvm/ExecutionEngine/Orc/OrcABISupport.h index fa236b0de88a3..e1b55649b9f2c 100644 --- a/include/llvm/ExecutionEngine/Orc/OrcABISupport.h +++ b/include/llvm/ExecutionEngine/Orc/OrcABISupport.h @@ -1,4 +1,4 @@ -//===-------------- OrcABISupport.h - ABI support code ---------*- C++ -*-===// +//===- OrcABISupport.h - ABI support code -----------------------*- C++ -*-===// // // The LLVM Compiler Infrastructure // @@ -18,9 +18,12 @@ #ifndef LLVM_EXECUTIONENGINE_ORC_ORCABISUPPORT_H #define LLVM_EXECUTIONENGINE_ORC_ORCABISUPPORT_H -#include "IndirectionUtils.h" +#include "llvm/ExecutionEngine/JITSymbol.h" +#include "llvm/Support/Error.h" +#include "llvm/Support/ErrorHandling.h" #include "llvm/Support/Memory.h" -#include "llvm/Support/Process.h" +#include <algorithm> +#include <cstdint> namespace llvm { namespace orc { @@ -37,8 +40,8 @@ public: static const unsigned TrampolineSize = 1; static const unsigned ResolverCodeSize = 1; - typedef JITTargetAddress (*JITReentryFn)(void *CallbackMgr, - void *TrampolineId); + using JITReentryFn = JITTargetAddress (*)(void *CallbackMgr, + void *TrampolineId); static void writeResolverCode(uint8_t *ResolveMem, JITReentryFn Reentry, void *CallbackMgr) { @@ -55,6 +58,7 @@ public: class IndirectStubsInfo { public: const static unsigned StubSize = 1; + unsigned getNumStubs() const { llvm_unreachable("Not supported"); } void *getStub(unsigned Idx) const { llvm_unreachable("Not supported"); } void **getPtr(unsigned Idx) const { llvm_unreachable("Not supported"); } @@ -73,13 +77,14 @@ template <unsigned StubSizeVal> class GenericIndirectStubsInfo { public: const static unsigned StubSize = StubSizeVal; - GenericIndirectStubsInfo() : NumStubs(0) {} + GenericIndirectStubsInfo() = default; GenericIndirectStubsInfo(unsigned NumStubs, sys::OwningMemoryBlock StubsMem) : NumStubs(NumStubs), StubsMem(std::move(StubsMem)) {} GenericIndirectStubsInfo(GenericIndirectStubsInfo &&Other) : NumStubs(Other.NumStubs), StubsMem(std::move(Other.StubsMem)) { Other.NumStubs = 0; } + GenericIndirectStubsInfo &operator=(GenericIndirectStubsInfo &&Other) { NumStubs = Other.NumStubs; Other.NumStubs = 0; @@ -104,7 +109,7 @@ public: } private: - unsigned NumStubs; + unsigned NumStubs = 0; sys::OwningMemoryBlock StubsMem; }; @@ -114,10 +119,10 @@ public: static const unsigned TrampolineSize = 12; static const unsigned ResolverCodeSize = 0x120; - typedef GenericIndirectStubsInfo<8> IndirectStubsInfo; + using IndirectStubsInfo = GenericIndirectStubsInfo<8>; - typedef JITTargetAddress (*JITReentryFn)(void *CallbackMgr, - void *TrampolineId); + using JITReentryFn = JITTargetAddress (*)(void *CallbackMgr, + void *TrampolineId); /// @brief Write the resolver code into the given memory. The user is be /// responsible for allocating the memory and setting permissions. @@ -148,7 +153,7 @@ public: static const unsigned PointerSize = 8; static const unsigned TrampolineSize = 8; - typedef GenericIndirectStubsInfo<8> IndirectStubsInfo; + using IndirectStubsInfo = GenericIndirectStubsInfo<8>; /// @brief Write the requsted number of trampolines into the given memory, /// which must be big enough to hold 1 pointer, plus NumTrampolines @@ -172,8 +177,9 @@ public: class OrcX86_64_SysV : public OrcX86_64_Base { public: static const unsigned ResolverCodeSize = 0x6C; - typedef JITTargetAddress (*JITReentryFn)(void *CallbackMgr, - void *TrampolineId); + + using JITReentryFn = JITTargetAddress (*)(void *CallbackMgr, + void *TrampolineId); /// @brief Write the resolver code into the given memory. The user is be /// responsible for allocating the memory and setting permissions. @@ -187,8 +193,9 @@ public: class OrcX86_64_Win32 : public OrcX86_64_Base { public: static const unsigned ResolverCodeSize = 0x74; - typedef JITTargetAddress (*JITReentryFn)(void *CallbackMgr, - void *TrampolineId); + + using JITReentryFn = JITTargetAddress (*)(void *CallbackMgr, + void *TrampolineId); /// @brief Write the resolver code into the given memory. The user is be /// responsible for allocating the memory and setting permissions. @@ -205,10 +212,10 @@ public: static const unsigned TrampolineSize = 8; static const unsigned ResolverCodeSize = 0x4a; - typedef GenericIndirectStubsInfo<8> IndirectStubsInfo; + using IndirectStubsInfo = GenericIndirectStubsInfo<8>; - typedef JITTargetAddress (*JITReentryFn)(void *CallbackMgr, - void *TrampolineId); + using JITReentryFn = JITTargetAddress (*)(void *CallbackMgr, + void *TrampolineId); /// @brief Write the resolver code into the given memory. The user is be /// responsible for allocating the memory and setting permissions. @@ -231,7 +238,7 @@ public: unsigned MinStubs, void *InitialPtrVal); }; -} // End namespace orc. -} // End namespace llvm. +} // end namespace orc +} // end namespace llvm #endif // LLVM_EXECUTIONENGINE_ORC_ORCABISUPPORT_H diff --git a/include/llvm/ExecutionEngine/Orc/OrcRemoteTargetClient.h b/include/llvm/ExecutionEngine/Orc/OrcRemoteTargetClient.h index a19c30631c573..da02250ba1692 100644 --- a/include/llvm/ExecutionEngine/Orc/OrcRemoteTargetClient.h +++ b/include/llvm/ExecutionEngine/Orc/OrcRemoteTargetClient.h @@ -1,4 +1,4 @@ -//===---- OrcRemoteTargetClient.h - Orc Remote-target Client ----*- C++ -*-===// +//===- OrcRemoteTargetClient.h - Orc Remote-target Client -------*- C++ -*-===// // // The LLVM Compiler Infrastructure // @@ -16,10 +16,29 @@ #ifndef LLVM_EXECUTIONENGINE_ORC_ORCREMOTETARGETCLIENT_H #define LLVM_EXECUTIONENGINE_ORC_ORCREMOTETARGETCLIENT_H -#include "IndirectionUtils.h" -#include "OrcRemoteTargetRPCAPI.h" +#include "llvm/ADT/Optional.h" +#include "llvm/ADT/STLExtras.h" +#include "llvm/ADT/StringMap.h" +#include "llvm/ADT/StringRef.h" +#include "llvm/ExecutionEngine/JITSymbol.h" +#include "llvm/ExecutionEngine/Orc/IndirectionUtils.h" +#include "llvm/ExecutionEngine/Orc/OrcRemoteTargetRPCAPI.h" #include "llvm/ExecutionEngine/RuntimeDyld.h" -#include <system_error> +#include "llvm/Support/Debug.h" +#include "llvm/Support/Error.h" +#include "llvm/Support/ErrorHandling.h" +#include "llvm/Support/Format.h" +#include "llvm/Support/MathExtras.h" +#include "llvm/Support/Memory.h" +#include "llvm/Support/raw_ostream.h" +#include <algorithm> +#include <cassert> +#include <cstdint> +#include <memory> +#include <string> +#include <tuple> +#include <utility> +#include <vector> #define DEBUG_TYPE "orc-remote" @@ -207,7 +226,6 @@ public: DEBUG(dbgs() << "Allocator " << Id << " finalizing:\n"); for (auto &ObjAllocs : Unfinalized) { - for (auto &Alloc : ObjAllocs.CodeAllocs) { DEBUG(dbgs() << " copying code: " << static_cast<void *>(Alloc.getLocalAddress()) << " -> " @@ -469,7 +487,7 @@ public: OrcRemoteTargetClient &Remote; ResourceIdMgr::ResourceId Id; std::vector<RemoteIndirectStubsInfo> RemoteIndirectStubsInfos; - typedef std::pair<uint16_t, uint16_t> StubKey; + using StubKey = std::pair<uint16_t, uint16_t>; std::vector<StubKey> FreeStubs; StringMap<std::pair<StubKey, JITSymbolFlags>> StubIndexes; @@ -710,7 +728,6 @@ private: Expected<JITTargetAddress> reserveMem(ResourceIdMgr::ResourceId Id, uint64_t Size, uint32_t Align) { - // Check for an 'out-of-band' error, e.g. from an MM destructor. if (ExistingError) return std::move(ExistingError); diff --git a/include/llvm/ExecutionEngine/Orc/OrcRemoteTargetRPCAPI.h b/include/llvm/ExecutionEngine/Orc/OrcRemoteTargetRPCAPI.h index 3086ef0cdf803..07ae7f04d1a02 100644 --- a/include/llvm/ExecutionEngine/Orc/OrcRemoteTargetRPCAPI.h +++ b/include/llvm/ExecutionEngine/Orc/OrcRemoteTargetRPCAPI.h @@ -1,4 +1,4 @@ -//===--- OrcRemoteTargetRPCAPI.h - Orc Remote-target RPC API ----*- C++ -*-===// +//===- OrcRemoteTargetRPCAPI.h - Orc Remote-target RPC API ------*- C++ -*-===// // // The LLVM Compiler Infrastructure // @@ -16,12 +16,13 @@ #ifndef LLVM_EXECUTIONENGINE_ORC_ORCREMOTETARGETRPCAPI_H #define LLVM_EXECUTIONENGINE_ORC_ORCREMOTETARGETRPCAPI_H -#include "RPCUtils.h" -#include "RawByteChannel.h" #include "llvm/ExecutionEngine/JITSymbol.h" +#include "llvm/ExecutionEngine/Orc/RPCUtils.h" +#include "llvm/ExecutionEngine/Orc/RawByteChannel.h" namespace llvm { namespace orc { + namespace remote { class DirectBufferWriter { @@ -72,7 +73,7 @@ public: return EC; char *Addr = reinterpret_cast<char *>(static_cast<uintptr_t>(Dst)); - DBW = remote::DirectBufferWriter(0, Dst, Size); + DBW = remote::DirectBufferWriter(nullptr, Dst, Size); return C.readBytes(Addr, Size); } @@ -87,7 +88,7 @@ class OrcRemoteTargetRPCAPI protected: class ResourceIdMgr { public: - typedef uint64_t ResourceId; + using ResourceId = uint64_t; static const ResourceId InvalidId = ~0U; ResourceId getNext() { @@ -98,6 +99,7 @@ protected: } return NextId++; } + void release(ResourceId I) { FreeIds.push_back(I); } private: @@ -261,7 +263,8 @@ public: }; } // end namespace remote + } // end namespace orc } // end namespace llvm -#endif +#endif // LLVM_EXECUTIONENGINE_ORC_ORCREMOTETARGETRPCAPI_H diff --git a/include/llvm/ExecutionEngine/Orc/OrcRemoteTargetServer.h b/include/llvm/ExecutionEngine/Orc/OrcRemoteTargetServer.h index a61ff102be0b0..e7b6d64931b6a 100644 --- a/include/llvm/ExecutionEngine/Orc/OrcRemoteTargetServer.h +++ b/include/llvm/ExecutionEngine/Orc/OrcRemoteTargetServer.h @@ -1,4 +1,4 @@ -//===---- OrcRemoteTargetServer.h - Orc Remote-target Server ----*- C++ -*-===// +//===- OrcRemoteTargetServer.h - Orc Remote-target Server -------*- C++ -*-===// // // The LLVM Compiler Infrastructure // @@ -15,10 +15,9 @@ #ifndef LLVM_EXECUTIONENGINE_ORC_ORCREMOTETARGETSERVER_H #define LLVM_EXECUTIONENGINE_ORC_ORCREMOTETARGETSERVER_H -#include "OrcRemoteTargetRPCAPI.h" #include "llvm/ExecutionEngine/JITSymbol.h" #include "llvm/ExecutionEngine/Orc/OrcError.h" -#include "llvm/ExecutionEngine/RTDyldMemoryManager.h" +#include "llvm/ExecutionEngine/Orc/OrcRemoteTargetRPCAPI.h" #include "llvm/Support/Debug.h" #include "llvm/Support/Error.h" #include "llvm/Support/Format.h" @@ -48,20 +47,18 @@ namespace remote { template <typename ChannelT, typename TargetT> class OrcRemoteTargetServer : public OrcRemoteTargetRPCAPI { public: - typedef std::function<JITTargetAddress(const std::string &Name)> - SymbolLookupFtor; + using SymbolLookupFtor = + std::function<JITTargetAddress(const std::string &Name)>; - typedef std::function<void(uint8_t *Addr, uint32_t Size)> - EHFrameRegistrationFtor; + using EHFrameRegistrationFtor = + std::function<void(uint8_t *Addr, uint32_t Size)>; OrcRemoteTargetServer(ChannelT &Channel, SymbolLookupFtor SymbolLookup, EHFrameRegistrationFtor EHFramesRegister, EHFrameRegistrationFtor EHFramesDeregister) : OrcRemoteTargetRPCAPI(Channel), SymbolLookup(std::move(SymbolLookup)), EHFramesRegister(std::move(EHFramesRegister)), - EHFramesDeregister(std::move(EHFramesDeregister)), - TerminateFlag(false) { - + EHFramesDeregister(std::move(EHFramesDeregister)) { using ThisT = typename std::remove_reference<decltype(*this)>::type; addHandler<CallIntVoid>(*this, &ThisT::handleCallIntVoid); addHandler<CallMain>(*this, &ThisT::handleCallMain); @@ -106,6 +103,7 @@ private: struct Allocator { Allocator() = default; Allocator(Allocator &&Other) : Allocs(std::move(Other.Allocs)) {} + Allocator &operator=(Allocator &&Other) { Allocs = std::move(Other.Allocs); return *this; @@ -153,7 +151,8 @@ private: } Expected<int32_t> handleCallIntVoid(JITTargetAddress Addr) { - typedef int (*IntVoidFnTy)(); + using IntVoidFnTy = int (*)(); + IntVoidFnTy Fn = reinterpret_cast<IntVoidFnTy>(static_cast<uintptr_t>(Addr)); @@ -166,7 +165,7 @@ private: Expected<int32_t> handleCallMain(JITTargetAddress Addr, std::vector<std::string> Args) { - typedef int (*MainFnTy)(int, const char *[]); + using MainFnTy = int (*)(int, const char *[]); MainFnTy Fn = reinterpret_cast<MainFnTy>(static_cast<uintptr_t>(Addr)); int ArgC = Args.size() + 1; @@ -175,6 +174,12 @@ private: ArgV[0] = "<jit process>"; for (auto &Arg : Args) ArgV[Idx++] = Arg.c_str(); + ArgV[ArgC] = 0; + DEBUG( + for (int Idx = 0; Idx < ArgC; ++Idx) { + llvm::dbgs() << "Arg " << Idx << ": " << ArgV[Idx] << "\n"; + } + ); DEBUG(dbgs() << " Calling " << format("0x%016x", Addr) << "\n"); int Result = Fn(ArgC, ArgV.get()); @@ -184,7 +189,8 @@ private: } Error handleCallVoidVoid(JITTargetAddress Addr) { - typedef void (*VoidVoidFnTy)(); + using VoidVoidFnTy = void (*)(); + VoidVoidFnTy Fn = reinterpret_cast<VoidVoidFnTy>(static_cast<uintptr_t>(Addr)); @@ -420,11 +426,11 @@ private: SymbolLookupFtor SymbolLookup; EHFrameRegistrationFtor EHFramesRegister, EHFramesDeregister; std::map<ResourceIdMgr::ResourceId, Allocator> Allocators; - typedef std::vector<typename TargetT::IndirectStubsInfo> ISBlockOwnerList; + using ISBlockOwnerList = std::vector<typename TargetT::IndirectStubsInfo>; std::map<ResourceIdMgr::ResourceId, ISBlockOwnerList> IndirectStubsOwners; sys::OwningMemoryBlock ResolverBlock; std::vector<sys::OwningMemoryBlock> TrampolineBlocks; - bool TerminateFlag; + bool TerminateFlag = false; }; } // end namespace remote @@ -433,4 +439,4 @@ private: #undef DEBUG_TYPE -#endif +#endif // LLVM_EXECUTIONENGINE_ORC_ORCREMOTETARGETSERVER_H diff --git a/include/llvm/ExecutionEngine/Orc/RTDyldObjectLinkingLayer.h b/include/llvm/ExecutionEngine/Orc/RTDyldObjectLinkingLayer.h index aabb44eef99dc..66ad36be01c8d 100644 --- a/include/llvm/ExecutionEngine/Orc/RTDyldObjectLinkingLayer.h +++ b/include/llvm/ExecutionEngine/Orc/RTDyldObjectLinkingLayer.h @@ -1,4 +1,4 @@ -//===-- RTDyldObjectLinkingLayer.h - RTDyld-based jit linking --*- C++ -*-===// +//===- RTDyldObjectLinkingLayer.h - RTDyld-based jit linking ---*- C++ -*-===// // // The LLVM Compiler Infrastructure // @@ -17,10 +17,8 @@ #include "llvm/ADT/STLExtras.h" #include "llvm/ADT/StringMap.h" #include "llvm/ADT/StringRef.h" -#include "llvm/ExecutionEngine/ExecutionEngine.h" #include "llvm/ExecutionEngine/JITSymbol.h" #include "llvm/ExecutionEngine/RuntimeDyld.h" -#include "llvm/ExecutionEngine/SectionMemoryManager.h" #include "llvm/Object/ObjectFile.h" #include "llvm/Support/Error.h" #include <algorithm> @@ -36,20 +34,26 @@ namespace llvm { namespace orc { class RTDyldObjectLinkingLayerBase { +public: + + using ObjectPtr = + std::shared_ptr<object::OwningBinary<object::ObjectFile>>; + protected: + /// @brief Holds a set of objects to be allocated/linked as a unit in the JIT. /// /// An instance of this class will be created for each set of objects added - /// via JITObjectLayer::addObjectSet. Deleting the instance (via - /// removeObjectSet) frees its memory, removing all symbol definitions that + /// via JITObjectLayer::addObject. Deleting the instance (via + /// removeObject) frees its memory, removing all symbol definitions that /// had been provided by this instance. Higher level layers are responsible /// for taking any action required to handle the missing symbols. - class LinkedObjectSet { + class LinkedObject { public: - LinkedObjectSet() = default; - LinkedObjectSet(const LinkedObjectSet&) = delete; - void operator=(const LinkedObjectSet&) = delete; - virtual ~LinkedObjectSet() = default; + LinkedObject() = default; + LinkedObject(const LinkedObject&) = delete; + void operator=(const LinkedObject&) = delete; + virtual ~LinkedObject() = default; virtual void finalize() = 0; @@ -76,19 +80,11 @@ protected: bool Finalized = false; }; - typedef std::list<std::unique_ptr<LinkedObjectSet>> LinkedObjectSetListT; + using LinkedObjectListT = std::list<std::unique_ptr<LinkedObject>>; public: /// @brief Handle to a set of loaded objects. - typedef LinkedObjectSetListT::iterator ObjSetHandleT; -}; - -/// @brief Default (no-op) action to perform when loading objects. -class DoNothingOnNotifyLoaded { -public: - template <typename ObjSetT, typename LoadResult> - void operator()(RTDyldObjectLinkingLayerBase::ObjSetHandleT, const ObjSetT &, - const LoadResult &) {} + using ObjHandleT = LinkedObjectListT::iterator; }; /// @brief Bare bones object linking layer. @@ -97,46 +93,54 @@ public: /// object files to be loaded into memory, linked, and the addresses of their /// symbols queried. All objects added to this layer can see each other's /// symbols. -template <typename NotifyLoadedFtor = DoNothingOnNotifyLoaded> class RTDyldObjectLinkingLayer : public RTDyldObjectLinkingLayerBase { public: + + using RTDyldObjectLinkingLayerBase::ObjectPtr; + + /// @brief Functor for receiving object-loaded notifications. + using NotifyLoadedFtor = std::function<void(ObjHandleT, const ObjectPtr &Obj, + const LoadedObjectInfo &)>; + /// @brief Functor for receiving finalization notifications. - typedef std::function<void(ObjSetHandleT)> NotifyFinalizedFtor; + using NotifyFinalizedFtor = std::function<void(ObjHandleT)>; private: - template <typename ObjSetT, typename MemoryManagerPtrT, - typename SymbolResolverPtrT, typename FinalizerFtor> - class ConcreteLinkedObjectSet : public LinkedObjectSet { + + + template <typename MemoryManagerPtrT, typename SymbolResolverPtrT, + typename FinalizerFtor> + class ConcreteLinkedObject : public LinkedObject { public: - ConcreteLinkedObjectSet(ObjSetT Objects, MemoryManagerPtrT MemMgr, - SymbolResolverPtrT Resolver, - FinalizerFtor Finalizer, - bool ProcessAllSections) + ConcreteLinkedObject(ObjectPtr Obj, MemoryManagerPtrT MemMgr, + SymbolResolverPtrT Resolver, + FinalizerFtor Finalizer, + bool ProcessAllSections) : MemMgr(std::move(MemMgr)), - PFC(llvm::make_unique<PreFinalizeContents>(std::move(Objects), + PFC(llvm::make_unique<PreFinalizeContents>(std::move(Obj), std::move(Resolver), std::move(Finalizer), ProcessAllSections)) { - buildInitialSymbolTable(PFC->Objects); + buildInitialSymbolTable(PFC->Obj); } - ~ConcreteLinkedObjectSet() override { + ~ConcreteLinkedObject() override { MemMgr->deregisterEHFrames(); } - - void setHandle(ObjSetHandleT H) { + + void setHandle(ObjHandleT H) { PFC->Handle = H; } void finalize() override { - assert(PFC && "mapSectionAddress called on finalized LinkedObjectSet"); + assert(PFC && "mapSectionAddress called on finalized LinkedObject"); RuntimeDyld RTDyld(*MemMgr, *PFC->Resolver); RTDyld.setProcessAllSections(PFC->ProcessAllSections); PFC->RTDyld = &RTDyld; this->Finalized = true; - PFC->Finalizer(PFC->Handle, RTDyld, std::move(PFC->Objects), + PFC->Finalizer(PFC->Handle, RTDyld, std::move(PFC->Obj), [&]() { this->updateSymbolTable(RTDyld); }); @@ -158,27 +162,27 @@ private: void mapSectionAddress(const void *LocalAddress, JITTargetAddress TargetAddr) const override { - assert(PFC && "mapSectionAddress called on finalized LinkedObjectSet"); - assert(PFC->RTDyld && "mapSectionAddress called on raw LinkedObjectSet"); + assert(PFC && "mapSectionAddress called on finalized LinkedObject"); + assert(PFC->RTDyld && "mapSectionAddress called on raw LinkedObject"); PFC->RTDyld->mapSectionAddress(LocalAddress, TargetAddr); } private: - void buildInitialSymbolTable(const ObjSetT &Objects) { - for (const auto &Obj : Objects) - for (auto &Symbol : getObject(*Obj).symbols()) { - if (Symbol.getFlags() & object::SymbolRef::SF_Undefined) - continue; - Expected<StringRef> SymbolName = Symbol.getName(); - // FIXME: Raise an error for bad symbols. - if (!SymbolName) { - consumeError(SymbolName.takeError()); - continue; - } - auto Flags = JITSymbolFlags::fromObjectSymbol(Symbol); - SymbolTable.insert( - std::make_pair(*SymbolName, JITEvaluatedSymbol(0, Flags))); + + void buildInitialSymbolTable(const ObjectPtr &Obj) { + for (auto &Symbol : Obj->getBinary()->symbols()) { + if (Symbol.getFlags() & object::SymbolRef::SF_Undefined) + continue; + Expected<StringRef> SymbolName = Symbol.getName(); + // FIXME: Raise an error for bad symbols. + if (!SymbolName) { + consumeError(SymbolName.takeError()); + continue; } + auto Flags = JITSymbolFlags::fromObjectSymbol(Symbol); + SymbolTable.insert( + std::make_pair(*SymbolName, JITEvaluatedSymbol(0, Flags))); + } } void updateSymbolTable(const RuntimeDyld &RTDyld) { @@ -189,17 +193,17 @@ private: // Contains the information needed prior to finalization: the object files, // memory manager, resolver, and flags needed for RuntimeDyld. struct PreFinalizeContents { - PreFinalizeContents(ObjSetT Objects, SymbolResolverPtrT Resolver, + PreFinalizeContents(ObjectPtr Obj, SymbolResolverPtrT Resolver, FinalizerFtor Finalizer, bool ProcessAllSections) - : Objects(std::move(Objects)), Resolver(std::move(Resolver)), + : Obj(std::move(Obj)), Resolver(std::move(Resolver)), Finalizer(std::move(Finalizer)), ProcessAllSections(ProcessAllSections) {} - ObjSetT Objects; + ObjectPtr Obj; SymbolResolverPtrT Resolver; FinalizerFtor Finalizer; bool ProcessAllSections; - ObjSetHandleT Handle; + ObjHandleT Handle; RuntimeDyld *RTDyld; }; @@ -207,27 +211,22 @@ private: std::unique_ptr<PreFinalizeContents> PFC; }; - template <typename ObjSetT, typename MemoryManagerPtrT, - typename SymbolResolverPtrT, typename FinalizerFtor> + template <typename MemoryManagerPtrT, typename SymbolResolverPtrT, + typename FinalizerFtor> std::unique_ptr< - ConcreteLinkedObjectSet<ObjSetT, MemoryManagerPtrT, - SymbolResolverPtrT, FinalizerFtor>> - createLinkedObjectSet(ObjSetT Objects, MemoryManagerPtrT MemMgr, - SymbolResolverPtrT Resolver, - FinalizerFtor Finalizer, - bool ProcessAllSections) { - typedef ConcreteLinkedObjectSet<ObjSetT, MemoryManagerPtrT, - SymbolResolverPtrT, FinalizerFtor> LOS; - return llvm::make_unique<LOS>(std::move(Objects), std::move(MemMgr), + ConcreteLinkedObject<MemoryManagerPtrT, SymbolResolverPtrT, FinalizerFtor>> + createLinkedObject(ObjectPtr Obj, MemoryManagerPtrT MemMgr, + SymbolResolverPtrT Resolver, + FinalizerFtor Finalizer, + bool ProcessAllSections) { + using LOS = ConcreteLinkedObject<MemoryManagerPtrT, SymbolResolverPtrT, + FinalizerFtor>; + return llvm::make_unique<LOS>(std::move(Obj), std::move(MemMgr), std::move(Resolver), std::move(Finalizer), ProcessAllSections); } public: - /// @brief LoadedObjectInfo list. Contains a list of owning pointers to - /// RuntimeDyld::LoadedObjectInfo instances. - typedef std::vector<std::unique_ptr<RuntimeDyld::LoadedObjectInfo>> - LoadedObjInfoList; /// @brief Construct an ObjectLinkingLayer with the given NotifyLoaded, /// and NotifyFinalized functors. @@ -235,8 +234,7 @@ public: NotifyLoadedFtor NotifyLoaded = NotifyLoadedFtor(), NotifyFinalizedFtor NotifyFinalized = NotifyFinalizedFtor()) : NotifyLoaded(std::move(NotifyLoaded)), - NotifyFinalized(std::move(NotifyFinalized)), - ProcessAllSections(false) {} + NotifyFinalized(std::move(NotifyFinalized)) {} /// @brief Set the 'ProcessAllSections' flag. /// @@ -253,23 +251,22 @@ public: /// /// @return A handle that can be used to refer to the loaded objects (for /// symbol searching, finalization, freeing memory, etc.). - template <typename ObjSetT, - typename MemoryManagerPtrT, + template <typename MemoryManagerPtrT, typename SymbolResolverPtrT> - ObjSetHandleT addObjectSet(ObjSetT Objects, - MemoryManagerPtrT MemMgr, - SymbolResolverPtrT Resolver) { - auto Finalizer = [&](ObjSetHandleT H, RuntimeDyld &RTDyld, - const ObjSetT &Objs, - std::function<void()> LOSHandleLoad) { - LoadedObjInfoList LoadedObjInfos; + ObjHandleT addObject(ObjectPtr Obj, + MemoryManagerPtrT MemMgr, + SymbolResolverPtrT Resolver) { - for (auto &Obj : Objs) - LoadedObjInfos.push_back(RTDyld.loadObject(this->getObject(*Obj))); + auto Finalizer = [&](ObjHandleT H, RuntimeDyld &RTDyld, + const ObjectPtr &ObjToLoad, + std::function<void()> LOSHandleLoad) { + std::unique_ptr<RuntimeDyld::LoadedObjectInfo> Info = + RTDyld.loadObject(*ObjToLoad->getBinary()); LOSHandleLoad(); - this->NotifyLoaded(H, Objs, LoadedObjInfos); + if (this->NotifyLoaded) + this->NotifyLoaded(H, ObjToLoad, *Info); RTDyld.finalizeWithMemoryManagerLocking(); @@ -277,17 +274,15 @@ public: this->NotifyFinalized(H); }; - auto LOS = - createLinkedObjectSet(std::move(Objects), std::move(MemMgr), - std::move(Resolver), std::move(Finalizer), - ProcessAllSections); + auto LO = + createLinkedObject(std::move(Obj), std::move(MemMgr), std::move(Resolver), + std::move(Finalizer), ProcessAllSections); // LOS is an owning-ptr. Keep a non-owning one so that we can set the handle // below. - auto *LOSPtr = LOS.get(); + auto *LOPtr = LO.get(); - ObjSetHandleT Handle = LinkedObjSetList.insert(LinkedObjSetList.end(), - std::move(LOS)); - LOSPtr->setHandle(Handle); + ObjHandleT Handle = LinkedObjList.insert(LinkedObjList.end(), std::move(LO)); + LOPtr->setHandle(Handle); return Handle; } @@ -300,9 +295,9 @@ public: /// indirectly) will result in undefined behavior. If dependence tracking is /// required to detect or resolve such issues it should be added at a higher /// layer. - void removeObjectSet(ObjSetHandleT H) { + void removeObject(ObjHandleT H) { // How do we invalidate the symbols in H? - LinkedObjSetList.erase(H); + LinkedObjList.erase(H); } /// @brief Search for the given named symbol. @@ -310,7 +305,7 @@ public: /// @param ExportedSymbolsOnly If true, search only for exported symbols. /// @return A handle for the given named symbol, if it exists. JITSymbol findSymbol(StringRef Name, bool ExportedSymbolsOnly) { - for (auto I = LinkedObjSetList.begin(), E = LinkedObjSetList.end(); I != E; + for (auto I = LinkedObjList.begin(), E = LinkedObjList.end(); I != E; ++I) if (auto Symbol = findSymbolIn(I, Name, ExportedSymbolsOnly)) return Symbol; @@ -325,13 +320,13 @@ public: /// @param ExportedSymbolsOnly If true, search only for exported symbols. /// @return A handle for the given named symbol, if it is found in the /// given object set. - JITSymbol findSymbolIn(ObjSetHandleT H, StringRef Name, + JITSymbol findSymbolIn(ObjHandleT H, StringRef Name, bool ExportedSymbolsOnly) { return (*H)->getSymbol(Name, ExportedSymbolsOnly); } /// @brief Map section addresses for the objects associated with the handle H. - void mapSectionAddress(ObjSetHandleT H, const void *LocalAddress, + void mapSectionAddress(ObjHandleT H, const void *LocalAddress, JITTargetAddress TargetAddr) { (*H)->mapSectionAddress(LocalAddress, TargetAddr); } @@ -339,25 +334,16 @@ public: /// @brief Immediately emit and finalize the object set represented by the /// given handle. /// @param H Handle for object set to emit/finalize. - void emitAndFinalize(ObjSetHandleT H) { + void emitAndFinalize(ObjHandleT H) { (*H)->finalize(); } private: - static const object::ObjectFile& getObject(const object::ObjectFile &Obj) { - return Obj; - } - - template <typename ObjT> - static const object::ObjectFile& - getObject(const object::OwningBinary<ObjT> &Obj) { - return *Obj.getBinary(); - } - LinkedObjectSetListT LinkedObjSetList; + LinkedObjectListT LinkedObjList; NotifyLoadedFtor NotifyLoaded; NotifyFinalizedFtor NotifyFinalized; - bool ProcessAllSections; + bool ProcessAllSections = false; }; } // end namespace orc diff --git a/include/llvm/ExecutionEngine/Orc/RawByteChannel.h b/include/llvm/ExecutionEngine/Orc/RawByteChannel.h index 52a546f7c6eb9..db810f4ef2e53 100644 --- a/include/llvm/ExecutionEngine/Orc/RawByteChannel.h +++ b/include/llvm/ExecutionEngine/Orc/RawByteChannel.h @@ -10,20 +10,14 @@ #ifndef LLVM_EXECUTIONENGINE_ORC_RAWBYTECHANNEL_H #define LLVM_EXECUTIONENGINE_ORC_RAWBYTECHANNEL_H -#include "OrcError.h" -#include "RPCSerialization.h" -#include "llvm/ADT/ArrayRef.h" -#include "llvm/ADT/STLExtras.h" #include "llvm/ADT/StringRef.h" +#include "llvm/ExecutionEngine/Orc/RPCSerialization.h" #include "llvm/Support/Endian.h" #include "llvm/Support/Error.h" -#include <cstddef> #include <cstdint> #include <mutex> #include <string> -#include <tuple> #include <type_traits> -#include <vector> namespace llvm { namespace orc { @@ -32,7 +26,7 @@ namespace rpc { /// Interface for byte-streams to be used with RPC. class RawByteChannel { public: - virtual ~RawByteChannel() {} + virtual ~RawByteChannel() = default; /// Read Size bytes from the stream into *Dst. virtual Error readBytes(char *Dst, unsigned Size) = 0; |