12#include "llvm/Config/llvm-config.h"
18#include <condition_variable>
22#define DEBUG_TYPE "orc"
40void MaterializationUnit::anchor() {}
43 assert((
reinterpret_cast<uintptr_t
>(JD.get()) & 0x1) == 0 &&
44 "JITDylib must be two byte aligned");
46 JDAndFlag.store(
reinterpret_cast<uintptr_t
>(JD.get()));
62void ResourceTracker::makeDefunct() {
63 uintptr_t Val = JDAndFlag.load();
78 OS <<
"Resource tracker " << (
void *)RT.
get() <<
" became defunct";
82 std::shared_ptr<SymbolStringPool> SSP,
83 std::shared_ptr<SymbolDependenceMap> Symbols)
85 assert(this->SSP &&
"String pool cannot be null");
86 assert(!this->Symbols->empty() &&
"Can not fail to resolve an empty set");
90 for (
auto &[JD, Syms] : *this->Symbols)
95 for (
auto &[JD, Syms] : *Symbols)
104 OS <<
"Failed to materialize symbols: " << *Symbols;
108 std::shared_ptr<SymbolStringPool> SSP,
JITDylibSP JD,
110 std::string Explanation)
112 FailedSymbols(
std::
move(FailedSymbols)), BadDeps(
std::
move(BadDeps)),
113 Explanation(
std::
move(Explanation)) {}
120 OS <<
"In " << JD->getName() <<
", failed to materialize " << FailedSymbols
121 <<
", due to unsatisfied dependencies " << BadDeps;
122 if (!Explanation.empty())
123 OS <<
" (" << Explanation <<
")";
129 for (
auto &
Sym : Symbols)
130 this->Symbols.push_back(
Sym);
131 assert(!this->Symbols.
empty() &&
"Can not fail to resolve an empty set");
137 assert(!this->Symbols.empty() &&
"Can not fail to resolve an empty set");
145 OS <<
"Symbols not found: " << Symbols;
149 std::shared_ptr<SymbolStringPool> SSP,
SymbolNameSet Symbols)
151 assert(!this->Symbols.
empty() &&
"Can not fail to resolve an empty set");
159 OS <<
"Symbols could not be removed: " << Symbols;
183 : NotifyComplete(
std::
move(NotifyComplete)), RequiredState(RequiredState) {
185 "Cannot query for a symbols that have not reached the resolve state "
188 OutstandingSymbolsCount = Symbols.size();
190 for (
auto &[
Name, Flags] : Symbols)
198 "Resolving symbol outside the requested set");
200 "Redundantly resolving symbol Name");
204 if (
Sym.getFlags().hasMaterializationSideEffectsOnly())
207 I->second = std::move(
Sym);
208 --OutstandingSymbolsCount;
212 assert(OutstandingSymbolsCount == 0 &&
213 "Symbols remain, handleComplete called prematurely");
215 class RunQueryCompleteTask :
public Task {
217 RunQueryCompleteTask(
SymbolMap ResolvedSymbols,
219 : ResolvedSymbols(
std::
move(ResolvedSymbols)),
220 NotifyComplete(
std::
move(NotifyComplete)) {}
222 OS <<
"Execute query complete callback for " << ResolvedSymbols;
224 void run()
override { NotifyComplete(std::move(ResolvedSymbols)); }
231 auto T = std::make_unique<RunQueryCompleteTask>(std::move(ResolvedSymbols),
232 std::move(NotifyComplete));
237void AsynchronousSymbolQuery::handleFailed(Error Err) {
239 OutstandingSymbolsCount == 0 &&
240 "Query should already have been abandoned");
241 NotifyComplete(std::move(Err));
245void AsynchronousSymbolQuery::addQueryDependence(JITDylib &JD,
246 SymbolStringPtr
Name) {
249 assert(Added &&
"Duplicate dependence notification?");
252void AsynchronousSymbolQuery::removeQueryDependence(
253 JITDylib &JD,
const SymbolStringPtr &
Name) {
254 auto QRI = QueryRegistrations.
find(&JD);
255 assert(QRI != QueryRegistrations.
end() &&
256 "No dependencies registered for JD");
257 assert(QRI->second.count(
Name) &&
"No dependency on Name in JD");
258 QRI->second.erase(
Name);
259 if (QRI->second.empty())
260 QueryRegistrations.
erase(QRI);
263void AsynchronousSymbolQuery::dropSymbol(
const SymbolStringPtr &
Name) {
266 "Redundant removal of weakly-referenced symbol");
268 --OutstandingSymbolsCount;
271void AsynchronousSymbolQuery::detach() {
272 ResolvedSymbols.
clear();
273 OutstandingSymbolsCount = 0;
274 for (
auto &[JD, Syms] : QueryRegistrations)
275 JD->detachQueryHelper(*
this, Syms);
276 QueryRegistrations.clear();
284 return "<Absolute Symbols>";
287void AbsoluteSymbolsMaterializationUnit::materialize(
288 std::unique_ptr<MaterializationResponsibility> R) {
294 if (
auto Err = R->notifyResolved(Symbols)) {
295 R->getExecutionSession().reportError(std::move(Err));
296 R->failMaterialization();
299 if (
auto Err = R->notifyEmitted({})) {
300 R->getExecutionSession().reportError(std::move(Err));
301 R->failMaterialization();
306void AbsoluteSymbolsMaterializationUnit::discard(
const JITDylib &JD,
307 const SymbolStringPtr &
Name) {
312MaterializationUnit::Interface
313AbsoluteSymbolsMaterializationUnit::extractFlags(
const SymbolMap &Symbols) {
315 for (
const auto &[
Name, Def] : Symbols)
317 return MaterializationUnit::Interface(std::move(Flags),
nullptr);
324 SourceJDLookupFlags(SourceJDLookupFlags), Aliases(
std::
move(Aliases)) {}
327 return "<Reexports>";
330void ReExportsMaterializationUnit::materialize(
331 std::unique_ptr<MaterializationResponsibility> R) {
333 auto &ES = R->getTargetJITDylib().getExecutionSession();
334 JITDylib &TgtJD = R->getTargetJITDylib();
335 JITDylib &SrcJD = SourceJD ? *SourceJD : TgtJD;
340 auto RequestedSymbols = R->getRequestedSymbols();
343 for (
auto &
Name : RequestedSymbols) {
345 assert(
I != Aliases.
end() &&
"Symbol not found in aliases map?");
346 RequestedAliases[
Name] = std::move(
I->second);
352 dbgs() <<
"materializing reexports: target = " << TgtJD.
getName()
353 <<
", source = " << SrcJD.
getName() <<
" " << RequestedAliases
358 if (!Aliases.
empty()) {
359 auto Err = SourceJD ?
R->replace(
reexports(*SourceJD, std::move(Aliases),
360 SourceJDLookupFlags))
367 R->failMaterialization();
374 struct OnResolveInfo {
375 OnResolveInfo(std::unique_ptr<MaterializationResponsibility> R,
376 SymbolAliasMap Aliases)
379 std::unique_ptr<MaterializationResponsibility>
R;
381 std::vector<SymbolDependenceGroup> SDGs;
392 std::vector<std::pair<SymbolLookupSet, std::shared_ptr<OnResolveInfo>>>
394 while (!RequestedAliases.
empty()) {
396 SymbolLookupSet QuerySymbols;
400 for (
auto &KV : RequestedAliases) {
402 if (&SrcJD == &TgtJD && (QueryAliases.count(KV.second.Aliasee) ||
403 RequestedAliases.count(KV.second.Aliasee)))
406 ResponsibilitySymbols.
insert(KV.first);
407 QuerySymbols.add(KV.second.Aliasee,
408 KV.second.AliasFlags.hasMaterializationSideEffectsOnly()
411 QueryAliases[KV.first] = std::move(KV.second);
415 for (
auto &KV : QueryAliases)
416 RequestedAliases.erase(KV.first);
418 assert(!QuerySymbols.empty() &&
"Alias cycle detected!");
420 auto NewR =
R->delegate(ResponsibilitySymbols);
423 R->failMaterialization();
427 auto QueryInfo = std::make_shared<OnResolveInfo>(std::move(*NewR),
428 std::move(QueryAliases));
429 QueryInfos.push_back(
430 make_pair(std::move(QuerySymbols), std::move(QueryInfo)));
434 while (!QueryInfos.empty()) {
435 auto QuerySymbols = std::move(QueryInfos.back().first);
436 auto QueryInfo = std::move(QueryInfos.back().second);
438 QueryInfos.pop_back();
440 auto RegisterDependencies = [QueryInfo,
447 assert(Deps.size() == 1 && Deps.count(&SrcJD) &&
448 "Unexpected dependencies for reexports");
450 auto &SrcJDDeps = Deps.find(&SrcJD)->second;
452 for (
auto &[Alias, AliasInfo] : QueryInfo->Aliases)
453 if (SrcJDDeps.count(AliasInfo.Aliasee))
454 QueryInfo->SDGs.push_back({{Alias}, {{&SrcJD, {AliasInfo.Aliasee}}}});
457 auto OnComplete = [QueryInfo](Expected<SymbolMap>
Result) {
458 auto &ES = QueryInfo->R->getTargetJITDylib().getExecutionSession();
461 for (
auto &KV : QueryInfo->Aliases) {
462 assert((KV.second.AliasFlags.hasMaterializationSideEffectsOnly() ||
463 Result->count(KV.second.Aliasee)) &&
464 "Result map missing entry?");
466 if (KV.second.AliasFlags.hasMaterializationSideEffectsOnly())
469 ResolutionMap[KV.first] = {(*Result)[KV.second.Aliasee].getAddress(),
470 KV.second.AliasFlags};
472 if (
auto Err = QueryInfo->R->notifyResolved(ResolutionMap)) {
474 QueryInfo->R->failMaterialization();
477 if (
auto Err = QueryInfo->R->notifyEmitted(QueryInfo->SDGs)) {
479 QueryInfo->R->failMaterialization();
484 QueryInfo->R->failMaterialization();
491 std::move(RegisterDependencies));
495void ReExportsMaterializationUnit::discard(
const JITDylib &JD,
496 const SymbolStringPtr &
Name) {
498 "Symbol not covered by this MaterializationUnit");
502MaterializationUnit::Interface
503ReExportsMaterializationUnit::extractFlags(
const SymbolAliasMap &Aliases) {
505 for (
auto &KV : Aliases)
508 return MaterializationUnit::Interface(std::move(
SymbolFlags),
nullptr);
519 return Flags.takeError();
522 for (
auto &
Name : Symbols) {
523 assert(Flags->count(
Name) &&
"Missing entry in flags map");
542 virtual void complete(std::unique_ptr<InProgressLookupState> IPLS) = 0;
570 OnComplete(
std::
move(OnComplete)) {}
572 void complete(std::unique_ptr<InProgressLookupState> IPLS)
override {
573 auto &ES =
SearchOrder.front().first->getExecutionSession();
574 ES.OL_completeLookupFlags(std::move(IPLS), std::move(OnComplete));
577 void fail(
Error Err)
override { OnComplete(std::move(Err)); }
588 std::shared_ptr<AsynchronousSymbolQuery> Q,
592 Q(
std::
move(Q)), RegisterDependencies(
std::
move(RegisterDependencies)) {
595 void complete(std::unique_ptr<InProgressLookupState> IPLS)
override {
596 auto &ES =
SearchOrder.front().first->getExecutionSession();
597 ES.OL_completeLookup(std::move(IPLS), std::move(Q),
598 std::move(RegisterDependencies));
603 Q->handleFailed(std::move(Err));
607 std::shared_ptr<AsynchronousSymbolQuery> Q;
614 : SourceJD(SourceJD), SourceJDLookupFlags(SourceJDLookupFlags),
621 assert(&JD != &SourceJD &&
"Cannot re-export from the same dylib");
625 K, {{&SourceJD, JDLookupFlags}}, LookupSet);
627 return Flags.takeError();
631 for (
auto &KV : *Flags)
632 if (!Allow || Allow(KV.first))
635 if (AliasMap.
empty())
645void LookupState::reset(InProgressLookupState *IPLS) { this->IPLS.reset(IPLS); }
653 assert(IPLS &&
"Cannot call continueLookup on empty LookupState");
654 auto &ES = IPLS->SearchOrder.begin()->first->getExecutionSession();
655 ES.OL_applyQueryPhase1(std::move(IPLS), std::move(Err));
659 std::deque<LookupState> LookupsToFail;
661 std::lock_guard<std::mutex> Lock(M);
662 std::swap(PendingLookups, LookupsToFail);
666 for (
auto &LS : LookupsToFail)
667 LS.continueLookup(make_error<StringError>(
668 "Query waiting on DefinitionGenerator that was destroyed",
677 std::vector<ResourceTrackerSP> TrackersToRemove;
679 assert(State != Closed &&
"JD is defunct");
680 for (
auto &KV : TrackerSymbols)
681 TrackersToRemove.push_back(KV.first);
686 for (
auto &RT : TrackersToRemove)
687 Err =
joinErrors(std::move(Err), RT->remove());
693 assert(State != Closed &&
"JD is defunct");
696 return DefaultTracker;
702 assert(State == Open &&
"JD is defunct");
711 std::shared_ptr<DefinitionGenerator> TmpDG;
714 assert(State == Open &&
"JD is defunct");
716 [&](
const std::shared_ptr<DefinitionGenerator> &
H) {
717 return H.get() == &
G;
719 assert(
I != DefGenerators.end() &&
"Generator not found");
720 TmpDG = std::move(*
I);
721 DefGenerators.erase(
I);
730 if (FromMR.RT->isDefunct())
731 return make_error<ResourceTrackerDefunct>(FromMR.RT);
733 std::vector<NonOwningSymbolStringPtr> AddedSyms;
734 std::vector<NonOwningSymbolStringPtr> RejectedWeakDefs;
736 for (
auto SFItr = SymbolFlags.begin(), SFEnd = SymbolFlags.end();
737 SFItr != SFEnd; ++SFItr) {
739 auto &Name = SFItr->first;
740 auto &Flags = SFItr->second;
742 auto EntryItr = Symbols.find(Name);
745 if (EntryItr != Symbols.end()) {
748 if (!Flags.isWeak()) {
750 for (auto &S : AddedSyms)
751 Symbols.erase(Symbols.find_as(S));
754 return make_error<DuplicateDefinition>(std::string(*Name));
758 RejectedWeakDefs.push_back(NonOwningSymbolStringPtr(Name));
769 while (!RejectedWeakDefs.empty()) {
770 SymbolFlags.erase(SymbolFlags.find_as(RejectedWeakDefs.back()));
771 RejectedWeakDefs.pop_back();
778Error JITDylib::replace(MaterializationResponsibility &FromMR,
779 std::unique_ptr<MaterializationUnit> MU) {
780 assert(MU !=
nullptr &&
"Can not replace with a null MaterializationUnit");
781 std::unique_ptr<MaterializationUnit> MustRunMU;
782 std::unique_ptr<MaterializationResponsibility> MustRunMR;
786 if (FromMR.RT->isDefunct())
787 return make_error<ResourceTrackerDefunct>(std::move(FromMR.RT));
790 for (
auto &KV : MU->getSymbols()) {
791 auto SymI = Symbols.find(KV.first);
792 assert(SymI != Symbols.end() &&
"Replacing unknown symbol");
793 assert(SymI->second.getState() == SymbolState::Materializing &&
794 "Can not replace a symbol that ha is not materializing");
795 assert(!SymI->second.hasMaterializerAttached() &&
796 "Symbol should not have materializer attached already");
797 assert(UnmaterializedInfos.count(KV.first) == 0 &&
798 "Symbol being replaced should have no UnmaterializedInfo");
806 for (
auto &KV : MU->getSymbols()) {
807 auto MII = MaterializingInfos.find(KV.first);
808 if (MII != MaterializingInfos.end()) {
809 if (MII->second.hasQueriesPending()) {
810 MustRunMR = ES.createMaterializationResponsibility(
811 *FromMR.RT, std::move(MU->SymbolFlags),
812 std::move(MU->InitSymbol));
813 MustRunMU = std::move(MU);
814 return Error::success();
820 auto UMI = std::make_shared<UnmaterializedInfo>(std::move(MU),
822 for (
auto &KV : UMI->MU->getSymbols()) {
823 auto SymI = Symbols.find(KV.first);
824 assert(SymI->second.getState() == SymbolState::Materializing &&
825 "Can not replace a symbol that is not materializing");
826 assert(!SymI->second.hasMaterializerAttached() &&
827 "Can not replace a symbol that has a materializer attached");
828 assert(UnmaterializedInfos.count(KV.first) == 0 &&
829 "Unexpected materializer entry in map");
830 SymI->second.setAddress(SymI->second.getAddress());
831 SymI->second.setMaterializerAttached(
true);
833 auto &UMIEntry = UnmaterializedInfos[KV.first];
834 assert((!UMIEntry || !UMIEntry->MU) &&
835 "Replacing symbol with materializer still attached");
846 assert(MustRunMR &&
"MustRunMU set implies MustRunMR set");
848 std::move(MustRunMU), std::move(MustRunMR)));
850 assert(!MustRunMR &&
"MustRunMU unset implies MustRunMR unset");
856Expected<std::unique_ptr<MaterializationResponsibility>>
857JITDylib::delegate(MaterializationResponsibility &FromMR,
858 SymbolFlagsMap SymbolFlags, SymbolStringPtr InitSymbol) {
861 [&]() -> Expected<std::unique_ptr<MaterializationResponsibility>> {
862 if (FromMR.RT->isDefunct())
863 return make_error<ResourceTrackerDefunct>(std::move(FromMR.RT));
865 return ES.createMaterializationResponsibility(
866 *FromMR.RT, std::move(SymbolFlags), std::move(InitSymbol));
871JITDylib::getRequestedSymbols(
const SymbolFlagsMap &SymbolFlags)
const {
875 for (
auto &KV : SymbolFlags) {
876 assert(Symbols.count(KV.first) &&
"JITDylib does not cover this symbol?");
877 assert(Symbols.find(KV.first)->second.getState() !=
878 SymbolState::NeverSearched &&
879 Symbols.find(KV.first)->second.getState() != SymbolState::Ready &&
880 "getRequestedSymbols can only be called for symbols that have "
881 "started materializing");
882 auto I = MaterializingInfos.find(KV.first);
883 if (
I == MaterializingInfos.end())
886 if (
I->second.hasQueriesPending())
887 RequestedSymbols.insert(KV.first);
890 return RequestedSymbols;
894Error JITDylib::resolve(MaterializationResponsibility &MR,
895 const SymbolMap &Resolved) {
896 AsynchronousSymbolQuerySet CompletedQueries;
899 if (MR.RT->isDefunct())
900 return make_error<ResourceTrackerDefunct>(MR.RT);
903 return make_error<StringError>(
"JITDylib " + getName() +
905 inconvertibleErrorCode());
907 struct WorklistEntry {
908 SymbolTable::iterator SymI;
909 ExecutorSymbolDef ResolvedSym;
912 SymbolNameSet SymbolsInErrorState;
913 std::vector<WorklistEntry> Worklist;
917 for (
const auto &KV : Resolved) {
919 assert(!KV.second.getFlags().hasError() &&
920 "Resolution result can not have error flag set");
922 auto SymI = Symbols.find(KV.first);
924 assert(SymI != Symbols.end() &&
"Symbol not found");
925 assert(!SymI->second.hasMaterializerAttached() &&
926 "Resolving symbol with materializer attached?");
927 assert(SymI->second.getState() == SymbolState::Materializing &&
928 "Symbol should be materializing");
929 assert(SymI->second.getAddress() == ExecutorAddr() &&
930 "Symbol has already been resolved");
932 if (SymI->second.getFlags().hasError())
933 SymbolsInErrorState.insert(KV.first);
935 auto Flags = KV.second.getFlags();
936 Flags &= ~JITSymbolFlags::Common;
938 (SymI->second.getFlags() & ~JITSymbolFlags::Common) &&
939 "Resolved flags should match the declared flags");
941 Worklist.push_back({SymI, {KV.second.getAddress(), Flags}});
946 if (!SymbolsInErrorState.empty()) {
947 auto FailedSymbolsDepMap = std::make_shared<SymbolDependenceMap>();
948 (*FailedSymbolsDepMap)[
this] = std::move(SymbolsInErrorState);
949 return make_error<FailedToMaterialize>(
950 getExecutionSession().getSymbolStringPool(),
951 std::move(FailedSymbolsDepMap));
954 while (!Worklist.empty()) {
955 auto SymI = Worklist.back().SymI;
956 auto ResolvedSym = Worklist.back().ResolvedSym;
959 auto &
Name = SymI->first;
962 JITSymbolFlags ResolvedFlags = ResolvedSym.getFlags();
963 SymI->second.setAddress(ResolvedSym.getAddress());
964 SymI->second.setFlags(ResolvedFlags);
965 SymI->second.setState(SymbolState::Resolved);
967 auto MII = MaterializingInfos.find(
Name);
968 if (MII == MaterializingInfos.end())
971 auto &
MI = MII->second;
972 for (
auto &Q :
MI.takeQueriesMeeting(SymbolState::Resolved)) {
973 Q->notifySymbolMetRequiredState(
Name, ResolvedSym);
974 Q->removeQueryDependence(*
this,
Name);
976 CompletedQueries.insert(std::move(Q));
980 return Error::success();
985 for (
auto &Q : CompletedQueries) {
986 assert(Q->isComplete() &&
"Q not completed");
987 Q->handleComplete(ES);
990 return Error::success();
993void JITDylib::unlinkMaterializationResponsibility(
994 MaterializationResponsibility &MR) {
996 auto I = TrackerMRs.find(MR.RT.get());
997 assert(
I != TrackerMRs.end() &&
"No MRs in TrackerMRs list for RT");
998 assert(
I->second.count(&MR) &&
"MR not in TrackerMRs list for RT");
999 I->second.erase(&MR);
1000 if (
I->second.empty())
1001 TrackerMRs.erase(MR.RT.get());
1005void JITDylib::shrinkMaterializationInfoMemory() {
1009 if (UnmaterializedInfos.empty())
1010 UnmaterializedInfos.clear();
1012 if (MaterializingInfos.empty())
1013 MaterializingInfos.clear();
1017 bool LinkAgainstThisJITDylibFirst) {
1019 assert(State == Open &&
"JD is defunct");
1020 if (LinkAgainstThisJITDylibFirst) {
1022 if (NewLinkOrder.empty() || NewLinkOrder.front().first !=
this)
1023 LinkOrder.push_back(
1024 std::make_pair(
this, JITDylibLookupFlags::MatchAllSymbols));
1027 LinkOrder = std::move(NewLinkOrder);
1033 for (
auto &KV : NewLinks) {
1038 LinkOrder.push_back(std::move(KV));
1050 assert(State == Open &&
"JD is defunct");
1051 for (
auto &KV : LinkOrder)
1052 if (KV.first == &OldJD) {
1053 KV = {&NewJD, JDLookupFlags};
1061 assert(State == Open &&
"JD is defunct");
1063 [&](
const JITDylibSearchOrder::value_type &KV) {
1064 return KV.first == &JD;
1066 if (
I != LinkOrder.end())
1073 assert(State == Open &&
"JD is defunct");
1074 using SymbolMaterializerItrPair =
1075 std::pair<SymbolTable::iterator, UnmaterializedInfosMap::iterator>;
1076 std::vector<SymbolMaterializerItrPair> SymbolsToRemove;
1080 for (
auto &
Name : Names) {
1081 auto I = Symbols.find(
Name);
1084 if (
I == Symbols.end()) {
1085 Missing.insert(
Name);
1090 if (
I->second.getState() != SymbolState::NeverSearched &&
1091 I->second.getState() != SymbolState::Ready) {
1096 auto UMII =
I->second.hasMaterializerAttached()
1097 ? UnmaterializedInfos.find(
Name)
1098 : UnmaterializedInfos.end();
1099 SymbolsToRemove.push_back(std::make_pair(
I, UMII));
1103 if (!Missing.empty())
1105 std::move(Missing));
1113 for (
auto &SymbolMaterializerItrPair : SymbolsToRemove) {
1114 auto UMII = SymbolMaterializerItrPair.second;
1117 if (UMII != UnmaterializedInfos.end()) {
1118 UMII->second->MU->doDiscard(*
this, UMII->first);
1119 UnmaterializedInfos.erase(UMII);
1122 auto SymI = SymbolMaterializerItrPair.first;
1123 Symbols.erase(SymI);
1126 shrinkMaterializationInfoMemory();
1128 return Error::success();
1134 OS <<
"JITDylib \"" <<
getName() <<
"\" (ES: "
1135 <<
format(
"0x%016" PRIx64,
reinterpret_cast<uintptr_t
>(&ES))
1149 if (State == Closed)
1151 OS <<
"Link order: " << LinkOrder <<
"\n"
1152 <<
"Symbol table:\n";
1155 std::vector<std::pair<SymbolStringPtr, SymbolTableEntry *>> SymbolsSorted;
1156 for (
auto &KV : Symbols)
1157 SymbolsSorted.emplace_back(KV.first, &KV.second);
1158 std::sort(SymbolsSorted.begin(), SymbolsSorted.end(),
1159 [](
const auto &L,
const auto &R) { return *L.first < *R.first; });
1161 for (
auto &KV : SymbolsSorted) {
1162 OS <<
" \"" << *KV.first <<
"\": ";
1163 if (
auto Addr = KV.second->getAddress())
1166 OS <<
"<not resolved> ";
1168 OS <<
" " << KV.second->getFlags() <<
" " << KV.second->getState();
1170 if (KV.second->hasMaterializerAttached()) {
1171 OS <<
" (Materializer ";
1172 auto I = UnmaterializedInfos.find(KV.first);
1173 assert(
I != UnmaterializedInfos.end() &&
1174 "Lazy symbol should have UnmaterializedInfo");
1175 OS <<
I->second->MU.get() <<
", " <<
I->second->MU->getName() <<
")\n";
1180 if (!MaterializingInfos.empty())
1181 OS <<
" MaterializingInfos entries:\n";
1182 for (
auto &KV : MaterializingInfos) {
1183 OS <<
" \"" << *KV.first <<
"\":\n"
1184 <<
" " << KV.second.pendingQueries().size()
1185 <<
" pending queries: { ";
1186 for (
const auto &Q : KV.second.pendingQueries())
1187 OS << Q.get() <<
" (" << Q->getRequiredState() <<
") ";
1188 OS <<
"}\n Defining EDU: ";
1189 if (KV.second.DefiningEDU) {
1190 OS << KV.second.DefiningEDU.get() <<
" { ";
1191 for (
auto &[
Name, Flags] : KV.second.DefiningEDU->Symbols)
1194 OS <<
" Dependencies:\n";
1195 if (!KV.second.DefiningEDU->Dependencies.empty()) {
1196 for (
auto &[DepJD, Deps] : KV.second.DefiningEDU->Dependencies) {
1197 OS <<
" " << DepJD->getName() <<
": [ ";
1198 for (
auto &Dep : Deps)
1206 OS <<
" Dependant EDUs:\n";
1207 if (!KV.second.DependantEDUs.empty()) {
1208 for (
auto &DependantEDU : KV.second.DependantEDUs) {
1209 OS <<
" " << DependantEDU <<
": "
1210 << DependantEDU->JD->getName() <<
" { ";
1211 for (
auto &[
Name, Flags] : DependantEDU->Symbols)
1217 assert((Symbols[KV.first].getState() != SymbolState::Ready ||
1218 (KV.second.pendingQueries().empty() && !KV.second.DefiningEDU &&
1219 !KV.second.DependantEDUs.empty())) &&
1220 "Stale materializing info entry");
1225void JITDylib::MaterializingInfo::addQuery(
1226 std::shared_ptr<AsynchronousSymbolQuery> Q) {
1230 [](
const std::shared_ptr<AsynchronousSymbolQuery> &V,
SymbolState S) {
1231 return V->getRequiredState() <= S;
1233 PendingQueries.insert(
I.base(), std::move(Q));
1236void JITDylib::MaterializingInfo::removeQuery(
1237 const AsynchronousSymbolQuery &Q) {
1240 PendingQueries, [&Q](
const std::shared_ptr<AsynchronousSymbolQuery> &V) {
1241 return V.get() == &Q;
1243 assert(
I != PendingQueries.end() &&
1244 "Query is not attached to this MaterializingInfo");
1245 PendingQueries.erase(
I);
1248JITDylib::AsynchronousSymbolQueryList
1249JITDylib::MaterializingInfo::takeQueriesMeeting(SymbolState RequiredState) {
1250 AsynchronousSymbolQueryList
Result;
1251 while (!PendingQueries.empty()) {
1252 if (PendingQueries.back()->getRequiredState() > RequiredState)
1255 Result.push_back(std::move(PendingQueries.back()));
1256 PendingQueries.pop_back();
1262JITDylib::JITDylib(ExecutionSession &ES, std::string
Name)
1267std::pair<JITDylib::AsynchronousSymbolQuerySet,
1268 std::shared_ptr<SymbolDependenceMap>>
1269JITDylib::removeTracker(ResourceTracker &RT) {
1271 assert(State != Closed &&
"JD is defunct");
1276 if (&RT == DefaultTracker.
get()) {
1278 for (
auto &KV : TrackerSymbols)
1279 for (
auto &
Sym : KV.second)
1282 for (
auto &KV : Symbols) {
1283 auto &
Sym = KV.first;
1284 if (!TrackedSymbols.count(
Sym))
1285 SymbolsToRemove.push_back(
Sym);
1288 DefaultTracker.
reset();
1291 auto I = TrackerSymbols.find(&RT);
1292 if (
I != TrackerSymbols.end()) {
1293 SymbolsToRemove = std::move(
I->second);
1294 TrackerSymbols.erase(
I);
1299 for (
auto &
Sym : SymbolsToRemove) {
1300 assert(Symbols.count(
Sym) &&
"Symbol not in symbol table");
1303 auto MII = MaterializingInfos.
find(
Sym);
1304 if (MII != MaterializingInfos.
end())
1305 SymbolsToFail.push_back(
Sym);
1308 AsynchronousSymbolQuerySet QueriesToFail;
1309 auto Result = ES.runSessionLocked(
1310 [&]() {
return ES.IL_failSymbols(*
this, std::move(SymbolsToFail)); });
1313 for (
auto &
Sym : SymbolsToRemove) {
1314 auto I = Symbols.find(
Sym);
1315 assert(
I != Symbols.end() &&
"Symbol not present in table");
1318 if (
I->second.hasMaterializerAttached()) {
1323 "Symbol has materializer attached");
1329 shrinkMaterializationInfoMemory();
1334void JITDylib::transferTracker(ResourceTracker &DstRT, ResourceTracker &SrcRT) {
1335 assert(State != Closed &&
"JD is defunct");
1336 assert(&DstRT != &SrcRT &&
"No-op transfers shouldn't call transferTracker");
1337 assert(&DstRT.getJITDylib() ==
this &&
"DstRT is not for this JITDylib");
1338 assert(&SrcRT.getJITDylib() ==
this &&
"SrcRT is not for this JITDylib");
1341 for (
auto &KV : UnmaterializedInfos) {
1342 if (KV.second->RT == &SrcRT)
1343 KV.second->RT = &DstRT;
1348 auto I = TrackerMRs.find(&SrcRT);
1349 if (
I != TrackerMRs.end()) {
1350 auto &SrcMRs =
I->second;
1351 auto &DstMRs = TrackerMRs[&DstRT];
1352 for (
auto *MR : SrcMRs)
1355 DstMRs = std::move(SrcMRs);
1357 for (
auto *MR : SrcMRs)
1361 TrackerMRs.erase(&SrcRT);
1367 if (&DstRT == DefaultTracker.
get()) {
1368 TrackerSymbols.erase(&SrcRT);
1374 if (&SrcRT == DefaultTracker.
get()) {
1375 assert(!TrackerSymbols.count(&SrcRT) &&
1376 "Default tracker should not appear in TrackerSymbols");
1381 for (
auto &KV : TrackerSymbols)
1382 for (
auto &
Sym : KV.second)
1385 for (
auto &KV : Symbols) {
1386 auto &
Sym = KV.first;
1387 if (!CurrentlyTrackedSymbols.count(
Sym))
1388 SymbolsToTrack.push_back(
Sym);
1391 TrackerSymbols[&DstRT] = std::move(SymbolsToTrack);
1395 auto &DstTrackedSymbols = TrackerSymbols[&DstRT];
1399 auto SI = TrackerSymbols.find(&SrcRT);
1400 if (SI == TrackerSymbols.end())
1403 DstTrackedSymbols.reserve(DstTrackedSymbols.size() +
SI->second.size());
1404 for (
auto &
Sym :
SI->second)
1405 DstTrackedSymbols.push_back(std::move(
Sym));
1406 TrackerSymbols.erase(SI);
1409Error JITDylib::defineImpl(MaterializationUnit &MU) {
1413 std::vector<SymbolStringPtr> ExistingDefsOverridden;
1414 std::vector<SymbolStringPtr> MUDefsOverridden;
1416 for (
const auto &KV : MU.getSymbols()) {
1417 auto I = Symbols.find(KV.first);
1419 if (
I != Symbols.end()) {
1420 if (KV.second.isStrong()) {
1421 if (
I->second.getFlags().isStrong() ||
1423 Duplicates.insert(KV.first);
1426 "Overridden existing def should be in the never-searched "
1428 ExistingDefsOverridden.push_back(KV.first);
1431 MUDefsOverridden.push_back(KV.first);
1436 if (!Duplicates.empty()) {
1438 {
dbgs() <<
" Error: Duplicate symbols " << Duplicates <<
"\n"; });
1439 return make_error<DuplicateDefinition>(std::string(**Duplicates.begin()));
1444 if (!MUDefsOverridden.empty())
1445 dbgs() <<
" Defs in this MU overridden: " << MUDefsOverridden <<
"\n";
1447 for (
auto &S : MUDefsOverridden)
1448 MU.doDiscard(*
this, S);
1452 if (!ExistingDefsOverridden.empty())
1453 dbgs() <<
" Existing defs overridden by this MU: " << MUDefsOverridden
1456 for (
auto &S : ExistingDefsOverridden) {
1458 auto UMII = UnmaterializedInfos.find(S);
1459 assert(UMII != UnmaterializedInfos.end() &&
1460 "Overridden existing def should have an UnmaterializedInfo");
1461 UMII->second->MU->doDiscard(*
this, S);
1465 for (
auto &KV : MU.getSymbols()) {
1466 auto &SymEntry = Symbols[KV.first];
1467 SymEntry.setFlags(KV.second);
1469 SymEntry.setMaterializerAttached(
true);
1475void JITDylib::installMaterializationUnit(
1476 std::unique_ptr<MaterializationUnit> MU, ResourceTracker &RT) {
1479 if (&RT != DefaultTracker.
get()) {
1480 auto &TS = TrackerSymbols[&RT];
1481 TS.reserve(TS.size() + MU->getSymbols().size());
1482 for (
auto &KV : MU->getSymbols())
1483 TS.push_back(KV.first);
1486 auto UMI = std::make_shared<UnmaterializedInfo>(std::move(MU), &RT);
1487 for (
auto &KV : UMI->MU->getSymbols())
1488 UnmaterializedInfos[KV.first] = UMI;
1491void JITDylib::detachQueryHelper(AsynchronousSymbolQuery &Q,
1493 for (
auto &QuerySymbol : QuerySymbols) {
1495 "QuerySymbol does not have MaterializingInfo");
1496 auto &
MI = MaterializingInfos[QuerySymbol];
1509 std::mutex LookupMutex;
1510 std::condition_variable CV;
1514 dbgs() <<
"Issuing init-symbol lookup:\n";
1515 for (
auto &KV : InitSyms)
1516 dbgs() <<
" " << KV.first->getName() <<
": " << KV.second <<
"\n";
1519 for (
auto &KV : InitSyms) {
1520 auto *JD = KV.first;
1521 auto Names = std::move(KV.second);
1528 std::lock_guard<std::mutex> Lock(LookupMutex);
1532 "Duplicate JITDylib in lookup?");
1533 CompoundResult[JD] = std::move(*
Result);
1543 std::unique_lock<std::mutex> Lock(LookupMutex);
1544 CV.wait(Lock, [&] {
return Count == 0 || CompoundErr; });
1547 return std::move(CompoundErr);
1549 return std::move(CompoundResult);
1556 class TriggerOnComplete {
1559 TriggerOnComplete(OnCompleteFn OnComplete)
1560 : OnComplete(std::move(OnComplete)) {}
1561 ~TriggerOnComplete() { OnComplete(std::move(LookupResult)); }
1562 void reportResult(
Error Err) {
1563 std::lock_guard<std::mutex> Lock(ResultMutex);
1564 LookupResult =
joinErrors(std::move(LookupResult), std::move(Err));
1568 std::mutex ResultMutex;
1569 Error LookupResult{Error::success()};
1570 OnCompleteFn OnComplete;
1574 dbgs() <<
"Issuing init-symbol lookup:\n";
1575 for (
auto &KV : InitSyms)
1576 dbgs() <<
" " << KV.first->getName() <<
": " << KV.second <<
"\n";
1579 auto TOC = std::make_shared<TriggerOnComplete>(std::move(OnComplete));
1581 for (
auto &KV : InitSyms) {
1582 auto *JD = KV.first;
1583 auto Names = std::move(KV.second);
1589 TOC->reportResult(
Result.takeError());
1596 OS <<
"Materialization task: " << MU->getName() <<
" in "
1597 << MR->getTargetJITDylib().getName();
1609 this->EPC->ES =
this;
1615 "Session still open. Did you forget to call endSession?");
1619 LLVM_DEBUG(
dbgs() <<
"Ending ExecutionSession " <<
this <<
"\n");
1623#ifdef EXPENSIVE_CHECKS
1624 verifySessionState(
"Entering ExecutionSession::endSession");
1627 SessionOpen =
false;
1631 std::reverse(JDsToRemove.begin(), JDsToRemove.end());
1635 Err =
joinErrors(std::move(Err), EPC->disconnect());
1646 assert(!ResourceManagers.empty() &&
"No managers registered");
1647 if (ResourceManagers.back() == &RM)
1648 ResourceManagers.pop_back();
1651 assert(
I != ResourceManagers.end() &&
"RM not registered");
1652 ResourceManagers.erase(
I);
1659 for (
auto &JD : JDs)
1660 if (JD->getName() ==
Name)
1669 assert(SessionOpen &&
"Cannot create JITDylib after session is closed");
1678 if (
auto Err = P->setupJITDylib(JD))
1679 return std::move(Err);
1686 for (
auto &JD : JDsToRemove) {
1687 assert(JD->State == JITDylib::Open &&
"JD already closed");
1688 JD->State = JITDylib::Closing;
1690 assert(
I != JDs.end() &&
"JD does not appear in session JDs");
1697 for (
auto JD : JDsToRemove) {
1698 Err =
joinErrors(std::move(Err), JD->clear());
1700 Err =
joinErrors(std::move(Err), P->teardownJITDylib(*JD));
1705 for (
auto &JD : JDsToRemove) {
1706 assert(JD->State == JITDylib::Closing &&
"JD should be closing");
1707 JD->State = JITDylib::Closed;
1708 assert(JD->Symbols.empty() &&
"JD.Symbols is not empty after clear");
1709 assert(JD->UnmaterializedInfos.empty() &&
1710 "JD.UnmaterializedInfos is not empty after clear");
1711 assert(JD->MaterializingInfos.empty() &&
1712 "JD.MaterializingInfos is not empty after clear");
1713 assert(JD->TrackerSymbols.empty() &&
1714 "TrackerSymbols is not empty after clear");
1715 JD->DefGenerators.clear();
1716 JD->LinkOrder.clear();
1726 return std::vector<JITDylibSP>();
1728 auto &ES = JDs.
front()->getExecutionSession();
1729 return ES.runSessionLocked([&]() ->
Expected<std::vector<JITDylibSP>> {
1731 std::vector<JITDylibSP>
Result;
1733 for (
auto &JD : JDs) {
1735 if (JD->State != Open)
1736 return make_error<StringError>(
1737 "Error building link order: " + JD->getName() +
" is defunct",
1739 if (Visited.
count(JD.get()))
1744 Visited.
insert(JD.get());
1746 while (!WorkStack.empty()) {
1747 Result.push_back(std::move(WorkStack.back()));
1748 WorkStack.pop_back();
1750 for (auto &KV : llvm::reverse(Result.back()->LinkOrder)) {
1751 auto &JD = *KV.first;
1752 if (!Visited.insert(&JD).second)
1754 WorkStack.push_back(&JD);
1782 OL_applyQueryPhase1(std::make_unique<InProgressLookupFlagsState>(
1783 K, std::move(SearchOrder), std::move(LookupSet),
1784 std::move(OnComplete)),
1792 std::promise<MSVCPExpected<SymbolFlagsMap>> ResultP;
1793 OL_applyQueryPhase1(std::make_unique<InProgressLookupFlagsState>(
1794 K, std::move(SearchOrder), std::move(LookupSet),
1796 ResultP.set_value(std::move(
Result));
1800 auto ResultF = ResultP.get_future();
1801 return ResultF.get();
1812 dbgs() <<
"Looking up " << Symbols <<
" in " << SearchOrder
1813 <<
" (required state: " << RequiredState <<
")\n";
1820 dispatchOutstandingMUs();
1822 auto Unresolved = std::move(Symbols);
1823 auto Q = std::make_shared<AsynchronousSymbolQuery>(Unresolved, RequiredState,
1824 std::move(NotifyComplete));
1826 auto IPLS = std::make_unique<InProgressFullLookupState>(
1827 K, SearchOrder, std::move(Unresolved), RequiredState, std::move(Q),
1828 std::move(RegisterDependencies));
1838#if LLVM_ENABLE_THREADS
1840 std::promise<SymbolMap> PromisedResult;
1845 PromisedResult.set_value(std::move(*R));
1848 ResolutionError = R.takeError();
1862 ResolutionError = R.takeError();
1867 lookup(K, SearchOrder, std::move(Symbols), RequiredState, NotifyComplete,
1868 RegisterDependencies);
1870#if LLVM_ENABLE_THREADS
1871 auto ResultFuture = PromisedResult.get_future();
1872 auto Result = ResultFuture.get();
1874 if (ResolutionError)
1875 return std::move(ResolutionError);
1877 return std::move(
Result);
1880 if (ResolutionError)
1881 return std::move(ResolutionError);
1894 assert(ResultMap->size() == 1 &&
"Unexpected number of results");
1895 assert(ResultMap->count(
Name) &&
"Missing result for symbol");
1896 return std::move(ResultMap->begin()->second);
1898 return ResultMap.takeError();
1920 return TagAddrs.takeError();
1923 std::lock_guard<std::mutex> Lock(JITDispatchHandlersMutex);
1924 for (
auto &KV : *TagAddrs) {
1925 auto TagAddr = KV.second.getAddress();
1926 if (JITDispatchHandlers.count(TagAddr))
1927 return make_error<StringError>(
"Tag " +
formatv(
"{0:x16}", TagAddr) +
1928 " (for " + *KV.first +
1929 ") already registered",
1931 auto I = WFs.
find(KV.first);
1933 "JITDispatchHandler implementation missing");
1934 JITDispatchHandlers[KV.second.getAddress()] =
1935 std::make_shared<JITDispatchHandlerFunction>(std::move(
I->second));
1937 dbgs() <<
"Associated function tag \"" << *KV.first <<
"\" ("
1938 <<
formatv(
"{0:x}", KV.second.getAddress()) <<
") with handler\n";
1948 std::shared_ptr<JITDispatchHandlerFunction>
F;
1950 std::lock_guard<std::mutex> Lock(JITDispatchHandlersMutex);
1951 auto I = JITDispatchHandlers.find(HandlerFnTagAddr);
1952 if (
I != JITDispatchHandlers.end())
1957 (*F)(std::move(SendResult), ArgBuffer.
data(), ArgBuffer.
size());
1960 (
"No function registered for tag " +
1961 formatv(
"{0:x16}", HandlerFnTagAddr))
1967 for (
auto &JD : JDs)
1972#ifdef EXPENSIVE_CHECKS
1973bool ExecutionSession::verifySessionState(
Twine Phase) {
1980 for (
auto &JD : JDs) {
1983 auto &Stream =
errs();
1985 Stream <<
"ERROR: Bad ExecutionSession state detected " <<
Phase
1987 Stream <<
" In JITDylib " << JD->getName() <<
", ";
1992 if (JD->State != JITDylib::Open) {
1994 <<
"state is not Open, but JD is in ExecutionSession list.";
2002 for (
auto &[
Sym, Entry] : JD->Symbols) {
2005 if (Entry.getAddress()) {
2006 LogFailure() <<
"symbol " <<
Sym <<
" has state "
2008 <<
" (not-yet-resolved) but non-null address "
2009 << Entry.getAddress() <<
".\n";
2014 auto UMIItr = JD->UnmaterializedInfos.find(
Sym);
2015 if (Entry.hasMaterializerAttached()) {
2016 if (UMIItr == JD->UnmaterializedInfos.end()) {
2017 LogFailure() <<
"symbol " <<
Sym
2018 <<
" entry claims materializer attached, but "
2019 "UnmaterializedInfos has no corresponding entry.\n";
2021 }
else if (UMIItr != JD->UnmaterializedInfos.end()) {
2024 <<
" entry claims no materializer attached, but "
2025 "UnmaterializedInfos has an unexpected entry for it.\n";
2031 for (
auto &[
Sym, UMI] : JD->UnmaterializedInfos) {
2032 auto SymItr = JD->Symbols.find(
Sym);
2033 if (SymItr == JD->Symbols.end()) {
2036 <<
" has UnmaterializedInfos entry, but no Symbols entry.\n";
2041 for (
auto &[
Sym, MII] : JD->MaterializingInfos) {
2043 auto SymItr = JD->Symbols.find(
Sym);
2044 if (SymItr == JD->Symbols.end()) {
2049 <<
" has MaterializingInfos entry, but no Symbols entry.\n";
2057 <<
" is in Ready state, should not have MaterializingInfo.\n";
2062 static_cast<std::underlying_type_t<SymbolState>
>(
2063 SymItr->second.getState()) + 1);
2064 for (
auto &Q : MII.PendingQueries) {
2065 if (Q->getRequiredState() != CurState) {
2066 if (Q->getRequiredState() > CurState)
2067 CurState = Q->getRequiredState();
2069 LogFailure() <<
"symbol " <<
Sym
2070 <<
" has stale or misordered queries.\n";
2078 if (MII.DefiningEDU) {
2080 EDUsToCheck.
insert(MII.DefiningEDU.get());
2082 if (MII.DefiningEDU->JD != JD.get()) {
2083 LogFailure() <<
"symbol " <<
Sym
2084 <<
" has DefiningEDU with incorrect JD"
2086 ?
" (JD not currently in ExecutionSession"
2094 <<
" has DefiningEDU, but is not in Emitted state.\n";
2100 for (
auto &DepEDU : MII.DependantEDUs) {
2102 LogFailure() <<
"symbol " <<
Sym <<
" has DependantEDU "
2103 << (
void *)DepEDU <<
" with JD (" << DepEDU->JD
2104 <<
") that isn't in ExecutionSession.\n";
2112 for (
auto *EDU : EDUsToCheck) {
2113 assert(EDU->JD->State == JITDylib::Open &&
"EDU->JD is not Open");
2115 auto LogFailure = [&]() -> raw_fd_ostream & {
2117 auto &Stream =
errs();
2118 Stream <<
"In EDU defining " << EDU->JD->getName() <<
": { ";
2119 for (
auto &[
Sym, Flags] : EDU->Symbols)
2120 Stream <<
Sym <<
" ";
2125 if (EDU->Symbols.empty())
2126 LogFailure() <<
"no symbols defined.\n";
2128 for (
auto &[
Sym, Flags] : EDU->Symbols) {
2130 LogFailure() <<
"null symbol defined.\n";
2132 if (!EDU->JD->Symbols.count(SymbolStringPtr(
Sym))) {
2133 LogFailure() <<
"symbol " <<
Sym
2134 <<
" isn't present in JD's symbol table.\n";
2140 for (
auto &[DepJD, Symbols] : EDU->Dependencies) {
2142 LogFailure() <<
"dependant symbols listed for JD that isn't in "
2143 "ExecutionSession.\n";
2145 for (
auto &DepSym : Symbols) {
2146 if (!DepJD->Symbols.count(SymbolStringPtr(DepSym))) {
2148 <<
"dependant symbol " << DepSym
2149 <<
" does not appear in symbol table for dependant JD "
2150 << DepJD->getName() <<
".\n";
2162void ExecutionSession::dispatchOutstandingMUs() {
2165 std::optional<std::pair<std::unique_ptr<MaterializationUnit>,
2166 std::unique_ptr<MaterializationResponsibility>>>
2170 std::lock_guard<std::recursive_mutex> Lock(OutstandingMUsMutex);
2171 if (!OutstandingMUs.empty()) {
2172 JMU.emplace(std::move(OutstandingMUs.back()));
2173 OutstandingMUs.pop_back();
2180 assert(JMU->first &&
"No MU?");
2181 LLVM_DEBUG(
dbgs() <<
" Dispatching \"" << JMU->first->getName() <<
"\"\n");
2182 dispatchTask(std::make_unique<MaterializationTask>(std::move(JMU->first),
2183 std::move(JMU->second)));
2185 LLVM_DEBUG(
dbgs() <<
"Done dispatching MaterializationUnits.\n");
2188Error ExecutionSession::removeResourceTracker(ResourceTracker &RT) {
2190 dbgs() <<
"In " << RT.getJITDylib().getName() <<
" removing tracker "
2191 <<
formatv(
"{0:x}", RT.getKeyUnsafe()) <<
"\n";
2193 std::vector<ResourceManager *> CurrentResourceManagers;
2195 JITDylib::AsynchronousSymbolQuerySet QueriesToFail;
2196 std::shared_ptr<SymbolDependenceMap> FailedSymbols;
2199 CurrentResourceManagers = ResourceManagers;
2201 std::tie(QueriesToFail, FailedSymbols) = RT.getJITDylib().removeTracker(RT);
2206 auto &JD = RT.getJITDylib();
2207 for (
auto *L :
reverse(CurrentResourceManagers))
2209 L->handleRemoveResources(JD, RT.getKeyUnsafe()));
2211 for (
auto &Q : QueriesToFail)
2218void ExecutionSession::transferResourceTracker(ResourceTracker &DstRT,
2219 ResourceTracker &SrcRT) {
2221 dbgs() <<
"In " << SrcRT.getJITDylib().getName()
2222 <<
" transfering resources from tracker "
2223 <<
formatv(
"{0:x}", SrcRT.getKeyUnsafe()) <<
" to tracker "
2224 <<
formatv(
"{0:x}", DstRT.getKeyUnsafe()) <<
"\n";
2228 if (&DstRT == &SrcRT)
2231 assert(&DstRT.getJITDylib() == &SrcRT.getJITDylib() &&
2232 "Can't transfer resources between JITDylibs");
2234 SrcRT.makeDefunct();
2235 auto &JD = DstRT.getJITDylib();
2236 JD.transferTracker(DstRT, SrcRT);
2237 for (
auto *L :
reverse(ResourceManagers))
2238 L->handleTransferResources(JD, DstRT.getKeyUnsafe(),
2239 SrcRT.getKeyUnsafe());
2243void ExecutionSession::destroyResourceTracker(ResourceTracker &RT) {
2246 dbgs() <<
"In " << RT.getJITDylib().getName() <<
" destroying tracker "
2247 <<
formatv(
"{0:x}", RT.getKeyUnsafe()) <<
"\n";
2249 if (!RT.isDefunct())
2250 transferResourceTracker(*RT.getJITDylib().getDefaultResourceTracker(),
2255Error ExecutionSession::IL_updateCandidatesFor(
2257 SymbolLookupSet &Candidates, SymbolLookupSet *NonCandidates) {
2258 return Candidates.forEachWithRemoval(
2259 [&](
const SymbolStringPtr &
Name,
2263 auto SymI = JD.Symbols.find(
Name);
2264 if (SymI == JD.Symbols.end())
2272 if (!SymI->second.getFlags().isExported() &&
2275 NonCandidates->add(Name, SymLookupFlags);
2284 if (SymI->second.getFlags().hasMaterializationSideEffectsOnly() &&
2291 if (SymI->second.getFlags().hasError()) {
2292 auto FailedSymbolsMap = std::make_shared<SymbolDependenceMap>();
2293 (*FailedSymbolsMap)[&JD] = {Name};
2295 std::move(FailedSymbolsMap));
2303void ExecutionSession::OL_resumeLookupAfterGeneration(
2304 InProgressLookupState &IPLS) {
2307 "Should not be called for not-in-generator lookups");
2312 if (
auto DG = IPLS.CurDefGeneratorStack.back().lock()) {
2313 IPLS.CurDefGeneratorStack.pop_back();
2314 std::lock_guard<std::mutex> Lock(DG->M);
2318 if (DG->PendingLookups.empty()) {
2324 LS = std::move(DG->PendingLookups.front());
2325 DG->PendingLookups.pop_front();
2330 dispatchTask(std::make_unique<LookupTask>(std::move(LS)));
2334void ExecutionSession::OL_applyQueryPhase1(
2335 std::unique_ptr<InProgressLookupState> IPLS, Error Err) {
2338 dbgs() <<
"Entering OL_applyQueryPhase1:\n"
2339 <<
" Lookup kind: " << IPLS->K <<
"\n"
2340 <<
" Search order: " << IPLS->SearchOrder
2341 <<
", Current index = " << IPLS->CurSearchOrderIndex
2342 << (IPLS->NewJITDylib ?
" (entering new JITDylib)" :
"") <<
"\n"
2343 <<
" Lookup set: " << IPLS->LookupSet <<
"\n"
2344 <<
" Definition generator candidates: "
2345 << IPLS->DefGeneratorCandidates <<
"\n"
2346 <<
" Definition generator non-candidates: "
2347 << IPLS->DefGeneratorNonCandidates <<
"\n";
2351 OL_resumeLookupAfterGeneration(*IPLS);
2354 "Lookup should not be in InGenerator state here");
2361 while (IPLS->CurSearchOrderIndex != IPLS->SearchOrder.size()) {
2367 return IPLS->fail(std::move(Err));
2370 auto &KV = IPLS->SearchOrder[IPLS->CurSearchOrderIndex];
2371 auto &JD = *KV.first;
2372 auto JDLookupFlags = KV.second;
2375 dbgs() <<
"Visiting \"" << JD.getName() <<
"\" (" << JDLookupFlags
2376 <<
") with lookup set " << IPLS->LookupSet <<
":\n";
2380 if (IPLS->NewJITDylib) {
2384 SymbolLookupSet Tmp;
2385 std::swap(IPLS->DefGeneratorNonCandidates, Tmp);
2386 IPLS->DefGeneratorCandidates.append(std::move(Tmp));
2389 dbgs() <<
" First time visiting " << JD.getName()
2390 <<
", resetting candidate sets and building generator stack\n";
2395 IPLS->CurDefGeneratorStack.reserve(JD.DefGenerators.size());
2397 IPLS->CurDefGeneratorStack.push_back(DG);
2401 IPLS->NewJITDylib =
false;
2410 Err = IL_updateCandidatesFor(
2411 JD, JDLookupFlags, IPLS->DefGeneratorCandidates,
2412 JD.DefGenerators.empty() ?
nullptr
2413 : &IPLS->DefGeneratorNonCandidates);
2415 dbgs() <<
" Remaining candidates = " << IPLS->DefGeneratorCandidates
2423 IPLS->DefGeneratorCandidates.empty())
2424 OL_resumeLookupAfterGeneration(*IPLS);
2430 return IPLS->fail(std::move(Err));
2434 if (IPLS->CurDefGeneratorStack.empty())
2435 LLVM_DEBUG(
dbgs() <<
" No generators to run for this JITDylib.\n");
2436 else if (IPLS->DefGeneratorCandidates.empty())
2439 dbgs() <<
" Running " << IPLS->CurDefGeneratorStack.size()
2440 <<
" remaining generators for "
2441 << IPLS->DefGeneratorCandidates.size() <<
" candidates\n";
2443 while (!IPLS->CurDefGeneratorStack.empty() &&
2444 !IPLS->DefGeneratorCandidates.empty()) {
2445 auto DG = IPLS->CurDefGeneratorStack.back().lock();
2448 return IPLS->fail(make_error<StringError>(
2449 "DefinitionGenerator removed while lookup in progress",
2462 std::lock_guard<std::mutex> Lock(DG->M);
2464 DG->PendingLookups.push_back(std::move(IPLS));
2473 auto &LookupSet = IPLS->DefGeneratorCandidates;
2478 LLVM_DEBUG(
dbgs() <<
" Attempting to generate " << LookupSet <<
"\n");
2480 Err = DG->tryToGenerate(LS, K, JD, JDLookupFlags, LookupSet);
2481 IPLS = std::move(
LS.IPLS);
2487 OL_resumeLookupAfterGeneration(*IPLS);
2492 dbgs() <<
" Error attempting to generate " << LookupSet <<
"\n";
2494 assert(IPLS &&
"LS cannot be retained if error is returned");
2495 return IPLS->fail(std::move(Err));
2501 {
dbgs() <<
" LookupState captured. Exiting phase1 for now.\n"; });
2508 LLVM_DEBUG(
dbgs() <<
" Updating candidate set post-generation\n");
2509 Err = IL_updateCandidatesFor(
2510 JD, JDLookupFlags, IPLS->DefGeneratorCandidates,
2511 JD.DefGenerators.empty() ?
nullptr
2512 : &IPLS->DefGeneratorNonCandidates);
2517 LLVM_DEBUG(
dbgs() <<
" Error encountered while updating candidates\n");
2518 return IPLS->fail(std::move(Err));
2522 if (IPLS->DefGeneratorCandidates.empty() &&
2523 IPLS->DefGeneratorNonCandidates.empty()) {
2526 IPLS->CurSearchOrderIndex = IPLS->SearchOrder.size();
2532 ++IPLS->CurSearchOrderIndex;
2533 IPLS->NewJITDylib =
true;
2538 IPLS->DefGeneratorCandidates.remove_if(
2546 if (IPLS->DefGeneratorCandidates.empty()) {
2548 IPLS->complete(std::move(IPLS));
2550 LLVM_DEBUG(
dbgs() <<
"Phase 1 failed with unresolved symbols.\n");
2551 IPLS->fail(make_error<SymbolsNotFound>(
2556void ExecutionSession::OL_completeLookup(
2557 std::unique_ptr<InProgressLookupState> IPLS,
2558 std::shared_ptr<AsynchronousSymbolQuery> Q,
2562 dbgs() <<
"Entering OL_completeLookup:\n"
2563 <<
" Lookup kind: " << IPLS->K <<
"\n"
2564 <<
" Search order: " << IPLS->SearchOrder
2565 <<
", Current index = " << IPLS->CurSearchOrderIndex
2566 << (IPLS->NewJITDylib ?
" (entering new JITDylib)" :
"") <<
"\n"
2567 <<
" Lookup set: " << IPLS->LookupSet <<
"\n"
2568 <<
" Definition generator candidates: "
2569 << IPLS->DefGeneratorCandidates <<
"\n"
2570 <<
" Definition generator non-candidates: "
2571 << IPLS->DefGeneratorNonCandidates <<
"\n";
2574 bool QueryComplete =
false;
2575 DenseMap<JITDylib *, JITDylib::UnmaterializedInfosList> CollectedUMIs;
2578 for (
auto &KV : IPLS->SearchOrder) {
2579 auto &JD = *KV.first;
2580 auto JDLookupFlags = KV.second;
2582 dbgs() <<
"Visiting \"" << JD.getName() <<
"\" (" << JDLookupFlags
2583 <<
") with lookup set " << IPLS->LookupSet <<
":\n";
2586 auto Err = IPLS->LookupSet.forEachWithRemoval(
2587 [&](
const SymbolStringPtr &
Name,
2590 dbgs() <<
" Attempting to match \"" <<
Name <<
"\" ("
2591 << SymLookupFlags <<
")... ";
2596 auto SymI = JD.Symbols.find(
Name);
2597 if (SymI == JD.Symbols.end()) {
2604 if (!SymI->second.getFlags().isExported() &&
2616 if (SymI->second.getFlags().hasMaterializationSideEffectsOnly() &&
2620 "required, but symbol is has-side-effects-only\n";
2628 if (SymI->second.getFlags().hasError()) {
2630 auto FailedSymbolsMap = std::make_shared<SymbolDependenceMap>();
2631 (*FailedSymbolsMap)[&JD] = {
Name};
2632 return make_error<FailedToMaterialize>(
2640 if (SymI->second.getState() >= Q->getRequiredState()) {
2642 <<
"matched, symbol already in required state\n");
2643 Q->notifySymbolMetRequiredState(
Name, SymI->second.getSymbol());
2650 if (SymI->second.hasMaterializerAttached()) {
2651 assert(SymI->second.getAddress() == ExecutorAddr() &&
2652 "Symbol not resolved but already has address?");
2653 auto UMII = JD.UnmaterializedInfos.find(
Name);
2654 assert(UMII != JD.UnmaterializedInfos.end() &&
2655 "Lazy symbol should have UnmaterializedInfo");
2657 auto UMI = UMII->second;
2658 assert(UMI->MU &&
"Materializer should not be null");
2659 assert(UMI->RT &&
"Tracker should not be null");
2661 dbgs() <<
"matched, preparing to dispatch MU@" << UMI->MU.get()
2662 <<
" (" << UMI->MU->getName() <<
")\n";
2667 for (
auto &KV : UMI->MU->getSymbols()) {
2668 auto SymK = JD.Symbols.find(KV.first);
2669 assert(SymK != JD.Symbols.end() &&
2670 "No entry for symbol covered by MaterializationUnit");
2671 SymK->second.setMaterializerAttached(
false);
2673 JD.UnmaterializedInfos.erase(KV.first);
2677 CollectedUMIs[&JD].push_back(std::move(UMI));
2685 "By this line the symbol should be materializing");
2686 auto &
MI = JD.MaterializingInfos[
Name];
2688 Q->addQueryDependence(JD,
Name);
2693 JD.shrinkMaterializationInfoMemory();
2699 dbgs() <<
"Lookup failed. Detaching query and replacing MUs.\n";
2706 for (
auto &KV : CollectedUMIs) {
2707 auto &JD = *KV.first;
2708 for (
auto &UMI : KV.second)
2709 for (
auto &KV2 : UMI->MU->getSymbols()) {
2710 assert(!JD.UnmaterializedInfos.count(KV2.first) &&
2711 "Unexpected materializer in map");
2712 auto SymI = JD.Symbols.find(KV2.first);
2713 assert(SymI != JD.Symbols.end() &&
"Missing symbol entry");
2715 "Can not replace symbol that is not materializing");
2716 assert(!SymI->second.hasMaterializerAttached() &&
2717 "MaterializerAttached flag should not be set");
2718 SymI->second.setMaterializerAttached(
true);
2719 JD.UnmaterializedInfos[KV2.first] = UMI;
2727 LLVM_DEBUG(
dbgs() <<
"Stripping unmatched weakly-referenced symbols\n");
2728 IPLS->LookupSet.forEachWithRemoval(
2731 Q->dropSymbol(
Name);
2737 if (!IPLS->LookupSet.empty()) {
2740 IPLS->LookupSet.getSymbolNames());
2744 QueryComplete = Q->isComplete();
2747 dbgs() <<
"Query successfully "
2748 << (QueryComplete ?
"completed" :
"lodged") <<
"\n";
2752 if (!CollectedUMIs.empty()) {
2753 std::lock_guard<std::recursive_mutex> Lock(OutstandingMUsMutex);
2756 for (
auto &KV : CollectedUMIs) {
2758 auto &JD = *KV.first;
2759 dbgs() <<
" For " << JD.getName() <<
": Adding " << KV.second.size()
2762 for (
auto &UMI : KV.second) {
2763 auto MR = createMaterializationResponsibility(
2764 *UMI->RT, std::move(UMI->MU->SymbolFlags),
2765 std::move(UMI->MU->InitSymbol));
2766 OutstandingMUs.push_back(
2767 std::make_pair(std::move(UMI->MU), std::move(MR)));
2773 if (RegisterDependencies && !Q->QueryRegistrations.empty()) {
2775 RegisterDependencies(Q->QueryRegistrations);
2785 Q->handleFailed(std::move(LodgingErr));
2789 if (QueryComplete) {
2791 Q->handleComplete(*
this);
2794 dispatchOutstandingMUs();
2797void ExecutionSession::OL_completeLookupFlags(
2798 std::unique_ptr<InProgressLookupState> IPLS,
2799 unique_function<
void(Expected<SymbolFlagsMap>)> OnComplete) {
2803 dbgs() <<
"Entering OL_completeLookupFlags:\n"
2804 <<
" Lookup kind: " << IPLS->K <<
"\n"
2805 <<
" Search order: " << IPLS->SearchOrder
2806 <<
", Current index = " << IPLS->CurSearchOrderIndex
2807 << (IPLS->NewJITDylib ?
" (entering new JITDylib)" :
"") <<
"\n"
2808 <<
" Lookup set: " << IPLS->LookupSet <<
"\n"
2809 <<
" Definition generator candidates: "
2810 << IPLS->DefGeneratorCandidates <<
"\n"
2811 <<
" Definition generator non-candidates: "
2812 << IPLS->DefGeneratorNonCandidates <<
"\n";
2818 for (
auto &KV : IPLS->SearchOrder) {
2819 auto &JD = *KV.first;
2820 auto JDLookupFlags = KV.second;
2822 dbgs() <<
"Visiting \"" << JD.getName() <<
"\" (" << JDLookupFlags
2823 <<
") with lookup set " << IPLS->LookupSet <<
":\n";
2826 IPLS->LookupSet.forEachWithRemoval([&](
const SymbolStringPtr &
Name,
2829 dbgs() <<
" Attempting to match \"" <<
Name <<
"\" ("
2830 << SymLookupFlags <<
")... ";
2835 auto SymI = JD.Symbols.find(
Name);
2836 if (SymI == JD.Symbols.end()) {
2842 if (!SymI->second.getFlags().isExported() &&
2849 dbgs() <<
"matched, \"" <<
Name <<
"\" -> " << SymI->second.getFlags()
2858 IPLS->LookupSet.remove_if(
2863 if (!IPLS->LookupSet.empty()) {
2866 IPLS->LookupSet.getSymbolNames());
2875 OnComplete(std::move(
Result));
2878void ExecutionSession::OL_destroyMaterializationResponsibility(
2879 MaterializationResponsibility &MR) {
2881 assert(MR.SymbolFlags.empty() &&
2882 "All symbols should have been explicitly materialized or failed");
2883 MR.JD.unlinkMaterializationResponsibility(MR);
2887 const MaterializationResponsibility &MR) {
2888 return MR.JD.getRequestedSymbols(MR.SymbolFlags);
2891Error ExecutionSession::OL_notifyResolved(MaterializationResponsibility &MR,
2894 dbgs() <<
"In " << MR.JD.getName() <<
" resolving " << Symbols <<
"\n";
2897 for (
auto &KV : Symbols) {
2898 auto I = MR.SymbolFlags.find(KV.first);
2899 assert(
I != MR.SymbolFlags.end() &&
2900 "Resolving symbol outside this responsibility set");
2901 assert(!
I->second.hasMaterializationSideEffectsOnly() &&
2902 "Can't resolve materialization-side-effects-only symbol");
2905 "Resolving symbol with incorrect flags");
2909 return MR.JD.resolve(MR, Symbols);
2912template <
typename HandleNewDepFn>
2913void ExecutionSession::propagateExtraEmitDeps(
2914 std::deque<JITDylib::EmissionDepUnit *> Worklist, EDUInfosMap &EDUInfos,
2915 HandleNewDepFn HandleNewDep) {
2919 while (!Worklist.empty()) {
2920 auto &EDU = *Worklist.front();
2921 Worklist.pop_front();
2923 assert(EDUInfos.count(&EDU) &&
"No info entry for EDU");
2924 auto &EDUInfo = EDUInfos[&EDU];
2927 for (
auto *UserEDU : EDUInfo.IntraEmitUsers) {
2930 JITDylib::EmissionDepUnitInfo *UserEDUInfo =
nullptr;
2932 auto UserEDUInfoItr = EDUInfos.find(UserEDU);
2933 if (UserEDUInfoItr != EDUInfos.end())
2934 UserEDUInfo = &UserEDUInfoItr->second;
2937 for (
auto &[DepJD, Deps] : EDUInfo.NewDeps) {
2938 auto &UserEDUDepsForJD = UserEDU->Dependencies[DepJD];
2939 DenseSet<NonOwningSymbolStringPtr> *UserEDUNewDepsForJD =
nullptr;
2940 for (
auto Dep : Deps) {
2941 if (UserEDUDepsForJD.insert(Dep).second) {
2942 HandleNewDep(*UserEDU, *DepJD, Dep);
2944 if (!UserEDUNewDepsForJD) {
2947 if (UserEDUInfo->NewDeps.empty())
2948 Worklist.push_back(UserEDU);
2949 UserEDUNewDepsForJD = &UserEDUInfo->NewDeps[DepJD];
2952 UserEDUNewDepsForJD->insert(Dep);
2959 EDUInfo.NewDeps.clear();
2964ExecutionSession::EDUInfosMap ExecutionSession::simplifyDepGroups(
2965 MaterializationResponsibility &MR,
2966 ArrayRef<SymbolDependenceGroup> EmittedDeps) {
2968 auto &TargetJD = MR.getTargetJITDylib();
2972 DenseMap<JITDylib::EmissionDepUnit *, JITDylib::EmissionDepUnitInfo> EDUInfos;
2973 EDUInfos.reserve(EmittedDeps.size());
2974 DenseMap<NonOwningSymbolStringPtr, JITDylib::EmissionDepUnit *> EDUForSymbol;
2975 for (
auto &DG : EmittedDeps) {
2976 assert(!DG.Symbols.empty() &&
"DepGroup does not cover any symbols");
2979 if (DG.Dependencies.empty())
2982 auto TmpEDU = std::make_shared<JITDylib::EmissionDepUnit>(TargetJD);
2983 auto &EDUInfo = EDUInfos[TmpEDU.get()];
2984 EDUInfo.EDU = std::move(TmpEDU);
2985 for (
const auto &Symbol : DG.Symbols) {
2986 NonOwningSymbolStringPtr NonOwningSymbol(Symbol);
2987 assert(!EDUForSymbol.count(NonOwningSymbol) &&
2988 "Symbol should not appear in more than one SymbolDependenceGroup");
2989 assert(MR.getSymbols().count(Symbol) &&
2990 "Symbol in DepGroups not in the emitted set");
2991 auto NewlyEmittedItr = MR.getSymbols().find(Symbol);
2992 EDUInfo.EDU->Symbols[NonOwningSymbol] = NewlyEmittedItr->second;
2993 EDUForSymbol[NonOwningSymbol] = EDUInfo.EDU.get();
2999 DenseMap<NonOwningSymbolStringPtr, JITSymbolFlags> ResidualSymbolFlags;
3000 for (
auto &[
Sym, Flags] : MR.getSymbols()) {
3001 if (!EDUForSymbol.count(NonOwningSymbolStringPtr(
Sym)))
3002 ResidualSymbolFlags[NonOwningSymbolStringPtr(
Sym)] =
Flags;
3004 if (!ResidualSymbolFlags.empty()) {
3005 auto ResidualEDU = std::make_shared<JITDylib::EmissionDepUnit>(TargetJD);
3006 ResidualEDU->Symbols = std::move(ResidualSymbolFlags);
3007 auto &ResidualEDUInfo = EDUInfos[ResidualEDU.get()];
3008 ResidualEDUInfo.EDU = std::move(ResidualEDU);
3011 if (EDUInfos.size() == 1)
3015 for (
auto &[
Sym, Flags] : ResidualEDUInfo.EDU->Symbols)
3016 EDUForSymbol[
Sym] = ResidualEDUInfo.EDU.get();
3021 assert(EDUForSymbol.size() == MR.getSymbols().size() &&
3022 "MR symbols not fully covered by EDUs?");
3023 for (
auto &[
Sym, Flags] : MR.getSymbols()) {
3024 assert(EDUForSymbol.count(NonOwningSymbolStringPtr(
Sym)) &&
3025 "Sym in MR not covered by EDU");
3033 for (
auto &DG : EmittedDeps) {
3036 if (DG.Dependencies.empty())
3039 assert(EDUForSymbol.count(NonOwningSymbolStringPtr(*DG.Symbols.begin())) &&
3042 *EDUForSymbol.find(NonOwningSymbolStringPtr(*DG.Symbols.begin()))
3045 for (
auto &[DepJD, Deps] : DG.Dependencies) {
3046 DenseSet<NonOwningSymbolStringPtr> NewDepsForJD;
3048 assert(!Deps.empty() &&
"Dependence set for DepJD is empty");
3050 if (DepJD != &TargetJD) {
3053 for (
auto &Dep : Deps)
3054 NewDepsForJD.insert(NonOwningSymbolStringPtr(Dep));
3058 for (
auto &Dep : Deps) {
3059 NonOwningSymbolStringPtr NonOwningDep(Dep);
3060 auto I = EDUForSymbol.find(NonOwningDep);
3061 if (
I == EDUForSymbol.end()) {
3062 if (!MR.getSymbols().count(Dep))
3063 NewDepsForJD.insert(NonOwningDep);
3067 if (
I->second != &EDU)
3068 EDUInfos[
I->second].IntraEmitUsers.insert(&EDU);
3072 if (!NewDepsForJD.empty())
3073 EDU.Dependencies[DepJD] = std::move(NewDepsForJD);
3078 std::deque<JITDylib::EmissionDepUnit *> Worklist;
3079 for (
auto &[EDU, EDUInfo] : EDUInfos) {
3082 if (!EDU->Dependencies.empty()) {
3083 auto I = EDUInfos.find(EDU);
3084 if (
I != EDUInfos.end()) {
3085 auto &EDUInfo =
I->second;
3086 if (!EDUInfo.IntraEmitUsers.empty()) {
3087 EDUInfo.NewDeps = EDU->Dependencies;
3088 Worklist.push_back(EDU);
3095 propagateExtraEmitDeps(
3097 [](JITDylib::EmissionDepUnit &,
JITDylib &, NonOwningSymbolStringPtr) {});
3102void ExecutionSession::IL_makeEDUReady(
3103 std::shared_ptr<JITDylib::EmissionDepUnit> EDU,
3104 JITDylib::AsynchronousSymbolQuerySet &Queries) {
3107 auto &JD = *EDU->JD;
3109 for (
auto &[
Sym, Flags] : EDU->Symbols) {
3110 assert(JD.Symbols.count(SymbolStringPtr(
Sym)) &&
3111 "JD does not have an entry for Sym");
3112 auto &Entry = JD.Symbols[SymbolStringPtr(
Sym)];
3114 assert(((Entry.getFlags().hasMaterializationSideEffectsOnly() &&
3118 "Emitting from state other than Resolved");
3122 auto MII = JD.MaterializingInfos.find(SymbolStringPtr(
Sym));
3125 if (MII == JD.MaterializingInfos.end())
3127 auto &
MI = MII->second;
3130 Q->notifySymbolMetRequiredState(SymbolStringPtr(
Sym), Entry.getSymbol());
3131 if (Q->isComplete())
3133 Q->removeQueryDependence(JD, SymbolStringPtr(
Sym));
3136 JD.MaterializingInfos.erase(MII);
3139 JD.shrinkMaterializationInfoMemory();
3142void ExecutionSession::IL_makeEDUEmitted(
3143 std::shared_ptr<JITDylib::EmissionDepUnit> EDU,
3144 JITDylib::AsynchronousSymbolQuerySet &Queries) {
3147 auto &JD = *EDU->JD;
3149 for (
auto &[
Sym, Flags] : EDU->Symbols) {
3150 assert(JD.Symbols.count(SymbolStringPtr(
Sym)) &&
3151 "JD does not have an entry for Sym");
3152 auto &Entry = JD.Symbols[SymbolStringPtr(
Sym)];
3154 assert(((Entry.getFlags().hasMaterializationSideEffectsOnly() &&
3158 "Emitting from state other than Resolved");
3163 for (
auto &[
Sym, Flags] : EDU->Symbols) {
3164 assert(JD.Symbols.count(SymbolStringPtr(
Sym)) &&
3165 "JD does not have an entry for Sym");
3166 auto &Entry = JD.Symbols[SymbolStringPtr(
Sym)];
3168 "Symbols for EDU in inconsistent state");
3169 assert(JD.MaterializingInfos.count(SymbolStringPtr(
Sym)) &&
3170 "Emitted symbol has no MI");
3171 auto MI = JD.MaterializingInfos[SymbolStringPtr(
Sym)];
3173 "Already-emitted symbol has waiting-on-emitted queries");
3180 auto &
MI = JD.MaterializingInfos[SymbolStringPtr(
Sym)];
3181 MI.DefiningEDU = EDU;
3184 Q->notifySymbolMetRequiredState(SymbolStringPtr(
Sym), Entry.getSymbol());
3185 if (Q->isComplete())
3187 Q->removeQueryDependence(JD, SymbolStringPtr(
Sym));
3191 for (
auto &[DepJD, Deps] : EDU->Dependencies) {
3192 for (
auto &Dep : Deps)
3193 DepJD->MaterializingInfos[SymbolStringPtr(Dep)].DependantEDUs.insert(
3201bool ExecutionSession::IL_removeEDUDependence(JITDylib::EmissionDepUnit &EDU,
3203 NonOwningSymbolStringPtr DepSym,
3204 EDUInfosMap &EDUInfos) {
3205 assert(EDU.Dependencies.count(&DepJD) &&
3206 "JD does not appear in Dependencies of DependantEDU");
3207 assert(EDU.Dependencies[&DepJD].count(DepSym) &&
3208 "Symbol does not appear in Dependencies of DependantEDU");
3209 auto &JDDeps = EDU.Dependencies[&DepJD];
3210 JDDeps.erase(DepSym);
3211 if (JDDeps.empty()) {
3212 EDU.Dependencies.erase(&DepJD);
3213 if (EDU.Dependencies.empty()) {
3218 auto &DepEDUInfo = EDUInfos[&EDU];
3219 if (!DepEDUInfo.EDU) {
3220 assert(EDU.JD->Symbols.count(
3221 SymbolStringPtr(EDU.Symbols.begin()->first)) &&
3222 "Missing symbol entry for first symbol in EDU");
3223 auto DepEDUFirstMI = EDU.JD->MaterializingInfos.find(
3224 SymbolStringPtr(EDU.Symbols.begin()->first));
3225 assert(DepEDUFirstMI != EDU.JD->MaterializingInfos.end() &&
3226 "Missing MI for first symbol in DependantEDU");
3227 DepEDUInfo.EDU = DepEDUFirstMI->second.DefiningEDU;
3235Error ExecutionSession::makeJDClosedError(JITDylib::EmissionDepUnit &EDU,
3236 JITDylib &ClosedJD) {
3238 for (
auto &[
Sym, Flags] : EDU.Symbols)
3239 FailedSymbols.
insert(SymbolStringPtr(
Sym));
3241 for (
auto &Dep : EDU.Dependencies[&ClosedJD])
3242 BadDeps[&ClosedJD].
insert(SymbolStringPtr(Dep));
3243 return make_error<UnsatisfiedSymbolDependencies>(
3244 ClosedJD.getExecutionSession().getSymbolStringPool(), EDU.JD,
3245 std::move(FailedSymbols), std::move(BadDeps),
3246 ClosedJD.getName() +
" is closed");
3249Error ExecutionSession::makeUnsatisfiedDepsError(JITDylib::EmissionDepUnit &EDU,
3253 for (
auto &[
Sym, Flags] : EDU.Symbols)
3254 FailedSymbols.
insert(SymbolStringPtr(
Sym));
3256 BadDepsMap[&BadJD] = std::move(BadDeps);
3257 return make_error<UnsatisfiedSymbolDependencies>(
3258 BadJD.getExecutionSession().getSymbolStringPool(), &BadJD,
3259 std::move(FailedSymbols), std::move(BadDepsMap),
3260 "dependencies removed or in error state");
3263Expected<JITDylib::AsynchronousSymbolQuerySet>
3264ExecutionSession::IL_emit(MaterializationResponsibility &MR,
3265 EDUInfosMap EDUInfos) {
3267 if (MR.RT->isDefunct())
3268 return make_error<ResourceTrackerDefunct>(MR.RT);
3270 auto &TargetJD = MR.getTargetJITDylib();
3271 if (TargetJD.State != JITDylib::Open)
3272 return make_error<StringError>(
"JITDylib " + TargetJD.getName() +
3275#ifdef EXPENSIVE_CHECKS
3276 verifySessionState(
"entering ExecutionSession::IL_emit");
3285 std::deque<JITDylib::EmissionDepUnit *> Worklist;
3286 for (
auto &[EDU,
_] : EDUInfos)
3287 Worklist.push_back(EDU);
3289 for (
auto *EDU : Worklist) {
3290 auto *EDUInfo = &EDUInfos[EDU];
3292 SmallVector<JITDylib *> DepJDsToRemove;
3293 for (
auto &[DepJD, Deps] : EDU->Dependencies) {
3294 if (DepJD->State != JITDylib::Open)
3295 return makeJDClosedError(*EDU, *DepJD);
3298 SmallVector<NonOwningSymbolStringPtr> DepsToRemove;
3299 for (
auto &Dep : Deps) {
3300 auto DepEntryItr = DepJD->Symbols.
find(SymbolStringPtr(Dep));
3305 if (DepEntryItr == DepJD->Symbols.end() ||
3306 DepEntryItr->second.getFlags().hasError()) {
3307 BadDeps.insert(SymbolStringPtr(Dep));
3313 auto &DepEntry = DepEntryItr->second;
3315 EDUInfo->NewDeps[DepJD].insert(Dep);
3321 DepsToRemove.push_back(Dep);
3325 assert(!DepJD->MaterializingInfos.count(SymbolStringPtr(Dep)) &&
3326 "Unexpected MaterializationInfo attached to ready symbol");
3334 assert(DepJD->MaterializingInfos.count(SymbolStringPtr(Dep)) &&
3335 "Expected MaterializationInfo for emitted dependency");
3336 auto &DepMI = DepJD->MaterializingInfos[SymbolStringPtr(Dep)];
3337 assert(DepMI.DefiningEDU &&
3338 "Emitted symbol does not have a defining EDU");
3339 assert(!DepMI.DefiningEDU->Dependencies.empty() &&
3340 "Emitted symbol has empty dependencies (should be ready)");
3341 assert(DepMI.DependantEDUs.empty() &&
3342 "Already-emitted symbol has dependant EDUs?");
3343 auto &DepEDUInfo = EDUInfos[DepMI.DefiningEDU.get()];
3344 if (!DepEDUInfo.EDU) {
3347 EDUInfo = &EDUInfos[EDU];
3348 DepEDUInfo.EDU = DepMI.DefiningEDU;
3349 for (
auto &[DepDepJD, DepDeps] : DepEDUInfo.EDU->Dependencies) {
3350 if (DepDepJD == &TargetJD) {
3351 for (
auto &DepDep : DepDeps)
3352 if (!MR.getSymbols().count(SymbolStringPtr(DepDep)))
3353 DepEDUInfo.NewDeps[DepDepJD].insert(DepDep);
3355 DepEDUInfo.NewDeps[DepDepJD] = DepDeps;
3358 DepEDUInfo.IntraEmitUsers.insert(EDU);
3362 if (!BadDeps.empty())
3363 return makeUnsatisfiedDepsError(*EDU, *DepJD, std::move(BadDeps));
3366 for (
auto &Dep : DepsToRemove)
3371 DepJDsToRemove.push_back(DepJD);
3375 for (
auto &DepJD : DepJDsToRemove) {
3376 assert(EDU->Dependencies.count(DepJD) &&
3377 "Trying to remove non-existent dep entries");
3378 EDU->Dependencies.erase(DepJD);
3382 for (
auto &[
Sym, Flags] : EDU->Symbols) {
3383 assert(TargetJD.Symbols.count(SymbolStringPtr(
Sym)) &&
3384 "Sym not present in symbol table");
3385 assert((TargetJD.Symbols[SymbolStringPtr(
Sym)].getState() ==
3387 TargetJD.Symbols[SymbolStringPtr(
Sym)]
3389 .hasMaterializationSideEffectsOnly()) &&
3390 "Emitting symbol not in the resolved state");
3392 "Symbol is already in an error state");
3394 auto MII = TargetJD.MaterializingInfos.find(SymbolStringPtr(
Sym));
3395 if (MII == TargetJD.MaterializingInfos.end() ||
3396 MII->second.DependantEDUs.empty())
3399 for (
auto &DependantEDU : MII->second.DependantEDUs) {
3400 if (IL_removeEDUDependence(*DependantEDU, TargetJD,
Sym, EDUInfos))
3401 EDUInfo = &EDUInfos[EDU];
3402 EDUInfo->IntraEmitUsers.insert(DependantEDU);
3404 MII->second.DependantEDUs.clear();
3409 for (
auto &[EDU, EDUInfo] : EDUInfos) {
3410 if (!EDUInfo.IntraEmitUsers.empty() && !EDU->Dependencies.empty()) {
3411 if (EDUInfo.NewDeps.empty())
3412 EDUInfo.NewDeps = EDU->Dependencies;
3413 Worklist.push_back(EDU);
3417 propagateExtraEmitDeps(
3419 [](JITDylib::EmissionDepUnit &EDU,
JITDylib &JD,
3420 NonOwningSymbolStringPtr
Sym) {
3421 JD.MaterializingInfos[SymbolStringPtr(
Sym)].DependantEDUs.insert(&EDU);
3424 JITDylib::AsynchronousSymbolQuerySet CompletedQueries;
3428 for (
auto &[EDU, EDUInfo] : EDUInfos) {
3429 if (EDU->Dependencies.empty())
3430 IL_makeEDUReady(std::move(EDUInfo.EDU), CompletedQueries);
3432 IL_makeEDUEmitted(std::move(EDUInfo.EDU), CompletedQueries);
3435#ifdef EXPENSIVE_CHECKS
3436 verifySessionState(
"exiting ExecutionSession::IL_emit");
3439 return std::move(CompletedQueries);
3442Error ExecutionSession::OL_notifyEmitted(
3443 MaterializationResponsibility &MR,
3444 ArrayRef<SymbolDependenceGroup> DepGroups) {
3446 dbgs() <<
"In " << MR.JD.getName() <<
" emitting " << MR.SymbolFlags
3448 if (!DepGroups.empty()) {
3449 dbgs() <<
" Initial dependencies:\n";
3450 for (
auto &SDG : DepGroups) {
3451 dbgs() <<
" Symbols: " << SDG.Symbols
3452 <<
", Dependencies: " << SDG.Dependencies <<
"\n";
3459 for (
auto &DG : DepGroups) {
3460 for (
auto &
Sym : DG.Symbols) {
3462 "DG contains dependence for symbol outside this MR");
3464 "DG contains duplicate entries for Name");
3469 auto EDUInfos = simplifyDepGroups(MR, DepGroups);
3472 dbgs() <<
" Simplified dependencies:\n";
3473 for (
auto &[EDU, EDUInfo] : EDUInfos) {
3474 dbgs() <<
" Symbols: { ";
3475 for (
auto &[
Sym, Flags] : EDU->Symbols)
3477 dbgs() <<
"}, Dependencies: { ";
3478 for (
auto &[DepJD, Deps] : EDU->Dependencies) {
3479 dbgs() <<
"(" << DepJD->getName() <<
", { ";
3480 for (
auto &Dep : Deps)
3481 dbgs() << Dep <<
" ";
3488 auto CompletedQueries =
3492 if (!CompletedQueries)
3493 return CompletedQueries.takeError();
3495 MR.SymbolFlags.clear();
3498 for (
auto &Q : *CompletedQueries) {
3499 assert(Q->isComplete() &&
"Q is not complete");
3500 Q->handleComplete(*
this);
3506Error ExecutionSession::OL_defineMaterializing(
3507 MaterializationResponsibility &MR,
SymbolFlagsMap NewSymbolFlags) {
3510 dbgs() <<
"In " << MR.JD.getName() <<
" defining materializing symbols "
3511 << NewSymbolFlags <<
"\n";
3513 if (
auto AcceptedDefs =
3514 MR.JD.defineMaterializing(MR, std::move(NewSymbolFlags))) {
3516 for (
auto &KV : *AcceptedDefs)
3517 MR.SymbolFlags.insert(KV);
3520 return AcceptedDefs.takeError();
3523std::pair<JITDylib::AsynchronousSymbolQuerySet,
3524 std::shared_ptr<SymbolDependenceMap>>
3525ExecutionSession::IL_failSymbols(JITDylib &JD,
3528#ifdef EXPENSIVE_CHECKS
3529 verifySessionState(
"entering ExecutionSession::IL_failSymbols");
3532 JITDylib::AsynchronousSymbolQuerySet FailedQueries;
3533 auto FailedSymbolsMap = std::make_shared<SymbolDependenceMap>();
3534 auto ExtractFailedQueries = [&](JITDylib::MaterializingInfo &
MI) {
3535 JITDylib::AsynchronousSymbolQueryList ToDetach;
3536 for (
auto &Q :
MI.pendingQueries()) {
3538 FailedQueries.insert(Q);
3539 ToDetach.push_back(Q);
3541 for (
auto &Q : ToDetach)
3543 assert(!
MI.hasQueriesPending() &&
"Queries still pending after detach");
3546 for (
auto &
Name : SymbolsToFail) {
3547 (*FailedSymbolsMap)[&JD].insert(
Name);
3550 auto SymI = JD.Symbols.find(
Name);
3559 if (SymI == JD.Symbols.end())
3561 auto &
Sym = SymI->second;
3565 if (
Sym.getFlags().hasError()) {
3567 "Symbol in error state still has MaterializingInfo");
3577 auto MII = JD.MaterializingInfos.find(
Name);
3578 if (MII == JD.MaterializingInfos.end())
3581 auto &
MI = MII->second;
3584 ExtractFailedQueries(
MI);
3586 if (
MI.DefiningEDU) {
3590 "Symbol with DefiningEDU should not have DependantEDUs");
3592 "Symbol has EDU, should have been emitted");
3593 assert(
MI.DefiningEDU->Symbols.count(NonOwningSymbolStringPtr(
Name)) &&
3594 "Symbol does not appear in its DefiningEDU");
3595 MI.DefiningEDU->Symbols.erase(NonOwningSymbolStringPtr(
Name));
3596 MI.DefiningEDU =
nullptr;
3603 for (
auto &DependantEDU :
MI.DependantEDUs) {
3606 for (
auto &[DepJD, DepSyms] : DependantEDU->Dependencies) {
3607 for (
auto DepSym : DepSyms) {
3610 if (DepJD == &JD && DepSym ==
Name)
3612 assert(DepJD->Symbols.count(SymbolStringPtr(DepSym)) &&
3613 "DepSym not in DepJD?");
3614 assert(DepJD->MaterializingInfos.count(SymbolStringPtr(DepSym)) &&
3615 "DependantEDU not registered with symbol it depends on");
3616 auto &SymMI = DepJD->MaterializingInfos[SymbolStringPtr(DepSym)];
3617 assert(SymMI.DependantEDUs.count(DependantEDU) &&
3618 "DependantEDU missing from DependantEDUs list");
3619 SymMI.DependantEDUs.erase(DependantEDU);
3625 auto &DepJD = *DependantEDU->JD;
3626 auto DepEDUSymbols = std::move(DependantEDU->Symbols);
3627 for (
auto &[DepName, Flags] : DepEDUSymbols) {
3628 auto DepSymItr = DepJD.Symbols.find(SymbolStringPtr(DepName));
3629 assert(DepSymItr != DepJD.Symbols.end() &&
3630 "Symbol not present in table");
3631 auto &DepSym = DepSymItr->second;
3634 "Symbol has EDU, should have been emitted");
3635 assert(!DepSym.getFlags().hasError() &&
3636 "Symbol is already in the error state?");
3638 (*FailedSymbolsMap)[&DepJD].insert(SymbolStringPtr(DepName));
3643 DepJD.MaterializingInfos.find(SymbolStringPtr(DepName));
3644 assert(DepMIItr != DepJD.MaterializingInfos.end() &&
3645 "Symbol has defining EDU but not MaterializingInfo");
3646 auto &DepMI = DepMIItr->second;
3647 assert(DepMI.DefiningEDU.get() == DependantEDU &&
3648 "Bad EDU dependence edge");
3649 assert(DepMI.DependantEDUs.empty() &&
3650 "Symbol was emitted, should not have any DependantEDUs");
3651 ExtractFailedQueries(DepMI);
3652 DepJD.MaterializingInfos.erase(SymbolStringPtr(DepName));
3655 DepJD.shrinkMaterializationInfoMemory();
3658 MI.DependantEDUs.clear();
3661 assert(!
MI.DefiningEDU &&
"DefiningEDU should have been reset");
3663 "DependantEDUs should have been removed above");
3665 "Can not delete MaterializingInfo with queries pending");
3666 JD.MaterializingInfos.erase(
Name);
3669 JD.shrinkMaterializationInfoMemory();
3671#ifdef EXPENSIVE_CHECKS
3672 verifySessionState(
"exiting ExecutionSession::IL_failSymbols");
3675 return std::make_pair(std::move(FailedQueries), std::move(FailedSymbolsMap));
3678void ExecutionSession::OL_notifyFailed(MaterializationResponsibility &MR) {
3681 dbgs() <<
"In " << MR.JD.getName() <<
" failing materialization for "
3682 << MR.SymbolFlags <<
"\n";
3685 if (MR.SymbolFlags.empty())
3689 for (
auto &[
Name, Flags] : MR.SymbolFlags)
3690 SymbolsToFail.push_back(
Name);
3691 MR.SymbolFlags.clear();
3693 JITDylib::AsynchronousSymbolQuerySet FailedQueries;
3694 std::shared_ptr<SymbolDependenceMap> FailedSymbols;
3698 if (MR.RT->isDefunct())
3699 return std::pair<JITDylib::AsynchronousSymbolQuerySet,
3700 std::shared_ptr<SymbolDependenceMap>>();
3701 return IL_failSymbols(MR.getTargetJITDylib(), SymbolsToFail);
3704 for (
auto &Q : FailedQueries)
3709Error ExecutionSession::OL_replace(MaterializationResponsibility &MR,
3710 std::unique_ptr<MaterializationUnit> MU) {
3711 for (
auto &KV : MU->getSymbols()) {
3712 assert(MR.SymbolFlags.count(KV.first) &&
3713 "Replacing definition outside this responsibility set");
3714 MR.SymbolFlags.erase(KV.first);
3717 if (MU->getInitializerSymbol() == MR.InitSymbol)
3718 MR.InitSymbol =
nullptr;
3720 LLVM_DEBUG(MR.JD.getExecutionSession().runSessionLocked([&]() {
3721 dbgs() <<
"In " << MR.JD.getName() <<
" replacing symbols with " << *MU
3725 return MR.JD.replace(MR, std::move(MU));
3728Expected<std::unique_ptr<MaterializationResponsibility>>
3729ExecutionSession::OL_delegate(MaterializationResponsibility &MR,
3732 SymbolStringPtr DelegatedInitSymbol;
3735 for (
auto &
Name : Symbols) {
3736 auto I = MR.SymbolFlags.find(
Name);
3737 assert(
I != MR.SymbolFlags.end() &&
3738 "Symbol is not tracked by this MaterializationResponsibility "
3741 DelegatedFlags[
Name] = std::move(
I->second);
3742 if (
Name == MR.InitSymbol)
3743 std::swap(MR.InitSymbol, DelegatedInitSymbol);
3745 MR.SymbolFlags.erase(
I);
3748 return MR.JD.delegate(MR, std::move(DelegatedFlags),
3749 std::move(DelegatedInitSymbol));
3753void ExecutionSession::dumpDispatchInfo(Task &
T) {
3755 dbgs() <<
"Dispatching: ";
3756 T.printDescription(
dbgs());
for(const MachineOperand &MO :llvm::drop_begin(OldMI.operands(), Desc.getNumOperands()))
aarch64 falkor hwpf fix Falkor HW Prefetch Fix Late Phase
while(!ToSimplify.empty())
static StringRef getName(Value *V)
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
static uint32_t getFlags(const Symbol *Sym)
ArrayRef - Represent a constant reference to an array (0 or more elements consecutively in memory),...
const T & front() const
front - Get the first element.
size_t size() const
size - Get the array size.
bool empty() const
empty - Check if the array is empty.
iterator find(const_arg_type_t< KeyT > Val)
bool erase(const KeyT &Val)
size_type count(const_arg_type_t< KeyT > Val) const
Return 1 if the specified key is in the map, 0 otherwise.
std::pair< iterator, bool > insert(const std::pair< KeyT, ValueT > &KV)
Helper for Errors used as out-parameters.
Lightweight error class with error context and mandatory checking.
static ErrorSuccess success()
Create a success value.
Tagged union holding either a T or a Error.
void push_back(const T &Elt)
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small.
StringRef - Represent a constant reference to a string, i.e.
Symbol info for RuntimeDyld.
Twine - A lightweight data structure for efficiently representing the concatenation of temporary valu...
std::pair< iterator, bool > insert(const ValueT &V)
iterator find(const_arg_type_t< ValueT > V)
size_type count(const_arg_type_t< ValueT > V) const
Return 1 if the specified key is in the set, 0 otherwise.
const std::string & getName() const
Get the name for this JITLinkDylib.
StringRef getName() const override
Return the name of this materialization unit.
AbsoluteSymbolsMaterializationUnit(SymbolMap Symbols)
AsynchronousSymbolQuery(const SymbolLookupSet &Symbols, SymbolState RequiredState, SymbolsResolvedCallback NotifyComplete)
Create a query for the given symbols.
void notifySymbolMetRequiredState(const SymbolStringPtr &Name, ExecutorSymbolDef Sym)
Notify the query that a requested symbol has reached the required state.
Definition generators can be attached to JITDylibs to generate new definitions for otherwise unresolv...
virtual ~DefinitionGenerator()
An ExecutionSession represents a running JIT program.
Error endSession()
End the session.
void reportError(Error Err)
Report a error for this execution session.
void lookupFlags(LookupKind K, JITDylibSearchOrder SearchOrder, SymbolLookupSet Symbols, unique_function< void(Expected< SymbolFlagsMap >)> OnComplete)
Search the given JITDylibs to find the flags associated with each of the given symbols.
SymbolStringPtr intern(StringRef SymName)
Add a symbol name to the SymbolStringPool and return a pointer to it.
JITDylib * getJITDylibByName(StringRef Name)
Return a pointer to the "name" JITDylib.
JITDylib & createBareJITDylib(std::string Name)
Add a new bare JITDylib to this ExecutionSession.
std::shared_ptr< SymbolStringPool > getSymbolStringPool()
Get the SymbolStringPool for this instance.
void lookup(LookupKind K, const JITDylibSearchOrder &SearchOrder, SymbolLookupSet Symbols, SymbolState RequiredState, SymbolsResolvedCallback NotifyComplete, RegisterDependenciesFunction RegisterDependencies)
Search the given JITDylibs for the given symbols.
Error registerJITDispatchHandlers(JITDylib &JD, JITDispatchHandlerAssociationMap WFs)
For each tag symbol name, associate the corresponding AsyncHandlerWrapperFunction with the address of...
void registerResourceManager(ResourceManager &RM)
Register the given ResourceManager with this ExecutionSession.
~ExecutionSession()
Destroy an ExecutionSession.
void runJITDispatchHandler(SendResultFunction SendResult, ExecutorAddr HandlerFnTagAddr, ArrayRef< char > ArgBuffer)
Run a registered jit-side wrapper function.
void deregisterResourceManager(ResourceManager &RM)
Deregister the given ResourceManager with this ExecutionSession.
ExecutionSession(std::unique_ptr< ExecutorProcessControl > EPC)
Construct an ExecutionSession with the given ExecutorProcessControl object.
decltype(auto) runSessionLocked(Func &&F)
Run the given lambda with the session mutex locked.
void dump(raw_ostream &OS)
Dump the state of all the JITDylibs in this session.
Error removeJITDylibs(std::vector< JITDylibSP > JDsToRemove)
Removes the given JITDylibs from the ExecutionSession.
Expected< JITDylib & > createJITDylib(std::string Name)
Add a new JITDylib to this ExecutionSession.
void dispatchTask(std::unique_ptr< Task > T)
Materialize the given unit.
Represents an address in the executor process.
Represents a defining location for a JIT symbol.
FailedToMaterialize(std::shared_ptr< SymbolStringPool > SSP, std::shared_ptr< SymbolDependenceMap > Symbols)
std::error_code convertToErrorCode() const override
Convert this error to a std::error_code.
void log(raw_ostream &OS) const override
Print an error message to an output stream.
InProgressFullLookupState(LookupKind K, JITDylibSearchOrder SearchOrder, SymbolLookupSet LookupSet, SymbolState RequiredState, std::shared_ptr< AsynchronousSymbolQuery > Q, RegisterDependenciesFunction RegisterDependencies)
void complete(std::unique_ptr< InProgressLookupState > IPLS) override
void fail(Error Err) override
void complete(std::unique_ptr< InProgressLookupState > IPLS) override
void fail(Error Err) override
InProgressLookupFlagsState(LookupKind K, JITDylibSearchOrder SearchOrder, SymbolLookupSet LookupSet, unique_function< void(Expected< SymbolFlagsMap >)> OnComplete)
virtual ~InProgressLookupState()=default
SymbolLookupSet DefGeneratorCandidates
JITDylibSearchOrder SearchOrder
std::vector< std::weak_ptr< DefinitionGenerator > > CurDefGeneratorStack
size_t CurSearchOrderIndex
enum llvm::orc::InProgressLookupState::@467 GenState
SymbolLookupSet LookupSet
virtual void complete(std::unique_ptr< InProgressLookupState > IPLS)=0
InProgressLookupState(LookupKind K, JITDylibSearchOrder SearchOrder, SymbolLookupSet LookupSet, SymbolState RequiredState)
SymbolState RequiredState
SymbolLookupSet DefGeneratorNonCandidates
virtual void fail(Error Err)=0
Represents a JIT'd dynamic library.
Error clear()
Calls remove on all trackers currently associated with this JITDylib.
Error define(std::unique_ptr< MaterializationUnitType > &&MU, ResourceTrackerSP RT=nullptr)
Define all symbols provided by the materialization unit to be part of this JITDylib.
ExecutionSession & getExecutionSession() const
Get a reference to the ExecutionSession for this JITDylib.
ResourceTrackerSP createResourceTracker()
Create a resource tracker for this JITDylib.
Expected< std::vector< JITDylibSP > > getReverseDFSLinkOrder()
Rteurn this JITDylib and its transitive dependencies in reverse DFS order based on linkage relationsh...
ResourceTrackerSP getDefaultResourceTracker()
Get the default resource tracker for this JITDylib.
void removeGenerator(DefinitionGenerator &G)
Remove a definition generator from this JITDylib.
Expected< std::vector< JITDylibSP > > getDFSLinkOrder()
Return this JITDylib and its transitive dependencies in DFS order based on linkage relationships.
Wraps state for a lookup-in-progress.
void continueLookup(Error Err)
Continue the lookup.
LookupState & operator=(LookupState &&)
void printDescription(raw_ostream &OS) override
Tracks responsibility for materialization, and mediates interactions between MaterializationUnits and...
void printDescription(raw_ostream &OS) override
A MaterializationUnit represents a set of symbol definitions that can be materialized as a group,...
SymbolFlagsMap SymbolFlags
std::error_code convertToErrorCode() const override
Convert this error to a std::error_code.
void log(raw_ostream &OS) const override
Print an error message to an output stream.
Non-owning SymbolStringPool entry pointer.
StringRef getName() const override
Return the name of this materialization unit.
ReExportsMaterializationUnit(JITDylib *SourceJD, JITDylibLookupFlags SourceJDLookupFlags, SymbolAliasMap Aliases)
SourceJD is allowed to be nullptr, in which case the source JITDylib is taken to be whatever JITDylib...
std::function< bool(SymbolStringPtr)> SymbolPredicate
Error tryToGenerate(LookupState &LS, LookupKind K, JITDylib &JD, JITDylibLookupFlags JDLookupFlags, const SymbolLookupSet &LookupSet) override
DefinitionGenerators should override this method to insert new definitions into the parent JITDylib.
ReexportsGenerator(JITDylib &SourceJD, JITDylibLookupFlags SourceJDLookupFlags, SymbolPredicate Allow=SymbolPredicate())
Create a reexports generator.
Listens for ResourceTracker operations.
virtual ~ResourceManager()
ResourceTrackerDefunct(ResourceTrackerSP RT)
void log(raw_ostream &OS) const override
Print an error message to an output stream.
std::error_code convertToErrorCode() const override
Convert this error to a std::error_code.
API to remove / transfer ownership of JIT resources.
JITDylib & getJITDylib() const
Return the JITDylib targeted by this tracker.
void transferTo(ResourceTracker &DstRT)
Transfer all resources associated with this key to the given tracker, which must target the same JITD...
ResourceTracker(const ResourceTracker &)=delete
Error remove()
Remove all resources associated with this key.
A set of symbols to look up, each associated with a SymbolLookupFlags value.
static SymbolLookupSet fromMapKeys(const DenseMap< SymbolStringPtr, ValT > &M, SymbolLookupFlags Flags=SymbolLookupFlags::RequiredSymbol)
Construct a SymbolLookupSet from DenseMap keys.
Pointer to a pooled string representing a symbol name.
std::error_code convertToErrorCode() const override
Convert this error to a std::error_code.
void log(raw_ostream &OS) const override
Print an error message to an output stream.
SymbolsCouldNotBeRemoved(std::shared_ptr< SymbolStringPool > SSP, SymbolNameSet Symbols)
void log(raw_ostream &OS) const override
Print an error message to an output stream.
SymbolsNotFound(std::shared_ptr< SymbolStringPool > SSP, SymbolNameSet Symbols)
std::error_code convertToErrorCode() const override
Convert this error to a std::error_code.
Represents an abstract task for ORC to run.
std::error_code convertToErrorCode() const override
Convert this error to a std::error_code.
void log(raw_ostream &OS) const override
Print an error message to an output stream.
void log(raw_ostream &OS) const override
Print an error message to an output stream.
std::error_code convertToErrorCode() const override
Convert this error to a std::error_code.
UnsatisfiedSymbolDependencies(std::shared_ptr< SymbolStringPool > SSP, JITDylibSP JD, SymbolNameSet FailedSymbols, SymbolDependenceMap BadDeps, std::string Explanation)
static WrapperFunctionResult createOutOfBandError(const char *Msg)
Create an out-of-band error by copying the given string.
A raw_ostream that writes to a file descriptor.
This class implements an extremely fast bulk output stream that can only output to a stream.
IntrusiveRefCntPtr< JITDylib > JITDylibSP
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...
std::vector< std::pair< JITDylib *, JITDylibLookupFlags > > JITDylibSearchOrder
A list of (JITDylib*, JITDylibLookupFlags) pairs to be used as a search order during symbol lookup.
std::function< void(const SymbolDependenceMap &)> RegisterDependenciesFunction
Callback to register the dependencies for a given query.
@ MissingSymbolDefinitions
@ UnexpectedSymbolDefinitions
std::unique_ptr< ReExportsMaterializationUnit > symbolAliases(SymbolAliasMap Aliases)
Create a ReExportsMaterializationUnit with the given aliases.
SymbolLookupFlags
Lookup flags that apply to each symbol in a lookup.
std::unique_ptr< ReExportsMaterializationUnit > reexports(JITDylib &SourceJD, SymbolAliasMap Aliases, JITDylibLookupFlags SourceJDLookupFlags=JITDylibLookupFlags::MatchExportedSymbolsOnly)
Create a materialization unit for re-exporting symbols from another JITDylib with alternative names/f...
DenseMap< SymbolStringPtr, SymbolAliasMapEntry > SymbolAliasMap
A map of Symbols to (Symbol, Flags) pairs.
JITDylibLookupFlags
Lookup flags that apply to each dylib in the search order for a lookup.
@ MatchExportedSymbolsOnly
DenseMap< SymbolStringPtr, ExecutorSymbolDef > SymbolMap
A map from symbol names (as SymbolStringPtrs) to JITSymbols (address/flags pairs).
DenseMap< SymbolStringPtr, JITSymbolFlags > SymbolFlagsMap
A map from symbol names (as SymbolStringPtrs) to JITSymbolFlags.
unique_function< void(Expected< SymbolMap >)> SymbolsResolvedCallback
Callback to notify client that symbols have been resolved.
DenseSet< SymbolStringPtr > SymbolNameSet
A set of symbol names (represented by SymbolStringPtrs for.
LookupKind
Describes the kind of lookup being performed.
RegisterDependenciesFunction NoDependenciesToRegister
This can be used as the value for a RegisterDependenciesFunction if there are no dependants to regist...
std::vector< SymbolStringPtr > SymbolNameVector
A vector of symbol names.
SymbolState
Represents the state that a symbol has reached during materialization.
@ Materializing
Added to the symbol table, never queried.
@ NeverSearched
No symbol should be in this state.
@ Ready
Emitted to memory, but waiting on transitive dependencies.
@ Emitted
Assigned address, still materializing.
@ Resolved
Queried, materialization begun.
std::error_code orcError(OrcErrorCode ErrCode)
DenseMap< JITDylib *, SymbolNameSet > SymbolDependenceMap
A map from JITDylibs to sets of symbols.
Expected< SymbolAliasMap > buildSimpleReexportsAliasMap(JITDylib &SourceJD, const SymbolNameSet &Symbols)
Build a SymbolAliasMap for the common case where you want to re-export symbols from another JITDylib ...
NodeAddr< DefNode * > Def
This is an optimization pass for GlobalISel generic memory operations.
auto find(R &&Range, const T &Val)
Provide wrappers to std::find which take ranges instead of having to pass begin/end explicitly.
std::error_code inconvertibleErrorCode()
The value returned by this function can be returned from convertToErrorCode for Error values where no...
auto formatv(const char *Fmt, Ts &&...Vals) -> formatv_object< decltype(std::make_tuple(support::detail::build_format_adapter(std::forward< Ts >(Vals))...))>
void append_range(Container &C, Range &&R)
Wrapper function to append range R to container C.
auto reverse(ContainerTy &&C)
Error joinErrors(Error E1, Error E2)
Concatenate errors.
raw_ostream & dbgs()
dbgs() - This returns a reference to a raw_ostream for debugging messages.
format_object< Ts... > format(const char *Fmt, const Ts &... Vals)
These are helper functions used to produce formatted output.
raw_fd_ostream & errs()
This returns a reference to a raw_ostream for standard error.
auto lower_bound(R &&Range, T &&Value)
Provide wrappers to std::lower_bound which take ranges instead of having to pass begin/end explicitly...
OutputIt move(R &&Range, OutputIt Out)
Provide wrappers to std::move which take ranges instead of having to pass begin/end explicitly.
auto find_if(R &&Range, UnaryPredicate P)
Provide wrappers to std::find_if which take ranges instead of having to pass begin/end explicitly.
bool is_contained(R &&Range, const E &Element)
Returns true if Element is found in Range.
Implement std::hash so that hash_code can be used in STL containers.
void swap(llvm::BitVector &LHS, llvm::BitVector &RHS)
Implement std::swap in terms of BitVector swap.