13 #ifndef LLVM_LIB_EXECUTIONENGINE_ORC_ORCMCJITREPLACEMENT_H 14 #define LLVM_LIB_EXECUTIONENGINE_ORC_ORCMCJITREPLACEMENT_H 64 std::shared_ptr<MCJITMemoryManager> ClientMM)
65 : M(M), ClientMM(std::move(ClientMM)) {}
67 uint8_t *allocateCodeSection(uintptr_t
Size,
unsigned Alignment,
71 ClientMM->allocateCodeSection(Size, Alignment, SectionID,
73 M.SectionsAllocatedSinceLastLoad.insert(Addr);
77 uint8_t *allocateDataSection(uintptr_t Size,
unsigned Alignment,
78 unsigned SectionID,
StringRef SectionName,
79 bool IsReadOnly)
override {
80 uint8_t *Addr = ClientMM->allocateDataSection(Size, Alignment, SectionID,
81 SectionName, IsReadOnly);
82 M.SectionsAllocatedSinceLastLoad.insert(Addr);
86 void reserveAllocationSpace(uintptr_t CodeSize,
uint32_t CodeAlign,
87 uintptr_t RODataSize,
uint32_t RODataAlign,
90 return ClientMM->reserveAllocationSpace(CodeSize, CodeAlign,
91 RODataSize, RODataAlign,
92 RWDataSize, RWDataAlign);
95 bool needsToReserveAllocationSpace()
override {
96 return ClientMM->needsToReserveAllocationSpace();
99 void registerEHFrames(uint8_t *Addr, uint64_t LoadAddr,
100 size_t Size)
override {
101 return ClientMM->registerEHFrames(Addr, LoadAddr, Size);
104 void deregisterEHFrames()
override {
105 return ClientMM->deregisterEHFrames();
110 return ClientMM->notifyObjectLoaded(RTDyld, O);
115 return ClientMM->notifyObjectLoaded(EE, O);
118 bool finalizeMemory(std::string *ErrMsg =
nullptr)
override {
132 if (M.UnfinalizedSections.size() == 1)
133 return ClientMM->finalizeMemory(ErrMsg);
139 std::shared_ptr<MCJITMemoryManager> ClientMM;
149 for (
auto &S : Symbols) {
150 if (
auto Sym = M.findMangledSymbol(*S)) {
151 if (!Sym.getFlags().isStrong())
153 }
else if (
auto Err = Sym.takeError()) {
154 M.reportError(std::move(Err));
157 if (
auto Sym2 = M.ClientResolver->findSymbolInLogicalDylib(*S)) {
158 if (!Sym2.getFlags().isStrong())
160 }
else if (
auto Err = Sym2.takeError()) {
161 M.reportError(std::move(Err));
174 bool NewSymbolsResolved =
false;
176 for (
auto &S : Symbols) {
177 if (
auto Sym = M.findMangledSymbol(*S)) {
178 if (
auto Addr = Sym.getAddress()) {
179 Query->notifySymbolMetRequiredState(
181 NewSymbolsResolved =
true;
186 }
else if (
auto Err = Sym.takeError()) {
190 if (
auto Sym2 = M.ClientResolver->findSymbol(*S)) {
191 if (
auto Addr = Sym2.getAddress()) {
192 Query->notifySymbolMetRequiredState(
194 NewSymbolsResolved =
true;
199 }
else if (
auto Err = Sym2.takeError()) {
203 UnresolvedSymbols.
insert(S);
207 if (NewSymbolsResolved && Query->isComplete())
208 Query->handleComplete();
210 return UnresolvedSymbols;
219 createOrcMCJITReplacement(std::string *ErrorMsg,
220 std::shared_ptr<MCJITMemoryManager> MemMgr,
221 std::shared_ptr<LegacyJITSymbolResolver>
Resolver,
222 std::unique_ptr<TargetMachine> TM) {
227 void reportError(
Error Err) {
233 std::shared_ptr<LegacyJITSymbolResolver> ClientResolver,
234 std::unique_ptr<TargetMachine> TM)
237 std::make_shared<MCJITReplacementMemMgr>(*this,
std::move(MemMgr))),
238 Resolver(
std::make_shared<LinkingORCResolver>(*this)),
239 ClientResolver(
std::move(ClientResolver)), NotifyObjectLoaded(*this),
240 NotifyFinalized(*this),
246 NotifyObjectLoaded, NotifyFinalized),
249 [
this](
VModuleKey K, std::unique_ptr<Module> M) {
250 Modules.push_back(std::move(M));
261 if (M->getDataLayout().isDefault()) {
270 std::vector<std::string> CtorNames, DtorNames;
272 unsigned CtorId = 0, DtorId = 0;
274 std::string NewCtorName = (
"__ORCstatic_ctor." +
Twine(CtorId++)).str();
275 Ctor.Func->setName(NewCtorName);
278 CtorNames.push_back(mangle(NewCtorName));
281 std::string NewDtorName = (
"__ORCstatic_dtor." +
Twine(DtorId++)).str();
282 dbgs() <<
"Found dtor: " << NewDtorName <<
"\n";
283 Dtor.Func->setName(NewDtorName);
286 DtorNames.push_back(mangle(NewDtorName));
290 auto K = ES.allocateVModule();
292 UnexecutedConstructors[K] = std::move(CtorNames);
293 UnexecutedDestructors[K] = std::move(DtorNames);
295 cantFail(LazyEmitLayer.addModule(K, std::move(M)));
304 std::unique_ptr<object::ObjectFile> Obj;
305 std::unique_ptr<MemoryBuffer> ObjBuffer;
311 Archives.push_back(std::move(A));
330 return findMangledSymbol(mangle(Name));
339 uint64_t TargetAddress)
override {
340 for (
auto &
P : UnfinalizedSections)
341 if (
P.second.count(LocalAddress))
342 ObjectLayer.mapSectionAddress(
P.first, LocalAddress, TargetAddress);
355 return reinterpret_cast<void *
>(
static_cast<uintptr_t
>(FAddr));
359 bool AbortOnFailure =
true)
override {
361 if (!Addr && AbortOnFailure)
363 return reinterpret_cast<void *
>(
static_cast<uintptr_t
>(Addr));
370 CompileLayer.getCompiler().setObjectCache(NewCache);
374 ObjectLayer.setProcessAllSections(ProcessAllSections);
381 if (
auto Sym = LazyEmitLayer.findSymbol(Name,
false))
383 if (
auto Sym = ClientResolver->findSymbol(Name))
385 if (
auto Sym = scanArchives(Name))
395 auto OptionalChildOrErr = A->
findSym(Name);
396 if (!OptionalChildOrErr)
398 auto &OptionalChild = *OptionalChildOrErr;
402 OptionalChild->getAsBinary();
403 if (!ChildBinOrErr) {
408 std::unique_ptr<object::Binary> &ChildBin = ChildBinOrErr.
get();
409 if (ChildBin->isObject()) {
411 ES.allocateVModule(),
421 class NotifyObjectLoadedT {
423 using LoadedObjInfoListT =
424 std::vector<std::unique_ptr<RuntimeDyld::LoadedObjectInfo>>;
430 M.UnfinalizedSections[K] = std::move(M.SectionsAllocatedSinceLastLoad);
431 M.SectionsAllocatedSinceLastLoad = SectionAddrSet();
432 M.MemMgr->notifyObjectLoaded(&M, Obj);
438 class NotifyFinalizedT {
444 M.UnfinalizedSections.erase(K);
452 std::string MangledName;
455 Mang.getNameWithPrefix(MangledNameStream, Name,
getDataLayout());
466 std::unique_ptr<TargetMachine> TM;
467 std::shared_ptr<MCJITReplacementMemMgr> MemMgr;
468 std::shared_ptr<LinkingORCResolver> Resolver;
469 std::shared_ptr<LegacyJITSymbolResolver> ClientResolver;
475 std::map<Module*, bool> ShouldDelete;
477 NotifyObjectLoadedT NotifyObjectLoaded;
478 NotifyFinalizedT NotifyFinalized;
484 std::map<VModuleKey, std::vector<std::string>> UnexecutedConstructors;
485 std::map<VModuleKey, std::vector<std::string>> UnexecutedDestructors;
490 using SectionAddrSet = std::set<const void *>;
491 SectionAddrSet SectionsAllocatedSinceLastLoad;
492 std::map<VModuleKey, SectionAddrSet> UnfinalizedSections;
494 std::vector<object::OwningBinary<object::Archive>> Archives;
501 #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. ...
amdgpu Simplify well known AMD library false FunctionCallee Value const Twine & Name
void finalizeObject() override
finalizeObject - ensure the module is fully processed and is usable.
Expected< Optional< Child > > findSym(StringRef name) const
static bool lookup(const GsymReader &GR, DataExtractor &Data, uint64_t &Offset, uint64_t BaseAddr, uint64_t Addr, SourceLocations &SrcLocs, llvm::Error &Err)
A Lookup helper functions.
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.
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.
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.
uint8_t uint64_t VModuleKey
VModuleKey provides a unique identifier (allocated and managed by ExecutionSessions) for a module add...
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.
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.