10 #ifndef LLVM_LIB_EXECUTIONENGINE_ORC_ORCCBINDINGSSTACK_H 11 #define LLVM_LIB_EXECUTIONENGINE_ORC_ORCCBINDINGSSTACK_H 45 class OrcCBindingsStack;
58 bool ExportedSymbolsOnly) = 0;
67 bool ExportedSymbolsOnly)
override {
68 return Layer.findSymbolIn(K, Name, ExportedSymbolsOnly);
72 return Layer.removeModule(K);
87 bool ExportedSymbolsOnly)
override {
88 return Layer.findSymbolIn(K, Name, ExportedSymbolsOnly);
92 return Layer.removeObject(K);
99 template <
typename LayerT>
101 return llvm::make_unique<GenericLayerImpl<LayerT>>(Layer);
116 std::function<std::unique_ptr<CompileCallbackMgr>()>;
128 void *ExternalResolverCtx)
129 : Stack(Stack), ExternalResolver(std::move(ExternalResolver)),
130 ExternalResolverCtx(std::move(ExternalResolverCtx)) {}
136 for (
auto &S : Symbols) {
137 if (
auto Sym = findSymbol(*S))
138 SymbolFlags[S] = Sym.getFlags();
139 else if (
auto Err = Sym.takeError()) {
140 Stack.reportError(std::move(Err));
149 lookup(std::shared_ptr<orc::AsynchronousSymbolQuery>
Query,
153 for (
auto &S : Symbols) {
154 if (
auto Sym = findSymbol(*S)) {
155 if (
auto Addr = Sym.getAddress()) {
157 Query->notifySymbolReady();
162 }
else if (
auto Err = Sym.takeError()) {
166 UnresolvedSymbols.insert(S);
169 if (Query->isFullyResolved())
170 Query->handleFullyResolved();
172 if (Query->isFullyReady())
173 Query->handleFullyReady();
175 return UnresolvedSymbols;
185 if (
auto Sym = Stack.CODLayer.
findSymbol(Name,
true))
187 else if (
auto Err = Sym.takeError())
193 if (ExternalResolver)
194 return JITSymbol(ExternalResolver(Name.c_str(), ExternalResolverCtx),
202 void *ExternalResolverCtx =
nullptr;
209 DL(TM.createDataLayout()), IndirectStubsMgr(IndirectStubsMgrBuilder()),
212 auto ResolverI = Resolvers.find(K);
213 assert(ResolverI != Resolvers.end() &&
214 "No resolver for module K");
215 auto Resolver = std::move(ResolverI->second);
216 Resolvers.erase(ResolverI);
218 std::make_shared<SectionMemoryManager>(), Resolver};
223 this->notifyFinalized(K, Obj, LoadedObjInfo);
226 this->notifyFreed(K, Obj);
229 CODLayer(ES, CompileLayer,
231 auto ResolverI = Resolvers.find(K);
232 assert(ResolverI != Resolvers.end() &&
233 "No resolver for module K");
234 return ResolverI->second;
237 std::shared_ptr<orc::SymbolResolver>
Resolver) {
238 assert(!Resolvers.count(K) &&
"Resolver already present");
241 [](
Function &
F) {
return std::set<Function *>({&
F}); },
242 *this->CCMgr, std::move(IndirectStubsMgrBuilder),
false),
244 [
this](
const std::string &S) {
return mangle(S); }) {}
248 CXXRuntimeOverrides.runDestructors();
250 for (
auto &DtorRunner : IRStaticDestructorRunners)
251 if (
auto Err = DtorRunner.runViaLayer(*
this))
252 return mapError(std::move(Err));
257 std::string MangledName;
265 template <
typename PtrTy>
267 return reinterpret_cast<PtrTy
>(
static_cast<uintptr_t
>(Addr));
276 return Callback(
wrap(
this), CallbackCtx);
279 if (
auto CCAddr = CCMgr->getCompileCallback(std::move(WrappedCallback))) {
283 return mapError(CCAddr.takeError());
294 return mapError(IndirectStubsMgr->updatePointer(Name, Addr));
296 template <
typename LayerT>
299 std::unique_ptr<RuntimeDyld::MemoryManager> MemMgr,
301 void *ExternalResolverCtx) {
304 if (M->getDataLayout().isDefault())
305 M->setDataLayout(DL);
309 std::vector<std::string> CtorNames, DtorNames;
311 CtorNames.push_back(mangle(Ctor.Func->getName()));
313 DtorNames.push_back(mangle(Dtor.Func->getName()));
316 RetKey = ES.allocateVModule();
317 Resolvers[RetKey] = std::make_shared<CBindingsResolver>(
318 *
this, ExternalResolver, ExternalResolverCtx);
319 if (
auto Err = Layer.addModule(RetKey, std::move(M)))
320 return mapError(std::move(Err));
329 return mapError(std::move(Err));
331 IRStaticDestructorRunners.emplace_back(std::move(DtorNames), RetKey);
337 std::unique_ptr<Module> M,
339 void *ExternalResolverCtx) {
340 return addIRModule(RetKey, CompileLayer, std::move(M),
341 llvm::make_unique<SectionMemoryManager>(),
342 std::move(ExternalResolver), ExternalResolverCtx);
346 std::unique_ptr<Module> M,
348 void *ExternalResolverCtx) {
349 return addIRModule(RetKey, CODLayer, std::move(M),
350 llvm::make_unique<SectionMemoryManager>(),
351 std::move(ExternalResolver), ExternalResolverCtx);
357 return mapError(std::move(Err));
358 ES.releaseVModule(K);
364 std::unique_ptr<MemoryBuffer> ObjBuffer,
366 void *ExternalResolverCtx) {
368 ObjBuffer->getMemBufferRef())) {
370 RetKey = ES.allocateVModule();
371 Resolvers[RetKey] = std::make_shared<CBindingsResolver>(
372 *
this, ExternalResolver, ExternalResolverCtx);
374 if (
auto Err = ObjectLayer.addObject(RetKey, std::move(ObjBuffer)))
375 return mapError(std::move(Err));
381 return mapError(Obj.takeError());
385 bool ExportedSymbolsOnly) {
386 if (
auto Sym = IndirectStubsMgr->findStub(Name, ExportedSymbolsOnly))
388 return CODLayer.findSymbol(mangle(Name), ExportedSymbolsOnly);
392 bool ExportedSymbolsOnly) {
393 assert(KeyLayers.count(K) &&
"looking up symbol in unknown module");
394 return KeyLayers[K]->findSymbolIn(K, mangle(Name), ExportedSymbolsOnly);
398 const std::string &
Name,
399 bool ExportedSymbolsOnly) {
401 if (
auto Sym = findSymbol(Name, ExportedSymbolsOnly)) {
403 if (
auto AddrOrErr = Sym.getAddress()) {
404 RetAddr = *AddrOrErr;
407 return mapError(AddrOrErr.takeError());
408 }
else if (
auto Err = Sym.takeError()) {
410 return mapError(std::move(Err));
419 const std::string &
Name,
420 bool ExportedSymbolsOnly) {
422 if (
auto Sym =
findSymbolIn(K, Name, ExportedSymbolsOnly)) {
424 if (
auto AddrOrErr = Sym.getAddress()) {
425 RetAddr = *AddrOrErr;
428 return mapError(AddrOrErr.takeError());
429 }
else if (
auto Err = Sym.takeError()) {
431 return mapError(std::move(Err));
443 EventListeners.push_back(L);
451 if (
I != EventListeners.rend()) {
453 EventListeners.pop_back();
479 for (
auto &Listener : EventListeners)
480 Listener->NotifyObjectEmitted(Obj, LoadedObjInfo);
484 for (
auto &Listener : EventListeners)
485 Listener->NotifyFreeingObject(Obj);
489 std::unique_ptr<CompileCallbackMgr> CCMgr;
491 std::vector<JITEventListener *> EventListeners;
496 std::unique_ptr<orc::IndirectStubsManager> IndirectStubsMgr;
502 std::map<orc::VModuleKey, std::unique_ptr<detail::GenericLayer>> KeyLayers;
505 std::vector<orc::CtorDtorRunner<OrcCBindingsStack>> IRStaticDestructorRunners;
508 std::map<orc::VModuleKey, std::shared_ptr<orc::SymbolResolver>> Resolvers;
513 #endif // LLVM_LIB_EXECUTIONENGINE_ORC_ORCCBINDINGSSTACK_H static Expected< OwningBinary< ObjectFile > > createObjectFile(StringRef ObjectPath)
Information about the loaded object.
A parsed version of the target data layout string in and methods for querying it. ...
void RegisterJITEventListener(JITEventListener *L)
raw_ostream & errs()
This returns a reference to a raw_ostream for standard error.
CODLayerT::IndirectStubsManagerBuilderT IndirectStubsManagerBuilder
Represents a symbol in the JIT.
Compute iterated dominance frontiers using a linear time algorithm.
void logAllUnhandledErrors(Error E, raw_ostream &OS, Twine ErrorBanner)
Log all errors (if any) in E to OS.
std::function< std::unique_ptr< CompileCallbackMgr >()> CallbackManagerBuilder
LLVMOrcErrorCode setIndirectStubPointer(StringRef Name, JITTargetAddress Addr)
JITEventListener - Abstract interface for use by the JIT to notify clients about significant events d...
Bare bones object linking layer.
This class is the base class for all object file types.
Base class for error info classes.
void legacyFailQuery(AsynchronousSymbolQuery &Q, Error Err)
Error removeModule(orc::VModuleKey K) override
uint64_t VModuleKey
VModuleKey provides a unique identifier (allocated and managed by ExecutionSessions) for a module add...
LLVMOrcErrorCode addIRModuleEager(orc::VModuleKey &RetKey, std::unique_ptr< Module > M, LLVMOrcSymbolResolverFn ExternalResolver, void *ExternalResolverCtx)
Target-independent base class for compile callback management.
#define DEFINE_SIMPLE_CONVERSION_FUNCTIONS(ty, ref)
struct LLVMOpaqueTargetMachine * LLVMTargetMachineRef
JITSymbol findSymbolIn(orc::VModuleKey K, const std::string &Name, bool ExportedSymbolsOnly) override
std::map< SymbolStringPtr, JITSymbolFlags > SymbolFlagsMap
A map from symbol names (as SymbolStringPtrs) to JITSymbolFlags.
std::function< std::unique_ptr< IndirectStubsManager >()> IndirectStubsManagerBuilderT
Builder for IndirectStubsManagers.
virtual void log(raw_ostream &OS) const =0
Print an error message to an output stream.
LLVMOrcErrorCode shutdown()
Simple compile functor: Takes a single IR module and returns an ObjectFile.
static const uint16_t * lookup(unsigned opcode, unsigned domain, ArrayRef< uint16_t[3]> Table)
JITEvaluatedSymbol searchOverrides(const std::string &Name)
Search overrided symbols.
auto reverse(ContainerTy &&C, typename std::enable_if< has_rbegin< ContainerTy >::value >::type *=nullptr) -> decltype(make_range(C.rbegin(), C.rend()))
iterator_range< CtorDtorIterator > getDestructors(const Module &M)
Create an iterator range over the entries of the llvm.global_ctors array.
LLVMOrcErrorCode addObject(orc::VModuleKey &RetKey, std::unique_ptr< MemoryBuffer > ObjBuffer, LLVMOrcSymbolResolverFn ExternalResolver, void *ExternalResolverCtx)
JITSymbol findSymbolIn(orc::VModuleKey K, const std::string &Name, bool ExportedSymbolsOnly) override
LLVMOrcErrorCode removeModule(orc::VModuleKey K)
uint64_t JITTargetAddress
Represents an address in the target process's address space.
Error runViaLayer(JITLayerT &JITLayer) const
Run the recorded constructors/destructors through the given JIT layer.
Convenience class for recording constructor/destructor names for later execution. ...
virtual ~GenericLayer()=default
void UnregisterJITEventListener(JITEventListener *L)
LLVMOrcErrorCode createIndirectStub(StringRef StubName, JITTargetAddress Addr)
Interface for looking up the initializer for a variable name, used by Init::resolveReferences.
virtual Error removeModule(orc::VModuleKey K)=0
GenericLayerImpl(LayerT &Layer)
auto find(R &&Range, const T &Val) -> decltype(adl_begin(Range))
Provide wrappers to std::find which take ranges instead of having to pass begin/end explicitly...
std::string mangle(StringRef Name)
JITSymbol findSymbolIn(orc::VModuleKey K, const std::string &Name, bool ExportedSymbolsOnly)
void handleAllErrors(Error E, HandlerTs &&... Handlers)
Behaves the same as handleErrors, except that by contract all errors must be handled by the given han...
LLVMOrcErrorCode addIRModuleLazy(orc::VModuleKey &RetKey, std::unique_ptr< Module > M, LLVMOrcSymbolResolverFn ExternalResolver, void *ExternalResolverCtx)
std::unique_ptr< GenericLayerImpl< LayerT > > createGenericLayer(LayerT &Layer)
Module.h This file contains the declarations for the Module class.
virtual JITSymbol findSymbolIn(orc::VModuleKey K, const std::string &Name, bool ExportedSymbolsOnly)=0
LLVMOrcErrorCode createLazyCompileCallback(JITTargetAddress &RetAddr, LLVMOrcLazyCompileCallbackFn Callback, void *CallbackCtx)
uint64_t(* LLVMOrcLazyCompileCallbackFn)(LLVMOrcJITStackRef JITStack, void *CallbackCtx)
An ExecutionSession represents a running JIT program.
GenericLayerImpl(LayerT &Layer)
void swap(llvm::BitVector &LHS, llvm::BitVector &RHS)
Implement std::swap in terms of BitVector swap.
SymbolResolver is a composable interface for looking up symbol flags and addresses using the Asynchro...
LLVMOrcErrorCode addIRModule(orc::VModuleKey &RetKey, LayerT &Layer, std::unique_ptr< Module > M, std::unique_ptr< RuntimeDyld::MemoryManager > MemMgr, LLVMOrcSymbolResolverFn ExternalResolver, void *ExternalResolverCtx)
Represents a symbol that has been evaluated to an address already.
const std::string & getErrorMessage() const
static PtrTy fromTargetAddress(JITTargetAddress Addr)
static int reportError(const char *ProgName, Twine Msg)
uint64_t(* LLVMOrcSymbolResolverFn)(const char *Name, void *LookupCtx)
LLVMAttributeRef wrap(Attribute Attr)
OrcCBindingsStack(TargetMachine &TM, IndirectStubsManagerBuilder IndirectStubsMgrBuilder)
iterator_range< CtorDtorIterator > getConstructors(const Module &M)
Create an iterator range over the entries of the llvm.global_ctors array.
LLVMOrcErrorCode findSymbolAddressIn(JITTargetAddress &RetAddr, orc::VModuleKey K, const std::string &Name, bool ExportedSymbolsOnly)
std::unique_ptr< JITCompileCallbackManager > createLocalCompileCallbackManager(const Triple &T, ExecutionSession &ES, JITTargetAddress ErrorHandlerAddress)
Create a local compile callback manager.
static void Query(const MachineInstr &MI, AliasAnalysis &AA, bool &Read, bool &Write, bool &Effects, bool &StackPointer)
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
This is a simple memory manager which implements the methods called by the RuntimeDyld class to alloc...
A raw_ostream that writes to an std::string.
JITSymbol findSymbol(const std::string &Name, bool ExportedSymbolsOnly)
Lightweight error class with error context and mandatory checking.
Primary interface to the complete machine description for the target machine.
Error removeModule(orc::VModuleKey K) override
void getNameWithPrefix(raw_ostream &OS, const GlobalValue *GV, bool CannotUsePrivateLabel) const
Print the appropriate prefix and the specified global variable's name.
Error takeError()
Move the error field value out of this JITSymbol.
JITSymbol findSymbol(StringRef Name, bool ExportedSymbolsOnly)
Search for the given named symbol.
StringRef - Represent a constant reference to a string, i.e.
struct LLVMOrcOpaqueJITStack * LLVMOrcJITStackRef
std::set< SymbolStringPtr > SymbolNameSet
A set of symbol names (represented by SymbolStringPtrs for.
LLVMOrcErrorCode findSymbolAddress(JITTargetAddress &RetAddr, const std::string &Name, bool ExportedSymbolsOnly)