1 //===---- OrcMCJITReplacement.h - Orc based MCJIT replacement ---*- 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 // Orc based MCJIT replacement.
12 //===----------------------------------------------------------------------===//
14 #ifndef LLVM_LIB_EXECUTIONENGINE_ORC_ORCMCJITREPLACEMENT_H
15 #define LLVM_LIB_EXECUTIONENGINE_ORC_ORCMCJITREPLACEMENT_H
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"
27 class OrcMCJITReplacement : public ExecutionEngine {
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 {
34 MCJITReplacementMemMgr(OrcMCJITReplacement &M,
35 std::shared_ptr<MCJITMemoryManager> ClientMM)
36 : M(M), ClientMM(std::move(ClientMM)) {}
38 uint8_t *allocateCodeSection(uintptr_t Size, unsigned Alignment,
40 StringRef SectionName) override {
42 ClientMM->allocateCodeSection(Size, Alignment, SectionID,
44 M.SectionsAllocatedSinceLastLoad.insert(Addr);
48 uint8_t *allocateDataSection(uintptr_t Size, unsigned Alignment,
49 unsigned SectionID, StringRef SectionName,
50 bool IsReadOnly) override {
51 uint8_t *Addr = ClientMM->allocateDataSection(Size, Alignment, SectionID,
52 SectionName, IsReadOnly);
53 M.SectionsAllocatedSinceLastLoad.insert(Addr);
57 void reserveAllocationSpace(uintptr_t CodeSize, uintptr_t DataSizeRO,
58 uintptr_t DataSizeRW) override {
59 return ClientMM->reserveAllocationSpace(CodeSize, DataSizeRO,
63 bool needsToReserveAllocationSpace() override {
64 return ClientMM->needsToReserveAllocationSpace();
67 void registerEHFrames(uint8_t *Addr, uint64_t LoadAddr,
68 size_t Size) override {
69 return ClientMM->registerEHFrames(Addr, LoadAddr, Size);
72 void deregisterEHFrames(uint8_t *Addr, uint64_t LoadAddr,
73 size_t Size) override {
74 return ClientMM->deregisterEHFrames(Addr, LoadAddr, Size);
77 void notifyObjectLoaded(ExecutionEngine *EE,
78 const object::ObjectFile &O) override {
79 return ClientMM->notifyObjectLoaded(EE, O);
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)
97 return ClientMM->finalizeMemory(ErrMsg);
102 OrcMCJITReplacement &M;
103 std::shared_ptr<MCJITMemoryManager> ClientMM;
106 class LinkingResolver : public RuntimeDyld::SymbolResolver {
108 LinkingResolver(OrcMCJITReplacement &M) : M(M) {}
110 RuntimeDyld::SymbolInfo findSymbol(const std::string &Name) override {
111 return M.findMangledSymbol(Name);
114 RuntimeDyld::SymbolInfo
115 findSymbolInLogicalDylib(const std::string &Name) override {
116 return M.ClientResolver->findSymbolInLogicalDylib(Name);
120 OrcMCJITReplacement &M;
125 static ExecutionEngine *
126 createOrcMCJITReplacement(std::string *ErrorMsg,
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),
135 static void Register() {
136 OrcMCJITReplacementCtor = createOrcMCJITReplacement;
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),
148 CompileLayer(ObjectLayer, SimpleCompiler(*this->TM)),
149 LazyEmitLayer(CompileLayer) {}
151 void addModule(std::unique_ptr<Module> M) override {
153 // If this module doesn't have a DataLayout attached then attach the
155 if (M->getDataLayout().isDefault()) {
156 M->setDataLayout(getDataLayout());
158 assert(M->getDataLayout() == getDataLayout() && "DataLayout Mismatch");
160 Modules.push_back(std::move(M));
161 std::vector<Module *> Ms;
162 Ms.push_back(&*Modules.back());
163 LazyEmitLayer.addModuleSet(std::move(Ms), &MemMgr, &Resolver);
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));
169 ObjectLayer.addObjectSet(std::move(Objs), &MemMgr, &Resolver);
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 ObjectLayer.addObjectSet(std::move(Objs), &MemMgr, &Resolver);
181 void addArchive(object::OwningBinary<object::Archive> A) override {
182 Archives.push_back(std::move(A));
185 uint64_t getSymbolAddress(StringRef Name) {
186 return findSymbol(Name).getAddress();
189 RuntimeDyld::SymbolInfo findSymbol(StringRef Name) {
190 return findMangledSymbol(Mangle(Name));
193 void finalizeObject() override {
194 // This is deprecated - Aim to remove in ExecutionEngine.
195 // REMOVE IF POSSIBLE - Doesn't make sense for New JIT.
198 void mapSectionAddress(const void *LocalAddress,
199 uint64_t TargetAddress) override {
200 for (auto &P : UnfinalizedSections)
201 if (P.second.count(LocalAddress))
202 ObjectLayer.mapSectionAddress(P.first, LocalAddress, TargetAddress);
205 uint64_t getGlobalValueAddress(const std::string &Name) override {
206 return getSymbolAddress(Name);
209 uint64_t getFunctionAddress(const std::string &Name) override {
210 return getSymbolAddress(Name);
213 void *getPointerToFunction(Function *F) override {
214 uint64_t FAddr = getSymbolAddress(F->getName());
215 return reinterpret_cast<void *>(static_cast<uintptr_t>(FAddr));
218 void *getPointerToNamedFunction(StringRef Name,
219 bool AbortOnFailure = true) override {
220 uint64_t Addr = getSymbolAddress(Name);
221 if (!Addr && AbortOnFailure)
222 llvm_unreachable("Missing symbol!");
223 return reinterpret_cast<void *>(static_cast<uintptr_t>(Addr));
226 GenericValue runFunction(Function *F,
227 ArrayRef<GenericValue> ArgValues) override;
229 void setObjectCache(ObjectCache *NewCache) override {
230 CompileLayer.setObjectCache(NewCache);
233 void setProcessAllSections(bool ProcessAllSections) override {
234 ObjectLayer.setProcessAllSections(ProcessAllSections);
239 RuntimeDyld::SymbolInfo findMangledSymbol(StringRef Name) {
240 if (auto Sym = LazyEmitLayer.findSymbol(Name, false))
241 return RuntimeDyld::SymbolInfo(Sym.getAddress(), Sym.getFlags());
242 if (auto Sym = ClientResolver->findSymbol(Name))
243 return RuntimeDyld::SymbolInfo(Sym.getAddress(), Sym.getFlags());
244 if (auto Sym = scanArchives(Name))
245 return RuntimeDyld::SymbolInfo(Sym.getAddress(), Sym.getFlags());
250 JITSymbol scanArchives(StringRef Name) {
251 for (object::OwningBinary<object::Archive> &OB : Archives) {
252 object::Archive *A = OB.getBinary();
253 // Look for our symbols in each Archive
254 object::Archive::child_iterator ChildIt = A->findSym(Name);
255 if (std::error_code EC = ChildIt->getError())
256 report_fatal_error(EC.message());
257 if (ChildIt != A->child_end()) {
258 // FIXME: Support nested archives?
259 ErrorOr<std::unique_ptr<object::Binary>> ChildBinOrErr =
260 (*ChildIt)->getAsBinary();
261 if (ChildBinOrErr.getError())
263 std::unique_ptr<object::Binary> &ChildBin = ChildBinOrErr.get();
264 if (ChildBin->isObject()) {
265 std::vector<std::unique_ptr<object::ObjectFile>> ObjSet;
266 ObjSet.push_back(std::unique_ptr<object::ObjectFile>(
267 static_cast<object::ObjectFile *>(ChildBin.release())));
268 ObjectLayer.addObjectSet(std::move(ObjSet), &MemMgr, &Resolver);
269 if (auto Sym = ObjectLayer.findSymbol(Name, true))
277 class NotifyObjectLoadedT {
279 typedef std::vector<std::unique_ptr<object::ObjectFile>> ObjListT;
280 typedef std::vector<std::unique_ptr<RuntimeDyld::LoadedObjectInfo>>
283 NotifyObjectLoadedT(OrcMCJITReplacement &M) : M(M) {}
285 void operator()(ObjectLinkingLayerBase::ObjSetHandleT H,
286 const ObjListT &Objects,
287 const LoadedObjInfoListT &Infos) const {
288 M.UnfinalizedSections[H] = std::move(M.SectionsAllocatedSinceLastLoad);
289 M.SectionsAllocatedSinceLastLoad = SectionAddrSet();
290 assert(Objects.size() == Infos.size() &&
291 "Incorrect number of Infos for Objects.");
292 for (unsigned I = 0; I < Objects.size(); ++I)
293 M.MemMgr.notifyObjectLoaded(&M, *Objects[I]);
297 OrcMCJITReplacement &M;
300 class NotifyFinalizedT {
302 NotifyFinalizedT(OrcMCJITReplacement &M) : M(M) {}
303 void operator()(ObjectLinkingLayerBase::ObjSetHandleT H) {
304 M.UnfinalizedSections.erase(H);
308 OrcMCJITReplacement &M;
311 std::string Mangle(StringRef Name) {
312 std::string MangledName;
314 raw_string_ostream MangledNameStream(MangledName);
315 Mang.getNameWithPrefix(MangledNameStream, Name, getDataLayout());
320 typedef ObjectLinkingLayer<NotifyObjectLoadedT> ObjectLayerT;
321 typedef IRCompileLayer<ObjectLayerT> CompileLayerT;
322 typedef LazyEmittingLayer<CompileLayerT> LazyEmitLayerT;
324 std::unique_ptr<TargetMachine> TM;
325 MCJITReplacementMemMgr MemMgr;
326 LinkingResolver Resolver;
327 std::shared_ptr<RuntimeDyld::SymbolResolver> ClientResolver;
330 NotifyObjectLoadedT NotifyObjectLoaded;
331 NotifyFinalizedT NotifyFinalized;
333 ObjectLayerT ObjectLayer;
334 CompileLayerT CompileLayer;
335 LazyEmitLayerT LazyEmitLayer;
337 // We need to store ObjLayerT::ObjSetHandles for each of the object sets
338 // that have been emitted but not yet finalized so that we can forward the
339 // mapSectionAddress calls appropriately.
340 typedef std::set<const void *> SectionAddrSet;
341 struct ObjSetHandleCompare {
342 bool operator()(ObjectLayerT::ObjSetHandleT H1,
343 ObjectLayerT::ObjSetHandleT H2) const {
347 SectionAddrSet SectionsAllocatedSinceLastLoad;
348 std::map<ObjectLayerT::ObjSetHandleT, SectionAddrSet, ObjSetHandleCompare>
351 std::vector<object::OwningBinary<object::Archive>> Archives;
354 } // End namespace orc.
355 } // End namespace llvm.
357 #endif // LLVM_LIB_EXECUTIONENGINE_ORC_MCJITREPLACEMENT_H