14 #ifndef LLVM_LIB_EXECUTIONENGINE_ORC_ORCMCJITREPLACEMENT_H
15 #define LLVM_LIB_EXECUTIONENGINE_ORC_ORCMCJITREPLACEMENT_H
35 std::shared_ptr<MCJITMemoryManager> ClientMM)
36 : M(M), ClientMM(std::move(ClientMM)) {}
38 uint8_t *allocateCodeSection(uintptr_t Size,
unsigned Alignment,
42 ClientMM->allocateCodeSection(Size, Alignment, SectionID,
44 M.SectionsAllocatedSinceLastLoad.insert(Addr);
48 uint8_t *allocateDataSection(uintptr_t Size,
unsigned Alignment,
49 unsigned SectionID,
StringRef SectionName,
50 bool IsReadOnly)
override {
51 uint8_t *Addr = ClientMM->allocateDataSection(Size, Alignment, SectionID,
52 SectionName, IsReadOnly);
53 M.SectionsAllocatedSinceLastLoad.insert(Addr);
57 void reserveAllocationSpace(uintptr_t CodeSize, uintptr_t DataSizeRO,
58 uintptr_t DataSizeRW)
override {
59 return ClientMM->reserveAllocationSpace(CodeSize, DataSizeRO,
63 bool needsToReserveAllocationSpace()
override {
64 return ClientMM->needsToReserveAllocationSpace();
67 void registerEHFrames(uint8_t *Addr, uint64_t LoadAddr,
68 size_t Size)
override {
69 return ClientMM->registerEHFrames(Addr, LoadAddr, Size);
72 void deregisterEHFrames(uint8_t *Addr, uint64_t LoadAddr,
73 size_t Size)
override {
74 return ClientMM->deregisterEHFrames(Addr, LoadAddr, Size);
79 return ClientMM->notifyObjectLoaded(EE, O);
82 bool finalizeMemory(std::string *ErrMsg =
nullptr)
override {
96 if (M.UnfinalizedSections.size() == 1)
97 return ClientMM->finalizeMemory(ErrMsg);
103 std::shared_ptr<MCJITMemoryManager> ClientMM;
111 return M.findMangledSymbol(Name);
115 findSymbolInLogicalDylib(
const std::string &Name)
override {
116 return M.ClientResolver->findSymbolInLogicalDylib(Name);
126 createOrcMCJITReplacement(std::string *ErrorMsg,
127 std::shared_ptr<MCJITMemoryManager> MemMgr,
128 std::shared_ptr<RuntimeDyld::SymbolResolver> Resolver,
129 std::unique_ptr<TargetMachine> TM) {
140 std::shared_ptr<MCJITMemoryManager> MemMgr,
141 std::shared_ptr<RuntimeDyld::SymbolResolver> ClientResolver,
142 std::unique_ptr<TargetMachine> TM)
143 : TM(std::move(TM)), MemMgr(*this, std::move(MemMgr)),
144 Resolver(*this), ClientResolver(std::move(ClientResolver)),
145 NotifyObjectLoaded(*this), NotifyFinalized(*this),
146 ObjectLayer(NotifyObjectLoaded, NotifyFinalized),
148 LazyEmitLayer(CompileLayer) {
156 if (M->getDataLayout().isDefault())
159 Modules.push_back(std::move(M));
160 std::vector<Module *> Ms;
161 Ms.push_back(&*
Modules.back());
162 LazyEmitLayer.
addModuleSet(std::move(Ms), &MemMgr, &Resolver);
166 std::vector<std::unique_ptr<object::ObjectFile>> Objs;
167 Objs.push_back(std::move(O));
168 ObjectLayer.
addObjectSet(std::move(Objs), &MemMgr, &Resolver);
172 std::unique_ptr<object::ObjectFile> Obj;
173 std::unique_ptr<MemoryBuffer> Buf;
175 std::vector<std::unique_ptr<object::ObjectFile>> Objs;
176 Objs.push_back(std::move(Obj));
178 ObjectLayer.
addObjectSet(std::move(Objs), &MemMgr, &Resolver);
180 std::vector<std::unique_ptr<MemoryBuffer>> Bufs;
181 Bufs.push_back(std::move(Buf));
186 Archives.push_back(std::move(A));
194 return findMangledSymbol(Mangle(Name));
204 for (
auto &
P : UnfinalizedSections)
205 if (
P.second.count(LocalAddress))
219 return reinterpret_cast<void *
>(
static_cast<uintptr_t
>(FAddr));
223 bool AbortOnFailure =
true)
override {
225 if (!Addr && AbortOnFailure)
227 return reinterpret_cast<void *
>(
static_cast<uintptr_t
>(Addr));
240 if (
auto Sym = LazyEmitLayer.
findSymbol(Name,
false))
242 if (
auto Sym = ClientResolver->findSymbol(Name))
244 if (
auto Sym = scanArchives(Name))
261 std::unique_ptr<object::Binary> &ChildBin = ChildBinOrErr.
get();
262 if (ChildBin->isObject()) {
263 std::vector<std::unique_ptr<object::ObjectFile>> ObjSet;
264 ObjSet.push_back(std::unique_ptr<object::ObjectFile>(
265 static_cast<object::ObjectFile *>(ChildBin.release())));
266 ObjectLayer.
addObjectSet(std::move(ObjSet), &MemMgr, &Resolver);
267 if (
auto Sym = ObjectLayer.
findSymbol(Name,
true))
275 class NotifyObjectLoadedT {
277 typedef std::vector<std::unique_ptr<object::ObjectFile>> ObjListT;
278 typedef std::vector<std::unique_ptr<RuntimeDyld::LoadedObjectInfo>>
284 const ObjListT &Objects,
285 const LoadedObjInfoListT &Infos)
const {
286 M.UnfinalizedSections[
H] = std::move(M.SectionsAllocatedSinceLastLoad);
287 M.SectionsAllocatedSinceLastLoad = SectionAddrSet();
288 assert(Objects.size() == Infos.size() &&
289 "Incorrect number of Infos for Objects.");
290 for (
unsigned I = 0;
I < Objects.size(); ++
I)
291 M.MemMgr.notifyObjectLoaded(&M, *Objects[
I]);
298 class NotifyFinalizedT {
302 M.UnfinalizedSections.erase(H);
309 std::string Mangle(StringRef Name) {
310 std::string MangledName;
312 raw_string_ostream MangledNameStream(MangledName);
318 typedef ObjectLinkingLayer<NotifyObjectLoadedT> ObjectLayerT;
319 typedef IRCompileLayer<ObjectLayerT> CompileLayerT;
320 typedef LazyEmittingLayer<CompileLayerT> LazyEmitLayerT;
322 std::unique_ptr<TargetMachine> TM;
323 MCJITReplacementMemMgr MemMgr;
324 LinkingResolver Resolver;
325 std::shared_ptr<RuntimeDyld::SymbolResolver> ClientResolver;
328 NotifyObjectLoadedT NotifyObjectLoaded;
329 NotifyFinalizedT NotifyFinalized;
331 ObjectLayerT ObjectLayer;
332 CompileLayerT CompileLayer;
333 LazyEmitLayerT LazyEmitLayer;
338 typedef std::set<const void *> SectionAddrSet;
339 struct ObjSetHandleCompare {
345 SectionAddrSet SectionsAllocatedSinceLastLoad;
346 std::map<ObjectLayerT::ObjSetHandleT, SectionAddrSet, ObjSetHandleCompare>
349 std::vector<object::OwningBinary<object::Archive>> Archives;
355 #endif // LLVM_LIB_EXECUTIONENGINE_ORC_MCJITREPLACEMENT_H
void addObjectFile(std::unique_ptr< object::ObjectFile > O) override
addObjectFile - Add an ObjectFile to the execution engine.
std::error_code getError() const
Represents either an error or a value T.
static ExecutionEngine *(* OrcMCJITReplacementCtor)(std::string *ErrorStr, std::shared_ptr< MCJITMemoryManager > MM, std::shared_ptr< RuntimeDyld::SymbolResolver > SR, std::unique_ptr< TargetMachine > TM)
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.
ErrorOr< std::unique_ptr< Binary > > getAsBinary(LLVMContext *Context=nullptr) const
void finalizeObject() override
finalizeObject - ensure the module is fully processed and is usable.
uint64_t getAddress() const
This class is the base class for all object file types.
ObjSetHandleT addObjectSet(const 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...
StringRef getName() const
Return a constant reference to the value's name.
uint64_t getFunctionAddress(const std::string &Name) override
getFunctionAddress - Return the address of the specified function.
#define llvm_unreachable(msg)
Marks that the current location is not supposed to be reachable.
void mapSectionAddress(ObjSetHandleT H, const void *LocalAddress, TargetAddress TargetAddr)
Map section addresses for the objects associated with the handle H.
Simple compile functor: Takes a single IR module and returns an ObjectFile.
OrcMCJITReplacement(std::shared_ptr< MCJITMemoryManager > MemMgr, std::shared_ptr< RuntimeDyld::SymbolResolver > ClientResolver, std::unique_ptr< TargetMachine > TM)
void setObjectCache(ObjectCache *NewCache)
Set an ObjectCache to query before compiling.
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.
child_iterator child_end() const
void addObjectFile(object::OwningBinary< object::ObjectFile > O) override
void setObjectCache(ObjectCache *NewCache) override
Sets the pre-compiled object cache.
void setDataLayout(const DataLayout *Val)
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...
child_iterator findSym(StringRef name) const
std::pair< std::unique_ptr< T >, std::unique_ptr< MemoryBuffer > > takeBinary()
void takeOwnershipOfBuffers(ObjSetHandleT H, OwningMBSet MBs)
JITSymbol findSymbol(const std::string &Name, bool ExportedSymbolsOnly)
Search for the given named symbol.
uint64_t getSymbolAddress(StringRef Name)
uint64_t TargetAddress
Represents an address in the target process's address space.
SmallVector< std::unique_ptr< Module >, 1 > Modules
The list of Modules that we are JIT'ing from.
const DataLayout * getDataLayout() const
void getNameWithPrefix(raw_ostream &OS, const GlobalValue *GV, bool CannotUsePrivateLabel) const
Print the appropriate prefix and the specified global variable's name.
Information about a named symbol.
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...
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.
RuntimeDyld::SymbolInfo findSymbol(StringRef Name)
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.