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