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