83
|
1 //===---- OrcMCJITReplacement.h - Orc based MCJIT replacement ---*- 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 // Orc based MCJIT replacement.
|
|
11 //
|
|
12 //===----------------------------------------------------------------------===//
|
|
13
|
|
14 #ifndef LLVM_LIB_EXECUTIONENGINE_ORC_ORCMCJITREPLACEMENT_H
|
|
15 #define LLVM_LIB_EXECUTIONENGINE_ORC_ORCMCJITREPLACEMENT_H
|
|
16
|
|
17 #include "llvm/ExecutionEngine/ExecutionEngine.h"
|
|
18 #include "llvm/ExecutionEngine/Orc/CompileUtils.h"
|
|
19 #include "llvm/ExecutionEngine/Orc/IRCompileLayer.h"
|
|
20 #include "llvm/ExecutionEngine/Orc/LazyEmittingLayer.h"
|
|
21 #include "llvm/ExecutionEngine/Orc/ObjectLinkingLayer.h"
|
|
22 #include "llvm/Object/Archive.h"
|
|
23
|
|
24 namespace llvm {
|
95
|
25 namespace orc {
|
83
|
26
|
|
27 class OrcMCJITReplacement : public ExecutionEngine {
|
|
28
|
95
|
29 // OrcMCJITReplacement needs to do a little extra book-keeping to ensure that
|
|
30 // Orc's automatic finalization doesn't kick in earlier than MCJIT clients are
|
|
31 // expecting - see finalizeMemory.
|
|
32 class MCJITReplacementMemMgr : public MCJITMemoryManager {
|
83
|
33 public:
|
95
|
34 MCJITReplacementMemMgr(OrcMCJITReplacement &M,
|
|
35 std::shared_ptr<MCJITMemoryManager> ClientMM)
|
|
36 : M(M), ClientMM(std::move(ClientMM)) {}
|
83
|
37
|
|
38 uint8_t *allocateCodeSection(uintptr_t Size, unsigned Alignment,
|
|
39 unsigned SectionID,
|
|
40 StringRef SectionName) override {
|
|
41 uint8_t *Addr =
|
95
|
42 ClientMM->allocateCodeSection(Size, Alignment, SectionID,
|
|
43 SectionName);
|
83
|
44 M.SectionsAllocatedSinceLastLoad.insert(Addr);
|
|
45 return Addr;
|
|
46 }
|
|
47
|
|
48 uint8_t *allocateDataSection(uintptr_t Size, unsigned Alignment,
|
|
49 unsigned SectionID, StringRef SectionName,
|
|
50 bool IsReadOnly) override {
|
95
|
51 uint8_t *Addr = ClientMM->allocateDataSection(Size, Alignment, SectionID,
|
|
52 SectionName, IsReadOnly);
|
83
|
53 M.SectionsAllocatedSinceLastLoad.insert(Addr);
|
|
54 return Addr;
|
|
55 }
|
|
56
|
|
57 void reserveAllocationSpace(uintptr_t CodeSize, uintptr_t DataSizeRO,
|
|
58 uintptr_t DataSizeRW) override {
|
95
|
59 return ClientMM->reserveAllocationSpace(CodeSize, DataSizeRO,
|
|
60 DataSizeRW);
|
83
|
61 }
|
|
62
|
|
63 bool needsToReserveAllocationSpace() override {
|
95
|
64 return ClientMM->needsToReserveAllocationSpace();
|
83
|
65 }
|
|
66
|
|
67 void registerEHFrames(uint8_t *Addr, uint64_t LoadAddr,
|
|
68 size_t Size) override {
|
95
|
69 return ClientMM->registerEHFrames(Addr, LoadAddr, Size);
|
83
|
70 }
|
|
71
|
|
72 void deregisterEHFrames(uint8_t *Addr, uint64_t LoadAddr,
|
|
73 size_t Size) override {
|
95
|
74 return ClientMM->deregisterEHFrames(Addr, LoadAddr, Size);
|
83
|
75 }
|
|
76
|
|
77 void notifyObjectLoaded(ExecutionEngine *EE,
|
|
78 const object::ObjectFile &O) override {
|
95
|
79 return ClientMM->notifyObjectLoaded(EE, O);
|
83
|
80 }
|
|
81
|
|
82 bool finalizeMemory(std::string *ErrMsg = nullptr) override {
|
|
83 // Each set of objects loaded will be finalized exactly once, but since
|
|
84 // symbol lookup during relocation may recursively trigger the
|
|
85 // loading/relocation of other modules, and since we're forwarding all
|
|
86 // finalizeMemory calls to a single underlying memory manager, we need to
|
|
87 // defer forwarding the call on until all necessary objects have been
|
|
88 // loaded. Otherwise, during the relocation of a leaf object, we will end
|
|
89 // up finalizing memory, causing a crash further up the stack when we
|
|
90 // attempt to apply relocations to finalized memory.
|
|
91 // To avoid finalizing too early, look at how many objects have been
|
|
92 // loaded but not yet finalized. This is a bit of a hack that relies on
|
|
93 // the fact that we're lazily emitting object files: The only way you can
|
|
94 // get more than one set of objects loaded but not yet finalized is if
|
|
95 // they were loaded during relocation of another set.
|
|
96 if (M.UnfinalizedSections.size() == 1)
|
95
|
97 return ClientMM->finalizeMemory(ErrMsg);
|
83
|
98 return false;
|
|
99 }
|
|
100
|
|
101 private:
|
|
102 OrcMCJITReplacement &M;
|
95
|
103 std::shared_ptr<MCJITMemoryManager> ClientMM;
|
|
104 };
|
|
105
|
|
106 class LinkingResolver : public RuntimeDyld::SymbolResolver {
|
|
107 public:
|
|
108 LinkingResolver(OrcMCJITReplacement &M) : M(M) {}
|
|
109
|
|
110 RuntimeDyld::SymbolInfo findSymbol(const std::string &Name) override {
|
|
111 return M.findMangledSymbol(Name);
|
|
112 }
|
|
113
|
|
114 RuntimeDyld::SymbolInfo
|
|
115 findSymbolInLogicalDylib(const std::string &Name) override {
|
|
116 return M.ClientResolver->findSymbolInLogicalDylib(Name);
|
|
117 }
|
|
118
|
|
119 private:
|
|
120 OrcMCJITReplacement &M;
|
83
|
121 };
|
|
122
|
|
123 private:
|
95
|
124
|
83
|
125 static ExecutionEngine *
|
|
126 createOrcMCJITReplacement(std::string *ErrorMsg,
|
95
|
127 std::shared_ptr<MCJITMemoryManager> MemMgr,
|
|
128 std::shared_ptr<RuntimeDyld::SymbolResolver> Resolver,
|
|
129 std::unique_ptr<TargetMachine> TM) {
|
|
130 return new OrcMCJITReplacement(std::move(MemMgr), std::move(Resolver),
|
|
131 std::move(TM));
|
83
|
132 }
|
|
133
|
|
134 public:
|
|
135 static void Register() {
|
|
136 OrcMCJITReplacementCtor = createOrcMCJITReplacement;
|
|
137 }
|
|
138
|
95
|
139 OrcMCJITReplacement(
|
|
140 std::shared_ptr<MCJITMemoryManager> MemMgr,
|
|
141 std::shared_ptr<RuntimeDyld::SymbolResolver> ClientResolver,
|
|
142 std::unique_ptr<TargetMachine> TM)
|
|
143 : ExecutionEngine(TM->createDataLayout()), TM(std::move(TM)),
|
|
144 MemMgr(*this, std::move(MemMgr)), Resolver(*this),
|
|
145 ClientResolver(std::move(ClientResolver)), NotifyObjectLoaded(*this),
|
|
146 NotifyFinalized(*this),
|
|
147 ObjectLayer(NotifyObjectLoaded, NotifyFinalized),
|
83
|
148 CompileLayer(ObjectLayer, SimpleCompiler(*this->TM)),
|
95
|
149 LazyEmitLayer(CompileLayer) {}
|
83
|
150
|
|
151 void addModule(std::unique_ptr<Module> M) override {
|
|
152
|
|
153 // If this module doesn't have a DataLayout attached then attach the
|
|
154 // default.
|
95
|
155 if (M->getDataLayout().isDefault()) {
|
83
|
156 M->setDataLayout(getDataLayout());
|
95
|
157 } else {
|
|
158 assert(M->getDataLayout() == getDataLayout() && "DataLayout Mismatch");
|
|
159 }
|
83
|
160 Modules.push_back(std::move(M));
|
|
161 std::vector<Module *> Ms;
|
|
162 Ms.push_back(&*Modules.back());
|
95
|
163 LazyEmitLayer.addModuleSet(std::move(Ms), &MemMgr, &Resolver);
|
83
|
164 }
|
|
165
|
|
166 void addObjectFile(std::unique_ptr<object::ObjectFile> O) override {
|
|
167 std::vector<std::unique_ptr<object::ObjectFile>> Objs;
|
|
168 Objs.push_back(std::move(O));
|
95
|
169 ObjectLayer.addObjectSet(std::move(Objs), &MemMgr, &Resolver);
|
83
|
170 }
|
|
171
|
|
172 void addObjectFile(object::OwningBinary<object::ObjectFile> O) override {
|
|
173 std::unique_ptr<object::ObjectFile> Obj;
|
|
174 std::unique_ptr<MemoryBuffer> Buf;
|
|
175 std::tie(Obj, Buf) = O.takeBinary();
|
|
176 std::vector<std::unique_ptr<object::ObjectFile>> Objs;
|
|
177 Objs.push_back(std::move(Obj));
|
|
178 auto H =
|
95
|
179 ObjectLayer.addObjectSet(std::move(Objs), &MemMgr, &Resolver);
|
83
|
180
|
|
181 std::vector<std::unique_ptr<MemoryBuffer>> Bufs;
|
|
182 Bufs.push_back(std::move(Buf));
|
|
183 ObjectLayer.takeOwnershipOfBuffers(H, std::move(Bufs));
|
|
184 }
|
|
185
|
|
186 void addArchive(object::OwningBinary<object::Archive> A) override {
|
|
187 Archives.push_back(std::move(A));
|
|
188 }
|
|
189
|
|
190 uint64_t getSymbolAddress(StringRef Name) {
|
95
|
191 return findSymbol(Name).getAddress();
|
|
192 }
|
|
193
|
|
194 RuntimeDyld::SymbolInfo findSymbol(StringRef Name) {
|
|
195 return findMangledSymbol(Mangle(Name));
|
83
|
196 }
|
|
197
|
|
198 void finalizeObject() override {
|
|
199 // This is deprecated - Aim to remove in ExecutionEngine.
|
|
200 // REMOVE IF POSSIBLE - Doesn't make sense for New JIT.
|
|
201 }
|
|
202
|
|
203 void mapSectionAddress(const void *LocalAddress,
|
|
204 uint64_t TargetAddress) override {
|
|
205 for (auto &P : UnfinalizedSections)
|
|
206 if (P.second.count(LocalAddress))
|
|
207 ObjectLayer.mapSectionAddress(P.first, LocalAddress, TargetAddress);
|
|
208 }
|
|
209
|
|
210 uint64_t getGlobalValueAddress(const std::string &Name) override {
|
|
211 return getSymbolAddress(Name);
|
|
212 }
|
|
213
|
|
214 uint64_t getFunctionAddress(const std::string &Name) override {
|
|
215 return getSymbolAddress(Name);
|
|
216 }
|
|
217
|
|
218 void *getPointerToFunction(Function *F) override {
|
|
219 uint64_t FAddr = getSymbolAddress(F->getName());
|
|
220 return reinterpret_cast<void *>(static_cast<uintptr_t>(FAddr));
|
|
221 }
|
|
222
|
|
223 void *getPointerToNamedFunction(StringRef Name,
|
|
224 bool AbortOnFailure = true) override {
|
|
225 uint64_t Addr = getSymbolAddress(Name);
|
|
226 if (!Addr && AbortOnFailure)
|
|
227 llvm_unreachable("Missing symbol!");
|
|
228 return reinterpret_cast<void *>(static_cast<uintptr_t>(Addr));
|
|
229 }
|
|
230
|
|
231 GenericValue runFunction(Function *F,
|
95
|
232 ArrayRef<GenericValue> ArgValues) override;
|
83
|
233
|
|
234 void setObjectCache(ObjectCache *NewCache) override {
|
|
235 CompileLayer.setObjectCache(NewCache);
|
|
236 }
|
|
237
|
|
238 private:
|
|
239
|
95
|
240 RuntimeDyld::SymbolInfo findMangledSymbol(StringRef Name) {
|
|
241 if (auto Sym = LazyEmitLayer.findSymbol(Name, false))
|
|
242 return RuntimeDyld::SymbolInfo(Sym.getAddress(), Sym.getFlags());
|
|
243 if (auto Sym = ClientResolver->findSymbol(Name))
|
|
244 return RuntimeDyld::SymbolInfo(Sym.getAddress(), Sym.getFlags());
|
|
245 if (auto Sym = scanArchives(Name))
|
|
246 return RuntimeDyld::SymbolInfo(Sym.getAddress(), Sym.getFlags());
|
|
247
|
|
248 return nullptr;
|
83
|
249 }
|
|
250
|
95
|
251 JITSymbol scanArchives(StringRef Name) {
|
83
|
252 for (object::OwningBinary<object::Archive> &OB : Archives) {
|
|
253 object::Archive *A = OB.getBinary();
|
|
254 // Look for our symbols in each Archive
|
|
255 object::Archive::child_iterator ChildIt = A->findSym(Name);
|
|
256 if (ChildIt != A->child_end()) {
|
|
257 // FIXME: Support nested archives?
|
|
258 ErrorOr<std::unique_ptr<object::Binary>> ChildBinOrErr =
|
|
259 ChildIt->getAsBinary();
|
|
260 if (ChildBinOrErr.getError())
|
|
261 continue;
|
|
262 std::unique_ptr<object::Binary> &ChildBin = ChildBinOrErr.get();
|
|
263 if (ChildBin->isObject()) {
|
|
264 std::vector<std::unique_ptr<object::ObjectFile>> ObjSet;
|
|
265 ObjSet.push_back(std::unique_ptr<object::ObjectFile>(
|
|
266 static_cast<object::ObjectFile *>(ChildBin.release())));
|
95
|
267 ObjectLayer.addObjectSet(std::move(ObjSet), &MemMgr, &Resolver);
|
|
268 if (auto Sym = ObjectLayer.findSymbol(Name, true))
|
|
269 return Sym;
|
83
|
270 }
|
|
271 }
|
|
272 }
|
95
|
273 return nullptr;
|
83
|
274 }
|
|
275
|
|
276 class NotifyObjectLoadedT {
|
|
277 public:
|
|
278 typedef std::vector<std::unique_ptr<object::ObjectFile>> ObjListT;
|
|
279 typedef std::vector<std::unique_ptr<RuntimeDyld::LoadedObjectInfo>>
|
|
280 LoadedObjInfoListT;
|
|
281
|
|
282 NotifyObjectLoadedT(OrcMCJITReplacement &M) : M(M) {}
|
|
283
|
|
284 void operator()(ObjectLinkingLayerBase::ObjSetHandleT H,
|
|
285 const ObjListT &Objects,
|
|
286 const LoadedObjInfoListT &Infos) const {
|
|
287 M.UnfinalizedSections[H] = std::move(M.SectionsAllocatedSinceLastLoad);
|
|
288 M.SectionsAllocatedSinceLastLoad = SectionAddrSet();
|
|
289 assert(Objects.size() == Infos.size() &&
|
|
290 "Incorrect number of Infos for Objects.");
|
|
291 for (unsigned I = 0; I < Objects.size(); ++I)
|
95
|
292 M.MemMgr.notifyObjectLoaded(&M, *Objects[I]);
|
|
293 }
|
83
|
294
|
|
295 private:
|
|
296 OrcMCJITReplacement &M;
|
|
297 };
|
|
298
|
|
299 class NotifyFinalizedT {
|
|
300 public:
|
|
301 NotifyFinalizedT(OrcMCJITReplacement &M) : M(M) {}
|
|
302 void operator()(ObjectLinkingLayerBase::ObjSetHandleT H) {
|
|
303 M.UnfinalizedSections.erase(H);
|
|
304 }
|
|
305
|
|
306 private:
|
|
307 OrcMCJITReplacement &M;
|
|
308 };
|
|
309
|
|
310 std::string Mangle(StringRef Name) {
|
|
311 std::string MangledName;
|
|
312 {
|
|
313 raw_string_ostream MangledNameStream(MangledName);
|
95
|
314 Mang.getNameWithPrefix(MangledNameStream, Name, getDataLayout());
|
83
|
315 }
|
|
316 return MangledName;
|
|
317 }
|
|
318
|
|
319 typedef ObjectLinkingLayer<NotifyObjectLoadedT> ObjectLayerT;
|
|
320 typedef IRCompileLayer<ObjectLayerT> CompileLayerT;
|
|
321 typedef LazyEmittingLayer<CompileLayerT> LazyEmitLayerT;
|
|
322
|
|
323 std::unique_ptr<TargetMachine> TM;
|
95
|
324 MCJITReplacementMemMgr MemMgr;
|
|
325 LinkingResolver Resolver;
|
|
326 std::shared_ptr<RuntimeDyld::SymbolResolver> ClientResolver;
|
83
|
327 Mangler Mang;
|
|
328
|
|
329 NotifyObjectLoadedT NotifyObjectLoaded;
|
|
330 NotifyFinalizedT NotifyFinalized;
|
|
331
|
|
332 ObjectLayerT ObjectLayer;
|
|
333 CompileLayerT CompileLayer;
|
|
334 LazyEmitLayerT LazyEmitLayer;
|
|
335
|
|
336 // We need to store ObjLayerT::ObjSetHandles for each of the object sets
|
|
337 // that have been emitted but not yet finalized so that we can forward the
|
|
338 // mapSectionAddress calls appropriately.
|
|
339 typedef std::set<const void *> SectionAddrSet;
|
|
340 struct ObjSetHandleCompare {
|
|
341 bool operator()(ObjectLayerT::ObjSetHandleT H1,
|
|
342 ObjectLayerT::ObjSetHandleT H2) const {
|
|
343 return &*H1 < &*H2;
|
|
344 }
|
|
345 };
|
|
346 SectionAddrSet SectionsAllocatedSinceLastLoad;
|
|
347 std::map<ObjectLayerT::ObjSetHandleT, SectionAddrSet, ObjSetHandleCompare>
|
|
348 UnfinalizedSections;
|
|
349
|
|
350 std::vector<object::OwningBinary<object::Archive>> Archives;
|
|
351 };
|
95
|
352
|
|
353 } // End namespace orc.
|
|
354 } // End namespace llvm.
|
83
|
355
|
|
356 #endif // LLVM_LIB_EXECUTIONENGINE_ORC_MCJITREPLACEMENT_H
|