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