14 #ifndef LLVM_LIB_EXECUTIONENGINE_ORC_ORCMCJITREPLACEMENT_H
15 #define LLVM_LIB_EXECUTIONENGINE_ORC_ORCMCJITREPLACEMENT_H
57 std::shared_ptr<MCJITMemoryManager> ClientMM)
58 : M(M), ClientMM(std::move(ClientMM)) {}
60 uint8_t *allocateCodeSection(uintptr_t Size,
unsigned Alignment,
64 ClientMM->allocateCodeSection(Size, Alignment, SectionID,
66 M.SectionsAllocatedSinceLastLoad.insert(Addr);
70 uint8_t *allocateDataSection(uintptr_t Size,
unsigned Alignment,
71 unsigned SectionID,
StringRef SectionName,
72 bool IsReadOnly)
override {
73 uint8_t *Addr = ClientMM->allocateDataSection(Size, Alignment, SectionID,
74 SectionName, IsReadOnly);
75 M.SectionsAllocatedSinceLastLoad.insert(Addr);
79 void reserveAllocationSpace(uintptr_t CodeSize,
uint32_t CodeAlign,
80 uintptr_t RODataSize,
uint32_t RODataAlign,
83 return ClientMM->reserveAllocationSpace(CodeSize, CodeAlign,
84 RODataSize, RODataAlign,
85 RWDataSize, RWDataAlign);
88 bool needsToReserveAllocationSpace()
override {
89 return ClientMM->needsToReserveAllocationSpace();
92 void registerEHFrames(uint8_t *Addr, uint64_t LoadAddr,
93 size_t Size)
override {
94 return ClientMM->registerEHFrames(Addr, LoadAddr, Size);
97 void deregisterEHFrames(uint8_t *Addr, uint64_t LoadAddr,
98 size_t Size)
override {
99 return ClientMM->deregisterEHFrames(Addr, LoadAddr, Size);
104 return ClientMM->notifyObjectLoaded(RTDyld, O);
109 return ClientMM->notifyObjectLoaded(EE, O);
112 bool finalizeMemory(std::string *ErrMsg =
nullptr)
override {
126 if (M.UnfinalizedSections.size() == 1)
127 return ClientMM->finalizeMemory(ErrMsg);
133 std::shared_ptr<MCJITMemoryManager> ClientMM;
141 return M.ClientResolver->findSymbol(Name);
144 JITSymbol findSymbolInLogicalDylib(
const std::string &Name)
override {
145 if (
auto Sym = M.findMangledSymbol(Name))
147 return M.ClientResolver->findSymbolInLogicalDylib(Name);
157 createOrcMCJITReplacement(std::string *ErrorMsg,
158 std::shared_ptr<MCJITMemoryManager> MemMgr,
159 std::shared_ptr<JITSymbolResolver> Resolver,
160 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(*this, std::move(MemMgr)), Resolver(*this),
176 ClientResolver(std::move(ClientResolver)), NotifyObjectLoaded(*this),
177 NotifyFinalized(*this),
178 ObjectLayer(NotifyObjectLoaded, NotifyFinalized),
180 LazyEmitLayer(CompileLayer) {}
186 if (M->getDataLayout().isDefault()) {
191 Modules.push_back(std::move(M));
192 std::vector<Module *> Ms;
193 Ms.push_back(&*
Modules.back());
194 LazyEmitLayer.
addModuleSet(std::move(Ms), &MemMgr, &Resolver);
198 std::vector<std::unique_ptr<object::ObjectFile>> Objs;
199 Objs.push_back(std::move(O));
200 ObjectLayer.
addObjectSet(std::move(Objs), &MemMgr, &Resolver);
204 std::vector<std::unique_ptr<object::OwningBinary<object::ObjectFile>>> Objs;
208 ObjectLayer.
addObjectSet(std::move(Objs), &MemMgr, &Resolver);
212 Archives.push_back(std::move(A));
220 return findMangledSymbol(Mangle(Name));
229 uint64_t TargetAddress)
override {
230 for (
auto &
P : UnfinalizedSections)
231 if (
P.second.count(LocalAddress))
245 return reinterpret_cast<void *
>(
static_cast<uintptr_t
>(FAddr));
249 bool AbortOnFailure =
true)
override {
251 if (!Addr && AbortOnFailure)
253 return reinterpret_cast<void *
>(
static_cast<uintptr_t
>(Addr));
269 if (
auto Sym = LazyEmitLayer.
findSymbol(Name,
false))
271 if (
auto Sym = ClientResolver->findSymbol(Name))
273 if (
auto Sym = scanArchives(Name))
283 auto OptionalChildOrErr = A->
findSym(Name);
284 if (!OptionalChildOrErr)
286 auto &OptionalChild = *OptionalChildOrErr;
290 OptionalChild->getAsBinary();
291 if (!ChildBinOrErr) {
296 std::unique_ptr<object::Binary> &ChildBin = ChildBinOrErr.
get();
297 if (ChildBin->isObject()) {
298 std::vector<std::unique_ptr<object::ObjectFile>> ObjSet;
299 ObjSet.push_back(std::unique_ptr<object::ObjectFile>(
300 static_cast<object::ObjectFile *>(ChildBin.release())));
301 ObjectLayer.
addObjectSet(std::move(ObjSet), &MemMgr, &Resolver);
302 if (
auto Sym = ObjectLayer.
findSymbol(Name,
true))
310 class NotifyObjectLoadedT {
312 typedef std::vector<std::unique_ptr<RuntimeDyld::LoadedObjectInfo>>
317 template <
typename ObjListT>
319 const ObjListT &Objects,
320 const LoadedObjInfoListT &Infos)
const {
321 M.UnfinalizedSections[
H] = std::move(M.SectionsAllocatedSinceLastLoad);
322 M.SectionsAllocatedSinceLastLoad = SectionAddrSet();
323 assert(Objects.size() == Infos.size() &&
324 "Incorrect number of Infos for Objects.");
325 for (
unsigned I = 0;
I < Objects.size(); ++
I)
326 M.MemMgr.notifyObjectLoaded(&M,
getObject(*Objects[
I]));
330 static const object::ObjectFile&
getObject(
const object::ObjectFile &Obj) {
334 template <
typename ObjT>
335 static const object::ObjectFile&
336 getObject(
const object::OwningBinary<ObjT> &Obj) {
337 return *Obj.getBinary();
343 class NotifyFinalizedT {
348 M.UnfinalizedSections.erase(H);
355 std::string Mangle(StringRef Name) {
356 std::string MangledName;
358 raw_string_ostream MangledNameStream(MangledName);
364 typedef ObjectLinkingLayer<NotifyObjectLoadedT> ObjectLayerT;
365 typedef IRCompileLayer<ObjectLayerT> CompileLayerT;
366 typedef LazyEmittingLayer<CompileLayerT> LazyEmitLayerT;
368 std::unique_ptr<TargetMachine> TM;
369 MCJITReplacementMemMgr MemMgr;
370 LinkingResolver Resolver;
371 std::shared_ptr<JITSymbolResolver> ClientResolver;
374 NotifyObjectLoadedT NotifyObjectLoaded;
375 NotifyFinalizedT NotifyFinalized;
377 ObjectLayerT ObjectLayer;
378 CompileLayerT CompileLayer;
379 LazyEmitLayerT LazyEmitLayer;
384 typedef std::set<const void *> SectionAddrSet;
385 struct ObjSetHandleCompare {
391 SectionAddrSet SectionsAllocatedSinceLastLoad;
392 std::map<ObjectLayerT::ObjSetHandleT, SectionAddrSet, ObjSetHandleCompare>
395 std::vector<object::OwningBinary<object::Archive>> Archives;
401 #endif // LLVM_LIB_EXECUTIONENGINE_ORC_MCJITREPLACEMENT_H
void addObjectFile(std::unique_ptr< object::ObjectFile > O) override
addObjectFile - Add an ObjectFile to the execution engine.
JITTargetAddress getAddress()
Get the address of the symbol in the target address space.
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.
ObjSetHandleT addObjectSet(ObjSetT Objects, MemoryManagerPtrT MemMgr, SymbolResolverPtrT Resolver)
Add a set of objects (or archives) that will be treated as a unit for the purposes of symbol lookup a...
Expected< Optional< Child > > findSym(StringRef name) const
JITSymbol findSymbol(StringRef Name, bool ExportedSymbolsOnly)
Search for the given named symbol.
ModuleSetHandleT addModuleSet(ModuleSetT Ms, MemoryManagerPtrT MemMgr, SymbolResolverPtrT Resolver)
Add the given set of modules to the lazy emitting layer.
static std::error_code getObject(const T *&Obj, MemoryBufferRef M, const void *Ptr, const uint64_t Size=sizeof(T))
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.
StringRef getName() const
Return a constant reference to the value's name.
const DataLayout & getDataLayout() const
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.
void setObjectCache(ObjectCache *NewCache)
Set an ObjectCache to query before compiling.
OrcMCJITReplacement(std::shared_ptr< MCJITMemoryManager > MemMgr, std::shared_ptr< JITSymbolResolver > ClientResolver, std::unique_ptr< TargetMachine > TM)
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)
std::enable_if<!std::is_array< T >::value, std::unique_ptr< T > >::type make_unique(Args &&...args)
Constructs a new T() with the given args and returns a unique_ptr<T> which owns the object...
void addObjectFile(object::OwningBinary< object::ObjectFile > O) override
void mapSectionAddress(ObjSetHandleT H, const void *LocalAddress, JITTargetAddress TargetAddr)
Map section addresses for the objects associated with the handle H.
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...
JITSymbol findSymbol(const std::string &Name, bool ExportedSymbolsOnly)
Search for the given named symbol.
JITSymbol findSymbol(StringRef Name)
uint64_t getSymbolAddress(StringRef Name)
reference get()
Returns a reference to the stored T value.
SmallVector< std::unique_ptr< Module >, 1 > Modules
The list of Modules that we are JIT'ing from.
void getNameWithPrefix(raw_ostream &OS, const GlobalValue *GV, bool CannotUsePrivateLabel) const
Print the appropriate prefix and the specified global variable's name.
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())
void addArchive(object::OwningBinary< object::Archive > A) override
addArchive - Add an Archive to the execution engine.
void setProcessAllSections(bool ProcessAllSections)
Set the 'ProcessAllSections' flag.
StringRef - Represent a constant reference to a string, i.e.
static GCRegistry::Add< ErlangGC > A("erlang","erlang-compatible garbage collector")
void mapSectionAddress(const void *LocalAddress, uint64_t TargetAddress) override
mapSectionAddress - map a section to its target address space value.
LinkedObjectSetListT::iterator ObjSetHandleT
Handle to a set of loaded objects.