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 
9 #include "llvm/ExecutionEngine/Orc/Core.h"
10 
11 #include "llvm/ADT/STLExtras.h"
12 #include "llvm/Config/llvm-config.h"
13 #include "llvm/ExecutionEngine/Orc/DebugUtils.h"
14 #include "llvm/ExecutionEngine/Orc/Shared/OrcError.h"
15 #include "llvm/Support/FormatVariadic.h"
16 #include "llvm/Support/MSVCErrorWorkarounds.h"
17 
18 #include <condition_variable>
19 #include <future>
20 
21 #define DEBUG_TYPE "orc"
22 
23 namespace llvm {
24 namespace orc {
25 
26 char ResourceTrackerDefunct::ID = 0;
27 char FailedToMaterialize::ID = 0;
28 char SymbolsNotFound::ID = 0;
29 char SymbolsCouldNotBeRemoved::ID = 0;
30 char MissingSymbolDefinitions::ID = 0;
31 char UnexpectedSymbolDefinitions::ID = 0;
32 char MaterializationTask::ID = 0;
33 
34 RegisterDependenciesFunction NoDependenciesToRegister =
35     RegisterDependenciesFunction();
36 
37 void MaterializationUnit::anchor() {}
38 
39 ResourceTracker::ResourceTracker(JITDylibSP JD) {
40   assert((reinterpret_cast<uintptr_t>(JD.get()) & 0x1) == 0 &&
41          "JITDylib must be two byte aligned");
42   JD->Retain();
43   JDAndFlag.store(reinterpret_cast<uintptr_t>(JD.get()));
44 }
45 
46 ResourceTracker::~ResourceTracker() {
47   getJITDylib().getExecutionSession().destroyResourceTracker(*this);
48   getJITDylib().Release();
49 }
50 
51 Error ResourceTracker::remove() {
52   return getJITDylib().getExecutionSession().removeResourceTracker(*this);
53 }
54 
55 void ResourceTracker::transferTo(ResourceTracker &DstRT) {
56   getJITDylib().getExecutionSession().transferResourceTracker(DstRT, *this);
57 }
58 
59 void ResourceTracker::makeDefunct() {
60   uintptr_t Val = JDAndFlag.load();
61   Val |= 0x1U;
62   JDAndFlag.store(Val);
63 }
64 
65 ResourceManager::~ResourceManager() {}
66 
67 ResourceTrackerDefunct::ResourceTrackerDefunct(ResourceTrackerSP RT)
68     : RT(std::move(RT)) {}
69 
70 std::error_code ResourceTrackerDefunct::convertToErrorCode() const {
71   return orcError(OrcErrorCode::UnknownORCError);
72 }
73 
74 void ResourceTrackerDefunct::log(raw_ostream &OS) const {
75   OS << "Resource tracker " << (void *)RT.get() << " became defunct";
76 }
77 
78 FailedToMaterialize::FailedToMaterialize(
79     std::shared_ptr<SymbolDependenceMap> Symbols)
80     : Symbols(std::move(Symbols)) {
81   assert(!this->Symbols->empty() && "Can not fail to resolve an empty set");
82 }
83 
84 std::error_code FailedToMaterialize::convertToErrorCode() const {
85   return orcError(OrcErrorCode::UnknownORCError);
86 }
87 
88 void FailedToMaterialize::log(raw_ostream &OS) const {
89   OS << "Failed to materialize symbols: " << *Symbols;
90 }
91 
92 SymbolsNotFound::SymbolsNotFound(std::shared_ptr<SymbolStringPool> SSP,
93                                  SymbolNameSet Symbols)
94     : SSP(std::move(SSP)) {
95   for (auto &Sym : Symbols)
96     this->Symbols.push_back(Sym);
97   assert(!this->Symbols.empty() && "Can not fail to resolve an empty set");
98 }
99 
100 SymbolsNotFound::SymbolsNotFound(std::shared_ptr<SymbolStringPool> SSP,
101                                  SymbolNameVector Symbols)
102     : SSP(std::move(SSP)), Symbols(std::move(Symbols)) {
103   assert(!this->Symbols.empty() && "Can not fail to resolve an empty set");
104 }
105 
106 std::error_code SymbolsNotFound::convertToErrorCode() const {
107   return orcError(OrcErrorCode::UnknownORCError);
108 }
109 
110 void SymbolsNotFound::log(raw_ostream &OS) const {
111   OS << "Symbols not found: " << Symbols;
112 }
113 
114 SymbolsCouldNotBeRemoved::SymbolsCouldNotBeRemoved(
115     std::shared_ptr<SymbolStringPool> SSP, SymbolNameSet Symbols)
116     : SSP(std::move(SSP)), Symbols(std::move(Symbols)) {
117   assert(!this->Symbols.empty() && "Can not fail to resolve an empty set");
118 }
119 
120 std::error_code SymbolsCouldNotBeRemoved::convertToErrorCode() const {
121   return orcError(OrcErrorCode::UnknownORCError);
122 }
123 
124 void SymbolsCouldNotBeRemoved::log(raw_ostream &OS) const {
125   OS << "Symbols could not be removed: " << Symbols;
126 }
127 
128 std::error_code MissingSymbolDefinitions::convertToErrorCode() const {
129   return orcError(OrcErrorCode::MissingSymbolDefinitions);
130 }
131 
132 void MissingSymbolDefinitions::log(raw_ostream &OS) const {
133   OS << "Missing definitions in module " << ModuleName
134      << ": " << Symbols;
135 }
136 
137 std::error_code UnexpectedSymbolDefinitions::convertToErrorCode() const {
138   return orcError(OrcErrorCode::UnexpectedSymbolDefinitions);
139 }
140 
141 void UnexpectedSymbolDefinitions::log(raw_ostream &OS) const {
142   OS << "Unexpected definitions in module " << ModuleName
143      << ": " << Symbols;
144 }
145 
146 AsynchronousSymbolQuery::AsynchronousSymbolQuery(
147     const SymbolLookupSet &Symbols, SymbolState RequiredState,
148     SymbolsResolvedCallback NotifyComplete)
149     : NotifyComplete(std::move(NotifyComplete)), RequiredState(RequiredState) {
150   assert(RequiredState >= SymbolState::Resolved &&
151          "Cannot query for a symbols that have not reached the resolve state "
152          "yet");
153 
154   OutstandingSymbolsCount = Symbols.size();
155 
156   for (auto &KV : Symbols)
157     ResolvedSymbols[KV.first] = nullptr;
158 }
159 
160 void AsynchronousSymbolQuery::notifySymbolMetRequiredState(
161     const SymbolStringPtr &Name, JITEvaluatedSymbol Sym) {
162   auto I = ResolvedSymbols.find(Name);
163   assert(I != ResolvedSymbols.end() &&
164          "Resolving symbol outside the requested set");
165   assert(I->second.getAddress() == 0 && "Redundantly resolving symbol Name");
166 
167   // If this is a materialization-side-effects-only symbol then drop it,
168   // otherwise update its map entry with its resolved address.
169   if (Sym.getFlags().hasMaterializationSideEffectsOnly())
170     ResolvedSymbols.erase(I);
171   else
172     I->second = std::move(Sym);
173   --OutstandingSymbolsCount;
174 }
175 
176 void AsynchronousSymbolQuery::handleComplete(ExecutionSession &ES) {
177   assert(OutstandingSymbolsCount == 0 &&
178          "Symbols remain, handleComplete called prematurely");
179 
180   class RunQueryCompleteTask : public Task {
181   public:
182     RunQueryCompleteTask(SymbolMap ResolvedSymbols,
183                          SymbolsResolvedCallback NotifyComplete)
184         : ResolvedSymbols(std::move(ResolvedSymbols)),
185           NotifyComplete(std::move(NotifyComplete)) {}
186     void printDescription(raw_ostream &OS) override {
187       OS << "Execute query complete callback for " << ResolvedSymbols;
188     }
189     void run() override { NotifyComplete(std::move(ResolvedSymbols)); }
190 
191   private:
192     SymbolMap ResolvedSymbols;
193     SymbolsResolvedCallback NotifyComplete;
194   };
195 
196   auto T = std::make_unique<RunQueryCompleteTask>(std::move(ResolvedSymbols),
197                                                   std::move(NotifyComplete));
198   NotifyComplete = SymbolsResolvedCallback();
199   ES.dispatchTask(std::move(T));
200 }
201 
202 void AsynchronousSymbolQuery::handleFailed(Error Err) {
203   assert(QueryRegistrations.empty() && ResolvedSymbols.empty() &&
204          OutstandingSymbolsCount == 0 &&
205          "Query should already have been abandoned");
206   NotifyComplete(std::move(Err));
207   NotifyComplete = SymbolsResolvedCallback();
208 }
209 
210 void AsynchronousSymbolQuery::addQueryDependence(JITDylib &JD,
211                                                  SymbolStringPtr Name) {
212   bool Added = QueryRegistrations[&JD].insert(std::move(Name)).second;
213   (void)Added;
214   assert(Added && "Duplicate dependence notification?");
215 }
216 
217 void AsynchronousSymbolQuery::removeQueryDependence(
218     JITDylib &JD, const SymbolStringPtr &Name) {
219   auto QRI = QueryRegistrations.find(&JD);
220   assert(QRI != QueryRegistrations.end() &&
221          "No dependencies registered for JD");
222   assert(QRI->second.count(Name) && "No dependency on Name in JD");
223   QRI->second.erase(Name);
224   if (QRI->second.empty())
225     QueryRegistrations.erase(QRI);
226 }
227 
228 void AsynchronousSymbolQuery::dropSymbol(const SymbolStringPtr &Name) {
229   auto I = ResolvedSymbols.find(Name);
230   assert(I != ResolvedSymbols.end() &&
231          "Redundant removal of weakly-referenced symbol");
232   ResolvedSymbols.erase(I);
233   --OutstandingSymbolsCount;
234 }
235 
236 void AsynchronousSymbolQuery::detach() {
237   ResolvedSymbols.clear();
238   OutstandingSymbolsCount = 0;
239   for (auto &KV : QueryRegistrations)
240     KV.first->detachQueryHelper(*this, KV.second);
241   QueryRegistrations.clear();
242 }
243 
244 AbsoluteSymbolsMaterializationUnit::AbsoluteSymbolsMaterializationUnit(
245     SymbolMap Symbols)
246     : MaterializationUnit(extractFlags(Symbols)), Symbols(std::move(Symbols)) {}
247 
248 StringRef AbsoluteSymbolsMaterializationUnit::getName() const {
249   return "<Absolute Symbols>";
250 }
251 
252 void AbsoluteSymbolsMaterializationUnit::materialize(
253     std::unique_ptr<MaterializationResponsibility> R) {
254   // No dependencies, so these calls can't fail.
255   cantFail(R->notifyResolved(Symbols));
256   cantFail(R->notifyEmitted());
257 }
258 
259 void AbsoluteSymbolsMaterializationUnit::discard(const JITDylib &JD,
260                                                  const SymbolStringPtr &Name) {
261   assert(Symbols.count(Name) && "Symbol is not part of this MU");
262   Symbols.erase(Name);
263 }
264 
265 MaterializationUnit::Interface
266 AbsoluteSymbolsMaterializationUnit::extractFlags(const SymbolMap &Symbols) {
267   SymbolFlagsMap Flags;
268   for (const auto &KV : Symbols)
269     Flags[KV.first] = KV.second.getFlags();
270   return MaterializationUnit::Interface(std::move(Flags), nullptr);
271 }
272 
273 ReExportsMaterializationUnit::ReExportsMaterializationUnit(
274     JITDylib *SourceJD, JITDylibLookupFlags SourceJDLookupFlags,
275     SymbolAliasMap Aliases)
276     : MaterializationUnit(extractFlags(Aliases)), SourceJD(SourceJD),
277       SourceJDLookupFlags(SourceJDLookupFlags), Aliases(std::move(Aliases)) {}
278 
279 StringRef ReExportsMaterializationUnit::getName() const {
280   return "<Reexports>";
281 }
282 
283 void ReExportsMaterializationUnit::materialize(
284     std::unique_ptr<MaterializationResponsibility> R) {
285 
286   auto &ES = R->getTargetJITDylib().getExecutionSession();
287   JITDylib &TgtJD = R->getTargetJITDylib();
288   JITDylib &SrcJD = SourceJD ? *SourceJD : TgtJD;
289 
290   // Find the set of requested aliases and aliasees. Return any unrequested
291   // aliases back to the JITDylib so as to not prematurely materialize any
292   // aliasees.
293   auto RequestedSymbols = R->getRequestedSymbols();
294   SymbolAliasMap RequestedAliases;
295 
296   for (auto &Name : RequestedSymbols) {
297     auto I = Aliases.find(Name);
298     assert(I != Aliases.end() && "Symbol not found in aliases map?");
299     RequestedAliases[Name] = std::move(I->second);
300     Aliases.erase(I);
301   }
302 
303   LLVM_DEBUG({
304     ES.runSessionLocked([&]() {
305       dbgs() << "materializing reexports: target = " << TgtJD.getName()
306              << ", source = " << SrcJD.getName() << " " << RequestedAliases
307              << "\n";
308     });
309   });
310 
311   if (!Aliases.empty()) {
312     auto Err = SourceJD ? R->replace(reexports(*SourceJD, std::move(Aliases),
313                                                SourceJDLookupFlags))
314                         : R->replace(symbolAliases(std::move(Aliases)));
315 
316     if (Err) {
317       // FIXME: Should this be reported / treated as failure to materialize?
318       // Or should this be treated as a sanctioned bailing-out?
319       ES.reportError(std::move(Err));
320       R->failMaterialization();
321       return;
322     }
323   }
324 
325   // The OnResolveInfo struct will hold the aliases and responsibilty for each
326   // query in the list.
327   struct OnResolveInfo {
328     OnResolveInfo(std::unique_ptr<MaterializationResponsibility> R,
329                   SymbolAliasMap Aliases)
330         : R(std::move(R)), Aliases(std::move(Aliases)) {}
331 
332     std::unique_ptr<MaterializationResponsibility> R;
333     SymbolAliasMap Aliases;
334   };
335 
336   // Build a list of queries to issue. In each round we build a query for the
337   // largest set of aliases that we can resolve without encountering a chain of
338   // aliases (e.g. Foo -> Bar, Bar -> Baz). Such a chain would deadlock as the
339   // query would be waiting on a symbol that it itself had to resolve. Creating
340   // a new query for each link in such a chain eliminates the possibility of
341   // deadlock. In practice chains are likely to be rare, and this algorithm will
342   // usually result in a single query to issue.
343 
344   std::vector<std::pair<SymbolLookupSet, std::shared_ptr<OnResolveInfo>>>
345       QueryInfos;
346   while (!RequestedAliases.empty()) {
347     SymbolNameSet ResponsibilitySymbols;
348     SymbolLookupSet QuerySymbols;
349     SymbolAliasMap QueryAliases;
350 
351     // Collect as many aliases as we can without including a chain.
352     for (auto &KV : RequestedAliases) {
353       // Chain detected. Skip this symbol for this round.
354       if (&SrcJD == &TgtJD && (QueryAliases.count(KV.second.Aliasee) ||
355                                RequestedAliases.count(KV.second.Aliasee)))
356         continue;
357 
358       ResponsibilitySymbols.insert(KV.first);
359       QuerySymbols.add(KV.second.Aliasee,
360                        KV.second.AliasFlags.hasMaterializationSideEffectsOnly()
361                            ? SymbolLookupFlags::WeaklyReferencedSymbol
362                            : SymbolLookupFlags::RequiredSymbol);
363       QueryAliases[KV.first] = std::move(KV.second);
364     }
365 
366     // Remove the aliases collected this round from the RequestedAliases map.
367     for (auto &KV : QueryAliases)
368       RequestedAliases.erase(KV.first);
369 
370     assert(!QuerySymbols.empty() && "Alias cycle detected!");
371 
372     auto NewR = R->delegate(ResponsibilitySymbols);
373     if (!NewR) {
374       ES.reportError(NewR.takeError());
375       R->failMaterialization();
376       return;
377     }
378 
379     auto QueryInfo = std::make_shared<OnResolveInfo>(std::move(*NewR),
380                                                      std::move(QueryAliases));
381     QueryInfos.push_back(
382         make_pair(std::move(QuerySymbols), std::move(QueryInfo)));
383   }
384 
385   // Issue the queries.
386   while (!QueryInfos.empty()) {
387     auto QuerySymbols = std::move(QueryInfos.back().first);
388     auto QueryInfo = std::move(QueryInfos.back().second);
389 
390     QueryInfos.pop_back();
391 
392     auto RegisterDependencies = [QueryInfo,
393                                  &SrcJD](const SymbolDependenceMap &Deps) {
394       // If there were no materializing symbols, just bail out.
395       if (Deps.empty())
396         return;
397 
398       // Otherwise the only deps should be on SrcJD.
399       assert(Deps.size() == 1 && Deps.count(&SrcJD) &&
400              "Unexpected dependencies for reexports");
401 
402       auto &SrcJDDeps = Deps.find(&SrcJD)->second;
403       SymbolDependenceMap PerAliasDepsMap;
404       auto &PerAliasDeps = PerAliasDepsMap[&SrcJD];
405 
406       for (auto &KV : QueryInfo->Aliases)
407         if (SrcJDDeps.count(KV.second.Aliasee)) {
408           PerAliasDeps = {KV.second.Aliasee};
409           QueryInfo->R->addDependencies(KV.first, PerAliasDepsMap);
410         }
411     };
412 
413     auto OnComplete = [QueryInfo](Expected<SymbolMap> Result) {
414       auto &ES = QueryInfo->R->getTargetJITDylib().getExecutionSession();
415       if (Result) {
416         SymbolMap ResolutionMap;
417         for (auto &KV : QueryInfo->Aliases) {
418           assert((KV.second.AliasFlags.hasMaterializationSideEffectsOnly() ||
419                   Result->count(KV.second.Aliasee)) &&
420                  "Result map missing entry?");
421           // Don't try to resolve materialization-side-effects-only symbols.
422           if (KV.second.AliasFlags.hasMaterializationSideEffectsOnly())
423             continue;
424 
425           ResolutionMap[KV.first] = JITEvaluatedSymbol(
426               (*Result)[KV.second.Aliasee].getAddress(), KV.second.AliasFlags);
427         }
428         if (auto Err = QueryInfo->R->notifyResolved(ResolutionMap)) {
429           ES.reportError(std::move(Err));
430           QueryInfo->R->failMaterialization();
431           return;
432         }
433         if (auto Err = QueryInfo->R->notifyEmitted()) {
434           ES.reportError(std::move(Err));
435           QueryInfo->R->failMaterialization();
436           return;
437         }
438       } else {
439         ES.reportError(Result.takeError());
440         QueryInfo->R->failMaterialization();
441       }
442     };
443 
444     ES.lookup(LookupKind::Static,
445               JITDylibSearchOrder({{&SrcJD, SourceJDLookupFlags}}),
446               QuerySymbols, SymbolState::Resolved, std::move(OnComplete),
447               std::move(RegisterDependencies));
448   }
449 }
450 
451 void ReExportsMaterializationUnit::discard(const JITDylib &JD,
452                                            const SymbolStringPtr &Name) {
453   assert(Aliases.count(Name) &&
454          "Symbol not covered by this MaterializationUnit");
455   Aliases.erase(Name);
456 }
457 
458 MaterializationUnit::Interface
459 ReExportsMaterializationUnit::extractFlags(const SymbolAliasMap &Aliases) {
460   SymbolFlagsMap SymbolFlags;
461   for (auto &KV : Aliases)
462     SymbolFlags[KV.first] = KV.second.AliasFlags;
463 
464   return MaterializationUnit::Interface(std::move(SymbolFlags), nullptr);
465 }
466 
467 Expected<SymbolAliasMap> buildSimpleReexportsAliasMap(JITDylib &SourceJD,
468                                                       SymbolNameSet Symbols) {
469   SymbolLookupSet LookupSet(Symbols);
470   auto Flags = SourceJD.getExecutionSession().lookupFlags(
471       LookupKind::Static, {{&SourceJD, JITDylibLookupFlags::MatchAllSymbols}},
472       SymbolLookupSet(std::move(Symbols)));
473 
474   if (!Flags)
475     return Flags.takeError();
476 
477   SymbolAliasMap Result;
478   for (auto &Name : Symbols) {
479     assert(Flags->count(Name) && "Missing entry in flags map");
480     Result[Name] = SymbolAliasMapEntry(Name, (*Flags)[Name]);
481   }
482 
483   return Result;
484 }
485 
486 class InProgressLookupState {
487 public:
488   InProgressLookupState(LookupKind K, JITDylibSearchOrder SearchOrder,
489                         SymbolLookupSet LookupSet, SymbolState RequiredState)
490       : K(K), SearchOrder(std::move(SearchOrder)),
491         LookupSet(std::move(LookupSet)), RequiredState(RequiredState) {
492     DefGeneratorCandidates = this->LookupSet;
493   }
494   virtual ~InProgressLookupState() {}
495   virtual void complete(std::unique_ptr<InProgressLookupState> IPLS) = 0;
496   virtual void fail(Error Err) = 0;
497 
498   LookupKind K;
499   JITDylibSearchOrder SearchOrder;
500   SymbolLookupSet LookupSet;
501   SymbolState RequiredState;
502 
503   std::unique_lock<std::mutex> GeneratorLock;
504   size_t CurSearchOrderIndex = 0;
505   bool NewJITDylib = true;
506   SymbolLookupSet DefGeneratorCandidates;
507   SymbolLookupSet DefGeneratorNonCandidates;
508   std::vector<std::weak_ptr<DefinitionGenerator>> CurDefGeneratorStack;
509 };
510 
511 class InProgressLookupFlagsState : public InProgressLookupState {
512 public:
513   InProgressLookupFlagsState(
514       LookupKind K, JITDylibSearchOrder SearchOrder, SymbolLookupSet LookupSet,
515       unique_function<void(Expected<SymbolFlagsMap>)> OnComplete)
516       : InProgressLookupState(K, std::move(SearchOrder), std::move(LookupSet),
517                               SymbolState::NeverSearched),
518         OnComplete(std::move(OnComplete)) {}
519 
520   void complete(std::unique_ptr<InProgressLookupState> IPLS) override {
521     GeneratorLock = {}; // Unlock and release.
522     auto &ES = SearchOrder.front().first->getExecutionSession();
523     ES.OL_completeLookupFlags(std::move(IPLS), std::move(OnComplete));
524   }
525 
526   void fail(Error Err) override {
527     GeneratorLock = {}; // Unlock and release.
528     OnComplete(std::move(Err));
529   }
530 
531 private:
532   unique_function<void(Expected<SymbolFlagsMap>)> OnComplete;
533 };
534 
535 class InProgressFullLookupState : public InProgressLookupState {
536 public:
537   InProgressFullLookupState(LookupKind K, JITDylibSearchOrder SearchOrder,
538                             SymbolLookupSet LookupSet,
539                             SymbolState RequiredState,
540                             std::shared_ptr<AsynchronousSymbolQuery> Q,
541                             RegisterDependenciesFunction RegisterDependencies)
542       : InProgressLookupState(K, std::move(SearchOrder), std::move(LookupSet),
543                               RequiredState),
544         Q(std::move(Q)), RegisterDependencies(std::move(RegisterDependencies)) {
545   }
546 
547   void complete(std::unique_ptr<InProgressLookupState> IPLS) override {
548     GeneratorLock = {}; // Unlock and release.
549     auto &ES = SearchOrder.front().first->getExecutionSession();
550     ES.OL_completeLookup(std::move(IPLS), std::move(Q),
551                          std::move(RegisterDependencies));
552   }
553 
554   void fail(Error Err) override {
555     GeneratorLock = {};
556     Q->detach();
557     Q->handleFailed(std::move(Err));
558   }
559 
560 private:
561   std::shared_ptr<AsynchronousSymbolQuery> Q;
562   RegisterDependenciesFunction RegisterDependencies;
563 };
564 
565 ReexportsGenerator::ReexportsGenerator(JITDylib &SourceJD,
566                                        JITDylibLookupFlags SourceJDLookupFlags,
567                                        SymbolPredicate Allow)
568     : SourceJD(SourceJD), SourceJDLookupFlags(SourceJDLookupFlags),
569       Allow(std::move(Allow)) {}
570 
571 Error ReexportsGenerator::tryToGenerate(LookupState &LS, LookupKind K,
572                                         JITDylib &JD,
573                                         JITDylibLookupFlags JDLookupFlags,
574                                         const SymbolLookupSet &LookupSet) {
575   assert(&JD != &SourceJD && "Cannot re-export from the same dylib");
576 
577   // Use lookupFlags to find the subset of symbols that match our lookup.
578   auto Flags = JD.getExecutionSession().lookupFlags(
579       K, {{&SourceJD, JDLookupFlags}}, LookupSet);
580   if (!Flags)
581     return Flags.takeError();
582 
583   // Create an alias map.
584   orc::SymbolAliasMap AliasMap;
585   for (auto &KV : *Flags)
586     if (!Allow || Allow(KV.first))
587       AliasMap[KV.first] = SymbolAliasMapEntry(KV.first, KV.second);
588 
589   if (AliasMap.empty())
590     return Error::success();
591 
592   // Define the re-exports.
593   return JD.define(reexports(SourceJD, AliasMap, SourceJDLookupFlags));
594 }
595 
596 LookupState::LookupState(std::unique_ptr<InProgressLookupState> IPLS)
597     : IPLS(std::move(IPLS)) {}
598 
599 void LookupState::reset(InProgressLookupState *IPLS) { this->IPLS.reset(IPLS); }
600 
601 LookupState::LookupState() = default;
602 LookupState::LookupState(LookupState &&) = default;
603 LookupState &LookupState::operator=(LookupState &&) = default;
604 LookupState::~LookupState() = default;
605 
606 void LookupState::continueLookup(Error Err) {
607   assert(IPLS && "Cannot call continueLookup on empty LookupState");
608   auto &ES = IPLS->SearchOrder.begin()->first->getExecutionSession();
609   ES.OL_applyQueryPhase1(std::move(IPLS), std::move(Err));
610 }
611 
612 DefinitionGenerator::~DefinitionGenerator() {}
613 
614 JITDylib::~JITDylib() {
615   LLVM_DEBUG(dbgs() << "Destroying JITDylib " << getName() << "\n");
616 }
617 
618 Error JITDylib::clear() {
619   std::vector<ResourceTrackerSP> TrackersToRemove;
620   ES.runSessionLocked([&]() {
621     assert(State != Closed && "JD is defunct");
622     for (auto &KV : TrackerSymbols)
623       TrackersToRemove.push_back(KV.first);
624     TrackersToRemove.push_back(getDefaultResourceTracker());
625   });
626 
627   Error Err = Error::success();
628   for (auto &RT : TrackersToRemove)
629     Err = joinErrors(std::move(Err), RT->remove());
630   return Err;
631 }
632 
633 ResourceTrackerSP JITDylib::getDefaultResourceTracker() {
634   return ES.runSessionLocked([this] {
635     assert(State != Closed && "JD is defunct");
636     if (!DefaultTracker)
637       DefaultTracker = new ResourceTracker(this);
638     return DefaultTracker;
639   });
640 }
641 
642 ResourceTrackerSP JITDylib::createResourceTracker() {
643   return ES.runSessionLocked([this] {
644     assert(State == Open && "JD is defunct");
645     ResourceTrackerSP RT = new ResourceTracker(this);
646     return RT;
647   });
648 }
649 
650 void JITDylib::removeGenerator(DefinitionGenerator &G) {
651   ES.runSessionLocked([&] {
652     assert(State == Open && "JD is defunct");
653     auto I = llvm::find_if(DefGenerators,
654                            [&](const std::shared_ptr<DefinitionGenerator> &H) {
655                              return H.get() == &G;
656                            });
657     assert(I != DefGenerators.end() && "Generator not found");
658     DefGenerators.erase(I);
659   });
660 }
661 
662 Expected<SymbolFlagsMap>
663 JITDylib::defineMaterializing(SymbolFlagsMap SymbolFlags) {
664 
665   return ES.runSessionLocked([&]() -> Expected<SymbolFlagsMap> {
666     std::vector<SymbolTable::iterator> AddedSyms;
667     std::vector<SymbolFlagsMap::iterator> RejectedWeakDefs;
668 
669     for (auto SFItr = SymbolFlags.begin(), SFEnd = SymbolFlags.end();
670          SFItr != SFEnd; ++SFItr) {
671 
672       auto &Name = SFItr->first;
673       auto &Flags = SFItr->second;
674 
675       auto EntryItr = Symbols.find(Name);
676 
677       // If the entry already exists...
678       if (EntryItr != Symbols.end()) {
679 
680         // If this is a strong definition then error out.
681         if (!Flags.isWeak()) {
682           // Remove any symbols already added.
683           for (auto &SI : AddedSyms)
684             Symbols.erase(SI);
685 
686           // FIXME: Return all duplicates.
687           return make_error<DuplicateDefinition>(std::string(*Name));
688         }
689 
690         // Otherwise just make a note to discard this symbol after the loop.
691         RejectedWeakDefs.push_back(SFItr);
692         continue;
693       } else
694         EntryItr =
695           Symbols.insert(std::make_pair(Name, SymbolTableEntry(Flags))).first;
696 
697       AddedSyms.push_back(EntryItr);
698       EntryItr->second.setState(SymbolState::Materializing);
699     }
700 
701     // Remove any rejected weak definitions from the SymbolFlags map.
702     while (!RejectedWeakDefs.empty()) {
703       SymbolFlags.erase(RejectedWeakDefs.back());
704       RejectedWeakDefs.pop_back();
705     }
706 
707     return SymbolFlags;
708   });
709 }
710 
711 Error JITDylib::replace(MaterializationResponsibility &FromMR,
712                         std::unique_ptr<MaterializationUnit> MU) {
713   assert(MU != nullptr && "Can not replace with a null MaterializationUnit");
714   std::unique_ptr<MaterializationUnit> MustRunMU;
715   std::unique_ptr<MaterializationResponsibility> MustRunMR;
716 
717   auto Err =
718       ES.runSessionLocked([&, this]() -> Error {
719         if (FromMR.RT->isDefunct())
720           return make_error<ResourceTrackerDefunct>(std::move(FromMR.RT));
721 
722 #ifndef NDEBUG
723         for (auto &KV : MU->getSymbols()) {
724           auto SymI = Symbols.find(KV.first);
725           assert(SymI != Symbols.end() && "Replacing unknown symbol");
726           assert(SymI->second.getState() == SymbolState::Materializing &&
727                  "Can not replace a symbol that ha is not materializing");
728           assert(!SymI->second.hasMaterializerAttached() &&
729                  "Symbol should not have materializer attached already");
730           assert(UnmaterializedInfos.count(KV.first) == 0 &&
731                  "Symbol being replaced should have no UnmaterializedInfo");
732         }
733 #endif // NDEBUG
734 
735         // If the tracker is defunct we need to bail out immediately.
736 
737         // If any symbol has pending queries against it then we need to
738         // materialize MU immediately.
739         for (auto &KV : MU->getSymbols()) {
740           auto MII = MaterializingInfos.find(KV.first);
741           if (MII != MaterializingInfos.end()) {
742             if (MII->second.hasQueriesPending()) {
743               MustRunMR = ES.createMaterializationResponsibility(
744                   *FromMR.RT, std::move(MU->SymbolFlags),
745                   std::move(MU->InitSymbol));
746               MustRunMU = std::move(MU);
747               return Error::success();
748             }
749           }
750         }
751 
752         // Otherwise, make MU responsible for all the symbols.
753         auto UMI = std::make_shared<UnmaterializedInfo>(std::move(MU),
754                                                         FromMR.RT.get());
755         for (auto &KV : UMI->MU->getSymbols()) {
756           auto SymI = Symbols.find(KV.first);
757           assert(SymI->second.getState() == SymbolState::Materializing &&
758                  "Can not replace a symbol that is not materializing");
759           assert(!SymI->second.hasMaterializerAttached() &&
760                  "Can not replace a symbol that has a materializer attached");
761           assert(UnmaterializedInfos.count(KV.first) == 0 &&
762                  "Unexpected materializer entry in map");
763           SymI->second.setAddress(SymI->second.getAddress());
764           SymI->second.setMaterializerAttached(true);
765 
766           auto &UMIEntry = UnmaterializedInfos[KV.first];
767           assert((!UMIEntry || !UMIEntry->MU) &&
768                  "Replacing symbol with materializer still attached");
769           UMIEntry = UMI;
770         }
771 
772         return Error::success();
773       });
774 
775   if (Err)
776     return Err;
777 
778   if (MustRunMU) {
779     assert(MustRunMR && "MustRunMU set implies MustRunMR set");
780     ES.dispatchTask(std::make_unique<MaterializationTask>(
781         std::move(MustRunMU), std::move(MustRunMR)));
782   } else {
783     assert(!MustRunMR && "MustRunMU unset implies MustRunMR unset");
784   }
785 
786   return Error::success();
787 }
788 
789 Expected<std::unique_ptr<MaterializationResponsibility>>
790 JITDylib::delegate(MaterializationResponsibility &FromMR,
791                    SymbolFlagsMap SymbolFlags, SymbolStringPtr InitSymbol) {
792 
793   return ES.runSessionLocked(
794       [&]() -> Expected<std::unique_ptr<MaterializationResponsibility>> {
795         if (FromMR.RT->isDefunct())
796           return make_error<ResourceTrackerDefunct>(std::move(FromMR.RT));
797 
798         return ES.createMaterializationResponsibility(
799             *FromMR.RT, std::move(SymbolFlags), std::move(InitSymbol));
800       });
801 }
802 
803 SymbolNameSet
804 JITDylib::getRequestedSymbols(const SymbolFlagsMap &SymbolFlags) const {
805   return ES.runSessionLocked([&]() {
806     SymbolNameSet RequestedSymbols;
807 
808     for (auto &KV : SymbolFlags) {
809       assert(Symbols.count(KV.first) && "JITDylib does not cover this symbol?");
810       assert(Symbols.find(KV.first)->second.getState() !=
811                  SymbolState::NeverSearched &&
812              Symbols.find(KV.first)->second.getState() != SymbolState::Ready &&
813              "getRequestedSymbols can only be called for symbols that have "
814              "started materializing");
815       auto I = MaterializingInfos.find(KV.first);
816       if (I == MaterializingInfos.end())
817         continue;
818 
819       if (I->second.hasQueriesPending())
820         RequestedSymbols.insert(KV.first);
821     }
822 
823     return RequestedSymbols;
824   });
825 }
826 
827 void JITDylib::addDependencies(const SymbolStringPtr &Name,
828                                const SymbolDependenceMap &Dependencies) {
829   ES.runSessionLocked([&]() {
830     assert(Symbols.count(Name) && "Name not in symbol table");
831     assert(Symbols[Name].getState() < SymbolState::Emitted &&
832            "Can not add dependencies for a symbol that is not materializing");
833 
834     LLVM_DEBUG({
835       dbgs() << "In " << getName() << " adding dependencies for " << *Name
836              << ": " << Dependencies << "\n";
837     });
838 
839     // If Name is already in an error state then just bail out.
840     if (Symbols[Name].getFlags().hasError())
841       return;
842 
843     auto &MI = MaterializingInfos[Name];
844     assert(Symbols[Name].getState() != SymbolState::Emitted &&
845            "Can not add dependencies to an emitted symbol");
846 
847     bool DependsOnSymbolInErrorState = false;
848 
849     // Register dependencies, record whether any depenendency is in the error
850     // state.
851     for (auto &KV : Dependencies) {
852       assert(KV.first && "Null JITDylib in dependency?");
853       auto &OtherJITDylib = *KV.first;
854       auto &DepsOnOtherJITDylib = MI.UnemittedDependencies[&OtherJITDylib];
855 
856       for (auto &OtherSymbol : KV.second) {
857 
858         // Check the sym entry for the dependency.
859         auto OtherSymI = OtherJITDylib.Symbols.find(OtherSymbol);
860 
861         // Assert that this symbol exists and has not reached the ready state
862         // already.
863         assert(OtherSymI != OtherJITDylib.Symbols.end() &&
864                "Dependency on unknown symbol");
865 
866         auto &OtherSymEntry = OtherSymI->second;
867 
868         // If the other symbol is already in the Ready state then there's no
869         // dependency to add.
870         if (OtherSymEntry.getState() == SymbolState::Ready)
871           continue;
872 
873         // If the dependency is in an error state then note this and continue,
874         // we will move this symbol to the error state below.
875         if (OtherSymEntry.getFlags().hasError()) {
876           DependsOnSymbolInErrorState = true;
877           continue;
878         }
879 
880         // If the dependency was not in the error state then add it to
881         // our list of dependencies.
882         auto &OtherMI = OtherJITDylib.MaterializingInfos[OtherSymbol];
883 
884         if (OtherSymEntry.getState() == SymbolState::Emitted)
885           transferEmittedNodeDependencies(MI, Name, OtherMI);
886         else if (&OtherJITDylib != this || OtherSymbol != Name) {
887           OtherMI.Dependants[this].insert(Name);
888           DepsOnOtherJITDylib.insert(OtherSymbol);
889         }
890       }
891 
892       if (DepsOnOtherJITDylib.empty())
893         MI.UnemittedDependencies.erase(&OtherJITDylib);
894     }
895 
896     // If this symbol dependended on any symbols in the error state then move
897     // this symbol to the error state too.
898     if (DependsOnSymbolInErrorState)
899       Symbols[Name].setFlags(Symbols[Name].getFlags() |
900                              JITSymbolFlags::HasError);
901   });
902 }
903 
904 Error JITDylib::resolve(MaterializationResponsibility &MR,
905                         const SymbolMap &Resolved) {
906   AsynchronousSymbolQuerySet CompletedQueries;
907 
908   if (auto Err = ES.runSessionLocked([&, this]() -> Error {
909         if (MR.RT->isDefunct())
910           return make_error<ResourceTrackerDefunct>(MR.RT);
911 
912         if (State != Open)
913           return make_error<StringError>("JITDylib " + getName() +
914                                              " is defunct",
915                                          inconvertibleErrorCode());
916 
917         struct WorklistEntry {
918           SymbolTable::iterator SymI;
919           JITEvaluatedSymbol ResolvedSym;
920         };
921 
922         SymbolNameSet SymbolsInErrorState;
923         std::vector<WorklistEntry> Worklist;
924         Worklist.reserve(Resolved.size());
925 
926         // Build worklist and check for any symbols in the error state.
927         for (const auto &KV : Resolved) {
928 
929           assert(!KV.second.getFlags().hasError() &&
930                  "Resolution result can not have error flag set");
931 
932           auto SymI = Symbols.find(KV.first);
933 
934           assert(SymI != Symbols.end() && "Symbol not found");
935           assert(!SymI->second.hasMaterializerAttached() &&
936                  "Resolving symbol with materializer attached?");
937           assert(SymI->second.getState() == SymbolState::Materializing &&
938                  "Symbol should be materializing");
939           assert(SymI->second.getAddress() == 0 &&
940                  "Symbol has already been resolved");
941 
942           if (SymI->second.getFlags().hasError())
943             SymbolsInErrorState.insert(KV.first);
944           else {
945             auto Flags = KV.second.getFlags();
946             Flags &= ~(JITSymbolFlags::Weak | JITSymbolFlags::Common);
947             assert(Flags ==
948                        (SymI->second.getFlags() &
949                         ~(JITSymbolFlags::Weak | JITSymbolFlags::Common)) &&
950                    "Resolved flags should match the declared flags");
951 
952             Worklist.push_back(
953                 {SymI, JITEvaluatedSymbol(KV.second.getAddress(), Flags)});
954           }
955         }
956 
957         // If any symbols were in the error state then bail out.
958         if (!SymbolsInErrorState.empty()) {
959           auto FailedSymbolsDepMap = std::make_shared<SymbolDependenceMap>();
960           (*FailedSymbolsDepMap)[this] = std::move(SymbolsInErrorState);
961           return make_error<FailedToMaterialize>(
962               std::move(FailedSymbolsDepMap));
963         }
964 
965         while (!Worklist.empty()) {
966           auto SymI = Worklist.back().SymI;
967           auto ResolvedSym = Worklist.back().ResolvedSym;
968           Worklist.pop_back();
969 
970           auto &Name = SymI->first;
971 
972           // Resolved symbols can not be weak: discard the weak flag.
973           JITSymbolFlags ResolvedFlags = ResolvedSym.getFlags();
974           SymI->second.setAddress(ResolvedSym.getAddress());
975           SymI->second.setFlags(ResolvedFlags);
976           SymI->second.setState(SymbolState::Resolved);
977 
978           auto MII = MaterializingInfos.find(Name);
979           if (MII == MaterializingInfos.end())
980             continue;
981 
982           auto &MI = MII->second;
983           for (auto &Q : MI.takeQueriesMeeting(SymbolState::Resolved)) {
984             Q->notifySymbolMetRequiredState(Name, ResolvedSym);
985             Q->removeQueryDependence(*this, Name);
986             if (Q->isComplete())
987               CompletedQueries.insert(std::move(Q));
988           }
989         }
990 
991         return Error::success();
992       }))
993     return Err;
994 
995   // Otherwise notify all the completed queries.
996   for (auto &Q : CompletedQueries) {
997     assert(Q->isComplete() && "Q not completed");
998     Q->handleComplete(ES);
999   }
1000 
1001   return Error::success();
1002 }
1003 
1004 Error JITDylib::emit(MaterializationResponsibility &MR,
1005                      const SymbolFlagsMap &Emitted) {
1006   AsynchronousSymbolQuerySet CompletedQueries;
1007   DenseMap<JITDylib *, SymbolNameVector> ReadySymbols;
1008 
1009   if (auto Err = ES.runSessionLocked([&, this]() -> Error {
1010         if (MR.RT->isDefunct())
1011           return make_error<ResourceTrackerDefunct>(MR.RT);
1012 
1013         if (State != Open)
1014           return make_error<StringError>("JITDylib " + getName() +
1015                                              " is defunct",
1016                                          inconvertibleErrorCode());
1017 
1018         SymbolNameSet SymbolsInErrorState;
1019         std::vector<SymbolTable::iterator> Worklist;
1020 
1021         // Scan to build worklist, record any symbols in the erorr state.
1022         for (const auto &KV : Emitted) {
1023           auto &Name = KV.first;
1024 
1025           auto SymI = Symbols.find(Name);
1026           assert(SymI != Symbols.end() && "No symbol table entry for Name");
1027 
1028           if (SymI->second.getFlags().hasError())
1029             SymbolsInErrorState.insert(Name);
1030           else
1031             Worklist.push_back(SymI);
1032         }
1033 
1034         // If any symbols were in the error state then bail out.
1035         if (!SymbolsInErrorState.empty()) {
1036           auto FailedSymbolsDepMap = std::make_shared<SymbolDependenceMap>();
1037           (*FailedSymbolsDepMap)[this] = std::move(SymbolsInErrorState);
1038           return make_error<FailedToMaterialize>(
1039               std::move(FailedSymbolsDepMap));
1040         }
1041 
1042         // Otherwise update dependencies and move to the emitted state.
1043         while (!Worklist.empty()) {
1044           auto SymI = Worklist.back();
1045           Worklist.pop_back();
1046 
1047           auto &Name = SymI->first;
1048           auto &SymEntry = SymI->second;
1049 
1050           // Move symbol to the emitted state.
1051           assert(((SymEntry.getFlags().hasMaterializationSideEffectsOnly() &&
1052                    SymEntry.getState() == SymbolState::Materializing) ||
1053                   SymEntry.getState() == SymbolState::Resolved) &&
1054                  "Emitting from state other than Resolved");
1055           SymEntry.setState(SymbolState::Emitted);
1056 
1057           auto MII = MaterializingInfos.find(Name);
1058 
1059           // If this symbol has no MaterializingInfo then it's trivially ready.
1060           // Update its state and continue.
1061           if (MII == MaterializingInfos.end()) {
1062             SymEntry.setState(SymbolState::Ready);
1063             continue;
1064           }
1065 
1066           auto &MI = MII->second;
1067 
1068           // For each dependant, transfer this node's emitted dependencies to
1069           // it. If the dependant node is ready (i.e. has no unemitted
1070           // dependencies) then notify any pending queries.
1071           for (auto &KV : MI.Dependants) {
1072             auto &DependantJD = *KV.first;
1073             auto &DependantJDReadySymbols = ReadySymbols[&DependantJD];
1074             for (auto &DependantName : KV.second) {
1075               auto DependantMII =
1076                   DependantJD.MaterializingInfos.find(DependantName);
1077               assert(DependantMII != DependantJD.MaterializingInfos.end() &&
1078                      "Dependant should have MaterializingInfo");
1079 
1080               auto &DependantMI = DependantMII->second;
1081 
1082               // Remove the dependant's dependency on this node.
1083               assert(DependantMI.UnemittedDependencies.count(this) &&
1084                      "Dependant does not have an unemitted dependencies record "
1085                      "for "
1086                      "this JITDylib");
1087               assert(DependantMI.UnemittedDependencies[this].count(Name) &&
1088                      "Dependant does not count this symbol as a dependency?");
1089 
1090               DependantMI.UnemittedDependencies[this].erase(Name);
1091               if (DependantMI.UnemittedDependencies[this].empty())
1092                 DependantMI.UnemittedDependencies.erase(this);
1093 
1094               // Transfer unemitted dependencies from this node to the
1095               // dependant.
1096               DependantJD.transferEmittedNodeDependencies(DependantMI,
1097                                                           DependantName, MI);
1098 
1099               auto DependantSymI = DependantJD.Symbols.find(DependantName);
1100               assert(DependantSymI != DependantJD.Symbols.end() &&
1101                      "Dependant has no entry in the Symbols table");
1102               auto &DependantSymEntry = DependantSymI->second;
1103 
1104               // If the dependant is emitted and this node was the last of its
1105               // unemitted dependencies then the dependant node is now ready, so
1106               // notify any pending queries on the dependant node.
1107               if (DependantSymEntry.getState() == SymbolState::Emitted &&
1108                   DependantMI.UnemittedDependencies.empty()) {
1109                 assert(DependantMI.Dependants.empty() &&
1110                        "Dependants should be empty by now");
1111 
1112                 // Since this dependant is now ready, we erase its
1113                 // MaterializingInfo and update its materializing state.
1114                 DependantSymEntry.setState(SymbolState::Ready);
1115                 DependantJDReadySymbols.push_back(DependantName);
1116 
1117                 for (auto &Q :
1118                      DependantMI.takeQueriesMeeting(SymbolState::Ready)) {
1119                   Q->notifySymbolMetRequiredState(
1120                       DependantName, DependantSymI->second.getSymbol());
1121                   if (Q->isComplete())
1122                     CompletedQueries.insert(Q);
1123                   Q->removeQueryDependence(DependantJD, DependantName);
1124                 }
1125                 DependantJD.MaterializingInfos.erase(DependantMII);
1126               }
1127             }
1128           }
1129 
1130           auto &ThisJDReadySymbols = ReadySymbols[this];
1131           MI.Dependants.clear();
1132           if (MI.UnemittedDependencies.empty()) {
1133             SymI->second.setState(SymbolState::Ready);
1134             ThisJDReadySymbols.push_back(Name);
1135             for (auto &Q : MI.takeQueriesMeeting(SymbolState::Ready)) {
1136               Q->notifySymbolMetRequiredState(Name, SymI->second.getSymbol());
1137               if (Q->isComplete())
1138                 CompletedQueries.insert(Q);
1139               Q->removeQueryDependence(*this, Name);
1140             }
1141             MaterializingInfos.erase(MII);
1142           }
1143         }
1144 
1145         return Error::success();
1146       }))
1147     return Err;
1148 
1149   // Otherwise notify all the completed queries.
1150   for (auto &Q : CompletedQueries) {
1151     assert(Q->isComplete() && "Q is not complete");
1152     Q->handleComplete(ES);
1153   }
1154 
1155   return Error::success();
1156 }
1157 
1158 void JITDylib::unlinkMaterializationResponsibility(
1159     MaterializationResponsibility &MR) {
1160   ES.runSessionLocked([&]() {
1161     auto I = TrackerMRs.find(MR.RT.get());
1162     assert(I != TrackerMRs.end() && "No MRs in TrackerMRs list for RT");
1163     assert(I->second.count(&MR) && "MR not in TrackerMRs list for RT");
1164     I->second.erase(&MR);
1165     if (I->second.empty())
1166       TrackerMRs.erase(MR.RT.get());
1167   });
1168 }
1169 
1170 std::pair<JITDylib::AsynchronousSymbolQuerySet,
1171           std::shared_ptr<SymbolDependenceMap>>
1172 JITDylib::failSymbols(FailedSymbolsWorklist Worklist) {
1173   AsynchronousSymbolQuerySet FailedQueries;
1174   auto FailedSymbolsMap = std::make_shared<SymbolDependenceMap>();
1175 
1176   while (!Worklist.empty()) {
1177     assert(Worklist.back().first && "Failed JITDylib can not be null");
1178     auto &JD = *Worklist.back().first;
1179     auto Name = std::move(Worklist.back().second);
1180     Worklist.pop_back();
1181 
1182     (*FailedSymbolsMap)[&JD].insert(Name);
1183 
1184     // Look up the symbol to fail.
1185     auto SymI = JD.Symbols.find(Name);
1186 
1187     // It's possible that this symbol has already been removed, e.g. if a
1188     // materialization failure happens concurrently with a ResourceTracker or
1189     // JITDylib removal. In that case we can safely skip this symbol and
1190     // continue.
1191     if (SymI == JD.Symbols.end())
1192       continue;
1193     auto &Sym = SymI->second;
1194 
1195     // Move the symbol into the error state.
1196     // Note that this may be redundant: The symbol might already have been
1197     // moved to this state in response to the failure of a dependence.
1198     Sym.setFlags(Sym.getFlags() | JITSymbolFlags::HasError);
1199 
1200     // FIXME: Come up with a sane mapping of state to
1201     // presence-of-MaterializingInfo so that we can assert presence / absence
1202     // here, rather than testing it.
1203     auto MII = JD.MaterializingInfos.find(Name);
1204 
1205     if (MII == JD.MaterializingInfos.end())
1206       continue;
1207 
1208     auto &MI = MII->second;
1209 
1210     // Move all dependants to the error state and disconnect from them.
1211     for (auto &KV : MI.Dependants) {
1212       auto &DependantJD = *KV.first;
1213       for (auto &DependantName : KV.second) {
1214         assert(DependantJD.Symbols.count(DependantName) &&
1215                "No symbol table entry for DependantName");
1216         auto &DependantSym = DependantJD.Symbols[DependantName];
1217         DependantSym.setFlags(DependantSym.getFlags() |
1218                               JITSymbolFlags::HasError);
1219 
1220         assert(DependantJD.MaterializingInfos.count(DependantName) &&
1221                "No MaterializingInfo for dependant");
1222         auto &DependantMI = DependantJD.MaterializingInfos[DependantName];
1223 
1224         auto UnemittedDepI = DependantMI.UnemittedDependencies.find(&JD);
1225         assert(UnemittedDepI != DependantMI.UnemittedDependencies.end() &&
1226                "No UnemittedDependencies entry for this JITDylib");
1227         assert(UnemittedDepI->second.count(Name) &&
1228                "No UnemittedDependencies entry for this symbol");
1229         UnemittedDepI->second.erase(Name);
1230         if (UnemittedDepI->second.empty())
1231           DependantMI.UnemittedDependencies.erase(UnemittedDepI);
1232 
1233         // If this symbol is already in the emitted state then we need to
1234         // take responsibility for failing its queries, so add it to the
1235         // worklist.
1236         if (DependantSym.getState() == SymbolState::Emitted) {
1237           assert(DependantMI.Dependants.empty() &&
1238                  "Emitted symbol should not have dependants");
1239           Worklist.push_back(std::make_pair(&DependantJD, DependantName));
1240         }
1241       }
1242     }
1243     MI.Dependants.clear();
1244 
1245     // Disconnect from all unemitted depenencies.
1246     for (auto &KV : MI.UnemittedDependencies) {
1247       auto &UnemittedDepJD = *KV.first;
1248       for (auto &UnemittedDepName : KV.second) {
1249         auto UnemittedDepMII =
1250             UnemittedDepJD.MaterializingInfos.find(UnemittedDepName);
1251         assert(UnemittedDepMII != UnemittedDepJD.MaterializingInfos.end() &&
1252                "Missing MII for unemitted dependency");
1253         assert(UnemittedDepMII->second.Dependants.count(&JD) &&
1254                "JD not listed as a dependant of unemitted dependency");
1255         assert(UnemittedDepMII->second.Dependants[&JD].count(Name) &&
1256                "Name is not listed as a dependant of unemitted dependency");
1257         UnemittedDepMII->second.Dependants[&JD].erase(Name);
1258         if (UnemittedDepMII->second.Dependants[&JD].empty())
1259           UnemittedDepMII->second.Dependants.erase(&JD);
1260       }
1261     }
1262     MI.UnemittedDependencies.clear();
1263 
1264     // Collect queries to be failed for this MII.
1265     AsynchronousSymbolQueryList ToDetach;
1266     for (auto &Q : MII->second.pendingQueries()) {
1267       // Add the query to the list to be failed and detach it.
1268       FailedQueries.insert(Q);
1269       ToDetach.push_back(Q);
1270     }
1271     for (auto &Q : ToDetach)
1272       Q->detach();
1273 
1274     assert(MI.Dependants.empty() &&
1275            "Can not delete MaterializingInfo with dependants still attached");
1276     assert(MI.UnemittedDependencies.empty() &&
1277            "Can not delete MaterializingInfo with unemitted dependencies "
1278            "still attached");
1279     assert(!MI.hasQueriesPending() &&
1280            "Can not delete MaterializingInfo with queries pending");
1281     JD.MaterializingInfos.erase(MII);
1282   }
1283 
1284   return std::make_pair(std::move(FailedQueries), std::move(FailedSymbolsMap));
1285 }
1286 
1287 void JITDylib::setLinkOrder(JITDylibSearchOrder NewLinkOrder,
1288                             bool LinkAgainstThisJITDylibFirst) {
1289   ES.runSessionLocked([&]() {
1290     assert(State == Open && "JD is defunct");
1291     if (LinkAgainstThisJITDylibFirst) {
1292       LinkOrder.clear();
1293       if (NewLinkOrder.empty() || NewLinkOrder.front().first != this)
1294         LinkOrder.push_back(
1295             std::make_pair(this, JITDylibLookupFlags::MatchAllSymbols));
1296       llvm::append_range(LinkOrder, NewLinkOrder);
1297     } else
1298       LinkOrder = std::move(NewLinkOrder);
1299   });
1300 }
1301 
1302 void JITDylib::addToLinkOrder(JITDylib &JD, JITDylibLookupFlags JDLookupFlags) {
1303   ES.runSessionLocked([&]() { LinkOrder.push_back({&JD, JDLookupFlags}); });
1304 }
1305 
1306 void JITDylib::replaceInLinkOrder(JITDylib &OldJD, JITDylib &NewJD,
1307                                   JITDylibLookupFlags JDLookupFlags) {
1308   ES.runSessionLocked([&]() {
1309     assert(State == Open && "JD is defunct");
1310     for (auto &KV : LinkOrder)
1311       if (KV.first == &OldJD) {
1312         KV = {&NewJD, JDLookupFlags};
1313         break;
1314       }
1315   });
1316 }
1317 
1318 void JITDylib::removeFromLinkOrder(JITDylib &JD) {
1319   ES.runSessionLocked([&]() {
1320     assert(State == Open && "JD is defunct");
1321     auto I = llvm::find_if(LinkOrder,
1322                            [&](const JITDylibSearchOrder::value_type &KV) {
1323                              return KV.first == &JD;
1324                            });
1325     if (I != LinkOrder.end())
1326       LinkOrder.erase(I);
1327   });
1328 }
1329 
1330 Error JITDylib::remove(const SymbolNameSet &Names) {
1331   return ES.runSessionLocked([&]() -> Error {
1332     assert(State == Open && "JD is defunct");
1333     using SymbolMaterializerItrPair =
1334         std::pair<SymbolTable::iterator, UnmaterializedInfosMap::iterator>;
1335     std::vector<SymbolMaterializerItrPair> SymbolsToRemove;
1336     SymbolNameSet Missing;
1337     SymbolNameSet Materializing;
1338 
1339     for (auto &Name : Names) {
1340       auto I = Symbols.find(Name);
1341 
1342       // Note symbol missing.
1343       if (I == Symbols.end()) {
1344         Missing.insert(Name);
1345         continue;
1346       }
1347 
1348       // Note symbol materializing.
1349       if (I->second.getState() != SymbolState::NeverSearched &&
1350           I->second.getState() != SymbolState::Ready) {
1351         Materializing.insert(Name);
1352         continue;
1353       }
1354 
1355       auto UMII = I->second.hasMaterializerAttached()
1356                       ? UnmaterializedInfos.find(Name)
1357                       : UnmaterializedInfos.end();
1358       SymbolsToRemove.push_back(std::make_pair(I, UMII));
1359     }
1360 
1361     // If any of the symbols are not defined, return an error.
1362     if (!Missing.empty())
1363       return make_error<SymbolsNotFound>(ES.getSymbolStringPool(),
1364                                          std::move(Missing));
1365 
1366     // If any of the symbols are currently materializing, return an error.
1367     if (!Materializing.empty())
1368       return make_error<SymbolsCouldNotBeRemoved>(ES.getSymbolStringPool(),
1369                                                   std::move(Materializing));
1370 
1371     // Remove the symbols.
1372     for (auto &SymbolMaterializerItrPair : SymbolsToRemove) {
1373       auto UMII = SymbolMaterializerItrPair.second;
1374 
1375       // If there is a materializer attached, call discard.
1376       if (UMII != UnmaterializedInfos.end()) {
1377         UMII->second->MU->doDiscard(*this, UMII->first);
1378         UnmaterializedInfos.erase(UMII);
1379       }
1380 
1381       auto SymI = SymbolMaterializerItrPair.first;
1382       Symbols.erase(SymI);
1383     }
1384 
1385     return Error::success();
1386   });
1387 }
1388 
1389 void JITDylib::dump(raw_ostream &OS) {
1390   ES.runSessionLocked([&, this]() {
1391     OS << "JITDylib \"" << getName() << "\" (ES: "
1392        << format("0x%016" PRIx64, reinterpret_cast<uintptr_t>(&ES))
1393        << ", State = ";
1394     switch (State) {
1395     case Open:
1396       OS << "Open";
1397       break;
1398     case Closing:
1399       OS << "Closing";
1400       break;
1401     case Closed:
1402       OS << "Closed";
1403       break;
1404     }
1405     OS << ")\n";
1406     if (State == Closed)
1407       return;
1408     OS << "Link order: " << LinkOrder << "\n"
1409        << "Symbol table:\n";
1410 
1411     for (auto &KV : Symbols) {
1412       OS << "    \"" << *KV.first << "\": ";
1413       if (auto Addr = KV.second.getAddress())
1414         OS << format("0x%016" PRIx64, Addr) << ", " << KV.second.getFlags()
1415            << " ";
1416       else
1417         OS << "<not resolved> ";
1418 
1419       OS << KV.second.getFlags() << " " << KV.second.getState();
1420 
1421       if (KV.second.hasMaterializerAttached()) {
1422         OS << " (Materializer ";
1423         auto I = UnmaterializedInfos.find(KV.first);
1424         assert(I != UnmaterializedInfos.end() &&
1425                "Lazy symbol should have UnmaterializedInfo");
1426         OS << I->second->MU.get() << ", " << I->second->MU->getName() << ")\n";
1427       } else
1428         OS << "\n";
1429     }
1430 
1431     if (!MaterializingInfos.empty())
1432       OS << "  MaterializingInfos entries:\n";
1433     for (auto &KV : MaterializingInfos) {
1434       OS << "    \"" << *KV.first << "\":\n"
1435          << "      " << KV.second.pendingQueries().size()
1436          << " pending queries: { ";
1437       for (const auto &Q : KV.second.pendingQueries())
1438         OS << Q.get() << " (" << Q->getRequiredState() << ") ";
1439       OS << "}\n      Dependants:\n";
1440       for (auto &KV2 : KV.second.Dependants)
1441         OS << "        " << KV2.first->getName() << ": " << KV2.second << "\n";
1442       OS << "      Unemitted Dependencies:\n";
1443       for (auto &KV2 : KV.second.UnemittedDependencies)
1444         OS << "        " << KV2.first->getName() << ": " << KV2.second << "\n";
1445       assert((Symbols[KV.first].getState() != SymbolState::Ready ||
1446               !KV.second.pendingQueries().empty() ||
1447               !KV.second.Dependants.empty() ||
1448               !KV.second.UnemittedDependencies.empty()) &&
1449              "Stale materializing info entry");
1450     }
1451   });
1452 }
1453 
1454 void JITDylib::MaterializingInfo::addQuery(
1455     std::shared_ptr<AsynchronousSymbolQuery> Q) {
1456 
1457   auto I = std::lower_bound(
1458       PendingQueries.rbegin(), PendingQueries.rend(), Q->getRequiredState(),
1459       [](const std::shared_ptr<AsynchronousSymbolQuery> &V, SymbolState S) {
1460         return V->getRequiredState() <= S;
1461       });
1462   PendingQueries.insert(I.base(), std::move(Q));
1463 }
1464 
1465 void JITDylib::MaterializingInfo::removeQuery(
1466     const AsynchronousSymbolQuery &Q) {
1467   // FIXME: Implement 'find_as' for shared_ptr<T>/T*.
1468   auto I = llvm::find_if(
1469       PendingQueries, [&Q](const std::shared_ptr<AsynchronousSymbolQuery> &V) {
1470         return V.get() == &Q;
1471       });
1472   assert(I != PendingQueries.end() &&
1473          "Query is not attached to this MaterializingInfo");
1474   PendingQueries.erase(I);
1475 }
1476 
1477 JITDylib::AsynchronousSymbolQueryList
1478 JITDylib::MaterializingInfo::takeQueriesMeeting(SymbolState RequiredState) {
1479   AsynchronousSymbolQueryList Result;
1480   while (!PendingQueries.empty()) {
1481     if (PendingQueries.back()->getRequiredState() > RequiredState)
1482       break;
1483 
1484     Result.push_back(std::move(PendingQueries.back()));
1485     PendingQueries.pop_back();
1486   }
1487 
1488   return Result;
1489 }
1490 
1491 JITDylib::JITDylib(ExecutionSession &ES, std::string Name)
1492     : JITLinkDylib(std::move(Name)), ES(ES) {
1493   LinkOrder.push_back({this, JITDylibLookupFlags::MatchAllSymbols});
1494 }
1495 
1496 std::pair<JITDylib::AsynchronousSymbolQuerySet,
1497           std::shared_ptr<SymbolDependenceMap>>
1498 JITDylib::removeTracker(ResourceTracker &RT) {
1499   // Note: Should be called under the session lock.
1500   assert(State != Closed && "JD is defunct");
1501 
1502   SymbolNameVector SymbolsToRemove;
1503   std::vector<std::pair<JITDylib *, SymbolStringPtr>> SymbolsToFail;
1504 
1505   if (&RT == DefaultTracker.get()) {
1506     SymbolNameSet TrackedSymbols;
1507     for (auto &KV : TrackerSymbols)
1508       for (auto &Sym : KV.second)
1509         TrackedSymbols.insert(Sym);
1510 
1511     for (auto &KV : Symbols) {
1512       auto &Sym = KV.first;
1513       if (!TrackedSymbols.count(Sym))
1514         SymbolsToRemove.push_back(Sym);
1515     }
1516 
1517     DefaultTracker.reset();
1518   } else {
1519     /// Check for a non-default tracker.
1520     auto I = TrackerSymbols.find(&RT);
1521     if (I != TrackerSymbols.end()) {
1522       SymbolsToRemove = std::move(I->second);
1523       TrackerSymbols.erase(I);
1524     }
1525     // ... if not found this tracker was already defunct. Nothing to do.
1526   }
1527 
1528   for (auto &Sym : SymbolsToRemove) {
1529     assert(Symbols.count(Sym) && "Symbol not in symbol table");
1530 
1531     // If there is a MaterializingInfo then collect any queries to fail.
1532     auto MII = MaterializingInfos.find(Sym);
1533     if (MII != MaterializingInfos.end())
1534       SymbolsToFail.push_back({this, Sym});
1535   }
1536 
1537   AsynchronousSymbolQuerySet QueriesToFail;
1538   auto Result = failSymbols(std::move(SymbolsToFail));
1539 
1540   // Removed symbols should be taken out of the table altogether.
1541   for (auto &Sym : SymbolsToRemove) {
1542     auto I = Symbols.find(Sym);
1543     assert(I != Symbols.end() && "Symbol not present in table");
1544 
1545     // Remove Materializer if present.
1546     if (I->second.hasMaterializerAttached()) {
1547       // FIXME: Should this discard the symbols?
1548       UnmaterializedInfos.erase(Sym);
1549     } else {
1550       assert(!UnmaterializedInfos.count(Sym) &&
1551              "Symbol has materializer attached");
1552     }
1553 
1554     Symbols.erase(I);
1555   }
1556 
1557   return Result;
1558 }
1559 
1560 void JITDylib::transferTracker(ResourceTracker &DstRT, ResourceTracker &SrcRT) {
1561   assert(State != Closed && "JD is defunct");
1562   assert(&DstRT != &SrcRT && "No-op transfers shouldn't call transferTracker");
1563   assert(&DstRT.getJITDylib() == this && "DstRT is not for this JITDylib");
1564   assert(&SrcRT.getJITDylib() == this && "SrcRT is not for this JITDylib");
1565 
1566   // Update trackers for any not-yet materialized units.
1567   for (auto &KV : UnmaterializedInfos) {
1568     if (KV.second->RT == &SrcRT)
1569       KV.second->RT = &DstRT;
1570   }
1571 
1572   // Update trackers for any active materialization responsibilities.
1573   {
1574     auto I = TrackerMRs.find(&SrcRT);
1575     if (I != TrackerMRs.end()) {
1576       auto &SrcMRs = I->second;
1577       auto &DstMRs = TrackerMRs[&DstRT];
1578       for (auto *MR : SrcMRs)
1579         MR->RT = &DstRT;
1580       if (DstMRs.empty())
1581         DstMRs = std::move(SrcMRs);
1582       else
1583         for (auto *MR : SrcMRs)
1584           DstMRs.insert(MR);
1585       // Erase SrcRT entry in TrackerMRs. Use &SrcRT key rather than iterator I
1586       // for this, since I may have been invalidated by 'TrackerMRs[&DstRT]'.
1587       TrackerMRs.erase(&SrcRT);
1588     }
1589   }
1590 
1591   // If we're transfering to the default tracker we just need to delete the
1592   // tracked symbols for the source tracker.
1593   if (&DstRT == DefaultTracker.get()) {
1594     TrackerSymbols.erase(&SrcRT);
1595     return;
1596   }
1597 
1598   // If we're transferring from the default tracker we need to find all
1599   // currently untracked symbols.
1600   if (&SrcRT == DefaultTracker.get()) {
1601     assert(!TrackerSymbols.count(&SrcRT) &&
1602            "Default tracker should not appear in TrackerSymbols");
1603 
1604     SymbolNameVector SymbolsToTrack;
1605 
1606     SymbolNameSet CurrentlyTrackedSymbols;
1607     for (auto &KV : TrackerSymbols)
1608       for (auto &Sym : KV.second)
1609         CurrentlyTrackedSymbols.insert(Sym);
1610 
1611     for (auto &KV : Symbols) {
1612       auto &Sym = KV.first;
1613       if (!CurrentlyTrackedSymbols.count(Sym))
1614         SymbolsToTrack.push_back(Sym);
1615     }
1616 
1617     TrackerSymbols[&DstRT] = std::move(SymbolsToTrack);
1618     return;
1619   }
1620 
1621   auto &DstTrackedSymbols = TrackerSymbols[&DstRT];
1622 
1623   // Finally if neither SrtRT or DstRT are the default tracker then
1624   // just append DstRT's tracked symbols to SrtRT's.
1625   auto SI = TrackerSymbols.find(&SrcRT);
1626   if (SI == TrackerSymbols.end())
1627     return;
1628 
1629   DstTrackedSymbols.reserve(DstTrackedSymbols.size() + SI->second.size());
1630   for (auto &Sym : SI->second)
1631     DstTrackedSymbols.push_back(std::move(Sym));
1632   TrackerSymbols.erase(SI);
1633 }
1634 
1635 Error JITDylib::defineImpl(MaterializationUnit &MU) {
1636 
1637   LLVM_DEBUG({ dbgs() << "  " << MU.getSymbols() << "\n"; });
1638 
1639   SymbolNameSet Duplicates;
1640   std::vector<SymbolStringPtr> ExistingDefsOverridden;
1641   std::vector<SymbolStringPtr> MUDefsOverridden;
1642 
1643   for (const auto &KV : MU.getSymbols()) {
1644     auto I = Symbols.find(KV.first);
1645 
1646     if (I != Symbols.end()) {
1647       if (KV.second.isStrong()) {
1648         if (I->second.getFlags().isStrong() ||
1649             I->second.getState() > SymbolState::NeverSearched)
1650           Duplicates.insert(KV.first);
1651         else {
1652           assert(I->second.getState() == SymbolState::NeverSearched &&
1653                  "Overridden existing def should be in the never-searched "
1654                  "state");
1655           ExistingDefsOverridden.push_back(KV.first);
1656         }
1657       } else
1658         MUDefsOverridden.push_back(KV.first);
1659     }
1660   }
1661 
1662   // If there were any duplicate definitions then bail out.
1663   if (!Duplicates.empty()) {
1664     LLVM_DEBUG(
1665         { dbgs() << "  Error: Duplicate symbols " << Duplicates << "\n"; });
1666     return make_error<DuplicateDefinition>(std::string(**Duplicates.begin()));
1667   }
1668 
1669   // Discard any overridden defs in this MU.
1670   LLVM_DEBUG({
1671     if (!MUDefsOverridden.empty())
1672       dbgs() << "  Defs in this MU overridden: " << MUDefsOverridden << "\n";
1673   });
1674   for (auto &S : MUDefsOverridden)
1675     MU.doDiscard(*this, S);
1676 
1677   // Discard existing overridden defs.
1678   LLVM_DEBUG({
1679     if (!ExistingDefsOverridden.empty())
1680       dbgs() << "  Existing defs overridden by this MU: " << MUDefsOverridden
1681              << "\n";
1682   });
1683   for (auto &S : ExistingDefsOverridden) {
1684 
1685     auto UMII = UnmaterializedInfos.find(S);
1686     assert(UMII != UnmaterializedInfos.end() &&
1687            "Overridden existing def should have an UnmaterializedInfo");
1688     UMII->second->MU->doDiscard(*this, S);
1689   }
1690 
1691   // Finally, add the defs from this MU.
1692   for (auto &KV : MU.getSymbols()) {
1693     auto &SymEntry = Symbols[KV.first];
1694     SymEntry.setFlags(KV.second);
1695     SymEntry.setState(SymbolState::NeverSearched);
1696     SymEntry.setMaterializerAttached(true);
1697   }
1698 
1699   return Error::success();
1700 }
1701 
1702 void JITDylib::installMaterializationUnit(
1703     std::unique_ptr<MaterializationUnit> MU, ResourceTracker &RT) {
1704 
1705   /// defineImpl succeeded.
1706   if (&RT != DefaultTracker.get()) {
1707     auto &TS = TrackerSymbols[&RT];
1708     TS.reserve(TS.size() + MU->getSymbols().size());
1709     for (auto &KV : MU->getSymbols())
1710       TS.push_back(KV.first);
1711   }
1712 
1713   auto UMI = std::make_shared<UnmaterializedInfo>(std::move(MU), &RT);
1714   for (auto &KV : UMI->MU->getSymbols())
1715     UnmaterializedInfos[KV.first] = UMI;
1716 }
1717 
1718 void JITDylib::detachQueryHelper(AsynchronousSymbolQuery &Q,
1719                                  const SymbolNameSet &QuerySymbols) {
1720   for (auto &QuerySymbol : QuerySymbols) {
1721     assert(MaterializingInfos.count(QuerySymbol) &&
1722            "QuerySymbol does not have MaterializingInfo");
1723     auto &MI = MaterializingInfos[QuerySymbol];
1724     MI.removeQuery(Q);
1725   }
1726 }
1727 
1728 void JITDylib::transferEmittedNodeDependencies(
1729     MaterializingInfo &DependantMI, const SymbolStringPtr &DependantName,
1730     MaterializingInfo &EmittedMI) {
1731   for (auto &KV : EmittedMI.UnemittedDependencies) {
1732     auto &DependencyJD = *KV.first;
1733     SymbolNameSet *UnemittedDependenciesOnDependencyJD = nullptr;
1734 
1735     for (auto &DependencyName : KV.second) {
1736       auto &DependencyMI = DependencyJD.MaterializingInfos[DependencyName];
1737 
1738       // Do not add self dependencies.
1739       if (&DependencyMI == &DependantMI)
1740         continue;
1741 
1742       // If we haven't looked up the dependencies for DependencyJD yet, do it
1743       // now and cache the result.
1744       if (!UnemittedDependenciesOnDependencyJD)
1745         UnemittedDependenciesOnDependencyJD =
1746             &DependantMI.UnemittedDependencies[&DependencyJD];
1747 
1748       DependencyMI.Dependants[this].insert(DependantName);
1749       UnemittedDependenciesOnDependencyJD->insert(DependencyName);
1750     }
1751   }
1752 }
1753 
1754 Platform::~Platform() {}
1755 
1756 Expected<DenseMap<JITDylib *, SymbolMap>> Platform::lookupInitSymbols(
1757     ExecutionSession &ES,
1758     const DenseMap<JITDylib *, SymbolLookupSet> &InitSyms) {
1759 
1760   DenseMap<JITDylib *, SymbolMap> CompoundResult;
1761   Error CompoundErr = Error::success();
1762   std::mutex LookupMutex;
1763   std::condition_variable CV;
1764   uint64_t Count = InitSyms.size();
1765 
1766   LLVM_DEBUG({
1767     dbgs() << "Issuing init-symbol lookup:\n";
1768     for (auto &KV : InitSyms)
1769       dbgs() << "  " << KV.first->getName() << ": " << KV.second << "\n";
1770   });
1771 
1772   for (auto &KV : InitSyms) {
1773     auto *JD = KV.first;
1774     auto Names = std::move(KV.second);
1775     ES.lookup(
1776         LookupKind::Static,
1777         JITDylibSearchOrder({{JD, JITDylibLookupFlags::MatchAllSymbols}}),
1778         std::move(Names), SymbolState::Ready,
1779         [&, JD](Expected<SymbolMap> Result) {
1780           {
1781             std::lock_guard<std::mutex> Lock(LookupMutex);
1782             --Count;
1783             if (Result) {
1784               assert(!CompoundResult.count(JD) &&
1785                      "Duplicate JITDylib in lookup?");
1786               CompoundResult[JD] = std::move(*Result);
1787             } else
1788               CompoundErr =
1789                   joinErrors(std::move(CompoundErr), Result.takeError());
1790           }
1791           CV.notify_one();
1792         },
1793         NoDependenciesToRegister);
1794   }
1795 
1796   std::unique_lock<std::mutex> Lock(LookupMutex);
1797   CV.wait(Lock, [&] { return Count == 0 || CompoundErr; });
1798 
1799   if (CompoundErr)
1800     return std::move(CompoundErr);
1801 
1802   return std::move(CompoundResult);
1803 }
1804 
1805 void Platform::lookupInitSymbolsAsync(
1806     unique_function<void(Error)> OnComplete, ExecutionSession &ES,
1807     const DenseMap<JITDylib *, SymbolLookupSet> &InitSyms) {
1808 
1809   class TriggerOnComplete {
1810   public:
1811     using OnCompleteFn = unique_function<void(Error)>;
1812     TriggerOnComplete(OnCompleteFn OnComplete)
1813         : OnComplete(std::move(OnComplete)) {}
1814     ~TriggerOnComplete() { OnComplete(std::move(LookupResult)); }
1815     void reportResult(Error Err) {
1816       std::lock_guard<std::mutex> Lock(ResultMutex);
1817       LookupResult = joinErrors(std::move(LookupResult), std::move(Err));
1818     }
1819 
1820   private:
1821     std::mutex ResultMutex;
1822     Error LookupResult{Error::success()};
1823     OnCompleteFn OnComplete;
1824   };
1825 
1826   LLVM_DEBUG({
1827     dbgs() << "Issuing init-symbol lookup:\n";
1828     for (auto &KV : InitSyms)
1829       dbgs() << "  " << KV.first->getName() << ": " << KV.second << "\n";
1830   });
1831 
1832   auto TOC = std::make_shared<TriggerOnComplete>(std::move(OnComplete));
1833 
1834   for (auto &KV : InitSyms) {
1835     auto *JD = KV.first;
1836     auto Names = std::move(KV.second);
1837     ES.lookup(
1838         LookupKind::Static,
1839         JITDylibSearchOrder({{JD, JITDylibLookupFlags::MatchAllSymbols}}),
1840         std::move(Names), SymbolState::Ready,
1841         [TOC](Expected<SymbolMap> Result) {
1842           TOC->reportResult(Result.takeError());
1843         },
1844         NoDependenciesToRegister);
1845   }
1846 }
1847 
1848 void MaterializationTask::printDescription(raw_ostream &OS) {
1849   OS << "Materialization task: " << MU->getName() << " in "
1850      << MR->getTargetJITDylib().getName();
1851 }
1852 
1853 void MaterializationTask::run() { MU->materialize(std::move(MR)); }
1854 
1855 ExecutionSession::ExecutionSession(std::unique_ptr<ExecutorProcessControl> EPC)
1856     : EPC(std::move(EPC)) {
1857   // Associated EPC and this.
1858   this->EPC->ES = this;
1859 }
1860 
1861 Error ExecutionSession::endSession() {
1862   LLVM_DEBUG(dbgs() << "Ending ExecutionSession " << this << "\n");
1863 
1864   std::vector<JITDylibSP> JITDylibsToClose = runSessionLocked([&] {
1865     SessionOpen = false;
1866     return std::move(JDs);
1867   });
1868 
1869   // TODO: notifiy platform? run static deinits?
1870 
1871   Error Err = Error::success();
1872   for (auto &JD : JITDylibsToClose)
1873     Err = joinErrors(std::move(Err), JD->clear());
1874 
1875   Err = joinErrors(std::move(Err), EPC->disconnect());
1876 
1877   return Err;
1878 }
1879 
1880 void ExecutionSession::registerResourceManager(ResourceManager &RM) {
1881   runSessionLocked([&] { ResourceManagers.push_back(&RM); });
1882 }
1883 
1884 void ExecutionSession::deregisterResourceManager(ResourceManager &RM) {
1885   runSessionLocked([&] {
1886     assert(!ResourceManagers.empty() && "No managers registered");
1887     if (ResourceManagers.back() == &RM)
1888       ResourceManagers.pop_back();
1889     else {
1890       auto I = llvm::find(ResourceManagers, &RM);
1891       assert(I != ResourceManagers.end() && "RM not registered");
1892       ResourceManagers.erase(I);
1893     }
1894   });
1895 }
1896 
1897 JITDylib *ExecutionSession::getJITDylibByName(StringRef Name) {
1898   return runSessionLocked([&, this]() -> JITDylib * {
1899     for (auto &JD : JDs)
1900       if (JD->getName() == Name)
1901         return JD.get();
1902     return nullptr;
1903   });
1904 }
1905 
1906 JITDylib &ExecutionSession::createBareJITDylib(std::string Name) {
1907   assert(!getJITDylibByName(Name) && "JITDylib with that name already exists");
1908   return runSessionLocked([&, this]() -> JITDylib & {
1909     JDs.push_back(new JITDylib(*this, std::move(Name)));
1910     return *JDs.back();
1911   });
1912 }
1913 
1914 Expected<JITDylib &> ExecutionSession::createJITDylib(std::string Name) {
1915   auto &JD = createBareJITDylib(Name);
1916   if (P)
1917     if (auto Err = P->setupJITDylib(JD))
1918       return std::move(Err);
1919   return JD;
1920 }
1921 
1922 Error ExecutionSession::removeJITDylib(JITDylib &JD) {
1923   // Keep JD alive throughout this routine, even if all other references
1924   // have been dropped.
1925   JITDylibSP JDKeepAlive = &JD;
1926 
1927   // Set JD to 'Closing' state and remove JD from the ExecutionSession.
1928   runSessionLocked([&] {
1929     assert(JD.State == JITDylib::Open && "JD already closed");
1930     JD.State = JITDylib::Closing;
1931     auto I = llvm::find(JDs, &JD);
1932     assert(I != JDs.end() && "JD does not appear in session JDs");
1933     JDs.erase(I);
1934   });
1935 
1936   // Clear the JITDylib. Hold on to any error while we clean up the
1937   // JITDylib members below.
1938   auto Err = JD.clear();
1939 
1940   // Notify the platform of the teardown.
1941   if (P)
1942     Err = joinErrors(std::move(Err), P->teardownJITDylib(JD));
1943 
1944   // Set JD to closed state. Clear remaining data structures.
1945   runSessionLocked([&] {
1946     assert(JD.State == JITDylib::Closing && "JD should be closing");
1947     JD.State = JITDylib::Closed;
1948     assert(JD.Symbols.empty() && "JD.Symbols is not empty after clear");
1949     assert(JD.UnmaterializedInfos.empty() &&
1950            "JD.UnmaterializedInfos is not empty after clear");
1951     assert(JD.MaterializingInfos.empty() &&
1952            "JD.MaterializingInfos is not empty after clear");
1953     assert(JD.TrackerSymbols.empty() &&
1954            "TrackerSymbols is not empty after clear");
1955     JD.DefGenerators.clear();
1956     JD.LinkOrder.clear();
1957   });
1958   return Err;
1959 }
1960 
1961 std::vector<JITDylibSP> JITDylib::getDFSLinkOrder(ArrayRef<JITDylibSP> JDs) {
1962   if (JDs.empty())
1963     return {};
1964 
1965   auto &ES = JDs.front()->getExecutionSession();
1966   return ES.runSessionLocked([&]() {
1967     DenseSet<JITDylib *> Visited;
1968     std::vector<JITDylibSP> Result;
1969 
1970     for (auto &JD : JDs) {
1971 
1972       assert(JD->State == Open && "JD is defunct");
1973 
1974       if (Visited.count(JD.get()))
1975         continue;
1976 
1977       SmallVector<JITDylibSP, 64> WorkStack;
1978       WorkStack.push_back(JD);
1979       Visited.insert(JD.get());
1980 
1981       while (!WorkStack.empty()) {
1982         Result.push_back(std::move(WorkStack.back()));
1983         WorkStack.pop_back();
1984 
1985         for (auto &KV : llvm::reverse(Result.back()->LinkOrder)) {
1986           auto &JD = *KV.first;
1987           if (Visited.count(&JD))
1988             continue;
1989           Visited.insert(&JD);
1990           WorkStack.push_back(&JD);
1991         }
1992       }
1993     }
1994     return Result;
1995   });
1996 }
1997 
1998 std::vector<JITDylibSP>
1999 JITDylib::getReverseDFSLinkOrder(ArrayRef<JITDylibSP> JDs) {
2000   auto Tmp = getDFSLinkOrder(JDs);
2001   std::reverse(Tmp.begin(), Tmp.end());
2002   return Tmp;
2003 }
2004 
2005 std::vector<JITDylibSP> JITDylib::getDFSLinkOrder() {
2006   return getDFSLinkOrder({this});
2007 }
2008 
2009 std::vector<JITDylibSP> JITDylib::getReverseDFSLinkOrder() {
2010   return getReverseDFSLinkOrder({this});
2011 }
2012 
2013 void ExecutionSession::lookupFlags(
2014     LookupKind K, JITDylibSearchOrder SearchOrder, SymbolLookupSet LookupSet,
2015     unique_function<void(Expected<SymbolFlagsMap>)> OnComplete) {
2016 
2017   OL_applyQueryPhase1(std::make_unique<InProgressLookupFlagsState>(
2018                           K, std::move(SearchOrder), std::move(LookupSet),
2019                           std::move(OnComplete)),
2020                       Error::success());
2021 }
2022 
2023 Expected<SymbolFlagsMap>
2024 ExecutionSession::lookupFlags(LookupKind K, JITDylibSearchOrder SearchOrder,
2025                               SymbolLookupSet LookupSet) {
2026 
2027   std::promise<MSVCPExpected<SymbolFlagsMap>> ResultP;
2028   OL_applyQueryPhase1(std::make_unique<InProgressLookupFlagsState>(
2029                           K, std::move(SearchOrder), std::move(LookupSet),
2030                           [&ResultP](Expected<SymbolFlagsMap> Result) {
2031                             ResultP.set_value(std::move(Result));
2032                           }),
2033                       Error::success());
2034 
2035   auto ResultF = ResultP.get_future();
2036   return ResultF.get();
2037 }
2038 
2039 void ExecutionSession::lookup(
2040     LookupKind K, const JITDylibSearchOrder &SearchOrder,
2041     SymbolLookupSet Symbols, SymbolState RequiredState,
2042     SymbolsResolvedCallback NotifyComplete,
2043     RegisterDependenciesFunction RegisterDependencies) {
2044 
2045   LLVM_DEBUG({
2046     runSessionLocked([&]() {
2047       dbgs() << "Looking up " << Symbols << " in " << SearchOrder
2048              << " (required state: " << RequiredState << ")\n";
2049     });
2050   });
2051 
2052   // lookup can be re-entered recursively if running on a single thread. Run any
2053   // outstanding MUs in case this query depends on them, otherwise this lookup
2054   // will starve waiting for a result from an MU that is stuck in the queue.
2055   dispatchOutstandingMUs();
2056 
2057   auto Unresolved = std::move(Symbols);
2058   auto Q = std::make_shared<AsynchronousSymbolQuery>(Unresolved, RequiredState,
2059                                                      std::move(NotifyComplete));
2060 
2061   auto IPLS = std::make_unique<InProgressFullLookupState>(
2062       K, SearchOrder, std::move(Unresolved), RequiredState, std::move(Q),
2063       std::move(RegisterDependencies));
2064 
2065   OL_applyQueryPhase1(std::move(IPLS), Error::success());
2066 }
2067 
2068 Expected<SymbolMap>
2069 ExecutionSession::lookup(const JITDylibSearchOrder &SearchOrder,
2070                          const SymbolLookupSet &Symbols, LookupKind K,
2071                          SymbolState RequiredState,
2072                          RegisterDependenciesFunction RegisterDependencies) {
2073 #if LLVM_ENABLE_THREADS
2074   // In the threaded case we use promises to return the results.
2075   std::promise<SymbolMap> PromisedResult;
2076   Error ResolutionError = Error::success();
2077 
2078   auto NotifyComplete = [&](Expected<SymbolMap> R) {
2079     if (R)
2080       PromisedResult.set_value(std::move(*R));
2081     else {
2082       ErrorAsOutParameter _(&ResolutionError);
2083       ResolutionError = R.takeError();
2084       PromisedResult.set_value(SymbolMap());
2085     }
2086   };
2087 
2088 #else
2089   SymbolMap Result;
2090   Error ResolutionError = Error::success();
2091 
2092   auto NotifyComplete = [&](Expected<SymbolMap> R) {
2093     ErrorAsOutParameter _(&ResolutionError);
2094     if (R)
2095       Result = std::move(*R);
2096     else
2097       ResolutionError = R.takeError();
2098   };
2099 #endif
2100 
2101   // Perform the asynchronous lookup.
2102   lookup(K, SearchOrder, Symbols, RequiredState, NotifyComplete,
2103          RegisterDependencies);
2104 
2105 #if LLVM_ENABLE_THREADS
2106   auto ResultFuture = PromisedResult.get_future();
2107   auto Result = ResultFuture.get();
2108 
2109   if (ResolutionError)
2110     return std::move(ResolutionError);
2111 
2112   return std::move(Result);
2113 
2114 #else
2115   if (ResolutionError)
2116     return std::move(ResolutionError);
2117 
2118   return Result;
2119 #endif
2120 }
2121 
2122 Expected<JITEvaluatedSymbol>
2123 ExecutionSession::lookup(const JITDylibSearchOrder &SearchOrder,
2124                          SymbolStringPtr Name, SymbolState RequiredState) {
2125   SymbolLookupSet Names({Name});
2126 
2127   if (auto ResultMap = lookup(SearchOrder, std::move(Names), LookupKind::Static,
2128                               RequiredState, NoDependenciesToRegister)) {
2129     assert(ResultMap->size() == 1 && "Unexpected number of results");
2130     assert(ResultMap->count(Name) && "Missing result for symbol");
2131     return std::move(ResultMap->begin()->second);
2132   } else
2133     return ResultMap.takeError();
2134 }
2135 
2136 Expected<JITEvaluatedSymbol>
2137 ExecutionSession::lookup(ArrayRef<JITDylib *> SearchOrder, SymbolStringPtr Name,
2138                          SymbolState RequiredState) {
2139   return lookup(makeJITDylibSearchOrder(SearchOrder), Name, RequiredState);
2140 }
2141 
2142 Expected<JITEvaluatedSymbol>
2143 ExecutionSession::lookup(ArrayRef<JITDylib *> SearchOrder, StringRef Name,
2144                          SymbolState RequiredState) {
2145   return lookup(SearchOrder, intern(Name), RequiredState);
2146 }
2147 
2148 Error ExecutionSession::registerJITDispatchHandlers(
2149     JITDylib &JD, JITDispatchHandlerAssociationMap WFs) {
2150 
2151   auto TagAddrs = lookup({{&JD, JITDylibLookupFlags::MatchAllSymbols}},
2152                          SymbolLookupSet::fromMapKeys(
2153                              WFs, SymbolLookupFlags::WeaklyReferencedSymbol));
2154   if (!TagAddrs)
2155     return TagAddrs.takeError();
2156 
2157   // Associate tag addresses with implementations.
2158   std::lock_guard<std::mutex> Lock(JITDispatchHandlersMutex);
2159   for (auto &KV : *TagAddrs) {
2160     auto TagAddr = KV.second.getAddress();
2161     if (JITDispatchHandlers.count(TagAddr))
2162       return make_error<StringError>("Tag " + formatv("{0:x16}", TagAddr) +
2163                                          " (for " + *KV.first +
2164                                          ") already registered",
2165                                      inconvertibleErrorCode());
2166     auto I = WFs.find(KV.first);
2167     assert(I != WFs.end() && I->second &&
2168            "JITDispatchHandler implementation missing");
2169     JITDispatchHandlers[KV.second.getAddress()] =
2170         std::make_shared<JITDispatchHandlerFunction>(std::move(I->second));
2171     LLVM_DEBUG({
2172       dbgs() << "Associated function tag \"" << *KV.first << "\" ("
2173              << formatv("{0:x}", KV.second.getAddress()) << ") with handler\n";
2174     });
2175   }
2176   return Error::success();
2177 }
2178 
2179 void ExecutionSession::runJITDispatchHandler(
2180     SendResultFunction SendResult, JITTargetAddress HandlerFnTagAddr,
2181     ArrayRef<char> ArgBuffer) {
2182 
2183   std::shared_ptr<JITDispatchHandlerFunction> F;
2184   {
2185     std::lock_guard<std::mutex> Lock(JITDispatchHandlersMutex);
2186     auto I = JITDispatchHandlers.find(HandlerFnTagAddr);
2187     if (I != JITDispatchHandlers.end())
2188       F = I->second;
2189   }
2190 
2191   if (F)
2192     (*F)(std::move(SendResult), ArgBuffer.data(), ArgBuffer.size());
2193   else
2194     SendResult(shared::WrapperFunctionResult::createOutOfBandError(
2195         ("No function registered for tag " +
2196          formatv("{0:x16}", HandlerFnTagAddr))
2197             .str()));
2198 }
2199 
2200 void ExecutionSession::dump(raw_ostream &OS) {
2201   runSessionLocked([this, &OS]() {
2202     for (auto &JD : JDs)
2203       JD->dump(OS);
2204   });
2205 }
2206 
2207 void ExecutionSession::dispatchOutstandingMUs() {
2208   LLVM_DEBUG(dbgs() << "Dispatching MaterializationUnits...\n");
2209   while (true) {
2210     Optional<std::pair<std::unique_ptr<MaterializationUnit>,
2211                        std::unique_ptr<MaterializationResponsibility>>>
2212         JMU;
2213 
2214     {
2215       std::lock_guard<std::recursive_mutex> Lock(OutstandingMUsMutex);
2216       if (!OutstandingMUs.empty()) {
2217         JMU.emplace(std::move(OutstandingMUs.back()));
2218         OutstandingMUs.pop_back();
2219       }
2220     }
2221 
2222     if (!JMU)
2223       break;
2224 
2225     assert(JMU->first && "No MU?");
2226     LLVM_DEBUG(dbgs() << "  Dispatching \"" << JMU->first->getName() << "\"\n");
2227     dispatchTask(std::make_unique<MaterializationTask>(std::move(JMU->first),
2228                                                        std::move(JMU->second)));
2229   }
2230   LLVM_DEBUG(dbgs() << "Done dispatching MaterializationUnits.\n");
2231 }
2232 
2233 Error ExecutionSession::removeResourceTracker(ResourceTracker &RT) {
2234   LLVM_DEBUG({
2235     dbgs() << "In " << RT.getJITDylib().getName() << " removing tracker "
2236            << formatv("{0:x}", RT.getKeyUnsafe()) << "\n";
2237   });
2238   std::vector<ResourceManager *> CurrentResourceManagers;
2239 
2240   JITDylib::AsynchronousSymbolQuerySet QueriesToFail;
2241   std::shared_ptr<SymbolDependenceMap> FailedSymbols;
2242 
2243   runSessionLocked([&] {
2244     CurrentResourceManagers = ResourceManagers;
2245     RT.makeDefunct();
2246     std::tie(QueriesToFail, FailedSymbols) = RT.getJITDylib().removeTracker(RT);
2247   });
2248 
2249   Error Err = Error::success();
2250 
2251   for (auto *L : reverse(CurrentResourceManagers))
2252     Err =
2253         joinErrors(std::move(Err), L->handleRemoveResources(RT.getKeyUnsafe()));
2254 
2255   for (auto &Q : QueriesToFail)
2256     Q->handleFailed(make_error<FailedToMaterialize>(FailedSymbols));
2257 
2258   return Err;
2259 }
2260 
2261 void ExecutionSession::transferResourceTracker(ResourceTracker &DstRT,
2262                                                ResourceTracker &SrcRT) {
2263   LLVM_DEBUG({
2264     dbgs() << "In " << SrcRT.getJITDylib().getName()
2265            << " transfering resources from tracker "
2266            << formatv("{0:x}", SrcRT.getKeyUnsafe()) << " to tracker "
2267            << formatv("{0:x}", DstRT.getKeyUnsafe()) << "\n";
2268   });
2269 
2270   // No-op transfers are allowed and do not invalidate the source.
2271   if (&DstRT == &SrcRT)
2272     return;
2273 
2274   assert(&DstRT.getJITDylib() == &SrcRT.getJITDylib() &&
2275          "Can't transfer resources between JITDylibs");
2276   runSessionLocked([&]() {
2277     SrcRT.makeDefunct();
2278     auto &JD = DstRT.getJITDylib();
2279     JD.transferTracker(DstRT, SrcRT);
2280     for (auto *L : reverse(ResourceManagers))
2281       L->handleTransferResources(DstRT.getKeyUnsafe(), SrcRT.getKeyUnsafe());
2282   });
2283 }
2284 
2285 void ExecutionSession::destroyResourceTracker(ResourceTracker &RT) {
2286   runSessionLocked([&]() {
2287     LLVM_DEBUG({
2288       dbgs() << "In " << RT.getJITDylib().getName() << " destroying tracker "
2289              << formatv("{0:x}", RT.getKeyUnsafe()) << "\n";
2290     });
2291     if (!RT.isDefunct())
2292       transferResourceTracker(*RT.getJITDylib().getDefaultResourceTracker(),
2293                               RT);
2294   });
2295 }
2296 
2297 Error ExecutionSession::IL_updateCandidatesFor(
2298     JITDylib &JD, JITDylibLookupFlags JDLookupFlags,
2299     SymbolLookupSet &Candidates, SymbolLookupSet *NonCandidates) {
2300   return Candidates.forEachWithRemoval(
2301       [&](const SymbolStringPtr &Name,
2302           SymbolLookupFlags SymLookupFlags) -> Expected<bool> {
2303         /// Search for the symbol. If not found then continue without
2304         /// removal.
2305         auto SymI = JD.Symbols.find(Name);
2306         if (SymI == JD.Symbols.end())
2307           return false;
2308 
2309         // If this is a non-exported symbol and we're matching exported
2310         // symbols only then remove this symbol from the candidates list.
2311         //
2312         // If we're tracking non-candidates then add this to the non-candidate
2313         // list.
2314         if (!SymI->second.getFlags().isExported() &&
2315             JDLookupFlags == JITDylibLookupFlags::MatchExportedSymbolsOnly) {
2316           if (NonCandidates)
2317             NonCandidates->add(Name, SymLookupFlags);
2318           return true;
2319         }
2320 
2321         // If we match against a materialization-side-effects only symbol
2322         // then make sure it is weakly-referenced. Otherwise bail out with
2323         // an error.
2324         // FIXME: Use a "materialization-side-effects-only symbols must be
2325         // weakly referenced" specific error here to reduce confusion.
2326         if (SymI->second.getFlags().hasMaterializationSideEffectsOnly() &&
2327             SymLookupFlags != SymbolLookupFlags::WeaklyReferencedSymbol)
2328           return make_error<SymbolsNotFound>(getSymbolStringPool(),
2329                                              SymbolNameVector({Name}));
2330 
2331         // If we matched against this symbol but it is in the error state
2332         // then bail out and treat it as a failure to materialize.
2333         if (SymI->second.getFlags().hasError()) {
2334           auto FailedSymbolsMap = std::make_shared<SymbolDependenceMap>();
2335           (*FailedSymbolsMap)[&JD] = {Name};
2336           return make_error<FailedToMaterialize>(std::move(FailedSymbolsMap));
2337         }
2338 
2339         // Otherwise this is a match. Remove it from the candidate set.
2340         return true;
2341       });
2342 }
2343 
2344 void ExecutionSession::OL_applyQueryPhase1(
2345     std::unique_ptr<InProgressLookupState> IPLS, Error Err) {
2346 
2347   LLVM_DEBUG({
2348     dbgs() << "Entering OL_applyQueryPhase1:\n"
2349            << "  Lookup kind: " << IPLS->K << "\n"
2350            << "  Search order: " << IPLS->SearchOrder
2351            << ", Current index = " << IPLS->CurSearchOrderIndex
2352            << (IPLS->NewJITDylib ? " (entering new JITDylib)" : "") << "\n"
2353            << "  Lookup set: " << IPLS->LookupSet << "\n"
2354            << "  Definition generator candidates: "
2355            << IPLS->DefGeneratorCandidates << "\n"
2356            << "  Definition generator non-candidates: "
2357            << IPLS->DefGeneratorNonCandidates << "\n";
2358   });
2359 
2360   // FIXME: We should attach the query as we go: This provides a result in a
2361   // single pass in the common case where all symbols have already reached the
2362   // required state. The query could be detached again in the 'fail' method on
2363   // IPLS. Phase 2 would be reduced to collecting and dispatching the MUs.
2364 
2365   while (IPLS->CurSearchOrderIndex != IPLS->SearchOrder.size()) {
2366 
2367     // If we've been handed an error or received one back from a generator then
2368     // fail the query. We don't need to unlink: At this stage the query hasn't
2369     // actually been lodged.
2370     if (Err)
2371       return IPLS->fail(std::move(Err));
2372 
2373     // Get the next JITDylib and lookup flags.
2374     auto &KV = IPLS->SearchOrder[IPLS->CurSearchOrderIndex];
2375     auto &JD = *KV.first;
2376     auto JDLookupFlags = KV.second;
2377 
2378     LLVM_DEBUG({
2379       dbgs() << "Visiting \"" << JD.getName() << "\" (" << JDLookupFlags
2380              << ") with lookup set " << IPLS->LookupSet << ":\n";
2381     });
2382 
2383     // If we've just reached a new JITDylib then perform some setup.
2384     if (IPLS->NewJITDylib) {
2385 
2386       // Acquire the generator lock for this JITDylib.
2387       IPLS->GeneratorLock = std::unique_lock<std::mutex>(JD.GeneratorsMutex);
2388 
2389       // Add any non-candidates from the last JITDylib (if any) back on to the
2390       // list of definition candidates for this JITDylib, reset definition
2391       // non-candiates to the empty set.
2392       SymbolLookupSet Tmp;
2393       std::swap(IPLS->DefGeneratorNonCandidates, Tmp);
2394       IPLS->DefGeneratorCandidates.append(std::move(Tmp));
2395 
2396       LLVM_DEBUG({
2397         dbgs() << "  First time visiting " << JD.getName()
2398                << ", resetting candidate sets and building generator stack\n";
2399       });
2400 
2401       // Build the definition generator stack for this JITDylib.
2402       runSessionLocked([&] {
2403         IPLS->CurDefGeneratorStack.reserve(JD.DefGenerators.size());
2404         for (auto &DG : reverse(JD.DefGenerators))
2405           IPLS->CurDefGeneratorStack.push_back(DG);
2406       });
2407 
2408       // Flag that we've done our initialization.
2409       IPLS->NewJITDylib = false;
2410     }
2411 
2412     // Remove any generation candidates that are already defined (and match) in
2413     // this JITDylib.
2414     runSessionLocked([&] {
2415       // Update the list of candidates (and non-candidates) for definition
2416       // generation.
2417       LLVM_DEBUG(dbgs() << "  Updating candidate set...\n");
2418       Err = IL_updateCandidatesFor(
2419           JD, JDLookupFlags, IPLS->DefGeneratorCandidates,
2420           JD.DefGenerators.empty() ? nullptr
2421                                    : &IPLS->DefGeneratorNonCandidates);
2422       LLVM_DEBUG({
2423         dbgs() << "    Remaining candidates = " << IPLS->DefGeneratorCandidates
2424                << "\n";
2425       });
2426     });
2427 
2428     // If we encountered an error while filtering generation candidates then
2429     // bail out.
2430     if (Err)
2431       return IPLS->fail(std::move(Err));
2432 
2433     /// Apply any definition generators on the stack.
2434     LLVM_DEBUG({
2435       if (IPLS->CurDefGeneratorStack.empty())
2436         LLVM_DEBUG(dbgs() << "  No generators to run for this JITDylib.\n");
2437       else if (IPLS->DefGeneratorCandidates.empty())
2438         LLVM_DEBUG(dbgs() << "  No candidates to generate.\n");
2439       else
2440         dbgs() << "  Running " << IPLS->CurDefGeneratorStack.size()
2441                << " remaining generators for "
2442                << IPLS->DefGeneratorCandidates.size() << " candidates\n";
2443     });
2444     while (!IPLS->CurDefGeneratorStack.empty() &&
2445            !IPLS->DefGeneratorCandidates.empty()) {
2446       auto DG = IPLS->CurDefGeneratorStack.back().lock();
2447       IPLS->CurDefGeneratorStack.pop_back();
2448 
2449       if (!DG)
2450         return IPLS->fail(make_error<StringError>(
2451             "DefinitionGenerator removed while lookup in progress",
2452             inconvertibleErrorCode()));
2453 
2454       auto K = IPLS->K;
2455       auto &LookupSet = IPLS->DefGeneratorCandidates;
2456 
2457       // Run the generator. If the generator takes ownership of QA then this
2458       // will break the loop.
2459       {
2460         LLVM_DEBUG(dbgs() << "  Attempting to generate " << LookupSet << "\n");
2461         LookupState LS(std::move(IPLS));
2462         Err = DG->tryToGenerate(LS, K, JD, JDLookupFlags, LookupSet);
2463         IPLS = std::move(LS.IPLS);
2464       }
2465 
2466       // If there was an error then fail the query.
2467       if (Err) {
2468         LLVM_DEBUG({
2469           dbgs() << "  Error attempting to generate " << LookupSet << "\n";
2470         });
2471         assert(IPLS && "LS cannot be retained if error is returned");
2472         return IPLS->fail(std::move(Err));
2473       }
2474 
2475       // Otherwise if QA was captured then break the loop.
2476       if (!IPLS) {
2477         LLVM_DEBUG(
2478             { dbgs() << "  LookupState captured. Exiting phase1 for now.\n"; });
2479         return;
2480       }
2481 
2482       // Otherwise if we're continuing around the loop then update candidates
2483       // for the next round.
2484       runSessionLocked([&] {
2485         LLVM_DEBUG(dbgs() << "  Updating candidate set post-generation\n");
2486         Err = IL_updateCandidatesFor(
2487             JD, JDLookupFlags, IPLS->DefGeneratorCandidates,
2488             JD.DefGenerators.empty() ? nullptr
2489                                      : &IPLS->DefGeneratorNonCandidates);
2490       });
2491 
2492       // If updating candidates failed then fail the query.
2493       if (Err) {
2494         LLVM_DEBUG(dbgs() << "  Error encountered while updating candidates\n");
2495         return IPLS->fail(std::move(Err));
2496       }
2497     }
2498 
2499     if (IPLS->DefGeneratorCandidates.empty() &&
2500         IPLS->DefGeneratorNonCandidates.empty()) {
2501       // Early out if there are no remaining symbols.
2502       LLVM_DEBUG(dbgs() << "All symbols matched.\n");
2503       IPLS->CurSearchOrderIndex = IPLS->SearchOrder.size();
2504       break;
2505     } else {
2506       // If we get here then we've moved on to the next JITDylib with candidates
2507       // remaining.
2508       LLVM_DEBUG(dbgs() << "Phase 1 moving to next JITDylib.\n");
2509       ++IPLS->CurSearchOrderIndex;
2510       IPLS->NewJITDylib = true;
2511     }
2512   }
2513 
2514   // Remove any weakly referenced candidates that could not be found/generated.
2515   IPLS->DefGeneratorCandidates.remove_if(
2516       [](const SymbolStringPtr &Name, SymbolLookupFlags SymLookupFlags) {
2517         return SymLookupFlags == SymbolLookupFlags::WeaklyReferencedSymbol;
2518       });
2519 
2520   // If we get here then we've finished searching all JITDylibs.
2521   // If we matched all symbols then move to phase 2, otherwise fail the query
2522   // with a SymbolsNotFound error.
2523   if (IPLS->DefGeneratorCandidates.empty()) {
2524     LLVM_DEBUG(dbgs() << "Phase 1 succeeded.\n");
2525     IPLS->complete(std::move(IPLS));
2526   } else {
2527     LLVM_DEBUG(dbgs() << "Phase 1 failed with unresolved symbols.\n");
2528     IPLS->fail(make_error<SymbolsNotFound>(
2529         getSymbolStringPool(), IPLS->DefGeneratorCandidates.getSymbolNames()));
2530   }
2531 }
2532 
2533 void ExecutionSession::OL_completeLookup(
2534     std::unique_ptr<InProgressLookupState> IPLS,
2535     std::shared_ptr<AsynchronousSymbolQuery> Q,
2536     RegisterDependenciesFunction RegisterDependencies) {
2537 
2538   LLVM_DEBUG({
2539     dbgs() << "Entering OL_completeLookup:\n"
2540            << "  Lookup kind: " << IPLS->K << "\n"
2541            << "  Search order: " << IPLS->SearchOrder
2542            << ", Current index = " << IPLS->CurSearchOrderIndex
2543            << (IPLS->NewJITDylib ? " (entering new JITDylib)" : "") << "\n"
2544            << "  Lookup set: " << IPLS->LookupSet << "\n"
2545            << "  Definition generator candidates: "
2546            << IPLS->DefGeneratorCandidates << "\n"
2547            << "  Definition generator non-candidates: "
2548            << IPLS->DefGeneratorNonCandidates << "\n";
2549   });
2550 
2551   bool QueryComplete = false;
2552   DenseMap<JITDylib *, JITDylib::UnmaterializedInfosList> CollectedUMIs;
2553 
2554   auto LodgingErr = runSessionLocked([&]() -> Error {
2555     for (auto &KV : IPLS->SearchOrder) {
2556       auto &JD = *KV.first;
2557       auto JDLookupFlags = KV.second;
2558       LLVM_DEBUG({
2559         dbgs() << "Visiting \"" << JD.getName() << "\" (" << JDLookupFlags
2560                << ") with lookup set " << IPLS->LookupSet << ":\n";
2561       });
2562 
2563       auto Err = IPLS->LookupSet.forEachWithRemoval(
2564           [&](const SymbolStringPtr &Name,
2565               SymbolLookupFlags SymLookupFlags) -> Expected<bool> {
2566             LLVM_DEBUG({
2567               dbgs() << "  Attempting to match \"" << Name << "\" ("
2568                      << SymLookupFlags << ")... ";
2569             });
2570 
2571             /// Search for the symbol. If not found then continue without
2572             /// removal.
2573             auto SymI = JD.Symbols.find(Name);
2574             if (SymI == JD.Symbols.end()) {
2575               LLVM_DEBUG(dbgs() << "skipping: not present\n");
2576               return false;
2577             }
2578 
2579             // If this is a non-exported symbol and we're matching exported
2580             // symbols only then skip this symbol without removal.
2581             if (!SymI->second.getFlags().isExported() &&
2582                 JDLookupFlags ==
2583                     JITDylibLookupFlags::MatchExportedSymbolsOnly) {
2584               LLVM_DEBUG(dbgs() << "skipping: not exported\n");
2585               return false;
2586             }
2587 
2588             // If we match against a materialization-side-effects only symbol
2589             // then make sure it is weakly-referenced. Otherwise bail out with
2590             // an error.
2591             // FIXME: Use a "materialization-side-effects-only symbols must be
2592             // weakly referenced" specific error here to reduce confusion.
2593             if (SymI->second.getFlags().hasMaterializationSideEffectsOnly() &&
2594                 SymLookupFlags != SymbolLookupFlags::WeaklyReferencedSymbol) {
2595               LLVM_DEBUG({
2596                 dbgs() << "error: "
2597                           "required, but symbol is has-side-effects-only\n";
2598               });
2599               return make_error<SymbolsNotFound>(getSymbolStringPool(),
2600                                                  SymbolNameVector({Name}));
2601             }
2602 
2603             // If we matched against this symbol but it is in the error state
2604             // then bail out and treat it as a failure to materialize.
2605             if (SymI->second.getFlags().hasError()) {
2606               LLVM_DEBUG(dbgs() << "error: symbol is in error state\n");
2607               auto FailedSymbolsMap = std::make_shared<SymbolDependenceMap>();
2608               (*FailedSymbolsMap)[&JD] = {Name};
2609               return make_error<FailedToMaterialize>(
2610                   std::move(FailedSymbolsMap));
2611             }
2612 
2613             // Otherwise this is a match.
2614 
2615             // If this symbol is already in the requried state then notify the
2616             // query, remove the symbol and continue.
2617             if (SymI->second.getState() >= Q->getRequiredState()) {
2618               LLVM_DEBUG(dbgs()
2619                          << "matched, symbol already in required state\n");
2620               Q->notifySymbolMetRequiredState(Name, SymI->second.getSymbol());
2621               return true;
2622             }
2623 
2624             // Otherwise this symbol does not yet meet the required state. Check
2625             // whether it has a materializer attached, and if so prepare to run
2626             // it.
2627             if (SymI->second.hasMaterializerAttached()) {
2628               assert(SymI->second.getAddress() == 0 &&
2629                      "Symbol not resolved but already has address?");
2630               auto UMII = JD.UnmaterializedInfos.find(Name);
2631               assert(UMII != JD.UnmaterializedInfos.end() &&
2632                      "Lazy symbol should have UnmaterializedInfo");
2633 
2634               auto UMI = UMII->second;
2635               assert(UMI->MU && "Materializer should not be null");
2636               assert(UMI->RT && "Tracker should not be null");
2637               LLVM_DEBUG({
2638                 dbgs() << "matched, preparing to dispatch MU@" << UMI->MU.get()
2639                        << " (" << UMI->MU->getName() << ")\n";
2640               });
2641 
2642               // Move all symbols associated with this MaterializationUnit into
2643               // materializing state.
2644               for (auto &KV : UMI->MU->getSymbols()) {
2645                 auto SymK = JD.Symbols.find(KV.first);
2646                 assert(SymK != JD.Symbols.end() &&
2647                        "No entry for symbol covered by MaterializationUnit");
2648                 SymK->second.setMaterializerAttached(false);
2649                 SymK->second.setState(SymbolState::Materializing);
2650                 JD.UnmaterializedInfos.erase(KV.first);
2651               }
2652 
2653               // Add MU to the list of MaterializationUnits to be materialized.
2654               CollectedUMIs[&JD].push_back(std::move(UMI));
2655             } else
2656               LLVM_DEBUG(dbgs() << "matched, registering query");
2657 
2658             // Add the query to the PendingQueries list and continue, deleting
2659             // the element from the lookup set.
2660             assert(SymI->second.getState() != SymbolState::NeverSearched &&
2661                    SymI->second.getState() != SymbolState::Ready &&
2662                    "By this line the symbol should be materializing");
2663             auto &MI = JD.MaterializingInfos[Name];
2664             MI.addQuery(Q);
2665             Q->addQueryDependence(JD, Name);
2666 
2667             return true;
2668           });
2669 
2670       // Handle failure.
2671       if (Err) {
2672 
2673         LLVM_DEBUG({
2674           dbgs() << "Lookup failed. Detaching query and replacing MUs.\n";
2675         });
2676 
2677         // Detach the query.
2678         Q->detach();
2679 
2680         // Replace the MUs.
2681         for (auto &KV : CollectedUMIs) {
2682           auto &JD = *KV.first;
2683           for (auto &UMI : KV.second)
2684             for (auto &KV2 : UMI->MU->getSymbols()) {
2685               assert(!JD.UnmaterializedInfos.count(KV2.first) &&
2686                      "Unexpected materializer in map");
2687               auto SymI = JD.Symbols.find(KV2.first);
2688               assert(SymI != JD.Symbols.end() && "Missing symbol entry");
2689               assert(SymI->second.getState() == SymbolState::Materializing &&
2690                      "Can not replace symbol that is not materializing");
2691               assert(!SymI->second.hasMaterializerAttached() &&
2692                      "MaterializerAttached flag should not be set");
2693               SymI->second.setMaterializerAttached(true);
2694               JD.UnmaterializedInfos[KV2.first] = UMI;
2695             }
2696         }
2697 
2698         return Err;
2699       }
2700     }
2701 
2702     LLVM_DEBUG(dbgs() << "Stripping unmatched weakly-referenced symbols\n");
2703     IPLS->LookupSet.forEachWithRemoval(
2704         [&](const SymbolStringPtr &Name, SymbolLookupFlags SymLookupFlags) {
2705           if (SymLookupFlags == SymbolLookupFlags::WeaklyReferencedSymbol) {
2706             Q->dropSymbol(Name);
2707             return true;
2708           } else
2709             return false;
2710         });
2711 
2712     if (!IPLS->LookupSet.empty()) {
2713       LLVM_DEBUG(dbgs() << "Failing due to unresolved symbols\n");
2714       return make_error<SymbolsNotFound>(getSymbolStringPool(),
2715                                          IPLS->LookupSet.getSymbolNames());
2716     }
2717 
2718     // Record whether the query completed.
2719     QueryComplete = Q->isComplete();
2720 
2721     LLVM_DEBUG({
2722       dbgs() << "Query successfully "
2723              << (QueryComplete ? "completed" : "lodged") << "\n";
2724     });
2725 
2726     // Move the collected MUs to the OutstandingMUs list.
2727     if (!CollectedUMIs.empty()) {
2728       std::lock_guard<std::recursive_mutex> Lock(OutstandingMUsMutex);
2729 
2730       LLVM_DEBUG(dbgs() << "Adding MUs to dispatch:\n");
2731       for (auto &KV : CollectedUMIs) {
2732         LLVM_DEBUG({
2733           auto &JD = *KV.first;
2734           dbgs() << "  For " << JD.getName() << ": Adding " << KV.second.size()
2735                  << " MUs.\n";
2736         });
2737         for (auto &UMI : KV.second) {
2738           auto MR = createMaterializationResponsibility(
2739               *UMI->RT, std::move(UMI->MU->SymbolFlags),
2740               std::move(UMI->MU->InitSymbol));
2741           OutstandingMUs.push_back(
2742               std::make_pair(std::move(UMI->MU), std::move(MR)));
2743         }
2744       }
2745     } else
2746       LLVM_DEBUG(dbgs() << "No MUs to dispatch.\n");
2747 
2748     if (RegisterDependencies && !Q->QueryRegistrations.empty()) {
2749       LLVM_DEBUG(dbgs() << "Registering dependencies\n");
2750       RegisterDependencies(Q->QueryRegistrations);
2751     } else
2752       LLVM_DEBUG(dbgs() << "No dependencies to register\n");
2753 
2754     return Error::success();
2755   });
2756 
2757   if (LodgingErr) {
2758     LLVM_DEBUG(dbgs() << "Failing query\n");
2759     Q->detach();
2760     Q->handleFailed(std::move(LodgingErr));
2761     return;
2762   }
2763 
2764   if (QueryComplete) {
2765     LLVM_DEBUG(dbgs() << "Completing query\n");
2766     Q->handleComplete(*this);
2767   }
2768 
2769   dispatchOutstandingMUs();
2770 }
2771 
2772 void ExecutionSession::OL_completeLookupFlags(
2773     std::unique_ptr<InProgressLookupState> IPLS,
2774     unique_function<void(Expected<SymbolFlagsMap>)> OnComplete) {
2775 
2776   auto Result = runSessionLocked([&]() -> Expected<SymbolFlagsMap> {
2777     LLVM_DEBUG({
2778       dbgs() << "Entering OL_completeLookupFlags:\n"
2779              << "  Lookup kind: " << IPLS->K << "\n"
2780              << "  Search order: " << IPLS->SearchOrder
2781              << ", Current index = " << IPLS->CurSearchOrderIndex
2782              << (IPLS->NewJITDylib ? " (entering new JITDylib)" : "") << "\n"
2783              << "  Lookup set: " << IPLS->LookupSet << "\n"
2784              << "  Definition generator candidates: "
2785              << IPLS->DefGeneratorCandidates << "\n"
2786              << "  Definition generator non-candidates: "
2787              << IPLS->DefGeneratorNonCandidates << "\n";
2788     });
2789 
2790     SymbolFlagsMap Result;
2791 
2792     // Attempt to find flags for each symbol.
2793     for (auto &KV : IPLS->SearchOrder) {
2794       auto &JD = *KV.first;
2795       auto JDLookupFlags = KV.second;
2796       LLVM_DEBUG({
2797         dbgs() << "Visiting \"" << JD.getName() << "\" (" << JDLookupFlags
2798                << ") with lookup set " << IPLS->LookupSet << ":\n";
2799       });
2800 
2801       IPLS->LookupSet.forEachWithRemoval([&](const SymbolStringPtr &Name,
2802                                              SymbolLookupFlags SymLookupFlags) {
2803         LLVM_DEBUG({
2804           dbgs() << "  Attempting to match \"" << Name << "\" ("
2805                  << SymLookupFlags << ")... ";
2806         });
2807 
2808         // Search for the symbol. If not found then continue without removing
2809         // from the lookup set.
2810         auto SymI = JD.Symbols.find(Name);
2811         if (SymI == JD.Symbols.end()) {
2812           LLVM_DEBUG(dbgs() << "skipping: not present\n");
2813           return false;
2814         }
2815 
2816         // If this is a non-exported symbol then it doesn't match. Skip it.
2817         if (!SymI->second.getFlags().isExported() &&
2818             JDLookupFlags == JITDylibLookupFlags::MatchExportedSymbolsOnly) {
2819           LLVM_DEBUG(dbgs() << "skipping: not exported\n");
2820           return false;
2821         }
2822 
2823         LLVM_DEBUG({
2824           dbgs() << "matched, \"" << Name << "\" -> " << SymI->second.getFlags()
2825                  << "\n";
2826         });
2827         Result[Name] = SymI->second.getFlags();
2828         return true;
2829       });
2830     }
2831 
2832     // Remove any weakly referenced symbols that haven't been resolved.
2833     IPLS->LookupSet.remove_if(
2834         [](const SymbolStringPtr &Name, SymbolLookupFlags SymLookupFlags) {
2835           return SymLookupFlags == SymbolLookupFlags::WeaklyReferencedSymbol;
2836         });
2837 
2838     if (!IPLS->LookupSet.empty()) {
2839       LLVM_DEBUG(dbgs() << "Failing due to unresolved symbols\n");
2840       return make_error<SymbolsNotFound>(getSymbolStringPool(),
2841                                          IPLS->LookupSet.getSymbolNames());
2842     }
2843 
2844     LLVM_DEBUG(dbgs() << "Succeded, result = " << Result << "\n");
2845     return Result;
2846   });
2847 
2848   // Run the callback on the result.
2849   LLVM_DEBUG(dbgs() << "Sending result to handler.\n");
2850   OnComplete(std::move(Result));
2851 }
2852 
2853 void ExecutionSession::OL_destroyMaterializationResponsibility(
2854     MaterializationResponsibility &MR) {
2855 
2856   assert(MR.SymbolFlags.empty() &&
2857          "All symbols should have been explicitly materialized or failed");
2858   MR.JD.unlinkMaterializationResponsibility(MR);
2859 }
2860 
2861 SymbolNameSet ExecutionSession::OL_getRequestedSymbols(
2862     const MaterializationResponsibility &MR) {
2863   return MR.JD.getRequestedSymbols(MR.SymbolFlags);
2864 }
2865 
2866 Error ExecutionSession::OL_notifyResolved(MaterializationResponsibility &MR,
2867                                           const SymbolMap &Symbols) {
2868   LLVM_DEBUG({
2869     dbgs() << "In " << MR.JD.getName() << " resolving " << Symbols << "\n";
2870   });
2871 #ifndef NDEBUG
2872   for (auto &KV : Symbols) {
2873     auto WeakFlags = JITSymbolFlags::Weak | JITSymbolFlags::Common;
2874     auto I = MR.SymbolFlags.find(KV.first);
2875     assert(I != MR.SymbolFlags.end() &&
2876            "Resolving symbol outside this responsibility set");
2877     assert(!I->second.hasMaterializationSideEffectsOnly() &&
2878            "Can't resolve materialization-side-effects-only symbol");
2879     assert((KV.second.getFlags() & ~WeakFlags) == (I->second & ~WeakFlags) &&
2880            "Resolving symbol with incorrect flags");
2881   }
2882 #endif
2883 
2884   return MR.JD.resolve(MR, Symbols);
2885 }
2886 
2887 Error ExecutionSession::OL_notifyEmitted(MaterializationResponsibility &MR) {
2888   LLVM_DEBUG({
2889     dbgs() << "In " << MR.JD.getName() << " emitting " << MR.SymbolFlags
2890            << "\n";
2891   });
2892 
2893   if (auto Err = MR.JD.emit(MR, MR.SymbolFlags))
2894     return Err;
2895 
2896   MR.SymbolFlags.clear();
2897   return Error::success();
2898 }
2899 
2900 Error ExecutionSession::OL_defineMaterializing(
2901     MaterializationResponsibility &MR, SymbolFlagsMap NewSymbolFlags) {
2902 
2903   LLVM_DEBUG({
2904     dbgs() << "In " << MR.JD.getName() << " defining materializing symbols "
2905            << NewSymbolFlags << "\n";
2906   });
2907   if (auto AcceptedDefs =
2908           MR.JD.defineMaterializing(std::move(NewSymbolFlags))) {
2909     // Add all newly accepted symbols to this responsibility object.
2910     for (auto &KV : *AcceptedDefs)
2911       MR.SymbolFlags.insert(KV);
2912     return Error::success();
2913   } else
2914     return AcceptedDefs.takeError();
2915 }
2916 
2917 void ExecutionSession::OL_notifyFailed(MaterializationResponsibility &MR) {
2918 
2919   LLVM_DEBUG({
2920     dbgs() << "In " << MR.JD.getName() << " failing materialization for "
2921            << MR.SymbolFlags << "\n";
2922   });
2923 
2924   JITDylib::FailedSymbolsWorklist Worklist;
2925 
2926   for (auto &KV : MR.SymbolFlags)
2927     Worklist.push_back(std::make_pair(&MR.JD, KV.first));
2928   MR.SymbolFlags.clear();
2929 
2930   if (Worklist.empty())
2931     return;
2932 
2933   JITDylib::AsynchronousSymbolQuerySet FailedQueries;
2934   std::shared_ptr<SymbolDependenceMap> FailedSymbols;
2935 
2936   runSessionLocked([&]() {
2937     // If the tracker is defunct then there's nothing to do here.
2938     if (MR.RT->isDefunct())
2939       return;
2940 
2941     std::tie(FailedQueries, FailedSymbols) =
2942         JITDylib::failSymbols(std::move(Worklist));
2943   });
2944 
2945   for (auto &Q : FailedQueries)
2946     Q->handleFailed(make_error<FailedToMaterialize>(FailedSymbols));
2947 }
2948 
2949 Error ExecutionSession::OL_replace(MaterializationResponsibility &MR,
2950                                    std::unique_ptr<MaterializationUnit> MU) {
2951   for (auto &KV : MU->getSymbols()) {
2952     assert(MR.SymbolFlags.count(KV.first) &&
2953            "Replacing definition outside this responsibility set");
2954     MR.SymbolFlags.erase(KV.first);
2955   }
2956 
2957   if (MU->getInitializerSymbol() == MR.InitSymbol)
2958     MR.InitSymbol = nullptr;
2959 
2960   LLVM_DEBUG(MR.JD.getExecutionSession().runSessionLocked([&]() {
2961     dbgs() << "In " << MR.JD.getName() << " replacing symbols with " << *MU
2962            << "\n";
2963   }););
2964 
2965   return MR.JD.replace(MR, std::move(MU));
2966 }
2967 
2968 Expected<std::unique_ptr<MaterializationResponsibility>>
2969 ExecutionSession::OL_delegate(MaterializationResponsibility &MR,
2970                               const SymbolNameSet &Symbols) {
2971 
2972   SymbolStringPtr DelegatedInitSymbol;
2973   SymbolFlagsMap DelegatedFlags;
2974 
2975   for (auto &Name : Symbols) {
2976     auto I = MR.SymbolFlags.find(Name);
2977     assert(I != MR.SymbolFlags.end() &&
2978            "Symbol is not tracked by this MaterializationResponsibility "
2979            "instance");
2980 
2981     DelegatedFlags[Name] = std::move(I->second);
2982     if (Name == MR.InitSymbol)
2983       std::swap(MR.InitSymbol, DelegatedInitSymbol);
2984 
2985     MR.SymbolFlags.erase(I);
2986   }
2987 
2988   return MR.JD.delegate(MR, std::move(DelegatedFlags),
2989                         std::move(DelegatedInitSymbol));
2990 }
2991 
2992 void ExecutionSession::OL_addDependencies(
2993     MaterializationResponsibility &MR, const SymbolStringPtr &Name,
2994     const SymbolDependenceMap &Dependencies) {
2995   LLVM_DEBUG({
2996     dbgs() << "Adding dependencies for " << Name << ": " << Dependencies
2997            << "\n";
2998   });
2999   assert(MR.SymbolFlags.count(Name) &&
3000          "Symbol not covered by this MaterializationResponsibility instance");
3001   MR.JD.addDependencies(Name, Dependencies);
3002 }
3003 
3004 void ExecutionSession::OL_addDependenciesForAll(
3005     MaterializationResponsibility &MR,
3006     const SymbolDependenceMap &Dependencies) {
3007   LLVM_DEBUG({
3008     dbgs() << "Adding dependencies for all symbols in " << MR.SymbolFlags << ": "
3009            << Dependencies << "\n";
3010   });
3011   for (auto &KV : MR.SymbolFlags)
3012     MR.JD.addDependencies(KV.first, Dependencies);
3013 }
3014 
3015 #ifndef NDEBUG
3016 void ExecutionSession::dumpDispatchInfo(Task &T) {
3017   runSessionLocked([&]() {
3018     dbgs() << "Dispatching: ";
3019     T.printDescription(dbgs());
3020     dbgs() << "\n";
3021   });
3022 }
3023 #endif // NDEBUG
3024 
3025 } // End namespace orc.
3026 } // End namespace llvm.
3027