LLVM 20.0.0git
MCJIT.cpp
Go to the documentation of this file.
1//===-- MCJIT.cpp - MC-based Just-in-Time Compiler ------------------------===//
2//
3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6//
7//===----------------------------------------------------------------------===//
8
9#include "MCJIT.h"
10#include "llvm/ADT/STLExtras.h"
16#include "llvm/IR/DataLayout.h"
18#include "llvm/IR/Function.h"
20#include "llvm/IR/Mangler.h"
21#include "llvm/IR/Module.h"
22#include "llvm/MC/MCContext.h"
23#include "llvm/Object/Archive.h"
29#include <mutex>
30
31using namespace llvm;
32
33namespace {
34
35static struct RegisterJIT {
36 RegisterJIT() { MCJIT::Register(); }
37} JITRegistrator;
38
39}
40
41extern "C" void LLVMLinkInMCJIT() {
42}
43
45MCJIT::createJIT(std::unique_ptr<Module> M, std::string *ErrorStr,
46 std::shared_ptr<MCJITMemoryManager> MemMgr,
47 std::shared_ptr<LegacyJITSymbolResolver> Resolver,
48 std::unique_ptr<TargetMachine> TM) {
49 // Try to register the program as a source of symbols to resolve against.
50 //
51 // FIXME: Don't do this here.
53
54 if (!MemMgr || !Resolver) {
55 auto RTDyldMM = std::make_shared<SectionMemoryManager>();
56 if (!MemMgr)
57 MemMgr = RTDyldMM;
58 if (!Resolver)
59 Resolver = RTDyldMM;
60 }
61
62 return new MCJIT(std::move(M), std::move(TM), std::move(MemMgr),
63 std::move(Resolver));
64}
65
66MCJIT::MCJIT(std::unique_ptr<Module> M, std::unique_ptr<TargetMachine> TM,
67 std::shared_ptr<MCJITMemoryManager> MemMgr,
68 std::shared_ptr<LegacyJITSymbolResolver> Resolver)
69 : ExecutionEngine(TM->createDataLayout(), std::move(M)), TM(std::move(TM)),
70 Ctx(nullptr), MemMgr(std::move(MemMgr)),
71 Resolver(*this, std::move(Resolver)), Dyld(*this->MemMgr, this->Resolver),
72 ObjCache(nullptr) {
73 // FIXME: We are managing our modules, so we do not want the base class
74 // ExecutionEngine to manage them as well. To avoid double destruction
75 // of the first (and only) module added in ExecutionEngine constructor
76 // we remove it from EE and will destruct it ourselves.
77 //
78 // It may make sense to move our module manager (based on SmallStPtr) back
79 // into EE if the JIT and Interpreter can live with it.
80 // If so, additional functions: addModule, removeModule, FindFunctionNamed,
81 // runStaticConstructorsDestructors could be moved back to EE as well.
82 //
83 std::unique_ptr<Module> First = std::move(Modules[0]);
84 Modules.clear();
85
86 if (First->getDataLayout().isDefault())
87 First->setDataLayout(getDataLayout());
88
89 OwnedModules.addModule(std::move(First));
91}
92
94 std::lock_guard<sys::Mutex> locked(lock);
95
96 Dyld.deregisterEHFrames();
97
98 for (auto &Obj : LoadedObjects)
99 if (Obj)
101
102 Archives.clear();
103}
104
105void MCJIT::addModule(std::unique_ptr<Module> M) {
106 std::lock_guard<sys::Mutex> locked(lock);
107
108 if (M->getDataLayout().isDefault())
109 M->setDataLayout(getDataLayout());
110
111 OwnedModules.addModule(std::move(M));
112}
113
115 std::lock_guard<sys::Mutex> locked(lock);
116 return OwnedModules.removeModule(M);
117}
118
119void MCJIT::addObjectFile(std::unique_ptr<object::ObjectFile> Obj) {
120 std::unique_ptr<RuntimeDyld::LoadedObjectInfo> L = Dyld.loadObject(*Obj);
121 if (Dyld.hasError())
123
124 notifyObjectLoaded(*Obj, *L);
125
126 LoadedObjects.push_back(std::move(Obj));
127}
128
130 std::unique_ptr<object::ObjectFile> ObjFile;
131 std::unique_ptr<MemoryBuffer> MemBuf;
132 std::tie(ObjFile, MemBuf) = Obj.takeBinary();
133 addObjectFile(std::move(ObjFile));
134 Buffers.push_back(std::move(MemBuf));
135}
136
138 Archives.push_back(std::move(A));
139}
140
142 std::lock_guard<sys::Mutex> locked(lock);
143 ObjCache = NewCache;
144}
145
146std::unique_ptr<MemoryBuffer> MCJIT::emitObject(Module *M) {
147 assert(M && "Can not emit a null module");
148
149 std::lock_guard<sys::Mutex> locked(lock);
150
151 // Materialize all globals in the module if they have not been
152 // materialized already.
153 cantFail(M->materializeAll());
154
155 // This must be a module which has already been added but not loaded to this
156 // MCJIT instance, since these conditions are tested by our caller,
157 // generateCodeForModule.
158
160
161 // The RuntimeDyld will take ownership of this shortly
162 SmallVector<char, 4096> ObjBufferSV;
163 raw_svector_ostream ObjStream(ObjBufferSV);
164
165 // Turn the machine code intermediate representation into bytes in memory
166 // that may be executed.
167 if (TM->addPassesToEmitMC(PM, Ctx, ObjStream, !getVerifyModules()))
168 report_fatal_error("Target does not support MC emission!");
169
170 // Initialize passes.
171 PM.run(*M);
172 // Flush the output buffer to get the generated code into memory
173
174 auto CompiledObjBuffer = std::make_unique<SmallVectorMemoryBuffer>(
175 std::move(ObjBufferSV), /*RequiresNullTerminator=*/false);
176
177 // If we have an object cache, tell it about the new object.
178 // Note that we're using the compiled image, not the loaded image (as below).
179 if (ObjCache) {
180 // MemoryBuffer is a thin wrapper around the actual memory, so it's OK
181 // to create a temporary object here and delete it after the call.
182 MemoryBufferRef MB = CompiledObjBuffer->getMemBufferRef();
183 ObjCache->notifyObjectCompiled(M, MB);
184 }
185
186 return CompiledObjBuffer;
187}
188
190 // Get a thread lock to make sure we aren't trying to load multiple times
191 std::lock_guard<sys::Mutex> locked(lock);
192
193 // This must be a module which has already been added to this MCJIT instance.
194 assert(OwnedModules.ownsModule(M) &&
195 "MCJIT::generateCodeForModule: Unknown module.");
196
197 // Re-compilation is not supported
198 if (OwnedModules.hasModuleBeenLoaded(M))
199 return;
200
201 std::unique_ptr<MemoryBuffer> ObjectToLoad;
202 // Try to load the pre-compiled object from cache if possible
203 if (ObjCache)
204 ObjectToLoad = ObjCache->getObject(M);
205
206 assert(M->getDataLayout() == getDataLayout() && "DataLayout Mismatch");
207
208 // If the cache did not contain a suitable object, compile the object
209 if (!ObjectToLoad) {
210 ObjectToLoad = emitObject(M);
211 assert(ObjectToLoad && "Compilation did not produce an object.");
212 }
213
214 // Load the object into the dynamic linker.
215 // MCJIT now owns the ObjectImage pointer (via its LoadedObjects list).
217 object::ObjectFile::createObjectFile(ObjectToLoad->getMemBufferRef());
218 if (!LoadedObject) {
219 std::string Buf;
221 logAllUnhandledErrors(LoadedObject.takeError(), OS);
223 }
224 std::unique_ptr<RuntimeDyld::LoadedObjectInfo> L =
225 Dyld.loadObject(*LoadedObject.get());
226
227 if (Dyld.hasError())
229
230 notifyObjectLoaded(*LoadedObject.get(), *L);
231
232 Buffers.push_back(std::move(ObjectToLoad));
233 LoadedObjects.push_back(std::move(*LoadedObject));
234
235 OwnedModules.markModuleAsLoaded(M);
236}
237
239 std::lock_guard<sys::Mutex> locked(lock);
240
241 // Resolve any outstanding relocations.
242 Dyld.resolveRelocations();
243
244 // Check for Dyld error.
245 if (Dyld.hasError())
246 ErrMsg = Dyld.getErrorString().str();
247
248 OwnedModules.markAllLoadedModulesAsFinalized();
249
250 // Register EH frame data for any module we own which has been loaded
251 Dyld.registerEHFrames();
252
253 // Set page permissions.
254 MemMgr->finalizeMemory();
255}
256
257// FIXME: Rename this.
259 std::lock_guard<sys::Mutex> locked(lock);
260
261 // Generate code for module is going to move objects out of the 'added' list,
262 // so we need to copy that out before using it:
263 SmallVector<Module *, 16> ModsToAdd(OwnedModules.added());
264
265 for (auto *M : ModsToAdd)
267
269}
270
272 std::lock_guard<sys::Mutex> locked(lock);
273
274 // This must be a module which has already been added to this MCJIT instance.
275 assert(OwnedModules.ownsModule(M) && "MCJIT::finalizeModule: Unknown module.");
276
277 // If the module hasn't been compiled, just do that.
278 if (!OwnedModules.hasModuleBeenLoaded(M))
280
282}
283
286 return JITSymbol(static_cast<uint64_t>(
287 reinterpret_cast<uintptr_t>(Addr)),
289
290 return Dyld.getSymbol(Name);
291}
292
294 bool CheckFunctionsOnly) {
295 StringRef DemangledName = Name;
296 if (DemangledName[0] == getDataLayout().getGlobalPrefix())
297 DemangledName = DemangledName.substr(1);
298
299 std::lock_guard<sys::Mutex> locked(lock);
300
301 // If it hasn't already been generated, see if it's in one of our modules.
302 for (ModulePtrSet::iterator I = OwnedModules.begin_added(),
303 E = OwnedModules.end_added();
304 I != E; ++I) {
305 Module *M = *I;
306 Function *F = M->getFunction(DemangledName);
307 if (F && !F->isDeclaration())
308 return M;
309 if (!CheckFunctionsOnly) {
310 GlobalVariable *G = M->getGlobalVariable(DemangledName);
311 if (G && !G->isDeclaration())
312 return M;
313 // FIXME: Do we need to worry about global aliases?
314 }
315 }
316 // We didn't find the symbol in any of our modules.
317 return nullptr;
318}
319
321 bool CheckFunctionsOnly) {
322 std::string MangledName;
323 {
324 raw_string_ostream MangledNameStream(MangledName);
325 Mangler::getNameWithPrefix(MangledNameStream, Name, getDataLayout());
326 }
327 if (auto Sym = findSymbol(MangledName, CheckFunctionsOnly)) {
328 if (auto AddrOrErr = Sym.getAddress())
329 return *AddrOrErr;
330 else
331 report_fatal_error(AddrOrErr.takeError());
332 } else if (auto Err = Sym.takeError())
333 report_fatal_error(Sym.takeError());
334 return 0;
335}
336
338 bool CheckFunctionsOnly) {
339 std::lock_guard<sys::Mutex> locked(lock);
340
341 // First, check to see if we already have this symbol.
342 if (auto Sym = findExistingSymbol(Name))
343 return Sym;
344
345 for (object::OwningBinary<object::Archive> &OB : Archives) {
346 object::Archive *A = OB.getBinary();
347 // Look for our symbols in each Archive
348 auto OptionalChildOrErr = A->findSym(Name);
349 if (!OptionalChildOrErr)
350 report_fatal_error(OptionalChildOrErr.takeError());
351 auto &OptionalChild = *OptionalChildOrErr;
352 if (OptionalChild) {
353 // FIXME: Support nested archives?
355 OptionalChild->getAsBinary();
356 if (!ChildBinOrErr) {
357 // TODO: Actually report errors helpfully.
358 consumeError(ChildBinOrErr.takeError());
359 continue;
360 }
361 std::unique_ptr<object::Binary> &ChildBin = ChildBinOrErr.get();
362 if (ChildBin->isObject()) {
363 std::unique_ptr<object::ObjectFile> OF(
364 static_cast<object::ObjectFile *>(ChildBin.release()));
365 // This causes the object file to be loaded.
366 addObjectFile(std::move(OF));
367 // The address should be here now.
368 if (auto Sym = findExistingSymbol(Name))
369 return Sym;
370 }
371 }
372 }
373
374 // If it hasn't already been generated, see if it's in one of our modules.
375 Module *M = findModuleForSymbol(Name, CheckFunctionsOnly);
376 if (M) {
378
379 // Check the RuntimeDyld table again, it should be there now.
380 return findExistingSymbol(Name);
381 }
382
383 // If a LazyFunctionCreator is installed, use it to get/create the function.
384 // FIXME: Should we instead have a LazySymbolCreator callback?
386 auto Addr = static_cast<uint64_t>(
387 reinterpret_cast<uintptr_t>(LazyFunctionCreator(Name)));
389 }
390
391 return nullptr;
392}
393
395 std::lock_guard<sys::Mutex> locked(lock);
396 uint64_t Result = getSymbolAddress(Name, false);
397 if (Result != 0)
399 return Result;
400}
401
403 std::lock_guard<sys::Mutex> locked(lock);
404 uint64_t Result = getSymbolAddress(Name, true);
405 if (Result != 0)
407 return Result;
408}
409
410// Deprecated. Use getFunctionAddress instead.
412 std::lock_guard<sys::Mutex> locked(lock);
413
414 Mangler Mang;
416 TM->getNameWithPrefix(Name, F, Mang);
417
418 if (F->isDeclaration() || F->hasAvailableExternallyLinkage()) {
419 bool AbortOnFailure = !F->hasExternalWeakLinkage();
420 void *Addr = getPointerToNamedFunction(Name, AbortOnFailure);
422 return Addr;
423 }
424
425 Module *M = F->getParent();
426 bool HasBeenAddedButNotLoaded = OwnedModules.hasModuleBeenAddedButNotLoaded(M);
427
428 // Make sure the relevant module has been compiled and loaded.
429 if (HasBeenAddedButNotLoaded)
431 else if (!OwnedModules.hasModuleBeenLoaded(M)) {
432 // If this function doesn't belong to one of our modules, we're done.
433 // FIXME: Asking for the pointer to a function that hasn't been registered,
434 // and isn't a declaration (which is handled above) should probably
435 // be an assertion.
436 return nullptr;
437 }
438
439 // FIXME: Should the Dyld be retaining module information? Probably not.
440 //
441 // This is the accessor for the target address, so make sure to check the
442 // load address of the symbol, not the local address.
443 return (void*)Dyld.getSymbol(Name).getAddress();
444}
445
446void MCJIT::runStaticConstructorsDestructorsInModulePtrSet(
447 bool isDtors, ModulePtrSet::iterator I, ModulePtrSet::iterator E) {
448 for (; I != E; ++I) {
450 }
451}
452
454 // Execute global ctors/dtors for each module in the program.
455 runStaticConstructorsDestructorsInModulePtrSet(
456 isDtors, OwnedModules.begin_added(), OwnedModules.end_added());
457 runStaticConstructorsDestructorsInModulePtrSet(
458 isDtors, OwnedModules.begin_loaded(), OwnedModules.end_loaded());
459 runStaticConstructorsDestructorsInModulePtrSet(
460 isDtors, OwnedModules.begin_finalized(), OwnedModules.end_finalized());
461}
462
463Function *MCJIT::FindFunctionNamedInModulePtrSet(StringRef FnName,
464 ModulePtrSet::iterator I,
465 ModulePtrSet::iterator E) {
466 for (; I != E; ++I) {
467 Function *F = (*I)->getFunction(FnName);
468 if (F && !F->isDeclaration())
469 return F;
470 }
471 return nullptr;
472}
473
474GlobalVariable *MCJIT::FindGlobalVariableNamedInModulePtrSet(StringRef Name,
475 bool AllowInternal,
476 ModulePtrSet::iterator I,
477 ModulePtrSet::iterator E) {
478 for (; I != E; ++I) {
479 GlobalVariable *GV = (*I)->getGlobalVariable(Name, AllowInternal);
480 if (GV && !GV->isDeclaration())
481 return GV;
482 }
483 return nullptr;
484}
485
486
488 Function *F = FindFunctionNamedInModulePtrSet(
489 FnName, OwnedModules.begin_added(), OwnedModules.end_added());
490 if (!F)
491 F = FindFunctionNamedInModulePtrSet(FnName, OwnedModules.begin_loaded(),
492 OwnedModules.end_loaded());
493 if (!F)
494 F = FindFunctionNamedInModulePtrSet(FnName, OwnedModules.begin_finalized(),
495 OwnedModules.end_finalized());
496 return F;
497}
498
500 GlobalVariable *GV = FindGlobalVariableNamedInModulePtrSet(
501 Name, AllowInternal, OwnedModules.begin_added(), OwnedModules.end_added());
502 if (!GV)
503 GV = FindGlobalVariableNamedInModulePtrSet(Name, AllowInternal, OwnedModules.begin_loaded(),
504 OwnedModules.end_loaded());
505 if (!GV)
506 GV = FindGlobalVariableNamedInModulePtrSet(Name, AllowInternal, OwnedModules.begin_finalized(),
507 OwnedModules.end_finalized());
508 return GV;
509}
510
512 assert(F && "Function *F was null at entry to run()");
513
514 void *FPtr = getPointerToFunction(F);
515 finalizeModule(F->getParent());
516 assert(FPtr && "Pointer to fn's code was null after getPointerToFunction");
517 FunctionType *FTy = F->getFunctionType();
518 Type *RetTy = FTy->getReturnType();
519
520 assert((FTy->getNumParams() == ArgValues.size() ||
521 (FTy->isVarArg() && FTy->getNumParams() <= ArgValues.size())) &&
522 "Wrong number of arguments passed into function!");
523 assert(FTy->getNumParams() == ArgValues.size() &&
524 "This doesn't support passing arguments through varargs (yet)!");
525
526 // Handle some common cases first. These cases correspond to common `main'
527 // prototypes.
528 if (RetTy->isIntegerTy(32) || RetTy->isVoidTy()) {
529 switch (ArgValues.size()) {
530 case 3:
531 if (FTy->getParamType(0)->isIntegerTy(32) &&
532 FTy->getParamType(1)->isPointerTy() &&
533 FTy->getParamType(2)->isPointerTy()) {
534 int (*PF)(int, char **, const char **) =
535 (int(*)(int, char **, const char **))(intptr_t)FPtr;
536
537 // Call the function.
538 GenericValue rv;
539 rv.IntVal = APInt(32, PF(ArgValues[0].IntVal.getZExtValue(),
540 (char **)GVTOP(ArgValues[1]),
541 (const char **)GVTOP(ArgValues[2])));
542 return rv;
543 }
544 break;
545 case 2:
546 if (FTy->getParamType(0)->isIntegerTy(32) &&
547 FTy->getParamType(1)->isPointerTy()) {
548 int (*PF)(int, char **) = (int(*)(int, char **))(intptr_t)FPtr;
549
550 // Call the function.
551 GenericValue rv;
552 rv.IntVal = APInt(32, PF(ArgValues[0].IntVal.getZExtValue(),
553 (char **)GVTOP(ArgValues[1])));
554 return rv;
555 }
556 break;
557 case 1:
558 if (FTy->getNumParams() == 1 &&
559 FTy->getParamType(0)->isIntegerTy(32)) {
560 GenericValue rv;
561 int (*PF)(int) = (int(*)(int))(intptr_t)FPtr;
562 rv.IntVal = APInt(32, PF(ArgValues[0].IntVal.getZExtValue()));
563 return rv;
564 }
565 break;
566 }
567 }
568
569 // Handle cases where no arguments are passed first.
570 if (ArgValues.empty()) {
571 GenericValue rv;
572 switch (RetTy->getTypeID()) {
573 default: llvm_unreachable("Unknown return type for function call!");
574 case Type::IntegerTyID: {
575 unsigned BitWidth = cast<IntegerType>(RetTy)->getBitWidth();
576 if (BitWidth == 1)
577 rv.IntVal = APInt(BitWidth, ((bool(*)())(intptr_t)FPtr)());
578 else if (BitWidth <= 8)
579 rv.IntVal = APInt(BitWidth, ((char(*)())(intptr_t)FPtr)());
580 else if (BitWidth <= 16)
581 rv.IntVal = APInt(BitWidth, ((short(*)())(intptr_t)FPtr)());
582 else if (BitWidth <= 32)
583 rv.IntVal = APInt(BitWidth, ((int(*)())(intptr_t)FPtr)());
584 else if (BitWidth <= 64)
585 rv.IntVal = APInt(BitWidth, ((int64_t(*)())(intptr_t)FPtr)());
586 else
587 llvm_unreachable("Integer types > 64 bits not supported");
588 return rv;
589 }
590 case Type::VoidTyID:
591 rv.IntVal = APInt(32, ((int(*)())(intptr_t)FPtr)());
592 return rv;
593 case Type::FloatTyID:
594 rv.FloatVal = ((float(*)())(intptr_t)FPtr)();
595 return rv;
596 case Type::DoubleTyID:
597 rv.DoubleVal = ((double(*)())(intptr_t)FPtr)();
598 return rv;
600 case Type::FP128TyID:
602 llvm_unreachable("long double not supported yet");
604 return PTOGV(((void*(*)())(intptr_t)FPtr)());
605 }
606 }
607
608 report_fatal_error("MCJIT::runFunction does not support full-featured "
609 "argument passing. Please use "
610 "ExecutionEngine::getFunctionAddress and cast the result "
611 "to the desired function pointer type.");
612}
613
616 if (auto Sym = Resolver.findSymbol(std::string(Name))) {
617 if (auto AddrOrErr = Sym.getAddress())
618 return reinterpret_cast<void*>(
619 static_cast<uintptr_t>(*AddrOrErr));
620 } else if (auto Err = Sym.takeError())
621 report_fatal_error(std::move(Err));
622 }
623
624 /// If a LazyFunctionCreator is installed, use it to get/create the function.
626 if (void *RP = LazyFunctionCreator(std::string(Name)))
627 return RP;
628
629 if (AbortOnFailure) {
630 report_fatal_error("Program used external function '"+Name+
631 "' which could not be resolved!");
632 }
633 return nullptr;
634}
635
637 if (!L)
638 return;
639 std::lock_guard<sys::Mutex> locked(lock);
640 EventListeners.push_back(L);
641}
642
644 if (!L)
645 return;
646 std::lock_guard<sys::Mutex> locked(lock);
647 auto I = find(reverse(EventListeners), L);
648 if (I != EventListeners.rend()) {
649 std::swap(*I, EventListeners.back());
650 EventListeners.pop_back();
651 }
652}
653
656 uint64_t Key =
657 static_cast<uint64_t>(reinterpret_cast<uintptr_t>(Obj.getData().data()));
658 std::lock_guard<sys::Mutex> locked(lock);
659 MemMgr->notifyObjectLoaded(this, Obj);
660 for (JITEventListener *EL : EventListeners)
661 EL->notifyObjectLoaded(Key, Obj, L);
662}
663
665 uint64_t Key =
666 static_cast<uint64_t>(reinterpret_cast<uintptr_t>(Obj.getData().data()));
667 std::lock_guard<sys::Mutex> locked(lock);
668 for (JITEventListener *L : EventListeners)
669 L->notifyFreeingObject(Key);
670}
671
674 auto Result = ParentEngine.findSymbol(Name, false);
675 if (Result)
676 return Result;
677 if (ParentEngine.isSymbolSearchingDisabled())
678 return nullptr;
679 return ClientResolver->findSymbol(Name);
680}
681
682void LinkingSymbolResolver::anchor() {}
static GCRegistry::Add< ErlangGC > A("erlang", "erlang-compatible garbage collector")
return RetTy
uint64_t Addr
std::string Name
Symbol * Sym
Definition: ELF_riscv.cpp:479
#define F(x, y, z)
Definition: MD5.cpp:55
#define I(x, y, z)
Definition: MD5.cpp:58
#define G(x, y, z)
Definition: MD5.cpp:56
Module.h This file contains the declarations for the Module class.
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
This file contains some templates that are useful if you are working with the STL at all.
raw_pwrite_stream & OS
Class for arbitrary precision integers.
Definition: APInt.h:78
ArrayRef - Represent a constant reference to an array (0 or more elements consecutively in memory),...
Definition: ArrayRef.h:41
size_t size() const
size - Get the array size.
Definition: ArrayRef.h:165
bool empty() const
empty - Check if the array is empty.
Definition: ArrayRef.h:160
Abstract interface for implementation execution of LLVM modules, designed to support both interpreter...
const DataLayout & getDataLayout() const
bool getVerifyModules() const
void * getPointerToGlobalIfAvailable(StringRef S)
getPointerToGlobalIfAvailable - This returns the address of the specified global value if it is has a...
sys::Mutex lock
lock - This lock protects the ExecutionEngine and MCJIT classes.
virtual void runStaticConstructorsDestructors(bool isDtors)
runStaticConstructorsDestructors - This method is used to execute all of the static constructors or d...
FunctionCreator LazyFunctionCreator
LazyFunctionCreator - If an unknown function is needed, this function pointer is invoked to create it...
SmallVector< std::unique_ptr< Module >, 1 > Modules
The list of Modules that we are JIT'ing from.
uint64_t updateGlobalMapping(const GlobalValue *GV, void *Addr)
updateGlobalMapping - Replace an existing mapping for GV with a new address.
bool isSymbolSearchingDisabled() const
Tagged union holding either a T or a Error.
Definition: Error.h:481
Error takeError()
Take ownership of the stored error.
Definition: Error.h:608
reference get()
Returns a reference to the stored T value.
Definition: Error.h:578
Class to represent function types.
Definition: DerivedTypes.h:103
unsigned getNumParams() const
Return the number of fixed parameters this function type requires.
Definition: DerivedTypes.h:142
Type * getParamType(unsigned i) const
Parameter type accessors.
Definition: DerivedTypes.h:135
bool isVarArg() const
Definition: DerivedTypes.h:123
Type * getReturnType() const
Definition: DerivedTypes.h:124
bool isDeclaration() const
Return true if the primary definition of this global value is outside of the current translation unit...
Definition: Globals.cpp:290
JITTargetAddress getAddress() const
Return the address of this symbol.
Definition: JITSymbol.h:251
JITEventListener - Abstract interface for use by the JIT to notify clients about significant events d...
static JITEventListener * createGDBRegistrationListener()
Represents a symbol in the JIT.
Definition: JITSymbol.h:265
JITSymbol findSymbol(const std::string &Name) override
This method returns the address of the specified function or variable.
Definition: MCJIT.cpp:673
GenericValue runFunction(Function *F, ArrayRef< GenericValue > ArgValues) override
runFunction - Execute the specified function with the specified arguments, and return the result.
Definition: MCJIT.cpp:511
void * getPointerToNamedFunction(StringRef Name, bool AbortOnFailure=true) override
getPointerToNamedFunction - This method returns the address of the specified function by using the dl...
Definition: MCJIT.cpp:614
void RegisterJITEventListener(JITEventListener *L) override
Registers a listener to be called back on various events within the JIT.
Definition: MCJIT.cpp:636
static ExecutionEngine * createJIT(std::unique_ptr< Module > M, std::string *ErrorStr, std::shared_ptr< MCJITMemoryManager > MemMgr, std::shared_ptr< LegacyJITSymbolResolver > Resolver, std::unique_ptr< TargetMachine > TM)
Definition: MCJIT.cpp:45
void runStaticConstructorsDestructors(bool isDtors) override
runStaticConstructorsDestructors - This method is used to execute all of the static constructors or d...
Definition: MCJIT.cpp:453
Module * findModuleForSymbol(const std::string &Name, bool CheckFunctionsOnly)
Definition: MCJIT.cpp:293
void addArchive(object::OwningBinary< object::Archive > O) override
addArchive - Add an Archive to the execution engine.
Definition: MCJIT.cpp:137
Function * FindFunctionNamed(StringRef FnName) override
FindFunctionNamed - Search all of the active modules to find the function that defines FnName.
Definition: MCJIT.cpp:487
~MCJIT() override
Definition: MCJIT.cpp:93
void finalizeObject() override
finalizeObject - ensure the module is fully processed and is usable.
Definition: MCJIT.cpp:258
void finalizeLoadedModules()
Definition: MCJIT.cpp:238
void notifyObjectLoaded(const object::ObjectFile &Obj, const RuntimeDyld::LoadedObjectInfo &L)
Definition: MCJIT.cpp:654
void generateCodeForModule(Module *M) override
generateCodeForModule - Run code generation for the specified module and load it into memory.
Definition: MCJIT.cpp:189
uint64_t getFunctionAddress(const std::string &Name) override
getFunctionAddress - Return the address of the specified function.
Definition: MCJIT.cpp:402
void addObjectFile(std::unique_ptr< object::ObjectFile > O) override
addObjectFile - Add an ObjectFile to the execution engine.
Definition: MCJIT.cpp:119
std::unique_ptr< MemoryBuffer > emitObject(Module *M)
emitObject – Generate a JITed object in memory from the specified module Currently,...
Definition: MCJIT.cpp:146
static void Register()
Definition: MCJIT.h:291
void UnregisterJITEventListener(JITEventListener *L) override
Definition: MCJIT.cpp:643
bool removeModule(Module *M) override
removeModule - Removes a Module from the list of modules, but does not free the module's memory.
Definition: MCJIT.cpp:114
JITSymbol findExistingSymbol(const std::string &Name)
Definition: MCJIT.cpp:284
GlobalVariable * FindGlobalVariableNamed(StringRef Name, bool AllowInternal=false) override
FindGlobalVariableNamed - Search all of the active modules to find the global variable that defines N...
Definition: MCJIT.cpp:499
uint64_t getGlobalValueAddress(const std::string &Name) override
getGlobalValueAddress - Return the address of the specified global value.
Definition: MCJIT.cpp:394
uint64_t getSymbolAddress(const std::string &Name, bool CheckFunctionsOnly)
Definition: MCJIT.cpp:320
void notifyFreeingObject(const object::ObjectFile &Obj)
Definition: MCJIT.cpp:664
virtual void finalizeModule(Module *)
Definition: MCJIT.cpp:271
JITSymbol findSymbol(const std::string &Name, bool CheckFunctionsOnly)
Definition: MCJIT.cpp:337
void * getPointerToFunction(Function *F) override
getPointerToFunction - The different EE's represent function bodies in different ways.
Definition: MCJIT.cpp:411
void setObjectCache(ObjectCache *manager) override
Sets the object manager that MCJIT should use to avoid compilation.
Definition: MCJIT.cpp:141
void addModule(std::unique_ptr< Module > M) override
Add a Module to the list of modules that we can JIT from.
Definition: MCJIT.cpp:105
void getNameWithPrefix(raw_ostream &OS, const GlobalValue *GV, bool CannotUsePrivateLabel) const
Print the appropriate prefix and the specified global variable's name.
Definition: Mangler.cpp:120
A Module instance is used to store all the information related to an LLVM module.
Definition: Module.h:65
This is the base ObjectCache type which can be provided to an ExecutionEngine for the purpose of avoi...
Definition: ObjectCache.h:23
virtual std::unique_ptr< MemoryBuffer > getObject(const Module *M)=0
Returns a pointer to a newly allocated MemoryBuffer that contains the object which corresponds with M...
virtual void notifyObjectCompiled(const Module *M, MemoryBufferRef Obj)=0
notifyObjectCompiled - Provides a pointer to compiled code for Module M.
Interface for looking up the initializer for a variable name, used by Init::resolveReferences.
Definition: Record.h:2212
Information about the loaded object.
Definition: RuntimeDyld.h:69
void resolveRelocations()
Resolve the relocations for all symbols we currently know about.
void registerEHFrames()
Register any EH frame sections that have been loaded but not previously registered with the memory ma...
JITEvaluatedSymbol getSymbol(StringRef Name) const
Get the target address and flags for the named symbol.
std::unique_ptr< LoadedObjectInfo > loadObject(const object::ObjectFile &O)
Add the referenced object file to the list of objects to be loaded and relocated.
StringRef getErrorString()
SmallPtrSetIterator - This implements a const_iterator for SmallPtrSet.
Definition: SmallPtrSet.h:295
SmallString - A SmallString is just a SmallVector with methods and accessors that make it work better...
Definition: SmallString.h:26
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small.
Definition: SmallVector.h:1209
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:50
std::string str() const
str - Get the contents as an std::string.
Definition: StringRef.h:215
constexpr StringRef substr(size_t Start, size_t N=npos) const
Return a reference to the substring from [Start, Start + N).
Definition: StringRef.h:556
constexpr const char * data() const
data - Get a pointer to the start of the string (which may not be null terminated).
Definition: StringRef.h:131
Twine - A lightweight data structure for efficiently representing the concatenation of temporary valu...
Definition: Twine.h:81
The instances of the Type class are immutable: once they are created, they are never changed.
Definition: Type.h:45
bool isPointerTy() const
True if this is an instance of PointerType.
Definition: Type.h:251
@ VoidTyID
type with no size
Definition: Type.h:63
@ FloatTyID
32-bit floating point type
Definition: Type.h:58
@ IntegerTyID
Arbitrary bit width integers.
Definition: Type.h:70
@ DoubleTyID
64-bit floating point type
Definition: Type.h:59
@ X86_FP80TyID
80-bit floating point type (X87)
Definition: Type.h:60
@ PPC_FP128TyID
128-bit floating point type (two 64-bits, PowerPC)
Definition: Type.h:62
@ PointerTyID
Pointers.
Definition: Type.h:72
@ FP128TyID
128-bit floating point type (112-bit significand)
Definition: Type.h:61
bool isIntegerTy() const
True if this is an instance of IntegerType.
Definition: Type.h:224
PassManager manages ModulePassManagers.
bool run(Module &M)
run - Execute all of the passes scheduled for execution.
StringRef getData() const
Definition: Binary.cpp:39
This class is the base class for all object file types.
Definition: ObjectFile.h:229
static Expected< OwningBinary< ObjectFile > > createObjectFile(StringRef ObjectPath)
Definition: ObjectFile.cpp:209
std::pair< std::unique_ptr< T >, std::unique_ptr< MemoryBuffer > > takeBinary()
Definition: Binary.h:232
A raw_ostream that writes to an std::string.
Definition: raw_ostream.h:661
A raw_ostream that writes to an SmallVector or SmallString.
Definition: raw_ostream.h:691
static bool LoadLibraryPermanently(const char *Filename, std::string *ErrMsg=nullptr)
This function permanently loads the dynamic library at the given path.
void LLVMLinkInMCJIT()
Definition: MCJIT.cpp:41
#define llvm_unreachable(msg)
Marks that the current location is not supposed to be reachable.
This is an optimization pass for GlobalISel generic memory operations.
Definition: AddressRanges.h:18
void logAllUnhandledErrors(Error E, raw_ostream &OS, Twine ErrorBanner={})
Log all errors (if any) in E to OS.
Definition: Error.cpp:65
auto find(R &&Range, const T &Val)
Provide wrappers to std::find which take ranges instead of having to pass begin/end explicitly.
Definition: STLExtras.h:1742
auto reverse(ContainerTy &&C)
Definition: STLExtras.h:419
GenericValue PTOGV(void *P)
Definition: GenericValue.h:49
void report_fatal_error(Error Err, bool gen_crash_diag=true)
Report a serious error, calling any installed error handler.
Definition: Error.cpp:167
@ First
Helpers to iterate all locations in the MemoryEffectsBase class.
void cantFail(Error Err, const char *Msg=nullptr)
Report a fatal error if Err is a failure value.
Definition: Error.h:756
constexpr unsigned BitWidth
Definition: BitmaskEnum.h:191
OutputIt move(R &&Range, OutputIt Out)
Provide wrappers to std::move which take ranges instead of having to pass begin/end explicitly.
Definition: STLExtras.h:1856
void * GVTOP(const GenericValue &GV)
Definition: GenericValue.h:50
void consumeError(Error Err)
Consume a Error without doing anything.
Definition: Error.h:1069
Implement std::hash so that hash_code can be used in STL containers.
Definition: BitVector.h:858
void swap(llvm::BitVector &LHS, llvm::BitVector &RHS)
Implement std::swap in terms of BitVector swap.
Definition: BitVector.h:860