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 const DenseSet<GlobalValue::GUID> &GUIDPreservedSymbols) { 462 463 DenseMap<GlobalValue::GUID, const GlobalValueSummary *> PrevailingCopy; 464 computePrevailingCopies(Index, PrevailingCopy); 465 466 auto isPrevailing = [&](GlobalValue::GUID GUID, const GlobalValueSummary *S) { 467 const auto &Prevailing = PrevailingCopy.find(GUID); 468 // Not in map means that there was only one copy, which must be prevailing. 469 if (Prevailing == PrevailingCopy.end()) 470 return true; 471 return Prevailing->second == S; 472 }; 473 474 auto recordNewLinkage = [&](StringRef ModuleIdentifier, 475 GlobalValue::GUID GUID, 476 GlobalValue::LinkageTypes NewLinkage) { 477 ResolvedODR[ModuleIdentifier][GUID] = NewLinkage; 478 }; 479 480 thinLTOResolvePrevailingInIndex(Index, isPrevailing, recordNewLinkage, 481 GUIDPreservedSymbols); 482 } 483 484 // Initialize the TargetMachine builder for a given Triple 485 static void initTMBuilder(TargetMachineBuilder &TMBuilder, 486 const Triple &TheTriple) { 487 // Set a default CPU for Darwin triples (copied from LTOCodeGenerator). 488 // FIXME this looks pretty terrible... 489 if (TMBuilder.MCpu.empty() && TheTriple.isOSDarwin()) { 490 if (TheTriple.getArch() == llvm::Triple::x86_64) 491 TMBuilder.MCpu = "core2"; 492 else if (TheTriple.getArch() == llvm::Triple::x86) 493 TMBuilder.MCpu = "yonah"; 494 else if (TheTriple.getArch() == llvm::Triple::aarch64) 495 TMBuilder.MCpu = "cyclone"; 496 } 497 TMBuilder.TheTriple = std::move(TheTriple); 498 } 499 500 } // end anonymous namespace 501 502 void ThinLTOCodeGenerator::addModule(StringRef Identifier, StringRef Data) { 503 MemoryBufferRef Buffer(Data, Identifier); 504 505 auto InputOrError = lto::InputFile::create(Buffer); 506 if (!InputOrError) 507 report_fatal_error("ThinLTO cannot create input file: " + 508 toString(InputOrError.takeError())); 509 510 auto TripleStr = (*InputOrError)->getTargetTriple(); 511 Triple TheTriple(TripleStr); 512 513 if (Modules.empty()) 514 initTMBuilder(TMBuilder, Triple(TheTriple)); 515 else if (TMBuilder.TheTriple != TheTriple) { 516 if (!TMBuilder.TheTriple.isCompatibleWith(TheTriple)) 517 report_fatal_error("ThinLTO modules with incompatible triples not " 518 "supported"); 519 initTMBuilder(TMBuilder, Triple(TMBuilder.TheTriple.merge(TheTriple))); 520 } 521 522 Modules.emplace_back(std::move(*InputOrError)); 523 } 524 525 void ThinLTOCodeGenerator::preserveSymbol(StringRef Name) { 526 PreservedSymbols.insert(Name); 527 } 528 529 void ThinLTOCodeGenerator::crossReferenceSymbol(StringRef Name) { 530 // FIXME: At the moment, we don't take advantage of this extra information, 531 // we're conservatively considering cross-references as preserved. 532 // CrossReferencedSymbols.insert(Name); 533 PreservedSymbols.insert(Name); 534 } 535 536 // TargetMachine factory 537 std::unique_ptr<TargetMachine> TargetMachineBuilder::create() const { 538 std::string ErrMsg; 539 const Target *TheTarget = 540 TargetRegistry::lookupTarget(TheTriple.str(), ErrMsg); 541 if (!TheTarget) { 542 report_fatal_error("Can't load target for this Triple: " + ErrMsg); 543 } 544 545 // Use MAttr as the default set of features. 546 SubtargetFeatures Features(MAttr); 547 Features.getDefaultSubtargetFeatures(TheTriple); 548 std::string FeatureStr = Features.getString(); 549 550 return std::unique_ptr<TargetMachine>( 551 TheTarget->createTargetMachine(TheTriple.str(), MCpu, FeatureStr, Options, 552 RelocModel, None, CGOptLevel)); 553 } 554 555 /** 556 * Produce the combined summary index from all the bitcode files: 557 * "thin-link". 558 */ 559 std::unique_ptr<ModuleSummaryIndex> ThinLTOCodeGenerator::linkCombinedIndex() { 560 std::unique_ptr<ModuleSummaryIndex> CombinedIndex = 561 llvm::make_unique<ModuleSummaryIndex>(/*HaveGVs=*/false); 562 uint64_t NextModuleId = 0; 563 for (auto &Mod : Modules) { 564 auto &M = Mod->getSingleBitcodeModule(); 565 if (Error Err = 566 M.readSummary(*CombinedIndex, Mod->getName(), NextModuleId++)) { 567 // FIXME diagnose 568 logAllUnhandledErrors( 569 std::move(Err), errs(), 570 "error: can't create module summary index for buffer: "); 571 return nullptr; 572 } 573 } 574 return CombinedIndex; 575 } 576 577 static void internalizeAndPromoteInIndex( 578 const StringMap<FunctionImporter::ExportSetTy> &ExportLists, 579 const DenseSet<GlobalValue::GUID> &GUIDPreservedSymbols, 580 ModuleSummaryIndex &Index) { 581 auto isExported = [&](StringRef ModuleIdentifier, GlobalValue::GUID GUID) { 582 const auto &ExportList = ExportLists.find(ModuleIdentifier); 583 return (ExportList != ExportLists.end() && 584 ExportList->second.count(GUID)) || 585 GUIDPreservedSymbols.count(GUID); 586 }; 587 588 thinLTOInternalizeAndPromoteInIndex(Index, isExported); 589 } 590 591 static void computeDeadSymbolsInIndex( 592 ModuleSummaryIndex &Index, 593 const DenseSet<GlobalValue::GUID> &GUIDPreservedSymbols) { 594 // We have no symbols resolution available. And can't do any better now in the 595 // case where the prevailing symbol is in a native object. It can be refined 596 // with linker information in the future. 597 auto isPrevailing = [&](GlobalValue::GUID G) { 598 return PrevailingType::Unknown; 599 }; 600 computeDeadSymbolsWithConstProp(Index, GUIDPreservedSymbols, isPrevailing, 601 /* ImportEnabled = */ true); 602 } 603 604 /** 605 * Perform promotion and renaming of exported internal functions. 606 * Index is updated to reflect linkage changes from weak resolution. 607 */ 608 void ThinLTOCodeGenerator::promote(Module &TheModule, ModuleSummaryIndex &Index, 609 const lto::InputFile &File) { 610 auto ModuleCount = Index.modulePaths().size(); 611 auto ModuleIdentifier = TheModule.getModuleIdentifier(); 612 613 // Collect for each module the list of function it defines (GUID -> Summary). 614 StringMap<GVSummaryMapTy> ModuleToDefinedGVSummaries; 615 Index.collectDefinedGVSummariesPerModule(ModuleToDefinedGVSummaries); 616 617 // Convert the preserved symbols set from string to GUID 618 auto GUIDPreservedSymbols = computeGUIDPreservedSymbols( 619 PreservedSymbols, Triple(TheModule.getTargetTriple())); 620 621 // Add used symbol to the preserved symbols. 622 addUsedSymbolToPreservedGUID(File, GUIDPreservedSymbols); 623 624 // Compute "dead" symbols, we don't want to import/export these! 625 computeDeadSymbolsInIndex(Index, GUIDPreservedSymbols); 626 627 // Generate import/export list 628 StringMap<FunctionImporter::ImportMapTy> ImportLists(ModuleCount); 629 StringMap<FunctionImporter::ExportSetTy> ExportLists(ModuleCount); 630 ComputeCrossModuleImport(Index, ModuleToDefinedGVSummaries, ImportLists, 631 ExportLists); 632 633 // Resolve prevailing symbols 634 StringMap<std::map<GlobalValue::GUID, GlobalValue::LinkageTypes>> ResolvedODR; 635 resolvePrevailingInIndex(Index, ResolvedODR, GUIDPreservedSymbols); 636 637 thinLTOResolvePrevailingInModule( 638 TheModule, ModuleToDefinedGVSummaries[ModuleIdentifier]); 639 640 // Promote the exported values in the index, so that they are promoted 641 // in the module. 642 internalizeAndPromoteInIndex(ExportLists, GUIDPreservedSymbols, Index); 643 644 promoteModule(TheModule, Index); 645 } 646 647 /** 648 * Perform cross-module importing for the module identified by ModuleIdentifier. 649 */ 650 void ThinLTOCodeGenerator::crossModuleImport(Module &TheModule, 651 ModuleSummaryIndex &Index, 652 const lto::InputFile &File) { 653 auto ModuleMap = generateModuleMap(Modules); 654 auto ModuleCount = Index.modulePaths().size(); 655 656 // Collect for each module the list of function it defines (GUID -> Summary). 657 StringMap<GVSummaryMapTy> ModuleToDefinedGVSummaries(ModuleCount); 658 Index.collectDefinedGVSummariesPerModule(ModuleToDefinedGVSummaries); 659 660 // Convert the preserved symbols set from string to GUID 661 auto GUIDPreservedSymbols = computeGUIDPreservedSymbols( 662 PreservedSymbols, Triple(TheModule.getTargetTriple())); 663 664 addUsedSymbolToPreservedGUID(File, GUIDPreservedSymbols); 665 666 // Compute "dead" symbols, we don't want to import/export these! 667 computeDeadSymbolsInIndex(Index, GUIDPreservedSymbols); 668 669 // Generate import/export list 670 StringMap<FunctionImporter::ImportMapTy> ImportLists(ModuleCount); 671 StringMap<FunctionImporter::ExportSetTy> ExportLists(ModuleCount); 672 ComputeCrossModuleImport(Index, ModuleToDefinedGVSummaries, ImportLists, 673 ExportLists); 674 auto &ImportList = ImportLists[TheModule.getModuleIdentifier()]; 675 676 crossImportIntoModule(TheModule, Index, ModuleMap, ImportList); 677 } 678 679 /** 680 * Compute the list of summaries needed for importing into module. 681 */ 682 void ThinLTOCodeGenerator::gatherImportedSummariesForModule( 683 Module &TheModule, ModuleSummaryIndex &Index, 684 std::map<std::string, GVSummaryMapTy> &ModuleToSummariesForIndex, 685 const lto::InputFile &File) { 686 auto ModuleCount = Index.modulePaths().size(); 687 auto ModuleIdentifier = TheModule.getModuleIdentifier(); 688 689 // Collect for each module the list of function it defines (GUID -> Summary). 690 StringMap<GVSummaryMapTy> ModuleToDefinedGVSummaries(ModuleCount); 691 Index.collectDefinedGVSummariesPerModule(ModuleToDefinedGVSummaries); 692 693 // Convert the preserved symbols set from string to GUID 694 auto GUIDPreservedSymbols = computeGUIDPreservedSymbols( 695 PreservedSymbols, Triple(TheModule.getTargetTriple())); 696 697 addUsedSymbolToPreservedGUID(File, GUIDPreservedSymbols); 698 699 // Compute "dead" symbols, we don't want to import/export these! 700 computeDeadSymbolsInIndex(Index, GUIDPreservedSymbols); 701 702 // Generate import/export list 703 StringMap<FunctionImporter::ImportMapTy> ImportLists(ModuleCount); 704 StringMap<FunctionImporter::ExportSetTy> ExportLists(ModuleCount); 705 ComputeCrossModuleImport(Index, ModuleToDefinedGVSummaries, ImportLists, 706 ExportLists); 707 708 llvm::gatherImportedSummariesForModule( 709 ModuleIdentifier, ModuleToDefinedGVSummaries, 710 ImportLists[ModuleIdentifier], ModuleToSummariesForIndex); 711 } 712 713 /** 714 * Emit the list of files needed for importing into module. 715 */ 716 void ThinLTOCodeGenerator::emitImports(Module &TheModule, StringRef OutputName, 717 ModuleSummaryIndex &Index, 718 const lto::InputFile &File) { 719 auto ModuleCount = Index.modulePaths().size(); 720 auto ModuleIdentifier = TheModule.getModuleIdentifier(); 721 722 // Collect for each module the list of function it defines (GUID -> Summary). 723 StringMap<GVSummaryMapTy> ModuleToDefinedGVSummaries(ModuleCount); 724 Index.collectDefinedGVSummariesPerModule(ModuleToDefinedGVSummaries); 725 726 // Convert the preserved symbols set from string to GUID 727 auto GUIDPreservedSymbols = computeGUIDPreservedSymbols( 728 PreservedSymbols, Triple(TheModule.getTargetTriple())); 729 730 addUsedSymbolToPreservedGUID(File, GUIDPreservedSymbols); 731 732 // Compute "dead" symbols, we don't want to import/export these! 733 computeDeadSymbolsInIndex(Index, GUIDPreservedSymbols); 734 735 // Generate import/export list 736 StringMap<FunctionImporter::ImportMapTy> ImportLists(ModuleCount); 737 StringMap<FunctionImporter::ExportSetTy> ExportLists(ModuleCount); 738 ComputeCrossModuleImport(Index, ModuleToDefinedGVSummaries, ImportLists, 739 ExportLists); 740 741 std::map<std::string, GVSummaryMapTy> ModuleToSummariesForIndex; 742 llvm::gatherImportedSummariesForModule( 743 ModuleIdentifier, ModuleToDefinedGVSummaries, 744 ImportLists[ModuleIdentifier], ModuleToSummariesForIndex); 745 746 std::error_code EC; 747 if ((EC = EmitImportsFiles(ModuleIdentifier, OutputName, 748 ModuleToSummariesForIndex))) 749 report_fatal_error(Twine("Failed to open ") + OutputName + 750 " to save imports lists\n"); 751 } 752 753 /** 754 * Perform internalization. Runs promote and internalization together. 755 * Index is updated to reflect linkage changes. 756 */ 757 void ThinLTOCodeGenerator::internalize(Module &TheModule, 758 ModuleSummaryIndex &Index, 759 const lto::InputFile &File) { 760 initTMBuilder(TMBuilder, Triple(TheModule.getTargetTriple())); 761 auto ModuleCount = Index.modulePaths().size(); 762 auto ModuleIdentifier = TheModule.getModuleIdentifier(); 763 764 // Convert the preserved symbols set from string to GUID 765 auto GUIDPreservedSymbols = 766 computeGUIDPreservedSymbols(PreservedSymbols, TMBuilder.TheTriple); 767 768 addUsedSymbolToPreservedGUID(File, GUIDPreservedSymbols); 769 770 // Collect for each module the list of function it defines (GUID -> Summary). 771 StringMap<GVSummaryMapTy> ModuleToDefinedGVSummaries(ModuleCount); 772 Index.collectDefinedGVSummariesPerModule(ModuleToDefinedGVSummaries); 773 774 // Compute "dead" symbols, we don't want to import/export these! 775 computeDeadSymbolsInIndex(Index, GUIDPreservedSymbols); 776 777 // Generate import/export list 778 StringMap<FunctionImporter::ImportMapTy> ImportLists(ModuleCount); 779 StringMap<FunctionImporter::ExportSetTy> ExportLists(ModuleCount); 780 ComputeCrossModuleImport(Index, ModuleToDefinedGVSummaries, ImportLists, 781 ExportLists); 782 auto &ExportList = ExportLists[ModuleIdentifier]; 783 784 // Be friendly and don't nuke totally the module when the client didn't 785 // supply anything to preserve. 786 if (ExportList.empty() && GUIDPreservedSymbols.empty()) 787 return; 788 789 // Resolve prevailing symbols 790 StringMap<std::map<GlobalValue::GUID, GlobalValue::LinkageTypes>> ResolvedODR; 791 resolvePrevailingInIndex(Index, ResolvedODR, GUIDPreservedSymbols); 792 793 // Promote the exported values in the index, so that they are promoted 794 // in the module. 795 internalizeAndPromoteInIndex(ExportLists, GUIDPreservedSymbols, Index); 796 797 promoteModule(TheModule, Index); 798 799 // Internalization 800 thinLTOResolvePrevailingInModule( 801 TheModule, ModuleToDefinedGVSummaries[ModuleIdentifier]); 802 803 thinLTOInternalizeModule(TheModule, 804 ModuleToDefinedGVSummaries[ModuleIdentifier]); 805 } 806 807 /** 808 * Perform post-importing ThinLTO optimizations. 809 */ 810 void ThinLTOCodeGenerator::optimize(Module &TheModule) { 811 initTMBuilder(TMBuilder, Triple(TheModule.getTargetTriple())); 812 813 // Optimize now 814 optimizeModule(TheModule, *TMBuilder.create(), OptLevel, Freestanding); 815 } 816 817 /// Write out the generated object file, either from CacheEntryPath or from 818 /// OutputBuffer, preferring hard-link when possible. 819 /// Returns the path to the generated file in SavedObjectsDirectoryPath. 820 std::string 821 ThinLTOCodeGenerator::writeGeneratedObject(int count, StringRef CacheEntryPath, 822 const MemoryBuffer &OutputBuffer) { 823 auto ArchName = TMBuilder.TheTriple.getArchName(); 824 SmallString<128> OutputPath(SavedObjectsDirectoryPath); 825 llvm::sys::path::append(OutputPath, 826 Twine(count) + "." + ArchName + ".thinlto.o"); 827 OutputPath.c_str(); // Ensure the string is null terminated. 828 if (sys::fs::exists(OutputPath)) 829 sys::fs::remove(OutputPath); 830 831 // We don't return a memory buffer to the linker, just a list of files. 832 if (!CacheEntryPath.empty()) { 833 // Cache is enabled, hard-link the entry (or copy if hard-link fails). 834 auto Err = sys::fs::create_hard_link(CacheEntryPath, OutputPath); 835 if (!Err) 836 return OutputPath.str(); 837 // Hard linking failed, try to copy. 838 Err = sys::fs::copy_file(CacheEntryPath, OutputPath); 839 if (!Err) 840 return OutputPath.str(); 841 // Copy failed (could be because the CacheEntry was removed from the cache 842 // in the meantime by another process), fall back and try to write down the 843 // buffer to the output. 844 errs() << "error: can't link or copy from cached entry '" << CacheEntryPath 845 << "' to '" << OutputPath << "'\n"; 846 } 847 // No cache entry, just write out the buffer. 848 std::error_code Err; 849 raw_fd_ostream OS(OutputPath, Err, sys::fs::F_None); 850 if (Err) 851 report_fatal_error("Can't open output '" + OutputPath + "'\n"); 852 OS << OutputBuffer.getBuffer(); 853 return OutputPath.str(); 854 } 855 856 // Main entry point for the ThinLTO processing 857 void ThinLTOCodeGenerator::run() { 858 // Prepare the resulting object vector 859 assert(ProducedBinaries.empty() && "The generator should not be reused"); 860 if (SavedObjectsDirectoryPath.empty()) 861 ProducedBinaries.resize(Modules.size()); 862 else { 863 sys::fs::create_directories(SavedObjectsDirectoryPath); 864 bool IsDir; 865 sys::fs::is_directory(SavedObjectsDirectoryPath, IsDir); 866 if (!IsDir) 867 report_fatal_error("Unexistent dir: '" + SavedObjectsDirectoryPath + "'"); 868 ProducedBinaryFiles.resize(Modules.size()); 869 } 870 871 if (CodeGenOnly) { 872 // Perform only parallel codegen and return. 873 ThreadPool Pool; 874 int count = 0; 875 for (auto &Mod : Modules) { 876 Pool.async([&](int count) { 877 LLVMContext Context; 878 Context.setDiscardValueNames(LTODiscardValueNames); 879 880 // Parse module now 881 auto TheModule = loadModuleFromInput(Mod.get(), Context, false, 882 /*IsImporting*/ false); 883 884 // CodeGen 885 auto OutputBuffer = codegenModule(*TheModule, *TMBuilder.create()); 886 if (SavedObjectsDirectoryPath.empty()) 887 ProducedBinaries[count] = std::move(OutputBuffer); 888 else 889 ProducedBinaryFiles[count] = 890 writeGeneratedObject(count, "", *OutputBuffer); 891 }, count++); 892 } 893 894 return; 895 } 896 897 // Sequential linking phase 898 auto Index = linkCombinedIndex(); 899 900 // Save temps: index. 901 if (!SaveTempsDir.empty()) { 902 auto SaveTempPath = SaveTempsDir + "index.bc"; 903 std::error_code EC; 904 raw_fd_ostream OS(SaveTempPath, EC, sys::fs::F_None); 905 if (EC) 906 report_fatal_error(Twine("Failed to open ") + SaveTempPath + 907 " to save optimized bitcode\n"); 908 WriteIndexToFile(*Index, OS); 909 } 910 911 912 // Prepare the module map. 913 auto ModuleMap = generateModuleMap(Modules); 914 auto ModuleCount = Modules.size(); 915 916 // Collect for each module the list of function it defines (GUID -> Summary). 917 StringMap<GVSummaryMapTy> ModuleToDefinedGVSummaries(ModuleCount); 918 Index->collectDefinedGVSummariesPerModule(ModuleToDefinedGVSummaries); 919 920 // Convert the preserved symbols set from string to GUID, this is needed for 921 // computing the caching hash and the internalization. 922 auto GUIDPreservedSymbols = 923 computeGUIDPreservedSymbols(PreservedSymbols, TMBuilder.TheTriple); 924 925 // Add used symbol from inputs to the preserved symbols. 926 for (const auto &M : Modules) 927 addUsedSymbolToPreservedGUID(*M, GUIDPreservedSymbols); 928 929 // Compute "dead" symbols, we don't want to import/export these! 930 computeDeadSymbolsInIndex(*Index, GUIDPreservedSymbols); 931 932 // Synthesize entry counts for functions in the combined index. 933 computeSyntheticCounts(*Index); 934 935 // Collect the import/export lists for all modules from the call-graph in the 936 // combined index. 937 StringMap<FunctionImporter::ImportMapTy> ImportLists(ModuleCount); 938 StringMap<FunctionImporter::ExportSetTy> ExportLists(ModuleCount); 939 ComputeCrossModuleImport(*Index, ModuleToDefinedGVSummaries, ImportLists, 940 ExportLists); 941 942 // We use a std::map here to be able to have a defined ordering when 943 // producing a hash for the cache entry. 944 // FIXME: we should be able to compute the caching hash for the entry based 945 // on the index, and nuke this map. 946 StringMap<std::map<GlobalValue::GUID, GlobalValue::LinkageTypes>> ResolvedODR; 947 948 // Resolve prevailing symbols, this has to be computed early because it 949 // impacts the caching. 950 resolvePrevailingInIndex(*Index, ResolvedODR, GUIDPreservedSymbols); 951 952 // Use global summary-based analysis to identify symbols that can be 953 // internalized (because they aren't exported or preserved as per callback). 954 // Changes are made in the index, consumed in the ThinLTO backends. 955 internalizeAndPromoteInIndex(ExportLists, GUIDPreservedSymbols, *Index); 956 957 // Make sure that every module has an entry in the ExportLists, ImportList, 958 // GVSummary and ResolvedODR maps to enable threaded access to these maps 959 // below. 960 for (auto &Module : Modules) { 961 auto ModuleIdentifier = Module->getName(); 962 ExportLists[ModuleIdentifier]; 963 ImportLists[ModuleIdentifier]; 964 ResolvedODR[ModuleIdentifier]; 965 ModuleToDefinedGVSummaries[ModuleIdentifier]; 966 } 967 968 // Compute the ordering we will process the inputs: the rough heuristic here 969 // is to sort them per size so that the largest module get schedule as soon as 970 // possible. This is purely a compile-time optimization. 971 std::vector<int> ModulesOrdering; 972 ModulesOrdering.resize(Modules.size()); 973 std::iota(ModulesOrdering.begin(), ModulesOrdering.end(), 0); 974 llvm::sort(ModulesOrdering, [&](int LeftIndex, int RightIndex) { 975 auto LSize = 976 Modules[LeftIndex]->getSingleBitcodeModule().getBuffer().size(); 977 auto RSize = 978 Modules[RightIndex]->getSingleBitcodeModule().getBuffer().size(); 979 return LSize > RSize; 980 }); 981 982 // Parallel optimizer + codegen 983 { 984 ThreadPool Pool(ThreadCount); 985 for (auto IndexCount : ModulesOrdering) { 986 auto &Mod = Modules[IndexCount]; 987 Pool.async([&](int count) { 988 auto ModuleIdentifier = Mod->getName(); 989 auto &ExportList = ExportLists[ModuleIdentifier]; 990 991 auto &DefinedGVSummaries = ModuleToDefinedGVSummaries[ModuleIdentifier]; 992 993 // The module may be cached, this helps handling it. 994 ModuleCacheEntry CacheEntry(CacheOptions.Path, *Index, ModuleIdentifier, 995 ImportLists[ModuleIdentifier], ExportList, 996 ResolvedODR[ModuleIdentifier], 997 DefinedGVSummaries, OptLevel, Freestanding, 998 TMBuilder); 999 auto CacheEntryPath = CacheEntry.getEntryPath(); 1000 1001 { 1002 auto ErrOrBuffer = CacheEntry.tryLoadingBuffer(); 1003 LLVM_DEBUG(dbgs() << "Cache " << (ErrOrBuffer ? "hit" : "miss") 1004 << " '" << CacheEntryPath << "' for buffer " 1005 << count << " " << ModuleIdentifier << "\n"); 1006 1007 if (ErrOrBuffer) { 1008 // Cache Hit! 1009 if (SavedObjectsDirectoryPath.empty()) 1010 ProducedBinaries[count] = std::move(ErrOrBuffer.get()); 1011 else 1012 ProducedBinaryFiles[count] = writeGeneratedObject( 1013 count, CacheEntryPath, *ErrOrBuffer.get()); 1014 return; 1015 } 1016 } 1017 1018 LLVMContext Context; 1019 Context.setDiscardValueNames(LTODiscardValueNames); 1020 Context.enableDebugTypeODRUniquing(); 1021 auto DiagFileOrErr = lto::setupOptimizationRemarks( 1022 Context, LTORemarksFilename, LTORemarksPasses, 1023 LTOPassRemarksWithHotness, count); 1024 if (!DiagFileOrErr) { 1025 errs() << "Error: " << toString(DiagFileOrErr.takeError()) << "\n"; 1026 report_fatal_error("ThinLTO: Can't get an output file for the " 1027 "remarks"); 1028 } 1029 1030 // Parse module now 1031 auto TheModule = loadModuleFromInput(Mod.get(), Context, false, 1032 /*IsImporting*/ false); 1033 1034 // Save temps: original file. 1035 saveTempBitcode(*TheModule, SaveTempsDir, count, ".0.original.bc"); 1036 1037 auto &ImportList = ImportLists[ModuleIdentifier]; 1038 // Run the main process now, and generates a binary 1039 auto OutputBuffer = ProcessThinLTOModule( 1040 *TheModule, *Index, ModuleMap, *TMBuilder.create(), ImportList, 1041 ExportList, GUIDPreservedSymbols, 1042 ModuleToDefinedGVSummaries[ModuleIdentifier], CacheOptions, 1043 DisableCodeGen, SaveTempsDir, Freestanding, OptLevel, count); 1044 1045 // Commit to the cache (if enabled) 1046 CacheEntry.write(*OutputBuffer); 1047 1048 if (SavedObjectsDirectoryPath.empty()) { 1049 // We need to generated a memory buffer for the linker. 1050 if (!CacheEntryPath.empty()) { 1051 // When cache is enabled, reload from the cache if possible. 1052 // Releasing the buffer from the heap and reloading it from the 1053 // cache file with mmap helps us to lower memory pressure. 1054 // The freed memory can be used for the next input file. 1055 // The final binary link will read from the VFS cache (hopefully!) 1056 // or from disk (if the memory pressure was too high). 1057 auto ReloadedBufferOrErr = CacheEntry.tryLoadingBuffer(); 1058 if (auto EC = ReloadedBufferOrErr.getError()) { 1059 // On error, keep the preexisting buffer and print a diagnostic. 1060 errs() << "error: can't reload cached file '" << CacheEntryPath 1061 << "': " << EC.message() << "\n"; 1062 } else { 1063 OutputBuffer = std::move(*ReloadedBufferOrErr); 1064 } 1065 } 1066 ProducedBinaries[count] = std::move(OutputBuffer); 1067 return; 1068 } 1069 ProducedBinaryFiles[count] = writeGeneratedObject( 1070 count, CacheEntryPath, *OutputBuffer); 1071 }, IndexCount); 1072 } 1073 } 1074 1075 pruneCache(CacheOptions.Path, CacheOptions.Policy); 1076 1077 // If statistics were requested, print them out now. 1078 if (llvm::AreStatisticsEnabled()) 1079 llvm::PrintStatistics(); 1080 reportAndResetTimings(); 1081 } 1082