13 #ifndef LLVM_EXECUTIONENGINE_ORC_CORE_H 14 #define LLVM_EXECUTIONENGINE_ORC_CORE_H 27 #define DEBUG_TYPE "orc" 33 class AsynchronousSymbolQuery;
34 class ExecutionSession;
35 class MaterializationUnit;
36 class MaterializationResponsibility;
94 std::vector<std::pair<JITDylib *, JITDylibLookupFlags>>;
102 O.reserve(JDs.
size());
104 O.push_back(std::make_pair(JD, Flags));
117 using value_type = std::pair<SymbolStringPtr, SymbolLookupFlags>;
127 add(std::move(Name), Flags);
132 std::initializer_list<SymbolStringPtr> Names,
134 Symbols.reserve(Names.size());
135 for (
auto &
Name : Names)
144 Symbols.reserve(Names.
size());
145 for (
const auto &
Name : Names)
156 Symbols.reserve(Names.
size());
157 for (
const auto &
Name : Names)
165 Symbols.push_back(std::make_pair(std::move(Name), Flags));
168 bool empty()
const {
return Symbols.empty(); }
169 UnderlyingVector::size_type
size()
const {
return Symbols.size(); }
176 void remove(UnderlyingVector::size_type
I) {
187 template <
typename PredFn>
void remove_if(PredFn &&Pred) {
188 UnderlyingVector::size_type
I = 0;
189 while (I != Symbols.size()) {
190 const auto &
Name = Symbols[
I].first;
191 auto Flags = Symbols[
I].second;
192 if (Pred(
Name, Flags))
204 template <
typename BodyFn>
206 std::is_same<decltype(Body(std::declval<const SymbolStringPtr &>(),
207 std::declval<SymbolLookupFlags>())),
209 UnderlyingVector::size_type
I = 0;
210 while (I != Symbols.size()) {
211 const auto &
Name = Symbols[
I].first;
212 auto Flags = Symbols[
I].second;
213 if (Body(
Name, Flags))
226 template <
typename BodyFn>
228 std::is_same<decltype(Body(std::declval<const SymbolStringPtr &>(),
229 std::declval<SymbolLookupFlags>())),
232 UnderlyingVector::size_type
I = 0;
233 while (I != Symbols.size()) {
234 const auto &
Name = Symbols[
I].first;
235 auto Flags = Symbols[
I].second;
236 auto Remove = Body(
Name, Flags);
238 return Remove.takeError();
251 Names.reserve(Symbols.size());
252 for (
auto &KV : Symbols)
253 Names.push_back(KV.first);
262 return LHS.first < RHS.first;
270 return *LHS.first < *RHS.first;
278 auto LastI = std::unique(Symbols.begin(), Symbols.end());
279 Symbols.erase(LastI, Symbols.end());
286 if (Symbols.size() < 2)
289 for (UnderlyingVector::size_type
I = 1;
I != Symbols.size(); ++
I)
303 : Aliasee(
std::move(Aliasee)), AliasFlags(AliasFlags) {}
377 std::function<void(const SymbolDependenceMap &)>;
390 std::error_code convertToErrorCode()
const override;
395 std::shared_ptr<SymbolDependenceMap> Symbols;
405 std::error_code convertToErrorCode()
const override;
419 std::error_code convertToErrorCode()
const override;
490 Error notifyEmitted();
510 void failMaterialization();
517 void replace(std::unique_ptr<MaterializationUnit> MU);
576 discard(JD, std::move(Name));
584 virtual void anchor();
599 std::vector<std::unique_ptr<MaterializationUnit>>;
629 inline std::unique_ptr<AbsoluteSymbolsMaterializationUnit>
631 return std::make_unique<AbsoluteSymbolsMaterializationUnit>(
632 std::move(Symbols), std::move(K));
674 inline std::unique_ptr<ReExportsMaterializationUnit>
676 return std::make_unique<ReExportsMaterializationUnit>(
684 inline std::unique_ptr<ReExportsMaterializationUnit>
689 return std::make_unique<ReExportsMaterializationUnit>(
690 &SourceJD, SourceJDLookupFlags, std::move(Aliases), std::move(K));
732 assert(ResolvedSymbols.count(Name) &&
733 "Redundant removal of weakly-referenced symbol");
734 ResolvedSymbols.erase(Name);
735 --OutstandingSymbolsCount;
740 bool isComplete()
const {
return OutstandingSymbolsCount == 0; }
746 void handleComplete();
749 SymbolState getRequiredState() {
return RequiredState; }
757 void handleFailed(
Error Err);
764 size_t OutstandingSymbolsCount;
797 std::set<std::shared_ptr<AsynchronousSymbolQuery>>;
805 const std::string &
getName()
const {
return JITDylibName; }
816 template <
typename GeneratorT>
817 GeneratorT &addGenerator(std::unique_ptr<GeneratorT> DefGenerator);
845 bool SearchThisJITDylibFirst =
true);
861 void removeFromSearchOrder(
JITDylib &JD);
864 template <
typename Func>
865 auto withSearchOrderDo(Func &&
F)
866 -> decltype(
F(std::declval<const JITDylibSearchOrder &>()));
873 template <
typename MaterializationUnitType>
874 Error define(std::unique_ptr<MaterializationUnitType> &&MU);
883 template <
typename MaterializationUnitType>
884 Error define(std::unique_ptr<MaterializationUnitType> &MU);
917 legacyLookup(std::shared_ptr<AsynchronousSymbolQuery> Q,
SymbolNameSet Names);
920 using AsynchronousSymbolQueryList =
921 std::vector<std::shared_ptr<AsynchronousSymbolQuery>>;
923 struct UnmaterializedInfo {
924 UnmaterializedInfo(std::unique_ptr<MaterializationUnit> MU)
925 : MU(std::move(MU)) {}
927 std::unique_ptr<MaterializationUnit> MU;
933 struct MaterializingInfo {
937 void addQuery(std::shared_ptr<AsynchronousSymbolQuery> Q);
939 AsynchronousSymbolQueryList takeQueriesMeeting(
SymbolState RequiredState);
940 AsynchronousSymbolQueryList takeAllPendingQueries() {
941 return std::move(PendingQueries);
943 bool hasQueriesPending()
const {
return !PendingQueries.empty(); }
944 const AsynchronousSymbolQueryList &pendingQueries()
const {
945 return PendingQueries;
948 AsynchronousSymbolQueryList PendingQueries;
953 class SymbolTableEntry {
955 SymbolTableEntry() =
default;
958 MaterializerAttached(
false), PendingRemoval(
false) {}
964 bool isInMaterializationPhase()
const {
969 bool hasMaterializerAttached()
const {
return MaterializerAttached; }
970 bool isPendingRemoval()
const {
return PendingRemoval; }
975 assert(static_cast<uint8_t>(State) < (1 << 6) &&
976 "State does not fit in bitfield");
977 this->State =
static_cast<uint8_t
>(State);
980 void setMaterializerAttached(
bool MaterializerAttached) {
981 this->MaterializerAttached = MaterializerAttached;
984 void setPendingRemoval(
bool PendingRemoval) {
985 this->PendingRemoval = PendingRemoval;
996 uint8_t MaterializerAttached : 1;
997 uint8_t PendingRemoval : 1;
1011 std::shared_ptr<AsynchronousSymbolQuery> &Q,
LookupKind K,
1016 std::shared_ptr<AsynchronousSymbolQuery> &Q,
1020 bool lookupImpl(std::shared_ptr<AsynchronousSymbolQuery> &Q,
1021 std::vector<std::unique_ptr<MaterializationUnit>> &MUs,
1027 void transferEmittedNodeDependencies(MaterializingInfo &DependantMI,
1029 MaterializingInfo &EmittedMI);
1033 void replace(std::unique_ptr<MaterializationUnit> MU);
1044 using FailedSymbolsWorklist =
1045 std::vector<std::pair<JITDylib *, SymbolStringPtr>>;
1046 static void notifyFailed(FailedSymbolsWorklist FailedSymbols);
1049 std::string JITDylibName;
1053 std::vector<std::unique_ptr<DefinitionGenerator>> DefGenerators;
1068 JITDylib &JD, std::unique_ptr<MaterializationUnit> MU)>;
1083 std::lock_guard<std::recursive_mutex>
Lock(SessionMutex);
1096 JITDylib &createJITDylib(std::string Name);
1100 return runSessionLocked([
this]() {
return ++LastKey; });
1111 this->ReportError = std::move(ReportError);
1123 this->DispatchMaterialization = std::move(DispatchMaterialization);
1175 NoDependenciesToRegister);
1197 std::unique_ptr<MaterializationUnit> MU) {
1199 runSessionLocked([&]() {
1200 dbgs() <<
"Dispatching " << *MU <<
" for " << JD.
getName() <<
"\n";
1203 DispatchMaterialization(JD, std::move(MU));
1210 static void logErrorsToStdErr(
Error Err) {
1215 materializeOnCurrentThread(
JITDylib &JD,
1216 std::unique_ptr<MaterializationUnit> MU) {
1217 MU->doMaterialize(JD);
1220 void runOutstandingMUs();
1222 mutable std::recursive_mutex SessionMutex;
1223 std::shared_ptr<SymbolStringPool> SSP;
1227 materializeOnCurrentThread;
1229 std::vector<std::unique_ptr<JITDylib>> JDs;
1233 mutable std::recursive_mutex OutstandingMUsMutex;
1234 std::vector<std::pair<JITDylib *, std::unique_ptr<MaterializationUnit>>>
1238 template <
typename GeneratorT>
1240 auto &
G = *DefGenerator;
1241 ES.runSessionLocked(
1242 [&]() { DefGenerators.push_back(std::move(DefGenerator)); });
1246 template <
typename Func>
1248 -> decltype(
F(std::declval<const JITDylibSearchOrder &>())) {
1249 return ES.runSessionLocked([&]() {
return F(SearchOrder); });
1252 template <
typename MaterializationUnitType>
1254 assert(MU &&
"Can not define with a null MU");
1255 return ES.runSessionLocked([&,
this]() ->
Error {
1256 if (
auto Err = defineImpl(*MU))
1260 auto UMI = std::make_shared<UnmaterializedInfo>(std::move(MU));
1261 for (
auto &KV : UMI->MU->getSymbols())
1262 UnmaterializedInfos[KV.first] = UMI;
1268 template <
typename MaterializationUnitType>
1270 assert(MU &&
"Can not define with a null MU");
1272 return ES.runSessionLocked([&,
this]() ->
Error {
1273 if (
auto Err = defineImpl(*MU))
1277 auto UMI = std::make_shared<UnmaterializedInfo>(std::move(MU));
1278 for (
auto &KV : UMI->MU->getSymbols())
1279 UnmaterializedInfos[KV.first] = UMI;
1323 #undef DEBUG_TYPE // "orc" 1325 #endif // LLVM_EXECUTIONENGINE_ORC_CORE_H JITSymbolFlags AliasFlags
Queried, materialization begun.
SymbolLookupSet(std::initializer_list< SymbolStringPtr > Names, SymbolLookupFlags Flags=SymbolLookupFlags::RequiredSymbol)
Construct a SymbolLookupSet from an initializer list of SymbolStringPtrs.
A parsed version of the target data layout string in and methods for querying it. ...
std::shared_ptr< SymbolStringPool > getSymbolStringPool() const
Returns a shared_ptr to the SymbolStringPool for this ExecutionSession.
void remove_if(PredFn &&Pred)
Removes all elements matching the given predicate, which must be callable as bool(const SymbolStringP...
ExecutionSession & setDispatchMaterialization(DispatchMaterializationFunction DispatchMaterialization)
Set the materialization dispatch function.
raw_ostream & errs()
This returns a reference to a raw_ostream for standard error.
const_iterator begin(StringRef path, Style style=Style::native)
Get begin iterator over path.
Added to the symbol table, never queried.
LookupKind
Describes the kind of lookup being performed.
This class represents lattice values for constants.
void dispatchMaterialization(JITDylib &JD, std::unique_ptr< MaterializationUnit > MU)
Materialize the given unit.
amdgpu Simplify well known AMD library false FunctionCallee Value const Twine & Name
std::function< void(const SymbolDependenceMap &)> RegisterDependenciesFunction
Callback to register the dependencies for a given query.
void doDiscard(const JITDylib &JD, const SymbolStringPtr &Name)
Called by JITDylibs to notify MaterializationUnits that the given symbol has been overridden...
bool isComplete() const
Returns true if all symbols covered by this query have been resolved.
SymbolLookupSet(const SymbolNameSet &Names, SymbolLookupFlags Flags=SymbolLookupFlags::RequiredSymbol)
Construct a SymbolLookupSet from a SymbolNameSet with the given Flags used for each value...
SymbolState
Represents the state that a symbol has reached during materialization.
static bool lookup(const GsymReader &GR, DataExtractor &Data, uint64_t &Offset, uint64_t BaseAddr, uint64_t Addr, SourceLocations &SrcLocs, llvm::Error &Err)
A Lookup helper functions.
void replace(Container &Cont, typename Container::iterator ContIt, typename Container::iterator ContEnd, RandomAccessIterator ValIt, RandomAccessIterator ValEnd)
Given a sequence container Cont, replace the range [ContIt, ContEnd) with the range [ValIt...
std::vector< std::pair< JITDylib *, JITDylibLookupFlags > > JITDylibSearchOrder
A list of (JITDylib*, JITDylibLookupFlags) pairs to be used as a search order during symbol lookup...
void removeDuplicates()
Remove any duplicate elements.
auto forEachWithRemoval(BodyFn &&Body) -> typename std::enable_if< std::is_same< decltype(Body(std::declval< const SymbolStringPtr &>(), std::declval< SymbolLookupFlags >())), bool >::value >::type
Loop over the elements of this SymbolLookupSet, applying the Body function to each one...
std::vector< value_type > UnderlyingVector
GeneratorT & addGenerator(std::unique_ptr< GeneratorT > DefGenerator)
Adds a definition generator to this JITDylib and returns a referenece to it.
Used to notify a JITDylib that the given set of symbols failed to materialize.
std::function< bool(SymbolStringPtr)> SymbolPredicate
std::unique_ptr< AbsoluteSymbolsMaterializationUnit > absoluteSymbols(SymbolMap Symbols, VModuleKey K=VModuleKey())
Create an AbsoluteSymbolsMaterializationUnit with the given symbols.
void doMaterialize(JITDylib &JD)
Called by materialization dispatchers (see ExecutionSession::DispatchMaterializationFunction) to trig...
SymbolStringPtr intern(StringRef SymName)
Add a symbol name to the SymbolStringPool and return a pointer to it.
SymbolFlagsMap SymbolFlags
Error define(std::unique_ptr< MaterializationUnitType > &&MU)
Define all symbols provided by the materialization unit to be part of this JITDylib.
Mangles symbol names then uniques them in the context of an ExecutionSession.
static StringRef getName(Value *V)
raw_ostream & operator<<(raw_ostream &OS, const SymbolStringPtr &Sym)
Render a SymbolStringPtr.
auto withSearchOrderDo(Func &&F) -> decltype(F(std::declval< const JITDylibSearchOrder &>()))
Do something with the search order (run under the session lock).
Tagged union holding either a T or a Error.
std::function< SymbolNameSet(std::shared_ptr< AsynchronousSymbolQuery > Q, SymbolNameSet Names)> LegacyAsyncLookupFunction
Tracks responsibility for materialization, and mediates interactions between MaterializationUnits and...
virtual ~MaterializationUnit()
void releaseVModule(VModuleKey Key)
Return a module key to the ExecutionSession so that it can be re-used.
SymbolLookupFlags
Lookup flags that apply to each symbol in a lookup.
std::set< std::shared_ptr< AsynchronousSymbolQuery > > AsynchronousSymbolQuerySet
llvm::detail::DenseMapPair< SymbolStringPtr, JITSymbolFlags > value_type
void dropSymbol(const SymbolStringPtr &Name)
Remove a symbol from the query.
ArrayRef - Represent a constant reference to an array (0 or more elements consecutively in memory)...
Legacy adapter. Remove once we kill off the old ORC layers.
JITDylib & getTargetJITDylib() const
Returns the target JITDylib that these symbols are being materialized into.
uint64_t JITTargetAddress
Represents an address in the target process's address space.
ExecutionSession & getExecutionSession() const
Get a reference to the ExecutionSession for this JITDylib.
auto forEachWithRemoval(BodyFn &&Body) -> typename std::enable_if< std::is_same< decltype(Body(std::declval< const SymbolStringPtr &>(), std::declval< SymbolLookupFlags >())), Expected< bool >>::value, Error >::type
Loop over the elements of this SymbolLookupSet, applying the Body function to each one...
bool containsDuplicates()
Returns true if this set contains any duplicates.
std::unique_ptr< ReExportsMaterializationUnit > symbolAliases(SymbolAliasMap Aliases, VModuleKey K=VModuleKey())
Create a ReExportsMaterializationUnit with the given aliases.
Pointer to a pooled string representing a symbol name.
DenseSet< SymbolStringPtr > SymbolNameSet
A set of symbol names (represented by SymbolStringPtrs for.
void dump(const SparseBitVector< ElementSize > &LHS, raw_ostream &out)
VModuleKey getVModuleKey() const
Returns the VModuleKey for this instance.
A materialization unit for symbol aliases.
static void LLVM_ATTRIBUTE_NORETURN ReportError(uint64_t StartOffset, const char *ErrorMsg)
size_t size() const
size - Get the array size.
void sortByAddress()
Sort the lookup set by pointer value.
const SymbolNameSet & getSymbols() const
VModuleKey allocateVModule()
Allocate a module key for a new module to add to the JIT.
Expected< SymbolAliasMap > buildSimpleReexportsAAliasMap(JITDylib &SourceJD, const SymbolNameSet &Symbols)
Build a SymbolAliasMap for the common case where you want to re-export symbols from another JITDylib ...
UnderlyingVector::const_iterator const_iterator
Flags for symbols in the JIT.
SymbolNameVector getSymbolNames() const
Construct a SymbolNameVector from this instance by dropping the Flags values.
void add(SymbolStringPtr Name, SymbolLookupFlags Flags=SymbolLookupFlags::RequiredSymbol)
Add an element to the set.
Used to notify clients when symbols can not be found during a lookup.
JITDylibSearchOrder makeJITDylibSearchOrder(ArrayRef< JITDylib *> JDs, JITDylibLookupFlags Flags=JITDylibLookupFlags::MatchExportedSymbolsOnly)
Convenience function for creating a search order from an ArrayRef of JITDylib*, all with the same fla...
Definition generators can be attached to JITDylibs to generate new definitions for otherwise unresolv...
const SymbolDependenceMap & getSymbols() const
void logAllUnhandledErrors(Error E, raw_ostream &OS, Twine ErrorBanner={})
Log all errors (if any) in E to OS.
void sort(IteratorTy Start, IteratorTy End)
No symbol should be in this state.
A MaterializationUnit represents a set of symbol definitions that can be materialized as a group...
static ErrorSuccess success()
Create a success value.
static uint64_t add(uint64_t LeftOp, uint64_t RightOp)
Module.h This file contains the declarations for the Module class.
SymbolLookupSet(ArrayRef< SymbolStringPtr > Names, SymbolLookupFlags Flags=SymbolLookupFlags::RequiredSymbol)
Construct a SymbolLookupSet from a vector of symbols with the given Flags used for each value...
A set of symbols to look up, each associated with a SymbolLookupFlags value.
Emitted to memory, but waiting on transitive dependencies.
static uint32_t getFlags(const Symbol *Sym)
An ExecutionSession represents a running JIT program.
raw_ostream & dbgs()
dbgs() - This returns a reference to a raw_ostream for debugging messages.
void swap(llvm::BitVector &LHS, llvm::BitVector &RHS)
Implement std::swap in terms of BitVector swap.
const SymbolFlagsMap & getSymbols() const
Returns the symbol flags map for this responsibility instance.
const SymbolNameVector & getSymbols() const
const_iterator begin() const
Represents a symbol that has been evaluated to an address already.
Base class for user error types.
A symbol query that returns results via a callback when results are ready.
std::vector< std::unique_ptr< MaterializationUnit > > MaterializationUnitList
uint8_t uint64_t VModuleKey
VModuleKey provides a unique identifier (allocated and managed by ExecutionSessions) for a module add...
const SymbolFlagsMap & getSymbols() const
Return the set of symbols that this source provides.
const_iterator end() const
UnderlyingVector::iterator iterator
void reportError(Error Err)
Report a error for this execution session.
std::unique_ptr< ReExportsMaterializationUnit > reexports(JITDylib &SourceJD, SymbolAliasMap Aliases, JITDylibLookupFlags SourceJDLookupFlags=JITDylibLookupFlags::MatchExportedSymbolsOnly, VModuleKey K=VModuleKey())
Create a materialization unit for re-exporting symbols from another JITDylib with alternative names/f...
auto runSessionLocked(Func &&F) -> decltype(F())
Run the given lambda with the session mutex locked.
A MaterializationUnit implementation for pre-existing absolute symbols.
const std::string & getName() const
Get the name for this JITDylib.
ExecutionSession & setErrorReporter(ErrorReporter ReportError)
Set the error reporter function.
SymbolLookupSet(SymbolStringPtr Name, SymbolLookupFlags Flags=SymbolLookupFlags::RequiredSymbol)
UnderlyingVector::size_type size() const
MaterializationUnit(SymbolFlagsMap InitalSymbolFlags, VModuleKey K)
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
Assigned address, still materializing.
std::pair< SymbolStringPtr, SymbolLookupFlags > value_type
Lightweight error class with error context and mandatory checking.
Used to notify clients that a set of symbols could not be removed.
std::function< void(Error)> ErrorReporter
For reporting errors.
This class implements an extremely fast bulk output stream that can only output to a stream...
print Print MemDeps of function
StringRef - Represent a constant reference to a string, i.e.
void sortByName()
Sort the lookup set lexicographically.
RegisterDependenciesFunction NoDependenciesToRegister
This can be used as the value for a RegisterDependenciesFunction if there are no dependants to regist...
SymbolAliasMapEntry(SymbolStringPtr Aliasee, JITSymbolFlags AliasFlags)
JITDylibLookupFlags
Lookup flags that apply to each dylib in the search order for a lookup.
std::function< void(JITDylib &JD, std::unique_ptr< MaterializationUnit > MU)> DispatchMaterializationFunction
For dispatching MaterializationUnit::materialize calls.
std::vector< SymbolStringPtr > SymbolNameVector
A vector of symbol names.
A symbol table that supports asynchoronous symbol queries.
ReexportsGenerator can be used with JITDylib::addGenerator to automatically re-export a subset of the...