33 static struct RegisterJIT {
44 std::shared_ptr<MCJITMemoryManager> MemMgr,
45 std::shared_ptr<LegacyJITSymbolResolver>
Resolver,
46 std::unique_ptr<TargetMachine>
TM) {
52 if (!MemMgr || !Resolver) {
53 auto RTDyldMM = std::make_shared<SectionMemoryManager>();
60 return new MCJIT(std::move(M), std::move(TM), std::move(MemMgr),
64 MCJIT::MCJIT(std::unique_ptr<Module> M, std::unique_ptr<TargetMachine>
TM,
65 std::shared_ptr<MCJITMemoryManager> MemMgr,
66 std::shared_ptr<LegacyJITSymbolResolver>
Resolver)
68 Ctx(
nullptr), MemMgr(std::move(MemMgr)),
81 std::unique_ptr<Module> First = std::move(Modules[0]);
84 if (First->getDataLayout().isDefault())
85 First->setDataLayout(getDataLayout());
87 OwnedModules.addModule(std::move(First));
94 Dyld.deregisterEHFrames();
96 for (
auto &Obj : LoadedObjects)
98 notifyFreeingObject(*Obj);
106 if (M->getDataLayout().isDefault())
107 M->setDataLayout(getDataLayout());
109 OwnedModules.addModule(std::move(M));
114 return OwnedModules.removeModule(M);
118 std::unique_ptr<RuntimeDyld::LoadedObjectInfo> L = Dyld.loadObject(*Obj);
122 notifyObjectLoaded(*Obj, *L);
124 LoadedObjects.push_back(std::move(Obj));
128 std::unique_ptr<object::ObjectFile> ObjFile;
129 std::unique_ptr<MemoryBuffer> MemBuf;
131 addObjectFile(std::move(ObjFile));
132 Buffers.push_back(std::move(MemBuf));
136 Archives.push_back(std::move(A));
145 assert(M &&
"Can not emit a null module");
165 if (
TM->addPassesToEmitMC(PM, Ctx, ObjStream, !getVerifyModules()))
172 std::unique_ptr<MemoryBuffer> CompiledObjBuffer(
181 ObjCache->notifyObjectCompiled(M, MB);
184 return CompiledObjBuffer;
192 assert(OwnedModules.ownsModule(M) &&
193 "MCJIT::generateCodeForModule: Unknown module.");
196 if (OwnedModules.hasModuleBeenLoaded(M))
199 std::unique_ptr<MemoryBuffer> ObjectToLoad;
202 ObjectToLoad = ObjCache->getObject(M);
208 ObjectToLoad = emitObject(M);
209 assert(ObjectToLoad &&
"Compilation did not produce an object.");
223 std::unique_ptr<RuntimeDyld::LoadedObjectInfo> L =
224 Dyld.loadObject(*LoadedObject.
get());
229 notifyObjectLoaded(*LoadedObject.
get(), *L);
231 Buffers.push_back(std::move(ObjectToLoad));
232 LoadedObjects.push_back(std::move(*LoadedObject));
234 OwnedModules.markModuleAsLoaded(M);
241 Dyld.resolveRelocations();
243 OwnedModules.markAllLoadedModulesAsFinalized();
246 Dyld.registerEHFrames();
249 MemMgr->finalizeMemory();
259 for (
auto M : OwnedModules.added())
262 for (
auto M : ModsToAdd)
263 generateCodeForModule(M);
265 finalizeLoadedModules();
272 assert(OwnedModules.ownsModule(M) &&
"MCJIT::finalizeModule: Unknown module.");
275 if (!OwnedModules.hasModuleBeenLoaded(M))
276 generateCodeForModule(M);
278 finalizeLoadedModules();
282 if (
void *Addr = getPointerToGlobalIfAvailable(Name))
284 reinterpret_cast<uintptr_t>(Addr)),
287 return Dyld.getSymbol(Name);
291 bool CheckFunctionsOnly) {
293 if (DemangledName[0] == getDataLayout().getGlobalPrefix())
294 DemangledName = DemangledName.
substr(1);
299 for (ModulePtrSet::iterator
I = OwnedModules.begin_added(),
300 E = OwnedModules.end_added();
306 if (!CheckFunctionsOnly) {
318 bool CheckFunctionsOnly) {
319 std::string MangledName;
324 if (
auto Sym = findSymbol(MangledName, CheckFunctionsOnly)) {
325 if (
auto AddrOrErr = Sym.getAddress())
329 }
else if (
auto Err = Sym.takeError())
335 bool CheckFunctionsOnly) {
339 if (
auto Sym = findExistingSymbol(Name))
345 auto OptionalChildOrErr = A->
findSym(Name);
346 if (!OptionalChildOrErr)
348 auto &OptionalChild = *OptionalChildOrErr;
352 OptionalChild->getAsBinary();
353 if (!ChildBinOrErr) {
358 std::unique_ptr<object::Binary> &ChildBin = ChildBinOrErr.
get();
359 if (ChildBin->isObject()) {
360 std::unique_ptr<object::ObjectFile> OF(
361 static_cast<object::ObjectFile *>(ChildBin.release()));
363 addObjectFile(std::move(OF));
365 if (
auto Sym = findExistingSymbol(Name))
372 Module *M = findModuleForSymbol(Name, CheckFunctionsOnly);
374 generateCodeForModule(M);
377 return findExistingSymbol(Name);
382 if (LazyFunctionCreator) {
383 auto Addr =
static_cast<uint64_t
>(
384 reinterpret_cast<uintptr_t
>(LazyFunctionCreator(Name)));
393 uint64_t Result = getSymbolAddress(Name,
false);
395 finalizeLoadedModules();
401 uint64_t Result = getSymbolAddress(Name,
true);
403 finalizeLoadedModules();
413 TM->getNameWithPrefix(Name, F, Mang);
417 void *Addr = getPointerToNamedFunction(Name, AbortOnFailure);
418 updateGlobalMapping(F, Addr);
423 bool HasBeenAddedButNotLoaded = OwnedModules.hasModuleBeenAddedButNotLoaded(M);
426 if (HasBeenAddedButNotLoaded)
427 generateCodeForModule(M);
428 else if (!OwnedModules.hasModuleBeenLoaded(M)) {
440 return (
void*)Dyld.getSymbol(Name).getAddress();
443 void MCJIT::runStaticConstructorsDestructorsInModulePtrSet(
444 bool isDtors, ModulePtrSet::iterator
I, ModulePtrSet::iterator
E) {
445 for (;
I !=
E; ++
I) {
452 runStaticConstructorsDestructorsInModulePtrSet(
453 isDtors, OwnedModules.begin_added(), OwnedModules.end_added());
454 runStaticConstructorsDestructorsInModulePtrSet(
455 isDtors, OwnedModules.begin_loaded(), OwnedModules.end_loaded());
456 runStaticConstructorsDestructorsInModulePtrSet(
457 isDtors, OwnedModules.begin_finalized(), OwnedModules.end_finalized());
461 ModulePtrSet::iterator
I,
462 ModulePtrSet::iterator
E) {
463 for (;
I !=
E; ++
I) {
473 ModulePtrSet::iterator
I,
474 ModulePtrSet::iterator
E) {
475 for (;
I !=
E; ++
I) {
476 GlobalVariable *GV = (*I)->getGlobalVariable(Name, AllowInternal);
485 Function *
F = FindFunctionNamedInModulePtrSet(
486 FnName, OwnedModules.begin_added(), OwnedModules.end_added());
488 F = FindFunctionNamedInModulePtrSet(FnName, OwnedModules.begin_loaded(),
489 OwnedModules.end_loaded());
491 F = FindFunctionNamedInModulePtrSet(FnName, OwnedModules.begin_finalized(),
492 OwnedModules.end_finalized());
498 Name, AllowInternal, OwnedModules.begin_added(), OwnedModules.end_added());
500 GV = FindGlobalVariableNamedInModulePtrSet(Name, AllowInternal, OwnedModules.begin_loaded(),
501 OwnedModules.end_loaded());
503 GV = FindGlobalVariableNamedInModulePtrSet(Name, AllowInternal, OwnedModules.begin_finalized(),
504 OwnedModules.end_finalized());
509 assert(F &&
"Function *F was null at entry to run()");
511 void *FPtr = getPointerToFunction(F);
513 assert(FPtr &&
"Pointer to fn's code was null after getPointerToFunction");
519 "Wrong number of arguments passed into function!");
521 "This doesn't support passing arguments through varargs (yet)!");
526 switch (ArgValues.
size()) {
531 int (*PF)(int,
char **,
const char **) =
532 (
int(*)(int,
char **,
const char **))(
intptr_t)FPtr;
537 (
char **)
GVTOP(ArgValues[1]),
538 (
const char **)
GVTOP(ArgValues[2])));
545 int (*PF)(int,
char **) = (
int(*)(int,
char **))(
intptr_t)FPtr;
550 (
char **)
GVTOP(ArgValues[1])));
558 int (*PF)(int) = (
int(*)(int))(
intptr_t)FPtr;
567 if (ArgValues.
empty()) {
572 unsigned BitWidth = cast<IntegerType>(RetTy)->
getBitWidth();
575 else if (BitWidth <= 8)
577 else if (BitWidth <= 16)
579 else if (BitWidth <= 32)
581 else if (BitWidth <= 64)
606 "argument passing. Please use " 607 "ExecutionEngine::getFunctionAddress and cast the result " 608 "to the desired function pointer type.");
612 if (!isSymbolSearchingDisabled()) {
613 if (
auto Sym =
Resolver.findSymbol(Name)) {
614 if (
auto AddrOrErr = Sym.getAddress())
615 return reinterpret_cast<void*>(
616 static_cast<uintptr_t>(*AddrOrErr));
617 }
else if (
auto Err = Sym.takeError())
622 if (LazyFunctionCreator)
623 if (
void *
RP = LazyFunctionCreator(Name))
626 if (AbortOnFailure) {
628 "' which could not be resolved!");
637 EventListeners.push_back(L);
645 if (
I != EventListeners.rend()) {
647 EventListeners.pop_back();
654 static_cast<uint64_t
>(
reinterpret_cast<uintptr_t
>(Obj.
getData().
data()));
656 MemMgr->notifyObjectLoaded(
this, Obj);
657 for (
unsigned I = 0, S = EventListeners.size();
I < S; ++
I) {
658 EventListeners[
I]->notifyObjectLoaded(Key, Obj, L);
664 static_cast<uint64_t
>(
reinterpret_cast<uintptr_t
>(Obj.
getData().
data()));
667 L->notifyFreeingObject(Key);
672 auto Result = ParentEngine.findSymbol(Name,
false);
675 if (ParentEngine.isSymbolSearchingDisabled())
677 return ClientResolver->findSymbol(Name);
680 void LinkingSymbolResolver::anchor() {}
static unsigned getBitWidth(Type *Ty, const DataLayout &DL)
Returns the bitwidth of the given scalar or pointer type.
Function * FindFunctionNamed(StringRef FnName) override
FindFunctionNamed - Search all of the active modules to find the function that defines FnName...
void cantFail(Error Err, const char *Msg=nullptr)
Report a fatal error if Err is a failure value.
static Expected< OwningBinary< ObjectFile > > createObjectFile(StringRef ObjectPath)
Information about the loaded object.
void setObjectCache(ObjectCache *manager) override
Sets the object manager that MCJIT should use to avoid compilation.
llvm::Error materializeAll()
Make sure all GlobalValues in this Module are fully read and clear the Materializer.
Represents a symbol in the JIT.
LLVM_ATTRIBUTE_NORETURN void report_fatal_error(Error Err, bool gen_crash_diag=true)
Report a serious error, calling any installed error handler.
This class represents lattice values for constants.
Type * getParamType(unsigned i) const
Parameter type accessors.
A Module instance is used to store all the information related to an LLVM module. ...
2: 32-bit floating point type
void push_back(const T &Elt)
JITEventListener - Abstract interface for use by the JIT to notify clients about significant events d...
Expected< Optional< Child > > findSym(StringRef name) const
bool hasAvailableExternallyLinkage() const
static JITEventListener * createGDBRegistrationListener()
This class is the base class for all object file types.
A raw_ostream that writes to an SmallVector or SmallString.
void generateCodeForModule(Module *M) override
generateCodeForModule - Run code generation for the specified module and load it into memory...
GlobalVariable * getGlobalVariable(StringRef Name) const
Look up the specified global variable in the module symbol table.
4: 80-bit floating point type (X87)
bool hasExternalWeakLinkage() const
Error takeError()
Take ownership of the stored error.
void UnregisterJITEventListener(JITEventListener *L) override
LLVM_NODISCARD LLVM_ATTRIBUTE_ALWAYS_INLINE const char * data() const
data - Get a pointer to the start of the string (which may not be null terminated).
amdgpu Simplify well known AMD library false Value Value const Twine & Name
const DataLayout & getDataLayout() const
Get the data layout for the module's target platform.
virtual void runStaticConstructorsDestructors(bool isDtors)
runStaticConstructorsDestructors - This method is used to execute all of the static constructors or d...
TypeID getTypeID() const
Return the type id for the type.
bool isIntegerTy() const
True if this is an instance of IntegerType.
static bool LoadLibraryPermanently(const char *Filename, std::string *ErrMsg=nullptr)
This function permanently loads the dynamic library at the given path.
Tagged union holding either a T or a Error.
void runStaticConstructorsDestructors(bool isDtors) override
runStaticConstructorsDestructors - This method is used to execute all of the static constructors or d...
auto reverse(ContainerTy &&C, typename std::enable_if< has_rbegin< ContainerTy >::value >::type *=nullptr) -> decltype(make_range(C.rbegin(), C.rend()))
uint64_t getGlobalValueAddress(const std::string &Name) override
getGlobalValueAddress - Return the address of the specified global value.
Class to represent function types.
virtual void finalizeModule(Module *)
void notifyObjectLoaded(const object::ObjectFile &Obj, const RuntimeDyld::LoadedObjectInfo &L)
GlobalVariable * FindGlobalVariableNamed(StringRef Name, bool AllowInternal=false) override
FindGlobalVariableNamed - Search all of the active modules to find the global variable that defines N...
ArrayRef - Represent a constant reference to an array (0 or more elements consecutively in memory)...
void * getPointerToFunction(Function *F) override
getPointerToFunction - The different EE's represent function bodies in different ways.
JITSymbol findSymbol(const std::string &Name) override
This method returns the address of the specified function or variable.
LLVM_NODISCARD LLVM_ATTRIBUTE_ALWAYS_INLINE StringRef substr(size_t Start, size_t N=npos) const
Return a reference to the substring from [Start, Start + N).
JITSymbol findExistingSymbol(const std::string &Name)
void finalizeObject() override
finalizeObject - ensure the module is fully processed and is usable.
11: Arbitrary bit width integers
PassManager manages ModulePassManagers.
bool isVoidTy() const
Return true if this is 'void'.
Instances of this class acquire a given Mutex Lock when constructed and hold that lock until destruct...
void addModule(std::unique_ptr< Module > M) override
Add a Module to the list of modules that we can JIT from.
The instances of the Type class are immutable: once they are created, they are never changed...
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)
bool removeModule(Module *M) override
removeModule - Removes a Module from the list of modules, but does not free the module's memory...
size_t size() const
size - Get the array size.
static GCRegistry::Add< CoreCLRGC > E("coreclr", "CoreCLR-compatible GC")
void * getPointerToNamedFunction(StringRef Name, bool AbortOnFailure=true) override
getPointerToNamedFunction - This method returns the address of the specified function by using the dl...
bool isPointerTy() const
True if this is an instance of PointerType.
Interface for looking up the initializer for a variable name, used by Init::resolveReferences.
unsigned getNumParams() const
Return the number of fixed parameters this function type requires.
uint64_t getFunctionAddress(const std::string &Name) override
getFunctionAddress - Return the address of the specified function.
6: 128-bit floating point type (two 64-bits, PowerPC)
JITSymbol findSymbol(const std::string &Name, bool CheckFunctionsOnly)
void consumeError(Error Err)
Consume a Error without doing anything.
auto find(R &&Range, const T &Val) -> decltype(adl_begin(Range))
Provide wrappers to std::find which take ranges instead of having to pass begin/end explicitly...
void logAllUnhandledErrors(Error E, raw_ostream &OS, Twine ErrorBanner={})
Log all errors (if any) in E to OS.
#define llvm_unreachable(msg)
Marks that the current location is not supposed to be reachable.
Abstract interface for implementation execution of LLVM modules, designed to support both interpreter...
void * GVTOP(const GenericValue &GV)
std::pair< std::unique_ptr< T >, std::unique_ptr< MemoryBuffer > > takeBinary()
void addArchive(object::OwningBinary< object::Archive > O) override
addArchive - Add an Archive to the execution engine.
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small...
Module.h This file contains the declarations for the Module class.
Type * getReturnType() const
reference get()
Returns a reference to the stored T value.
uint64_t getSymbolAddress(const std::string &Name, bool CheckFunctionsOnly)
void RegisterJITEventListener(JITEventListener *L) override
Registers a listener to be called back on various events within the JIT.
Function * getFunction(StringRef Name) const
Look up the specified function in the module symbol table.
void swap(llvm::BitVector &LHS, llvm::BitVector &RHS)
Implement std::swap in terms of BitVector swap.
FunctionType * getFunctionType() const
Returns the FunctionType for me.
GenericValue PTOGV(void *P)
Class for arbitrary precision integers.
Module * findModuleForSymbol(const std::string &Name, bool CheckFunctionsOnly)
GenericValue runFunction(Function *F, ArrayRef< GenericValue > ArgValues) override
runFunction - Execute the specified function with the specified arguments, and return the result...
void notifyFreeingObject(const object::ObjectFile &Obj)
std::unique_ptr< MemoryBuffer > emitObject(Module *M)
emitObject – Generate a JITed object in memory from the specified module Currently, MCJIT only supports a single module and the module passed to this function call is expected to be the contained module.
SmallVector-backed MemoryBuffer instance.
This is the base ObjectCache type which can be provided to an ExecutionEngine for the purpose of avoi...
bool isDeclaration() const
Return true if the primary definition of this global value is outside of the current translation unit...
3: 64-bit floating point type
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
A raw_ostream that writes to an std::string.
Module * getParent()
Get the module that this global value is contained inside of...
void getNameWithPrefix(raw_ostream &OS, const GlobalValue *GV, bool CannotUsePrivateLabel) const
Print the appropriate prefix and the specified global variable's name.
StringRef - Represent a constant reference to a string, i.e.
StringRef getData() const
void finalizeLoadedModules()
bool empty() const
empty - Check if the array is empty.
5: 128-bit floating point type (112-bit mantissa)
void addObjectFile(std::unique_ptr< object::ObjectFile > O) override
addObjectFile - Add an ObjectFile to the execution engine.