14 #ifndef LLVM_EXECUTIONENGINE_ORC_INDIRECTIONUTILS_H 15 #define LLVM_EXECUTIONENGINE_ORC_INDIRECTIONUTILS_H 32 #include <system_error> 60 : ES(ES), CallbacksVSO(ES.createVSO(
"<Callbacks>")),
61 ErrorHandlerAddress(ErrorHandlerAddress) {}
77 if (this->AvailableTrampolines.empty())
78 if (
auto Err = grow())
79 return std::move(Err);
80 assert(!this->AvailableTrampolines.empty() &&
81 "Failed to grow available trampolines.");
83 this->AvailableTrampolines.pop_back();
84 return TrampolineAddr;
88 virtual Error grow() = 0;
90 virtual void anchor();
92 std::mutex CCMgrMutex;
96 std::map<JITTargetAddress, SymbolStringPtr> AddrToSymbol;
97 size_t NextCallbackId = 0;
101 template <
typename TargetT>
113 TargetT::ResolverCodeSize,
nullptr,
115 assert(!EC &&
"Failed to allocate resolver block");
117 TargetT::writeResolverCode(static_cast<uint8_t *>(ResolverBlock.base()),
123 assert(!EC &&
"Failed to mprotect resolver block");
131 static_cast<JITTargetAddress>(
132 reinterpret_cast<uintptr_t>(TrampolineId)));
135 Error grow()
override {
139 auto TrampolineBlock =
146 unsigned NumTrampolines =
148 TargetT::TrampolineSize;
150 uint8_t *TrampolineMem =
static_cast<uint8_t *
>(TrampolineBlock.base());
151 TargetT::writeTrampolines(TrampolineMem, ResolverBlock.base(),
154 for (
unsigned I = 0;
I < NumTrampolines; ++
I)
156 static_cast<JITTargetAddress>(reinterpret_cast<uintptr_t>(
157 TrampolineMem + (
I * TargetT::TrampolineSize))));
160 TrampolineBlock.getMemoryBlock(),
164 TrampolineBlocks.push_back(std::move(TrampolineBlock));
169 std::vector<sys::OwningMemoryBlock> TrampolineBlocks;
200 virtual void anchor();
205 template <
typename TargetT>
210 if (
auto Err = reserveStubs(1))
213 createStubInternal(StubName, StubAddr, StubFlags);
219 if (
auto Err = reserveStubs(StubInits.
size()))
222 for (
auto &Entry : StubInits)
223 createStubInternal(Entry.first(), Entry.second.first,
224 Entry.second.second);
230 auto I = StubIndexes.find(Name);
231 if (
I == StubIndexes.end())
233 auto Key =
I->second.first;
234 void *StubAddr = IndirectStubsInfos[
Key.first].getStub(
Key.second);
235 assert(StubAddr &&
"Missing stub address");
236 auto StubTargetAddr =
239 if (ExportedStubsOnly && !StubSymbol.getFlags().isExported())
245 auto I = StubIndexes.find(Name);
246 if (
I == StubIndexes.end())
248 auto Key =
I->second.first;
249 void *PtrAddr = IndirectStubsInfos[
Key.first].getPtr(
Key.second);
250 assert(PtrAddr &&
"Missing pointer address");
257 auto I = StubIndexes.find(Name);
258 assert(
I != StubIndexes.end() &&
"No stub pointer for symbol");
259 auto Key =
I->second.first;
260 *IndirectStubsInfos[Key.first].getPtr(Key.second) =
261 reinterpret_cast<void *
>(
static_cast<uintptr_t
>(NewAddr));
266 Error reserveStubs(
unsigned NumStubs) {
267 if (NumStubs <= FreeStubs.size())
270 unsigned NewStubsRequired = NumStubs - FreeStubs.size();
271 unsigned NewBlockId = IndirectStubsInfos.size();
272 typename TargetT::IndirectStubsInfo ISI;
274 TargetT::emitIndirectStubsBlock(ISI, NewStubsRequired,
nullptr))
276 for (
unsigned I = 0;
I < ISI.getNumStubs(); ++
I)
277 FreeStubs.push_back(std::make_pair(NewBlockId,
I));
278 IndirectStubsInfos.push_back(std::move(ISI));
284 auto Key = FreeStubs.back();
285 FreeStubs.pop_back();
286 *IndirectStubsInfos[
Key.first].getPtr(
Key.second) =
287 reinterpret_cast<void *
>(
static_cast<uintptr_t
>(InitAddr));
288 StubIndexes[StubName] = std::make_pair(
Key, StubFlags);
291 std::vector<typename TargetT::IndirectStubsInfo> IndirectStubsInfos;
292 using StubKey = std::pair<uint16_t, uint16_t>;
293 std::vector<StubKey> FreeStubs;
302 std::unique_ptr<JITCompileCallbackManager>
309 std::function<std::unique_ptr<IndirectStubsManager>()>
391 #endif // LLVM_EXECUTIONENGINE_ORC_INDIRECTIONUTILS_H IndirectStubsManager implementation for the host architecture, e.g.
Base class for managing collections of named indirect stubs.
Compute iterated dominance frontiers using a linear time algorithm.
std::function< std::unique_ptr< IndirectStubsManager >)> createLocalIndirectStubsManagerBuilder(const Triple &T)
Create a local indriect stubs manager builder.
GlobalAlias * cloneGlobalAliasDecl(Module &Dst, const GlobalAlias &OrigA, ValueToValueMapTy &VMap)
Clone a global alias declaration into a new module.
A Module instance is used to store all the information related to an LLVM module. ...
A symbol table that supports asynchoronous symbol queries.
void cloneModuleFlagsMetadata(Module &Dst, const Module &Src, ValueToValueMapTy &VMap)
Clone module flags metadata into the destination module.
Manage compile callbacks for in-process JITs.
static MemoryBlock allocateMappedMemory(size_t NumBytes, const MemoryBlock *const NearBlock, unsigned Flags, std::error_code &EC)
This method allocates a block of memory that is suitable for loading dynamically generated code (e...
Target-independent base class for compile callback management.
Twine - A lightweight data structure for efficiently representing the concatenation of temporary valu...
Error updatePointer(StringRef Name, JITTargetAddress NewAddr) override
Change the value of the implementation pointer for the stub.
GlobalVariable * createImplPointer(PointerType &PT, Module &M, const Twine &Name, Constant *Initializer)
Create a function pointer with the given type, name, and initializer in the given Module...
JITCompileCallbackManager(ExecutionSession &ES, JITTargetAddress ErrorHandlerAddress)
Construct a JITCompileCallbackManager.
void moveGlobalVariableInitializer(GlobalVariable &OrigGV, ValueToValueMapTy &VMap, ValueMaterializer *Materializer=nullptr, GlobalVariable *NewGV=nullptr)
Move global variable GV from its parent module to cloned global declaration in a different module...
Tagged union holding either a T or a Error.
JITEvaluatedSymbol findPointer(StringRef Name) override
Find the implementation-pointer for the stub.
void makeStub(Function &F, Value &ImplPointer)
Turn a function declaration into a stub function that makes an indirect call using the given function...
Class to represent function types.
uint64_t JITTargetAddress
Represents an address in the target process's address space.
std::vector< JITTargetAddress > AvailableTrampolines
GlobalVariable * cloneGlobalVariableDecl(Module &Dst, const GlobalVariable &GV, ValueToValueMapTy *VMap=nullptr)
Clone a global variable declaration into a new module.
Class to represent pointers.
This is a class that can be implemented by clients to materialize Values on demand.
This is an important base class in LLVM.
Error errorCodeToError(std::error_code EC)
Helper for converting an std::error_code to a Error.
Flags for symbols in the JIT.
Triple - Helper class for working with autoconf configuration names.
static unsigned getPageSize()
Constant * createIRTypedAddress(FunctionType &FT, JITTargetAddress Addr)
Build a function pointer of FunctionType with the given constant address.
std::function< JITTargetAddress()> CompileFunction
static ErrorSuccess success()
Create a success value.
Function * cloneFunctionDecl(Module &Dst, const Function &F, ValueToValueMapTy *VMap=nullptr)
Clone a function declaration into a new module.
Error createStub(StringRef StubName, JITTargetAddress StubAddr, JITSymbolFlags StubFlags) override
Create a single stub with the given name, target address and flags.
Owning version of MemoryBlock.
Expected< JITTargetAddress > getCompileCallback(CompileFunction Compile)
Reserve a compile callback.
An ExecutionSession represents a running JIT program.
StringMap - This is an unconventional map that is specialized for handling keys that are "strings"...
Represents a symbol that has been evaluated to an address already.
void makeAllSymbolsExternallyAccessible(Module &M)
Raise linkage types and rename as necessary to ensure that all symbols are accessible for other modul...
LocalJITCompileCallbackManager(ExecutionSession &ES, JITTargetAddress ErrorHandlerAddress)
Construct a InProcessJITCompileCallbackManager.
JITTargetAddress executeCompileCallback(JITTargetAddress TrampolineAddr)
Execute the callback for the given trampoline id.
Provides a library for accessing information about this process and other processes on the operating ...
Error createStubs(const StubInitsMap &StubInits) override
Create StubInits.size() stubs with the given names, target addresses, and flags.
std::unique_ptr< JITCompileCallbackManager > createLocalCompileCallbackManager(const Triple &T, ExecutionSession &ES, JITTargetAddress ErrorHandlerAddress)
Create a local compile callback manager.
virtual ~JITCompileCallbackManager()=default
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
JITEvaluatedSymbol findStub(StringRef Name, bool ExportedStubsOnly) override
Find the stub with the given name.
LLVM Value Representation.
Lightweight error class with error context and mandatory checking.
StringRef - Represent a constant reference to a string, i.e.
void moveFunctionBody(Function &OrigF, ValueToValueMapTy &VMap, ValueMaterializer *Materializer=nullptr, Function *NewF=nullptr)
Move the body of function 'F' to a cloned function declaration in a different module (See related clo...
static std::error_code protectMappedMemory(const MemoryBlock &Block, unsigned Flags)
This method sets the protection flags for a block of memory to the state specified by /p Flags...