1 //===-- RemoteJITUtils.h - Utilities for remote-JITing with LLI -*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // Utilities for remote-JITing with LLI.
12 //===----------------------------------------------------------------------===//
14 #ifndef LLVM_TOOLS_LLI_REMOTEJITUTILS_H
15 #define LLVM_TOOLS_LLI_REMOTEJITUTILS_H
17 #include "llvm/ExecutionEngine/Orc/RPCChannel.h"
18 #include "llvm/ExecutionEngine/RTDyldMemoryManager.h"
21 #if !defined(_MSC_VER) && !defined(__MINGW32__)
27 /// RPC channel that reads from and writes from file descriptors.
28 class FDRPCChannel final : public llvm::orc::remote::RPCChannel {
30 FDRPCChannel(int InFD, int OutFD) : InFD(InFD), OutFD(OutFD) {}
32 llvm::Error readBytes(char *Dst, unsigned Size) override {
33 assert(Dst && "Attempt to read into null.");
34 ssize_t Completed = 0;
35 while (Completed < static_cast<ssize_t>(Size)) {
36 ssize_t Read = ::read(InFD, Dst + Completed, Size - Completed);
39 if (ErrNo == EAGAIN || ErrNo == EINTR)
42 return llvm::errorCodeToError(
43 std::error_code(errno, std::generic_category()));
47 return llvm::Error::success();
50 llvm::Error appendBytes(const char *Src, unsigned Size) override {
51 assert(Src && "Attempt to append from null.");
52 ssize_t Completed = 0;
53 while (Completed < static_cast<ssize_t>(Size)) {
54 ssize_t Written = ::write(OutFD, Src + Completed, Size - Completed);
57 if (ErrNo == EAGAIN || ErrNo == EINTR)
60 return llvm::errorCodeToError(
61 std::error_code(errno, std::generic_category()));
65 return llvm::Error::success();
68 llvm::Error send() override { return llvm::Error::success(); }
74 // launch the remote process (see lli.cpp) and return a channel to it.
75 std::unique_ptr<FDRPCChannel> launchRemote();
79 // ForwardingMM - Adapter to connect MCJIT to Orc's Remote memory manager.
80 class ForwardingMemoryManager : public llvm::RTDyldMemoryManager {
82 void setMemMgr(std::unique_ptr<RuntimeDyld::MemoryManager> MemMgr) {
83 this->MemMgr = std::move(MemMgr);
86 void setResolver(std::unique_ptr<RuntimeDyld::SymbolResolver> Resolver) {
87 this->Resolver = std::move(Resolver);
90 uint8_t *allocateCodeSection(uintptr_t Size, unsigned Alignment,
92 StringRef SectionName) override {
93 return MemMgr->allocateCodeSection(Size, Alignment, SectionID, SectionName);
96 uint8_t *allocateDataSection(uintptr_t Size, unsigned Alignment,
97 unsigned SectionID, StringRef SectionName,
98 bool IsReadOnly) override {
99 return MemMgr->allocateDataSection(Size, Alignment, SectionID, SectionName,
103 void reserveAllocationSpace(uintptr_t CodeSize, uint32_t CodeAlign,
104 uintptr_t RODataSize, uint32_t RODataAlign,
105 uintptr_t RWDataSize,
106 uint32_t RWDataAlign) override {
107 MemMgr->reserveAllocationSpace(CodeSize, CodeAlign, RODataSize, RODataAlign,
108 RWDataSize, RWDataAlign);
111 bool needsToReserveAllocationSpace() override {
112 return MemMgr->needsToReserveAllocationSpace();
115 void registerEHFrames(uint8_t *Addr, uint64_t LoadAddr,
116 size_t Size) override {
117 MemMgr->registerEHFrames(Addr, LoadAddr, Size);
120 void deregisterEHFrames(uint8_t *Addr, uint64_t LoadAddr,
121 size_t Size) override {
122 MemMgr->deregisterEHFrames(Addr, LoadAddr, Size);
125 bool finalizeMemory(std::string *ErrMsg = nullptr) override {
126 return MemMgr->finalizeMemory(ErrMsg);
129 void notifyObjectLoaded(RuntimeDyld &RTDyld,
130 const object::ObjectFile &Obj) override {
131 MemMgr->notifyObjectLoaded(RTDyld, Obj);
134 // Don't hide the sibling notifyObjectLoaded from RTDyldMemoryManager.
135 using RTDyldMemoryManager::notifyObjectLoaded;
137 RuntimeDyld::SymbolInfo findSymbol(const std::string &Name) override {
138 return Resolver->findSymbol(Name);
141 RuntimeDyld::SymbolInfo
142 findSymbolInLogicalDylib(const std::string &Name) override {
143 return Resolver->findSymbolInLogicalDylib(Name);
147 std::unique_ptr<RuntimeDyld::MemoryManager> MemMgr;
148 std::unique_ptr<RuntimeDyld::SymbolResolver> Resolver;