35static struct RegisterJIT {
46 std::shared_ptr<MCJITMemoryManager> MemMgr,
47 std::shared_ptr<LegacyJITSymbolResolver>
Resolver,
48 std::unique_ptr<TargetMachine> TM) {
55 auto RTDyldMM = std::make_shared<SectionMemoryManager>();
62 return new MCJIT(std::move(M), std::move(TM), std::move(MemMgr),
66MCJIT::MCJIT(std::unique_ptr<Module> M, std::unique_ptr<TargetMachine> TM,
67 std::shared_ptr<MCJITMemoryManager> MemMgr,
68 std::shared_ptr<LegacyJITSymbolResolver>
Resolver)
70 Ctx(nullptr), MemMgr(
std::
move(MemMgr)),
86 if (
First->getDataLayout().isDefault())
89 OwnedModules.addModule(std::move(
First));
94 std::lock_guard<sys::Mutex> locked(
lock);
98 for (
auto &Obj : LoadedObjects)
106 std::lock_guard<sys::Mutex> locked(
lock);
108 if (M->getDataLayout().isDefault())
111 OwnedModules.addModule(std::move(M));
115 std::lock_guard<sys::Mutex> locked(
lock);
116 return OwnedModules.removeModule(M);
120 std::unique_ptr<RuntimeDyld::LoadedObjectInfo> L = Dyld.
loadObject(*Obj);
126 LoadedObjects.push_back(std::move(Obj));
130 std::unique_ptr<object::ObjectFile> ObjFile;
131 std::unique_ptr<MemoryBuffer> MemBuf;
134 Buffers.push_back(std::move(MemBuf));
138 Archives.push_back(std::move(
A));
142 std::lock_guard<sys::Mutex> locked(
lock);
147 assert(M &&
"Can not emit a null module");
149 std::lock_guard<sys::Mutex> locked(
lock);
174 auto CompiledObjBuffer = std::make_unique<SmallVectorMemoryBuffer>(
175 std::move(ObjBufferSV),
false);
186 return CompiledObjBuffer;
191 std::lock_guard<sys::Mutex> locked(
lock);
194 assert(OwnedModules.ownsModule(M) &&
195 "MCJIT::generateCodeForModule: Unknown module.");
198 if (OwnedModules.hasModuleBeenLoaded(M))
201 std::unique_ptr<MemoryBuffer> ObjectToLoad;
211 assert(ObjectToLoad &&
"Compilation did not produce an object.");
224 std::unique_ptr<RuntimeDyld::LoadedObjectInfo> L =
232 Buffers.push_back(std::move(ObjectToLoad));
233 LoadedObjects.push_back(std::move(*LoadedObject));
235 OwnedModules.markModuleAsLoaded(M);
239 std::lock_guard<sys::Mutex> locked(
lock);
248 OwnedModules.markAllLoadedModulesAsFinalized();
254 MemMgr->finalizeMemory();
259 std::lock_guard<sys::Mutex> locked(
lock);
265 for (
auto *M : ModsToAdd)
272 std::lock_guard<sys::Mutex> locked(
lock);
275 assert(OwnedModules.ownsModule(M) &&
"MCJIT::finalizeModule: Unknown module.");
278 if (!OwnedModules.hasModuleBeenLoaded(M))
287 reinterpret_cast<uintptr_t
>(
Addr)),
294 bool CheckFunctionsOnly) {
297 DemangledName = DemangledName.
substr(1);
299 std::lock_guard<sys::Mutex> locked(
lock);
303 E = OwnedModules.end_added();
306 Function *
F = M->getFunction(DemangledName);
307 if (
F && !
F->isDeclaration())
309 if (!CheckFunctionsOnly) {
311 if (
G && !
G->isDeclaration())
321 bool CheckFunctionsOnly) {
322 std::string MangledName;
327 if (
auto Sym =
findSymbol(MangledName, CheckFunctionsOnly)) {
328 if (
auto AddrOrErr =
Sym.getAddress())
332 }
else if (
auto Err =
Sym.takeError())
338 bool CheckFunctionsOnly) {
339 std::lock_guard<sys::Mutex> locked(
lock);
348 auto OptionalChildOrErr =
A->findSym(
Name);
349 if (!OptionalChildOrErr)
351 auto &OptionalChild = *OptionalChildOrErr;
355 OptionalChild->getAsBinary();
356 if (!ChildBinOrErr) {
361 std::unique_ptr<object::Binary> &ChildBin = ChildBinOrErr.
get();
362 if (ChildBin->isObject()) {
363 std::unique_ptr<object::ObjectFile> OF(
395 std::lock_guard<sys::Mutex> locked(
lock);
403 std::lock_guard<sys::Mutex> locked(
lock);
412 std::lock_guard<sys::Mutex> locked(
lock);
416 TM->getNameWithPrefix(
Name,
F, Mang);
418 if (
F->isDeclaration() ||
F->hasAvailableExternallyLinkage()) {
419 bool AbortOnFailure = !
F->hasExternalWeakLinkage();
426 bool HasBeenAddedButNotLoaded = OwnedModules.hasModuleBeenAddedButNotLoaded(M);
429 if (HasBeenAddedButNotLoaded)
431 else if (!OwnedModules.hasModuleBeenLoaded(M)) {
446void MCJIT::runStaticConstructorsDestructorsInModulePtrSet(
447 bool isDtors, ModulePtrSet::iterator
I, ModulePtrSet::iterator E) {
448 for (;
I != E; ++
I) {
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());
464 ModulePtrSet::iterator
I,
465 ModulePtrSet::iterator E) {
466 for (;
I != E; ++
I) {
468 if (
F && !
F->isDeclaration())
476 ModulePtrSet::iterator
I,
477 ModulePtrSet::iterator E) {
478 for (;
I != E; ++
I) {
488 Function *
F = FindFunctionNamedInModulePtrSet(
489 FnName, OwnedModules.begin_added(), OwnedModules.end_added());
491 F = FindFunctionNamedInModulePtrSet(FnName, OwnedModules.begin_loaded(),
492 OwnedModules.end_loaded());
494 F = FindFunctionNamedInModulePtrSet(FnName, OwnedModules.begin_finalized(),
495 OwnedModules.end_finalized());
501 Name, AllowInternal, OwnedModules.begin_added(), OwnedModules.end_added());
503 GV = FindGlobalVariableNamedInModulePtrSet(
Name, AllowInternal, OwnedModules.begin_loaded(),
504 OwnedModules.end_loaded());
506 GV = FindGlobalVariableNamedInModulePtrSet(
Name, AllowInternal, OwnedModules.begin_finalized(),
507 OwnedModules.end_finalized());
512 assert(
F &&
"Function *F was null at entry to run()");
516 assert(FPtr &&
"Pointer to fn's code was null after getPointerToFunction");
522 "Wrong number of arguments passed into function!");
524 "This doesn't support passing arguments through varargs (yet)!");
528 if (
RetTy->isIntegerTy(32) ||
RetTy->isVoidTy()) {
529 switch (ArgValues.
size()) {
534 int (*PF)(int,
char **,
const char **) =
535 (
int(*)(int,
char **,
const char **))(intptr_t)FPtr;
539 rv.
IntVal =
APInt(32, PF(ArgValues[0].IntVal.getZExtValue(),
540 (
char **)
GVTOP(ArgValues[1]),
541 (
const char **)
GVTOP(ArgValues[2])));
548 int (*PF)(int,
char **) = (
int(*)(int,
char **))(intptr_t)FPtr;
552 rv.
IntVal =
APInt(32, PF(ArgValues[0].IntVal.getZExtValue(),
553 (
char **)
GVTOP(ArgValues[1])));
561 int (*PF)(int) = (
int(*)(int))(intptr_t)FPtr;
562 rv.
IntVal =
APInt(32, PF(ArgValues[0].IntVal.getZExtValue()));
570 if (ArgValues.
empty()) {
572 switch (
RetTy->getTypeID()) {
591 rv.
IntVal =
APInt(32, ((
int(*)())(intptr_t)FPtr)());
594 rv.
FloatVal = ((float(*)())(intptr_t)FPtr)();
597 rv.
DoubleVal = ((double(*)())(intptr_t)FPtr)();
604 return PTOGV(((
void*(*)())(intptr_t)FPtr)());
609 "argument passing. Please use "
610 "ExecutionEngine::getFunctionAddress and cast the result "
611 "to the desired function pointer type.");
617 if (
auto AddrOrErr =
Sym.getAddress())
618 return reinterpret_cast<void*
>(
619 static_cast<uintptr_t
>(*AddrOrErr));
620 }
else if (
auto Err =
Sym.takeError())
629 if (AbortOnFailure) {
631 "' which could not be resolved!");
639 std::lock_guard<sys::Mutex> locked(
lock);
640 EventListeners.push_back(L);
646 std::lock_guard<sys::Mutex> locked(
lock);
648 if (
I != EventListeners.rend()) {
650 EventListeners.pop_back();
658 std::lock_guard<sys::Mutex> locked(
lock);
659 MemMgr->notifyObjectLoaded(
this, Obj);
661 EL->notifyObjectLoaded(Key, Obj, L);
667 std::lock_guard<sys::Mutex> locked(
lock);
669 L->notifyFreeingObject(Key);
679 return ClientResolver->findSymbol(
Name);
682void LinkingSymbolResolver::anchor() {}
static GCRegistry::Add< ErlangGC > A("erlang", "erlang-compatible garbage collector")
Module.h This file contains the declarations for the Module class.
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
Class for arbitrary precision integers.
ArrayRef - Represent a constant reference to an array (0 or more elements consecutively in memory),...
size_t size() const
size - Get the array size.
bool empty() const
empty - Check if the array is empty.
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.
Error takeError()
Take ownership of the stored error.
reference get()
Returns a reference to the stored T value.
Class to represent function types.
unsigned getNumParams() const
Return the number of fixed parameters this function type requires.
Type * getParamType(unsigned i) const
Parameter type accessors.
Type * getReturnType() const
bool isDeclaration() const
Return true if the primary definition of this global value is outside of the current translation unit...
JITTargetAddress getAddress() const
Return the address of this symbol.
JITEventListener - Abstract interface for use by the JIT to notify clients about significant events d...
static JITEventListener * createGDBRegistrationListener()
Represents a symbol in the JIT.
JITSymbol findSymbol(const std::string &Name) override
This method returns the address of the specified function or variable.
GenericValue runFunction(Function *F, ArrayRef< GenericValue > ArgValues) override
runFunction - Execute the specified function with the specified arguments, and return the result.
void * getPointerToNamedFunction(StringRef Name, bool AbortOnFailure=true) override
getPointerToNamedFunction - This method returns the address of the specified function by using the dl...
void RegisterJITEventListener(JITEventListener *L) override
Registers a listener to be called back on various events within the JIT.
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)
void runStaticConstructorsDestructors(bool isDtors) override
runStaticConstructorsDestructors - This method is used to execute all of the static constructors or d...
Module * findModuleForSymbol(const std::string &Name, bool CheckFunctionsOnly)
void addArchive(object::OwningBinary< object::Archive > O) override
addArchive - Add an Archive to the execution engine.
Function * FindFunctionNamed(StringRef FnName) override
FindFunctionNamed - Search all of the active modules to find the function that defines FnName.
void finalizeObject() override
finalizeObject - ensure the module is fully processed and is usable.
void finalizeLoadedModules()
void notifyObjectLoaded(const object::ObjectFile &Obj, const RuntimeDyld::LoadedObjectInfo &L)
void generateCodeForModule(Module *M) override
generateCodeForModule - Run code generation for the specified module and load it into memory.
uint64_t getFunctionAddress(const std::string &Name) override
getFunctionAddress - Return the address of the specified function.
void addObjectFile(std::unique_ptr< object::ObjectFile > O) override
addObjectFile - Add an ObjectFile to the execution engine.
std::unique_ptr< MemoryBuffer > emitObject(Module *M)
emitObject – Generate a JITed object in memory from the specified module Currently,...
void UnregisterJITEventListener(JITEventListener *L) override
bool removeModule(Module *M) override
removeModule - Removes a Module from the list of modules, but does not free the module's memory.
JITSymbol findExistingSymbol(const std::string &Name)
GlobalVariable * FindGlobalVariableNamed(StringRef Name, bool AllowInternal=false) override
FindGlobalVariableNamed - Search all of the active modules to find the global variable that defines N...
uint64_t getGlobalValueAddress(const std::string &Name) override
getGlobalValueAddress - Return the address of the specified global value.
uint64_t getSymbolAddress(const std::string &Name, bool CheckFunctionsOnly)
void notifyFreeingObject(const object::ObjectFile &Obj)
virtual void finalizeModule(Module *)
JITSymbol findSymbol(const std::string &Name, bool CheckFunctionsOnly)
void * getPointerToFunction(Function *F) override
getPointerToFunction - The different EE's represent function bodies in different ways.
void setObjectCache(ObjectCache *manager) override
Sets the object manager that MCJIT should use to avoid compilation.
void addModule(std::unique_ptr< Module > M) override
Add a Module to the list of modules that we can JIT from.
void getNameWithPrefix(raw_ostream &OS, const GlobalValue *GV, bool CannotUsePrivateLabel) const
Print the appropriate prefix and the specified global variable's name.
A Module instance is used to store all the information related to an LLVM module.
This is the base ObjectCache type which can be provided to an ExecutionEngine for the purpose of avoi...
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.
Information about the loaded object.
void resolveRelocations()
Resolve the relocations for all symbols we currently know about.
void deregisterEHFrames()
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.
SmallString - A SmallString is just a SmallVector with methods and accessors that make it work better...
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small.
StringRef - Represent a constant reference to a string, i.e.
std::string str() const
str - Get the contents as an std::string.
constexpr StringRef substr(size_t Start, size_t N=npos) const
Return a reference to the substring from [Start, Start + N).
constexpr const char * data() const
data - Get a pointer to the start of the string (which may not be null terminated).
Twine - A lightweight data structure for efficiently representing the concatenation of temporary valu...
The instances of the Type class are immutable: once they are created, they are never changed.
bool isPointerTy() const
True if this is an instance of PointerType.
@ VoidTyID
type with no size
@ FloatTyID
32-bit floating point type
@ IntegerTyID
Arbitrary bit width integers.
@ DoubleTyID
64-bit floating point type
@ X86_FP80TyID
80-bit floating point type (X87)
@ PPC_FP128TyID
128-bit floating point type (two 64-bits, PowerPC)
@ FP128TyID
128-bit floating point type (112-bit significand)
bool isIntegerTy() const
True if this is an instance of IntegerType.
PassManager manages ModulePassManagers.
bool run(Module &M)
run - Execute all of the passes scheduled for execution.
StringRef getData() const
This class is the base class for all object file types.
static Expected< OwningBinary< ObjectFile > > createObjectFile(StringRef ObjectPath)
std::pair< std::unique_ptr< T >, std::unique_ptr< MemoryBuffer > > takeBinary()
A raw_ostream that writes to an std::string.
A raw_ostream that writes to an SmallVector or SmallString.
static bool LoadLibraryPermanently(const char *Filename, std::string *ErrMsg=nullptr)
This function permanently loads the dynamic library at the given path.
#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.
void logAllUnhandledErrors(Error E, raw_ostream &OS, Twine ErrorBanner={})
Log all errors (if any) in E to OS.
auto find(R &&Range, const T &Val)
Provide wrappers to std::find which take ranges instead of having to pass begin/end explicitly.
auto reverse(ContainerTy &&C)
GenericValue PTOGV(void *P)
void report_fatal_error(Error Err, bool gen_crash_diag=true)
Report a serious error, calling any installed error handler.
@ 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.
constexpr unsigned BitWidth
OutputIt move(R &&Range, OutputIt Out)
Provide wrappers to std::move which take ranges instead of having to pass begin/end explicitly.
void * GVTOP(const GenericValue &GV)
void consumeError(Error Err)
Consume a Error without doing anything.
Implement std::hash so that hash_code can be used in STL containers.
void swap(llvm::BitVector &LHS, llvm::BitVector &RHS)
Implement std::swap in terms of BitVector swap.