diff options
Diffstat (limited to 'include/llvm/ExecutionEngine/Orc/IndirectionUtils.h')
-rw-r--r-- | include/llvm/ExecutionEngine/Orc/IndirectionUtils.h | 127 |
1 files changed, 66 insertions, 61 deletions
diff --git a/include/llvm/ExecutionEngine/Orc/IndirectionUtils.h b/include/llvm/ExecutionEngine/Orc/IndirectionUtils.h index e17630fa05ff..51172c51e136 100644 --- a/include/llvm/ExecutionEngine/Orc/IndirectionUtils.h +++ b/include/llvm/ExecutionEngine/Orc/IndirectionUtils.h @@ -16,14 +16,12 @@ #include "JITSymbol.h" #include "LambdaResolver.h" -#include "llvm/ADT/DenseSet.h" #include "llvm/ExecutionEngine/RuntimeDyld.h" #include "llvm/IR/IRBuilder.h" #include "llvm/IR/Mangler.h" #include "llvm/IR/Module.h" -#include "llvm/Transforms/Utils/ValueMapper.h" #include "llvm/Support/Process.h" -#include <sstream> +#include "llvm/Transforms/Utils/ValueMapper.h" namespace llvm { namespace orc { @@ -31,7 +29,6 @@ namespace orc { /// @brief Target-independent base class for compile callback management. class JITCompileCallbackManager { public: - typedef std::function<TargetAddress()> CompileFtor; /// @brief Handle to a newly created compile callback. Can be used to get an @@ -40,12 +37,13 @@ public: class CompileCallbackInfo { public: CompileCallbackInfo(TargetAddress Addr, CompileFtor &Compile) - : Addr(Addr), Compile(Compile) {} + : Addr(Addr), Compile(Compile) {} TargetAddress getAddress() const { return Addr; } void setCompileAction(CompileFtor Compile) { this->Compile = std::move(Compile); } + private: TargetAddress Addr; CompileFtor &Compile; @@ -55,7 +53,7 @@ public: /// @param ErrorHandlerAddress The address of an error handler in the target /// process to be used if a compile callback fails. JITCompileCallbackManager(TargetAddress ErrorHandlerAddress) - : ErrorHandlerAddress(ErrorHandlerAddress) {} + : ErrorHandlerAddress(ErrorHandlerAddress) {} virtual ~JITCompileCallbackManager() {} @@ -71,8 +69,10 @@ public: // Found a callback handler. Yank this trampoline out of the active list and // put it back in the available trampolines list, then try to run the // handler's compile and update actions. - // Moving the trampoline ID back to the available list first means there's at - // least one available trampoline if the compile action triggers a request for + // Moving the trampoline ID back to the available list first means there's + // at + // least one available trampoline if the compile action triggers a request + // for // a new one. auto Compile = std::move(I->second); ActiveTrampolines.erase(I); @@ -118,7 +118,6 @@ protected: std::vector<TargetAddress> AvailableTrampolines; private: - TargetAddress getAvailableTrampolineAddr() { if (this->AvailableTrampolines.empty()) grow(); @@ -139,20 +138,17 @@ private: template <typename TargetT> class LocalJITCompileCallbackManager : public JITCompileCallbackManager { public: - /// @brief Construct a InProcessJITCompileCallbackManager. /// @param ErrorHandlerAddress The address of an error handler in the target /// process to be used if a compile callback fails. LocalJITCompileCallbackManager(TargetAddress ErrorHandlerAddress) - : JITCompileCallbackManager(ErrorHandlerAddress) { + : JITCompileCallbackManager(ErrorHandlerAddress) { /// Set up the resolver block. std::error_code EC; - ResolverBlock = - sys::OwningMemoryBlock( - sys::Memory::allocateMappedMemory(TargetT::ResolverCodeSize, nullptr, - sys::Memory::MF_READ | - sys::Memory::MF_WRITE, EC)); + ResolverBlock = sys::OwningMemoryBlock(sys::Memory::allocateMappedMemory( + TargetT::ResolverCodeSize, nullptr, + sys::Memory::MF_READ | sys::Memory::MF_WRITE, EC)); assert(!EC && "Failed to allocate resolver block"); TargetT::writeResolverCode(static_cast<uint8_t *>(ResolverBlock.base()), @@ -165,13 +161,11 @@ public: } private: - static TargetAddress reenter(void *CCMgr, void *TrampolineId) { JITCompileCallbackManager *Mgr = - static_cast<JITCompileCallbackManager*>(CCMgr); + static_cast<JITCompileCallbackManager *>(CCMgr); return Mgr->executeCompileCallback( - static_cast<TargetAddress>( - reinterpret_cast<uintptr_t>(TrampolineId))); + static_cast<TargetAddress>(reinterpret_cast<uintptr_t>(TrampolineId))); } void grow() override { @@ -179,18 +173,16 @@ private: std::error_code EC; auto TrampolineBlock = - sys::OwningMemoryBlock( - sys::Memory::allocateMappedMemory(sys::Process::getPageSize(), nullptr, - sys::Memory::MF_READ | - sys::Memory::MF_WRITE, EC)); + sys::OwningMemoryBlock(sys::Memory::allocateMappedMemory( + sys::Process::getPageSize(), nullptr, + sys::Memory::MF_READ | sys::Memory::MF_WRITE, EC)); assert(!EC && "Failed to allocate trampoline block"); - unsigned NumTrampolines = - (sys::Process::getPageSize() - TargetT::PointerSize) / + (sys::Process::getPageSize() - TargetT::PointerSize) / TargetT::TrampolineSize; - uint8_t *TrampolineMem = static_cast<uint8_t*>(TrampolineBlock.base()); + uint8_t *TrampolineMem = static_cast<uint8_t *>(TrampolineBlock.base()); TargetT::writeTrampolines(TrampolineMem, ResolverBlock.base(), NumTrampolines); @@ -214,19 +206,18 @@ private: /// @brief Base class for managing collections of named indirect stubs. class IndirectStubsManager { public: - /// @brief Map type for initializing the manager. See init. typedef StringMap<std::pair<TargetAddress, JITSymbolFlags>> StubInitsMap; virtual ~IndirectStubsManager() {} /// @brief Create a single stub with the given name, target address and flags. - virtual std::error_code createStub(StringRef StubName, TargetAddress StubAddr, - JITSymbolFlags StubFlags) = 0; + virtual Error createStub(StringRef StubName, TargetAddress StubAddr, + JITSymbolFlags StubFlags) = 0; /// @brief Create StubInits.size() stubs with the given names, target /// addresses, and flags. - virtual std::error_code createStubs(const StubInitsMap &StubInits) = 0; + virtual Error createStubs(const StubInitsMap &StubInits) = 0; /// @brief Find the stub with the given name. If ExportedStubsOnly is true, /// this will only return a result if the stub's flags indicate that it @@ -237,7 +228,8 @@ public: virtual JITSymbol findPointer(StringRef Name) = 0; /// @brief Change the value of the implementation pointer for the stub. - virtual std::error_code updatePointer(StringRef Name, TargetAddress NewAddr) = 0; + virtual Error updatePointer(StringRef Name, TargetAddress NewAddr) = 0; + private: virtual void anchor(); }; @@ -247,26 +239,25 @@ private: template <typename TargetT> class LocalIndirectStubsManager : public IndirectStubsManager { public: - - std::error_code createStub(StringRef StubName, TargetAddress StubAddr, - JITSymbolFlags StubFlags) override { - if (auto EC = reserveStubs(1)) - return EC; + Error createStub(StringRef StubName, TargetAddress StubAddr, + JITSymbolFlags StubFlags) override { + if (auto Err = reserveStubs(1)) + return Err; createStubInternal(StubName, StubAddr, StubFlags); - return std::error_code(); + return Error::success(); } - std::error_code createStubs(const StubInitsMap &StubInits) override { - if (auto EC = reserveStubs(StubInits.size())) - return EC; + Error createStubs(const StubInitsMap &StubInits) override { + if (auto Err = reserveStubs(StubInits.size())) + return Err; for (auto &Entry : StubInits) createStubInternal(Entry.first(), Entry.second.first, Entry.second.second); - return std::error_code(); + return Error::success(); } JITSymbol findStub(StringRef Name, bool ExportedStubsOnly) override { @@ -277,7 +268,7 @@ public: void *StubAddr = IndirectStubsInfos[Key.first].getStub(Key.second); assert(StubAddr && "Missing stub address"); auto StubTargetAddr = - static_cast<TargetAddress>(reinterpret_cast<uintptr_t>(StubAddr)); + static_cast<TargetAddress>(reinterpret_cast<uintptr_t>(StubAddr)); auto StubSymbol = JITSymbol(StubTargetAddr, I->second.second); if (ExportedStubsOnly && !StubSymbol.isExported()) return nullptr; @@ -292,35 +283,34 @@ public: void *PtrAddr = IndirectStubsInfos[Key.first].getPtr(Key.second); assert(PtrAddr && "Missing pointer address"); auto PtrTargetAddr = - static_cast<TargetAddress>(reinterpret_cast<uintptr_t>(PtrAddr)); + static_cast<TargetAddress>(reinterpret_cast<uintptr_t>(PtrAddr)); return JITSymbol(PtrTargetAddr, I->second.second); } - std::error_code updatePointer(StringRef Name, TargetAddress NewAddr) override { + Error updatePointer(StringRef Name, TargetAddress NewAddr) override { auto I = StubIndexes.find(Name); assert(I != StubIndexes.end() && "No stub pointer for symbol"); auto Key = I->second.first; *IndirectStubsInfos[Key.first].getPtr(Key.second) = - reinterpret_cast<void*>(static_cast<uintptr_t>(NewAddr)); - return std::error_code(); + reinterpret_cast<void *>(static_cast<uintptr_t>(NewAddr)); + return Error::success(); } private: - - std::error_code reserveStubs(unsigned NumStubs) { + Error reserveStubs(unsigned NumStubs) { if (NumStubs <= FreeStubs.size()) - return std::error_code(); + return Error::success(); unsigned NewStubsRequired = NumStubs - FreeStubs.size(); unsigned NewBlockId = IndirectStubsInfos.size(); typename TargetT::IndirectStubsInfo ISI; - if (auto EC = TargetT::emitIndirectStubsBlock(ISI, NewStubsRequired, - nullptr)) - return EC; + if (auto Err = + TargetT::emitIndirectStubsBlock(ISI, NewStubsRequired, nullptr)) + return Err; for (unsigned I = 0; I < ISI.getNumStubs(); ++I) FreeStubs.push_back(std::make_pair(NewBlockId, I)); IndirectStubsInfos.push_back(std::move(ISI)); - return std::error_code(); + return Error::success(); } void createStubInternal(StringRef StubName, TargetAddress InitAddr, @@ -328,7 +318,7 @@ private: auto Key = FreeStubs.back(); FreeStubs.pop_back(); *IndirectStubsInfos[Key.first].getPtr(Key.second) = - reinterpret_cast<void*>(static_cast<uintptr_t>(InitAddr)); + reinterpret_cast<void *>(static_cast<uintptr_t>(InitAddr)); StubIndexes[StubName] = std::make_pair(Key, StubFlags); } @@ -338,17 +328,32 @@ private: StringMap<std::pair<StubKey, JITSymbolFlags>> StubIndexes; }; +/// @brief Create a local compile callback manager. +/// +/// The given target triple will determine the ABI, and the given +/// ErrorHandlerAddress will be used by the resulting compile callback +/// manager if a compile callback fails. +std::unique_ptr<JITCompileCallbackManager> +createLocalCompileCallbackManager(const Triple &T, + TargetAddress ErrorHandlerAddress); + +/// @brief Create a local indriect stubs manager builder. +/// +/// The given target triple will determine the ABI. +std::function<std::unique_ptr<IndirectStubsManager>()> +createLocalIndirectStubsManagerBuilder(const Triple &T); + /// @brief Build a function pointer of FunctionType with the given constant /// address. /// /// Usage example: Turn a trampoline address into a function pointer constant /// for use in a stub. -Constant* createIRTypedAddress(FunctionType &FT, TargetAddress Addr); +Constant *createIRTypedAddress(FunctionType &FT, TargetAddress Addr); /// @brief Create a function pointer with the given type, name, and initializer /// in the given Module. -GlobalVariable* createImplPointer(PointerType &PT, Module &M, - const Twine &Name, Constant *Initializer); +GlobalVariable *createImplPointer(PointerType &PT, Module &M, const Twine &Name, + Constant *Initializer); /// @brief Turn a function declaration into a stub function that makes an /// indirect call using the given function pointer. @@ -373,7 +378,7 @@ void makeAllSymbolsExternallyAccessible(Module &M); /// modules with these utilities, all decls should be cloned (and added to a /// single VMap) before any bodies are moved. This will ensure that references /// between functions all refer to the versions in the new module. -Function* cloneFunctionDecl(Module &Dst, const Function &F, +Function *cloneFunctionDecl(Module &Dst, const Function &F, ValueToValueMapTy *VMap = nullptr); /// @brief Move the body of function 'F' to a cloned function declaration in a @@ -389,7 +394,7 @@ void moveFunctionBody(Function &OrigF, ValueToValueMapTy &VMap, Function *NewF = nullptr); /// @brief Clone a global variable declaration into a new module. -GlobalVariable* cloneGlobalVariableDecl(Module &Dst, const GlobalVariable &GV, +GlobalVariable *cloneGlobalVariableDecl(Module &Dst, const GlobalVariable &GV, ValueToValueMapTy *VMap = nullptr); /// @brief Move global variable GV from its parent module to cloned global @@ -406,7 +411,7 @@ void moveGlobalVariableInitializer(GlobalVariable &OrigGV, GlobalVariable *NewGV = nullptr); /// @brief Clone -GlobalAlias* cloneGlobalAliasDecl(Module &Dst, const GlobalAlias &OrigA, +GlobalAlias *cloneGlobalAliasDecl(Module &Dst, const GlobalAlias &OrigA, ValueToValueMapTy &VMap); } // End namespace orc. |