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