14 #ifndef LLVM_LIB_EXECUTIONENGINE_ORC_ORCMCJITREPLACEMENT_H 15 #define LLVM_LIB_EXECUTIONENGINE_ORC_ORCMCJITREPLACEMENT_H 65 std::shared_ptr<MCJITMemoryManager> ClientMM)
66 : M(M), ClientMM(std::move(ClientMM)) {}
68 uint8_t *allocateCodeSection(uintptr_t
Size,
unsigned Alignment,
72 ClientMM->allocateCodeSection(Size, Alignment, SectionID,
74 M.SectionsAllocatedSinceLastLoad.insert(Addr);
78 uint8_t *allocateDataSection(uintptr_t Size,
unsigned Alignment,
79 unsigned SectionID,
StringRef SectionName,
80 bool IsReadOnly)
override {
81 uint8_t *Addr = ClientMM->allocateDataSection(Size, Alignment, SectionID,
82 SectionName, IsReadOnly);
83 M.SectionsAllocatedSinceLastLoad.insert(Addr);
87 void reserveAllocationSpace(uintptr_t CodeSize,
uint32_t CodeAlign,
88 uintptr_t RODataSize,
uint32_t RODataAlign,
91 return ClientMM->reserveAllocationSpace(CodeSize, CodeAlign,
92 RODataSize, RODataAlign,
93 RWDataSize, RWDataAlign);
96 bool needsToReserveAllocationSpace()
override {
97 return ClientMM->needsToReserveAllocationSpace();
100 void registerEHFrames(uint8_t *Addr, uint64_t LoadAddr,
101 size_t Size)
override {
102 return ClientMM->registerEHFrames(Addr, LoadAddr, Size);
105 void deregisterEHFrames()
override {
106 return ClientMM->deregisterEHFrames();
111 return ClientMM->notifyObjectLoaded(RTDyld, O);
116 return ClientMM->notifyObjectLoaded(EE, O);
119 bool finalizeMemory(std::string *ErrMsg =
nullptr)
override {
133 if (M.UnfinalizedSections.size() == 1)
134 return ClientMM->finalizeMemory(ErrMsg);
140 std::shared_ptr<MCJITMemoryManager> ClientMM;
150 for (
auto &S : Symbols) {
151 if (
auto Sym = M.findMangledSymbol(*S)) {
152 if (!Sym.getFlags().isStrong())
154 }
else if (
auto Err = Sym.takeError()) {
155 M.reportError(std::move(Err));
158 if (
auto Sym2 = M.ClientResolver->findSymbolInLogicalDylib(*S)) {
159 if (!Sym2.getFlags().isStrong())
161 }
else if (
auto Err = Sym2.takeError()) {
162 M.reportError(std::move(Err));
175 bool NewSymbolsResolved =
false;
177 for (
auto &S : Symbols) {
178 if (
auto Sym = M.findMangledSymbol(*S)) {
179 if (
auto Addr = Sym.getAddress()) {
181 Query->notifySymbolReady();
182 NewSymbolsResolved =
true;
187 }
else if (
auto Err = Sym.takeError()) {
191 if (
auto Sym2 = M.ClientResolver->findSymbol(*S)) {
192 if (
auto Addr = Sym2.getAddress()) {
194 Query->notifySymbolReady();
195 NewSymbolsResolved =
true;
200 }
else if (
auto Err = Sym2.takeError()) {
204 UnresolvedSymbols.
insert(S);
208 if (NewSymbolsResolved && Query->isFullyResolved())
209 Query->handleFullyResolved();
211 if (NewSymbolsResolved && Query->isFullyReady())
212 Query->handleFullyReady();
214 return UnresolvedSymbols;
223 createOrcMCJITReplacement(std::string *ErrorMsg,
224 std::shared_ptr<MCJITMemoryManager> MemMgr,
225 std::shared_ptr<LegacyJITSymbolResolver>
Resolver,
226 std::unique_ptr<TargetMachine> TM) {
231 void reportError(
Error Err) {
237 std::shared_ptr<LegacyJITSymbolResolver> ClientResolver,
238 std::unique_ptr<TargetMachine> TM)
242 std::make_shared<MCJITReplacementMemMgr>(*this,
std::move(MemMgr))),
243 Resolver(
std::make_shared<LinkingORCResolver>(*this)),
244 ClientResolver(
std::move(ClientResolver)), NotifyObjectLoaded(*this),
245 NotifyFinalized(*this),
251 NotifyObjectLoaded, NotifyFinalized),
253 [
this](
VModuleKey K, std::unique_ptr<Module> M) {
254 Modules.push_back(std::move(M));
256 LazyEmitLayer(CompileLayer) {}
265 if (M->getDataLayout().isDefault()) {
274 std::vector<std::string> CtorNames, DtorNames;
276 unsigned CtorId = 0, DtorId = 0;
278 std::string NewCtorName = (
"__ORCstatic_ctor." +
Twine(CtorId++)).str();
279 Ctor.Func->setName(NewCtorName);
282 CtorNames.push_back(mangle(NewCtorName));
285 std::string NewDtorName = (
"__ORCstatic_dtor." +
Twine(DtorId++)).str();
286 dbgs() <<
"Found dtor: " << NewDtorName <<
"\n";
287 Dtor.Func->setName(NewDtorName);
290 DtorNames.push_back(mangle(NewDtorName));
294 auto K = ES.allocateVModule();
296 UnexecutedConstructors[K] = std::move(CtorNames);
297 UnexecutedDestructors[K] = std::move(DtorNames);
299 cantFail(LazyEmitLayer.addModule(K, std::move(M)));
308 std::unique_ptr<object::ObjectFile> Obj;
309 std::unique_ptr<MemoryBuffer> ObjBuffer;
315 Archives.push_back(std::move(A));
334 return findMangledSymbol(mangle(Name));
343 uint64_t TargetAddress)
override {
344 for (
auto &
P : UnfinalizedSections)
345 if (
P.second.count(LocalAddress))
346 ObjectLayer.mapSectionAddress(
P.first, LocalAddress, TargetAddress);
359 return reinterpret_cast<void *
>(
static_cast<uintptr_t
>(FAddr));
363 bool AbortOnFailure =
true)
override {
365 if (!Addr && AbortOnFailure)
367 return reinterpret_cast<void *
>(
static_cast<uintptr_t
>(Addr));
374 CompileLayer.getCompiler().setObjectCache(NewCache);
378 ObjectLayer.setProcessAllSections(ProcessAllSections);
385 if (
auto Sym = LazyEmitLayer.findSymbol(Name,
false))
387 if (
auto Sym = ClientResolver->findSymbol(Name))
389 if (
auto Sym = scanArchives(Name))
399 auto OptionalChildOrErr = A->
findSym(Name);
400 if (!OptionalChildOrErr)
402 auto &OptionalChild = *OptionalChildOrErr;
406 OptionalChild->getAsBinary();
407 if (!ChildBinOrErr) {
412 std::unique_ptr<object::Binary> &ChildBin = ChildBinOrErr.
get();
413 if (ChildBin->isObject()) {
415 ES.allocateVModule(),
425 class NotifyObjectLoadedT {
427 using LoadedObjInfoListT =
428 std::vector<std::unique_ptr<RuntimeDyld::LoadedObjectInfo>>;
434 M.UnfinalizedSections[K] = std::move(M.SectionsAllocatedSinceLastLoad);
435 M.SectionsAllocatedSinceLastLoad = SectionAddrSet();
436 M.MemMgr->notifyObjectLoaded(&M, Obj);
442 class NotifyFinalizedT {
448 M.UnfinalizedSections.erase(K);
456 std::string MangledName;
459 Mang.getNameWithPrefix(MangledNameStream, Name,
getDataLayout());
470 std::unique_ptr<TargetMachine> TM;
471 std::shared_ptr<MCJITReplacementMemMgr> MemMgr;
472 std::shared_ptr<LinkingORCResolver> Resolver;
473 std::shared_ptr<LegacyJITSymbolResolver> ClientResolver;
479 std::map<Module*, bool> ShouldDelete;
481 NotifyObjectLoadedT NotifyObjectLoaded;
482 NotifyFinalizedT NotifyFinalized;
488 std::map<VModuleKey, std::vector<std::string>> UnexecutedConstructors;
489 std::map<VModuleKey, std::vector<std::string>> UnexecutedDestructors;
494 using SectionAddrSet = std::set<const void *>;
495 SectionAddrSet SectionsAllocatedSinceLastLoad;
496 std::map<VModuleKey, SectionAddrSet> UnfinalizedSections;
498 std::vector<object::OwningBinary<object::Archive>> Archives;
505 #endif // LLVM_LIB_EXECUTIONENGINE_ORC_MCJITREPLACEMENT_H void addObjectFile(std::unique_ptr< object::ObjectFile > O) override
addObjectFile - Add an ObjectFile to the execution engine.
void cantFail(Error Err, const char *Msg=nullptr)
Report a fatal error if Err is a failure value.
Information about the loaded object.
raw_ostream & errs()
This returns a reference to a raw_ostream for standard error.
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.
A Module instance is used to store all the information related to an LLVM module. ...
void finalizeObject() override
finalizeObject - ensure the module is fully processed and is usable.
Expected< Optional< Child > > findSym(StringRef name) const
This class is the base class for all object file types.
Externally visible function.
const DataLayout & getDataLayout() const
Error takeError()
Take ownership of the stored error.
uint64_t VModuleKey
VModuleKey provides a unique identifier (allocated and managed by ExecutionSessions) for a module add...
amdgpu Simplify well known AMD library false Value Value const Twine & Name
Twine - A lightweight data structure for efficiently representing the concatenation of temporary valu...
uint64_t getFunctionAddress(const std::string &Name) override
getFunctionAddress - Return the address of the specified function.
Tagged union holding either a T or a Error.
Simple compile functor: Takes a single IR module and returns an ObjectFile.
static const uint16_t * lookup(unsigned opcode, unsigned domain, ArrayRef< uint16_t[3]> Table)
OrcMCJITReplacement(std::shared_ptr< MCJITMemoryManager > MemMgr, std::shared_ptr< LegacyJITSymbolResolver > ClientResolver, std::unique_ptr< TargetMachine > TM)
iterator_range< CtorDtorIterator > getDestructors(const Module &M)
Create an iterator range over the entries of the llvm.global_ctors array.
bool removeModule(Module *M) override
removeModule - Removes a Module from the list of modules, but does not free the module's memory...
ArrayRef - Represent a constant reference to an array (0 or more elements consecutively in memory)...
void legacyFailQuery(AsynchronousSymbolQuery &Q, Error Err)
Analysis containing CSE Info
GenericValue runFunction(Function *F, ArrayRef< GenericValue > ArgValues) override
runFunction - Execute the specified function with the specified arguments, and return the result...
uint64_t getGlobalValueAddress(const std::string &Name) override
getGlobalValueAddress - Return the address of the specified global value.
DenseSet< SymbolStringPtr > SymbolNameSet
A set of symbol names (represented by SymbolStringPtrs for.
static GCRegistry::Add< CoreCLRGC > E("coreclr", "CoreCLR-compatible GC")
void addObjectFile(object::OwningBinary< object::ObjectFile > O) override
Interface for looking up the initializer for a variable name, used by Init::resolveReferences.
std::pair< iterator, bool > insert(const ValueT &V)
void setObjectCache(ObjectCache *NewCache) override
Sets the pre-compiled object cache.
void consumeError(Error Err)
Consume a Error without doing anything.
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.
void * getPointerToFunction(Function *F) override
getPointerToFunction - The different EE's represent function bodies in different ways.
Abstract interface for implementation execution of LLVM modules, designed to support both interpreter...
std::pair< std::unique_ptr< T >, std::unique_ptr< MemoryBuffer > > takeBinary()
void runStaticConstructorsDestructors(bool isDtors) override
runStaticConstructorsDestructors - This method is used to execute all of the static constructors or d...
Module.h This file contains the declarations for the Module class.
JITSymbol findSymbol(StringRef Name)
uint64_t getSymbolAddress(StringRef Name)
reference get()
Returns a reference to the stored T value.
An ExecutionSession represents a running JIT program.
raw_ostream & dbgs()
dbgs() - This returns a reference to a raw_ostream for debugging messages.
SymbolResolver is a composable interface for looking up symbol flags and addresses using the Asynchro...
static std::unique_ptr< MemoryBuffer > getMemBufferCopy(StringRef InputData, const Twine &BufferName="")
Open the specified memory range as a MemoryBuffer, copying the contents and taking ownership of it...
static ExecutionEngine *(* OrcMCJITReplacementCtor)(std::string *ErrorStr, std::shared_ptr< MCJITMemoryManager > MM, std::shared_ptr< LegacyJITSymbolResolver > SR, std::unique_ptr< TargetMachine > TM)
Represents a symbol that has been evaluated to an address already.
SmallVector< std::unique_ptr< Module >, 1 > Modules
The list of Modules that we are JIT'ing from.
StringRef getName() const
Return a constant reference to the value's name.
iterator_range< CtorDtorIterator > getConstructors(const Module &M)
Create an iterator range over the entries of the llvm.global_ctors array.
static void Query(const MachineInstr &MI, AliasAnalysis &AA, bool &Read, bool &Write, bool &Effects, bool &StackPointer)
void setProcessAllSections(bool ProcessAllSections) override
setProcessAllSections (MCJIT Only): By default, only sections that are "required for execution" are p...
This is the base ObjectCache type which can be provided to an ExecutionEngine for the purpose of avoi...
void addModule(std::unique_ptr< Module > M) override
Add a Module to the list of modules that we can JIT from.
void * getPointerToNamedFunction(StringRef Name, bool AbortOnFailure=true) override
getPointerToNamedFunction - This method returns the address of the specified function by using the dl...
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
A raw_ostream that writes to an std::string.
void addArchive(object::OwningBinary< object::Archive > A) override
addArchive - Add an Archive to the execution engine.
Lightweight error class with error context and mandatory checking.
Interface for Layers that accept object files.
StringRef - Represent a constant reference to a string, i.e.
Bare bones object linking layer.
void mapSectionAddress(const void *LocalAddress, uint64_t TargetAddress) override
mapSectionAddress - map a section to its target address space value.