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 SymbolFlags[S] = Sym.getFlags();
153 }
else if (
auto Err = Sym.takeError()) {
154 M.reportError(std::move(Err));
157 if (
auto Sym2 = M.ClientResolver->findSymbolInLogicalDylib(*S)) {
158 SymbolFlags[S] = Sym2.getFlags();
159 }
else if (
auto Err = Sym2.takeError()) {
160 M.reportError(std::move(Err));
172 bool NewSymbolsResolved =
false;
174 for (
auto &S : Symbols) {
175 if (
auto Sym = M.findMangledSymbol(*S)) {
176 if (
auto Addr = Sym.getAddress()) {
178 Query->notifySymbolReady();
179 NewSymbolsResolved =
true;
184 }
else if (
auto Err = Sym.takeError()) {
188 if (
auto Sym2 = M.ClientResolver->findSymbol(*S)) {
189 if (
auto Addr = Sym2.getAddress()) {
191 Query->notifySymbolReady();
192 NewSymbolsResolved =
true;
197 }
else if (
auto Err = Sym2.takeError()) {
201 UnresolvedSymbols.insert(S);
205 if (NewSymbolsResolved && Query->isFullyResolved())
206 Query->handleFullyResolved();
208 if (NewSymbolsResolved && Query->isFullyReady())
209 Query->handleFullyReady();
211 return UnresolvedSymbols;
220 createOrcMCJITReplacement(std::string *ErrorMsg,
221 std::shared_ptr<MCJITMemoryManager> MemMgr,
222 std::shared_ptr<LegacyJITSymbolResolver>
Resolver,
223 std::unique_ptr<TargetMachine> TM) {
228 void reportError(
Error Err) {
234 std::shared_ptr<LegacyJITSymbolResolver> ClientResolver,
235 std::unique_ptr<TargetMachine> TM)
239 std::make_shared<MCJITReplacementMemMgr>(*this,
std::move(MemMgr))),
240 Resolver(
std::make_shared<LinkingORCResolver>(*this)),
241 ClientResolver(
std::move(ClientResolver)), NotifyObjectLoaded(*this),
242 NotifyFinalized(*this),
248 NotifyObjectLoaded, NotifyFinalized),
250 [
this](
VModuleKey K, std::unique_ptr<Module> M) {
251 Modules.push_back(std::move(M));
253 LazyEmitLayer(CompileLayer) {}
262 if (M->getDataLayout().isDefault()) {
271 std::vector<std::string> CtorNames, DtorNames;
273 unsigned CtorId = 0, DtorId = 0;
275 std::string NewCtorName = (
"$static_ctor." +
Twine(CtorId++)).str();
276 Ctor.Func->setName(NewCtorName);
279 CtorNames.push_back(mangle(NewCtorName));
282 std::string NewDtorName = (
"$static_dtor." +
Twine(DtorId++)).str();
283 dbgs() <<
"Found dtor: " << NewDtorName <<
"\n";
284 Dtor.Func->setName(NewDtorName);
287 DtorNames.push_back(mangle(NewDtorName));
291 auto K = ES.allocateVModule();
293 UnexecutedConstructors[K] = std::move(CtorNames);
294 UnexecutedDestructors[K] = std::move(DtorNames);
296 cantFail(LazyEmitLayer.addModule(K, std::move(M)));
305 std::unique_ptr<object::ObjectFile> Obj;
306 std::unique_ptr<MemoryBuffer> ObjBuffer;
312 Archives.push_back(std::move(A));
331 return findMangledSymbol(mangle(Name));
340 uint64_t TargetAddress)
override {
341 for (
auto &
P : UnfinalizedSections)
342 if (
P.second.count(LocalAddress))
343 ObjectLayer.mapSectionAddress(
P.first, LocalAddress, TargetAddress);
356 return reinterpret_cast<void *
>(
static_cast<uintptr_t
>(FAddr));
360 bool AbortOnFailure =
true)
override {
362 if (!Addr && AbortOnFailure)
364 return reinterpret_cast<void *
>(
static_cast<uintptr_t
>(Addr));
371 CompileLayer.getCompiler().setObjectCache(NewCache);
375 ObjectLayer.setProcessAllSections(ProcessAllSections);
382 if (
auto Sym = LazyEmitLayer.findSymbol(Name,
false))
384 if (
auto Sym = ClientResolver->findSymbol(Name))
386 if (
auto Sym = scanArchives(Name))
396 auto OptionalChildOrErr = A->
findSym(Name);
397 if (!OptionalChildOrErr)
399 auto &OptionalChild = *OptionalChildOrErr;
403 OptionalChild->getAsBinary();
404 if (!ChildBinOrErr) {
409 std::unique_ptr<object::Binary> &ChildBin = ChildBinOrErr.
get();
410 if (ChildBin->isObject()) {
412 ES.allocateVModule(),
422 class NotifyObjectLoadedT {
424 using LoadedObjInfoListT =
425 std::vector<std::unique_ptr<RuntimeDyld::LoadedObjectInfo>>;
431 M.UnfinalizedSections[K] = std::move(M.SectionsAllocatedSinceLastLoad);
432 M.SectionsAllocatedSinceLastLoad = SectionAddrSet();
433 M.MemMgr->notifyObjectLoaded(&M, Obj);
439 class NotifyFinalizedT {
445 M.UnfinalizedSections.erase(K);
453 std::string MangledName;
456 Mang.getNameWithPrefix(MangledNameStream, Name,
getDataLayout());
467 std::unique_ptr<TargetMachine> TM;
468 std::shared_ptr<MCJITReplacementMemMgr> MemMgr;
469 std::shared_ptr<LinkingORCResolver> Resolver;
470 std::shared_ptr<LegacyJITSymbolResolver> ClientResolver;
476 std::map<Module*, bool> ShouldDelete;
478 NotifyObjectLoadedT NotifyObjectLoaded;
479 NotifyFinalizedT NotifyFinalized;
485 std::map<VModuleKey, std::vector<std::string>> UnexecutedConstructors;
486 std::map<VModuleKey, std::vector<std::string>> UnexecutedDestructors;
491 using SectionAddrSet = std::set<const void *>;
492 SectionAddrSet SectionsAllocatedSinceLastLoad;
493 std::map<VModuleKey, SectionAddrSet> UnfinalizedSections;
495 std::vector<object::OwningBinary<object::Archive>> Archives;
502 #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.
Compute iterated dominance frontiers using a linear time algorithm.
A Module instance is used to store all the information related to an LLVM module. ...
void logAllUnhandledErrors(Error E, raw_ostream &OS, Twine ErrorBanner)
Log all errors (if any) in E to OS.
void finalizeObject() override
finalizeObject - ensure the module is fully processed and is usable.
Expected< Optional< Child > > findSym(StringRef name) const
Bare bones object linking layer.
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.
void legacyFailQuery(AsynchronousSymbolQuery &Q, Error Err)
uint64_t VModuleKey
VModuleKey provides a unique identifier (allocated and managed by ExecutionSessions) for a module add...
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.
std::map< SymbolStringPtr, JITSymbolFlags > SymbolFlagsMap
A map from symbol names (as SymbolStringPtrs) to JITSymbolFlags.
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)...
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.
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.
void setObjectCache(ObjectCache *NewCache) override
Sets the pre-compiled object cache.
void consumeError(Error Err)
Consume a Error without doing anything.
#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)
Expected< SymbolMap > lookup(const VSOList &VSOs, SymbolNameSet Names)
Look up the given names in the given VSOs.
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.
constexpr char Size[]
Key for Kernel::Arg::Metadata::mSize.
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.
std::set< SymbolStringPtr > SymbolNameSet
A set of symbol names (represented by SymbolStringPtrs for.
void mapSectionAddress(const void *LocalAddress, uint64_t TargetAddress) override
mapSectionAddress - map a section to its target address space value.