1 //===-ThinLTOCodeGenerator.cpp - LLVM Link Time Optimizer -----------------===// 2 // 3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 4 // See https://llvm.org/LICENSE.txt for license information. 5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 6 // 7 //===----------------------------------------------------------------------===// 8 // 9 // This file implements the Thin Link Time Optimization library. This library is 10 // intended to be used by linker to optimize code at link time. 11 // 12 //===----------------------------------------------------------------------===// 13 14 #include "llvm/LTO/legacy/ThinLTOCodeGenerator.h" 15 16 #include "llvm/ADT/Statistic.h" 17 #include "llvm/ADT/StringExtras.h" 18 #include "llvm/Analysis/ModuleSummaryAnalysis.h" 19 #include "llvm/Analysis/ProfileSummaryInfo.h" 20 #include "llvm/Analysis/TargetLibraryInfo.h" 21 #include "llvm/Analysis/TargetTransformInfo.h" 22 #include "llvm/Bitcode/BitcodeReader.h" 23 #include "llvm/Bitcode/BitcodeWriter.h" 24 #include "llvm/Bitcode/BitcodeWriterPass.h" 25 #include "llvm/Config/llvm-config.h" 26 #include "llvm/IR/DebugInfo.h" 27 #include "llvm/IR/DiagnosticPrinter.h" 28 #include "llvm/IR/LLVMContext.h" 29 #include "llvm/IR/LegacyPassManager.h" 30 #include "llvm/IR/Mangler.h" 31 #include "llvm/IR/PassTimingInfo.h" 32 #include "llvm/IR/Verifier.h" 33 #include "llvm/IRReader/IRReader.h" 34 #include "llvm/LTO/LTO.h" 35 #include "llvm/LTO/SummaryBasedOptimizations.h" 36 #include "llvm/MC/SubtargetFeature.h" 37 #include "llvm/Object/IRObjectFile.h" 38 #include "llvm/Support/CachePruning.h" 39 #include "llvm/Support/Debug.h" 40 #include "llvm/Support/Error.h" 41 #include "llvm/Support/Path.h" 42 #include "llvm/Support/SHA1.h" 43 #include "llvm/Support/SmallVectorMemoryBuffer.h" 44 #include "llvm/Support/TargetRegistry.h" 45 #include "llvm/Support/ThreadPool.h" 46 #include "llvm/Support/Threading.h" 47 #include "llvm/Support/ToolOutputFile.h" 48 #include "llvm/Support/VCSRevision.h" 49 #include "llvm/Target/TargetMachine.h" 50 #include "llvm/Transforms/IPO.h" 51 #include "llvm/Transforms/IPO/FunctionImport.h" 52 #include "llvm/Transforms/IPO/Internalize.h" 53 #include "llvm/Transforms/IPO/PassManagerBuilder.h" 54 #include "llvm/Transforms/ObjCARC.h" 55 #include "llvm/Transforms/Utils/FunctionImportUtils.h" 56 57 #include <numeric> 58 59 #if !defined(_MSC_VER) && !defined(__MINGW32__) 60 #include <unistd.h> 61 #else 62 #include <io.h> 63 #endif 64 65 using namespace llvm; 66 67 #define DEBUG_TYPE "thinlto" 68 69 namespace llvm { 70 // Flags -discard-value-names, defined in LTOCodeGenerator.cpp 71 extern cl::opt<bool> LTODiscardValueNames; 72 extern cl::opt<std::string> LTORemarksFilename; 73 extern cl::opt<std::string> LTORemarksPasses; 74 extern cl::opt<bool> LTOPassRemarksWithHotness; 75 } 76 77 namespace { 78 79 static cl::opt<int> 80 ThreadCount("threads", cl::init(llvm::heavyweight_hardware_concurrency())); 81 82 // Simple helper to save temporary files for debug. 83 static void saveTempBitcode(const Module &TheModule, StringRef TempDir, 84 unsigned count, StringRef Suffix) { 85 if (TempDir.empty()) 86 return; 87 // User asked to save temps, let dump the bitcode file after import. 88 std::string SaveTempPath = (TempDir + llvm::Twine(count) + Suffix).str(); 89 std::error_code EC; 90 raw_fd_ostream OS(SaveTempPath, EC, sys::fs::F_None); 91 if (EC) 92 report_fatal_error(Twine("Failed to open ") + SaveTempPath + 93 " to save optimized bitcode\n"); 94 WriteBitcodeToFile(TheModule, OS, /* ShouldPreserveUseListOrder */ true); 95 } 96 97 static const GlobalValueSummary * 98 getFirstDefinitionForLinker(const GlobalValueSummaryList &GVSummaryList) { 99 // If there is any strong definition anywhere, get it. 100 auto StrongDefForLinker = llvm::find_if( 101 GVSummaryList, [](const std::unique_ptr<GlobalValueSummary> &Summary) { 102 auto Linkage = Summary->linkage(); 103 return !GlobalValue::isAvailableExternallyLinkage(Linkage) && 104 !GlobalValue::isWeakForLinker(Linkage); 105 }); 106 if (StrongDefForLinker != GVSummaryList.end()) 107 return StrongDefForLinker->get(); 108 // Get the first *linker visible* definition for this global in the summary 109 // list. 110 auto FirstDefForLinker = llvm::find_if( 111 GVSummaryList, [](const std::unique_ptr<GlobalValueSummary> &Summary) { 112 auto Linkage = Summary->linkage(); 113 return !GlobalValue::isAvailableExternallyLinkage(Linkage); 114 }); 115 // Extern templates can be emitted as available_externally. 116 if (FirstDefForLinker == GVSummaryList.end()) 117 return nullptr; 118 return FirstDefForLinker->get(); 119 } 120 121 // Populate map of GUID to the prevailing copy for any multiply defined 122 // symbols. Currently assume first copy is prevailing, or any strong 123 // definition. Can be refined with Linker information in the future. 124 static void computePrevailingCopies( 125 const ModuleSummaryIndex &Index, 126 DenseMap<GlobalValue::GUID, const GlobalValueSummary *> &PrevailingCopy) { 127 auto HasMultipleCopies = [&](const GlobalValueSummaryList &GVSummaryList) { 128 return GVSummaryList.size() > 1; 129 }; 130 131 for (auto &I : Index) { 132 if (HasMultipleCopies(I.second.SummaryList)) 133 PrevailingCopy[I.first] = 134 getFirstDefinitionForLinker(I.second.SummaryList); 135 } 136 } 137 138 static StringMap<lto::InputFile *> 139 generateModuleMap(std::vector<std::unique_ptr<lto::InputFile>> &Modules) { 140 StringMap<lto::InputFile *> ModuleMap; 141 for (auto &M : Modules) { 142 assert(ModuleMap.find(M->getName()) == ModuleMap.end() && 143 "Expect unique Buffer Identifier"); 144 ModuleMap[M->getName()] = M.get(); 145 } 146 return ModuleMap; 147 } 148 149 static void promoteModule(Module &TheModule, const ModuleSummaryIndex &Index) { 150 if (renameModuleForThinLTO(TheModule, Index)) 151 report_fatal_error("renameModuleForThinLTO failed"); 152 } 153 154 namespace { 155 class ThinLTODiagnosticInfo : public DiagnosticInfo { 156 const Twine &Msg; 157 public: 158 ThinLTODiagnosticInfo(const Twine &DiagMsg, 159 DiagnosticSeverity Severity = DS_Error) 160 : DiagnosticInfo(DK_Linker, Severity), Msg(DiagMsg) {} 161 void print(DiagnosticPrinter &DP) const override { DP << Msg; } 162 }; 163 } 164 165 /// Verify the module and strip broken debug info. 166 static void verifyLoadedModule(Module &TheModule) { 167 bool BrokenDebugInfo = false; 168 if (verifyModule(TheModule, &dbgs(), &BrokenDebugInfo)) 169 report_fatal_error("Broken module found, compilation aborted!"); 170 if (BrokenDebugInfo) { 171 TheModule.getContext().diagnose(ThinLTODiagnosticInfo( 172 "Invalid debug info found, debug info will be stripped", DS_Warning)); 173 StripDebugInfo(TheModule); 174 } 175 } 176 177 static std::unique_ptr<Module> loadModuleFromInput(lto::InputFile *Input, 178 LLVMContext &Context, 179 bool Lazy, 180 bool IsImporting) { 181 auto &Mod = Input->getSingleBitcodeModule(); 182 SMDiagnostic Err; 183 Expected<std::unique_ptr<Module>> ModuleOrErr = 184 Lazy ? Mod.getLazyModule(Context, 185 /* ShouldLazyLoadMetadata */ true, IsImporting) 186 : Mod.parseModule(Context); 187 if (!ModuleOrErr) { 188 handleAllErrors(ModuleOrErr.takeError(), [&](ErrorInfoBase &EIB) { 189 SMDiagnostic Err = SMDiagnostic(Mod.getModuleIdentifier(), 190 SourceMgr::DK_Error, EIB.message()); 191 Err.print("ThinLTO", errs()); 192 }); 193 report_fatal_error("Can't load module, abort."); 194 } 195 if (!Lazy) 196 verifyLoadedModule(*ModuleOrErr.get()); 197 return std::move(*ModuleOrErr); 198 } 199 200 static void 201 crossImportIntoModule(Module &TheModule, const ModuleSummaryIndex &Index, 202 StringMap<lto::InputFile*> &ModuleMap, 203 const FunctionImporter::ImportMapTy &ImportList) { 204 auto Loader = [&](StringRef Identifier) { 205 auto &Input = ModuleMap[Identifier]; 206 return loadModuleFromInput(Input, TheModule.getContext(), 207 /*Lazy=*/true, /*IsImporting*/ true); 208 }; 209 210 FunctionImporter Importer(Index, Loader); 211 Expected<bool> Result = Importer.importFunctions(TheModule, ImportList); 212 if (!Result) { 213 handleAllErrors(Result.takeError(), [&](ErrorInfoBase &EIB) { 214 SMDiagnostic Err = SMDiagnostic(TheModule.getModuleIdentifier(), 215 SourceMgr::DK_Error, EIB.message()); 216 Err.print("ThinLTO", errs()); 217 }); 218 report_fatal_error("importFunctions failed"); 219 } 220 // Verify again after cross-importing. 221 verifyLoadedModule(TheModule); 222 } 223 224 static void optimizeModule(Module &TheModule, TargetMachine &TM, 225 unsigned OptLevel, bool Freestanding) { 226 // Populate the PassManager 227 PassManagerBuilder PMB; 228 PMB.LibraryInfo = new TargetLibraryInfoImpl(TM.getTargetTriple()); 229 if (Freestanding) 230 PMB.LibraryInfo->disableAllFunctions(); 231 PMB.Inliner = createFunctionInliningPass(); 232 // FIXME: should get it from the bitcode? 233 PMB.OptLevel = OptLevel; 234 PMB.LoopVectorize = true; 235 PMB.SLPVectorize = true; 236 // Already did this in verifyLoadedModule(). 237 PMB.VerifyInput = false; 238 PMB.VerifyOutput = false; 239 240 legacy::PassManager PM; 241 242 // Add the TTI (required to inform the vectorizer about register size for 243 // instance) 244 PM.add(createTargetTransformInfoWrapperPass(TM.getTargetIRAnalysis())); 245 246 // Add optimizations 247 PMB.populateThinLTOPassManager(PM); 248 249 PM.run(TheModule); 250 } 251 252 static void 253 addUsedSymbolToPreservedGUID(const lto::InputFile &File, 254 DenseSet<GlobalValue::GUID> &PreservedGUID) { 255 for (const auto &Sym : File.symbols()) { 256 if (Sym.isUsed()) 257 PreservedGUID.insert(GlobalValue::getGUID(Sym.getIRName())); 258 } 259 } 260 261 // Convert the PreservedSymbols map from "Name" based to "GUID" based. 262 static DenseSet<GlobalValue::GUID> 263 computeGUIDPreservedSymbols(const StringSet<> &PreservedSymbols, 264 const Triple &TheTriple) { 265 DenseSet<GlobalValue::GUID> GUIDPreservedSymbols(PreservedSymbols.size()); 266 for (auto &Entry : PreservedSymbols) { 267 StringRef Name = Entry.first(); 268 if (TheTriple.isOSBinFormatMachO() && Name.size() > 0 && Name[0] == '_') 269 Name = Name.drop_front(); 270 GUIDPreservedSymbols.insert(GlobalValue::getGUID(Name)); 271 } 272 return GUIDPreservedSymbols; 273 } 274 275 std::unique_ptr<MemoryBuffer> codegenModule(Module &TheModule, 276 TargetMachine &TM) { 277 SmallVector<char, 128> OutputBuffer; 278 279 // CodeGen 280 { 281 raw_svector_ostream OS(OutputBuffer); 282 legacy::PassManager PM; 283 284 // If the bitcode files contain ARC code and were compiled with optimization, 285 // the ObjCARCContractPass must be run, so do it unconditionally here. 286 PM.add(createObjCARCContractPass()); 287 288 // Setup the codegen now. 289 if (TM.addPassesToEmitFile(PM, OS, nullptr, TargetMachine::CGFT_ObjectFile, 290 /* DisableVerify */ true)) 291 report_fatal_error("Failed to setup codegen"); 292 293 // Run codegen now. resulting binary is in OutputBuffer. 294 PM.run(TheModule); 295 } 296 return make_unique<SmallVectorMemoryBuffer>(std::move(OutputBuffer)); 297 } 298 299 /// Manage caching for a single Module. 300 class ModuleCacheEntry { 301 SmallString<128> EntryPath; 302 303 public: 304 // Create a cache entry. This compute a unique hash for the Module considering 305 // the current list of export/import, and offer an interface to query to 306 // access the content in the cache. 307 ModuleCacheEntry( 308 StringRef CachePath, const ModuleSummaryIndex &Index, StringRef ModuleID, 309 const FunctionImporter::ImportMapTy &ImportList, 310 const FunctionImporter::ExportSetTy &ExportList, 311 const std::map<GlobalValue::GUID, GlobalValue::LinkageTypes> &ResolvedODR, 312 const GVSummaryMapTy &DefinedGVSummaries, unsigned OptLevel, 313 bool Freestanding, const TargetMachineBuilder &TMBuilder) { 314 if (CachePath.empty()) 315 return; 316 317 if (!Index.modulePaths().count(ModuleID)) 318 // The module does not have an entry, it can't have a hash at all 319 return; 320 321 if (all_of(Index.getModuleHash(ModuleID), 322 [](uint32_t V) { return V == 0; })) 323 // No hash entry, no caching! 324 return; 325 326 llvm::lto::Config Conf; 327 Conf.OptLevel = OptLevel; 328 Conf.Options = TMBuilder.Options; 329 Conf.CPU = TMBuilder.MCpu; 330 Conf.MAttrs.push_back(TMBuilder.MAttr); 331 Conf.RelocModel = TMBuilder.RelocModel; 332 Conf.CGOptLevel = TMBuilder.CGOptLevel; 333 Conf.Freestanding = Freestanding; 334 SmallString<40> Key; 335 computeLTOCacheKey(Key, Conf, Index, ModuleID, ImportList, ExportList, 336 ResolvedODR, DefinedGVSummaries); 337 338 // This choice of file name allows the cache to be pruned (see pruneCache() 339 // in include/llvm/Support/CachePruning.h). 340 sys::path::append(EntryPath, CachePath, "llvmcache-" + Key); 341 } 342 343 // Access the path to this entry in the cache. 344 StringRef getEntryPath() { return EntryPath; } 345 346 // Try loading the buffer for this cache entry. 347 ErrorOr<std::unique_ptr<MemoryBuffer>> tryLoadingBuffer() { 348 if (EntryPath.empty()) 349 return std::error_code(); 350 int FD; 351 SmallString<64> ResultPath; 352 std::error_code EC = sys::fs::openFileForRead( 353 Twine(EntryPath), FD, sys::fs::OF_UpdateAtime, &ResultPath); 354 if (EC) 355 return EC; 356 ErrorOr<std::unique_ptr<MemoryBuffer>> MBOrErr = 357 MemoryBuffer::getOpenFile(FD, EntryPath, 358 /*FileSize*/ -1, 359 /*RequiresNullTerminator*/ false); 360 close(FD); 361 return MBOrErr; 362 } 363 364 // Cache the Produced object file 365 void write(const MemoryBuffer &OutputBuffer) { 366 if (EntryPath.empty()) 367 return; 368 369 // Write to a temporary to avoid race condition 370 SmallString<128> TempFilename; 371 SmallString<128> CachePath(EntryPath); 372 int TempFD; 373 llvm::sys::path::remove_filename(CachePath); 374 sys::path::append(TempFilename, CachePath, "Thin-%%%%%%.tmp.o"); 375 std::error_code EC = 376 sys::fs::createUniqueFile(TempFilename, TempFD, TempFilename); 377 if (EC) { 378 errs() << "Error: " << EC.message() << "\n"; 379 report_fatal_error("ThinLTO: Can't get a temporary file"); 380 } 381 { 382 raw_fd_ostream OS(TempFD, /* ShouldClose */ true); 383 OS << OutputBuffer.getBuffer(); 384 } 385 // Rename temp file to final destination; rename is atomic 386 EC = sys::fs::rename(TempFilename, EntryPath); 387 if (EC) 388 sys::fs::remove(TempFilename); 389 } 390 }; 391 392 static std::unique_ptr<MemoryBuffer> 393 ProcessThinLTOModule(Module &TheModule, ModuleSummaryIndex &Index, 394 StringMap<lto::InputFile *> &ModuleMap, TargetMachine &TM, 395 const FunctionImporter::ImportMapTy &ImportList, 396 const FunctionImporter::ExportSetTy &ExportList, 397 const DenseSet<GlobalValue::GUID> &GUIDPreservedSymbols, 398 const GVSummaryMapTy &DefinedGlobals, 399 const ThinLTOCodeGenerator::CachingOptions &CacheOptions, 400 bool DisableCodeGen, StringRef SaveTempsDir, 401 bool Freestanding, unsigned OptLevel, unsigned count) { 402 403 // "Benchmark"-like optimization: single-source case 404 bool SingleModule = (ModuleMap.size() == 1); 405 406 if (!SingleModule) { 407 promoteModule(TheModule, Index); 408 409 // Apply summary-based prevailing-symbol resolution decisions. 410 thinLTOResolvePrevailingInModule(TheModule, DefinedGlobals); 411 412 // Save temps: after promotion. 413 saveTempBitcode(TheModule, SaveTempsDir, count, ".1.promoted.bc"); 414 } 415 416 // Be friendly and don't nuke totally the module when the client didn't 417 // supply anything to preserve. 418 if (!ExportList.empty() || !GUIDPreservedSymbols.empty()) { 419 // Apply summary-based internalization decisions. 420 thinLTOInternalizeModule(TheModule, DefinedGlobals); 421 } 422 423 // Save internalized bitcode 424 saveTempBitcode(TheModule, SaveTempsDir, count, ".2.internalized.bc"); 425 426 if (!SingleModule) { 427 crossImportIntoModule(TheModule, Index, ModuleMap, ImportList); 428 429 // Save temps: after cross-module import. 430 saveTempBitcode(TheModule, SaveTempsDir, count, ".3.imported.bc"); 431 } 432 433 optimizeModule(TheModule, TM, OptLevel, Freestanding); 434 435 saveTempBitcode(TheModule, SaveTempsDir, count, ".4.opt.bc"); 436 437 if (DisableCodeGen) { 438 // Configured to stop before CodeGen, serialize the bitcode and return. 439 SmallVector<char, 128> OutputBuffer; 440 { 441 raw_svector_ostream OS(OutputBuffer); 442 ProfileSummaryInfo PSI(TheModule); 443 auto Index = buildModuleSummaryIndex(TheModule, nullptr, &PSI); 444 WriteBitcodeToFile(TheModule, OS, true, &Index); 445 } 446 return make_unique<SmallVectorMemoryBuffer>(std::move(OutputBuffer)); 447 } 448 449 return codegenModule(TheModule, TM); 450 } 451 452 /// Resolve prevailing symbols. Record resolutions in the \p ResolvedODR map 453 /// for caching, and in the \p Index for application during the ThinLTO 454 /// backends. This is needed for correctness for exported symbols (ensure 455 /// at least one copy kept) and a compile-time optimization (to drop duplicate 456 /// copies when possible). 457 static void resolvePrevailingInIndex( 458 ModuleSummaryIndex &Index, 459 StringMap<std::map<GlobalValue::GUID, GlobalValue::LinkageTypes>> 460 &ResolvedODR) { 461 462 DenseMap<GlobalValue::GUID, const GlobalValueSummary *> PrevailingCopy; 463 computePrevailingCopies(Index, PrevailingCopy); 464 465 auto isPrevailing = [&](GlobalValue::GUID GUID, const GlobalValueSummary *S) { 466 const auto &Prevailing = PrevailingCopy.find(GUID); 467 // Not in map means that there was only one copy, which must be prevailing. 468 if (Prevailing == PrevailingCopy.end()) 469 return true; 470 return Prevailing->second == S; 471 }; 472 473 auto recordNewLinkage = [&](StringRef ModuleIdentifier, 474 GlobalValue::GUID GUID, 475 GlobalValue::LinkageTypes NewLinkage) { 476 ResolvedODR[ModuleIdentifier][GUID] = NewLinkage; 477 }; 478 479 thinLTOResolvePrevailingInIndex(Index, isPrevailing, recordNewLinkage); 480 } 481 482 // Initialize the TargetMachine builder for a given Triple 483 static void initTMBuilder(TargetMachineBuilder &TMBuilder, 484 const Triple &TheTriple) { 485 // Set a default CPU for Darwin triples (copied from LTOCodeGenerator). 486 // FIXME this looks pretty terrible... 487 if (TMBuilder.MCpu.empty() && TheTriple.isOSDarwin()) { 488 if (TheTriple.getArch() == llvm::Triple::x86_64) 489 TMBuilder.MCpu = "core2"; 490 else if (TheTriple.getArch() == llvm::Triple::x86) 491 TMBuilder.MCpu = "yonah"; 492 else if (TheTriple.getArch() == llvm::Triple::aarch64) 493 TMBuilder.MCpu = "cyclone"; 494 } 495 TMBuilder.TheTriple = std::move(TheTriple); 496 } 497 498 } // end anonymous namespace 499 500 void ThinLTOCodeGenerator::addModule(StringRef Identifier, StringRef Data) { 501 MemoryBufferRef Buffer(Data, Identifier); 502 503 auto InputOrError = lto::InputFile::create(Buffer); 504 if (!InputOrError) 505 report_fatal_error("ThinLTO cannot create input file: " + 506 toString(InputOrError.takeError())); 507 508 auto TripleStr = (*InputOrError)->getTargetTriple(); 509 Triple TheTriple(TripleStr); 510 511 if (Modules.empty()) 512 initTMBuilder(TMBuilder, Triple(TheTriple)); 513 else if (TMBuilder.TheTriple != TheTriple) { 514 if (!TMBuilder.TheTriple.isCompatibleWith(TheTriple)) 515 report_fatal_error("ThinLTO modules with incompatible triples not " 516 "supported"); 517 initTMBuilder(TMBuilder, Triple(TMBuilder.TheTriple.merge(TheTriple))); 518 } 519 520 Modules.emplace_back(std::move(*InputOrError)); 521 } 522 523 void ThinLTOCodeGenerator::preserveSymbol(StringRef Name) { 524 PreservedSymbols.insert(Name); 525 } 526 527 void ThinLTOCodeGenerator::crossReferenceSymbol(StringRef Name) { 528 // FIXME: At the moment, we don't take advantage of this extra information, 529 // we're conservatively considering cross-references as preserved. 530 // CrossReferencedSymbols.insert(Name); 531 PreservedSymbols.insert(Name); 532 } 533 534 // TargetMachine factory 535 std::unique_ptr<TargetMachine> TargetMachineBuilder::create() const { 536 std::string ErrMsg; 537 const Target *TheTarget = 538 TargetRegistry::lookupTarget(TheTriple.str(), ErrMsg); 539 if (!TheTarget) { 540 report_fatal_error("Can't load target for this Triple: " + ErrMsg); 541 } 542 543 // Use MAttr as the default set of features. 544 SubtargetFeatures Features(MAttr); 545 Features.getDefaultSubtargetFeatures(TheTriple); 546 std::string FeatureStr = Features.getString(); 547 548 return std::unique_ptr<TargetMachine>( 549 TheTarget->createTargetMachine(TheTriple.str(), MCpu, FeatureStr, Options, 550 RelocModel, None, CGOptLevel)); 551 } 552 553 /** 554 * Produce the combined summary index from all the bitcode files: 555 * "thin-link". 556 */ 557 std::unique_ptr<ModuleSummaryIndex> ThinLTOCodeGenerator::linkCombinedIndex() { 558 std::unique_ptr<ModuleSummaryIndex> CombinedIndex = 559 llvm::make_unique<ModuleSummaryIndex>(/*HaveGVs=*/false); 560 uint64_t NextModuleId = 0; 561 for (auto &Mod : Modules) { 562 auto &M = Mod->getSingleBitcodeModule(); 563 if (Error Err = 564 M.readSummary(*CombinedIndex, Mod->getName(), NextModuleId++)) { 565 // FIXME diagnose 566 logAllUnhandledErrors( 567 std::move(Err), errs(), 568 "error: can't create module summary index for buffer: "); 569 return nullptr; 570 } 571 } 572 return CombinedIndex; 573 } 574 575 static void internalizeAndPromoteInIndex( 576 const StringMap<FunctionImporter::ExportSetTy> &ExportLists, 577 const DenseSet<GlobalValue::GUID> &GUIDPreservedSymbols, 578 ModuleSummaryIndex &Index) { 579 auto isExported = [&](StringRef ModuleIdentifier, GlobalValue::GUID GUID) { 580 const auto &ExportList = ExportLists.find(ModuleIdentifier); 581 return (ExportList != ExportLists.end() && 582 ExportList->second.count(GUID)) || 583 GUIDPreservedSymbols.count(GUID); 584 }; 585 586 thinLTOInternalizeAndPromoteInIndex(Index, isExported); 587 } 588 589 static void computeDeadSymbolsInIndex( 590 ModuleSummaryIndex &Index, 591 const DenseSet<GlobalValue::GUID> &GUIDPreservedSymbols) { 592 // We have no symbols resolution available. And can't do any better now in the 593 // case where the prevailing symbol is in a native object. It can be refined 594 // with linker information in the future. 595 auto isPrevailing = [&](GlobalValue::GUID G) { 596 return PrevailingType::Unknown; 597 }; 598 computeDeadSymbolsWithConstProp(Index, GUIDPreservedSymbols, isPrevailing, 599 /* ImportEnabled = */ true); 600 } 601 602 /** 603 * Perform promotion and renaming of exported internal functions. 604 * Index is updated to reflect linkage changes from weak resolution. 605 */ 606 void ThinLTOCodeGenerator::promote(Module &TheModule, ModuleSummaryIndex &Index, 607 const lto::InputFile &File) { 608 auto ModuleCount = Index.modulePaths().size(); 609 auto ModuleIdentifier = TheModule.getModuleIdentifier(); 610 611 // Collect for each module the list of function it defines (GUID -> Summary). 612 StringMap<GVSummaryMapTy> ModuleToDefinedGVSummaries; 613 Index.collectDefinedGVSummariesPerModule(ModuleToDefinedGVSummaries); 614 615 // Convert the preserved symbols set from string to GUID 616 auto GUIDPreservedSymbols = computeGUIDPreservedSymbols( 617 PreservedSymbols, Triple(TheModule.getTargetTriple())); 618 619 // Add used symbol to the preserved symbols. 620 addUsedSymbolToPreservedGUID(File, GUIDPreservedSymbols); 621 622 // Compute "dead" symbols, we don't want to import/export these! 623 computeDeadSymbolsInIndex(Index, GUIDPreservedSymbols); 624 625 // Generate import/export list 626 StringMap<FunctionImporter::ImportMapTy> ImportLists(ModuleCount); 627 StringMap<FunctionImporter::ExportSetTy> ExportLists(ModuleCount); 628 ComputeCrossModuleImport(Index, ModuleToDefinedGVSummaries, ImportLists, 629 ExportLists); 630 631 // Resolve prevailing symbols 632 StringMap<std::map<GlobalValue::GUID, GlobalValue::LinkageTypes>> ResolvedODR; 633 resolvePrevailingInIndex(Index, ResolvedODR); 634 635 thinLTOResolvePrevailingInModule( 636 TheModule, ModuleToDefinedGVSummaries[ModuleIdentifier]); 637 638 // Promote the exported values in the index, so that they are promoted 639 // in the module. 640 internalizeAndPromoteInIndex(ExportLists, GUIDPreservedSymbols, Index); 641 642 promoteModule(TheModule, Index); 643 } 644 645 /** 646 * Perform cross-module importing for the module identified by ModuleIdentifier. 647 */ 648 void ThinLTOCodeGenerator::crossModuleImport(Module &TheModule, 649 ModuleSummaryIndex &Index, 650 const lto::InputFile &File) { 651 auto ModuleMap = generateModuleMap(Modules); 652 auto ModuleCount = Index.modulePaths().size(); 653 654 // Collect for each module the list of function it defines (GUID -> Summary). 655 StringMap<GVSummaryMapTy> ModuleToDefinedGVSummaries(ModuleCount); 656 Index.collectDefinedGVSummariesPerModule(ModuleToDefinedGVSummaries); 657 658 // Convert the preserved symbols set from string to GUID 659 auto GUIDPreservedSymbols = computeGUIDPreservedSymbols( 660 PreservedSymbols, Triple(TheModule.getTargetTriple())); 661 662 addUsedSymbolToPreservedGUID(File, GUIDPreservedSymbols); 663 664 // Compute "dead" symbols, we don't want to import/export these! 665 computeDeadSymbolsInIndex(Index, GUIDPreservedSymbols); 666 667 // Generate import/export list 668 StringMap<FunctionImporter::ImportMapTy> ImportLists(ModuleCount); 669 StringMap<FunctionImporter::ExportSetTy> ExportLists(ModuleCount); 670 ComputeCrossModuleImport(Index, ModuleToDefinedGVSummaries, ImportLists, 671 ExportLists); 672 auto &ImportList = ImportLists[TheModule.getModuleIdentifier()]; 673 674 crossImportIntoModule(TheModule, Index, ModuleMap, ImportList); 675 } 676 677 /** 678 * Compute the list of summaries needed for importing into module. 679 */ 680 void ThinLTOCodeGenerator::gatherImportedSummariesForModule( 681 Module &TheModule, ModuleSummaryIndex &Index, 682 std::map<std::string, GVSummaryMapTy> &ModuleToSummariesForIndex, 683 const lto::InputFile &File) { 684 auto ModuleCount = Index.modulePaths().size(); 685 auto ModuleIdentifier = TheModule.getModuleIdentifier(); 686 687 // Collect for each module the list of function it defines (GUID -> Summary). 688 StringMap<GVSummaryMapTy> ModuleToDefinedGVSummaries(ModuleCount); 689 Index.collectDefinedGVSummariesPerModule(ModuleToDefinedGVSummaries); 690 691 // Convert the preserved symbols set from string to GUID 692 auto GUIDPreservedSymbols = computeGUIDPreservedSymbols( 693 PreservedSymbols, Triple(TheModule.getTargetTriple())); 694 695 addUsedSymbolToPreservedGUID(File, GUIDPreservedSymbols); 696 697 // Compute "dead" symbols, we don't want to import/export these! 698 computeDeadSymbolsInIndex(Index, GUIDPreservedSymbols); 699 700 // Generate import/export list 701 StringMap<FunctionImporter::ImportMapTy> ImportLists(ModuleCount); 702 StringMap<FunctionImporter::ExportSetTy> ExportLists(ModuleCount); 703 ComputeCrossModuleImport(Index, ModuleToDefinedGVSummaries, ImportLists, 704 ExportLists); 705 706 llvm::gatherImportedSummariesForModule( 707 ModuleIdentifier, ModuleToDefinedGVSummaries, 708 ImportLists[ModuleIdentifier], ModuleToSummariesForIndex); 709 } 710 711 /** 712 * Emit the list of files needed for importing into module. 713 */ 714 void ThinLTOCodeGenerator::emitImports(Module &TheModule, StringRef OutputName, 715 ModuleSummaryIndex &Index, 716 const lto::InputFile &File) { 717 auto ModuleCount = Index.modulePaths().size(); 718 auto ModuleIdentifier = TheModule.getModuleIdentifier(); 719 720 // Collect for each module the list of function it defines (GUID -> Summary). 721 StringMap<GVSummaryMapTy> ModuleToDefinedGVSummaries(ModuleCount); 722 Index.collectDefinedGVSummariesPerModule(ModuleToDefinedGVSummaries); 723 724 // Convert the preserved symbols set from string to GUID 725 auto GUIDPreservedSymbols = computeGUIDPreservedSymbols( 726 PreservedSymbols, Triple(TheModule.getTargetTriple())); 727 728 addUsedSymbolToPreservedGUID(File, GUIDPreservedSymbols); 729 730 // Compute "dead" symbols, we don't want to import/export these! 731 computeDeadSymbolsInIndex(Index, GUIDPreservedSymbols); 732 733 // Generate import/export list 734 StringMap<FunctionImporter::ImportMapTy> ImportLists(ModuleCount); 735 StringMap<FunctionImporter::ExportSetTy> ExportLists(ModuleCount); 736 ComputeCrossModuleImport(Index, ModuleToDefinedGVSummaries, ImportLists, 737 ExportLists); 738 739 std::map<std::string, GVSummaryMapTy> ModuleToSummariesForIndex; 740 llvm::gatherImportedSummariesForModule( 741 ModuleIdentifier, ModuleToDefinedGVSummaries, 742 ImportLists[ModuleIdentifier], ModuleToSummariesForIndex); 743 744 std::error_code EC; 745 if ((EC = EmitImportsFiles(ModuleIdentifier, OutputName, 746 ModuleToSummariesForIndex))) 747 report_fatal_error(Twine("Failed to open ") + OutputName + 748 " to save imports lists\n"); 749 } 750 751 /** 752 * Perform internalization. Runs promote and internalization together. 753 * Index is updated to reflect linkage changes. 754 */ 755 void ThinLTOCodeGenerator::internalize(Module &TheModule, 756 ModuleSummaryIndex &Index, 757 const lto::InputFile &File) { 758 initTMBuilder(TMBuilder, Triple(TheModule.getTargetTriple())); 759 auto ModuleCount = Index.modulePaths().size(); 760 auto ModuleIdentifier = TheModule.getModuleIdentifier(); 761 762 // Convert the preserved symbols set from string to GUID 763 auto GUIDPreservedSymbols = 764 computeGUIDPreservedSymbols(PreservedSymbols, TMBuilder.TheTriple); 765 766 addUsedSymbolToPreservedGUID(File, GUIDPreservedSymbols); 767 768 // Collect for each module the list of function it defines (GUID -> Summary). 769 StringMap<GVSummaryMapTy> ModuleToDefinedGVSummaries(ModuleCount); 770 Index.collectDefinedGVSummariesPerModule(ModuleToDefinedGVSummaries); 771 772 // Compute "dead" symbols, we don't want to import/export these! 773 computeDeadSymbolsInIndex(Index, GUIDPreservedSymbols); 774 775 // Generate import/export list 776 StringMap<FunctionImporter::ImportMapTy> ImportLists(ModuleCount); 777 StringMap<FunctionImporter::ExportSetTy> ExportLists(ModuleCount); 778 ComputeCrossModuleImport(Index, ModuleToDefinedGVSummaries, ImportLists, 779 ExportLists); 780 auto &ExportList = ExportLists[ModuleIdentifier]; 781 782 // Be friendly and don't nuke totally the module when the client didn't 783 // supply anything to preserve. 784 if (ExportList.empty() && GUIDPreservedSymbols.empty()) 785 return; 786 787 // Resolve prevailing symbols 788 StringMap<std::map<GlobalValue::GUID, GlobalValue::LinkageTypes>> ResolvedODR; 789 resolvePrevailingInIndex(Index, ResolvedODR); 790 791 // Promote the exported values in the index, so that they are promoted 792 // in the module. 793 internalizeAndPromoteInIndex(ExportLists, GUIDPreservedSymbols, Index); 794 795 promoteModule(TheModule, Index); 796 797 // Internalization 798 thinLTOResolvePrevailingInModule( 799 TheModule, ModuleToDefinedGVSummaries[ModuleIdentifier]); 800 801 thinLTOInternalizeModule(TheModule, 802 ModuleToDefinedGVSummaries[ModuleIdentifier]); 803 } 804 805 /** 806 * Perform post-importing ThinLTO optimizations. 807 */ 808 void ThinLTOCodeGenerator::optimize(Module &TheModule) { 809 initTMBuilder(TMBuilder, Triple(TheModule.getTargetTriple())); 810 811 // Optimize now 812 optimizeModule(TheModule, *TMBuilder.create(), OptLevel, Freestanding); 813 } 814 815 /// Write out the generated object file, either from CacheEntryPath or from 816 /// OutputBuffer, preferring hard-link when possible. 817 /// Returns the path to the generated file in SavedObjectsDirectoryPath. 818 static std::string writeGeneratedObject(int count, StringRef CacheEntryPath, 819 StringRef SavedObjectsDirectoryPath, 820 const MemoryBuffer &OutputBuffer) { 821 SmallString<128> OutputPath(SavedObjectsDirectoryPath); 822 llvm::sys::path::append(OutputPath, Twine(count) + ".thinlto.o"); 823 OutputPath.c_str(); // Ensure the string is null terminated. 824 if (sys::fs::exists(OutputPath)) 825 sys::fs::remove(OutputPath); 826 827 // We don't return a memory buffer to the linker, just a list of files. 828 if (!CacheEntryPath.empty()) { 829 // Cache is enabled, hard-link the entry (or copy if hard-link fails). 830 auto Err = sys::fs::create_hard_link(CacheEntryPath, OutputPath); 831 if (!Err) 832 return OutputPath.str(); 833 // Hard linking failed, try to copy. 834 Err = sys::fs::copy_file(CacheEntryPath, OutputPath); 835 if (!Err) 836 return OutputPath.str(); 837 // Copy failed (could be because the CacheEntry was removed from the cache 838 // in the meantime by another process), fall back and try to write down the 839 // buffer to the output. 840 errs() << "error: can't link or copy from cached entry '" << CacheEntryPath 841 << "' to '" << OutputPath << "'\n"; 842 } 843 // No cache entry, just write out the buffer. 844 std::error_code Err; 845 raw_fd_ostream OS(OutputPath, Err, sys::fs::F_None); 846 if (Err) 847 report_fatal_error("Can't open output '" + OutputPath + "'\n"); 848 OS << OutputBuffer.getBuffer(); 849 return OutputPath.str(); 850 } 851 852 // Main entry point for the ThinLTO processing 853 void ThinLTOCodeGenerator::run() { 854 // Prepare the resulting object vector 855 assert(ProducedBinaries.empty() && "The generator should not be reused"); 856 if (SavedObjectsDirectoryPath.empty()) 857 ProducedBinaries.resize(Modules.size()); 858 else { 859 sys::fs::create_directories(SavedObjectsDirectoryPath); 860 bool IsDir; 861 sys::fs::is_directory(SavedObjectsDirectoryPath, IsDir); 862 if (!IsDir) 863 report_fatal_error("Unexistent dir: '" + SavedObjectsDirectoryPath + "'"); 864 ProducedBinaryFiles.resize(Modules.size()); 865 } 866 867 if (CodeGenOnly) { 868 // Perform only parallel codegen and return. 869 ThreadPool Pool; 870 int count = 0; 871 for (auto &Mod : Modules) { 872 Pool.async([&](int count) { 873 LLVMContext Context; 874 Context.setDiscardValueNames(LTODiscardValueNames); 875 876 // Parse module now 877 auto TheModule = loadModuleFromInput(Mod.get(), Context, false, 878 /*IsImporting*/ false); 879 880 // CodeGen 881 auto OutputBuffer = codegenModule(*TheModule, *TMBuilder.create()); 882 if (SavedObjectsDirectoryPath.empty()) 883 ProducedBinaries[count] = std::move(OutputBuffer); 884 else 885 ProducedBinaryFiles[count] = writeGeneratedObject( 886 count, "", SavedObjectsDirectoryPath, *OutputBuffer); 887 }, count++); 888 } 889 890 return; 891 } 892 893 // Sequential linking phase 894 auto Index = linkCombinedIndex(); 895 896 // Save temps: index. 897 if (!SaveTempsDir.empty()) { 898 auto SaveTempPath = SaveTempsDir + "index.bc"; 899 std::error_code EC; 900 raw_fd_ostream OS(SaveTempPath, EC, sys::fs::F_None); 901 if (EC) 902 report_fatal_error(Twine("Failed to open ") + SaveTempPath + 903 " to save optimized bitcode\n"); 904 WriteIndexToFile(*Index, OS); 905 } 906 907 908 // Prepare the module map. 909 auto ModuleMap = generateModuleMap(Modules); 910 auto ModuleCount = Modules.size(); 911 912 // Collect for each module the list of function it defines (GUID -> Summary). 913 StringMap<GVSummaryMapTy> ModuleToDefinedGVSummaries(ModuleCount); 914 Index->collectDefinedGVSummariesPerModule(ModuleToDefinedGVSummaries); 915 916 // Convert the preserved symbols set from string to GUID, this is needed for 917 // computing the caching hash and the internalization. 918 auto GUIDPreservedSymbols = 919 computeGUIDPreservedSymbols(PreservedSymbols, TMBuilder.TheTriple); 920 921 // Add used symbol from inputs to the preserved symbols. 922 for (const auto &M : Modules) 923 addUsedSymbolToPreservedGUID(*M, GUIDPreservedSymbols); 924 925 // Compute "dead" symbols, we don't want to import/export these! 926 computeDeadSymbolsInIndex(*Index, GUIDPreservedSymbols); 927 928 // Synthesize entry counts for functions in the combined index. 929 computeSyntheticCounts(*Index); 930 931 // Collect the import/export lists for all modules from the call-graph in the 932 // combined index. 933 StringMap<FunctionImporter::ImportMapTy> ImportLists(ModuleCount); 934 StringMap<FunctionImporter::ExportSetTy> ExportLists(ModuleCount); 935 ComputeCrossModuleImport(*Index, ModuleToDefinedGVSummaries, ImportLists, 936 ExportLists); 937 938 // We use a std::map here to be able to have a defined ordering when 939 // producing a hash for the cache entry. 940 // FIXME: we should be able to compute the caching hash for the entry based 941 // on the index, and nuke this map. 942 StringMap<std::map<GlobalValue::GUID, GlobalValue::LinkageTypes>> ResolvedODR; 943 944 // Resolve prevailing symbols, this has to be computed early because it 945 // impacts the caching. 946 resolvePrevailingInIndex(*Index, ResolvedODR); 947 948 // Use global summary-based analysis to identify symbols that can be 949 // internalized (because they aren't exported or preserved as per callback). 950 // Changes are made in the index, consumed in the ThinLTO backends. 951 internalizeAndPromoteInIndex(ExportLists, GUIDPreservedSymbols, *Index); 952 953 // Make sure that every module has an entry in the ExportLists, ImportList, 954 // GVSummary and ResolvedODR maps to enable threaded access to these maps 955 // below. 956 for (auto &Module : Modules) { 957 auto ModuleIdentifier = Module->getName(); 958 ExportLists[ModuleIdentifier]; 959 ImportLists[ModuleIdentifier]; 960 ResolvedODR[ModuleIdentifier]; 961 ModuleToDefinedGVSummaries[ModuleIdentifier]; 962 } 963 964 // Compute the ordering we will process the inputs: the rough heuristic here 965 // is to sort them per size so that the largest module get schedule as soon as 966 // possible. This is purely a compile-time optimization. 967 std::vector<int> ModulesOrdering; 968 ModulesOrdering.resize(Modules.size()); 969 std::iota(ModulesOrdering.begin(), ModulesOrdering.end(), 0); 970 llvm::sort(ModulesOrdering, [&](int LeftIndex, int RightIndex) { 971 auto LSize = 972 Modules[LeftIndex]->getSingleBitcodeModule().getBuffer().size(); 973 auto RSize = 974 Modules[RightIndex]->getSingleBitcodeModule().getBuffer().size(); 975 return LSize > RSize; 976 }); 977 978 // Parallel optimizer + codegen 979 { 980 ThreadPool Pool(ThreadCount); 981 for (auto IndexCount : ModulesOrdering) { 982 auto &Mod = Modules[IndexCount]; 983 Pool.async([&](int count) { 984 auto ModuleIdentifier = Mod->getName(); 985 auto &ExportList = ExportLists[ModuleIdentifier]; 986 987 auto &DefinedGVSummaries = ModuleToDefinedGVSummaries[ModuleIdentifier]; 988 989 // The module may be cached, this helps handling it. 990 ModuleCacheEntry CacheEntry(CacheOptions.Path, *Index, ModuleIdentifier, 991 ImportLists[ModuleIdentifier], ExportList, 992 ResolvedODR[ModuleIdentifier], 993 DefinedGVSummaries, OptLevel, Freestanding, 994 TMBuilder); 995 auto CacheEntryPath = CacheEntry.getEntryPath(); 996 997 { 998 auto ErrOrBuffer = CacheEntry.tryLoadingBuffer(); 999 LLVM_DEBUG(dbgs() << "Cache " << (ErrOrBuffer ? "hit" : "miss") 1000 << " '" << CacheEntryPath << "' for buffer " 1001 << count << " " << ModuleIdentifier << "\n"); 1002 1003 if (ErrOrBuffer) { 1004 // Cache Hit! 1005 if (SavedObjectsDirectoryPath.empty()) 1006 ProducedBinaries[count] = std::move(ErrOrBuffer.get()); 1007 else 1008 ProducedBinaryFiles[count] = writeGeneratedObject( 1009 count, CacheEntryPath, SavedObjectsDirectoryPath, 1010 *ErrOrBuffer.get()); 1011 return; 1012 } 1013 } 1014 1015 LLVMContext Context; 1016 Context.setDiscardValueNames(LTODiscardValueNames); 1017 Context.enableDebugTypeODRUniquing(); 1018 auto DiagFileOrErr = lto::setupOptimizationRemarks( 1019 Context, LTORemarksFilename, LTORemarksPasses, 1020 LTOPassRemarksWithHotness, count); 1021 if (!DiagFileOrErr) { 1022 errs() << "Error: " << toString(DiagFileOrErr.takeError()) << "\n"; 1023 report_fatal_error("ThinLTO: Can't get an output file for the " 1024 "remarks"); 1025 } 1026 1027 // Parse module now 1028 auto TheModule = loadModuleFromInput(Mod.get(), Context, false, 1029 /*IsImporting*/ false); 1030 1031 // Save temps: original file. 1032 saveTempBitcode(*TheModule, SaveTempsDir, count, ".0.original.bc"); 1033 1034 auto &ImportList = ImportLists[ModuleIdentifier]; 1035 // Run the main process now, and generates a binary 1036 auto OutputBuffer = ProcessThinLTOModule( 1037 *TheModule, *Index, ModuleMap, *TMBuilder.create(), ImportList, 1038 ExportList, GUIDPreservedSymbols, 1039 ModuleToDefinedGVSummaries[ModuleIdentifier], CacheOptions, 1040 DisableCodeGen, SaveTempsDir, Freestanding, OptLevel, count); 1041 1042 // Commit to the cache (if enabled) 1043 CacheEntry.write(*OutputBuffer); 1044 1045 if (SavedObjectsDirectoryPath.empty()) { 1046 // We need to generated a memory buffer for the linker. 1047 if (!CacheEntryPath.empty()) { 1048 // When cache is enabled, reload from the cache if possible. 1049 // Releasing the buffer from the heap and reloading it from the 1050 // cache file with mmap helps us to lower memory pressure. 1051 // The freed memory can be used for the next input file. 1052 // The final binary link will read from the VFS cache (hopefully!) 1053 // or from disk (if the memory pressure was too high). 1054 auto ReloadedBufferOrErr = CacheEntry.tryLoadingBuffer(); 1055 if (auto EC = ReloadedBufferOrErr.getError()) { 1056 // On error, keep the preexisting buffer and print a diagnostic. 1057 errs() << "error: can't reload cached file '" << CacheEntryPath 1058 << "': " << EC.message() << "\n"; 1059 } else { 1060 OutputBuffer = std::move(*ReloadedBufferOrErr); 1061 } 1062 } 1063 ProducedBinaries[count] = std::move(OutputBuffer); 1064 return; 1065 } 1066 ProducedBinaryFiles[count] = writeGeneratedObject( 1067 count, CacheEntryPath, SavedObjectsDirectoryPath, *OutputBuffer); 1068 }, IndexCount); 1069 } 1070 } 1071 1072 pruneCache(CacheOptions.Path, CacheOptions.Policy); 1073 1074 // If statistics were requested, print them out now. 1075 if (llvm::AreStatisticsEnabled()) 1076 llvm::PrintStatistics(); 1077 reportAndResetTimings(); 1078 } 1079