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 if (!Sym.getFlags().isStrong())
140 }
else if (
auto Err = Sym.takeError()) {
141 Stack.reportError(std::move(Err));
150 lookup(std::shared_ptr<orc::AsynchronousSymbolQuery>
Query,
154 for (
auto &S : Symbols) {
155 if (
auto Sym = findSymbol(*S)) {
156 if (
auto Addr = Sym.getAddress()) {
158 Query->notifySymbolReady();
163 }
else if (
auto Err = Sym.takeError()) {
167 UnresolvedSymbols.
insert(S);
170 if (Query->isFullyResolved())
171 Query->handleFullyResolved();
173 if (Query->isFullyReady())
174 Query->handleFullyReady();
176 return UnresolvedSymbols;
186 if (Stack.CODLayer) {
187 if (
auto Sym = Stack.CODLayer->findSymbol(Name,
true))
189 else if (
auto Err = Sym.takeError())
192 if (
auto Sym = Stack.CompileLayer.
findSymbol(Name,
true))
194 else if (
auto Err = Sym.takeError())
195 return Sym.takeError();
201 if (ExternalResolver)
202 return JITSymbol(ExternalResolver(Name.c_str(), ExternalResolverCtx),
210 void *ExternalResolverCtx =
nullptr;
216 : CCMgr(createCompileCallbackManager(TM, ES)), DL(TM.createDataLayout()),
217 IndirectStubsMgr(IndirectStubsMgrBuilder()),
220 auto ResolverI = Resolvers.find(K);
221 assert(ResolverI != Resolvers.end() &&
222 "No resolver for module K");
223 auto Resolver = std::move(ResolverI->second);
224 Resolvers.erase(ResolverI);
226 std::make_shared<SectionMemoryManager>(), Resolver};
231 this->notifyFinalized(K, Obj, LoadedObjInfo);
234 this->notifyFreed(K, Obj);
237 CODLayer(createCODLayer(ES, CompileLayer, CCMgr.get(),
238 std::move(IndirectStubsMgrBuilder), Resolvers)),
240 [
this](
const std::string &S) {
return mangle(S); }) {}
244 CXXRuntimeOverrides.runDestructors();
246 for (
auto &DtorRunner : IRStaticDestructorRunners)
247 if (
auto Err = DtorRunner.runViaLayer(*
this))
253 std::string MangledName;
261 template <
typename PtrTy>
263 return reinterpret_cast<PtrTy
>(
static_cast<uintptr_t
>(Addr));
270 return Callback(
wrap(
this), CallbackCtx);
273 return CCMgr->getCompileCallback(std::move(WrappedCallback));
277 return IndirectStubsMgr->createStub(StubName, Addr,
282 return IndirectStubsMgr->updatePointer(Name, Addr);
285 template <
typename LayerT>
288 std::unique_ptr<RuntimeDyld::MemoryManager> MemMgr,
290 void *ExternalResolverCtx) {
293 if (M->getDataLayout().isDefault())
294 M->setDataLayout(DL);
298 std::vector<std::string> CtorNames, DtorNames;
300 CtorNames.push_back(mangle(Ctor.Func->getName()));
302 DtorNames.push_back(mangle(Dtor.Func->getName()));
305 auto K = ES.allocateVModule();
306 Resolvers[K] = std::make_shared<CBindingsResolver>(*
this, ExternalResolver,
307 ExternalResolverCtx);
308 if (
auto Err = Layer.addModule(K, std::move(M)))
309 return std::move(Err);
317 return std::move(Err);
319 IRStaticDestructorRunners.emplace_back(std::move(DtorNames), K);
327 void *ExternalResolverCtx) {
328 return addIRModule(CompileLayer, std::move(M),
329 llvm::make_unique<SectionMemoryManager>(),
330 std::move(ExternalResolver), ExternalResolverCtx);
336 void *ExternalResolverCtx) {
338 return make_error<StringError>(
"Can not add lazy module: No compile " 339 "callback manager available",
342 return addIRModule(*CODLayer, std::move(M),
343 llvm::make_unique<SectionMemoryManager>(),
344 std::move(ExternalResolver), ExternalResolverCtx);
351 ES.releaseVModule(K);
358 void *ExternalResolverCtx) {
360 ObjBuffer->getMemBufferRef())) {
362 auto K = ES.allocateVModule();
363 Resolvers[K] = std::make_shared<CBindingsResolver>(
364 *
this, ExternalResolver, ExternalResolverCtx);
366 if (
auto Err = ObjectLayer.addObject(K, std::move(ObjBuffer)))
367 return std::move(Err);
373 return Obj.takeError();
377 bool ExportedSymbolsOnly) {
378 if (
auto Sym = IndirectStubsMgr->findStub(Name, ExportedSymbolsOnly))
381 return CODLayer->findSymbol(mangle(Name), ExportedSymbolsOnly);
382 return CompileLayer.findSymbol(mangle(Name), ExportedSymbolsOnly);
386 bool ExportedSymbolsOnly) {
387 assert(KeyLayers.count(K) &&
"looking up symbol in unknown module");
388 return KeyLayers[K]->findSymbolIn(K, mangle(Name), ExportedSymbolsOnly);
392 bool ExportedSymbolsOnly) {
393 if (
auto Sym = findSymbol(Name, ExportedSymbolsOnly)) {
395 if (
auto AddrOrErr = Sym.getAddress())
398 return AddrOrErr.takeError();
399 }
else if (
auto Err = Sym.takeError()) {
401 return std::move(Err);
409 const std::string &
Name,
410 bool ExportedSymbolsOnly) {
411 if (
auto Sym =
findSymbolIn(K, Name, ExportedSymbolsOnly)) {
413 if (
auto AddrOrErr = Sym.getAddress())
416 return AddrOrErr.takeError();
417 }
else if (
auto Err = Sym.takeError()) {
419 return std::move(Err);
431 EventListeners.push_back(L);
439 if (
I != EventListeners.rend()) {
441 EventListeners.pop_back();
447 std::map<orc::VModuleKey, std::shared_ptr<orc::SymbolResolver>>;
449 static std::unique_ptr<CompileCallbackMgr>
458 return std::move(*CCMgr);
461 static std::unique_ptr<CODLayerT>
465 ResolverMap &Resolvers) {
471 return llvm::make_unique<CODLayerT>(
474 auto ResolverI = Resolvers.find(K);
475 assert(ResolverI != Resolvers.end() &&
"No resolver for module K");
476 return ResolverI->second;
479 std::shared_ptr<orc::SymbolResolver>
Resolver) {
480 assert(!Resolvers.count(K) &&
"Resolver already present");
483 [](
Function &
F) {
return std::set<Function *>({&
F}); }, *CCMgr,
484 std::move(IndirectStubsMgrBuilder),
false);
495 uint64_t
Key =
static_cast<uint64_t
>(
496 reinterpret_cast<uintptr_t
>(Obj.
getData().
data()));
497 for (
auto &Listener : EventListeners)
498 Listener->notifyObjectLoaded(Key, Obj, LoadedObjInfo);
502 uint64_t
Key =
static_cast<uint64_t
>(
503 reinterpret_cast<uintptr_t
>(Obj.
getData().
data()));
504 for (
auto &Listener : EventListeners)
505 Listener->notifyFreeingObject(Key);
509 std::unique_ptr<CompileCallbackMgr> CCMgr;
511 std::vector<JITEventListener *> EventListeners;
516 std::unique_ptr<orc::IndirectStubsManager> IndirectStubsMgr;
520 std::unique_ptr<CODLayerT> CODLayer;
522 std::map<orc::VModuleKey, std::unique_ptr<detail::GenericLayer>> KeyLayers;
525 std::vector<orc::LegacyCtorDtorRunner<OrcCBindingsStack>> IRStaticDestructorRunners;
528 ResolverMap Resolvers;
533 #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. ...
Error createIndirectStub(StringRef StubName, JITTargetAddress Addr)
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.
This class represents lattice values for constants.
Error removeModule(orc::VModuleKey K)
Convenience class for recording constructor/destructor names for later execution. ...
JITEvaluatedSymbol searchOverrides(const std::string &Name)
Search overrided symbols.
std::function< std::unique_ptr< CompileCallbackMgr >()> CallbackManagerBuilder
JITEventListener - Abstract interface for use by the JIT to notify clients about significant events d...
This class is the base class for all object file types.
uint64_t VModuleKey
VModuleKey provides a unique identifier (allocated and managed by ExecutionSessions) for a module add...
Expected< orc::VModuleKey > addIRModuleLazy(std::unique_ptr< Module > M, LLVMOrcSymbolResolverFn ExternalResolver, void *ExternalResolverCtx)
LLVM_NODISCARD LLVM_ATTRIBUTE_ALWAYS_INLINE const char * data() const
data - Get a pointer to the start of the string (which may not be null terminated).
Target-independent base class for compile callback management.
amdgpu Simplify well known AMD library false Value Value const Twine & Name
#define DEFINE_SIMPLE_CONVERSION_FUNCTIONS(ty, ref)
Error setIndirectStubPointer(StringRef Name, JITTargetAddress Addr)
struct LLVMOpaqueTargetMachine * LLVMTargetMachineRef
JITSymbol findSymbolIn(orc::VModuleKey K, const std::string &Name, bool ExportedSymbolsOnly) override
Tagged union holding either a T or a Error.
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)
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.
void legacyFailQuery(AsynchronousSymbolQuery &Q, Error Err)
uint64_t JITTargetAddress
Represents an address in the target process's address space.
DenseSet< SymbolStringPtr > SymbolNameSet
A set of symbol names (represented by SymbolStringPtrs for.
virtual ~GenericLayer()=default
JITSymbol findSymbolIn(orc::VModuleKey K, const std::string &Name, bool ExportedSymbolsOnly) override
Expected< JITTargetAddress > findSymbolAddress(const std::string &Name, bool ExportedSymbolsOnly)
void UnregisterJITEventListener(JITEventListener *L)
Interface for looking up the initializer for a variable name, used by Init::resolveReferences.
std::pair< iterator, bool > insert(const ValueT &V)
virtual Error removeModule(orc::VModuleKey K)=0
GenericLayerImpl(LayerT &Layer)
const Triple & getTargetTriple() const
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...
void logAllUnhandledErrors(Error E, raw_ostream &OS, Twine ErrorBanner={})
Log all errors (if any) in E to OS.
std::string mangle(StringRef Name)
JITSymbol findSymbolIn(orc::VModuleKey K, const std::string &Name, bool ExportedSymbolsOnly)
static ErrorSuccess success()
Create a success value.
std::unique_ptr< GenericLayerImpl< LayerT > > createGenericLayer(LayerT &Layer)
Expected< std::unique_ptr< JITCompileCallbackManager > > createLocalCompileCallbackManager(const Triple &T, ExecutionSession &ES, JITTargetAddress ErrorHandlerAddress)
Create a local compile callback manager.
Module.h This file contains the declarations for the Module class.
virtual JITSymbol findSymbolIn(orc::VModuleKey K, const std::string &Name, bool ExportedSymbolsOnly)=0
uint64_t(* LLVMOrcLazyCompileCallbackFn)(LLVMOrcJITStackRef JITStack, void *CallbackCtx)
An ExecutionSession represents a running JIT program.
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...
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)
Expected< orc::VModuleKey > addIRModule(LayerT &Layer, std::unique_ptr< Module > M, std::unique_ptr< RuntimeDyld::MemoryManager > MemMgr, LLVMOrcSymbolResolverFn ExternalResolver, void *ExternalResolverCtx)
uint64_t(* LLVMOrcSymbolResolverFn)(const char *Name, void *LookupCtx)
LLVMAttributeRef wrap(Attribute Attr)
Error removeModule(orc::VModuleKey K) override
OrcCBindingsStack(TargetMachine &TM, IndirectStubsManagerBuilder IndirectStubsMgrBuilder)
Expected< orc::VModuleKey > addObject(std::unique_ptr< MemoryBuffer > ObjBuffer, LLVMOrcSymbolResolverFn ExternalResolver, void *ExternalResolverCtx)
iterator_range< CtorDtorIterator > getConstructors(const Module &M)
Create an iterator range over the entries of the llvm.global_ctors array.
Expected< orc::VModuleKey > addIRModuleEager(std::unique_ptr< Module > M, LLVMOrcSymbolResolverFn ExternalResolver, void *ExternalResolverCtx)
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.
Expected< JITTargetAddress > createLazyCompileCallback(LLVMOrcLazyCompileCallbackFn Callback, void *CallbackCtx)
std::function< std::unique_ptr< IndirectStubsMgrT >()> IndirectStubsManagerBuilderT
Builder for IndirectStubsManagers.
Expected< JITTargetAddress > findSymbolAddressIn(orc::VModuleKey K, const std::string &Name, bool ExportedSymbolsOnly)
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.
StringRef - Represent a constant reference to a string, i.e.
Bare bones object linking layer.
struct LLVMOrcOpaqueJITStack * LLVMOrcJITStackRef
StringRef getData() const
Error runViaLayer(JITLayerT &JITLayer) const
Run the recorded constructors/destructors through the given JIT layer.
JITSymbol findSymbol(const std::string &Name, bool ExportedSymbolsOnly)
Search for the given named symbol.
std::error_code inconvertibleErrorCode()
The value returned by this function can be returned from convertToErrorCode for Error values where no...