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