LLVM 19.0.0git
Core.cpp
Go to the documentation of this file.
1//===--- Core.cpp - Core ORC APIs (MaterializationUnit, JITDylib, etc.) ---===//
2//
3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6//
7//===----------------------------------------------------------------------===//
8
10
11#include "llvm/ADT/STLExtras.h"
12#include "llvm/Config/llvm-config.h"
17
18#include <condition_variable>
19#include <future>
20#include <optional>
21
22#define DEBUG_TYPE "orc"
23
24namespace llvm {
25namespace orc {
26
29char SymbolsNotFound::ID = 0;
35char LookupTask::ID = 0;
36
39
40void MaterializationUnit::anchor() {}
41
43 assert((reinterpret_cast<uintptr_t>(JD.get()) & 0x1) == 0 &&
44 "JITDylib must be two byte aligned");
45 JD->Retain();
46 JDAndFlag.store(reinterpret_cast<uintptr_t>(JD.get()));
47}
48
50 getJITDylib().getExecutionSession().destroyResourceTracker(*this);
52}
53
55 return getJITDylib().getExecutionSession().removeResourceTracker(*this);
56}
57
59 getJITDylib().getExecutionSession().transferResourceTracker(DstRT, *this);
60}
61
62void ResourceTracker::makeDefunct() {
63 uintptr_t Val = JDAndFlag.load();
64 Val |= 0x1U;
65 JDAndFlag.store(Val);
66}
67
69
71 : RT(std::move(RT)) {}
72
75}
76
78 OS << "Resource tracker " << (void *)RT.get() << " became defunct";
79}
80
82 std::shared_ptr<SymbolStringPool> SSP,
83 std::shared_ptr<SymbolDependenceMap> Symbols)
84 : SSP(std::move(SSP)), Symbols(std::move(Symbols)) {
85 assert(this->SSP && "String pool cannot be null");
86 assert(!this->Symbols->empty() && "Can not fail to resolve an empty set");
87
88 // FIXME: Use a new dep-map type for FailedToMaterialize errors so that we
89 // don't have to manually retain/release.
90 for (auto &KV : *this->Symbols)
91 KV.first->Retain();
92}
93
95 for (auto &KV : *Symbols)
96 KV.first->Release();
97}
98
101}
102
104 OS << "Failed to materialize symbols: " << *Symbols;
105}
106
108 std::shared_ptr<SymbolStringPool> SSP, JITDylibSP JD,
109 SymbolNameSet FailedSymbols, SymbolDependenceMap BadDeps,
110 std::string Explanation)
111 : SSP(std::move(SSP)), JD(std::move(JD)),
112 FailedSymbols(std::move(FailedSymbols)), BadDeps(std::move(BadDeps)),
113 Explanation(std::move(Explanation)) {}
114
117}
118
120 OS << "In " << JD->getName() << ", failed to materialize " << FailedSymbols
121 << ", due to unsatisfied dependencies " << BadDeps;
122 if (!Explanation.empty())
123 OS << " (" << Explanation << ")";
124}
125
126SymbolsNotFound::SymbolsNotFound(std::shared_ptr<SymbolStringPool> SSP,
127 SymbolNameSet Symbols)
128 : SSP(std::move(SSP)) {
129 for (auto &Sym : Symbols)
130 this->Symbols.push_back(Sym);
131 assert(!this->Symbols.empty() && "Can not fail to resolve an empty set");
132}
133
134SymbolsNotFound::SymbolsNotFound(std::shared_ptr<SymbolStringPool> SSP,
135 SymbolNameVector Symbols)
136 : SSP(std::move(SSP)), Symbols(std::move(Symbols)) {
137 assert(!this->Symbols.empty() && "Can not fail to resolve an empty set");
138}
139
140std::error_code SymbolsNotFound::convertToErrorCode() const {
142}
143
145 OS << "Symbols not found: " << Symbols;
146}
147
149 std::shared_ptr<SymbolStringPool> SSP, SymbolNameSet Symbols)
150 : SSP(std::move(SSP)), Symbols(std::move(Symbols)) {
151 assert(!this->Symbols.empty() && "Can not fail to resolve an empty set");
152}
153
156}
157
159 OS << "Symbols could not be removed: " << Symbols;
160}
161
164}
165
167 OS << "Missing definitions in module " << ModuleName
168 << ": " << Symbols;
169}
170
173}
174
176 OS << "Unexpected definitions in module " << ModuleName
177 << ": " << Symbols;
178}
179
181 const SymbolLookupSet &Symbols, SymbolState RequiredState,
182 SymbolsResolvedCallback NotifyComplete)
183 : NotifyComplete(std::move(NotifyComplete)), RequiredState(RequiredState) {
184 assert(RequiredState >= SymbolState::Resolved &&
185 "Cannot query for a symbols that have not reached the resolve state "
186 "yet");
187
188 OutstandingSymbolsCount = Symbols.size();
189
190 for (auto &KV : Symbols)
191 ResolvedSymbols[KV.first] = ExecutorSymbolDef();
192}
193
196 auto I = ResolvedSymbols.find(Name);
197 assert(I != ResolvedSymbols.end() &&
198 "Resolving symbol outside the requested set");
199 assert(I->second == ExecutorSymbolDef() &&
200 "Redundantly resolving symbol Name");
201
202 // If this is a materialization-side-effects-only symbol then drop it,
203 // otherwise update its map entry with its resolved address.
204 if (Sym.getFlags().hasMaterializationSideEffectsOnly())
205 ResolvedSymbols.erase(I);
206 else
207 I->second = std::move(Sym);
208 --OutstandingSymbolsCount;
209}
210
211void AsynchronousSymbolQuery::handleComplete(ExecutionSession &ES) {
212 assert(OutstandingSymbolsCount == 0 &&
213 "Symbols remain, handleComplete called prematurely");
214
215 class RunQueryCompleteTask : public Task {
216 public:
217 RunQueryCompleteTask(SymbolMap ResolvedSymbols,
218 SymbolsResolvedCallback NotifyComplete)
219 : ResolvedSymbols(std::move(ResolvedSymbols)),
220 NotifyComplete(std::move(NotifyComplete)) {}
221 void printDescription(raw_ostream &OS) override {
222 OS << "Execute query complete callback for " << ResolvedSymbols;
223 }
224 void run() override { NotifyComplete(std::move(ResolvedSymbols)); }
225
226 private:
227 SymbolMap ResolvedSymbols;
228 SymbolsResolvedCallback NotifyComplete;
229 };
230
231 auto T = std::make_unique<RunQueryCompleteTask>(std::move(ResolvedSymbols),
232 std::move(NotifyComplete));
233 NotifyComplete = SymbolsResolvedCallback();
234 ES.dispatchTask(std::move(T));
235}
236
237void AsynchronousSymbolQuery::handleFailed(Error Err) {
238 assert(QueryRegistrations.empty() && ResolvedSymbols.empty() &&
239 OutstandingSymbolsCount == 0 &&
240 "Query should already have been abandoned");
241 NotifyComplete(std::move(Err));
242 NotifyComplete = SymbolsResolvedCallback();
243}
244
245void AsynchronousSymbolQuery::addQueryDependence(JITDylib &JD,
246 SymbolStringPtr Name) {
247 bool Added = QueryRegistrations[&JD].insert(std::move(Name)).second;
248 (void)Added;
249 assert(Added && "Duplicate dependence notification?");
250}
251
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);
261}
262
263void AsynchronousSymbolQuery::dropSymbol(const SymbolStringPtr &Name) {
264 auto I = ResolvedSymbols.find(Name);
265 assert(I != ResolvedSymbols.end() &&
266 "Redundant removal of weakly-referenced symbol");
267 ResolvedSymbols.erase(I);
268 --OutstandingSymbolsCount;
269}
270
271void AsynchronousSymbolQuery::detach() {
272 ResolvedSymbols.clear();
273 OutstandingSymbolsCount = 0;
274 for (auto &KV : QueryRegistrations)
275 KV.first->detachQueryHelper(*this, KV.second);
276 QueryRegistrations.clear();
277}
278
280 SymbolMap Symbols)
281 : MaterializationUnit(extractFlags(Symbols)), Symbols(std::move(Symbols)) {}
282
284 return "<Absolute Symbols>";
285}
286
287void AbsoluteSymbolsMaterializationUnit::materialize(
288 std::unique_ptr<MaterializationResponsibility> R) {
289 // Even though these are just absolute symbols we need to check for failure
290 // to resolve/emit: the tracker for these symbols may have been removed while
291 // the materialization was in flight (e.g. due to a failure in some action
292 // triggered by the queries attached to the resolution/emission of these
293 // symbols).
294 if (auto Err = R->notifyResolved(Symbols)) {
295 R->getExecutionSession().reportError(std::move(Err));
296 R->failMaterialization();
297 return;
298 }
299 if (auto Err = R->notifyEmitted({})) {
300 R->getExecutionSession().reportError(std::move(Err));
301 R->failMaterialization();
302 return;
303 }
304}
305
306void AbsoluteSymbolsMaterializationUnit::discard(const JITDylib &JD,
307 const SymbolStringPtr &Name) {
308 assert(Symbols.count(Name) && "Symbol is not part of this MU");
309 Symbols.erase(Name);
310}
311
312MaterializationUnit::Interface
313AbsoluteSymbolsMaterializationUnit::extractFlags(const SymbolMap &Symbols) {
315 for (const auto &KV : Symbols)
316 Flags[KV.first] = KV.second.getFlags();
317 return MaterializationUnit::Interface(std::move(Flags), nullptr);
318}
319
321 JITDylib *SourceJD, JITDylibLookupFlags SourceJDLookupFlags,
322 SymbolAliasMap Aliases)
323 : MaterializationUnit(extractFlags(Aliases)), SourceJD(SourceJD),
324 SourceJDLookupFlags(SourceJDLookupFlags), Aliases(std::move(Aliases)) {}
325
327 return "<Reexports>";
328}
329
330void ReExportsMaterializationUnit::materialize(
331 std::unique_ptr<MaterializationResponsibility> R) {
332
333 auto &ES = R->getTargetJITDylib().getExecutionSession();
334 JITDylib &TgtJD = R->getTargetJITDylib();
335 JITDylib &SrcJD = SourceJD ? *SourceJD : TgtJD;
336
337 // Find the set of requested aliases and aliasees. Return any unrequested
338 // aliases back to the JITDylib so as to not prematurely materialize any
339 // aliasees.
340 auto RequestedSymbols = R->getRequestedSymbols();
341 SymbolAliasMap RequestedAliases;
342
343 for (auto &Name : RequestedSymbols) {
344 auto I = Aliases.find(Name);
345 assert(I != Aliases.end() && "Symbol not found in aliases map?");
346 RequestedAliases[Name] = std::move(I->second);
347 Aliases.erase(I);
348 }
349
350 LLVM_DEBUG({
351 ES.runSessionLocked([&]() {
352 dbgs() << "materializing reexports: target = " << TgtJD.getName()
353 << ", source = " << SrcJD.getName() << " " << RequestedAliases
354 << "\n";
355 });
356 });
357
358 if (!Aliases.empty()) {
359 auto Err = SourceJD ? R->replace(reexports(*SourceJD, std::move(Aliases),
360 SourceJDLookupFlags))
361 : R->replace(symbolAliases(std::move(Aliases)));
362
363 if (Err) {
364 // FIXME: Should this be reported / treated as failure to materialize?
365 // Or should this be treated as a sanctioned bailing-out?
366 ES.reportError(std::move(Err));
367 R->failMaterialization();
368 return;
369 }
370 }
371
372 // The OnResolveInfo struct will hold the aliases and responsibility for each
373 // query in the list.
374 struct OnResolveInfo {
375 OnResolveInfo(std::unique_ptr<MaterializationResponsibility> R,
376 SymbolAliasMap Aliases)
377 : R(std::move(R)), Aliases(std::move(Aliases)) {}
378
379 std::unique_ptr<MaterializationResponsibility> R;
380 SymbolAliasMap Aliases;
381 std::vector<SymbolDependenceGroup> SDGs;
382 };
383
384 // Build a list of queries to issue. In each round we build a query for the
385 // largest set of aliases that we can resolve without encountering a chain of
386 // aliases (e.g. Foo -> Bar, Bar -> Baz). Such a chain would deadlock as the
387 // query would be waiting on a symbol that it itself had to resolve. Creating
388 // a new query for each link in such a chain eliminates the possibility of
389 // deadlock. In practice chains are likely to be rare, and this algorithm will
390 // usually result in a single query to issue.
391
392 std::vector<std::pair<SymbolLookupSet, std::shared_ptr<OnResolveInfo>>>
393 QueryInfos;
394 while (!RequestedAliases.empty()) {
395 SymbolNameSet ResponsibilitySymbols;
396 SymbolLookupSet QuerySymbols;
397 SymbolAliasMap QueryAliases;
398
399 // Collect as many aliases as we can without including a chain.
400 for (auto &KV : RequestedAliases) {
401 // Chain detected. Skip this symbol for this round.
402 if (&SrcJD == &TgtJD && (QueryAliases.count(KV.second.Aliasee) ||
403 RequestedAliases.count(KV.second.Aliasee)))
404 continue;
405
406 ResponsibilitySymbols.insert(KV.first);
407 QuerySymbols.add(KV.second.Aliasee,
408 KV.second.AliasFlags.hasMaterializationSideEffectsOnly()
411 QueryAliases[KV.first] = std::move(KV.second);
412 }
413
414 // Remove the aliases collected this round from the RequestedAliases map.
415 for (auto &KV : QueryAliases)
416 RequestedAliases.erase(KV.first);
417
418 assert(!QuerySymbols.empty() && "Alias cycle detected!");
419
420 auto NewR = R->delegate(ResponsibilitySymbols);
421 if (!NewR) {
422 ES.reportError(NewR.takeError());
423 R->failMaterialization();
424 return;
425 }
426
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)));
431 }
432
433 // Issue the queries.
434 while (!QueryInfos.empty()) {
435 auto QuerySymbols = std::move(QueryInfos.back().first);
436 auto QueryInfo = std::move(QueryInfos.back().second);
437
438 QueryInfos.pop_back();
439
440 auto RegisterDependencies = [QueryInfo,
441 &SrcJD](const SymbolDependenceMap &Deps) {
442 // If there were no materializing symbols, just bail out.
443 if (Deps.empty())
444 return;
445
446 // Otherwise the only deps should be on SrcJD.
447 assert(Deps.size() == 1 && Deps.count(&SrcJD) &&
448 "Unexpected dependencies for reexports");
449
450 auto &SrcJDDeps = Deps.find(&SrcJD)->second;
451
452 for (auto &[Alias, AliasInfo] : QueryInfo->Aliases)
453 if (SrcJDDeps.count(AliasInfo.Aliasee))
454 QueryInfo->SDGs.push_back({{Alias}, {{&SrcJD, {AliasInfo.Aliasee}}}});
455 };
456
457 auto OnComplete = [QueryInfo](Expected<SymbolMap> Result) {
458 auto &ES = QueryInfo->R->getTargetJITDylib().getExecutionSession();
459 if (Result) {
460 SymbolMap ResolutionMap;
461 for (auto &KV : QueryInfo->Aliases) {
462 assert((KV.second.AliasFlags.hasMaterializationSideEffectsOnly() ||
463 Result->count(KV.second.Aliasee)) &&
464 "Result map missing entry?");
465 // Don't try to resolve materialization-side-effects-only symbols.
466 if (KV.second.AliasFlags.hasMaterializationSideEffectsOnly())
467 continue;
468
469 ResolutionMap[KV.first] = {(*Result)[KV.second.Aliasee].getAddress(),
470 KV.second.AliasFlags};
471 }
472 if (auto Err = QueryInfo->R->notifyResolved(ResolutionMap)) {
473 ES.reportError(std::move(Err));
474 QueryInfo->R->failMaterialization();
475 return;
476 }
477 if (auto Err = QueryInfo->R->notifyEmitted(QueryInfo->SDGs)) {
478 ES.reportError(std::move(Err));
479 QueryInfo->R->failMaterialization();
480 return;
481 }
482 } else {
483 ES.reportError(Result.takeError());
484 QueryInfo->R->failMaterialization();
485 }
486 };
487
489 JITDylibSearchOrder({{&SrcJD, SourceJDLookupFlags}}),
490 QuerySymbols, SymbolState::Resolved, std::move(OnComplete),
491 std::move(RegisterDependencies));
492 }
493}
494
495void ReExportsMaterializationUnit::discard(const JITDylib &JD,
496 const SymbolStringPtr &Name) {
497 assert(Aliases.count(Name) &&
498 "Symbol not covered by this MaterializationUnit");
499 Aliases.erase(Name);
500}
501
502MaterializationUnit::Interface
503ReExportsMaterializationUnit::extractFlags(const SymbolAliasMap &Aliases) {
505 for (auto &KV : Aliases)
506 SymbolFlags[KV.first] = KV.second.AliasFlags;
507
508 return MaterializationUnit::Interface(std::move(SymbolFlags), nullptr);
509}
510
512 SymbolNameSet Symbols) {
513 SymbolLookupSet LookupSet(Symbols);
514 auto Flags = SourceJD.getExecutionSession().lookupFlags(
516 SymbolLookupSet(std::move(Symbols)));
517
518 if (!Flags)
519 return Flags.takeError();
520
522 for (auto &Name : Symbols) {
523 assert(Flags->count(Name) && "Missing entry in flags map");
524 Result[Name] = SymbolAliasMapEntry(Name, (*Flags)[Name]);
525 }
526
527 return Result;
528}
529
531public:
532 // FIXME: Reduce the number of SymbolStringPtrs here. See
533 // https://github.com/llvm/llvm-project/issues/55576.
534
540 }
541 virtual ~InProgressLookupState() = default;
542 virtual void complete(std::unique_ptr<InProgressLookupState> IPLS) = 0;
543 virtual void fail(Error Err) = 0;
544
549
551 bool NewJITDylib = true;
554
555 enum {
556 NotInGenerator, // Not currently using a generator.
557 ResumedForGenerator, // Resumed after being auto-suspended before generator.
558 InGenerator // Currently using generator.
560 std::vector<std::weak_ptr<DefinitionGenerator>> CurDefGeneratorStack;
561};
562
564public:
570 OnComplete(std::move(OnComplete)) {}
571
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));
575 }
576
577 void fail(Error Err) override { OnComplete(std::move(Err)); }
578
579private:
581};
582
584public:
588 std::shared_ptr<AsynchronousSymbolQuery> Q,
589 RegisterDependenciesFunction RegisterDependencies)
592 Q(std::move(Q)), RegisterDependencies(std::move(RegisterDependencies)) {
593 }
594
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));
599 }
600
601 void fail(Error Err) override {
602 Q->detach();
603 Q->handleFailed(std::move(Err));
604 }
605
606private:
607 std::shared_ptr<AsynchronousSymbolQuery> Q;
608 RegisterDependenciesFunction RegisterDependencies;
609};
610
612 JITDylibLookupFlags SourceJDLookupFlags,
613 SymbolPredicate Allow)
614 : SourceJD(SourceJD), SourceJDLookupFlags(SourceJDLookupFlags),
615 Allow(std::move(Allow)) {}
616
618 JITDylib &JD,
619 JITDylibLookupFlags JDLookupFlags,
620 const SymbolLookupSet &LookupSet) {
621 assert(&JD != &SourceJD && "Cannot re-export from the same dylib");
622
623 // Use lookupFlags to find the subset of symbols that match our lookup.
624 auto Flags = JD.getExecutionSession().lookupFlags(
625 K, {{&SourceJD, JDLookupFlags}}, LookupSet);
626 if (!Flags)
627 return Flags.takeError();
628
629 // Create an alias map.
630 orc::SymbolAliasMap AliasMap;
631 for (auto &KV : *Flags)
632 if (!Allow || Allow(KV.first))
633 AliasMap[KV.first] = SymbolAliasMapEntry(KV.first, KV.second);
634
635 if (AliasMap.empty())
636 return Error::success();
637
638 // Define the re-exports.
639 return JD.define(reexports(SourceJD, AliasMap, SourceJDLookupFlags));
640}
641
642LookupState::LookupState(std::unique_ptr<InProgressLookupState> IPLS)
643 : IPLS(std::move(IPLS)) {}
644
645void LookupState::reset(InProgressLookupState *IPLS) { this->IPLS.reset(IPLS); }
646
647LookupState::LookupState() = default;
648LookupState::LookupState(LookupState &&) = default;
649LookupState &LookupState::operator=(LookupState &&) = default;
650LookupState::~LookupState() = default;
651
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));
656}
657
659 std::deque<LookupState> LookupsToFail;
660 {
661 std::lock_guard<std::mutex> Lock(M);
662 std::swap(PendingLookups, LookupsToFail);
663 InUse = false;
664 }
665
666 for (auto &LS : LookupsToFail)
667 LS.continueLookup(make_error<StringError>(
668 "Query waiting on DefinitionGenerator that was destroyed",
670}
671
673 LLVM_DEBUG(dbgs() << "Destroying JITDylib " << getName() << "\n");
674}
675
677 std::vector<ResourceTrackerSP> TrackersToRemove;
678 ES.runSessionLocked([&]() {
679 assert(State != Closed && "JD is defunct");
680 for (auto &KV : TrackerSymbols)
681 TrackersToRemove.push_back(KV.first);
682 TrackersToRemove.push_back(getDefaultResourceTracker());
683 });
684
685 Error Err = Error::success();
686 for (auto &RT : TrackersToRemove)
687 Err = joinErrors(std::move(Err), RT->remove());
688 return Err;
689}
690
692 return ES.runSessionLocked([this] {
693 assert(State != Closed && "JD is defunct");
694 if (!DefaultTracker)
695 DefaultTracker = new ResourceTracker(this);
696 return DefaultTracker;
697 });
698}
699
701 return ES.runSessionLocked([this] {
702 assert(State == Open && "JD is defunct");
703 ResourceTrackerSP RT = new ResourceTracker(this);
704 return RT;
705 });
706}
707
709 // DefGenerator moved into TmpDG to ensure that it's destroyed outside the
710 // session lock (since it may have to send errors to pending queries).
711 std::shared_ptr<DefinitionGenerator> TmpDG;
712
713 ES.runSessionLocked([&] {
714 assert(State == Open && "JD is defunct");
715 auto I = llvm::find_if(DefGenerators,
716 [&](const std::shared_ptr<DefinitionGenerator> &H) {
717 return H.get() == &G;
718 });
719 assert(I != DefGenerators.end() && "Generator not found");
720 TmpDG = std::move(*I);
721 DefGenerators.erase(I);
722 });
723}
724
726JITDylib::defineMaterializing(MaterializationResponsibility &FromMR,
727 SymbolFlagsMap SymbolFlags) {
728
729 return ES.runSessionLocked([&]() -> Expected<SymbolFlagsMap> {
730 if (FromMR.RT->isDefunct())
731 return make_error<ResourceTrackerDefunct>(FromMR.RT);
732
733 std::vector<NonOwningSymbolStringPtr> AddedSyms;
734 std::vector<NonOwningSymbolStringPtr> RejectedWeakDefs;
735
736 for (auto SFItr = SymbolFlags.begin(), SFEnd = SymbolFlags.end();
737 SFItr != SFEnd; ++SFItr) {
738
739 auto &Name = SFItr->first;
740 auto &Flags = SFItr->second;
741
742 auto EntryItr = Symbols.find(Name);
743
744 // If the entry already exists...
745 if (EntryItr != Symbols.end()) {
746
747 // If this is a strong definition then error out.
748 if (!Flags.isWeak()) {
749 // Remove any symbols already added.
750 for (auto &S : AddedSyms)
751 Symbols.erase(Symbols.find_as(S));
752
753 // FIXME: Return all duplicates.
754 return make_error<DuplicateDefinition>(std::string(*Name));
755 }
756
757 // Otherwise just make a note to discard this symbol after the loop.
758 RejectedWeakDefs.push_back(NonOwningSymbolStringPtr(Name));
759 continue;
760 } else
761 EntryItr =
762 Symbols.insert(std::make_pair(Name, SymbolTableEntry(Flags))).first;
763
764 AddedSyms.push_back(NonOwningSymbolStringPtr(Name));
765 EntryItr->second.setState(SymbolState::Materializing);
766 }
767
768 // Remove any rejected weak definitions from the SymbolFlags map.
769 while (!RejectedWeakDefs.empty()) {
770 SymbolFlags.erase(SymbolFlags.find_as(RejectedWeakDefs.back()));
771 RejectedWeakDefs.pop_back();
772 }
773
774 return SymbolFlags;
775 });
776}
777
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;
783
784 auto Err =
785 ES.runSessionLocked([&, this]() -> Error {
786 if (FromMR.RT->isDefunct())
787 return make_error<ResourceTrackerDefunct>(std::move(FromMR.RT));
788
789#ifndef NDEBUG
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");
799 }
800#endif // NDEBUG
801
802 // If the tracker is defunct we need to bail out immediately.
803
804 // If any symbol has pending queries against it then we need to
805 // materialize MU immediately.
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();
815 }
816 }
817 }
818
819 // Otherwise, make MU responsible for all the symbols.
820 auto UMI = std::make_shared<UnmaterializedInfo>(std::move(MU),
821 FromMR.RT.get());
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);
832
833 auto &UMIEntry = UnmaterializedInfos[KV.first];
834 assert((!UMIEntry || !UMIEntry->MU) &&
835 "Replacing symbol with materializer still attached");
836 UMIEntry = UMI;
837 }
838
839 return Error::success();
840 });
841
842 if (Err)
843 return Err;
844
845 if (MustRunMU) {
846 assert(MustRunMR && "MustRunMU set implies MustRunMR set");
847 ES.dispatchTask(std::make_unique<MaterializationTask>(
848 std::move(MustRunMU), std::move(MustRunMR)));
849 } else {
850 assert(!MustRunMR && "MustRunMU unset implies MustRunMR unset");
851 }
852
853 return Error::success();
854}
855
856Expected<std::unique_ptr<MaterializationResponsibility>>
857JITDylib::delegate(MaterializationResponsibility &FromMR,
858 SymbolFlagsMap SymbolFlags, SymbolStringPtr InitSymbol) {
859
860 return ES.runSessionLocked(
861 [&]() -> Expected<std::unique_ptr<MaterializationResponsibility>> {
862 if (FromMR.RT->isDefunct())
863 return make_error<ResourceTrackerDefunct>(std::move(FromMR.RT));
864
865 return ES.createMaterializationResponsibility(
866 *FromMR.RT, std::move(SymbolFlags), std::move(InitSymbol));
867 });
868}
869
871JITDylib::getRequestedSymbols(const SymbolFlagsMap &SymbolFlags) const {
872 return ES.runSessionLocked([&]() {
873 SymbolNameSet RequestedSymbols;
874
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())
884 continue;
885
886 if (I->second.hasQueriesPending())
887 RequestedSymbols.insert(KV.first);
888 }
889
890 return RequestedSymbols;
891 });
892}
893
894Error JITDylib::resolve(MaterializationResponsibility &MR,
895 const SymbolMap &Resolved) {
896 AsynchronousSymbolQuerySet CompletedQueries;
897
898 if (auto Err = ES.runSessionLocked([&, this]() -> Error {
899 if (MR.RT->isDefunct())
900 return make_error<ResourceTrackerDefunct>(MR.RT);
901
902 if (State != Open)
903 return make_error<StringError>("JITDylib " + getName() +
904 " is defunct",
905 inconvertibleErrorCode());
906
907 struct WorklistEntry {
908 SymbolTable::iterator SymI;
909 ExecutorSymbolDef ResolvedSym;
910 };
911
912 SymbolNameSet SymbolsInErrorState;
913 std::vector<WorklistEntry> Worklist;
914 Worklist.reserve(Resolved.size());
915
916 // Build worklist and check for any symbols in the error state.
917 for (const auto &KV : Resolved) {
918
919 assert(!KV.second.getFlags().hasError() &&
920 "Resolution result can not have error flag set");
921
922 auto SymI = Symbols.find(KV.first);
923
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");
931
932 if (SymI->second.getFlags().hasError())
933 SymbolsInErrorState.insert(KV.first);
934 else {
935 auto Flags = KV.second.getFlags();
936 Flags &= ~JITSymbolFlags::Common;
937 assert(Flags ==
938 (SymI->second.getFlags() & ~JITSymbolFlags::Common) &&
939 "Resolved flags should match the declared flags");
940
941 Worklist.push_back({SymI, {KV.second.getAddress(), Flags}});
942 }
943 }
944
945 // If any symbols were in the error state then bail out.
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));
952 }
953
954 while (!Worklist.empty()) {
955 auto SymI = Worklist.back().SymI;
956 auto ResolvedSym = Worklist.back().ResolvedSym;
957 Worklist.pop_back();
958
959 auto &Name = SymI->first;
960
961 // Resolved symbols can not be weak: discard the weak flag.
962 JITSymbolFlags ResolvedFlags = ResolvedSym.getFlags();
963 SymI->second.setAddress(ResolvedSym.getAddress());
964 SymI->second.setFlags(ResolvedFlags);
965 SymI->second.setState(SymbolState::Resolved);
966
967 auto MII = MaterializingInfos.find(Name);
968 if (MII == MaterializingInfos.end())
969 continue;
970
971 auto &MI = MII->second;
972 for (auto &Q : MI.takeQueriesMeeting(SymbolState::Resolved)) {
973 Q->notifySymbolMetRequiredState(Name, ResolvedSym);
974 Q->removeQueryDependence(*this, Name);
975 if (Q->isComplete())
976 CompletedQueries.insert(std::move(Q));
977 }
978 }
979
980 return Error::success();
981 }))
982 return Err;
983
984 // Otherwise notify all the completed queries.
985 for (auto &Q : CompletedQueries) {
986 assert(Q->isComplete() && "Q not completed");
987 Q->handleComplete(ES);
988 }
989
990 return Error::success();
991}
992
993void JITDylib::unlinkMaterializationResponsibility(
994 MaterializationResponsibility &MR) {
995 ES.runSessionLocked([&]() {
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());
1002 });
1003}
1004
1005void JITDylib::setLinkOrder(JITDylibSearchOrder NewLinkOrder,
1006 bool LinkAgainstThisJITDylibFirst) {
1007 ES.runSessionLocked([&]() {
1008 assert(State == Open && "JD is defunct");
1009 if (LinkAgainstThisJITDylibFirst) {
1010 LinkOrder.clear();
1011 if (NewLinkOrder.empty() || NewLinkOrder.front().first != this)
1012 LinkOrder.push_back(
1013 std::make_pair(this, JITDylibLookupFlags::MatchAllSymbols));
1014 llvm::append_range(LinkOrder, NewLinkOrder);
1015 } else
1016 LinkOrder = std::move(NewLinkOrder);
1017 });
1018}
1019
1020void JITDylib::addToLinkOrder(const JITDylibSearchOrder &NewLinks) {
1021 ES.runSessionLocked([&]() {
1022 for (auto &KV : NewLinks) {
1023 // Skip elements of NewLinks that are already in the link order.
1024 if (llvm::is_contained(LinkOrder, KV))
1025 continue;
1026
1027 LinkOrder.push_back(std::move(KV));
1028 }
1029 });
1030}
1031
1032void JITDylib::addToLinkOrder(JITDylib &JD, JITDylibLookupFlags JDLookupFlags) {
1033 ES.runSessionLocked([&]() { LinkOrder.push_back({&JD, JDLookupFlags}); });
1034}
1035
1036void JITDylib::replaceInLinkOrder(JITDylib &OldJD, JITDylib &NewJD,
1037 JITDylibLookupFlags JDLookupFlags) {
1038 ES.runSessionLocked([&]() {
1039 assert(State == Open && "JD is defunct");
1040 for (auto &KV : LinkOrder)
1041 if (KV.first == &OldJD) {
1042 KV = {&NewJD, JDLookupFlags};
1043 break;
1044 }
1045 });
1046}
1047
1048void JITDylib::removeFromLinkOrder(JITDylib &JD) {
1049 ES.runSessionLocked([&]() {
1050 assert(State == Open && "JD is defunct");
1051 auto I = llvm::find_if(LinkOrder,
1052 [&](const JITDylibSearchOrder::value_type &KV) {
1053 return KV.first == &JD;
1054 });
1055 if (I != LinkOrder.end())
1056 LinkOrder.erase(I);
1057 });
1058}
1059
1060Error JITDylib::remove(const SymbolNameSet &Names) {
1061 return ES.runSessionLocked([&]() -> Error {
1062 assert(State == Open && "JD is defunct");
1063 using SymbolMaterializerItrPair =
1064 std::pair<SymbolTable::iterator, UnmaterializedInfosMap::iterator>;
1065 std::vector<SymbolMaterializerItrPair> SymbolsToRemove;
1066 SymbolNameSet Missing;
1068
1069 for (auto &Name : Names) {
1070 auto I = Symbols.find(Name);
1071
1072 // Note symbol missing.
1073 if (I == Symbols.end()) {
1074 Missing.insert(Name);
1075 continue;
1076 }
1077
1078 // Note symbol materializing.
1079 if (I->second.getState() != SymbolState::NeverSearched &&
1080 I->second.getState() != SymbolState::Ready) {
1081 Materializing.insert(Name);
1082 continue;
1083 }
1084
1085 auto UMII = I->second.hasMaterializerAttached()
1086 ? UnmaterializedInfos.find(Name)
1087 : UnmaterializedInfos.end();
1088 SymbolsToRemove.push_back(std::make_pair(I, UMII));
1089 }
1090
1091 // If any of the symbols are not defined, return an error.
1092 if (!Missing.empty())
1093 return make_error<SymbolsNotFound>(ES.getSymbolStringPool(),
1094 std::move(Missing));
1095
1096 // If any of the symbols are currently materializing, return an error.
1097 if (!Materializing.empty())
1098 return make_error<SymbolsCouldNotBeRemoved>(ES.getSymbolStringPool(),
1099 std::move(Materializing));
1100
1101 // Remove the symbols.
1102 for (auto &SymbolMaterializerItrPair : SymbolsToRemove) {
1103 auto UMII = SymbolMaterializerItrPair.second;
1104
1105 // If there is a materializer attached, call discard.
1106 if (UMII != UnmaterializedInfos.end()) {
1107 UMII->second->MU->doDiscard(*this, UMII->first);
1108 UnmaterializedInfos.erase(UMII);
1109 }
1110
1111 auto SymI = SymbolMaterializerItrPair.first;
1112 Symbols.erase(SymI);
1113 }
1114
1115 return Error::success();
1116 });
1117}
1118
1119void JITDylib::dump(raw_ostream &OS) {
1120 ES.runSessionLocked([&, this]() {
1121 OS << "JITDylib \"" << getName() << "\" (ES: "
1122 << format("0x%016" PRIx64, reinterpret_cast<uintptr_t>(&ES))
1123 << ", State = ";
1124 switch (State) {
1125 case Open:
1126 OS << "Open";
1127 break;
1128 case Closing:
1129 OS << "Closing";
1130 break;
1131 case Closed:
1132 OS << "Closed";
1133 break;
1134 }
1135 OS << ")\n";
1136 if (State == Closed)
1137 return;
1138 OS << "Link order: " << LinkOrder << "\n"
1139 << "Symbol table:\n";
1140
1141 // Sort symbols so we get a deterministic order and can check them in tests.
1142 std::vector<std::pair<SymbolStringPtr, SymbolTableEntry *>> SymbolsSorted;
1143 for (auto &KV : Symbols)
1144 SymbolsSorted.emplace_back(KV.first, &KV.second);
1145 std::sort(SymbolsSorted.begin(), SymbolsSorted.end(),
1146 [](const auto &L, const auto &R) { return *L.first < *R.first; });
1147
1148 for (auto &KV : SymbolsSorted) {
1149 OS << " \"" << *KV.first << "\": ";
1150 if (auto Addr = KV.second->getAddress())
1151 OS << Addr;
1152 else
1153 OS << "<not resolved> ";
1154
1155 OS << " " << KV.second->getFlags() << " " << KV.second->getState();
1156
1157 if (KV.second->hasMaterializerAttached()) {
1158 OS << " (Materializer ";
1159 auto I = UnmaterializedInfos.find(KV.first);
1160 assert(I != UnmaterializedInfos.end() &&
1161 "Lazy symbol should have UnmaterializedInfo");
1162 OS << I->second->MU.get() << ", " << I->second->MU->getName() << ")\n";
1163 } else
1164 OS << "\n";
1165 }
1166
1167 if (!MaterializingInfos.empty())
1168 OS << " MaterializingInfos entries:\n";
1169 for (auto &KV : MaterializingInfos) {
1170 OS << " \"" << *KV.first << "\":\n"
1171 << " " << KV.second.pendingQueries().size()
1172 << " pending queries: { ";
1173 for (const auto &Q : KV.second.pendingQueries())
1174 OS << Q.get() << " (" << Q->getRequiredState() << ") ";
1175 OS << "}\n Defining EDU: ";
1176 if (KV.second.DefiningEDU) {
1177 OS << KV.second.DefiningEDU.get() << " { ";
1178 for (auto &[Name, Flags] : KV.second.DefiningEDU->Symbols)
1179 OS << Name << " ";
1180 OS << "}\n";
1181 OS << " Dependencies:\n";
1182 if (!KV.second.DefiningEDU->Dependencies.empty()) {
1183 for (auto &[DepJD, Deps] : KV.second.DefiningEDU->Dependencies) {
1184 OS << " " << DepJD->getName() << ": [ ";
1185 for (auto &Dep : Deps)
1186 OS << Dep << " ";
1187 OS << "]\n";
1188 }
1189 } else
1190 OS << " none\n";
1191 } else
1192 OS << "none\n";
1193 OS << " Dependant EDUs:\n";
1194 if (!KV.second.DependantEDUs.empty()) {
1195 for (auto &DependantEDU : KV.second.DependantEDUs) {
1196 OS << " " << DependantEDU << ": "
1197 << DependantEDU->JD->getName() << " { ";
1198 for (auto &[Name, Flags] : DependantEDU->Symbols)
1199 OS << Name << " ";
1200 OS << "}\n";
1201 }
1202 } else
1203 OS << " none\n";
1204 assert((Symbols[KV.first].getState() != SymbolState::Ready ||
1205 (KV.second.pendingQueries().empty() && !KV.second.DefiningEDU &&
1206 !KV.second.DependantEDUs.empty())) &&
1207 "Stale materializing info entry");
1208 }
1209 });
1210}
1211
1212void JITDylib::MaterializingInfo::addQuery(
1213 std::shared_ptr<AsynchronousSymbolQuery> Q) {
1214
1215 auto I = llvm::lower_bound(
1216 llvm::reverse(PendingQueries), Q->getRequiredState(),
1217 [](const std::shared_ptr<AsynchronousSymbolQuery> &V, SymbolState S) {
1218 return V->getRequiredState() <= S;
1219 });
1220 PendingQueries.insert(I.base(), std::move(Q));
1221}
1222
1223void JITDylib::MaterializingInfo::removeQuery(
1224 const AsynchronousSymbolQuery &Q) {
1225 // FIXME: Implement 'find_as' for shared_ptr<T>/T*.
1226 auto I = llvm::find_if(
1227 PendingQueries, [&Q](const std::shared_ptr<AsynchronousSymbolQuery> &V) {
1228 return V.get() == &Q;
1229 });
1230 assert(I != PendingQueries.end() &&
1231 "Query is not attached to this MaterializingInfo");
1232 PendingQueries.erase(I);
1233}
1234
1235JITDylib::AsynchronousSymbolQueryList
1236JITDylib::MaterializingInfo::takeQueriesMeeting(SymbolState RequiredState) {
1237 AsynchronousSymbolQueryList Result;
1238 while (!PendingQueries.empty()) {
1239 if (PendingQueries.back()->getRequiredState() > RequiredState)
1240 break;
1241
1242 Result.push_back(std::move(PendingQueries.back()));
1243 PendingQueries.pop_back();
1244 }
1245
1246 return Result;
1247}
1248
1249JITDylib::JITDylib(ExecutionSession &ES, std::string Name)
1250 : JITLinkDylib(std::move(Name)), ES(ES) {
1251 LinkOrder.push_back({this, JITDylibLookupFlags::MatchAllSymbols});
1252}
1253
1254std::pair<JITDylib::AsynchronousSymbolQuerySet,
1255 std::shared_ptr<SymbolDependenceMap>>
1256JITDylib::removeTracker(ResourceTracker &RT) {
1257 // Note: Should be called under the session lock.
1258 assert(State != Closed && "JD is defunct");
1259
1260 SymbolNameVector SymbolsToRemove;
1261 SymbolNameVector SymbolsToFail;
1262
1263 if (&RT == DefaultTracker.get()) {
1264 SymbolNameSet TrackedSymbols;
1265 for (auto &KV : TrackerSymbols)
1266 for (auto &Sym : KV.second)
1267 TrackedSymbols.insert(Sym);
1268
1269 for (auto &KV : Symbols) {
1270 auto &Sym = KV.first;
1271 if (!TrackedSymbols.count(Sym))
1272 SymbolsToRemove.push_back(Sym);
1273 }
1274
1275 DefaultTracker.reset();
1276 } else {
1277 /// Check for a non-default tracker.
1278 auto I = TrackerSymbols.find(&RT);
1279 if (I != TrackerSymbols.end()) {
1280 SymbolsToRemove = std::move(I->second);
1281 TrackerSymbols.erase(I);
1282 }
1283 // ... if not found this tracker was already defunct. Nothing to do.
1284 }
1285
1286 for (auto &Sym : SymbolsToRemove) {
1287 assert(Symbols.count(Sym) && "Symbol not in symbol table");
1288
1289 // If there is a MaterializingInfo then collect any queries to fail.
1290 auto MII = MaterializingInfos.find(Sym);
1291 if (MII != MaterializingInfos.end())
1292 SymbolsToFail.push_back(Sym);
1293 }
1294
1295 AsynchronousSymbolQuerySet QueriesToFail;
1296 auto Result = ES.runSessionLocked(
1297 [&]() { return ES.IL_failSymbols(*this, std::move(SymbolsToFail)); });
1298
1299 // Removed symbols should be taken out of the table altogether.
1300 for (auto &Sym : SymbolsToRemove) {
1301 auto I = Symbols.find(Sym);
1302 assert(I != Symbols.end() && "Symbol not present in table");
1303
1304 // Remove Materializer if present.
1305 if (I->second.hasMaterializerAttached()) {
1306 // FIXME: Should this discard the symbols?
1307 UnmaterializedInfos.erase(Sym);
1308 } else {
1309 assert(!UnmaterializedInfos.count(Sym) &&
1310 "Symbol has materializer attached");
1311 }
1312
1313 Symbols.erase(I);
1314 }
1315
1316 return Result;
1317}
1318
1319void JITDylib::transferTracker(ResourceTracker &DstRT, ResourceTracker &SrcRT) {
1320 assert(State != Closed && "JD is defunct");
1321 assert(&DstRT != &SrcRT && "No-op transfers shouldn't call transferTracker");
1322 assert(&DstRT.getJITDylib() == this && "DstRT is not for this JITDylib");
1323 assert(&SrcRT.getJITDylib() == this && "SrcRT is not for this JITDylib");
1324
1325 // Update trackers for any not-yet materialized units.
1326 for (auto &KV : UnmaterializedInfos) {
1327 if (KV.second->RT == &SrcRT)
1328 KV.second->RT = &DstRT;
1329 }
1330
1331 // Update trackers for any active materialization responsibilities.
1332 {
1333 auto I = TrackerMRs.find(&SrcRT);
1334 if (I != TrackerMRs.end()) {
1335 auto &SrcMRs = I->second;
1336 auto &DstMRs = TrackerMRs[&DstRT];
1337 for (auto *MR : SrcMRs)
1338 MR->RT = &DstRT;
1339 if (DstMRs.empty())
1340 DstMRs = std::move(SrcMRs);
1341 else
1342 for (auto *MR : SrcMRs)
1343 DstMRs.insert(MR);
1344 // Erase SrcRT entry in TrackerMRs. Use &SrcRT key rather than iterator I
1345 // for this, since I may have been invalidated by 'TrackerMRs[&DstRT]'.
1346 TrackerMRs.erase(&SrcRT);
1347 }
1348 }
1349
1350 // If we're transfering to the default tracker we just need to delete the
1351 // tracked symbols for the source tracker.
1352 if (&DstRT == DefaultTracker.get()) {
1353 TrackerSymbols.erase(&SrcRT);
1354 return;
1355 }
1356
1357 // If we're transferring from the default tracker we need to find all
1358 // currently untracked symbols.
1359 if (&SrcRT == DefaultTracker.get()) {
1360 assert(!TrackerSymbols.count(&SrcRT) &&
1361 "Default tracker should not appear in TrackerSymbols");
1362
1363 SymbolNameVector SymbolsToTrack;
1364
1365 SymbolNameSet CurrentlyTrackedSymbols;
1366 for (auto &KV : TrackerSymbols)
1367 for (auto &Sym : KV.second)
1368 CurrentlyTrackedSymbols.insert(Sym);
1369
1370 for (auto &KV : Symbols) {
1371 auto &Sym = KV.first;
1372 if (!CurrentlyTrackedSymbols.count(Sym))
1373 SymbolsToTrack.push_back(Sym);
1374 }
1375
1376 TrackerSymbols[&DstRT] = std::move(SymbolsToTrack);
1377 return;
1378 }
1379
1380 auto &DstTrackedSymbols = TrackerSymbols[&DstRT];
1381
1382 // Finally if neither SrtRT or DstRT are the default tracker then
1383 // just append DstRT's tracked symbols to SrtRT's.
1384 auto SI = TrackerSymbols.find(&SrcRT);
1385 if (SI == TrackerSymbols.end())
1386 return;
1387
1388 DstTrackedSymbols.reserve(DstTrackedSymbols.size() + SI->second.size());
1389 for (auto &Sym : SI->second)
1390 DstTrackedSymbols.push_back(std::move(Sym));
1391 TrackerSymbols.erase(SI);
1392}
1393
1394Error JITDylib::defineImpl(MaterializationUnit &MU) {
1395
1396 LLVM_DEBUG({ dbgs() << " " << MU.getSymbols() << "\n"; });
1397
1398 SymbolNameSet Duplicates;
1399 std::vector<SymbolStringPtr> ExistingDefsOverridden;
1400 std::vector<SymbolStringPtr> MUDefsOverridden;
1401
1402 for (const auto &KV : MU.getSymbols()) {
1403 auto I = Symbols.find(KV.first);
1404
1405 if (I != Symbols.end()) {
1406 if (KV.second.isStrong()) {
1407 if (I->second.getFlags().isStrong() ||
1408 I->second.getState() > SymbolState::NeverSearched)
1409 Duplicates.insert(KV.first);
1410 else {
1411 assert(I->second.getState() == SymbolState::NeverSearched &&
1412 "Overridden existing def should be in the never-searched "
1413 "state");
1414 ExistingDefsOverridden.push_back(KV.first);
1415 }
1416 } else
1417 MUDefsOverridden.push_back(KV.first);
1418 }
1419 }
1420
1421 // If there were any duplicate definitions then bail out.
1422 if (!Duplicates.empty()) {
1423 LLVM_DEBUG(
1424 { dbgs() << " Error: Duplicate symbols " << Duplicates << "\n"; });
1425 return make_error<DuplicateDefinition>(std::string(**Duplicates.begin()));
1426 }
1427
1428 // Discard any overridden defs in this MU.
1429 LLVM_DEBUG({
1430 if (!MUDefsOverridden.empty())
1431 dbgs() << " Defs in this MU overridden: " << MUDefsOverridden << "\n";
1432 });
1433 for (auto &S : MUDefsOverridden)
1434 MU.doDiscard(*this, S);
1435
1436 // Discard existing overridden defs.
1437 LLVM_DEBUG({
1438 if (!ExistingDefsOverridden.empty())
1439 dbgs() << " Existing defs overridden by this MU: " << MUDefsOverridden
1440 << "\n";
1441 });
1442 for (auto &S : ExistingDefsOverridden) {
1443
1444 auto UMII = UnmaterializedInfos.find(S);
1445 assert(UMII != UnmaterializedInfos.end() &&
1446 "Overridden existing def should have an UnmaterializedInfo");
1447 UMII->second->MU->doDiscard(*this, S);
1448 }
1449
1450 // Finally, add the defs from this MU.
1451 for (auto &KV : MU.getSymbols()) {
1452 auto &SymEntry = Symbols[KV.first];
1453 SymEntry.setFlags(KV.second);
1454 SymEntry.setState(SymbolState::NeverSearched);
1455 SymEntry.setMaterializerAttached(true);
1456 }
1457
1458 return Error::success();
1459}
1460
1461void JITDylib::installMaterializationUnit(
1462 std::unique_ptr<MaterializationUnit> MU, ResourceTracker &RT) {
1463
1464 /// defineImpl succeeded.
1465 if (&RT != DefaultTracker.get()) {
1466 auto &TS = TrackerSymbols[&RT];
1467 TS.reserve(TS.size() + MU->getSymbols().size());
1468 for (auto &KV : MU->getSymbols())
1469 TS.push_back(KV.first);
1470 }
1471
1472 auto UMI = std::make_shared<UnmaterializedInfo>(std::move(MU), &RT);
1473 for (auto &KV : UMI->MU->getSymbols())
1474 UnmaterializedInfos[KV.first] = UMI;
1475}
1476
1477void JITDylib::detachQueryHelper(AsynchronousSymbolQuery &Q,
1478 const SymbolNameSet &QuerySymbols) {
1479 for (auto &QuerySymbol : QuerySymbols) {
1480 assert(MaterializingInfos.count(QuerySymbol) &&
1481 "QuerySymbol does not have MaterializingInfo");
1482 auto &MI = MaterializingInfos[QuerySymbol];
1483 MI.removeQuery(Q);
1484 }
1485}
1486
1487Platform::~Platform() = default;
1488
1490 ExecutionSession &ES,
1491 const DenseMap<JITDylib *, SymbolLookupSet> &InitSyms) {
1492
1493 DenseMap<JITDylib *, SymbolMap> CompoundResult;
1494 Error CompoundErr = Error::success();
1495 std::mutex LookupMutex;
1496 std::condition_variable CV;
1497 uint64_t Count = InitSyms.size();
1498
1499 LLVM_DEBUG({
1500 dbgs() << "Issuing init-symbol lookup:\n";
1501 for (auto &KV : InitSyms)
1502 dbgs() << " " << KV.first->getName() << ": " << KV.second << "\n";
1503 });
1504
1505 for (auto &KV : InitSyms) {
1506 auto *JD = KV.first;
1507 auto Names = std::move(KV.second);
1508 ES.lookup(
1511 std::move(Names), SymbolState::Ready,
1512 [&, JD](Expected<SymbolMap> Result) {
1513 {
1514 std::lock_guard<std::mutex> Lock(LookupMutex);
1515 --Count;
1516 if (Result) {
1517 assert(!CompoundResult.count(JD) &&
1518 "Duplicate JITDylib in lookup?");
1519 CompoundResult[JD] = std::move(*Result);
1520 } else
1521 CompoundErr =
1522 joinErrors(std::move(CompoundErr), Result.takeError());
1523 }
1524 CV.notify_one();
1525 },
1527 }
1528
1529 std::unique_lock<std::mutex> Lock(LookupMutex);
1530 CV.wait(Lock, [&] { return Count == 0 || CompoundErr; });
1531
1532 if (CompoundErr)
1533 return std::move(CompoundErr);
1534
1535 return std::move(CompoundResult);
1536}
1537
1539 unique_function<void(Error)> OnComplete, ExecutionSession &ES,
1540 const DenseMap<JITDylib *, SymbolLookupSet> &InitSyms) {
1541
1542 class TriggerOnComplete {
1543 public:
1544 using OnCompleteFn = unique_function<void(Error)>;
1545 TriggerOnComplete(OnCompleteFn OnComplete)
1546 : OnComplete(std::move(OnComplete)) {}
1547 ~TriggerOnComplete() { OnComplete(std::move(LookupResult)); }
1548 void reportResult(Error Err) {
1549 std::lock_guard<std::mutex> Lock(ResultMutex);
1550 LookupResult = joinErrors(std::move(LookupResult), std::move(Err));
1551 }
1552
1553 private:
1554 std::mutex ResultMutex;
1555 Error LookupResult{Error::success()};
1556 OnCompleteFn OnComplete;
1557 };
1558
1559 LLVM_DEBUG({
1560 dbgs() << "Issuing init-symbol lookup:\n";
1561 for (auto &KV : InitSyms)
1562 dbgs() << " " << KV.first->getName() << ": " << KV.second << "\n";
1563 });
1564
1565 auto TOC = std::make_shared<TriggerOnComplete>(std::move(OnComplete));
1566
1567 for (auto &KV : InitSyms) {
1568 auto *JD = KV.first;
1569 auto Names = std::move(KV.second);
1570 ES.lookup(
1573 std::move(Names), SymbolState::Ready,
1575 TOC->reportResult(Result.takeError());
1576 },
1578 }
1579}
1580
1582 OS << "Materialization task: " << MU->getName() << " in "
1583 << MR->getTargetJITDylib().getName();
1584}
1585
1586void MaterializationTask::run() { MU->materialize(std::move(MR)); }
1587
1588void LookupTask::printDescription(raw_ostream &OS) { OS << "Lookup task"; }
1589
1591
1592ExecutionSession::ExecutionSession(std::unique_ptr<ExecutorProcessControl> EPC)
1593 : EPC(std::move(EPC)) {
1594 // Associated EPC and this.
1595 this->EPC->ES = this;
1596}
1597
1599 // You must call endSession prior to destroying the session.
1600 assert(!SessionOpen &&
1601 "Session still open. Did you forget to call endSession?");
1602}
1603
1605 LLVM_DEBUG(dbgs() << "Ending ExecutionSession " << this << "\n");
1606
1607 auto JDsToRemove = runSessionLocked([&] {
1608 SessionOpen = false;
1609 return JDs;
1610 });
1611
1612 std::reverse(JDsToRemove.begin(), JDsToRemove.end());
1613
1614 auto Err = removeJITDylibs(std::move(JDsToRemove));
1615
1616 Err = joinErrors(std::move(Err), EPC->disconnect());
1617
1618 return Err;
1619}
1620
1622 runSessionLocked([&] { ResourceManagers.push_back(&RM); });
1623}
1624
1626 runSessionLocked([&] {
1627 assert(!ResourceManagers.empty() && "No managers registered");
1628 if (ResourceManagers.back() == &RM)
1629 ResourceManagers.pop_back();
1630 else {
1631 auto I = llvm::find(ResourceManagers, &RM);
1632 assert(I != ResourceManagers.end() && "RM not registered");
1633 ResourceManagers.erase(I);
1634 }
1635 });
1636}
1637
1639 return runSessionLocked([&, this]() -> JITDylib * {
1640 for (auto &JD : JDs)
1641 if (JD->getName() == Name)
1642 return JD.get();
1643 return nullptr;
1644 });
1645}
1646
1648 assert(!getJITDylibByName(Name) && "JITDylib with that name already exists");
1649 return runSessionLocked([&, this]() -> JITDylib & {
1650 assert(SessionOpen && "Cannot create JITDylib after session is closed");
1651 JDs.push_back(new JITDylib(*this, std::move(Name)));
1652 return *JDs.back();
1653 });
1654}
1655
1657 auto &JD = createBareJITDylib(Name);
1658 if (P)
1659 if (auto Err = P->setupJITDylib(JD))
1660 return std::move(Err);
1661 return JD;
1662}
1663
1664Error ExecutionSession::removeJITDylibs(std::vector<JITDylibSP> JDsToRemove) {
1665
1666 // Set JD to 'Closing' state and remove JD from the ExecutionSession.
1667 runSessionLocked([&] {
1668 for (auto &JD : JDsToRemove) {
1669 assert(JD->State == JITDylib::Open && "JD already closed");
1670 JD->State = JITDylib::Closing;
1671 auto I = llvm::find(JDs, JD);
1672 assert(I != JDs.end() && "JD does not appear in session JDs");
1673 JDs.erase(I);
1674 }
1675 });
1676
1677 // Clear JITDylibs and notify the platform.
1678 Error Err = Error::success();
1679 for (auto JD : JDsToRemove) {
1680 Err = joinErrors(std::move(Err), JD->clear());
1681 if (P)
1682 Err = joinErrors(std::move(Err), P->teardownJITDylib(*JD));
1683 }
1684
1685 // Set JD to closed state. Clear remaining data structures.
1686 runSessionLocked([&] {
1687 for (auto &JD : JDsToRemove) {
1688 assert(JD->State == JITDylib::Closing && "JD should be closing");
1689 JD->State = JITDylib::Closed;
1690 assert(JD->Symbols.empty() && "JD.Symbols is not empty after clear");
1691 assert(JD->UnmaterializedInfos.empty() &&
1692 "JD.UnmaterializedInfos is not empty after clear");
1693 assert(JD->MaterializingInfos.empty() &&
1694 "JD.MaterializingInfos is not empty after clear");
1695 assert(JD->TrackerSymbols.empty() &&
1696 "TrackerSymbols is not empty after clear");
1697 JD->DefGenerators.clear();
1698 JD->LinkOrder.clear();
1699 }
1700 });
1701
1702 return Err;
1703}
1704
1707 if (JDs.empty())
1708 return std::vector<JITDylibSP>();
1709
1710 auto &ES = JDs.front()->getExecutionSession();
1711 return ES.runSessionLocked([&]() -> Expected<std::vector<JITDylibSP>> {
1712 DenseSet<JITDylib *> Visited;
1713 std::vector<JITDylibSP> Result;
1714
1715 for (auto &JD : JDs) {
1716
1717 if (JD->State != Open)
1718 return make_error<StringError>(
1719 "Error building link order: " + JD->getName() + " is defunct",
1721 if (Visited.count(JD.get()))
1722 continue;
1723
1725 WorkStack.push_back(JD);
1726 Visited.insert(JD.get());
1727
1728 while (!WorkStack.empty()) {
1729 Result.push_back(std::move(WorkStack.back()));
1730 WorkStack.pop_back();
1731
1732 for (auto &KV : llvm::reverse(Result.back()->LinkOrder)) {
1733 auto &JD = *KV.first;
1734 if (!Visited.insert(&JD).second)
1735 continue;
1736 WorkStack.push_back(&JD);
1737 }
1738 }
1739 }
1740 return Result;
1741 });
1742}
1743
1746 auto Result = getDFSLinkOrder(JDs);
1747 if (Result)
1748 std::reverse(Result->begin(), Result->end());
1749 return Result;
1750}
1751
1753 return getDFSLinkOrder({this});
1754}
1755
1757 return getReverseDFSLinkOrder({this});
1758}
1759
1761 LookupKind K, JITDylibSearchOrder SearchOrder, SymbolLookupSet LookupSet,
1762 unique_function<void(Expected<SymbolFlagsMap>)> OnComplete) {
1763
1764 OL_applyQueryPhase1(std::make_unique<InProgressLookupFlagsState>(
1765 K, std::move(SearchOrder), std::move(LookupSet),
1766 std::move(OnComplete)),
1767 Error::success());
1768}
1769
1772 SymbolLookupSet LookupSet) {
1773
1774 std::promise<MSVCPExpected<SymbolFlagsMap>> ResultP;
1775 OL_applyQueryPhase1(std::make_unique<InProgressLookupFlagsState>(
1776 K, std::move(SearchOrder), std::move(LookupSet),
1777 [&ResultP](Expected<SymbolFlagsMap> Result) {
1778 ResultP.set_value(std::move(Result));
1779 }),
1780 Error::success());
1781
1782 auto ResultF = ResultP.get_future();
1783 return ResultF.get();
1784}
1785
1787 LookupKind K, const JITDylibSearchOrder &SearchOrder,
1788 SymbolLookupSet Symbols, SymbolState RequiredState,
1789 SymbolsResolvedCallback NotifyComplete,
1790 RegisterDependenciesFunction RegisterDependencies) {
1791
1792 LLVM_DEBUG({
1793 runSessionLocked([&]() {
1794 dbgs() << "Looking up " << Symbols << " in " << SearchOrder
1795 << " (required state: " << RequiredState << ")\n";
1796 });
1797 });
1798
1799 // lookup can be re-entered recursively if running on a single thread. Run any
1800 // outstanding MUs in case this query depends on them, otherwise this lookup
1801 // will starve waiting for a result from an MU that is stuck in the queue.
1802 dispatchOutstandingMUs();
1803
1804 auto Unresolved = std::move(Symbols);
1805 auto Q = std::make_shared<AsynchronousSymbolQuery>(Unresolved, RequiredState,
1806 std::move(NotifyComplete));
1807
1808 auto IPLS = std::make_unique<InProgressFullLookupState>(
1809 K, SearchOrder, std::move(Unresolved), RequiredState, std::move(Q),
1810 std::move(RegisterDependencies));
1811
1812 OL_applyQueryPhase1(std::move(IPLS), Error::success());
1813}
1814
1817 SymbolLookupSet Symbols, LookupKind K,
1818 SymbolState RequiredState,
1819 RegisterDependenciesFunction RegisterDependencies) {
1820#if LLVM_ENABLE_THREADS
1821 // In the threaded case we use promises to return the results.
1822 std::promise<SymbolMap> PromisedResult;
1823 Error ResolutionError = Error::success();
1824
1825 auto NotifyComplete = [&](Expected<SymbolMap> R) {
1826 if (R)
1827 PromisedResult.set_value(std::move(*R));
1828 else {
1829 ErrorAsOutParameter _(&ResolutionError);
1830 ResolutionError = R.takeError();
1831 PromisedResult.set_value(SymbolMap());
1832 }
1833 };
1834
1835#else
1837 Error ResolutionError = Error::success();
1838
1839 auto NotifyComplete = [&](Expected<SymbolMap> R) {
1840 ErrorAsOutParameter _(&ResolutionError);
1841 if (R)
1842 Result = std::move(*R);
1843 else
1844 ResolutionError = R.takeError();
1845 };
1846#endif
1847
1848 // Perform the asynchronous lookup.
1849 lookup(K, SearchOrder, std::move(Symbols), RequiredState, NotifyComplete,
1850 RegisterDependencies);
1851
1852#if LLVM_ENABLE_THREADS
1853 auto ResultFuture = PromisedResult.get_future();
1854 auto Result = ResultFuture.get();
1855
1856 if (ResolutionError)
1857 return std::move(ResolutionError);
1858
1859 return std::move(Result);
1860
1861#else
1862 if (ResolutionError)
1863 return std::move(ResolutionError);
1864
1865 return Result;
1866#endif
1867}
1868
1871 SymbolStringPtr Name, SymbolState RequiredState) {
1872 SymbolLookupSet Names({Name});
1873
1874 if (auto ResultMap = lookup(SearchOrder, std::move(Names), LookupKind::Static,
1875 RequiredState, NoDependenciesToRegister)) {
1876 assert(ResultMap->size() == 1 && "Unexpected number of results");
1877 assert(ResultMap->count(Name) && "Missing result for symbol");
1878 return std::move(ResultMap->begin()->second);
1879 } else
1880 return ResultMap.takeError();
1881}
1882
1885 SymbolState RequiredState) {
1886 return lookup(makeJITDylibSearchOrder(SearchOrder), Name, RequiredState);
1887}
1888
1891 SymbolState RequiredState) {
1892 return lookup(SearchOrder, intern(Name), RequiredState);
1893}
1894
1897
1898 auto TagAddrs = lookup({{&JD, JITDylibLookupFlags::MatchAllSymbols}},
1901 if (!TagAddrs)
1902 return TagAddrs.takeError();
1903
1904 // Associate tag addresses with implementations.
1905 std::lock_guard<std::mutex> Lock(JITDispatchHandlersMutex);
1906 for (auto &KV : *TagAddrs) {
1907 auto TagAddr = KV.second.getAddress();
1908 if (JITDispatchHandlers.count(TagAddr))
1909 return make_error<StringError>("Tag " + formatv("{0:x16}", TagAddr) +
1910 " (for " + *KV.first +
1911 ") already registered",
1913 auto I = WFs.find(KV.first);
1914 assert(I != WFs.end() && I->second &&
1915 "JITDispatchHandler implementation missing");
1916 JITDispatchHandlers[KV.second.getAddress()] =
1917 std::make_shared<JITDispatchHandlerFunction>(std::move(I->second));
1918 LLVM_DEBUG({
1919 dbgs() << "Associated function tag \"" << *KV.first << "\" ("
1920 << formatv("{0:x}", KV.second.getAddress()) << ") with handler\n";
1921 });
1922 }
1923 return Error::success();
1924}
1925
1927 ExecutorAddr HandlerFnTagAddr,
1928 ArrayRef<char> ArgBuffer) {
1929
1930 std::shared_ptr<JITDispatchHandlerFunction> F;
1931 {
1932 std::lock_guard<std::mutex> Lock(JITDispatchHandlersMutex);
1933 auto I = JITDispatchHandlers.find(HandlerFnTagAddr);
1934 if (I != JITDispatchHandlers.end())
1935 F = I->second;
1936 }
1937
1938 if (F)
1939 (*F)(std::move(SendResult), ArgBuffer.data(), ArgBuffer.size());
1940 else
1942 ("No function registered for tag " +
1943 formatv("{0:x16}", HandlerFnTagAddr))
1944 .str()));
1945}
1946
1948 runSessionLocked([this, &OS]() {
1949 for (auto &JD : JDs)
1950 JD->dump(OS);
1951 });
1952}
1953
1954void ExecutionSession::dispatchOutstandingMUs() {
1955 LLVM_DEBUG(dbgs() << "Dispatching MaterializationUnits...\n");
1956 while (true) {
1957 std::optional<std::pair<std::unique_ptr<MaterializationUnit>,
1958 std::unique_ptr<MaterializationResponsibility>>>
1959 JMU;
1960
1961 {
1962 std::lock_guard<std::recursive_mutex> Lock(OutstandingMUsMutex);
1963 if (!OutstandingMUs.empty()) {
1964 JMU.emplace(std::move(OutstandingMUs.back()));
1965 OutstandingMUs.pop_back();
1966 }
1967 }
1968
1969 if (!JMU)
1970 break;
1971
1972 assert(JMU->first && "No MU?");
1973 LLVM_DEBUG(dbgs() << " Dispatching \"" << JMU->first->getName() << "\"\n");
1974 dispatchTask(std::make_unique<MaterializationTask>(std::move(JMU->first),
1975 std::move(JMU->second)));
1976 }
1977 LLVM_DEBUG(dbgs() << "Done dispatching MaterializationUnits.\n");
1978}
1979
1980Error ExecutionSession::removeResourceTracker(ResourceTracker &RT) {
1981 LLVM_DEBUG({
1982 dbgs() << "In " << RT.getJITDylib().getName() << " removing tracker "
1983 << formatv("{0:x}", RT.getKeyUnsafe()) << "\n";
1984 });
1985 std::vector<ResourceManager *> CurrentResourceManagers;
1986
1987 JITDylib::AsynchronousSymbolQuerySet QueriesToFail;
1988 std::shared_ptr<SymbolDependenceMap> FailedSymbols;
1989
1990 runSessionLocked([&] {
1991 CurrentResourceManagers = ResourceManagers;
1992 RT.makeDefunct();
1993 std::tie(QueriesToFail, FailedSymbols) = RT.getJITDylib().removeTracker(RT);
1994 });
1995
1996 Error Err = Error::success();
1997
1998 auto &JD = RT.getJITDylib();
1999 for (auto *L : reverse(CurrentResourceManagers))
2000 Err = joinErrors(std::move(Err),
2001 L->handleRemoveResources(JD, RT.getKeyUnsafe()));
2002
2003 for (auto &Q : QueriesToFail)
2004 Q->handleFailed(
2005 make_error<FailedToMaterialize>(getSymbolStringPool(), FailedSymbols));
2006
2007 return Err;
2008}
2009
2010void ExecutionSession::transferResourceTracker(ResourceTracker &DstRT,
2011 ResourceTracker &SrcRT) {
2012 LLVM_DEBUG({
2013 dbgs() << "In " << SrcRT.getJITDylib().getName()
2014 << " transfering resources from tracker "
2015 << formatv("{0:x}", SrcRT.getKeyUnsafe()) << " to tracker "
2016 << formatv("{0:x}", DstRT.getKeyUnsafe()) << "\n";
2017 });
2018
2019 // No-op transfers are allowed and do not invalidate the source.
2020 if (&DstRT == &SrcRT)
2021 return;
2022
2023 assert(&DstRT.getJITDylib() == &SrcRT.getJITDylib() &&
2024 "Can't transfer resources between JITDylibs");
2025 runSessionLocked([&]() {
2026 SrcRT.makeDefunct();
2027 auto &JD = DstRT.getJITDylib();
2028 JD.transferTracker(DstRT, SrcRT);
2029 for (auto *L : reverse(ResourceManagers))
2030 L->handleTransferResources(JD, DstRT.getKeyUnsafe(),
2031 SrcRT.getKeyUnsafe());
2032 });
2033}
2034
2035void ExecutionSession::destroyResourceTracker(ResourceTracker &RT) {
2036 runSessionLocked([&]() {
2037 LLVM_DEBUG({
2038 dbgs() << "In " << RT.getJITDylib().getName() << " destroying tracker "
2039 << formatv("{0:x}", RT.getKeyUnsafe()) << "\n";
2040 });
2041 if (!RT.isDefunct())
2042 transferResourceTracker(*RT.getJITDylib().getDefaultResourceTracker(),
2043 RT);
2044 });
2045}
2046
2047Error ExecutionSession::IL_updateCandidatesFor(
2048 JITDylib &JD, JITDylibLookupFlags JDLookupFlags,
2049 SymbolLookupSet &Candidates, SymbolLookupSet *NonCandidates) {
2050 return Candidates.forEachWithRemoval(
2051 [&](const SymbolStringPtr &Name,
2052 SymbolLookupFlags SymLookupFlags) -> Expected<bool> {
2053 /// Search for the symbol. If not found then continue without
2054 /// removal.
2055 auto SymI = JD.Symbols.find(Name);
2056 if (SymI == JD.Symbols.end())
2057 return false;
2058
2059 // If this is a non-exported symbol and we're matching exported
2060 // symbols only then remove this symbol from the candidates list.
2061 //
2062 // If we're tracking non-candidates then add this to the non-candidate
2063 // list.
2064 if (!SymI->second.getFlags().isExported() &&
2066 if (NonCandidates)
2067 NonCandidates->add(Name, SymLookupFlags);
2068 return true;
2069 }
2070
2071 // If we match against a materialization-side-effects only symbol
2072 // then make sure it is weakly-referenced. Otherwise bail out with
2073 // an error.
2074 // FIXME: Use a "materialization-side-effects-only symbols must be
2075 // weakly referenced" specific error here to reduce confusion.
2076 if (SymI->second.getFlags().hasMaterializationSideEffectsOnly() &&
2078 return make_error<SymbolsNotFound>(getSymbolStringPool(),
2080
2081 // If we matched against this symbol but it is in the error state
2082 // then bail out and treat it as a failure to materialize.
2083 if (SymI->second.getFlags().hasError()) {
2084 auto FailedSymbolsMap = std::make_shared<SymbolDependenceMap>();
2085 (*FailedSymbolsMap)[&JD] = {Name};
2086 return make_error<FailedToMaterialize>(getSymbolStringPool(),
2087 std::move(FailedSymbolsMap));
2088 }
2089
2090 // Otherwise this is a match. Remove it from the candidate set.
2091 return true;
2092 });
2093}
2094
2095void ExecutionSession::OL_resumeLookupAfterGeneration(
2096 InProgressLookupState &IPLS) {
2097
2099 "Should not be called for not-in-generator lookups");
2101
2103
2104 if (auto DG = IPLS.CurDefGeneratorStack.back().lock()) {
2105 IPLS.CurDefGeneratorStack.pop_back();
2106 std::lock_guard<std::mutex> Lock(DG->M);
2107
2108 // If there are no pending lookups then mark the generator as free and
2109 // return.
2110 if (DG->PendingLookups.empty()) {
2111 DG->InUse = false;
2112 return;
2113 }
2114
2115 // Otherwise resume the next lookup.
2116 LS = std::move(DG->PendingLookups.front());
2117 DG->PendingLookups.pop_front();
2118 }
2119
2120 if (LS.IPLS) {
2122 dispatchTask(std::make_unique<LookupTask>(std::move(LS)));
2123 }
2124}
2125
2126void ExecutionSession::OL_applyQueryPhase1(
2127 std::unique_ptr<InProgressLookupState> IPLS, Error Err) {
2128
2129 LLVM_DEBUG({
2130 dbgs() << "Entering OL_applyQueryPhase1:\n"
2131 << " Lookup kind: " << IPLS->K << "\n"
2132 << " Search order: " << IPLS->SearchOrder
2133 << ", Current index = " << IPLS->CurSearchOrderIndex
2134 << (IPLS->NewJITDylib ? " (entering new JITDylib)" : "") << "\n"
2135 << " Lookup set: " << IPLS->LookupSet << "\n"
2136 << " Definition generator candidates: "
2137 << IPLS->DefGeneratorCandidates << "\n"
2138 << " Definition generator non-candidates: "
2139 << IPLS->DefGeneratorNonCandidates << "\n";
2140 });
2141
2142 if (IPLS->GenState == InProgressLookupState::InGenerator)
2143 OL_resumeLookupAfterGeneration(*IPLS);
2144
2145 assert(IPLS->GenState != InProgressLookupState::InGenerator &&
2146 "Lookup should not be in InGenerator state here");
2147
2148 // FIXME: We should attach the query as we go: This provides a result in a
2149 // single pass in the common case where all symbols have already reached the
2150 // required state. The query could be detached again in the 'fail' method on
2151 // IPLS. Phase 2 would be reduced to collecting and dispatching the MUs.
2152
2153 while (IPLS->CurSearchOrderIndex != IPLS->SearchOrder.size()) {
2154
2155 // If we've been handed an error or received one back from a generator then
2156 // fail the query. We don't need to unlink: At this stage the query hasn't
2157 // actually been lodged.
2158 if (Err)
2159 return IPLS->fail(std::move(Err));
2160
2161 // Get the next JITDylib and lookup flags.
2162 auto &KV = IPLS->SearchOrder[IPLS->CurSearchOrderIndex];
2163 auto &JD = *KV.first;
2164 auto JDLookupFlags = KV.second;
2165
2166 LLVM_DEBUG({
2167 dbgs() << "Visiting \"" << JD.getName() << "\" (" << JDLookupFlags
2168 << ") with lookup set " << IPLS->LookupSet << ":\n";
2169 });
2170
2171 // If we've just reached a new JITDylib then perform some setup.
2172 if (IPLS->NewJITDylib) {
2173 // Add any non-candidates from the last JITDylib (if any) back on to the
2174 // list of definition candidates for this JITDylib, reset definition
2175 // non-candidates to the empty set.
2176 SymbolLookupSet Tmp;
2177 std::swap(IPLS->DefGeneratorNonCandidates, Tmp);
2178 IPLS->DefGeneratorCandidates.append(std::move(Tmp));
2179
2180 LLVM_DEBUG({
2181 dbgs() << " First time visiting " << JD.getName()
2182 << ", resetting candidate sets and building generator stack\n";
2183 });
2184
2185 // Build the definition generator stack for this JITDylib.
2186 runSessionLocked([&] {
2187 IPLS->CurDefGeneratorStack.reserve(JD.DefGenerators.size());
2188 for (auto &DG : reverse(JD.DefGenerators))
2189 IPLS->CurDefGeneratorStack.push_back(DG);
2190 });
2191
2192 // Flag that we've done our initialization.
2193 IPLS->NewJITDylib = false;
2194 }
2195
2196 // Remove any generation candidates that are already defined (and match) in
2197 // this JITDylib.
2198 runSessionLocked([&] {
2199 // Update the list of candidates (and non-candidates) for definition
2200 // generation.
2201 LLVM_DEBUG(dbgs() << " Updating candidate set...\n");
2202 Err = IL_updateCandidatesFor(
2203 JD, JDLookupFlags, IPLS->DefGeneratorCandidates,
2204 JD.DefGenerators.empty() ? nullptr
2205 : &IPLS->DefGeneratorNonCandidates);
2206 LLVM_DEBUG({
2207 dbgs() << " Remaining candidates = " << IPLS->DefGeneratorCandidates
2208 << "\n";
2209 });
2210
2211 // If this lookup was resumed after auto-suspension but all candidates
2212 // have already been generated (by some previous call to the generator)
2213 // treat the lookup as if it had completed generation.
2214 if (IPLS->GenState == InProgressLookupState::ResumedForGenerator &&
2215 IPLS->DefGeneratorCandidates.empty())
2216 OL_resumeLookupAfterGeneration(*IPLS);
2217 });
2218
2219 // If we encountered an error while filtering generation candidates then
2220 // bail out.
2221 if (Err)
2222 return IPLS->fail(std::move(Err));
2223
2224 /// Apply any definition generators on the stack.
2225 LLVM_DEBUG({
2226 if (IPLS->CurDefGeneratorStack.empty())
2227 LLVM_DEBUG(dbgs() << " No generators to run for this JITDylib.\n");
2228 else if (IPLS->DefGeneratorCandidates.empty())
2229 LLVM_DEBUG(dbgs() << " No candidates to generate.\n");
2230 else
2231 dbgs() << " Running " << IPLS->CurDefGeneratorStack.size()
2232 << " remaining generators for "
2233 << IPLS->DefGeneratorCandidates.size() << " candidates\n";
2234 });
2235 while (!IPLS->CurDefGeneratorStack.empty() &&
2236 !IPLS->DefGeneratorCandidates.empty()) {
2237 auto DG = IPLS->CurDefGeneratorStack.back().lock();
2238
2239 if (!DG)
2240 return IPLS->fail(make_error<StringError>(
2241 "DefinitionGenerator removed while lookup in progress",
2243
2244 // At this point the lookup is in either the NotInGenerator state, or in
2245 // the ResumedForGenerator state.
2246 // If this lookup is in the NotInGenerator state then check whether the
2247 // generator is in use. If the generator is not in use then move the
2248 // lookup to the InGenerator state and continue. If the generator is
2249 // already in use then just add this lookup to the pending lookups list
2250 // and bail out.
2251 // If this lookup is in the ResumedForGenerator state then just move it
2252 // to InGenerator and continue.
2253 if (IPLS->GenState == InProgressLookupState::NotInGenerator) {
2254 std::lock_guard<std::mutex> Lock(DG->M);
2255 if (DG->InUse) {
2256 DG->PendingLookups.push_back(std::move(IPLS));
2257 return;
2258 }
2259 DG->InUse = true;
2260 }
2261
2262 IPLS->GenState = InProgressLookupState::InGenerator;
2263
2264 auto K = IPLS->K;
2265 auto &LookupSet = IPLS->DefGeneratorCandidates;
2266
2267 // Run the generator. If the generator takes ownership of QA then this
2268 // will break the loop.
2269 {
2270 LLVM_DEBUG(dbgs() << " Attempting to generate " << LookupSet << "\n");
2271 LookupState LS(std::move(IPLS));
2272 Err = DG->tryToGenerate(LS, K, JD, JDLookupFlags, LookupSet);
2273 IPLS = std::move(LS.IPLS);
2274 }
2275
2276 // If the lookup returned then pop the generator stack and unblock the
2277 // next lookup on this generator (if any).
2278 if (IPLS)
2279 OL_resumeLookupAfterGeneration(*IPLS);
2280
2281 // If there was an error then fail the query.
2282 if (Err) {
2283 LLVM_DEBUG({
2284 dbgs() << " Error attempting to generate " << LookupSet << "\n";
2285 });
2286 assert(IPLS && "LS cannot be retained if error is returned");
2287 return IPLS->fail(std::move(Err));
2288 }
2289
2290 // Otherwise if QA was captured then break the loop.
2291 if (!IPLS) {
2292 LLVM_DEBUG(
2293 { dbgs() << " LookupState captured. Exiting phase1 for now.\n"; });
2294 return;
2295 }
2296
2297 // Otherwise if we're continuing around the loop then update candidates
2298 // for the next round.
2299 runSessionLocked([&] {
2300 LLVM_DEBUG(dbgs() << " Updating candidate set post-generation\n");
2301 Err = IL_updateCandidatesFor(
2302 JD, JDLookupFlags, IPLS->DefGeneratorCandidates,
2303 JD.DefGenerators.empty() ? nullptr
2304 : &IPLS->DefGeneratorNonCandidates);
2305 });
2306
2307 // If updating candidates failed then fail the query.
2308 if (Err) {
2309 LLVM_DEBUG(dbgs() << " Error encountered while updating candidates\n");
2310 return IPLS->fail(std::move(Err));
2311 }
2312 }
2313
2314 if (IPLS->DefGeneratorCandidates.empty() &&
2315 IPLS->DefGeneratorNonCandidates.empty()) {
2316 // Early out if there are no remaining symbols.
2317 LLVM_DEBUG(dbgs() << "All symbols matched.\n");
2318 IPLS->CurSearchOrderIndex = IPLS->SearchOrder.size();
2319 break;
2320 } else {
2321 // If we get here then we've moved on to the next JITDylib with candidates
2322 // remaining.
2323 LLVM_DEBUG(dbgs() << "Phase 1 moving to next JITDylib.\n");
2324 ++IPLS->CurSearchOrderIndex;
2325 IPLS->NewJITDylib = true;
2326 }
2327 }
2328
2329 // Remove any weakly referenced candidates that could not be found/generated.
2330 IPLS->DefGeneratorCandidates.remove_if(
2331 [](const SymbolStringPtr &Name, SymbolLookupFlags SymLookupFlags) {
2332 return SymLookupFlags == SymbolLookupFlags::WeaklyReferencedSymbol;
2333 });
2334
2335 // If we get here then we've finished searching all JITDylibs.
2336 // If we matched all symbols then move to phase 2, otherwise fail the query
2337 // with a SymbolsNotFound error.
2338 if (IPLS->DefGeneratorCandidates.empty()) {
2339 LLVM_DEBUG(dbgs() << "Phase 1 succeeded.\n");
2340 IPLS->complete(std::move(IPLS));
2341 } else {
2342 LLVM_DEBUG(dbgs() << "Phase 1 failed with unresolved symbols.\n");
2343 IPLS->fail(make_error<SymbolsNotFound>(
2344 getSymbolStringPool(), IPLS->DefGeneratorCandidates.getSymbolNames()));
2345 }
2346}
2347
2348void ExecutionSession::OL_completeLookup(
2349 std::unique_ptr<InProgressLookupState> IPLS,
2350 std::shared_ptr<AsynchronousSymbolQuery> Q,
2351 RegisterDependenciesFunction RegisterDependencies) {
2352
2353 LLVM_DEBUG({
2354 dbgs() << "Entering OL_completeLookup:\n"
2355 << " Lookup kind: " << IPLS->K << "\n"
2356 << " Search order: " << IPLS->SearchOrder
2357 << ", Current index = " << IPLS->CurSearchOrderIndex
2358 << (IPLS->NewJITDylib ? " (entering new JITDylib)" : "") << "\n"
2359 << " Lookup set: " << IPLS->LookupSet << "\n"
2360 << " Definition generator candidates: "
2361 << IPLS->DefGeneratorCandidates << "\n"
2362 << " Definition generator non-candidates: "
2363 << IPLS->DefGeneratorNonCandidates << "\n";
2364 });
2365
2366 bool QueryComplete = false;
2367 DenseMap<JITDylib *, JITDylib::UnmaterializedInfosList> CollectedUMIs;
2368
2369 auto LodgingErr = runSessionLocked([&]() -> Error {
2370 for (auto &KV : IPLS->SearchOrder) {
2371 auto &JD = *KV.first;
2372 auto JDLookupFlags = KV.second;
2373 LLVM_DEBUG({
2374 dbgs() << "Visiting \"" << JD.getName() << "\" (" << JDLookupFlags
2375 << ") with lookup set " << IPLS->LookupSet << ":\n";
2376 });
2377
2378 auto Err = IPLS->LookupSet.forEachWithRemoval(
2379 [&](const SymbolStringPtr &Name,
2380 SymbolLookupFlags SymLookupFlags) -> Expected<bool> {
2381 LLVM_DEBUG({
2382 dbgs() << " Attempting to match \"" << Name << "\" ("
2383 << SymLookupFlags << ")... ";
2384 });
2385
2386 /// Search for the symbol. If not found then continue without
2387 /// removal.
2388 auto SymI = JD.Symbols.find(Name);
2389 if (SymI == JD.Symbols.end()) {
2390 LLVM_DEBUG(dbgs() << "skipping: not present\n");
2391 return false;
2392 }
2393
2394 // If this is a non-exported symbol and we're matching exported
2395 // symbols only then skip this symbol without removal.
2396 if (!SymI->second.getFlags().isExported() &&
2397 JDLookupFlags ==
2399 LLVM_DEBUG(dbgs() << "skipping: not exported\n");
2400 return false;
2401 }
2402
2403 // If we match against a materialization-side-effects only symbol
2404 // then make sure it is weakly-referenced. Otherwise bail out with
2405 // an error.
2406 // FIXME: Use a "materialization-side-effects-only symbols must be
2407 // weakly referenced" specific error here to reduce confusion.
2408 if (SymI->second.getFlags().hasMaterializationSideEffectsOnly() &&
2410 LLVM_DEBUG({
2411 dbgs() << "error: "
2412 "required, but symbol is has-side-effects-only\n";
2413 });
2414 return make_error<SymbolsNotFound>(getSymbolStringPool(),
2416 }
2417
2418 // If we matched against this symbol but it is in the error state
2419 // then bail out and treat it as a failure to materialize.
2420 if (SymI->second.getFlags().hasError()) {
2421 LLVM_DEBUG(dbgs() << "error: symbol is in error state\n");
2422 auto FailedSymbolsMap = std::make_shared<SymbolDependenceMap>();
2423 (*FailedSymbolsMap)[&JD] = {Name};
2424 return make_error<FailedToMaterialize>(
2425 getSymbolStringPool(), std::move(FailedSymbolsMap));
2426 }
2427
2428 // Otherwise this is a match.
2429
2430 // If this symbol is already in the required state then notify the
2431 // query, remove the symbol and continue.
2432 if (SymI->second.getState() >= Q->getRequiredState()) {
2434 << "matched, symbol already in required state\n");
2435 Q->notifySymbolMetRequiredState(Name, SymI->second.getSymbol());
2436 return true;
2437 }
2438
2439 // Otherwise this symbol does not yet meet the required state. Check
2440 // whether it has a materializer attached, and if so prepare to run
2441 // it.
2442 if (SymI->second.hasMaterializerAttached()) {
2443 assert(SymI->second.getAddress() == ExecutorAddr() &&
2444 "Symbol not resolved but already has address?");
2445 auto UMII = JD.UnmaterializedInfos.find(Name);
2446 assert(UMII != JD.UnmaterializedInfos.end() &&
2447 "Lazy symbol should have UnmaterializedInfo");
2448
2449 auto UMI = UMII->second;
2450 assert(UMI->MU && "Materializer should not be null");
2451 assert(UMI->RT && "Tracker should not be null");
2452 LLVM_DEBUG({
2453 dbgs() << "matched, preparing to dispatch MU@" << UMI->MU.get()
2454 << " (" << UMI->MU->getName() << ")\n";
2455 });
2456
2457 // Move all symbols associated with this MaterializationUnit into
2458 // materializing state.
2459 for (auto &KV : UMI->MU->getSymbols()) {
2460 auto SymK = JD.Symbols.find(KV.first);
2461 assert(SymK != JD.Symbols.end() &&
2462 "No entry for symbol covered by MaterializationUnit");
2463 SymK->second.setMaterializerAttached(false);
2464 SymK->second.setState(SymbolState::Materializing);
2465 JD.UnmaterializedInfos.erase(KV.first);
2466 }
2467
2468 // Add MU to the list of MaterializationUnits to be materialized.
2469 CollectedUMIs[&JD].push_back(std::move(UMI));
2470 } else
2471 LLVM_DEBUG(dbgs() << "matched, registering query");
2472
2473 // Add the query to the PendingQueries list and continue, deleting
2474 // the element from the lookup set.
2475 assert(SymI->second.getState() != SymbolState::NeverSearched &&
2476 SymI->second.getState() != SymbolState::Ready &&
2477 "By this line the symbol should be materializing");
2478 auto &MI = JD.MaterializingInfos[Name];
2479 MI.addQuery(Q);
2480 Q->addQueryDependence(JD, Name);
2481
2482 return true;
2483 });
2484
2485 // Handle failure.
2486 if (Err) {
2487
2488 LLVM_DEBUG({
2489 dbgs() << "Lookup failed. Detaching query and replacing MUs.\n";
2490 });
2491
2492 // Detach the query.
2493 Q->detach();
2494
2495 // Replace the MUs.
2496 for (auto &KV : CollectedUMIs) {
2497 auto &JD = *KV.first;
2498 for (auto &UMI : KV.second)
2499 for (auto &KV2 : UMI->MU->getSymbols()) {
2500 assert(!JD.UnmaterializedInfos.count(KV2.first) &&
2501 "Unexpected materializer in map");
2502 auto SymI = JD.Symbols.find(KV2.first);
2503 assert(SymI != JD.Symbols.end() && "Missing symbol entry");
2504 assert(SymI->second.getState() == SymbolState::Materializing &&
2505 "Can not replace symbol that is not materializing");
2506 assert(!SymI->second.hasMaterializerAttached() &&
2507 "MaterializerAttached flag should not be set");
2508 SymI->second.setMaterializerAttached(true);
2509 JD.UnmaterializedInfos[KV2.first] = UMI;
2510 }
2511 }
2512
2513 return Err;
2514 }
2515 }
2516
2517 LLVM_DEBUG(dbgs() << "Stripping unmatched weakly-referenced symbols\n");
2518 IPLS->LookupSet.forEachWithRemoval(
2519 [&](const SymbolStringPtr &Name, SymbolLookupFlags SymLookupFlags) {
2520 if (SymLookupFlags == SymbolLookupFlags::WeaklyReferencedSymbol) {
2521 Q->dropSymbol(Name);
2522 return true;
2523 } else
2524 return false;
2525 });
2526
2527 if (!IPLS->LookupSet.empty()) {
2528 LLVM_DEBUG(dbgs() << "Failing due to unresolved symbols\n");
2529 return make_error<SymbolsNotFound>(getSymbolStringPool(),
2530 IPLS->LookupSet.getSymbolNames());
2531 }
2532
2533 // Record whether the query completed.
2534 QueryComplete = Q->isComplete();
2535
2536 LLVM_DEBUG({
2537 dbgs() << "Query successfully "
2538 << (QueryComplete ? "completed" : "lodged") << "\n";
2539 });
2540
2541 // Move the collected MUs to the OutstandingMUs list.
2542 if (!CollectedUMIs.empty()) {
2543 std::lock_guard<std::recursive_mutex> Lock(OutstandingMUsMutex);
2544
2545 LLVM_DEBUG(dbgs() << "Adding MUs to dispatch:\n");
2546 for (auto &KV : CollectedUMIs) {
2547 LLVM_DEBUG({
2548 auto &JD = *KV.first;
2549 dbgs() << " For " << JD.getName() << ": Adding " << KV.second.size()
2550 << " MUs.\n";
2551 });
2552 for (auto &UMI : KV.second) {
2553 auto MR = createMaterializationResponsibility(
2554 *UMI->RT, std::move(UMI->MU->SymbolFlags),
2555 std::move(UMI->MU->InitSymbol));
2556 OutstandingMUs.push_back(
2557 std::make_pair(std::move(UMI->MU), std::move(MR)));
2558 }
2559 }
2560 } else
2561 LLVM_DEBUG(dbgs() << "No MUs to dispatch.\n");
2562
2563 if (RegisterDependencies && !Q->QueryRegistrations.empty()) {
2564 LLVM_DEBUG(dbgs() << "Registering dependencies\n");
2565 RegisterDependencies(Q->QueryRegistrations);
2566 } else
2567 LLVM_DEBUG(dbgs() << "No dependencies to register\n");
2568
2569 return Error::success();
2570 });
2571
2572 if (LodgingErr) {
2573 LLVM_DEBUG(dbgs() << "Failing query\n");
2574 Q->detach();
2575 Q->handleFailed(std::move(LodgingErr));
2576 return;
2577 }
2578
2579 if (QueryComplete) {
2580 LLVM_DEBUG(dbgs() << "Completing query\n");
2581 Q->handleComplete(*this);
2582 }
2583
2584 dispatchOutstandingMUs();
2585}
2586
2587void ExecutionSession::OL_completeLookupFlags(
2588 std::unique_ptr<InProgressLookupState> IPLS,
2589 unique_function<void(Expected<SymbolFlagsMap>)> OnComplete) {
2590
2591 auto Result = runSessionLocked([&]() -> Expected<SymbolFlagsMap> {
2592 LLVM_DEBUG({
2593 dbgs() << "Entering OL_completeLookupFlags:\n"
2594 << " Lookup kind: " << IPLS->K << "\n"
2595 << " Search order: " << IPLS->SearchOrder
2596 << ", Current index = " << IPLS->CurSearchOrderIndex
2597 << (IPLS->NewJITDylib ? " (entering new JITDylib)" : "") << "\n"
2598 << " Lookup set: " << IPLS->LookupSet << "\n"
2599 << " Definition generator candidates: "
2600 << IPLS->DefGeneratorCandidates << "\n"
2601 << " Definition generator non-candidates: "
2602 << IPLS->DefGeneratorNonCandidates << "\n";
2603 });
2604
2606
2607 // Attempt to find flags for each symbol.
2608 for (auto &KV : IPLS->SearchOrder) {
2609 auto &JD = *KV.first;
2610 auto JDLookupFlags = KV.second;
2611 LLVM_DEBUG({
2612 dbgs() << "Visiting \"" << JD.getName() << "\" (" << JDLookupFlags
2613 << ") with lookup set " << IPLS->LookupSet << ":\n";
2614 });
2615
2616 IPLS->LookupSet.forEachWithRemoval([&](const SymbolStringPtr &Name,
2617 SymbolLookupFlags SymLookupFlags) {
2618 LLVM_DEBUG({
2619 dbgs() << " Attempting to match \"" << Name << "\" ("
2620 << SymLookupFlags << ")... ";
2621 });
2622
2623 // Search for the symbol. If not found then continue without removing
2624 // from the lookup set.
2625 auto SymI = JD.Symbols.find(Name);
2626 if (SymI == JD.Symbols.end()) {
2627 LLVM_DEBUG(dbgs() << "skipping: not present\n");
2628 return false;
2629 }
2630
2631 // If this is a non-exported symbol then it doesn't match. Skip it.
2632 if (!SymI->second.getFlags().isExported() &&
2634 LLVM_DEBUG(dbgs() << "skipping: not exported\n");
2635 return false;
2636 }
2637
2638 LLVM_DEBUG({
2639 dbgs() << "matched, \"" << Name << "\" -> " << SymI->second.getFlags()
2640 << "\n";
2641 });
2642 Result[Name] = SymI->second.getFlags();
2643 return true;
2644 });
2645 }
2646
2647 // Remove any weakly referenced symbols that haven't been resolved.
2648 IPLS->LookupSet.remove_if(
2649 [](const SymbolStringPtr &Name, SymbolLookupFlags SymLookupFlags) {
2650 return SymLookupFlags == SymbolLookupFlags::WeaklyReferencedSymbol;
2651 });
2652
2653 if (!IPLS->LookupSet.empty()) {
2654 LLVM_DEBUG(dbgs() << "Failing due to unresolved symbols\n");
2655 return make_error<SymbolsNotFound>(getSymbolStringPool(),
2656 IPLS->LookupSet.getSymbolNames());
2657 }
2658
2659 LLVM_DEBUG(dbgs() << "Succeded, result = " << Result << "\n");
2660 return Result;
2661 });
2662
2663 // Run the callback on the result.
2664 LLVM_DEBUG(dbgs() << "Sending result to handler.\n");
2665 OnComplete(std::move(Result));
2666}
2667
2668void ExecutionSession::OL_destroyMaterializationResponsibility(
2669 MaterializationResponsibility &MR) {
2670
2671 assert(MR.SymbolFlags.empty() &&
2672 "All symbols should have been explicitly materialized or failed");
2673 MR.JD.unlinkMaterializationResponsibility(MR);
2674}
2675
2676SymbolNameSet ExecutionSession::OL_getRequestedSymbols(
2677 const MaterializationResponsibility &MR) {
2678 return MR.JD.getRequestedSymbols(MR.SymbolFlags);
2679}
2680
2681Error ExecutionSession::OL_notifyResolved(MaterializationResponsibility &MR,
2682 const SymbolMap &Symbols) {
2683 LLVM_DEBUG({
2684 dbgs() << "In " << MR.JD.getName() << " resolving " << Symbols << "\n";
2685 });
2686#ifndef NDEBUG
2687 for (auto &KV : Symbols) {
2688 auto I = MR.SymbolFlags.find(KV.first);
2689 assert(I != MR.SymbolFlags.end() &&
2690 "Resolving symbol outside this responsibility set");
2691 assert(!I->second.hasMaterializationSideEffectsOnly() &&
2692 "Can't resolve materialization-side-effects-only symbol");
2693 assert((KV.second.getFlags() & ~JITSymbolFlags::Common) ==
2694 (I->second & ~JITSymbolFlags::Common) &&
2695 "Resolving symbol with incorrect flags");
2696 }
2697#endif
2698
2699 return MR.JD.resolve(MR, Symbols);
2700}
2701
2702template <typename HandleNewDepFn>
2703void ExecutionSession::propagateExtraEmitDeps(
2704 std::deque<JITDylib::EmissionDepUnit *> Worklist, EDUInfosMap &EDUInfos,
2705 HandleNewDepFn HandleNewDep) {
2706
2707 // Iterate to a fixed-point to propagate extra-emit dependencies through the
2708 // EDU graph.
2709 while (!Worklist.empty()) {
2710 auto &EDU = *Worklist.front();
2711 Worklist.pop_front();
2712
2713 assert(EDUInfos.count(&EDU) && "No info entry for EDU");
2714 auto &EDUInfo = EDUInfos[&EDU];
2715
2716 // Propagate new dependencies to users.
2717 for (auto *UserEDU : EDUInfo.IntraEmitUsers) {
2718
2719 // UserEDUInfo only present if UserEDU has its own users.
2720 JITDylib::EmissionDepUnitInfo *UserEDUInfo = nullptr;
2721 {
2722 auto UserEDUInfoItr = EDUInfos.find(UserEDU);
2723 if (UserEDUInfoItr != EDUInfos.end())
2724 UserEDUInfo = &UserEDUInfoItr->second;
2725 }
2726
2727 for (auto &[DepJD, Deps] : EDUInfo.NewDeps) {
2728 auto &UserEDUDepsForJD = UserEDU->Dependencies[DepJD];
2729 DenseSet<NonOwningSymbolStringPtr> *UserEDUNewDepsForJD = nullptr;
2730 for (auto Dep : Deps) {
2731 if (UserEDUDepsForJD.insert(Dep).second) {
2732 HandleNewDep(*UserEDU, *DepJD, Dep);
2733 if (UserEDUInfo) {
2734 if (!UserEDUNewDepsForJD) {
2735 // If UserEDU has no new deps then it's not in the worklist
2736 // yet, so add it.
2737 if (UserEDUInfo->NewDeps.empty())
2738 Worklist.push_back(UserEDU);
2739 UserEDUNewDepsForJD = &UserEDUInfo->NewDeps[DepJD];
2740 }
2741 // Add (DepJD, Dep) to NewDeps.
2742 UserEDUNewDepsForJD->insert(Dep);
2743 }
2744 }
2745 }
2746 }
2747 }
2748
2749 EDUInfo.NewDeps.clear();
2750 }
2751}
2752
2753// Note: This method modifies the emitted set.
2754ExecutionSession::EDUInfosMap ExecutionSession::simplifyDepGroups(
2755 MaterializationResponsibility &MR,
2756 ArrayRef<SymbolDependenceGroup> EmittedDeps) {
2757
2758 auto &TargetJD = MR.getTargetJITDylib();
2759
2760 // 1. Build initial EmissionDepUnit -> EmissionDepUnitInfo and
2761 // Symbol -> EmissionDepUnit mappings.
2762 DenseMap<JITDylib::EmissionDepUnit *, JITDylib::EmissionDepUnitInfo> EDUInfos;
2763 EDUInfos.reserve(EmittedDeps.size());
2764 DenseMap<NonOwningSymbolStringPtr, JITDylib::EmissionDepUnit *> EDUForSymbol;
2765 for (auto &DG : EmittedDeps) {
2766 assert(!DG.Symbols.empty() && "DepGroup does not cover any symbols");
2767
2768 // Skip empty EDUs.
2769 if (DG.Dependencies.empty())
2770 continue;
2771
2772 auto TmpEDU = std::make_shared<JITDylib::EmissionDepUnit>(TargetJD);
2773 auto &EDUInfo = EDUInfos[TmpEDU.get()];
2774 EDUInfo.EDU = std::move(TmpEDU);
2775 for (const auto &Symbol : DG.Symbols) {
2776 NonOwningSymbolStringPtr NonOwningSymbol(Symbol);
2777 assert(!EDUForSymbol.count(NonOwningSymbol) &&
2778 "Symbol should not appear in more than one SymbolDependenceGroup");
2779 assert(MR.getSymbols().count(Symbol) &&
2780 "Symbol in DepGroups not in the emitted set");
2781 auto NewlyEmittedItr = MR.getSymbols().find(Symbol);
2782 EDUInfo.EDU->Symbols[NonOwningSymbol] = NewlyEmittedItr->second;
2783 EDUForSymbol[NonOwningSymbol] = EDUInfo.EDU.get();
2784 }
2785 }
2786
2787 // 2. Build a "residual" EDU to cover all symbols that have no dependencies.
2788 {
2789 DenseMap<NonOwningSymbolStringPtr, JITSymbolFlags> ResidualSymbolFlags;
2790 for (auto &[Sym, Flags] : MR.getSymbols()) {
2791 if (!EDUForSymbol.count(NonOwningSymbolStringPtr(Sym)))
2792 ResidualSymbolFlags[NonOwningSymbolStringPtr(Sym)] = Flags;
2793 }
2794 if (!ResidualSymbolFlags.empty()) {
2795 auto ResidualEDU = std::make_shared<JITDylib::EmissionDepUnit>(TargetJD);
2796 ResidualEDU->Symbols = std::move(ResidualSymbolFlags);
2797 auto &ResidualEDUInfo = EDUInfos[ResidualEDU.get()];
2798 ResidualEDUInfo.EDU = std::move(ResidualEDU);
2799
2800 // If the residual EDU is the only one then bail out early.
2801 if (EDUInfos.size() == 1)
2802 return EDUInfos;
2803
2804 // Otherwise add the residual EDU to the EDUForSymbol map.
2805 for (auto &[Sym, Flags] : ResidualEDUInfo.EDU->Symbols)
2806 EDUForSymbol[Sym] = ResidualEDUInfo.EDU.get();
2807 }
2808 }
2809
2810#ifndef NDEBUG
2811 assert(EDUForSymbol.size() == MR.getSymbols().size() &&
2812 "MR symbols not fully covered by EDUs?");
2813 for (auto &[Sym, Flags] : MR.getSymbols()) {
2814 assert(EDUForSymbol.count(NonOwningSymbolStringPtr(Sym)) &&
2815 "Sym in MR not covered by EDU");
2816 }
2817#endif // NDEBUG
2818
2819 // 3. Use the DepGroups array to build a graph of dependencies between
2820 // EmissionDepUnits in this finalization. We want to remove these
2821 // intra-finalization uses, propagating dependencies on symbols outside
2822 // this finalization. Add EDUs to the worklist.
2823 for (auto &DG : EmittedDeps) {
2824
2825 // Skip SymbolDependenceGroups with no dependencies.
2826 if (DG.Dependencies.empty())
2827 continue;
2828
2829 assert(EDUForSymbol.count(NonOwningSymbolStringPtr(*DG.Symbols.begin())) &&
2830 "No EDU for DG");
2831 auto &EDU =
2832 *EDUForSymbol.find(NonOwningSymbolStringPtr(*DG.Symbols.begin()))
2833 ->second;
2834
2835 for (auto &[DepJD, Deps] : DG.Dependencies) {
2836 DenseSet<NonOwningSymbolStringPtr> NewDepsForJD;
2837
2838 assert(!Deps.empty() && "Dependence set for DepJD is empty");
2839
2840 if (DepJD != &TargetJD) {
2841 // DepJD is some other JITDylib.There can't be any intra-finalization
2842 // edges here, so just skip.
2843 for (auto &Dep : Deps)
2844 NewDepsForJD.insert(NonOwningSymbolStringPtr(Dep));
2845 } else {
2846 // DepJD is the Target JITDylib. Check for intra-finaliztaion edges,
2847 // skipping any and recording the intra-finalization use instead.
2848 for (auto &Dep : Deps) {
2849 NonOwningSymbolStringPtr NonOwningDep(Dep);
2850 auto I = EDUForSymbol.find(NonOwningDep);
2851 if (I == EDUForSymbol.end()) {
2852 if (!MR.getSymbols().count(Dep))
2853 NewDepsForJD.insert(NonOwningDep);
2854 continue;
2855 }
2856
2857 if (I->second != &EDU)
2858 EDUInfos[I->second].IntraEmitUsers.insert(&EDU);
2859 }
2860 }
2861
2862 if (!NewDepsForJD.empty())
2863 EDU.Dependencies[DepJD] = std::move(NewDepsForJD);
2864 }
2865 }
2866
2867 // 4. Build the worklist.
2868 std::deque<JITDylib::EmissionDepUnit *> Worklist;
2869 for (auto &[EDU, EDUInfo] : EDUInfos) {
2870 // If this EDU has extra-finalization dependencies and intra-finalization
2871 // users then add it to the worklist.
2872 if (!EDU->Dependencies.empty()) {
2873 auto I = EDUInfos.find(EDU);
2874 if (I != EDUInfos.end()) {
2875 auto &EDUInfo = I->second;
2876 if (!EDUInfo.IntraEmitUsers.empty()) {
2877 EDUInfo.NewDeps = EDU->Dependencies;
2878 Worklist.push_back(EDU);
2879 }
2880 }
2881 }
2882 }
2883
2884 // 4. Propagate dependencies through the EDU graph.
2885 propagateExtraEmitDeps(
2886 Worklist, EDUInfos,
2887 [](JITDylib::EmissionDepUnit &, JITDylib &, NonOwningSymbolStringPtr) {});
2888
2889 return EDUInfos;
2890}
2891
2892void ExecutionSession::IL_makeEDUReady(
2893 std::shared_ptr<JITDylib::EmissionDepUnit> EDU,
2894 JITDylib::AsynchronousSymbolQuerySet &Queries) {
2895
2896 // The symbols for this EDU are ready.
2897 auto &JD = *EDU->JD;
2898
2899 for (auto &[Sym, Flags] : EDU->Symbols) {
2900 assert(JD.Symbols.count(SymbolStringPtr(Sym)) &&
2901 "JD does not have an entry for Sym");
2902 auto &Entry = JD.Symbols[SymbolStringPtr(Sym)];
2903
2904 assert(((Entry.getFlags().hasMaterializationSideEffectsOnly() &&
2905 Entry.getState() == SymbolState::Materializing) ||
2906 Entry.getState() == SymbolState::Resolved ||
2907 Entry.getState() == SymbolState::Emitted) &&
2908 "Emitting from state other than Resolved");
2909
2910 Entry.setState(SymbolState::Ready);
2911
2912 auto MII = JD.MaterializingInfos.find(SymbolStringPtr(Sym));
2913
2914 // Check for pending queries.
2915 if (MII == JD.MaterializingInfos.end())
2916 continue;
2917 auto &MI = MII->second;
2918
2919 for (auto &Q : MI.takeQueriesMeeting(SymbolState::Ready)) {
2920 Q->notifySymbolMetRequiredState(SymbolStringPtr(Sym), Entry.getSymbol());
2921 if (Q->isComplete())
2922 Queries.insert(Q);
2923 Q->removeQueryDependence(JD, SymbolStringPtr(Sym));
2924 }
2925
2926 JD.MaterializingInfos.erase(MII);
2927 }
2928}
2929
2930void ExecutionSession::IL_makeEDUEmitted(
2931 std::shared_ptr<JITDylib::EmissionDepUnit> EDU,
2932 JITDylib::AsynchronousSymbolQuerySet &Queries) {
2933
2934 // The symbols for this EDU are emitted, but not ready.
2935 auto &JD = *EDU->JD;
2936
2937 for (auto &[Sym, Flags] : EDU->Symbols) {
2938 assert(JD.Symbols.count(SymbolStringPtr(Sym)) &&
2939 "JD does not have an entry for Sym");
2940 auto &Entry = JD.Symbols[SymbolStringPtr(Sym)];
2941
2942 assert(((Entry.getFlags().hasMaterializationSideEffectsOnly() &&
2943 Entry.getState() == SymbolState::Materializing) ||
2944 Entry.getState() == SymbolState::Resolved ||
2945 Entry.getState() == SymbolState::Emitted) &&
2946 "Emitting from state other than Resolved");
2947
2948 if (Entry.getState() == SymbolState::Emitted) {
2949 // This was already emitted, so we can skip the rest of this loop.
2950#ifndef NDEBUG
2951 for (auto &[Sym, Flags] : EDU->Symbols) {
2952 assert(JD.Symbols.count(SymbolStringPtr(Sym)) &&
2953 "JD does not have an entry for Sym");
2954 auto &Entry = JD.Symbols[SymbolStringPtr(Sym)];
2955 assert(Entry.getState() == SymbolState::Emitted &&
2956 "Symbols for EDU in inconsistent state");
2957 assert(JD.MaterializingInfos.count(SymbolStringPtr(Sym)) &&
2958 "Emitted symbol has no MI");
2959 auto MI = JD.MaterializingInfos[SymbolStringPtr(Sym)];
2960 assert(MI.takeQueriesMeeting(SymbolState::Emitted).empty() &&
2961 "Already-emitted symbol has waiting-on-emitted queries");
2962 }
2963#endif // NDEBUG
2964 break;
2965 }
2966
2967 Entry.setState(SymbolState::Emitted);
2968 auto &MI = JD.MaterializingInfos[SymbolStringPtr(Sym)];
2969 MI.DefiningEDU = EDU;
2970
2971 for (auto &Q : MI.takeQueriesMeeting(SymbolState::Emitted)) {
2972 Q->notifySymbolMetRequiredState(SymbolStringPtr(Sym), Entry.getSymbol());
2973 if (Q->isComplete())
2974 Queries.insert(Q);
2975 Q->removeQueryDependence(JD, SymbolStringPtr(Sym));
2976 }
2977 }
2978
2979 for (auto &[DepJD, Deps] : EDU->Dependencies) {
2980 for (auto &Dep : Deps)
2981 DepJD->MaterializingInfos[SymbolStringPtr(Dep)].DependantEDUs.insert(
2982 EDU.get());
2983 }
2984}
2985
2986/// Removes the given dependence from EDU. If EDU's dependence set becomes
2987/// empty then this function adds an entry for it to the EDUInfos map.
2988/// Returns true if a new EDUInfosMap entry is added.
2989bool ExecutionSession::IL_removeEDUDependence(JITDylib::EmissionDepUnit &EDU,
2990 JITDylib &DepJD,
2991 NonOwningSymbolStringPtr DepSym,
2992 EDUInfosMap &EDUInfos) {
2993 assert(EDU.Dependencies.count(&DepJD) &&
2994 "JD does not appear in Dependencies of DependantEDU");
2995 assert(EDU.Dependencies[&DepJD].count(DepSym) &&
2996 "Symbol does not appear in Dependencies of DependantEDU");
2997 auto &JDDeps = EDU.Dependencies[&DepJD];
2998 JDDeps.erase(DepSym);
2999 if (JDDeps.empty()) {
3000 EDU.Dependencies.erase(&DepJD);
3001 if (EDU.Dependencies.empty()) {
3002 // If the dependencies set has become empty then EDU _may_ be ready
3003 // (we won't know for sure until we've propagated the extra-emit deps).
3004 // Create an EDUInfo for it (if it doesn't have one already) so that
3005 // it'll be visited after propagation.
3006 auto &DepEDUInfo = EDUInfos[&EDU];
3007 if (!DepEDUInfo.EDU) {
3008 assert(EDU.JD->Symbols.count(
3009 SymbolStringPtr(EDU.Symbols.begin()->first)) &&
3010 "Missing symbol entry for first symbol in EDU");
3011 auto DepEDUFirstMI = EDU.JD->MaterializingInfos.find(
3012 SymbolStringPtr(EDU.Symbols.begin()->first));
3013 assert(DepEDUFirstMI != EDU.JD->MaterializingInfos.end() &&
3014 "Missing MI for first symbol in DependantEDU");
3015 DepEDUInfo.EDU = DepEDUFirstMI->second.DefiningEDU;
3016 return true;
3017 }
3018 }
3019 }
3020 return false;
3021}
3022
3023Error ExecutionSession::makeJDClosedError(JITDylib::EmissionDepUnit &EDU,
3024 JITDylib &ClosedJD) {
3025 SymbolNameSet FailedSymbols;
3026 for (auto &[Sym, Flags] : EDU.Symbols)
3027 FailedSymbols.insert(SymbolStringPtr(Sym));
3028 SymbolDependenceMap BadDeps;
3029 for (auto &Dep : EDU.Dependencies[&ClosedJD])
3030 BadDeps[&ClosedJD].insert(SymbolStringPtr(Dep));
3031 return make_error<UnsatisfiedSymbolDependencies>(
3032 ClosedJD.getExecutionSession().getSymbolStringPool(), EDU.JD,
3033 std::move(FailedSymbols), std::move(BadDeps),
3034 ClosedJD.getName() + " is closed");
3035}
3036
3037Error ExecutionSession::makeUnsatisfiedDepsError(JITDylib::EmissionDepUnit &EDU,
3038 JITDylib &BadJD,
3039 SymbolNameSet BadDeps) {
3040 SymbolNameSet FailedSymbols;
3041 for (auto &[Sym, Flags] : EDU.Symbols)
3042 FailedSymbols.insert(SymbolStringPtr(Sym));
3043 SymbolDependenceMap BadDepsMap;
3044 BadDepsMap[&BadJD] = std::move(BadDeps);
3045 return make_error<UnsatisfiedSymbolDependencies>(
3046 BadJD.getExecutionSession().getSymbolStringPool(), &BadJD,
3047 std::move(FailedSymbols), std::move(BadDepsMap),
3048 "dependencies removed or in error state");
3049}
3050
3051Expected<JITDylib::AsynchronousSymbolQuerySet>
3052ExecutionSession::IL_emit(MaterializationResponsibility &MR,
3053 EDUInfosMap EDUInfos) {
3054
3055 if (MR.RT->isDefunct())
3056 return make_error<ResourceTrackerDefunct>(MR.RT);
3057
3058 auto &TargetJD = MR.getTargetJITDylib();
3059 if (TargetJD.State != JITDylib::Open)
3060 return make_error<StringError>("JITDylib " + TargetJD.getName() +
3061 " is defunct",
3063
3064 // Walk all EDUs:
3065 // 1. Verifying that dependencies are available (not removed or in the error
3066 // state.
3067 // 2. Removing any dependencies that are already Ready.
3068 // 3. Lifting any EDUs for Emitted symbols into the EDUInfos map.
3069 // 4. Finding any dependant EDUs and lifting them into the EDUInfos map.
3070 std::deque<JITDylib::EmissionDepUnit *> Worklist;
3071 for (auto &[EDU, _] : EDUInfos)
3072 Worklist.push_back(EDU);
3073
3074 for (auto *EDU : Worklist) {
3075 auto *EDUInfo = &EDUInfos[EDU];
3076
3077 SmallVector<JITDylib *> DepJDsToRemove;
3078 for (auto &[DepJD, Deps] : EDU->Dependencies) {
3079 if (DepJD->State != JITDylib::Open)
3080 return makeJDClosedError(*EDU, *DepJD);
3081
3082 SymbolNameSet BadDeps;
3083 SmallVector<NonOwningSymbolStringPtr> DepsToRemove;
3084 for (auto &Dep : Deps) {
3085 auto DepEntryItr = DepJD->Symbols.find(SymbolStringPtr(Dep));
3086
3087 // If this dep has been removed or moved to the error state then add it
3088 // to the bad deps set. We aggregate these bad deps for more
3089 // comprehensive error messages.
3090 if (DepEntryItr == DepJD->Symbols.end() ||
3091 DepEntryItr->second.getFlags().hasError()) {
3092 BadDeps.insert(SymbolStringPtr(Dep));
3093 continue;
3094 }
3095
3096 // If this dep isn't emitted yet then just add it to the NewDeps set to
3097 // be propagated.
3098 auto &DepEntry = DepEntryItr->second;
3099 if (DepEntry.getState() < SymbolState::Emitted) {
3100 EDUInfo->NewDeps[DepJD].insert(Dep);
3101 continue;
3102 }
3103
3104 // This dep has been emitted, so add it to the list to be removed from
3105 // EDU.
3106 DepsToRemove.push_back(Dep);
3107
3108 // If Dep is Ready then there's nothing further to do.
3109 if (DepEntry.getState() == SymbolState::Ready) {
3110 assert(!DepJD->MaterializingInfos.count(SymbolStringPtr(Dep)) &&
3111 "Unexpected MaterializationInfo attached to ready symbol");
3112 continue;
3113 }
3114
3115 // If we get here thene Dep is Emitted. We need to look up its defining
3116 // EDU and add this EDU to the defining EDU's list of users (this means
3117 // creating an EDUInfos entry if the defining EDU doesn't have one
3118 // already).
3119 assert(DepJD->MaterializingInfos.count(SymbolStringPtr(Dep)) &&
3120 "Expected MaterializationInfo for emitted dependency");
3121 auto &DepMI = DepJD->MaterializingInfos[SymbolStringPtr(Dep)];
3122 assert(DepMI.DefiningEDU &&
3123 "Emitted symbol does not have a defining EDU");
3124 assert(!DepMI.DefiningEDU->Dependencies.empty() &&
3125 "Emitted symbol has empty dependencies (should be ready)");
3126 assert(DepMI.DependantEDUs.empty() &&
3127 "Already-emitted symbol has dependant EDUs?");
3128 auto &DepEDUInfo = EDUInfos[DepMI.DefiningEDU.get()];
3129 if (!DepEDUInfo.EDU) {
3130 // No EDUInfo yet -- build initial entry, and reset the EDUInfo
3131 // pointer, which we will have invalidated.
3132 EDUInfo = &EDUInfos[EDU];
3133 DepEDUInfo.EDU = DepMI.DefiningEDU;
3134 for (auto &[DepDepJD, DepDeps] : DepEDUInfo.EDU->Dependencies) {
3135 if (DepDepJD == &TargetJD) {
3136 for (auto &DepDep : DepDeps)
3137 if (!MR.getSymbols().count(SymbolStringPtr(DepDep)))
3138 DepEDUInfo.NewDeps[DepDepJD].insert(DepDep);
3139 } else
3140 DepEDUInfo.NewDeps[DepDepJD] = DepDeps;
3141 }
3142 }
3143 DepEDUInfo.IntraEmitUsers.insert(EDU);
3144 }
3145
3146 // Some dependencies were removed or in an error state -- error out.
3147 if (!BadDeps.empty())
3148 return makeUnsatisfiedDepsError(*EDU, *DepJD, std::move(BadDeps));
3149
3150 // Remove the emitted / ready deps from DepJD.
3151 for (auto &Dep : DepsToRemove)
3152 Deps.erase(Dep);
3153
3154 // If there are no further deps in DepJD then flag it for removal too.
3155 if (Deps.empty())
3156 DepJDsToRemove.push_back(DepJD);
3157 }
3158
3159 // Remove any JDs whose dependence sets have become empty.
3160 for (auto &DepJD : DepJDsToRemove) {
3161 assert(EDU->Dependencies.count(DepJD) &&
3162 "Trying to remove non-existent dep entries");
3163 EDU->Dependencies.erase(DepJD);
3164 }
3165
3166 // Now look for users of this EDU.
3167 for (auto &[Sym, Flags] : EDU->Symbols) {
3168 assert(TargetJD.Symbols.count(SymbolStringPtr(Sym)) &&
3169 "Sym not present in symbol table");
3170 assert((TargetJD.Symbols[SymbolStringPtr(Sym)].getState() ==
3172 TargetJD.Symbols[SymbolStringPtr(Sym)]
3173 .getFlags()
3174 .hasMaterializationSideEffectsOnly()) &&
3175 "Emitting symbol not in the resolved state");
3176 assert(!TargetJD.Symbols[SymbolStringPtr(Sym)].getFlags().hasError() &&
3177 "Symbol is already in an error state");
3178
3179 auto MII = TargetJD.MaterializingInfos.find(SymbolStringPtr(Sym));
3180 if (MII == TargetJD.MaterializingInfos.end() ||
3181 MII->second.DependantEDUs.empty())
3182 continue;
3183
3184 for (auto &DependantEDU : MII->second.DependantEDUs) {
3185 if (IL_removeEDUDependence(*DependantEDU, TargetJD, Sym, EDUInfos))
3186 EDUInfo = &EDUInfos[EDU];
3187 EDUInfo->IntraEmitUsers.insert(DependantEDU);
3188 }
3189 MII->second.DependantEDUs.clear();
3190 }
3191 }
3192
3193 Worklist.clear();
3194 for (auto &[EDU, EDUInfo] : EDUInfos) {
3195 if (!EDUInfo.IntraEmitUsers.empty() && !EDU->Dependencies.empty()) {
3196 if (EDUInfo.NewDeps.empty())
3197 EDUInfo.NewDeps = EDU->Dependencies;
3198 Worklist.push_back(EDU);
3199 }
3200 }
3201
3202 propagateExtraEmitDeps(
3203 Worklist, EDUInfos,
3204 [](JITDylib::EmissionDepUnit &EDU, JITDylib &JD,
3205 NonOwningSymbolStringPtr Sym) {
3206 JD.MaterializingInfos[SymbolStringPtr(Sym)].DependantEDUs.insert(&EDU);
3207 });
3208
3209 JITDylib::AsynchronousSymbolQuerySet CompletedQueries;
3210
3211 // Extract completed queries and lodge not-yet-ready EDUs in the
3212 // session.
3213 for (auto &[EDU, EDUInfo] : EDUInfos) {
3214 if (EDU->Dependencies.empty())
3215 IL_makeEDUReady(std::move(EDUInfo.EDU), CompletedQueries);
3216 else
3217 IL_makeEDUEmitted(std::move(EDUInfo.EDU), CompletedQueries);
3218 }
3219
3220 return std::move(CompletedQueries);
3221}
3222
3223Error ExecutionSession::OL_notifyEmitted(
3224 MaterializationResponsibility &MR,
3225 ArrayRef<SymbolDependenceGroup> DepGroups) {
3226 LLVM_DEBUG({
3227 dbgs() << "In " << MR.JD.getName() << " emitting " << MR.SymbolFlags
3228 << "\n";
3229 if (!DepGroups.empty()) {
3230 dbgs() << " Initial dependencies:\n";
3231 for (auto &SDG : DepGroups) {
3232 dbgs() << " Symbols: " << SDG.Symbols
3233 << ", Dependencies: " << SDG.Dependencies << "\n";
3234 }
3235 }
3236 });
3237
3238#ifndef NDEBUG
3239 SymbolNameSet Visited;
3240 for (auto &DG : DepGroups) {
3241 for (auto &Sym : DG.Symbols) {
3242 assert(MR.SymbolFlags.count(Sym) &&
3243 "DG contains dependence for symbol outside this MR");
3244 assert(Visited.insert(Sym).second &&
3245 "DG contains duplicate entries for Name");
3246 }
3247 }
3248#endif // NDEBUG
3249
3250 auto EDUInfos = simplifyDepGroups(MR, DepGroups);
3251
3252 LLVM_DEBUG({
3253 dbgs() << " Simplified dependencies:\n";
3254 for (auto &[EDU, EDUInfo] : EDUInfos) {
3255 dbgs() << " Symbols: { ";
3256 for (auto &[Sym, Flags] : EDU->Symbols)
3257 dbgs() << Sym << " ";
3258 dbgs() << "}, Dependencies: { ";
3259 for (auto &[DepJD, Deps] : EDU->Dependencies) {
3260 dbgs() << "(" << DepJD->getName() << ", { ";
3261 for (auto &Dep : Deps)
3262 dbgs() << Dep << " ";
3263 dbgs() << "}) ";
3264 }
3265 dbgs() << "}\n";
3266 }
3267 });
3268
3269 auto CompletedQueries =
3270 runSessionLocked([&]() { return IL_emit(MR, EDUInfos); });
3271
3272 // On error bail out.
3273 if (!CompletedQueries)
3274 return CompletedQueries.takeError();
3275
3276 MR.SymbolFlags.clear();
3277
3278 // Otherwise notify all the completed queries.
3279 for (auto &Q : *CompletedQueries) {
3280 assert(Q->isComplete() && "Q is not complete");
3281 Q->handleComplete(*this);
3282 }
3283
3284 return Error::success();
3285}
3286
3287Error ExecutionSession::OL_defineMaterializing(
3288 MaterializationResponsibility &MR, SymbolFlagsMap NewSymbolFlags) {
3289
3290 LLVM_DEBUG({
3291 dbgs() << "In " << MR.JD.getName() << " defining materializing symbols "
3292 << NewSymbolFlags << "\n";
3293 });
3294 if (auto AcceptedDefs =
3295 MR.JD.defineMaterializing(MR, std::move(NewSymbolFlags))) {
3296 // Add all newly accepted symbols to this responsibility object.
3297 for (auto &KV : *AcceptedDefs)
3298 MR.SymbolFlags.insert(KV);
3299 return Error::success();
3300 } else
3301 return AcceptedDefs.takeError();
3302}
3303
3304std::pair<JITDylib::AsynchronousSymbolQuerySet,
3305 std::shared_ptr<SymbolDependenceMap>>
3306ExecutionSession::IL_failSymbols(JITDylib &JD,
3307 const SymbolNameVector &SymbolsToFail) {
3308 JITDylib::AsynchronousSymbolQuerySet FailedQueries;
3309 auto FailedSymbolsMap = std::make_shared<SymbolDependenceMap>();
3310 auto ExtractFailedQueries = [&](JITDylib::MaterializingInfo &MI) {
3311 JITDylib::AsynchronousSymbolQueryList ToDetach;
3312 for (auto &Q : MI.pendingQueries()) {
3313 // Add the query to the list to be failed and detach it.
3314 FailedQueries.insert(Q);
3315 ToDetach.push_back(Q);
3316 }
3317 for (auto &Q : ToDetach)
3318 Q->detach();
3319 assert(!MI.hasQueriesPending() && "Queries still pending after detach");
3320 };
3321
3322 for (auto &Name : SymbolsToFail) {
3323 (*FailedSymbolsMap)[&JD].insert(Name);
3324
3325 // Look up the symbol to fail.
3326 auto SymI = JD.Symbols.find(Name);
3327
3328 // FIXME: Revisit this. We should be able to assert sequencing between
3329 // ResourceTracker removal and symbol failure.
3330 //
3331 // It's possible that this symbol has already been removed, e.g. if a
3332 // materialization failure happens concurrently with a ResourceTracker or
3333 // JITDylib removal. In that case we can safely skip this symbol and
3334 // continue.
3335 if (SymI == JD.Symbols.end())
3336 continue;
3337 auto &Sym = SymI->second;
3338
3339 // If the symbol is already in the error state then we must have visited
3340 // it earlier.
3341 if (Sym.getFlags().hasError()) {
3342 assert(!JD.MaterializingInfos.count(Name) &&
3343 "Symbol in error state still has MaterializingInfo");
3344 continue;
3345 }
3346
3347 // Move the symbol into the error state.
3348 Sym.setFlags(Sym.getFlags() | JITSymbolFlags::HasError);
3349
3350 // FIXME: Come up with a sane mapping of state to
3351 // presence-of-MaterializingInfo so that we can assert presence / absence
3352 // here, rather than testing it.
3353 auto MII = JD.MaterializingInfos.find(Name);
3354 if (MII == JD.MaterializingInfos.end())
3355 continue;
3356
3357 auto &MI = MII->second;
3358
3359 // Collect queries to be failed for this MII.
3360 ExtractFailedQueries(MI);
3361
3362 if (MI.DefiningEDU) {
3363 // If there is a DefiningEDU for this symbol then remove this
3364 // symbol from it.
3365 assert(MI.DependantEDUs.empty() &&
3366 "Symbol with DefiningEDU should not have DependantEDUs");
3367 assert(Sym.getState() >= SymbolState::Emitted &&
3368 "Symbol has EDU, should have been emitted");
3369 assert(MI.DefiningEDU->Symbols.count(NonOwningSymbolStringPtr(Name)) &&
3370 "Symbol does not appear in its DefiningEDU");
3371 MI.DefiningEDU->Symbols.erase(NonOwningSymbolStringPtr(Name));
3372 MI.DefiningEDU = nullptr;
3373 } else {
3374 // Otherwise if there are any EDUs waiting on this symbol then move
3375 // those symbols to the error state too, and deregister them from the
3376 // symbols that they depend on.
3377 // Note: We use a copy of DependantEDUs here since we'll be removing
3378 // from the original set as we go.
3379 for (auto &DependantEDU : MI.DependantEDUs) {
3380
3381 // Remove DependantEDU from all of its users DependantEDUs lists.
3382 for (auto &[JD, Syms] : DependantEDU->Dependencies) {
3383 for (auto Sym : Syms) {
3384 assert(JD->Symbols.count(SymbolStringPtr(Sym)) && "Sym not in JD?");
3385 assert(JD->MaterializingInfos.count(SymbolStringPtr(Sym)) &&
3386 "DependantEDU not registered with symbol it depends on");
3387 auto SymMI = JD->MaterializingInfos[SymbolStringPtr(Sym)];
3388 assert(SymMI.DependantEDUs.count(DependantEDU) &&
3389 "DependantEDU missing from DependantEDUs list");
3390 SymMI.DependantEDUs.erase(DependantEDU);
3391 }
3392 }
3393
3394 // Move any symbols defined by DependantEDU into the error state and
3395 // fail any queries waiting on them.
3396 auto &DepJD = *DependantEDU->JD;
3397 auto DepEDUSymbols = std::move(DependantEDU->Symbols);
3398 for (auto &[DepName, Flags] : DepEDUSymbols) {
3399 auto DepSymItr = DepJD.Symbols.find(SymbolStringPtr(DepName));
3400 assert(DepSymItr != DepJD.Symbols.end() &&
3401 "Symbol not present in table");
3402 auto &DepSym = DepSymItr->second;
3403
3404 assert(DepSym.getState() >= SymbolState::Emitted &&
3405 "Symbol has EDU, should have been emitted");
3406 assert(!DepSym.getFlags().hasError() &&
3407 "Symbol is already in the error state?");
3408 DepSym.setFlags(DepSym.getFlags() | JITSymbolFlags::HasError);
3409 (*FailedSymbolsMap)[&DepJD].insert(SymbolStringPtr(DepName));
3410
3411 // This symbol has a defining EDU so its MaterializingInfo object must
3412 // exist.
3413 auto DepMIItr =
3414 DepJD.MaterializingInfos.find(SymbolStringPtr(DepName));
3415 assert(DepMIItr != DepJD.MaterializingInfos.end() &&
3416 "Symbol has defining EDU but not MaterializingInfo");
3417 auto &DepMI = DepMIItr->second;
3418 assert(DepMI.DefiningEDU.get() == DependantEDU &&
3419 "Bad EDU dependence edge");
3420 assert(DepMI.DependantEDUs.empty() &&
3421 "Symbol was emitted, should not have any DependantEDUs");
3422 ExtractFailedQueries(DepMI);
3423 DepJD.MaterializingInfos.erase(SymbolStringPtr(DepName));
3424 }
3425 }
3426
3427 MI.DependantEDUs.clear();
3428 }
3429
3430 assert(!MI.DefiningEDU && "DefiningEDU should have been reset");
3431 assert(MI.DependantEDUs.empty() &&
3432 "DependantEDUs should have been removed above");
3433 assert(!MI.hasQueriesPending() &&
3434 "Can not delete MaterializingInfo with queries pending");
3435 JD.MaterializingInfos.erase(Name);
3436 }
3437
3438 return std::make_pair(std::move(FailedQueries), std::move(FailedSymbolsMap));
3439}
3440
3441void ExecutionSession::OL_notifyFailed(MaterializationResponsibility &MR) {
3442
3443 LLVM_DEBUG({
3444 dbgs() << "In " << MR.JD.getName() << " failing materialization for "
3445 << MR.SymbolFlags << "\n";
3446 });
3447
3448 if (MR.SymbolFlags.empty())
3449 return;
3450
3451 SymbolNameVector SymbolsToFail;
3452 for (auto &[Name, Flags] : MR.SymbolFlags)
3453 SymbolsToFail.push_back(Name);
3454 MR.SymbolFlags.clear();
3455
3456 JITDylib::AsynchronousSymbolQuerySet FailedQueries;
3457 std::shared_ptr<SymbolDependenceMap> FailedSymbols;
3458
3459 std::tie(FailedQueries, FailedSymbols) = runSessionLocked([&]() {
3460 // If the tracker is defunct then there's nothing to do here.
3461 if (MR.RT->isDefunct())
3462 return std::pair<JITDylib::AsynchronousSymbolQuerySet,
3463 std::shared_ptr<SymbolDependenceMap>>();
3464 return IL_failSymbols(MR.getTargetJITDylib(), SymbolsToFail);
3465 });
3466
3467 for (auto &Q : FailedQueries)
3468 Q->handleFailed(
3469 make_error<FailedToMaterialize>(getSymbolStringPool(), FailedSymbols));
3470}
3471
3472Error ExecutionSession::OL_replace(MaterializationResponsibility &MR,
3473 std::unique_ptr<MaterializationUnit> MU) {
3474 for (auto &KV : MU->getSymbols()) {
3475 assert(MR.SymbolFlags.count(KV.first) &&
3476 "Replacing definition outside this responsibility set");
3477 MR.SymbolFlags.erase(KV.first);
3478 }
3479
3480 if (MU->getInitializerSymbol() == MR.InitSymbol)
3481 MR.InitSymbol = nullptr;
3482
3483 LLVM_DEBUG(MR.JD.getExecutionSession().runSessionLocked([&]() {
3484 dbgs() << "In " << MR.JD.getName() << " replacing symbols with " << *MU
3485 << "\n";
3486 }););
3487
3488 return MR.JD.replace(MR, std::move(MU));
3489}
3490
3491Expected<std::unique_ptr<MaterializationResponsibility>>
3492ExecutionSession::OL_delegate(MaterializationResponsibility &MR,
3493 const SymbolNameSet &Symbols) {
3494
3495 SymbolStringPtr DelegatedInitSymbol;
3496 SymbolFlagsMap DelegatedFlags;
3497
3498 for (auto &Name : Symbols) {
3499 auto I = MR.SymbolFlags.find(Name);
3500 assert(I != MR.SymbolFlags.end() &&
3501 "Symbol is not tracked by this MaterializationResponsibility "
3502 "instance");
3503
3504 DelegatedFlags[Name] = std::move(I->second);
3505 if (Name == MR.InitSymbol)
3506 std::swap(MR.InitSymbol, DelegatedInitSymbol);
3507
3508 MR.SymbolFlags.erase(I);
3509 }
3510
3511 return MR.JD.delegate(MR, std::move(DelegatedFlags),
3512 std::move(DelegatedInitSymbol));
3513}
3514
3515#ifndef NDEBUG
3516void ExecutionSession::dumpDispatchInfo(Task &T) {
3517 runSessionLocked([&]() {
3518 dbgs() << "Dispatching: ";
3519 T.printDescription(dbgs());
3520 dbgs() << "\n";
3521 });
3522}
3523#endif // NDEBUG
3524
3525} // End namespace orc.
3526} // End namespace llvm.
for(const MachineOperand &MO :llvm::drop_begin(OldMI.operands(), Desc.getNumOperands()))
#define LLVM_DEBUG(X)
Definition: Debug.h:101
uint64_t Addr
std::string Name
Symbol * Sym
Definition: ELF_riscv.cpp:479
#define _
IRTranslator LLVM IR MI
#define F(x, y, z)
Definition: MD5.cpp:55
#define I(x, y, z)
Definition: MD5.cpp:58
#define G(x, y, z)
Definition: MD5.cpp:56
#define H(x, y, z)
Definition: MD5.cpp:57
while(!ToSimplify.empty())
if(VerifyEach)
static StringRef getName(Value *V)
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
This file contains some templates that are useful if you are working with the STL at all.
raw_pwrite_stream & OS
static uint32_t getFlags(const Symbol *Sym)
Definition: TapiFile.cpp:27
ArrayRef - Represent a constant reference to an array (0 or more elements consecutively in memory),...
Definition: ArrayRef.h:41
const T & front() const
front - Get the first element.
Definition: ArrayRef.h:168
size_t size() const
size - Get the array size.
Definition: ArrayRef.h:165
bool empty() const
empty - Check if the array is empty.
Definition: ArrayRef.h:160
const T * data() const
Definition: ArrayRef.h:162
iterator find(const_arg_type_t< KeyT > Val)
Definition: DenseMap.h:155
bool erase(const KeyT &Val)
Definition: DenseMap.h:329
unsigned size() const
Definition: DenseMap.h:99
bool empty() const
Definition: DenseMap.h:98
size_type count(const_arg_type_t< KeyT > Val) const
Return 1 if the specified key is in the map, 0 otherwise.
Definition: DenseMap.h:151
iterator end()
Definition: DenseMap.h:84
std::pair< iterator, bool > insert(const std::pair< KeyT, ValueT > &KV)
Definition: DenseMap.h:220
Helper for Errors used as out-parameters.
Definition: Error.h:1102
Lightweight error class with error context and mandatory checking.
Definition: Error.h:160
static ErrorSuccess success()
Create a success value.
Definition: Error.h:334
Tagged union holding either a T or a Error.
Definition: Error.h:474
void push_back(const T &Elt)
Definition: SmallVector.h:426
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small.
Definition: SmallVector.h:1209
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:50
Symbol info for RuntimeDyld.
std::pair< iterator, bool > insert(const ValueT &V)
Definition: DenseSet.h:206
iterator find(const_arg_type_t< ValueT > V)
Definition: DenseSet.h:179
size_type count(const_arg_type_t< ValueT > V) const
Return 1 if the specified key is in the set, 0 otherwise.
Definition: DenseSet.h:97
StringRef getName() const override
Return the name of this materialization unit.
Definition: Core.cpp:283
AbsoluteSymbolsMaterializationUnit(SymbolMap Symbols)
Definition: Core.cpp:279
AsynchronousSymbolQuery(const SymbolLookupSet &Symbols, SymbolState RequiredState, SymbolsResolvedCallback NotifyComplete)
Create a query for the given symbols.
Definition: Core.cpp:180
void notifySymbolMetRequiredState(const SymbolStringPtr &Name, ExecutorSymbolDef Sym)
Notify the query that a requested symbol has reached the required state.
Definition: Core.cpp:194
Definition generators can be attached to JITDylibs to generate new definitions for otherwise unresolv...
Definition: Core.h:946
An ExecutionSession represents a running JIT program.
Definition: Core.h:1425
Error endSession()
End the session.
Definition: Core.cpp:1604
void reportError(Error Err)
Report a error for this execution session.
Definition: Core.h:1563
friend class JITDylib
Definition: Core.h:1428
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.
Definition: Core.cpp:1760
SymbolStringPtr intern(StringRef SymName)
Add a symbol name to the SymbolStringPool and return a pointer to it.
Definition: Core.h:1482
JITDylib * getJITDylibByName(StringRef Name)
Return a pointer to the "name" JITDylib.
Definition: Core.cpp:1638
friend class LookupState
Definition: Core.h:1429
JITDylib & createBareJITDylib(std::string Name)
Add a new bare JITDylib to this ExecutionSession.
Definition: Core.cpp:1647
std::shared_ptr< SymbolStringPool > getSymbolStringPool()
Get the SymbolStringPool for this instance.
Definition: Core.h:1477
void lookup(LookupKind K, const JITDylibSearchOrder &SearchOrder, SymbolLookupSet Symbols, SymbolState RequiredState, SymbolsResolvedCallback NotifyComplete, RegisterDependenciesFunction RegisterDependencies)
Search the given JITDylibs for the given symbols.
Definition: Core.cpp:1786
Error registerJITDispatchHandlers(JITDylib &JD, JITDispatchHandlerAssociationMap WFs)
For each tag symbol name, associate the corresponding AsyncHandlerWrapperFunction with the address of...
Definition: Core.cpp:1895
void registerResourceManager(ResourceManager &RM)
Register the given ResourceManager with this ExecutionSession.
Definition: Core.cpp:1621
~ExecutionSession()
Destroy an ExecutionSession.
Definition: Core.cpp:1598
void runJITDispatchHandler(SendResultFunction SendResult, ExecutorAddr HandlerFnTagAddr, ArrayRef< char > ArgBuffer)
Run a registered jit-side wrapper function.
Definition: Core.cpp:1926
void deregisterResourceManager(ResourceManager &RM)
Deregister the given ResourceManager with this ExecutionSession.
Definition: Core.cpp:1625
ExecutionSession(std::unique_ptr< ExecutorProcessControl > EPC)
Construct an ExecutionSession with the given ExecutorProcessControl object.
Definition: Core.cpp:1592
decltype(auto) runSessionLocked(Func &&F)
Run the given lambda with the session mutex locked.
Definition: Core.h:1492
void dump(raw_ostream &OS)
Dump the state of all the JITDylibs in this session.
Definition: Core.cpp:1947
Error removeJITDylibs(std::vector< JITDylibSP > JDsToRemove)
Removes the given JITDylibs from the ExecutionSession.
Definition: Core.cpp:1664
Expected< JITDylib & > createJITDylib(std::string Name)
Add a new JITDylib to this ExecutionSession.
Definition: Core.cpp:1656
void dispatchTask(std::unique_ptr< Task > T)
Materialize the given unit.
Definition: Core.h:1642
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)
Definition: Core.cpp:81
std::error_code convertToErrorCode() const override
Convert this error to a std::error_code.
Definition: Core.cpp:99
void log(raw_ostream &OS) const override
Print an error message to an output stream.
Definition: Core.cpp:103
InProgressFullLookupState(LookupKind K, JITDylibSearchOrder SearchOrder, SymbolLookupSet LookupSet, SymbolState RequiredState, std::shared_ptr< AsynchronousSymbolQuery > Q, RegisterDependenciesFunction RegisterDependencies)
Definition: Core.cpp:585
void complete(std::unique_ptr< InProgressLookupState > IPLS) override
Definition: Core.cpp:595
void fail(Error Err) override
Definition: Core.cpp:601
void complete(std::unique_ptr< InProgressLookupState > IPLS) override
Definition: Core.cpp:572
void fail(Error Err) override
Definition: Core.cpp:577
InProgressLookupFlagsState(LookupKind K, JITDylibSearchOrder SearchOrder, SymbolLookupSet LookupSet, unique_function< void(Expected< SymbolFlagsMap >)> OnComplete)
Definition: Core.cpp:565
virtual ~InProgressLookupState()=default
enum llvm::orc::InProgressLookupState::@465 GenState
SymbolLookupSet DefGeneratorCandidates
Definition: Core.cpp:552
JITDylibSearchOrder SearchOrder
Definition: Core.cpp:546
std::vector< std::weak_ptr< DefinitionGenerator > > CurDefGeneratorStack
Definition: Core.cpp:560
SymbolLookupSet LookupSet
Definition: Core.cpp:547
virtual void complete(std::unique_ptr< InProgressLookupState > IPLS)=0
InProgressLookupState(LookupKind K, JITDylibSearchOrder SearchOrder, SymbolLookupSet LookupSet, SymbolState RequiredState)
Definition: Core.cpp:535
SymbolLookupSet DefGeneratorNonCandidates
Definition: Core.cpp:553
virtual void fail(Error Err)=0
Represents a JIT'd dynamic library.
Definition: Core.h:989
Error clear()
Calls remove on all trackers currently associated with this JITDylib.
Definition: Core.cpp:676
Error define(std::unique_ptr< MaterializationUnitType > &&MU, ResourceTrackerSP RT=nullptr)
Define all symbols provided by the materialization unit to be part of this JITDylib.
Definition: Core.h:1911
ExecutionSession & getExecutionSession() const
Get a reference to the ExecutionSession for this JITDylib.
Definition: Core.h:1008
ResourceTrackerSP createResourceTracker()
Create a resource tracker for this JITDylib.
Definition: Core.cpp:700
Expected< std::vector< JITDylibSP > > getReverseDFSLinkOrder()
Rteurn this JITDylib and its transitive dependencies in reverse DFS order based on linkage relationsh...
Definition: Core.cpp:1756
ResourceTrackerSP getDefaultResourceTracker()
Get the default resource tracker for this JITDylib.
Definition: Core.cpp:691
void removeGenerator(DefinitionGenerator &G)
Remove a definition generator from this JITDylib.
Definition: Core.cpp:708
Expected< std::vector< JITDylibSP > > getDFSLinkOrder()
Return this JITDylib and its transitive dependencies in DFS order based on linkage relationships.
Definition: Core.cpp:1752
Wraps state for a lookup-in-progress.
Definition: Core.h:921
void continueLookup(Error Err)
Continue the lookup.
Definition: Core.cpp:652
LookupState & operator=(LookupState &&)
void run() override
Definition: Core.cpp:1590
static char ID
Definition: Core.h:1414
void printDescription(raw_ostream &OS) override
Definition: Core.cpp:1588
Tracks responsibility for materialization, and mediates interactions between MaterializationUnits and...
Definition: Core.h:555
void printDescription(raw_ostream &OS) override
Definition: Core.cpp:1581
A MaterializationUnit represents a set of symbol definitions that can be materialized as a group,...
Definition: Core.h:693
SymbolFlagsMap SymbolFlags
Definition: Core.h:749
std::error_code convertToErrorCode() const override
Convert this error to a std::error_code.
Definition: Core.cpp:162
void log(raw_ostream &OS) const override
Print an error message to an output stream.
Definition: Core.cpp:166
Non-owning SymbolStringPool entry pointer.
static void lookupInitSymbolsAsync(unique_function< void(Error)> OnComplete, ExecutionSession &ES, const DenseMap< JITDylib *, SymbolLookupSet > &InitSyms)
Performs an async lookup for the given symbols in each of the given JITDylibs, calling the given hand...
Definition: Core.cpp:1538
static Expected< DenseMap< JITDylib *, SymbolMap > > lookupInitSymbols(ExecutionSession &ES, const DenseMap< JITDylib *, SymbolLookupSet > &InitSyms)
A utility function for looking up initializer symbols.
Definition: Core.cpp:1489
StringRef getName() const override
Return the name of this materialization unit.
Definition: Core.cpp:326
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...
Definition: Core.cpp:320
std::function< bool(SymbolStringPtr)> SymbolPredicate
Definition: Core.h:2000
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.
Definition: Core.cpp:617
ReexportsGenerator(JITDylib &SourceJD, JITDylibLookupFlags SourceJDLookupFlags, SymbolPredicate Allow=SymbolPredicate())
Create a reexports generator.
Definition: Core.cpp:611
Listens for ResourceTracker operations.
Definition: Core.h:104
ResourceTrackerDefunct(ResourceTrackerSP RT)
Definition: Core.cpp:70
void log(raw_ostream &OS) const override
Print an error message to an output stream.
Definition: Core.cpp:77
std::error_code convertToErrorCode() const override
Convert this error to a std::error_code.
Definition: Core.cpp:73
API to remove / transfer ownership of JIT resources.
Definition: Core.h:56
JITDylib & getJITDylib() const
Return the JITDylib targeted by this tracker.
Definition: Core.h:71
void transferTo(ResourceTracker &DstRT)
Transfer all resources associated with this key to the given tracker, which must target the same JITD...
Definition: Core.cpp:58
ResourceTracker(const ResourceTracker &)=delete
Error remove()
Remove all resources associated with this key.
Definition: Core.cpp:54
A set of symbols to look up, each associated with a SymbolLookupFlags value.
Definition: Core.h:183
static SymbolLookupSet fromMapKeys(const DenseMap< SymbolStringPtr, KeyT > &M, SymbolLookupFlags Flags=SymbolLookupFlags::RequiredSymbol)
Construct a SymbolLookupSet from DenseMap keys.
Definition: Core.h:232
Pointer to a pooled string representing a symbol name.
std::error_code convertToErrorCode() const override
Convert this error to a std::error_code.
Definition: Core.cpp:154
void log(raw_ostream &OS) const override
Print an error message to an output stream.
Definition: Core.cpp:158
SymbolsCouldNotBeRemoved(std::shared_ptr< SymbolStringPool > SSP, SymbolNameSet Symbols)
Definition: Core.cpp:148
void log(raw_ostream &OS) const override
Print an error message to an output stream.
Definition: Core.cpp:144
SymbolsNotFound(std::shared_ptr< SymbolStringPool > SSP, SymbolNameSet Symbols)
Definition: Core.cpp:126
std::error_code convertToErrorCode() const override
Convert this error to a std::error_code.
Definition: Core.cpp:140
Represents an abstract task for ORC to run.
Definition: TaskDispatch.h:34
std::error_code convertToErrorCode() const override
Convert this error to a std::error_code.
Definition: Core.cpp:171
void log(raw_ostream &OS) const override
Print an error message to an output stream.
Definition: Core.cpp:175
void log(raw_ostream &OS) const override
Print an error message to an output stream.
Definition: Core.cpp:119
std::error_code convertToErrorCode() const override
Convert this error to a std::error_code.
Definition: Core.cpp:115
UnsatisfiedSymbolDependencies(std::shared_ptr< SymbolStringPool > SSP, JITDylibSP JD, SymbolNameSet FailedSymbols, SymbolDependenceMap BadDeps, std::string Explanation)
Definition: Core.cpp:107
static WrapperFunctionResult createOutOfBandError(const char *Msg)
Create an out-of-band error by copying the given string.
This class implements an extremely fast bulk output stream that can only output to a stream.
Definition: raw_ostream.h:52
IntrusiveRefCntPtr< JITDylib > JITDylibSP
Definition: Core.h:51
JITDylibSearchOrder makeJITDylibSearchOrder(ArrayRef< JITDylib * > JDs, JITDylibLookupFlags Flags=JITDylibLookupFlags::MatchExportedSymbolsOnly)
Convenience function for creating a search order from an ArrayRef of JITDylib*, all with the same fla...
Definition: Core.h:166
std::vector< std::pair< JITDylib *, JITDylibLookupFlags > > JITDylibSearchOrder
A list of (JITDylib*, JITDylibLookupFlags) pairs to be used as a search order during symbol lookup.
Definition: Core.h:162
std::function< void(const SymbolDependenceMap &)> RegisterDependenciesFunction
Callback to register the dependencies for a given query.
Definition: Core.h:403
std::unique_ptr< ReExportsMaterializationUnit > symbolAliases(SymbolAliasMap Aliases)
Create a ReExportsMaterializationUnit with the given aliases.
Definition: Core.h:837
SymbolLookupFlags
Lookup flags that apply to each symbol in a lookup.
Definition: Core.h:145
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...
Definition: Core.h:846
DenseMap< SymbolStringPtr, SymbolAliasMapEntry > SymbolAliasMap
A map of Symbols to (Symbol, Flags) pairs.
Definition: Core.h:396
JITDylibLookupFlags
Lookup flags that apply to each dylib in the search order for a lookup.
Definition: Core.h:135
DenseMap< SymbolStringPtr, ExecutorSymbolDef > SymbolMap
A map from symbol names (as SymbolStringPtrs) to JITSymbols (address/flags pairs).
Definition: Core.h:121
DenseMap< SymbolStringPtr, JITSymbolFlags > SymbolFlagsMap
A map from symbol names (as SymbolStringPtrs) to JITSymbolFlags.
Definition: Core.h:124
unique_function< void(Expected< SymbolMap >)> SymbolsResolvedCallback
Callback to notify client that symbols have been resolved.
Definition: Core.h:399
DenseSet< SymbolStringPtr > SymbolNameSet
A set of symbol names (represented by SymbolStringPtrs for.
Definition: Core.h:114
LookupKind
Describes the kind of lookup being performed.
Definition: Core.h:157
RegisterDependenciesFunction NoDependenciesToRegister
This can be used as the value for a RegisterDependenciesFunction if there are no dependants to regist...
Definition: Core.cpp:37
std::vector< SymbolStringPtr > SymbolNameVector
A vector of symbol names.
Definition: Core.h:117
SymbolState
Represents the state that a symbol has reached during materialization.
Definition: Core.h:859
@ 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)
Definition: OrcError.cpp:84
DenseMap< JITDylib *, SymbolNameSet > SymbolDependenceMap
A map from JITDylibs to sets of symbols.
Definition: Core.h:127
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 ...
This is an optimization pass for GlobalISel generic memory operations.
Definition: AddressRanges.h:18
auto formatv(const char *Fmt, Ts &&... Vals) -> formatv_object< decltype(std::make_tuple(detail::build_format_adapter(std::forward< Ts >(Vals))...))>
auto find(R &&Range, const T &Val)
Provide wrappers to std::find which take ranges instead of having to pass begin/end explicitly.
Definition: STLExtras.h:1751
std::error_code inconvertibleErrorCode()
The value returned by this function can be returned from convertToErrorCode for Error values where no...
Definition: Error.cpp:90
void append_range(Container &C, Range &&R)
Wrapper function to append range R to container C.
Definition: STLExtras.h:2082
auto reverse(ContainerTy &&C)
Definition: STLExtras.h:428
Error joinErrors(Error E1, Error E2)
Concatenate errors.
Definition: Error.h:431
raw_ostream & dbgs()
dbgs() - This returns a reference to a raw_ostream for debugging messages.
Definition: Debug.cpp:163
format_object< Ts... > format(const char *Fmt, const Ts &... Vals)
These are helper functions used to produce formatted output.
Definition: Format.h:125
auto lower_bound(R &&Range, T &&Value)
Provide wrappers to std::lower_bound which take ranges instead of having to pass begin/end explicitly...
Definition: STLExtras.h:1963
OutputIt move(R &&Range, OutputIt Out)
Provide wrappers to std::move which take ranges instead of having to pass begin/end explicitly.
Definition: STLExtras.h:1858
auto find_if(R &&Range, UnaryPredicate P)
Provide wrappers to std::find_if which take ranges instead of having to pass begin/end explicitly.
Definition: STLExtras.h:1758
bool is_contained(R &&Range, const E &Element)
Returns true if Element is found in Range.
Definition: STLExtras.h:1888
Implement std::hash so that hash_code can be used in STL containers.
Definition: BitVector.h:858
void swap(llvm::BitVector &LHS, llvm::BitVector &RHS)
Implement std::swap in terms of BitVector swap.
Definition: BitVector.h:860
#define NDEBUG
Definition: regutils.h:48