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