Skip to content
This repository was archived by the owner on Feb 5, 2019. It is now read-only.

Commit 7891d70

Browse files
committed
[ORC] Add new symbol lookup methods to ExecutionSessionBase in preparation for
deprecating SymbolResolver and AsynchronousSymbolQuery. Both lookup overloads take a VSO search order to perform the lookup. The first overload is non-blocking and takes OnResolved and OnReady callbacks. The second is blocking, takes a boolean flag to indicate whether to wait until all symbols are ready, and returns a SymbolMap. Both overloads take a RegisterDependencies function to register symbol dependencies (if any) on the query. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@337595 91177308-0d34-0410-b5e6-96231b3b80d8
1 parent ec3e957 commit 7891d70

File tree

9 files changed

+749
-512
lines changed

9 files changed

+749
-512
lines changed

include/llvm/ExecutionEngine/Orc/Core.h

Lines changed: 94 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -69,8 +69,23 @@ raw_ostream &operator<<(raw_ostream &OS, const SymbolDependenceMap &Deps);
6969
/// A list of VSO pointers.
7070
using VSOList = std::vector<VSO *>;
7171

72+
/// Render a VSOList.
7273
raw_ostream &operator<<(raw_ostream &OS, const VSOList &VSOs);
7374

75+
/// Callback to notify client that symbols have been resolved.
76+
using SymbolsResolvedCallback = std::function<void(Expected<SymbolMap>)>;
77+
78+
/// Callback to notify client that symbols are ready for execution.
79+
using SymbolsReadyCallback = std::function<void(Error)>;
80+
81+
/// Callback to register the dependencies for a given query.
82+
using RegisterDependenciesFunction =
83+
std::function<void(const SymbolDependenceMap &)>;
84+
85+
/// This can be used as the value for a RegisterDependenciesFunction if there
86+
/// are no dependants to register with.
87+
extern RegisterDependenciesFunction NoDependenciesToRegister;
88+
7489
/// Used to notify a VSO that the given set of symbols failed to materialize.
7590
class FailedToMaterialize : public ErrorInfo<FailedToMaterialize> {
7691
public:
@@ -122,6 +137,9 @@ class MaterializationResponsibility {
122137
/// into.
123138
VSO &getTargetVSO() const { return V; }
124139

140+
/// Returns the symbol flags map for this responsibility instance.
141+
SymbolFlagsMap getSymbols() { return SymbolFlags; }
142+
125143
/// Returns the names of any symbols covered by this
126144
/// MaterializationResponsibility object that have queries pending. This
127145
/// information can be used to return responsibility for unrequested symbols
@@ -223,6 +241,9 @@ class MaterializationUnit {
223241
virtual void discard(const VSO &V, SymbolStringPtr Name) = 0;
224242
};
225243

244+
using MaterializationUnitList =
245+
std::vector<std::unique_ptr<MaterializationUnit>>;
246+
226247
/// A MaterializationUnit implementation for pre-existing absolute symbols.
227248
///
228249
/// All symbols will be resolved and marked ready as soon as the unit is
@@ -322,6 +343,9 @@ buildSimpleReexportsAliasMap(VSO &SourceV, const SymbolNameSet &Symbols);
322343

323344
/// Base utilities for ExecutionSession.
324345
class ExecutionSessionBase {
346+
// FIXME: Remove this when we remove the old ORC layers.
347+
friend class VSO;
348+
325349
public:
326350
/// For reporting errors.
327351
using ErrorReporter = std::function<void(Error)>;
@@ -372,11 +396,50 @@ class ExecutionSessionBase {
372396
void releaseVModule(VModuleKey Key) { /* FIXME: Recycle keys */
373397
}
374398

375-
/// Cause the given query to fail with the given Error.
399+
void legacyFailQuery(AsynchronousSymbolQuery &Q, Error Err);
400+
401+
using LegacyAsyncLookupFunction = std::function<SymbolNameSet(
402+
std::shared_ptr<AsynchronousSymbolQuery> Q, SymbolNameSet Names)>;
403+
404+
/// A legacy lookup function for JITSymbolResolverAdapter.
405+
/// Do not use -- this will be removed soon.
406+
Expected<SymbolMap>
407+
legacyLookup(ExecutionSessionBase &ES, LegacyAsyncLookupFunction AsyncLookup,
408+
SymbolNameSet Names, bool WaiUntilReady,
409+
RegisterDependenciesFunction RegisterDependencies);
410+
411+
/// Search the given VSO list for the given symbols.
376412
///
377-
/// This should only be used by legacy APIs and will be deprecated in the
378-
/// future.
379-
void failQuery(AsynchronousSymbolQuery &Q, Error Err);
413+
///
414+
/// The OnResolve callback will be called once all requested symbols are
415+
/// resolved, or if an error occurs prior to resolution.
416+
///
417+
/// The OnReady callback will be called once all requested symbols are ready,
418+
/// or if an error occurs after resolution but before all symbols are ready.
419+
///
420+
/// If all symbols are found, the RegisterDependencies function will be called
421+
/// while the session lock is held. This gives clients a chance to register
422+
/// dependencies for on the queried symbols for any symbols they are
423+
/// materializing (if a MaterializationResponsibility instance is present,
424+
/// this can be implemented by calling
425+
/// MaterializationResponsibility::addDependencies). If there are no
426+
/// dependenant symbols for this query (e.g. it is being made by a top level
427+
/// client to get an address to call) then the value NoDependenciesToRegister
428+
/// can be used.
429+
void lookup(const VSOList &VSOs, const SymbolNameSet &Symbols,
430+
SymbolsResolvedCallback OnResolve, SymbolsReadyCallback OnReady,
431+
RegisterDependenciesFunction RegisterDependencies);
432+
433+
/// Blocking version of lookup above. Returns the resolved symbol map.
434+
/// If WaitUntilReady is true (the default), will not return until all
435+
/// requested symbols are ready (or an error occurs). If WaitUntilReady is
436+
/// false, will return as soon as all requested symbols are resolved,
437+
/// or an error occurs. If WaitUntilReady is false and an error occurs
438+
/// after resolution, the function will return a success value, but the
439+
/// error will be reported via reportErrors.
440+
Expected<SymbolMap> lookup(const VSOList &VSOs, const SymbolNameSet &Symbols,
441+
RegisterDependenciesFunction RegisterDependencies,
442+
bool WaitUntilReady = true);
380443

381444
/// Materialize the given unit.
382445
void dispatchMaterialization(VSO &V,
@@ -394,12 +457,20 @@ class ExecutionSessionBase {
394457
MU->doMaterialize(V);
395458
}
396459

460+
void runOutstandingMUs();
461+
397462
mutable std::recursive_mutex SessionMutex;
398463
std::shared_ptr<SymbolStringPool> SSP;
399464
VModuleKey LastKey = 0;
400465
ErrorReporter ReportError = logErrorsToStdErr;
401466
DispatchMaterializationFunction DispatchMaterialization =
402467
materializeOnCurrentThread;
468+
469+
// FIXME: Remove this (and runOutstandingMUs) once the linking layer works
470+
// with callbacks from asynchronous queries.
471+
mutable std::recursive_mutex OutstandingMUsMutex;
472+
std::vector<std::pair<VSO *, std::unique_ptr<MaterializationUnit>>>
473+
OutstandingMUs;
403474
};
404475

405476
/// A symbol query that returns results via a callback when results are
@@ -411,21 +482,6 @@ class AsynchronousSymbolQuery {
411482
friend class VSO;
412483

413484
public:
414-
class ResolutionResult {
415-
public:
416-
ResolutionResult(SymbolMap Symbols, const SymbolDependenceMap &Dependencies)
417-
: Symbols(std::move(Symbols)), Dependencies(Dependencies) {}
418-
419-
SymbolMap Symbols;
420-
const SymbolDependenceMap &Dependencies;
421-
};
422-
423-
/// Callback to notify client that symbols have been resolved.
424-
using SymbolsResolvedCallback =
425-
std::function<void(Expected<ResolutionResult>)>;
426-
427-
/// Callback to notify client that symbols are ready for execution.
428-
using SymbolsReadyCallback = std::function<void(Error)>;
429485

430486
/// Create a query for the given symbols, notify-resolved and
431487
/// notify-ready callbacks.
@@ -485,6 +541,7 @@ class AsynchronousSymbolQuery {
485541
class VSO {
486542
friend class AsynchronousSymbolQuery;
487543
friend class ExecutionSession;
544+
friend class ExecutionSessionBase;
488545
friend class MaterializationResponsibility;
489546
public:
490547
using FallbackDefinitionGeneratorFunction =
@@ -493,9 +550,6 @@ class VSO {
493550
using AsynchronousSymbolQuerySet =
494551
std::set<std::shared_ptr<AsynchronousSymbolQuery>>;
495552

496-
using MaterializationUnitList =
497-
std::vector<std::unique_ptr<MaterializationUnit>>;
498-
499553
VSO(const VSO &) = delete;
500554
VSO &operator=(const VSO &) = delete;
501555
VSO(VSO &&) = delete;
@@ -547,8 +601,10 @@ class VSO {
547601
void removeFromSearchOrder(VSO &V);
548602

549603
/// Do something with the search order (run under the session lock).
550-
template <typename Func> void withSearchOrderDo(Func F) {
551-
ES.runSessionLocked([&]() { F(SearchOrder); });
604+
template <typename Func>
605+
auto withSearchOrderDo(Func &&F)
606+
-> decltype(F(std::declval<const VSOList &>())) {
607+
return ES.runSessionLocked([&]() { return F(SearchOrder); });
552608
}
553609

554610
/// Define all symbols provided by the materialization unit to be part
@@ -579,18 +635,19 @@ class VSO {
579635
/// the flags for each symbol in Flags. Returns any unresolved symbols.
580636
SymbolFlagsMap lookupFlags(const SymbolNameSet &Names);
581637

638+
/// Dump current VSO state to OS.
639+
void dump(raw_ostream &OS);
640+
641+
/// FIXME: Remove this when we remove the old ORC layers.
582642
/// Search the given VSOs in order for the symbols in Symbols. Results
583643
/// (once they become available) will be returned via the given Query.
584644
///
585645
/// If any symbol is not found then the unresolved symbols will be returned,
586646
/// and the query will not be applied. The Query is not failed and can be
587647
/// re-used in a subsequent lookup once the symbols have been added, or
588648
/// manually failed.
589-
SymbolNameSet lookup(std::shared_ptr<AsynchronousSymbolQuery> Q,
590-
SymbolNameSet Names);
591-
592-
/// Dump current VSO state to OS.
593-
void dump(raw_ostream &OS);
649+
SymbolNameSet legacyLookup(std::shared_ptr<AsynchronousSymbolQuery> Q,
650+
SymbolNameSet Names);
594651

595652
private:
596653
using AsynchronousSymbolQueryList =
@@ -629,6 +686,12 @@ class VSO {
629686
SymbolNameSet lookupFlagsImpl(SymbolFlagsMap &Flags,
630687
const SymbolNameSet &Names);
631688

689+
void lodgeQuery(std::shared_ptr<AsynchronousSymbolQuery> &Q,
690+
SymbolNameSet &Unresolved, MaterializationUnitList &MUs);
691+
692+
void lodgeQueryImpl(std::shared_ptr<AsynchronousSymbolQuery> &Q,
693+
SymbolNameSet &Unresolved, MaterializationUnitList &MUs);
694+
632695
LookupImplActionFlags
633696
lookupImpl(std::shared_ptr<AsynchronousSymbolQuery> &Q,
634697
std::vector<std::unique_ptr<MaterializationUnit>> &MUs,
@@ -647,29 +710,22 @@ class VSO {
647710

648711
SymbolNameSet getRequestedSymbols(const SymbolFlagsMap &SymbolFlags);
649712

650-
void addDependencies(const SymbolFlagsMap &Dependents,
651-
const SymbolDependenceMap &Dependencies);
713+
void addDependencies(const SymbolStringPtr &Name,
714+
const SymbolDependenceMap &Dependants);
652715

653716
void resolve(const SymbolMap &Resolved);
654717

655718
void finalize(const SymbolFlagsMap &Finalized);
656719

657720
void notifyFailed(const SymbolNameSet &FailedSymbols);
658721

659-
void runOutstandingMUs();
660-
661722
ExecutionSessionBase &ES;
662723
std::string VSOName;
663724
SymbolMap Symbols;
664725
UnmaterializedInfosMap UnmaterializedInfos;
665726
MaterializingInfosMap MaterializingInfos;
666727
FallbackDefinitionGeneratorFunction FallbackDefinitionGenerator;
667728
VSOList SearchOrder;
668-
669-
// FIXME: Remove this (and runOutstandingMUs) once the linking layer works
670-
// with callbacks from asynchronous queries.
671-
mutable std::recursive_mutex OutstandingMUsMutex;
672-
std::vector<std::unique_ptr<MaterializationUnit>> OutstandingMUs;
673729
};
674730

675731
/// An ExecutionSession represents a running JIT program.
@@ -693,15 +749,6 @@ class ExecutionSession : public ExecutionSessionBase {
693749
std::vector<std::unique_ptr<VSO>> VSOs;
694750
};
695751

696-
using AsynchronousLookupFunction = std::function<SymbolNameSet(
697-
std::shared_ptr<AsynchronousSymbolQuery> Q, SymbolNameSet Names)>;
698-
699-
/// Perform a blocking lookup on the given symbols.
700-
Expected<SymbolMap> blockingLookup(ExecutionSessionBase &ES,
701-
AsynchronousLookupFunction AsyncLookup,
702-
SymbolNameSet Names, bool WaiUntilReady,
703-
MaterializationResponsibility *MR = nullptr);
704-
705752
/// Look up the given names in the given VSOs.
706753
/// VSOs will be searched in order and no VSO pointer may be null.
707754
/// All symbols must be found within the given VSOs or an error

include/llvm/ExecutionEngine/Orc/Legacy.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -146,11 +146,11 @@ lookupWithLegacyFn(ExecutionSession &ES, AsynchronousSymbolQuery &Query,
146146
Query.notifySymbolReady();
147147
NewSymbolsResolved = true;
148148
} else {
149-
ES.failQuery(Query, Addr.takeError());
149+
ES.legacyFailQuery(Query, Addr.takeError());
150150
return SymbolNameSet();
151151
}
152152
} else if (auto Err = Sym.takeError()) {
153-
ES.failQuery(Query, std::move(Err));
153+
ES.legacyFailQuery(Query, std::move(Err));
154154
return SymbolNameSet();
155155
} else
156156
SymbolsNotFound.insert(S);

0 commit comments

Comments
 (0)