Lang Hames | 9d7a269 | 2016-01-11 16:35:55 +0000 | [diff] [blame] | 1 | //===-- RemoteJITUtils.h - Utilities for remote-JITing with LLI -*- C++ -*-===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
| 10 | // Utilities for remote-JITing with LLI. |
| 11 | // |
| 12 | //===----------------------------------------------------------------------===// |
| 13 | |
| 14 | #ifndef LLVM_TOOLS_LLI_REMOTEJITUTILS_H |
| 15 | #define LLVM_TOOLS_LLI_REMOTEJITUTILS_H |
| 16 | |
| 17 | #include "llvm/ExecutionEngine/Orc/RPCChannel.h" |
| 18 | #include "llvm/ExecutionEngine/RTDyldMemoryManager.h" |
Lang Hames | 3fde652 | 2016-04-18 19:55:43 +0000 | [diff] [blame^] | 19 | #include <mutex> |
Lang Hames | 9d7a269 | 2016-01-11 16:35:55 +0000 | [diff] [blame] | 20 | |
| 21 | #if !defined(_MSC_VER) && !defined(__MINGW32__) |
| 22 | #include <unistd.h> |
| 23 | #else |
| 24 | #include <io.h> |
| 25 | #endif |
| 26 | |
| 27 | /// RPC channel that reads from and writes from file descriptors. |
Lang Hames | 22c5af7 | 2016-01-15 21:35:24 +0000 | [diff] [blame] | 28 | class FDRPCChannel final : public llvm::orc::remote::RPCChannel { |
Lang Hames | 9d7a269 | 2016-01-11 16:35:55 +0000 | [diff] [blame] | 29 | public: |
| 30 | FDRPCChannel(int InFD, int OutFD) : InFD(InFD), OutFD(OutFD) {} |
| 31 | |
| 32 | std::error_code readBytes(char *Dst, unsigned Size) override { |
| 33 | assert(Dst && "Attempt to read into null."); |
| 34 | ssize_t ReadResult = ::read(InFD, Dst, Size); |
NAKAMURA Takumi | 65c4eaf | 2016-01-12 01:23:30 +0000 | [diff] [blame] | 35 | if (ReadResult != (ssize_t)Size) |
Lang Hames | 9d7a269 | 2016-01-11 16:35:55 +0000 | [diff] [blame] | 36 | return std::error_code(errno, std::generic_category()); |
| 37 | return std::error_code(); |
| 38 | } |
| 39 | |
| 40 | std::error_code appendBytes(const char *Src, unsigned Size) override { |
| 41 | assert(Src && "Attempt to append from null."); |
| 42 | ssize_t WriteResult = ::write(OutFD, Src, Size); |
NAKAMURA Takumi | 65c4eaf | 2016-01-12 01:23:30 +0000 | [diff] [blame] | 43 | if (WriteResult != (ssize_t)Size) |
Lang Hames | 9d7a269 | 2016-01-11 16:35:55 +0000 | [diff] [blame] | 44 | std::error_code(errno, std::generic_category()); |
| 45 | return std::error_code(); |
| 46 | } |
| 47 | |
| 48 | std::error_code send() override { return std::error_code(); } |
| 49 | |
| 50 | private: |
| 51 | int InFD, OutFD; |
| 52 | }; |
| 53 | |
| 54 | // launch the remote process (see lli.cpp) and return a channel to it. |
| 55 | std::unique_ptr<FDRPCChannel> launchRemote(); |
| 56 | |
| 57 | namespace llvm { |
| 58 | |
| 59 | // ForwardingMM - Adapter to connect MCJIT to Orc's Remote memory manager. |
| 60 | class ForwardingMemoryManager : public llvm::RTDyldMemoryManager { |
| 61 | public: |
| 62 | void setMemMgr(std::unique_ptr<RuntimeDyld::MemoryManager> MemMgr) { |
| 63 | this->MemMgr = std::move(MemMgr); |
| 64 | } |
| 65 | |
| 66 | void setResolver(std::unique_ptr<RuntimeDyld::SymbolResolver> Resolver) { |
| 67 | this->Resolver = std::move(Resolver); |
| 68 | } |
| 69 | |
| 70 | uint8_t *allocateCodeSection(uintptr_t Size, unsigned Alignment, |
| 71 | unsigned SectionID, |
| 72 | StringRef SectionName) override { |
| 73 | return MemMgr->allocateCodeSection(Size, Alignment, SectionID, SectionName); |
| 74 | } |
| 75 | |
| 76 | uint8_t *allocateDataSection(uintptr_t Size, unsigned Alignment, |
| 77 | unsigned SectionID, StringRef SectionName, |
| 78 | bool IsReadOnly) override { |
| 79 | return MemMgr->allocateDataSection(Size, Alignment, SectionID, SectionName, |
| 80 | IsReadOnly); |
| 81 | } |
| 82 | |
| 83 | void reserveAllocationSpace(uintptr_t CodeSize, uint32_t CodeAlign, |
| 84 | uintptr_t RODataSize, uint32_t RODataAlign, |
| 85 | uintptr_t RWDataSize, |
| 86 | uint32_t RWDataAlign) override { |
| 87 | MemMgr->reserveAllocationSpace(CodeSize, CodeAlign, RODataSize, RODataAlign, |
| 88 | RWDataSize, RWDataAlign); |
| 89 | } |
| 90 | |
| 91 | bool needsToReserveAllocationSpace() override { |
| 92 | return MemMgr->needsToReserveAllocationSpace(); |
| 93 | } |
| 94 | |
| 95 | void registerEHFrames(uint8_t *Addr, uint64_t LoadAddr, |
| 96 | size_t Size) override { |
| 97 | MemMgr->registerEHFrames(Addr, LoadAddr, Size); |
| 98 | } |
| 99 | |
| 100 | void deregisterEHFrames(uint8_t *Addr, uint64_t LoadAddr, |
| 101 | size_t Size) override { |
| 102 | MemMgr->deregisterEHFrames(Addr, LoadAddr, Size); |
| 103 | } |
| 104 | |
| 105 | bool finalizeMemory(std::string *ErrMsg = nullptr) override { |
| 106 | return MemMgr->finalizeMemory(ErrMsg); |
| 107 | } |
| 108 | |
| 109 | void notifyObjectLoaded(RuntimeDyld &RTDyld, |
| 110 | const object::ObjectFile &Obj) override { |
| 111 | MemMgr->notifyObjectLoaded(RTDyld, Obj); |
| 112 | } |
| 113 | |
| 114 | // Don't hide the sibling notifyObjectLoaded from RTDyldMemoryManager. |
| 115 | using RTDyldMemoryManager::notifyObjectLoaded; |
| 116 | |
| 117 | RuntimeDyld::SymbolInfo findSymbol(const std::string &Name) override { |
| 118 | return Resolver->findSymbol(Name); |
| 119 | } |
| 120 | |
| 121 | RuntimeDyld::SymbolInfo |
| 122 | findSymbolInLogicalDylib(const std::string &Name) override { |
| 123 | return Resolver->findSymbolInLogicalDylib(Name); |
| 124 | } |
| 125 | |
| 126 | private: |
| 127 | std::unique_ptr<RuntimeDyld::MemoryManager> MemMgr; |
| 128 | std::unique_ptr<RuntimeDyld::SymbolResolver> Resolver; |
| 129 | }; |
| 130 | } |
| 131 | |
| 132 | #endif |