14 #ifndef LLVM_LIB_EXECUTIONENGINE_ORC_ORCMCJITREPLACEMENT_H
15 #define LLVM_LIB_EXECUTIONENGINE_ORC_ORCMCJITREPLACEMENT_H
63 std::shared_ptr<MCJITMemoryManager> ClientMM)
64 : M(M), ClientMM(std::move(ClientMM)) {}
66 uint8_t *allocateCodeSection(uintptr_t
Size,
unsigned Alignment,
70 ClientMM->allocateCodeSection(Size, Alignment, SectionID,
72 M.SectionsAllocatedSinceLastLoad.insert(Addr);
76 uint8_t *allocateDataSection(uintptr_t Size,
unsigned Alignment,
77 unsigned SectionID,
StringRef SectionName,
78 bool IsReadOnly)
override {
79 uint8_t *Addr = ClientMM->allocateDataSection(Size, Alignment, SectionID,
80 SectionName, IsReadOnly);
81 M.SectionsAllocatedSinceLastLoad.insert(Addr);
85 void reserveAllocationSpace(uintptr_t CodeSize,
uint32_t CodeAlign,
86 uintptr_t RODataSize,
uint32_t RODataAlign,
89 return ClientMM->reserveAllocationSpace(CodeSize, CodeAlign,
90 RODataSize, RODataAlign,
91 RWDataSize, RWDataAlign);
94 bool needsToReserveAllocationSpace()
override {
95 return ClientMM->needsToReserveAllocationSpace();
98 void registerEHFrames(uint8_t *Addr, uint64_t LoadAddr,
99 size_t Size)
override {
100 return ClientMM->registerEHFrames(Addr, LoadAddr, Size);
103 void deregisterEHFrames()
override {
104 return ClientMM->deregisterEHFrames();
109 return ClientMM->notifyObjectLoaded(RTDyld, O);
114 return ClientMM->notifyObjectLoaded(EE, O);
117 bool finalizeMemory(std::string *
ErrMsg =
nullptr)
override {
131 if (M.UnfinalizedSections.size() == 1)
132 return ClientMM->finalizeMemory(
ErrMsg);
138 std::shared_ptr<MCJITMemoryManager> ClientMM;
146 return M.ClientResolver->findSymbol(Name);
149 JITSymbol findSymbolInLogicalDylib(
const std::string &Name)
override {
150 if (
auto Sym = M.findMangledSymbol(Name))
152 return M.ClientResolver->findSymbolInLogicalDylib(Name);
161 createOrcMCJITReplacement(std::string *ErrorMsg,
162 std::shared_ptr<MCJITMemoryManager> MemMgr,
163 std::shared_ptr<JITSymbolResolver> Resolver,
164 std::unique_ptr<TargetMachine> TM) {
171 std::shared_ptr<MCJITMemoryManager> MemMgr,
172 std::shared_ptr<JITSymbolResolver> ClientResolver,
173 std::unique_ptr<TargetMachine> TM)
175 MemMgr(std::make_shared<MCJITReplacementMemMgr>(*this,
177 Resolver(std::make_shared<LinkingResolver>(*this)),
192 if (M->getDataLayout().isDefault()) {
197 auto *MPtr = M.release();
198 ShouldDelete[MPtr] =
true;
199 auto Deleter = [
this](
Module *Mod) {
200 auto I = ShouldDelete.find(Mod);
201 if (
I != ShouldDelete.end() &&
I->second)
204 LocalModules.push_back(std::shared_ptr<Module>(MPtr, std::move(Deleter)));
210 std::make_shared<object::OwningBinary<object::ObjectFile>>(std::move(O),
217 std::make_shared<object::OwningBinary<object::ObjectFile>>(std::move(O));
222 Archives.push_back(std::move(A));
226 for (
auto I = LocalModules.begin(),
E = LocalModules.end();
I !=
E; ++
I) {
228 ShouldDelete[M] =
false;
229 LocalModules.erase(
I);
241 return findMangledSymbol(Mangle(Name));
250 uint64_t TargetAddress)
override {
251 for (
auto &
P : UnfinalizedSections)
252 if (
P.second.count(LocalAddress))
266 return reinterpret_cast<void *
>(
static_cast<uintptr_t
>(FAddr));
270 bool AbortOnFailure =
true)
override {
272 if (!Addr && AbortOnFailure)
274 return reinterpret_cast<void *
>(
static_cast<uintptr_t
>(Addr));
292 if (
auto Sym = LazyEmitLayer.
findSymbol(Name,
false))
294 if (
auto Sym = ClientResolver->findSymbol(Name))
296 if (
auto Sym = scanArchives(Name))
306 auto OptionalChildOrErr = A->
findSym(Name);
307 if (!OptionalChildOrErr)
309 auto &OptionalChild = *OptionalChildOrErr;
313 OptionalChild->getAsBinary();
314 if (!ChildBinOrErr) {
319 std::unique_ptr<object::Binary> &ChildBin = ChildBinOrErr.
get();
320 if (ChildBin->isObject()) {
321 std::unique_ptr<object::ObjectFile> ChildObj(
322 static_cast<object::ObjectFile*>(ChildBinOrErr->release()));
324 std::make_shared<object::OwningBinary<object::ObjectFile>>(
325 std::move(ChildObj),
nullptr);
327 if (
auto Sym = ObjectLayer.
findSymbol(Name,
true))
335 class NotifyObjectLoadedT {
337 using LoadedObjInfoListT =
338 std::vector<std::unique_ptr<RuntimeDyld::LoadedObjectInfo>>;
344 const LoadedObjectInfo &Info)
const {
345 M.UnfinalizedSections[
H] = std::move(M.SectionsAllocatedSinceLastLoad);
346 M.SectionsAllocatedSinceLastLoad = SectionAddrSet();
347 M.MemMgr->notifyObjectLoaded(&M, *Obj->getBinary());
353 class NotifyFinalizedT {
358 M.UnfinalizedSections.erase(H);
365 std::string Mangle(StringRef Name) {
366 std::string MangledName;
368 raw_string_ostream MangledNameStream(MangledName);
374 using ObjectLayerT = RTDyldObjectLinkingLayer;
375 using CompileLayerT = IRCompileLayer<ObjectLayerT, orc::SimpleCompiler>;
376 using LazyEmitLayerT = LazyEmittingLayer<CompileLayerT>;
378 std::unique_ptr<TargetMachine> TM;
379 std::shared_ptr<MCJITReplacementMemMgr> MemMgr;
380 std::shared_ptr<LinkingResolver> Resolver;
381 std::shared_ptr<JITSymbolResolver> ClientResolver;
387 ObjectLayerT ObjectLayer;
388 CompileLayerT CompileLayer;
389 LazyEmitLayerT LazyEmitLayer;
394 using SectionAddrSet = std::set<const void *>;
395 struct ObjHandleCompare {
401 SectionAddrSet SectionsAllocatedSinceLastLoad;
402 std::map<ObjectLayerT::ObjHandleT, SectionAddrSet, ObjHandleCompare>
405 std::map<Module*, bool> ShouldDelete;
406 std::vector<std::shared_ptr<Module>> LocalModules;
407 std::vector<object::OwningBinary<object::Archive>> Archives;
414 #endif // LLVM_LIB_EXECUTIONENGINE_ORC_MCJITREPLACEMENT_H
void addObjectFile(std::unique_ptr< object::ObjectFile > O) override
addObjectFile - Add an ObjectFile to the execution engine.
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.
std::shared_ptr< object::OwningBinary< object::ObjectFile >> ObjectPtr
Expected< Optional< Child > > findSym(StringRef name) const
A Module instance is used to store all the information related to an LLVM module. ...
void cantFail(Error Err)
Report a fatal error if Err is a failure value.
void finalizeObject() override
finalizeObject - ensure the module is fully processed and is usable.
This class is the base class for all object file types.
Error takeError()
Take ownership of the stored error.
LinkedObjectListT::iterator ObjHandleT
Handle to a set of loaded objects.
StringRef getName() const
Return a constant reference to the value's name.
const DataLayout & getDataLayout() const
CompileFtor & getCompiler()
Get a reference to the compiler functor.
void mapSectionAddress(ObjHandleT H, const void *LocalAddress, JITTargetAddress TargetAddr)
Map section addresses for the objects associated with the handle H.
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< JITSymbolResolver > ClientResolver, 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...
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 ExecutionEngine *(* OrcMCJITReplacementCtor)(std::string *ErrorStr, std::shared_ptr< MCJITMemoryManager > MM, std::shared_ptr< JITSymbolResolver > SR, std::unique_ptr< TargetMachine > TM)
Expected< ModuleHandleT > addModule(std::shared_ptr< Module > M, std::shared_ptr< JITSymbolResolver > Resolver)
Add the given module to the lazy emitting layer.
void addObjectFile(object::OwningBinary< object::ObjectFile > O) override
void setObjectCache(ObjectCache *NewCache) override
Sets the pre-compiled object cache.
void consumeError(Error Err)
Consume a Error without doing anything.
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...
JITSymbol findSymbol(const std::string &Name, bool ExportedSymbolsOnly)
Search for the given named symbol.
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.
void setProcessAllSections(bool ProcessAllSections)
Set the 'ProcessAllSections' flag.
LazyEmitLayer(CompileLayer)
constexpr char Size[]
Key for Kernel::Arg::Metadata::mSize.
llvm_unreachable("Unknown RegBank!")
void setObjectCache(ObjectCache *NewCache)
Set an ObjectCache to query before compiling.
void getNameWithPrefix(raw_ostream &OS, const GlobalValue *GV, bool CannotUsePrivateLabel) const
Print the appropriate prefix and the specified global variable's name.
JITSymbol findSymbol(StringRef Name, bool ExportedSymbolsOnly)
Search for the given named symbol.
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())
Expected< ObjHandleT > addObject(ObjectPtr Obj, std::shared_ptr< JITSymbolResolver > Resolver)
Add a set of objects (or archives) that will be treated as a unit for the purposes of symbol lookup a...
void addArchive(object::OwningBinary< object::Archive > A) override
addArchive - Add an Archive to the execution engine.
StringRef - Represent a constant reference to a string, i.e.
void mapSectionAddress(const void *LocalAddress, uint64_t TargetAddress) override
mapSectionAddress - map a section to its target address space value.