blob: 89a5142025672d0c4c84709f1461722c0dfe2635 [file] [log] [blame]
Lang Hames9d7a2692016-01-11 16:35:55 +00001//===-- 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
Lang Hames1f2bf2d2016-11-11 21:42:09 +000017#include "llvm/ExecutionEngine/Orc/RawByteChannel.h"
Lang Hames9d7a2692016-01-11 16:35:55 +000018#include "llvm/ExecutionEngine/RTDyldMemoryManager.h"
Lang Hames3fde6522016-04-18 19:55:43 +000019#include <mutex>
Lang Hames9d7a2692016-01-11 16:35:55 +000020
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 Hames1f2bf2d2016-11-11 21:42:09 +000028class FDRawChannel final : public llvm::orc::rpc::RawByteChannel {
Lang Hames9d7a2692016-01-11 16:35:55 +000029public:
Lang Hames1f2bf2d2016-11-11 21:42:09 +000030 FDRawChannel(int InFD, int OutFD) : InFD(InFD), OutFD(OutFD) {}
Lang Hames9d7a2692016-01-11 16:35:55 +000031
Lang Hamesef5a0ee2016-04-25 19:56:45 +000032 llvm::Error readBytes(char *Dst, unsigned Size) override {
Lang Hames9d7a2692016-01-11 16:35:55 +000033 assert(Dst && "Attempt to read into null.");
Lang Hamesef5a0ee2016-04-25 19:56:45 +000034 ssize_t Completed = 0;
Lang Hamese246c432016-04-26 01:45:25 +000035 while (Completed < static_cast<ssize_t>(Size)) {
Lang Hamesef5a0ee2016-04-25 19:56:45 +000036 ssize_t Read = ::read(InFD, Dst + Completed, Size - Completed);
37 if (Read <= 0) {
38 auto ErrNo = errno;
39 if (ErrNo == EAGAIN || ErrNo == EINTR)
40 continue;
41 else
42 return llvm::errorCodeToError(
43 std::error_code(errno, std::generic_category()));
44 }
45 Completed += Read;
46 }
47 return llvm::Error::success();
Lang Hames9d7a2692016-01-11 16:35:55 +000048 }
49
Lang Hamesef5a0ee2016-04-25 19:56:45 +000050 llvm::Error appendBytes(const char *Src, unsigned Size) override {
Lang Hames9d7a2692016-01-11 16:35:55 +000051 assert(Src && "Attempt to append from null.");
Lang Hamesef5a0ee2016-04-25 19:56:45 +000052 ssize_t Completed = 0;
Lang Hamese246c432016-04-26 01:45:25 +000053 while (Completed < static_cast<ssize_t>(Size)) {
Lang Hamesef5a0ee2016-04-25 19:56:45 +000054 ssize_t Written = ::write(OutFD, Src + Completed, Size - Completed);
55 if (Written < 0) {
56 auto ErrNo = errno;
57 if (ErrNo == EAGAIN || ErrNo == EINTR)
58 continue;
59 else
60 return llvm::errorCodeToError(
61 std::error_code(errno, std::generic_category()));
62 }
63 Completed += Written;
64 }
65 return llvm::Error::success();
Lang Hames9d7a2692016-01-11 16:35:55 +000066 }
67
Lang Hamesef5a0ee2016-04-25 19:56:45 +000068 llvm::Error send() override { return llvm::Error::success(); }
Lang Hames9d7a2692016-01-11 16:35:55 +000069
70private:
71 int InFD, OutFD;
72};
73
74// launch the remote process (see lli.cpp) and return a channel to it.
Lang Hames1f2bf2d2016-11-11 21:42:09 +000075std::unique_ptr<FDRawChannel> launchRemote();
Lang Hames9d7a2692016-01-11 16:35:55 +000076
77namespace llvm {
78
Lang Hames1f2bf2d2016-11-11 21:42:09 +000079// ForwardingMM - Adapter to connect MCJIT to Orc's Remote8
80// memory manager.
Lang Hames9d7a2692016-01-11 16:35:55 +000081class ForwardingMemoryManager : public llvm::RTDyldMemoryManager {
82public:
83 void setMemMgr(std::unique_ptr<RuntimeDyld::MemoryManager> MemMgr) {
84 this->MemMgr = std::move(MemMgr);
85 }
86
Lang Hamesad4a9112016-08-01 20:49:11 +000087 void setResolver(std::unique_ptr<JITSymbolResolver> Resolver) {
Lang Hames9d7a2692016-01-11 16:35:55 +000088 this->Resolver = std::move(Resolver);
89 }
90
91 uint8_t *allocateCodeSection(uintptr_t Size, unsigned Alignment,
92 unsigned SectionID,
93 StringRef SectionName) override {
94 return MemMgr->allocateCodeSection(Size, Alignment, SectionID, SectionName);
95 }
96
97 uint8_t *allocateDataSection(uintptr_t Size, unsigned Alignment,
98 unsigned SectionID, StringRef SectionName,
99 bool IsReadOnly) override {
100 return MemMgr->allocateDataSection(Size, Alignment, SectionID, SectionName,
101 IsReadOnly);
102 }
103
104 void reserveAllocationSpace(uintptr_t CodeSize, uint32_t CodeAlign,
105 uintptr_t RODataSize, uint32_t RODataAlign,
106 uintptr_t RWDataSize,
107 uint32_t RWDataAlign) override {
108 MemMgr->reserveAllocationSpace(CodeSize, CodeAlign, RODataSize, RODataAlign,
109 RWDataSize, RWDataAlign);
110 }
111
112 bool needsToReserveAllocationSpace() override {
113 return MemMgr->needsToReserveAllocationSpace();
114 }
115
116 void registerEHFrames(uint8_t *Addr, uint64_t LoadAddr,
117 size_t Size) override {
118 MemMgr->registerEHFrames(Addr, LoadAddr, Size);
119 }
120
121 void deregisterEHFrames(uint8_t *Addr, uint64_t LoadAddr,
122 size_t Size) override {
123 MemMgr->deregisterEHFrames(Addr, LoadAddr, Size);
124 }
125
126 bool finalizeMemory(std::string *ErrMsg = nullptr) override {
127 return MemMgr->finalizeMemory(ErrMsg);
128 }
129
130 void notifyObjectLoaded(RuntimeDyld &RTDyld,
131 const object::ObjectFile &Obj) override {
132 MemMgr->notifyObjectLoaded(RTDyld, Obj);
133 }
134
135 // Don't hide the sibling notifyObjectLoaded from RTDyldMemoryManager.
136 using RTDyldMemoryManager::notifyObjectLoaded;
137
Lang Hamesad4a9112016-08-01 20:49:11 +0000138 JITSymbol findSymbol(const std::string &Name) override {
Lang Hames9d7a2692016-01-11 16:35:55 +0000139 return Resolver->findSymbol(Name);
140 }
141
Lang Hamesad4a9112016-08-01 20:49:11 +0000142 JITSymbol
Lang Hames9d7a2692016-01-11 16:35:55 +0000143 findSymbolInLogicalDylib(const std::string &Name) override {
144 return Resolver->findSymbolInLogicalDylib(Name);
145 }
146
147private:
148 std::unique_ptr<RuntimeDyld::MemoryManager> MemMgr;
Lang Hamesad4a9112016-08-01 20:49:11 +0000149 std::unique_ptr<JITSymbolResolver> Resolver;
Lang Hames9d7a2692016-01-11 16:35:55 +0000150};
151}
152
153#endif