1 //===- FunctionImport.cpp - ThinLTO Summary-based Function Import ---------===// 2 // 3 // The LLVM Compiler Infrastructure 4 // 5 // This file is distributed under the University of Illinois Open Source 6 // License. See LICENSE.TXT for details. 7 // 8 //===----------------------------------------------------------------------===// 9 // 10 // This file implements Function import based on summaries. 11 // 12 //===----------------------------------------------------------------------===// 13 14 #include "llvm/Transforms/IPO/FunctionImport.h" 15 #include "llvm/ADT/ArrayRef.h" 16 #include "llvm/ADT/STLExtras.h" 17 #include "llvm/ADT/SetVector.h" 18 #include "llvm/ADT/SmallVector.h" 19 #include "llvm/ADT/Statistic.h" 20 #include "llvm/ADT/StringMap.h" 21 #include "llvm/ADT/StringRef.h" 22 #include "llvm/ADT/StringSet.h" 23 #include "llvm/Bitcode/BitcodeReader.h" 24 #include "llvm/IR/AutoUpgrade.h" 25 #include "llvm/IR/Constants.h" 26 #include "llvm/IR/Function.h" 27 #include "llvm/IR/GlobalAlias.h" 28 #include "llvm/IR/GlobalObject.h" 29 #include "llvm/IR/GlobalValue.h" 30 #include "llvm/IR/GlobalVariable.h" 31 #include "llvm/IR/Metadata.h" 32 #include "llvm/IR/Module.h" 33 #include "llvm/IR/ModuleSummaryIndex.h" 34 #include "llvm/IRReader/IRReader.h" 35 #include "llvm/Linker/IRMover.h" 36 #include "llvm/Object/ModuleSymbolTable.h" 37 #include "llvm/Object/SymbolicFile.h" 38 #include "llvm/Pass.h" 39 #include "llvm/Support/Casting.h" 40 #include "llvm/Support/CommandLine.h" 41 #include "llvm/Support/Debug.h" 42 #include "llvm/Support/Error.h" 43 #include "llvm/Support/ErrorHandling.h" 44 #include "llvm/Support/FileSystem.h" 45 #include "llvm/Support/SourceMgr.h" 46 #include "llvm/Support/raw_ostream.h" 47 #include "llvm/Transforms/IPO/Internalize.h" 48 #include "llvm/Transforms/Utils/Cloning.h" 49 #include "llvm/Transforms/Utils/FunctionImportUtils.h" 50 #include "llvm/Transforms/Utils/ValueMapper.h" 51 #include <cassert> 52 #include <memory> 53 #include <set> 54 #include <string> 55 #include <system_error> 56 #include <tuple> 57 #include <utility> 58 59 using namespace llvm; 60 61 #define DEBUG_TYPE "function-import" 62 63 STATISTIC(NumImportedFunctions, "Number of functions imported"); 64 STATISTIC(NumImportedGlobalVars, "Number of global variables imported"); 65 STATISTIC(NumImportedModules, "Number of modules imported from"); 66 STATISTIC(NumDeadSymbols, "Number of dead stripped symbols in index"); 67 STATISTIC(NumLiveSymbols, "Number of live symbols in index"); 68 69 /// Limit on instruction count of imported functions. 70 static cl::opt<unsigned> ImportInstrLimit( 71 "import-instr-limit", cl::init(100), cl::Hidden, cl::value_desc("N"), 72 cl::desc("Only import functions with less than N instructions")); 73 74 static cl::opt<int> ImportCutoff( 75 "import-cutoff", cl::init(-1), cl::Hidden, cl::value_desc("N"), 76 cl::desc("Only import first N functions if N>=0 (default -1)")); 77 78 static cl::opt<float> 79 ImportInstrFactor("import-instr-evolution-factor", cl::init(0.7), 80 cl::Hidden, cl::value_desc("x"), 81 cl::desc("As we import functions, multiply the " 82 "`import-instr-limit` threshold by this factor " 83 "before processing newly imported functions")); 84 85 static cl::opt<float> ImportHotInstrFactor( 86 "import-hot-evolution-factor", cl::init(1.0), cl::Hidden, 87 cl::value_desc("x"), 88 cl::desc("As we import functions called from hot callsite, multiply the " 89 "`import-instr-limit` threshold by this factor " 90 "before processing newly imported functions")); 91 92 static cl::opt<float> ImportHotMultiplier( 93 "import-hot-multiplier", cl::init(10.0), cl::Hidden, cl::value_desc("x"), 94 cl::desc("Multiply the `import-instr-limit` threshold for hot callsites")); 95 96 static cl::opt<float> ImportCriticalMultiplier( 97 "import-critical-multiplier", cl::init(100.0), cl::Hidden, 98 cl::value_desc("x"), 99 cl::desc( 100 "Multiply the `import-instr-limit` threshold for critical callsites")); 101 102 // FIXME: This multiplier was not really tuned up. 103 static cl::opt<float> ImportColdMultiplier( 104 "import-cold-multiplier", cl::init(0), cl::Hidden, cl::value_desc("N"), 105 cl::desc("Multiply the `import-instr-limit` threshold for cold callsites")); 106 107 static cl::opt<bool> PrintImports("print-imports", cl::init(false), cl::Hidden, 108 cl::desc("Print imported functions")); 109 110 static cl::opt<bool> PrintImportFailures( 111 "print-import-failures", cl::init(false), cl::Hidden, 112 cl::desc("Print information for functions rejected for importing")); 113 114 static cl::opt<bool> ComputeDead("compute-dead", cl::init(true), cl::Hidden, 115 cl::desc("Compute dead symbols")); 116 117 static cl::opt<bool> EnableImportMetadata( 118 "enable-import-metadata", cl::init( 119 #if !defined(NDEBUG) 120 true /*Enabled with asserts.*/ 121 #else 122 false 123 #endif 124 ), 125 cl::Hidden, cl::desc("Enable import metadata like 'thinlto_src_module'")); 126 127 /// Summary file to use for function importing when using -function-import from 128 /// the command line. 129 static cl::opt<std::string> 130 SummaryFile("summary-file", 131 cl::desc("The summary file to use for function importing.")); 132 133 /// Used when testing importing from distributed indexes via opt 134 // -function-import. 135 static cl::opt<bool> 136 ImportAllIndex("import-all-index", 137 cl::desc("Import all external functions in index.")); 138 139 // Load lazily a module from \p FileName in \p Context. 140 static std::unique_ptr<Module> loadFile(const std::string &FileName, 141 LLVMContext &Context) { 142 SMDiagnostic Err; 143 LLVM_DEBUG(dbgs() << "Loading '" << FileName << "'\n"); 144 // Metadata isn't loaded until functions are imported, to minimize 145 // the memory overhead. 146 std::unique_ptr<Module> Result = 147 getLazyIRFileModule(FileName, Err, Context, 148 /* ShouldLazyLoadMetadata = */ true); 149 if (!Result) { 150 Err.print("function-import", errs()); 151 report_fatal_error("Abort"); 152 } 153 154 return Result; 155 } 156 157 /// Given a list of possible callee implementation for a call site, select one 158 /// that fits the \p Threshold. 159 /// 160 /// FIXME: select "best" instead of first that fits. But what is "best"? 161 /// - The smallest: more likely to be inlined. 162 /// - The one with the least outgoing edges (already well optimized). 163 /// - One from a module already being imported from in order to reduce the 164 /// number of source modules parsed/linked. 165 /// - One that has PGO data attached. 166 /// - [insert you fancy metric here] 167 static const GlobalValueSummary * 168 selectCallee(const ModuleSummaryIndex &Index, 169 ArrayRef<std::unique_ptr<GlobalValueSummary>> CalleeSummaryList, 170 unsigned Threshold, StringRef CallerModulePath, 171 FunctionImporter::ImportFailureReason &Reason, 172 GlobalValue::GUID GUID) { 173 Reason = FunctionImporter::ImportFailureReason::None; 174 auto It = llvm::find_if( 175 CalleeSummaryList, 176 [&](const std::unique_ptr<GlobalValueSummary> &SummaryPtr) { 177 auto *GVSummary = SummaryPtr.get(); 178 if (!Index.isGlobalValueLive(GVSummary)) { 179 Reason = FunctionImporter::ImportFailureReason::NotLive; 180 return false; 181 } 182 183 // For SamplePGO, in computeImportForFunction the OriginalId 184 // may have been used to locate the callee summary list (See 185 // comment there). 186 // The mapping from OriginalId to GUID may return a GUID 187 // that corresponds to a static variable. Filter it out here. 188 // This can happen when 189 // 1) There is a call to a library function which is not defined 190 // in the index. 191 // 2) There is a static variable with the OriginalGUID identical 192 // to the GUID of the library function in 1); 193 // When this happens, the logic for SamplePGO kicks in and 194 // the static variable in 2) will be found, which needs to be 195 // filtered out. 196 if (GVSummary->getSummaryKind() == GlobalValueSummary::GlobalVarKind) { 197 Reason = FunctionImporter::ImportFailureReason::GlobalVar; 198 return false; 199 } 200 if (GlobalValue::isInterposableLinkage(GVSummary->linkage())) { 201 Reason = FunctionImporter::ImportFailureReason::InterposableLinkage; 202 // There is no point in importing these, we can't inline them 203 return false; 204 } 205 206 auto *Summary = cast<FunctionSummary>(GVSummary->getBaseObject()); 207 208 // If this is a local function, make sure we import the copy 209 // in the caller's module. The only time a local function can 210 // share an entry in the index is if there is a local with the same name 211 // in another module that had the same source file name (in a different 212 // directory), where each was compiled in their own directory so there 213 // was not distinguishing path. 214 // However, do the import from another module if there is only one 215 // entry in the list - in that case this must be a reference due 216 // to indirect call profile data, since a function pointer can point to 217 // a local in another module. 218 if (GlobalValue::isLocalLinkage(Summary->linkage()) && 219 CalleeSummaryList.size() > 1 && 220 Summary->modulePath() != CallerModulePath) { 221 Reason = 222 FunctionImporter::ImportFailureReason::LocalLinkageNotInModule; 223 return false; 224 } 225 226 if (Summary->instCount() > Threshold) { 227 Reason = FunctionImporter::ImportFailureReason::TooLarge; 228 return false; 229 } 230 231 if (Summary->notEligibleToImport()) { 232 Reason = FunctionImporter::ImportFailureReason::NotEligible; 233 return false; 234 } 235 236 return true; 237 }); 238 if (It == CalleeSummaryList.end()) 239 return nullptr; 240 241 return cast<GlobalValueSummary>(It->get()); 242 } 243 244 namespace { 245 246 using EdgeInfo = std::tuple<const FunctionSummary *, unsigned /* Threshold */, 247 GlobalValue::GUID>; 248 249 } // anonymous namespace 250 251 static ValueInfo 252 updateValueInfoForIndirectCalls(const ModuleSummaryIndex &Index, ValueInfo VI) { 253 if (!VI.getSummaryList().empty()) 254 return VI; 255 // For SamplePGO, the indirect call targets for local functions will 256 // have its original name annotated in profile. We try to find the 257 // corresponding PGOFuncName as the GUID. 258 // FIXME: Consider updating the edges in the graph after building 259 // it, rather than needing to perform this mapping on each walk. 260 auto GUID = Index.getGUIDFromOriginalID(VI.getGUID()); 261 if (GUID == 0) 262 return ValueInfo(); 263 return Index.getValueInfo(GUID); 264 } 265 266 static void computeImportForReferencedGlobals( 267 const FunctionSummary &Summary, const GVSummaryMapTy &DefinedGVSummaries, 268 FunctionImporter::ImportMapTy &ImportList, 269 StringMap<FunctionImporter::ExportSetTy> *ExportLists) { 270 for (auto &VI : Summary.refs()) { 271 if (DefinedGVSummaries.count(VI.getGUID())) { 272 LLVM_DEBUG( 273 dbgs() << "Ref ignored! Target already in destination module.\n"); 274 continue; 275 } 276 277 LLVM_DEBUG(dbgs() << " ref -> " << VI << "\n"); 278 279 for (auto &RefSummary : VI.getSummaryList()) 280 if (RefSummary->getSummaryKind() == GlobalValueSummary::GlobalVarKind && 281 !RefSummary->notEligibleToImport() && 282 !GlobalValue::isInterposableLinkage(RefSummary->linkage()) && 283 RefSummary->refs().empty()) { 284 ImportList[RefSummary->modulePath()].insert(VI.getGUID()); 285 if (ExportLists) 286 (*ExportLists)[RefSummary->modulePath()].insert(VI.getGUID()); 287 break; 288 } 289 } 290 } 291 292 static const char * 293 getFailureName(FunctionImporter::ImportFailureReason Reason) { 294 switch (Reason) { 295 case FunctionImporter::ImportFailureReason::None: 296 return "None"; 297 case FunctionImporter::ImportFailureReason::GlobalVar: 298 return "GlobalVar"; 299 case FunctionImporter::ImportFailureReason::NotLive: 300 return "NotLive"; 301 case FunctionImporter::ImportFailureReason::TooLarge: 302 return "TooLarge"; 303 case FunctionImporter::ImportFailureReason::InterposableLinkage: 304 return "InterposableLinkage"; 305 case FunctionImporter::ImportFailureReason::LocalLinkageNotInModule: 306 return "LocalLinkageNotInModule"; 307 case FunctionImporter::ImportFailureReason::NotEligible: 308 return "NotEligible"; 309 } 310 llvm_unreachable("invalid reason"); 311 } 312 313 /// Compute the list of functions to import for a given caller. Mark these 314 /// imported functions and the symbols they reference in their source module as 315 /// exported from their source module. 316 static void computeImportForFunction( 317 const FunctionSummary &Summary, const ModuleSummaryIndex &Index, 318 const unsigned Threshold, const GVSummaryMapTy &DefinedGVSummaries, 319 SmallVectorImpl<EdgeInfo> &Worklist, 320 FunctionImporter::ImportMapTy &ImportList, 321 StringMap<FunctionImporter::ExportSetTy> *ExportLists, 322 FunctionImporter::ImportThresholdsTy &ImportThresholds) { 323 computeImportForReferencedGlobals(Summary, DefinedGVSummaries, ImportList, 324 ExportLists); 325 static int ImportCount = 0; 326 for (auto &Edge : Summary.calls()) { 327 ValueInfo VI = Edge.first; 328 LLVM_DEBUG(dbgs() << " edge -> " << VI << " Threshold:" << Threshold 329 << "\n"); 330 331 if (ImportCutoff >= 0 && ImportCount >= ImportCutoff) { 332 LLVM_DEBUG(dbgs() << "ignored! import-cutoff value of " << ImportCutoff 333 << " reached.\n"); 334 continue; 335 } 336 337 VI = updateValueInfoForIndirectCalls(Index, VI); 338 if (!VI) 339 continue; 340 341 if (DefinedGVSummaries.count(VI.getGUID())) { 342 LLVM_DEBUG(dbgs() << "ignored! Target already in destination module.\n"); 343 continue; 344 } 345 346 auto GetBonusMultiplier = [](CalleeInfo::HotnessType Hotness) -> float { 347 if (Hotness == CalleeInfo::HotnessType::Hot) 348 return ImportHotMultiplier; 349 if (Hotness == CalleeInfo::HotnessType::Cold) 350 return ImportColdMultiplier; 351 if (Hotness == CalleeInfo::HotnessType::Critical) 352 return ImportCriticalMultiplier; 353 return 1.0; 354 }; 355 356 const auto NewThreshold = 357 Threshold * GetBonusMultiplier(Edge.second.getHotness()); 358 359 auto IT = ImportThresholds.insert(std::make_pair( 360 VI.getGUID(), std::make_tuple(NewThreshold, nullptr, nullptr))); 361 bool PreviouslyVisited = !IT.second; 362 auto &ProcessedThreshold = std::get<0>(IT.first->second); 363 auto &CalleeSummary = std::get<1>(IT.first->second); 364 auto &FailureInfo = std::get<2>(IT.first->second); 365 366 const FunctionSummary *ResolvedCalleeSummary = nullptr; 367 if (CalleeSummary) { 368 assert(PreviouslyVisited); 369 // Since the traversal of the call graph is DFS, we can revisit a function 370 // a second time with a higher threshold. In this case, it is added back 371 // to the worklist with the new threshold (so that its own callee chains 372 // can be considered with the higher threshold). 373 if (NewThreshold <= ProcessedThreshold) { 374 LLVM_DEBUG( 375 dbgs() << "ignored! Target was already imported with Threshold " 376 << ProcessedThreshold << "\n"); 377 continue; 378 } 379 // Update with new larger threshold. 380 ProcessedThreshold = NewThreshold; 381 ResolvedCalleeSummary = cast<FunctionSummary>(CalleeSummary); 382 } else { 383 // If we already rejected importing a callee at the same or higher 384 // threshold, don't waste time calling selectCallee. 385 if (PreviouslyVisited && NewThreshold <= ProcessedThreshold) { 386 LLVM_DEBUG( 387 dbgs() << "ignored! Target was already rejected with Threshold " 388 << ProcessedThreshold << "\n"); 389 if (PrintImportFailures) { 390 assert(FailureInfo && 391 "Expected FailureInfo for previously rejected candidate"); 392 FailureInfo->Attempts++; 393 } 394 continue; 395 } 396 397 FunctionImporter::ImportFailureReason Reason; 398 CalleeSummary = selectCallee(Index, VI.getSummaryList(), NewThreshold, 399 Summary.modulePath(), Reason, VI.getGUID()); 400 if (!CalleeSummary) { 401 // Update with new larger threshold if this was a retry (otherwise 402 // we would have already inserted with NewThreshold above). Also 403 // update failure info if requested. 404 if (PreviouslyVisited) { 405 ProcessedThreshold = NewThreshold; 406 if (PrintImportFailures) { 407 assert(FailureInfo && 408 "Expected FailureInfo for previously rejected candidate"); 409 FailureInfo->Reason = Reason; 410 FailureInfo->Attempts++; 411 FailureInfo->MaxHotness = 412 std::max(FailureInfo->MaxHotness, Edge.second.getHotness()); 413 } 414 } else if (PrintImportFailures) { 415 assert(!FailureInfo && 416 "Expected no FailureInfo for newly rejected candidate"); 417 FailureInfo = llvm::make_unique<FunctionImporter::ImportFailureInfo>( 418 VI, Edge.second.getHotness(), Reason, 1); 419 } 420 LLVM_DEBUG( 421 dbgs() << "ignored! No qualifying callee with summary found.\n"); 422 continue; 423 } 424 425 // "Resolve" the summary 426 CalleeSummary = CalleeSummary->getBaseObject(); 427 ResolvedCalleeSummary = cast<FunctionSummary>(CalleeSummary); 428 429 assert(ResolvedCalleeSummary->instCount() <= NewThreshold && 430 "selectCallee() didn't honor the threshold"); 431 432 auto ExportModulePath = ResolvedCalleeSummary->modulePath(); 433 auto ILI = ImportList[ExportModulePath].insert(VI.getGUID()); 434 // We previously decided to import this GUID definition if it was already 435 // inserted in the set of imports from the exporting module. 436 bool PreviouslyImported = !ILI.second; 437 438 // Make exports in the source module. 439 if (ExportLists) { 440 auto &ExportList = (*ExportLists)[ExportModulePath]; 441 ExportList.insert(VI.getGUID()); 442 if (!PreviouslyImported) { 443 // This is the first time this function was exported from its source 444 // module, so mark all functions and globals it references as exported 445 // to the outside if they are defined in the same source module. 446 // For efficiency, we unconditionally add all the referenced GUIDs 447 // to the ExportList for this module, and will prune out any not 448 // defined in the module later in a single pass. 449 for (auto &Edge : ResolvedCalleeSummary->calls()) { 450 auto CalleeGUID = Edge.first.getGUID(); 451 ExportList.insert(CalleeGUID); 452 } 453 for (auto &Ref : ResolvedCalleeSummary->refs()) { 454 auto GUID = Ref.getGUID(); 455 ExportList.insert(GUID); 456 } 457 } 458 } 459 } 460 461 auto GetAdjustedThreshold = [](unsigned Threshold, bool IsHotCallsite) { 462 // Adjust the threshold for next level of imported functions. 463 // The threshold is different for hot callsites because we can then 464 // inline chains of hot calls. 465 if (IsHotCallsite) 466 return Threshold * ImportHotInstrFactor; 467 return Threshold * ImportInstrFactor; 468 }; 469 470 bool IsHotCallsite = 471 Edge.second.getHotness() == CalleeInfo::HotnessType::Hot; 472 const auto AdjThreshold = GetAdjustedThreshold(Threshold, IsHotCallsite); 473 474 ImportCount++; 475 476 // Insert the newly imported function to the worklist. 477 Worklist.emplace_back(ResolvedCalleeSummary, AdjThreshold, VI.getGUID()); 478 } 479 } 480 481 /// Given the list of globals defined in a module, compute the list of imports 482 /// as well as the list of "exports", i.e. the list of symbols referenced from 483 /// another module (that may require promotion). 484 static void ComputeImportForModule( 485 const GVSummaryMapTy &DefinedGVSummaries, const ModuleSummaryIndex &Index, 486 StringRef ModName, FunctionImporter::ImportMapTy &ImportList, 487 StringMap<FunctionImporter::ExportSetTy> *ExportLists = nullptr) { 488 // Worklist contains the list of function imported in this module, for which 489 // we will analyse the callees and may import further down the callgraph. 490 SmallVector<EdgeInfo, 128> Worklist; 491 FunctionImporter::ImportThresholdsTy ImportThresholds; 492 493 // Populate the worklist with the import for the functions in the current 494 // module 495 for (auto &GVSummary : DefinedGVSummaries) { 496 #ifndef NDEBUG 497 // FIXME: Change the GVSummaryMapTy to hold ValueInfo instead of GUID 498 // so this map look up (and possibly others) can be avoided. 499 auto VI = Index.getValueInfo(GVSummary.first); 500 #endif 501 if (!Index.isGlobalValueLive(GVSummary.second)) { 502 LLVM_DEBUG(dbgs() << "Ignores Dead GUID: " << VI << "\n"); 503 continue; 504 } 505 auto *FuncSummary = 506 dyn_cast<FunctionSummary>(GVSummary.second->getBaseObject()); 507 if (!FuncSummary) 508 // Skip import for global variables 509 continue; 510 LLVM_DEBUG(dbgs() << "Initialize import for " << VI << "\n"); 511 computeImportForFunction(*FuncSummary, Index, ImportInstrLimit, 512 DefinedGVSummaries, Worklist, ImportList, 513 ExportLists, ImportThresholds); 514 } 515 516 // Process the newly imported functions and add callees to the worklist. 517 while (!Worklist.empty()) { 518 auto FuncInfo = Worklist.pop_back_val(); 519 auto *Summary = std::get<0>(FuncInfo); 520 auto Threshold = std::get<1>(FuncInfo); 521 522 computeImportForFunction(*Summary, Index, Threshold, DefinedGVSummaries, 523 Worklist, ImportList, ExportLists, 524 ImportThresholds); 525 } 526 527 // Print stats about functions considered but rejected for importing 528 // when requested. 529 if (PrintImportFailures) { 530 dbgs() << "Missed imports into module " << ModName << "\n"; 531 for (auto &I : ImportThresholds) { 532 auto &ProcessedThreshold = std::get<0>(I.second); 533 auto &CalleeSummary = std::get<1>(I.second); 534 auto &FailureInfo = std::get<2>(I.second); 535 if (CalleeSummary) 536 continue; // We are going to import. 537 assert(FailureInfo); 538 FunctionSummary *FS = nullptr; 539 if (!FailureInfo->VI.getSummaryList().empty()) 540 FS = dyn_cast<FunctionSummary>( 541 FailureInfo->VI.getSummaryList()[0]->getBaseObject()); 542 dbgs() << FailureInfo->VI 543 << ": Reason = " << getFailureName(FailureInfo->Reason) 544 << ", Threshold = " << ProcessedThreshold 545 << ", Size = " << (FS ? (int)FS->instCount() : -1) 546 << ", MaxHotness = " << getHotnessName(FailureInfo->MaxHotness) 547 << ", Attempts = " << FailureInfo->Attempts << "\n"; 548 } 549 } 550 } 551 552 #ifndef NDEBUG 553 static bool isGlobalVarSummary(const ModuleSummaryIndex &Index, 554 GlobalValue::GUID G) { 555 if (const auto &VI = Index.getValueInfo(G)) { 556 auto SL = VI.getSummaryList(); 557 if (!SL.empty()) 558 return SL[0]->getSummaryKind() == GlobalValueSummary::GlobalVarKind; 559 } 560 return false; 561 } 562 563 static GlobalValue::GUID getGUID(GlobalValue::GUID G) { return G; } 564 565 template <class T> 566 static unsigned numGlobalVarSummaries(const ModuleSummaryIndex &Index, 567 T &Cont) { 568 unsigned NumGVS = 0; 569 for (auto &V : Cont) 570 if (isGlobalVarSummary(Index, getGUID(V))) 571 ++NumGVS; 572 return NumGVS; 573 } 574 #endif 575 576 /// Compute all the import and export for every module using the Index. 577 void llvm::ComputeCrossModuleImport( 578 const ModuleSummaryIndex &Index, 579 const StringMap<GVSummaryMapTy> &ModuleToDefinedGVSummaries, 580 StringMap<FunctionImporter::ImportMapTy> &ImportLists, 581 StringMap<FunctionImporter::ExportSetTy> &ExportLists) { 582 // For each module that has function defined, compute the import/export lists. 583 for (auto &DefinedGVSummaries : ModuleToDefinedGVSummaries) { 584 auto &ImportList = ImportLists[DefinedGVSummaries.first()]; 585 LLVM_DEBUG(dbgs() << "Computing import for Module '" 586 << DefinedGVSummaries.first() << "'\n"); 587 ComputeImportForModule(DefinedGVSummaries.second, Index, 588 DefinedGVSummaries.first(), ImportList, 589 &ExportLists); 590 } 591 592 // When computing imports we added all GUIDs referenced by anything 593 // imported from the module to its ExportList. Now we prune each ExportList 594 // of any not defined in that module. This is more efficient than checking 595 // while computing imports because some of the summary lists may be long 596 // due to linkonce (comdat) copies. 597 for (auto &ELI : ExportLists) { 598 const auto &DefinedGVSummaries = 599 ModuleToDefinedGVSummaries.lookup(ELI.first()); 600 for (auto EI = ELI.second.begin(); EI != ELI.second.end();) { 601 if (!DefinedGVSummaries.count(*EI)) 602 EI = ELI.second.erase(EI); 603 else 604 ++EI; 605 } 606 } 607 608 #ifndef NDEBUG 609 LLVM_DEBUG(dbgs() << "Import/Export lists for " << ImportLists.size() 610 << " modules:\n"); 611 for (auto &ModuleImports : ImportLists) { 612 auto ModName = ModuleImports.first(); 613 auto &Exports = ExportLists[ModName]; 614 unsigned NumGVS = numGlobalVarSummaries(Index, Exports); 615 LLVM_DEBUG(dbgs() << "* Module " << ModName << " exports " 616 << Exports.size() - NumGVS << " functions and " << NumGVS 617 << " vars. Imports from " << ModuleImports.second.size() 618 << " modules.\n"); 619 for (auto &Src : ModuleImports.second) { 620 auto SrcModName = Src.first(); 621 unsigned NumGVSPerMod = numGlobalVarSummaries(Index, Src.second); 622 LLVM_DEBUG(dbgs() << " - " << Src.second.size() - NumGVSPerMod 623 << " functions imported from " << SrcModName << "\n"); 624 LLVM_DEBUG(dbgs() << " - " << NumGVSPerMod 625 << " global vars imported from " << SrcModName << "\n"); 626 } 627 } 628 #endif 629 } 630 631 #ifndef NDEBUG 632 static void dumpImportListForModule(const ModuleSummaryIndex &Index, 633 StringRef ModulePath, 634 FunctionImporter::ImportMapTy &ImportList) { 635 LLVM_DEBUG(dbgs() << "* Module " << ModulePath << " imports from " 636 << ImportList.size() << " modules.\n"); 637 for (auto &Src : ImportList) { 638 auto SrcModName = Src.first(); 639 unsigned NumGVSPerMod = numGlobalVarSummaries(Index, Src.second); 640 LLVM_DEBUG(dbgs() << " - " << Src.second.size() - NumGVSPerMod 641 << " functions imported from " << SrcModName << "\n"); 642 LLVM_DEBUG(dbgs() << " - " << NumGVSPerMod << " vars imported from " 643 << SrcModName << "\n"); 644 } 645 } 646 #endif 647 648 /// Compute all the imports for the given module in the Index. 649 void llvm::ComputeCrossModuleImportForModule( 650 StringRef ModulePath, const ModuleSummaryIndex &Index, 651 FunctionImporter::ImportMapTy &ImportList) { 652 // Collect the list of functions this module defines. 653 // GUID -> Summary 654 GVSummaryMapTy FunctionSummaryMap; 655 Index.collectDefinedFunctionsForModule(ModulePath, FunctionSummaryMap); 656 657 // Compute the import list for this module. 658 LLVM_DEBUG(dbgs() << "Computing import for Module '" << ModulePath << "'\n"); 659 ComputeImportForModule(FunctionSummaryMap, Index, ModulePath, ImportList); 660 661 #ifndef NDEBUG 662 dumpImportListForModule(Index, ModulePath, ImportList); 663 #endif 664 } 665 666 // Mark all external summaries in Index for import into the given module. 667 // Used for distributed builds using a distributed index. 668 void llvm::ComputeCrossModuleImportForModuleFromIndex( 669 StringRef ModulePath, const ModuleSummaryIndex &Index, 670 FunctionImporter::ImportMapTy &ImportList) { 671 for (auto &GlobalList : Index) { 672 // Ignore entries for undefined references. 673 if (GlobalList.second.SummaryList.empty()) 674 continue; 675 676 auto GUID = GlobalList.first; 677 assert(GlobalList.second.SummaryList.size() == 1 && 678 "Expected individual combined index to have one summary per GUID"); 679 auto &Summary = GlobalList.second.SummaryList[0]; 680 // Skip the summaries for the importing module. These are included to 681 // e.g. record required linkage changes. 682 if (Summary->modulePath() == ModulePath) 683 continue; 684 // Add an entry to provoke importing by thinBackend. 685 ImportList[Summary->modulePath()].insert(GUID); 686 } 687 #ifndef NDEBUG 688 dumpImportListForModule(Index, ModulePath, ImportList); 689 #endif 690 } 691 692 void llvm::computeDeadSymbols( 693 ModuleSummaryIndex &Index, 694 const DenseSet<GlobalValue::GUID> &GUIDPreservedSymbols, 695 function_ref<PrevailingType(GlobalValue::GUID)> isPrevailing) { 696 assert(!Index.withGlobalValueDeadStripping()); 697 if (!ComputeDead) 698 return; 699 if (GUIDPreservedSymbols.empty()) 700 // Don't do anything when nothing is live, this is friendly with tests. 701 return; 702 unsigned LiveSymbols = 0; 703 SmallVector<ValueInfo, 128> Worklist; 704 Worklist.reserve(GUIDPreservedSymbols.size() * 2); 705 for (auto GUID : GUIDPreservedSymbols) { 706 ValueInfo VI = Index.getValueInfo(GUID); 707 if (!VI) 708 continue; 709 for (auto &S : VI.getSummaryList()) 710 S->setLive(true); 711 } 712 713 // Add values flagged in the index as live roots to the worklist. 714 for (const auto &Entry : Index) { 715 auto VI = Index.getValueInfo(Entry); 716 for (auto &S : Entry.second.SummaryList) 717 if (S->isLive()) { 718 LLVM_DEBUG(dbgs() << "Live root: " << VI << "\n"); 719 Worklist.push_back(VI); 720 ++LiveSymbols; 721 break; 722 } 723 } 724 725 // Make value live and add it to the worklist if it was not live before. 726 auto visit = [&](ValueInfo VI) { 727 // FIXME: If we knew which edges were created for indirect call profiles, 728 // we could skip them here. Any that are live should be reached via 729 // other edges, e.g. reference edges. Otherwise, using a profile collected 730 // on a slightly different binary might provoke preserving, importing 731 // and ultimately promoting calls to functions not linked into this 732 // binary, which increases the binary size unnecessarily. Note that 733 // if this code changes, the importer needs to change so that edges 734 // to functions marked dead are skipped. 735 VI = updateValueInfoForIndirectCalls(Index, VI); 736 if (!VI) 737 return; 738 for (auto &S : VI.getSummaryList()) 739 if (S->isLive()) 740 return; 741 742 // We only keep live symbols that are known to be non-prevailing if any are 743 // available_externally, linkonceodr, weakodr. Those symbols are discarded 744 // later in the EliminateAvailableExternally pass and setting them to 745 // not-live could break downstreams users of liveness information (PR36483) 746 // or limit optimization opportunities. 747 if (isPrevailing(VI.getGUID()) == PrevailingType::No) { 748 bool KeepAliveLinkage = false; 749 bool Interposable = false; 750 for (auto &S : VI.getSummaryList()) { 751 if (S->linkage() == GlobalValue::AvailableExternallyLinkage || 752 S->linkage() == GlobalValue::WeakODRLinkage || 753 S->linkage() == GlobalValue::LinkOnceODRLinkage) 754 KeepAliveLinkage = true; 755 else if (GlobalValue::isInterposableLinkage(S->linkage())) 756 Interposable = true; 757 } 758 759 if (!KeepAliveLinkage) 760 return; 761 762 if (Interposable) 763 report_fatal_error( 764 "Interposable and available_externally/linkonce_odr/weak_odr symbol"); 765 } 766 767 for (auto &S : VI.getSummaryList()) 768 S->setLive(true); 769 ++LiveSymbols; 770 Worklist.push_back(VI); 771 }; 772 773 while (!Worklist.empty()) { 774 auto VI = Worklist.pop_back_val(); 775 for (auto &Summary : VI.getSummaryList()) { 776 GlobalValueSummary *Base = Summary->getBaseObject(); 777 // Set base value live in case it is an alias. 778 Base->setLive(true); 779 for (auto Ref : Base->refs()) 780 visit(Ref); 781 if (auto *FS = dyn_cast<FunctionSummary>(Base)) 782 for (auto Call : FS->calls()) 783 visit(Call.first); 784 } 785 } 786 Index.setWithGlobalValueDeadStripping(); 787 788 unsigned DeadSymbols = Index.size() - LiveSymbols; 789 LLVM_DEBUG(dbgs() << LiveSymbols << " symbols Live, and " << DeadSymbols 790 << " symbols Dead \n"); 791 NumDeadSymbols += DeadSymbols; 792 NumLiveSymbols += LiveSymbols; 793 } 794 795 /// Compute the set of summaries needed for a ThinLTO backend compilation of 796 /// \p ModulePath. 797 void llvm::gatherImportedSummariesForModule( 798 StringRef ModulePath, 799 const StringMap<GVSummaryMapTy> &ModuleToDefinedGVSummaries, 800 const FunctionImporter::ImportMapTy &ImportList, 801 std::map<std::string, GVSummaryMapTy> &ModuleToSummariesForIndex) { 802 // Include all summaries from the importing module. 803 ModuleToSummariesForIndex[ModulePath] = 804 ModuleToDefinedGVSummaries.lookup(ModulePath); 805 // Include summaries for imports. 806 for (auto &ILI : ImportList) { 807 auto &SummariesForIndex = ModuleToSummariesForIndex[ILI.first()]; 808 const auto &DefinedGVSummaries = 809 ModuleToDefinedGVSummaries.lookup(ILI.first()); 810 for (auto &GI : ILI.second) { 811 const auto &DS = DefinedGVSummaries.find(GI); 812 assert(DS != DefinedGVSummaries.end() && 813 "Expected a defined summary for imported global value"); 814 SummariesForIndex[GI] = DS->second; 815 } 816 } 817 } 818 819 /// Emit the files \p ModulePath will import from into \p OutputFilename. 820 std::error_code llvm::EmitImportsFiles( 821 StringRef ModulePath, StringRef OutputFilename, 822 const std::map<std::string, GVSummaryMapTy> &ModuleToSummariesForIndex) { 823 std::error_code EC; 824 raw_fd_ostream ImportsOS(OutputFilename, EC, sys::fs::OpenFlags::F_None); 825 if (EC) 826 return EC; 827 for (auto &ILI : ModuleToSummariesForIndex) 828 // The ModuleToSummariesForIndex map includes an entry for the current 829 // Module (needed for writing out the index files). We don't want to 830 // include it in the imports file, however, so filter it out. 831 if (ILI.first != ModulePath) 832 ImportsOS << ILI.first << "\n"; 833 return std::error_code(); 834 } 835 836 bool llvm::convertToDeclaration(GlobalValue &GV) { 837 LLVM_DEBUG(dbgs() << "Converting to a declaration: `" << GV.getName() 838 << "\n"); 839 if (Function *F = dyn_cast<Function>(&GV)) { 840 F->deleteBody(); 841 F->clearMetadata(); 842 F->setComdat(nullptr); 843 } else if (GlobalVariable *V = dyn_cast<GlobalVariable>(&GV)) { 844 V->setInitializer(nullptr); 845 V->setLinkage(GlobalValue::ExternalLinkage); 846 V->clearMetadata(); 847 V->setComdat(nullptr); 848 } else { 849 GlobalValue *NewGV; 850 if (GV.getValueType()->isFunctionTy()) 851 NewGV = 852 Function::Create(cast<FunctionType>(GV.getValueType()), 853 GlobalValue::ExternalLinkage, "", GV.getParent()); 854 else 855 NewGV = 856 new GlobalVariable(*GV.getParent(), GV.getValueType(), 857 /*isConstant*/ false, GlobalValue::ExternalLinkage, 858 /*init*/ nullptr, "", 859 /*insertbefore*/ nullptr, GV.getThreadLocalMode(), 860 GV.getType()->getAddressSpace()); 861 NewGV->takeName(&GV); 862 GV.replaceAllUsesWith(NewGV); 863 return false; 864 } 865 return true; 866 } 867 868 /// Fixup WeakForLinker linkages in \p TheModule based on summary analysis. 869 void llvm::thinLTOResolveWeakForLinkerModule( 870 Module &TheModule, const GVSummaryMapTy &DefinedGlobals) { 871 auto updateLinkage = [&](GlobalValue &GV) { 872 // See if the global summary analysis computed a new resolved linkage. 873 const auto &GS = DefinedGlobals.find(GV.getGUID()); 874 if (GS == DefinedGlobals.end()) 875 return; 876 auto NewLinkage = GS->second->linkage(); 877 if (NewLinkage == GV.getLinkage()) 878 return; 879 880 // Switch the linkage to weakany if asked for, e.g. we do this for 881 // linker redefined symbols (via --wrap or --defsym). 882 // We record that the visibility should be changed here in `addThinLTO` 883 // as we need access to the resolution vectors for each input file in 884 // order to find which symbols have been redefined. 885 // We may consider reorganizing this code and moving the linkage recording 886 // somewhere else, e.g. in thinLTOResolveWeakForLinkerInIndex. 887 if (NewLinkage == GlobalValue::WeakAnyLinkage) { 888 GV.setLinkage(NewLinkage); 889 return; 890 } 891 892 if (!GlobalValue::isWeakForLinker(GV.getLinkage())) 893 return; 894 // Check for a non-prevailing def that has interposable linkage 895 // (e.g. non-odr weak or linkonce). In that case we can't simply 896 // convert to available_externally, since it would lose the 897 // interposable property and possibly get inlined. Simply drop 898 // the definition in that case. 899 if (GlobalValue::isAvailableExternallyLinkage(NewLinkage) && 900 GlobalValue::isInterposableLinkage(GV.getLinkage())) { 901 if (!convertToDeclaration(GV)) 902 // FIXME: Change this to collect replaced GVs and later erase 903 // them from the parent module once thinLTOResolveWeakForLinkerGUID is 904 // changed to enable this for aliases. 905 llvm_unreachable("Expected GV to be converted"); 906 } else { 907 // If the original symbols has global unnamed addr and linkonce_odr linkage, 908 // it should be an auto hide symbol. Add hidden visibility to the symbol to 909 // preserve the property. 910 if (GV.hasLinkOnceODRLinkage() && GV.hasGlobalUnnamedAddr() && 911 NewLinkage == GlobalValue::WeakODRLinkage) 912 GV.setVisibility(GlobalValue::HiddenVisibility); 913 914 LLVM_DEBUG(dbgs() << "ODR fixing up linkage for `" << GV.getName() 915 << "` from " << GV.getLinkage() << " to " << NewLinkage 916 << "\n"); 917 GV.setLinkage(NewLinkage); 918 } 919 // Remove declarations from comdats, including available_externally 920 // as this is a declaration for the linker, and will be dropped eventually. 921 // It is illegal for comdats to contain declarations. 922 auto *GO = dyn_cast_or_null<GlobalObject>(&GV); 923 if (GO && GO->isDeclarationForLinker() && GO->hasComdat()) 924 GO->setComdat(nullptr); 925 }; 926 927 // Process functions and global now 928 for (auto &GV : TheModule) 929 updateLinkage(GV); 930 for (auto &GV : TheModule.globals()) 931 updateLinkage(GV); 932 for (auto &GV : TheModule.aliases()) 933 updateLinkage(GV); 934 } 935 936 /// Run internalization on \p TheModule based on symmary analysis. 937 void llvm::thinLTOInternalizeModule(Module &TheModule, 938 const GVSummaryMapTy &DefinedGlobals) { 939 // Declare a callback for the internalize pass that will ask for every 940 // candidate GlobalValue if it can be internalized or not. 941 auto MustPreserveGV = [&](const GlobalValue &GV) -> bool { 942 // Lookup the linkage recorded in the summaries during global analysis. 943 auto GS = DefinedGlobals.find(GV.getGUID()); 944 if (GS == DefinedGlobals.end()) { 945 // Must have been promoted (possibly conservatively). Find original 946 // name so that we can access the correct summary and see if it can 947 // be internalized again. 948 // FIXME: Eventually we should control promotion instead of promoting 949 // and internalizing again. 950 StringRef OrigName = 951 ModuleSummaryIndex::getOriginalNameBeforePromote(GV.getName()); 952 std::string OrigId = GlobalValue::getGlobalIdentifier( 953 OrigName, GlobalValue::InternalLinkage, 954 TheModule.getSourceFileName()); 955 GS = DefinedGlobals.find(GlobalValue::getGUID(OrigId)); 956 if (GS == DefinedGlobals.end()) { 957 // Also check the original non-promoted non-globalized name. In some 958 // cases a preempted weak value is linked in as a local copy because 959 // it is referenced by an alias (IRLinker::linkGlobalValueProto). 960 // In that case, since it was originally not a local value, it was 961 // recorded in the index using the original name. 962 // FIXME: This may not be needed once PR27866 is fixed. 963 GS = DefinedGlobals.find(GlobalValue::getGUID(OrigName)); 964 assert(GS != DefinedGlobals.end()); 965 } 966 } 967 return !GlobalValue::isLocalLinkage(GS->second->linkage()); 968 }; 969 970 // FIXME: See if we can just internalize directly here via linkage changes 971 // based on the index, rather than invoking internalizeModule. 972 internalizeModule(TheModule, MustPreserveGV); 973 } 974 975 /// Make alias a clone of its aliasee. 976 static Function *replaceAliasWithAliasee(Module *SrcModule, GlobalAlias *GA) { 977 Function *Fn = cast<Function>(GA->getBaseObject()); 978 979 ValueToValueMapTy VMap; 980 Function *NewFn = CloneFunction(Fn, VMap); 981 // Clone should use the original alias's linkage and name, and we ensure 982 // all uses of alias instead use the new clone (casted if necessary). 983 NewFn->setLinkage(GA->getLinkage()); 984 GA->replaceAllUsesWith(ConstantExpr::getBitCast(NewFn, GA->getType())); 985 NewFn->takeName(GA); 986 return NewFn; 987 } 988 989 // Automatically import functions in Module \p DestModule based on the summaries 990 // index. 991 Expected<bool> FunctionImporter::importFunctions( 992 Module &DestModule, const FunctionImporter::ImportMapTy &ImportList) { 993 LLVM_DEBUG(dbgs() << "Starting import for Module " 994 << DestModule.getModuleIdentifier() << "\n"); 995 unsigned ImportedCount = 0, ImportedGVCount = 0; 996 997 IRMover Mover(DestModule); 998 // Do the actual import of functions now, one Module at a time 999 std::set<StringRef> ModuleNameOrderedList; 1000 for (auto &FunctionsToImportPerModule : ImportList) { 1001 ModuleNameOrderedList.insert(FunctionsToImportPerModule.first()); 1002 } 1003 for (auto &Name : ModuleNameOrderedList) { 1004 // Get the module for the import 1005 const auto &FunctionsToImportPerModule = ImportList.find(Name); 1006 assert(FunctionsToImportPerModule != ImportList.end()); 1007 Expected<std::unique_ptr<Module>> SrcModuleOrErr = ModuleLoader(Name); 1008 if (!SrcModuleOrErr) 1009 return SrcModuleOrErr.takeError(); 1010 std::unique_ptr<Module> SrcModule = std::move(*SrcModuleOrErr); 1011 assert(&DestModule.getContext() == &SrcModule->getContext() && 1012 "Context mismatch"); 1013 1014 // If modules were created with lazy metadata loading, materialize it 1015 // now, before linking it (otherwise this will be a noop). 1016 if (Error Err = SrcModule->materializeMetadata()) 1017 return std::move(Err); 1018 1019 auto &ImportGUIDs = FunctionsToImportPerModule->second; 1020 // Find the globals to import 1021 SetVector<GlobalValue *> GlobalsToImport; 1022 for (Function &F : *SrcModule) { 1023 if (!F.hasName()) 1024 continue; 1025 auto GUID = F.getGUID(); 1026 auto Import = ImportGUIDs.count(GUID); 1027 LLVM_DEBUG(dbgs() << (Import ? "Is" : "Not") << " importing function " 1028 << GUID << " " << F.getName() << " from " 1029 << SrcModule->getSourceFileName() << "\n"); 1030 if (Import) { 1031 if (Error Err = F.materialize()) 1032 return std::move(Err); 1033 if (EnableImportMetadata) { 1034 // Add 'thinlto_src_module' metadata for statistics and debugging. 1035 F.setMetadata( 1036 "thinlto_src_module", 1037 MDNode::get(DestModule.getContext(), 1038 {MDString::get(DestModule.getContext(), 1039 SrcModule->getSourceFileName())})); 1040 } 1041 GlobalsToImport.insert(&F); 1042 } 1043 } 1044 for (GlobalVariable &GV : SrcModule->globals()) { 1045 if (!GV.hasName()) 1046 continue; 1047 auto GUID = GV.getGUID(); 1048 auto Import = ImportGUIDs.count(GUID); 1049 LLVM_DEBUG(dbgs() << (Import ? "Is" : "Not") << " importing global " 1050 << GUID << " " << GV.getName() << " from " 1051 << SrcModule->getSourceFileName() << "\n"); 1052 if (Import) { 1053 if (Error Err = GV.materialize()) 1054 return std::move(Err); 1055 ImportedGVCount += GlobalsToImport.insert(&GV); 1056 } 1057 } 1058 for (GlobalAlias &GA : SrcModule->aliases()) { 1059 if (!GA.hasName()) 1060 continue; 1061 auto GUID = GA.getGUID(); 1062 auto Import = ImportGUIDs.count(GUID); 1063 LLVM_DEBUG(dbgs() << (Import ? "Is" : "Not") << " importing alias " 1064 << GUID << " " << GA.getName() << " from " 1065 << SrcModule->getSourceFileName() << "\n"); 1066 if (Import) { 1067 if (Error Err = GA.materialize()) 1068 return std::move(Err); 1069 // Import alias as a copy of its aliasee. 1070 GlobalObject *Base = GA.getBaseObject(); 1071 if (Error Err = Base->materialize()) 1072 return std::move(Err); 1073 auto *Fn = replaceAliasWithAliasee(SrcModule.get(), &GA); 1074 LLVM_DEBUG(dbgs() << "Is importing aliasee fn " << Base->getGUID() 1075 << " " << Base->getName() << " from " 1076 << SrcModule->getSourceFileName() << "\n"); 1077 if (EnableImportMetadata) { 1078 // Add 'thinlto_src_module' metadata for statistics and debugging. 1079 Fn->setMetadata( 1080 "thinlto_src_module", 1081 MDNode::get(DestModule.getContext(), 1082 {MDString::get(DestModule.getContext(), 1083 SrcModule->getSourceFileName())})); 1084 } 1085 GlobalsToImport.insert(Fn); 1086 } 1087 } 1088 1089 // Upgrade debug info after we're done materializing all the globals and we 1090 // have loaded all the required metadata! 1091 UpgradeDebugInfo(*SrcModule); 1092 1093 // Link in the specified functions. 1094 if (renameModuleForThinLTO(*SrcModule, Index, &GlobalsToImport)) 1095 return true; 1096 1097 if (PrintImports) { 1098 for (const auto *GV : GlobalsToImport) 1099 dbgs() << DestModule.getSourceFileName() << ": Import " << GV->getName() 1100 << " from " << SrcModule->getSourceFileName() << "\n"; 1101 } 1102 1103 if (Mover.move(std::move(SrcModule), GlobalsToImport.getArrayRef(), 1104 [](GlobalValue &, IRMover::ValueAdder) {}, 1105 /*IsPerformingImport=*/true)) 1106 report_fatal_error("Function Import: link error"); 1107 1108 ImportedCount += GlobalsToImport.size(); 1109 NumImportedModules++; 1110 } 1111 1112 NumImportedFunctions += (ImportedCount - ImportedGVCount); 1113 NumImportedGlobalVars += ImportedGVCount; 1114 1115 LLVM_DEBUG(dbgs() << "Imported " << ImportedCount - ImportedGVCount 1116 << " functions for Module " 1117 << DestModule.getModuleIdentifier() << "\n"); 1118 LLVM_DEBUG(dbgs() << "Imported " << ImportedGVCount 1119 << " global variables for Module " 1120 << DestModule.getModuleIdentifier() << "\n"); 1121 return ImportedCount; 1122 } 1123 1124 static bool doImportingForModule(Module &M) { 1125 if (SummaryFile.empty()) 1126 report_fatal_error("error: -function-import requires -summary-file\n"); 1127 Expected<std::unique_ptr<ModuleSummaryIndex>> IndexPtrOrErr = 1128 getModuleSummaryIndexForFile(SummaryFile); 1129 if (!IndexPtrOrErr) { 1130 logAllUnhandledErrors(IndexPtrOrErr.takeError(), errs(), 1131 "Error loading file '" + SummaryFile + "': "); 1132 return false; 1133 } 1134 std::unique_ptr<ModuleSummaryIndex> Index = std::move(*IndexPtrOrErr); 1135 1136 // First step is collecting the import list. 1137 FunctionImporter::ImportMapTy ImportList; 1138 // If requested, simply import all functions in the index. This is used 1139 // when testing distributed backend handling via the opt tool, when 1140 // we have distributed indexes containing exactly the summaries to import. 1141 if (ImportAllIndex) 1142 ComputeCrossModuleImportForModuleFromIndex(M.getModuleIdentifier(), *Index, 1143 ImportList); 1144 else 1145 ComputeCrossModuleImportForModule(M.getModuleIdentifier(), *Index, 1146 ImportList); 1147 1148 // Conservatively mark all internal values as promoted. This interface is 1149 // only used when doing importing via the function importing pass. The pass 1150 // is only enabled when testing importing via the 'opt' tool, which does 1151 // not do the ThinLink that would normally determine what values to promote. 1152 for (auto &I : *Index) { 1153 for (auto &S : I.second.SummaryList) { 1154 if (GlobalValue::isLocalLinkage(S->linkage())) 1155 S->setLinkage(GlobalValue::ExternalLinkage); 1156 } 1157 } 1158 1159 // Next we need to promote to global scope and rename any local values that 1160 // are potentially exported to other modules. 1161 if (renameModuleForThinLTO(M, *Index, nullptr)) { 1162 errs() << "Error renaming module\n"; 1163 return false; 1164 } 1165 1166 // Perform the import now. 1167 auto ModuleLoader = [&M](StringRef Identifier) { 1168 return loadFile(Identifier, M.getContext()); 1169 }; 1170 FunctionImporter Importer(*Index, ModuleLoader); 1171 Expected<bool> Result = Importer.importFunctions(M, ImportList); 1172 1173 // FIXME: Probably need to propagate Errors through the pass manager. 1174 if (!Result) { 1175 logAllUnhandledErrors(Result.takeError(), errs(), 1176 "Error importing module: "); 1177 return false; 1178 } 1179 1180 return *Result; 1181 } 1182 1183 namespace { 1184 1185 /// Pass that performs cross-module function import provided a summary file. 1186 class FunctionImportLegacyPass : public ModulePass { 1187 public: 1188 /// Pass identification, replacement for typeid 1189 static char ID; 1190 1191 explicit FunctionImportLegacyPass() : ModulePass(ID) {} 1192 1193 /// Specify pass name for debug output 1194 StringRef getPassName() const override { return "Function Importing"; } 1195 1196 bool runOnModule(Module &M) override { 1197 if (skipModule(M)) 1198 return false; 1199 1200 return doImportingForModule(M); 1201 } 1202 }; 1203 1204 } // end anonymous namespace 1205 1206 PreservedAnalyses FunctionImportPass::run(Module &M, 1207 ModuleAnalysisManager &AM) { 1208 if (!doImportingForModule(M)) 1209 return PreservedAnalyses::all(); 1210 1211 return PreservedAnalyses::none(); 1212 } 1213 1214 char FunctionImportLegacyPass::ID = 0; 1215 INITIALIZE_PASS(FunctionImportLegacyPass, "function-import", 1216 "Summary Based Function Import", false, false) 1217 1218 namespace llvm { 1219 1220 Pass *createFunctionImportPass() { 1221 return new FunctionImportLegacyPass(); 1222 } 1223 1224 } // end namespace llvm 1225