1 //===-ThinLTOCodeGenerator.cpp - LLVM Link Time Optimizer -----------------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
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.
13 //===----------------------------------------------------------------------===//
15 #include "llvm/LTO/legacy/ThinLTOCodeGenerator.h"
17 #ifdef HAVE_LLVM_REVISION
18 #include "LLVMLTORevision.h"
21 #include "llvm/ADT/Statistic.h"
22 #include "llvm/ADT/StringExtras.h"
23 #include "llvm/Analysis/ModuleSummaryAnalysis.h"
24 #include "llvm/Analysis/ProfileSummaryInfo.h"
25 #include "llvm/Analysis/TargetLibraryInfo.h"
26 #include "llvm/Analysis/TargetTransformInfo.h"
27 #include "llvm/Bitcode/BitcodeReader.h"
28 #include "llvm/Bitcode/BitcodeWriter.h"
29 #include "llvm/Bitcode/BitcodeWriterPass.h"
30 #include "llvm/ExecutionEngine/ObjectMemoryBuffer.h"
31 #include "llvm/IR/DiagnosticPrinter.h"
32 #include "llvm/IR/LLVMContext.h"
33 #include "llvm/IR/LegacyPassManager.h"
34 #include "llvm/IR/Mangler.h"
35 #include "llvm/IRReader/IRReader.h"
36 #include "llvm/LTO/LTO.h"
37 #include "llvm/Linker/Linker.h"
38 #include "llvm/MC/SubtargetFeature.h"
39 #include "llvm/Object/IRObjectFile.h"
40 #include "llvm/Object/ModuleSummaryIndexObjectFile.h"
41 #include "llvm/Support/CachePruning.h"
42 #include "llvm/Support/Debug.h"
43 #include "llvm/Support/Error.h"
44 #include "llvm/Support/Path.h"
45 #include "llvm/Support/SHA1.h"
46 #include "llvm/Support/TargetRegistry.h"
47 #include "llvm/Support/ThreadPool.h"
48 #include "llvm/Support/Threading.h"
49 #include "llvm/Support/ToolOutputFile.h"
50 #include "llvm/Target/TargetMachine.h"
51 #include "llvm/Transforms/IPO.h"
52 #include "llvm/Transforms/IPO/FunctionImport.h"
53 #include "llvm/Transforms/IPO/Internalize.h"
54 #include "llvm/Transforms/IPO/PassManagerBuilder.h"
55 #include "llvm/Transforms/ObjCARC.h"
56 #include "llvm/Transforms/Utils/FunctionImportUtils.h"
62 #define DEBUG_TYPE "thinlto"
65 // Flags -discard-value-names, defined in LTOCodeGenerator.cpp
66 extern cl::opt<bool> LTODiscardValueNames;
67 extern cl::opt<std::string> LTORemarksFilename;
68 extern cl::opt<bool> LTOPassRemarksWithHotness;
74 ThreadCount("threads", cl::init(llvm::heavyweight_hardware_concurrency()));
76 Expected<std::unique_ptr<tool_output_file>>
77 setupOptimizationRemarks(LLVMContext &Ctx, int Count) {
78 if (LTOPassRemarksWithHotness)
79 Ctx.setDiagnosticHotnessRequested(true);
81 if (LTORemarksFilename.empty())
84 std::string FileName =
85 LTORemarksFilename + ".thin." + llvm::utostr(Count) + ".yaml";
87 auto DiagnosticOutputFile =
88 llvm::make_unique<tool_output_file>(FileName, EC, sys::fs::F_None);
90 return errorCodeToError(EC);
91 Ctx.setDiagnosticsOutputFile(
92 llvm::make_unique<yaml::Output>(DiagnosticOutputFile->os()));
93 DiagnosticOutputFile->keep();
94 return std::move(DiagnosticOutputFile);
97 // Simple helper to save temporary files for debug.
98 static void saveTempBitcode(const Module &TheModule, StringRef TempDir,
99 unsigned count, StringRef Suffix) {
102 // User asked to save temps, let dump the bitcode file after import.
103 std::string SaveTempPath = (TempDir + llvm::utostr(count) + Suffix).str();
105 raw_fd_ostream OS(SaveTempPath, EC, sys::fs::F_None);
107 report_fatal_error(Twine("Failed to open ") + SaveTempPath +
108 " to save optimized bitcode\n");
109 WriteBitcodeToFile(&TheModule, OS, /* ShouldPreserveUseListOrder */ true);
112 static const GlobalValueSummary *
113 getFirstDefinitionForLinker(const GlobalValueSummaryList &GVSummaryList) {
114 // If there is any strong definition anywhere, get it.
115 auto StrongDefForLinker = llvm::find_if(
116 GVSummaryList, [](const std::unique_ptr<GlobalValueSummary> &Summary) {
117 auto Linkage = Summary->linkage();
118 return !GlobalValue::isAvailableExternallyLinkage(Linkage) &&
119 !GlobalValue::isWeakForLinker(Linkage);
121 if (StrongDefForLinker != GVSummaryList.end())
122 return StrongDefForLinker->get();
123 // Get the first *linker visible* definition for this global in the summary
125 auto FirstDefForLinker = llvm::find_if(
126 GVSummaryList, [](const std::unique_ptr<GlobalValueSummary> &Summary) {
127 auto Linkage = Summary->linkage();
128 return !GlobalValue::isAvailableExternallyLinkage(Linkage);
130 // Extern templates can be emitted as available_externally.
131 if (FirstDefForLinker == GVSummaryList.end())
133 return FirstDefForLinker->get();
136 // Populate map of GUID to the prevailing copy for any multiply defined
137 // symbols. Currently assume first copy is prevailing, or any strong
138 // definition. Can be refined with Linker information in the future.
139 static void computePrevailingCopies(
140 const ModuleSummaryIndex &Index,
141 DenseMap<GlobalValue::GUID, const GlobalValueSummary *> &PrevailingCopy) {
142 auto HasMultipleCopies = [&](const GlobalValueSummaryList &GVSummaryList) {
143 return GVSummaryList.size() > 1;
146 for (auto &I : Index) {
147 if (HasMultipleCopies(I.second))
148 PrevailingCopy[I.first] = getFirstDefinitionForLinker(I.second);
152 static StringMap<MemoryBufferRef>
153 generateModuleMap(const std::vector<MemoryBufferRef> &Modules) {
154 StringMap<MemoryBufferRef> ModuleMap;
155 for (auto &ModuleBuffer : Modules) {
156 assert(ModuleMap.find(ModuleBuffer.getBufferIdentifier()) ==
158 "Expect unique Buffer Identifier");
159 ModuleMap[ModuleBuffer.getBufferIdentifier()] = ModuleBuffer;
164 static void promoteModule(Module &TheModule, const ModuleSummaryIndex &Index) {
165 if (renameModuleForThinLTO(TheModule, Index))
166 report_fatal_error("renameModuleForThinLTO failed");
169 static std::unique_ptr<Module>
170 loadModuleFromBuffer(const MemoryBufferRef &Buffer, LLVMContext &Context,
171 bool Lazy, bool IsImporting) {
173 Expected<std::unique_ptr<Module>> ModuleOrErr =
175 ? getLazyBitcodeModule(Buffer, Context,
176 /* ShouldLazyLoadMetadata */ true, IsImporting)
177 : parseBitcodeFile(Buffer, Context);
179 handleAllErrors(ModuleOrErr.takeError(), [&](ErrorInfoBase &EIB) {
180 SMDiagnostic Err = SMDiagnostic(Buffer.getBufferIdentifier(),
181 SourceMgr::DK_Error, EIB.message());
182 Err.print("ThinLTO", errs());
184 report_fatal_error("Can't load module, abort.");
186 return std::move(ModuleOrErr.get());
190 crossImportIntoModule(Module &TheModule, const ModuleSummaryIndex &Index,
191 StringMap<MemoryBufferRef> &ModuleMap,
192 const FunctionImporter::ImportMapTy &ImportList) {
193 auto Loader = [&](StringRef Identifier) {
194 return loadModuleFromBuffer(ModuleMap[Identifier], TheModule.getContext(),
195 /*Lazy=*/true, /*IsImporting*/ true);
198 FunctionImporter Importer(Index, Loader);
199 Expected<bool> Result = Importer.importFunctions(TheModule, ImportList);
201 handleAllErrors(Result.takeError(), [&](ErrorInfoBase &EIB) {
202 SMDiagnostic Err = SMDiagnostic(TheModule.getModuleIdentifier(),
203 SourceMgr::DK_Error, EIB.message());
204 Err.print("ThinLTO", errs());
206 report_fatal_error("importFunctions failed");
210 static void optimizeModule(Module &TheModule, TargetMachine &TM,
212 // Populate the PassManager
213 PassManagerBuilder PMB;
214 PMB.LibraryInfo = new TargetLibraryInfoImpl(TM.getTargetTriple());
215 PMB.Inliner = createFunctionInliningPass();
216 // FIXME: should get it from the bitcode?
217 PMB.OptLevel = OptLevel;
218 PMB.LoopVectorize = true;
219 PMB.SLPVectorize = true;
220 PMB.VerifyInput = true;
221 PMB.VerifyOutput = false;
223 legacy::PassManager PM;
225 // Add the TTI (required to inform the vectorizer about register size for
227 PM.add(createTargetTransformInfoWrapperPass(TM.getTargetIRAnalysis()));
230 PMB.populateThinLTOPassManager(PM);
235 // Convert the PreservedSymbols map from "Name" based to "GUID" based.
236 static DenseSet<GlobalValue::GUID>
237 computeGUIDPreservedSymbols(const StringSet<> &PreservedSymbols,
238 const Triple &TheTriple) {
239 DenseSet<GlobalValue::GUID> GUIDPreservedSymbols(PreservedSymbols.size());
240 for (auto &Entry : PreservedSymbols) {
241 StringRef Name = Entry.first();
242 if (TheTriple.isOSBinFormatMachO() && Name.size() > 0 && Name[0] == '_')
243 Name = Name.drop_front();
244 GUIDPreservedSymbols.insert(GlobalValue::getGUID(Name));
246 return GUIDPreservedSymbols;
249 std::unique_ptr<MemoryBuffer> codegenModule(Module &TheModule,
251 SmallVector<char, 128> OutputBuffer;
255 raw_svector_ostream OS(OutputBuffer);
256 legacy::PassManager PM;
258 // If the bitcode files contain ARC code and were compiled with optimization,
259 // the ObjCARCContractPass must be run, so do it unconditionally here.
260 PM.add(createObjCARCContractPass());
262 // Setup the codegen now.
263 if (TM.addPassesToEmitFile(PM, OS, TargetMachine::CGFT_ObjectFile,
264 /* DisableVerify */ true))
265 report_fatal_error("Failed to setup codegen");
267 // Run codegen now. resulting binary is in OutputBuffer.
270 return make_unique<ObjectMemoryBuffer>(std::move(OutputBuffer));
273 /// Manage caching for a single Module.
274 class ModuleCacheEntry {
275 SmallString<128> EntryPath;
278 // Create a cache entry. This compute a unique hash for the Module considering
279 // the current list of export/import, and offer an interface to query to
280 // access the content in the cache.
282 StringRef CachePath, const ModuleSummaryIndex &Index, StringRef ModuleID,
283 const FunctionImporter::ImportMapTy &ImportList,
284 const FunctionImporter::ExportSetTy &ExportList,
285 const std::map<GlobalValue::GUID, GlobalValue::LinkageTypes> &ResolvedODR,
286 const GVSummaryMapTy &DefinedFunctions,
287 const DenseSet<GlobalValue::GUID> &PreservedSymbols, unsigned OptLevel,
288 const TargetMachineBuilder &TMBuilder) {
289 if (CachePath.empty())
292 if (!Index.modulePaths().count(ModuleID))
293 // The module does not have an entry, it can't have a hash at all
296 // Compute the unique hash for this entry
297 // This is based on the current compiler version, the module itself, the
298 // export list, the hash for every single module in the import list, the
299 // list of ResolvedODR for the module, and the list of preserved symbols.
301 // Include the hash for the current module
302 auto ModHash = Index.getModuleHash(ModuleID);
304 if (all_of(ModHash, [](uint32_t V) { return V == 0; }))
305 // No hash entry, no caching!
310 // Include the parts of the LTO configuration that affect code generation.
311 auto AddString = [&](StringRef Str) {
313 Hasher.update(ArrayRef<uint8_t>{0});
315 auto AddUnsigned = [&](unsigned I) {
321 Hasher.update(ArrayRef<uint8_t>{Data, 4});
324 // Start with the compiler revision
325 Hasher.update(LLVM_VERSION_STRING);
326 #ifdef HAVE_LLVM_REVISION
327 Hasher.update(LLVM_REVISION);
330 // Hash the optimization level and the target machine settings.
331 AddString(TMBuilder.MCpu);
332 // FIXME: Hash more of Options. For now all clients initialize Options from
333 // command-line flags (which is unsupported in production), but may set
334 // RelaxELFRelocations. The clang driver can also pass FunctionSections,
335 // DataSections and DebuggerTuning via command line flags.
336 AddUnsigned(TMBuilder.Options.RelaxELFRelocations);
337 AddUnsigned(TMBuilder.Options.FunctionSections);
338 AddUnsigned(TMBuilder.Options.DataSections);
339 AddUnsigned((unsigned)TMBuilder.Options.DebuggerTuning);
340 AddString(TMBuilder.MAttr);
341 if (TMBuilder.RelocModel)
342 AddUnsigned(*TMBuilder.RelocModel);
343 AddUnsigned(TMBuilder.CGOptLevel);
344 AddUnsigned(OptLevel);
346 Hasher.update(ArrayRef<uint8_t>((uint8_t *)&ModHash[0], sizeof(ModHash)));
347 for (auto F : ExportList)
348 // The export list can impact the internalization, be conservative here
349 Hasher.update(ArrayRef<uint8_t>((uint8_t *)&F, sizeof(F)));
351 // Include the hash for every module we import functions from
352 for (auto &Entry : ImportList) {
353 auto ModHash = Index.getModuleHash(Entry.first());
354 Hasher.update(ArrayRef<uint8_t>((uint8_t *)&ModHash[0], sizeof(ModHash)));
357 // Include the hash for the resolved ODR.
358 for (auto &Entry : ResolvedODR) {
359 Hasher.update(ArrayRef<uint8_t>((const uint8_t *)&Entry.first,
360 sizeof(GlobalValue::GUID)));
361 Hasher.update(ArrayRef<uint8_t>((const uint8_t *)&Entry.second,
362 sizeof(GlobalValue::LinkageTypes)));
365 // Include the hash for the preserved symbols.
366 for (auto &Entry : PreservedSymbols) {
367 if (DefinedFunctions.count(Entry))
369 ArrayRef<uint8_t>((const uint8_t *)&Entry, sizeof(GlobalValue::GUID)));
372 sys::path::append(EntryPath, CachePath, toHex(Hasher.result()));
375 // Access the path to this entry in the cache.
376 StringRef getEntryPath() { return EntryPath; }
378 // Try loading the buffer for this cache entry.
379 ErrorOr<std::unique_ptr<MemoryBuffer>> tryLoadingBuffer() {
380 if (EntryPath.empty())
381 return std::error_code();
382 return MemoryBuffer::getFile(EntryPath);
385 // Cache the Produced object file
386 void write(const MemoryBuffer &OutputBuffer) {
387 if (EntryPath.empty())
390 // Write to a temporary to avoid race condition
391 SmallString<128> TempFilename;
394 sys::fs::createTemporaryFile("Thin", "tmp.o", TempFD, TempFilename);
396 errs() << "Error: " << EC.message() << "\n";
397 report_fatal_error("ThinLTO: Can't get a temporary file");
400 raw_fd_ostream OS(TempFD, /* ShouldClose */ true);
401 OS << OutputBuffer.getBuffer();
403 // Rename to final destination (hopefully race condition won't matter here)
404 EC = sys::fs::rename(TempFilename, EntryPath);
406 sys::fs::remove(TempFilename);
407 raw_fd_ostream OS(EntryPath, EC, sys::fs::F_None);
409 report_fatal_error(Twine("Failed to open ") + EntryPath +
410 " to save cached entry\n");
411 OS << OutputBuffer.getBuffer();
416 static std::unique_ptr<MemoryBuffer>
417 ProcessThinLTOModule(Module &TheModule, ModuleSummaryIndex &Index,
418 StringMap<MemoryBufferRef> &ModuleMap, TargetMachine &TM,
419 const FunctionImporter::ImportMapTy &ImportList,
420 const FunctionImporter::ExportSetTy &ExportList,
421 const DenseSet<GlobalValue::GUID> &GUIDPreservedSymbols,
422 const GVSummaryMapTy &DefinedGlobals,
423 const ThinLTOCodeGenerator::CachingOptions &CacheOptions,
424 bool DisableCodeGen, StringRef SaveTempsDir,
425 unsigned OptLevel, unsigned count) {
427 // "Benchmark"-like optimization: single-source case
428 bool SingleModule = (ModuleMap.size() == 1);
431 promoteModule(TheModule, Index);
433 // Apply summary-based LinkOnce/Weak resolution decisions.
434 thinLTOResolveWeakForLinkerModule(TheModule, DefinedGlobals);
436 // Save temps: after promotion.
437 saveTempBitcode(TheModule, SaveTempsDir, count, ".1.promoted.bc");
440 // Be friendly and don't nuke totally the module when the client didn't
441 // supply anything to preserve.
442 if (!ExportList.empty() || !GUIDPreservedSymbols.empty()) {
443 // Apply summary-based internalization decisions.
444 thinLTOInternalizeModule(TheModule, DefinedGlobals);
447 // Save internalized bitcode
448 saveTempBitcode(TheModule, SaveTempsDir, count, ".2.internalized.bc");
451 crossImportIntoModule(TheModule, Index, ModuleMap, ImportList);
453 // Save temps: after cross-module import.
454 saveTempBitcode(TheModule, SaveTempsDir, count, ".3.imported.bc");
457 optimizeModule(TheModule, TM, OptLevel);
459 saveTempBitcode(TheModule, SaveTempsDir, count, ".4.opt.bc");
461 if (DisableCodeGen) {
462 // Configured to stop before CodeGen, serialize the bitcode and return.
463 SmallVector<char, 128> OutputBuffer;
465 raw_svector_ostream OS(OutputBuffer);
466 ProfileSummaryInfo PSI(TheModule);
467 auto Index = buildModuleSummaryIndex(TheModule, nullptr, nullptr);
468 WriteBitcodeToFile(&TheModule, OS, true, &Index);
470 return make_unique<ObjectMemoryBuffer>(std::move(OutputBuffer));
473 return codegenModule(TheModule, TM);
476 /// Resolve LinkOnce/Weak symbols. Record resolutions in the \p ResolvedODR map
477 /// for caching, and in the \p Index for application during the ThinLTO
478 /// backends. This is needed for correctness for exported symbols (ensure
479 /// at least one copy kept) and a compile-time optimization (to drop duplicate
480 /// copies when possible).
481 static void resolveWeakForLinkerInIndex(
482 ModuleSummaryIndex &Index,
483 StringMap<std::map<GlobalValue::GUID, GlobalValue::LinkageTypes>>
486 DenseMap<GlobalValue::GUID, const GlobalValueSummary *> PrevailingCopy;
487 computePrevailingCopies(Index, PrevailingCopy);
489 auto isPrevailing = [&](GlobalValue::GUID GUID, const GlobalValueSummary *S) {
490 const auto &Prevailing = PrevailingCopy.find(GUID);
491 // Not in map means that there was only one copy, which must be prevailing.
492 if (Prevailing == PrevailingCopy.end())
494 return Prevailing->second == S;
497 auto recordNewLinkage = [&](StringRef ModuleIdentifier,
498 GlobalValue::GUID GUID,
499 GlobalValue::LinkageTypes NewLinkage) {
500 ResolvedODR[ModuleIdentifier][GUID] = NewLinkage;
503 thinLTOResolveWeakForLinkerInIndex(Index, isPrevailing, recordNewLinkage);
506 // Initialize the TargetMachine builder for a given Triple
507 static void initTMBuilder(TargetMachineBuilder &TMBuilder,
508 const Triple &TheTriple) {
509 // Set a default CPU for Darwin triples (copied from LTOCodeGenerator).
510 // FIXME this looks pretty terrible...
511 if (TMBuilder.MCpu.empty() && TheTriple.isOSDarwin()) {
512 if (TheTriple.getArch() == llvm::Triple::x86_64)
513 TMBuilder.MCpu = "core2";
514 else if (TheTriple.getArch() == llvm::Triple::x86)
515 TMBuilder.MCpu = "yonah";
516 else if (TheTriple.getArch() == llvm::Triple::aarch64)
517 TMBuilder.MCpu = "cyclone";
519 TMBuilder.TheTriple = std::move(TheTriple);
522 } // end anonymous namespace
524 void ThinLTOCodeGenerator::addModule(StringRef Identifier, StringRef Data) {
525 MemoryBufferRef Buffer(Data, Identifier);
526 if (Modules.empty()) {
527 // First module added, so initialize the triple and some options
530 ErrorOr<std::string> TripleOrErr =
531 expectedToErrorOrAndEmitErrors(Context, getBitcodeTargetTriple(Buffer));
533 TripleStr = *TripleOrErr;
534 Triple TheTriple(TripleStr);
535 initTMBuilder(TMBuilder, Triple(TheTriple));
541 ErrorOr<std::string> TripleOrErr =
542 expectedToErrorOrAndEmitErrors(Context, getBitcodeTargetTriple(Buffer));
544 TripleStr = *TripleOrErr;
545 assert(TMBuilder.TheTriple.str() == TripleStr &&
546 "ThinLTO modules with different triple not supported");
549 Modules.push_back(Buffer);
552 void ThinLTOCodeGenerator::preserveSymbol(StringRef Name) {
553 PreservedSymbols.insert(Name);
556 void ThinLTOCodeGenerator::crossReferenceSymbol(StringRef Name) {
557 // FIXME: At the moment, we don't take advantage of this extra information,
558 // we're conservatively considering cross-references as preserved.
559 // CrossReferencedSymbols.insert(Name);
560 PreservedSymbols.insert(Name);
563 // TargetMachine factory
564 std::unique_ptr<TargetMachine> TargetMachineBuilder::create() const {
566 const Target *TheTarget =
567 TargetRegistry::lookupTarget(TheTriple.str(), ErrMsg);
569 report_fatal_error("Can't load target for this Triple: " + ErrMsg);
572 // Use MAttr as the default set of features.
573 SubtargetFeatures Features(MAttr);
574 Features.getDefaultSubtargetFeatures(TheTriple);
575 std::string FeatureStr = Features.getString();
577 return std::unique_ptr<TargetMachine>(TheTarget->createTargetMachine(
578 TheTriple.str(), MCpu, FeatureStr, Options, RelocModel,
579 CodeModel::Default, CGOptLevel));
583 * Produce the combined summary index from all the bitcode files:
586 std::unique_ptr<ModuleSummaryIndex> ThinLTOCodeGenerator::linkCombinedIndex() {
587 std::unique_ptr<ModuleSummaryIndex> CombinedIndex;
588 uint64_t NextModuleId = 0;
589 for (auto &ModuleBuffer : Modules) {
590 Expected<std::unique_ptr<object::ModuleSummaryIndexObjectFile>> ObjOrErr =
591 object::ModuleSummaryIndexObjectFile::create(ModuleBuffer);
594 logAllUnhandledErrors(
595 ObjOrErr.takeError(), errs(),
596 "error: can't create ModuleSummaryIndexObjectFile for buffer: ");
599 auto Index = (*ObjOrErr)->takeIndex();
601 CombinedIndex->mergeFrom(std::move(Index), ++NextModuleId);
603 CombinedIndex = std::move(Index);
606 return CombinedIndex;
610 * Perform promotion and renaming of exported internal functions.
611 * Index is updated to reflect linkage changes from weak resolution.
613 void ThinLTOCodeGenerator::promote(Module &TheModule,
614 ModuleSummaryIndex &Index) {
615 auto ModuleCount = Index.modulePaths().size();
616 auto ModuleIdentifier = TheModule.getModuleIdentifier();
618 // Collect for each module the list of function it defines (GUID -> Summary).
619 StringMap<GVSummaryMapTy> ModuleToDefinedGVSummaries;
620 Index.collectDefinedGVSummariesPerModule(ModuleToDefinedGVSummaries);
622 // Convert the preserved symbols set from string to GUID
623 auto GUIDPreservedSymbols = computeGUIDPreservedSymbols(
624 PreservedSymbols, Triple(TheModule.getTargetTriple()));
626 // Compute "dead" symbols, we don't want to import/export these!
627 auto DeadSymbols = computeDeadSymbols(Index, GUIDPreservedSymbols);
629 // Generate import/export list
630 StringMap<FunctionImporter::ImportMapTy> ImportLists(ModuleCount);
631 StringMap<FunctionImporter::ExportSetTy> ExportLists(ModuleCount);
632 ComputeCrossModuleImport(Index, ModuleToDefinedGVSummaries, ImportLists,
633 ExportLists, &DeadSymbols);
635 // Resolve LinkOnce/Weak symbols.
636 StringMap<std::map<GlobalValue::GUID, GlobalValue::LinkageTypes>> ResolvedODR;
637 resolveWeakForLinkerInIndex(Index, ResolvedODR);
639 thinLTOResolveWeakForLinkerModule(
640 TheModule, ModuleToDefinedGVSummaries[ModuleIdentifier]);
642 // Promote the exported values in the index, so that they are promoted
644 auto isExported = [&](StringRef ModuleIdentifier, GlobalValue::GUID GUID) {
645 const auto &ExportList = ExportLists.find(ModuleIdentifier);
646 return (ExportList != ExportLists.end() &&
647 ExportList->second.count(GUID)) ||
648 GUIDPreservedSymbols.count(GUID);
650 thinLTOInternalizeAndPromoteInIndex(Index, isExported);
652 promoteModule(TheModule, Index);
656 * Perform cross-module importing for the module identified by ModuleIdentifier.
658 void ThinLTOCodeGenerator::crossModuleImport(Module &TheModule,
659 ModuleSummaryIndex &Index) {
660 auto ModuleMap = generateModuleMap(Modules);
661 auto ModuleCount = Index.modulePaths().size();
663 // Collect for each module the list of function it defines (GUID -> Summary).
664 StringMap<GVSummaryMapTy> ModuleToDefinedGVSummaries(ModuleCount);
665 Index.collectDefinedGVSummariesPerModule(ModuleToDefinedGVSummaries);
667 // Convert the preserved symbols set from string to GUID
668 auto GUIDPreservedSymbols = computeGUIDPreservedSymbols(
669 PreservedSymbols, Triple(TheModule.getTargetTriple()));
671 // Compute "dead" symbols, we don't want to import/export these!
672 auto DeadSymbols = computeDeadSymbols(Index, GUIDPreservedSymbols);
674 // Generate import/export list
675 StringMap<FunctionImporter::ImportMapTy> ImportLists(ModuleCount);
676 StringMap<FunctionImporter::ExportSetTy> ExportLists(ModuleCount);
677 ComputeCrossModuleImport(Index, ModuleToDefinedGVSummaries, ImportLists,
678 ExportLists, &DeadSymbols);
679 auto &ImportList = ImportLists[TheModule.getModuleIdentifier()];
681 crossImportIntoModule(TheModule, Index, ModuleMap, ImportList);
685 * Compute the list of summaries needed for importing into module.
687 void ThinLTOCodeGenerator::gatherImportedSummariesForModule(
688 StringRef ModulePath, ModuleSummaryIndex &Index,
689 std::map<std::string, GVSummaryMapTy> &ModuleToSummariesForIndex) {
690 auto ModuleCount = Index.modulePaths().size();
692 // Collect for each module the list of function it defines (GUID -> Summary).
693 StringMap<GVSummaryMapTy> ModuleToDefinedGVSummaries(ModuleCount);
694 Index.collectDefinedGVSummariesPerModule(ModuleToDefinedGVSummaries);
696 // Generate import/export list
697 StringMap<FunctionImporter::ImportMapTy> ImportLists(ModuleCount);
698 StringMap<FunctionImporter::ExportSetTy> ExportLists(ModuleCount);
699 ComputeCrossModuleImport(Index, ModuleToDefinedGVSummaries, ImportLists,
702 llvm::gatherImportedSummariesForModule(ModulePath, ModuleToDefinedGVSummaries,
703 ImportLists[ModulePath],
704 ModuleToSummariesForIndex);
708 * Emit the list of files needed for importing into module.
710 void ThinLTOCodeGenerator::emitImports(StringRef ModulePath,
711 StringRef OutputName,
712 ModuleSummaryIndex &Index) {
713 auto ModuleCount = Index.modulePaths().size();
715 // Collect for each module the list of function it defines (GUID -> Summary).
716 StringMap<GVSummaryMapTy> ModuleToDefinedGVSummaries(ModuleCount);
717 Index.collectDefinedGVSummariesPerModule(ModuleToDefinedGVSummaries);
719 // Generate import/export list
720 StringMap<FunctionImporter::ImportMapTy> ImportLists(ModuleCount);
721 StringMap<FunctionImporter::ExportSetTy> ExportLists(ModuleCount);
722 ComputeCrossModuleImport(Index, ModuleToDefinedGVSummaries, ImportLists,
726 if ((EC = EmitImportsFiles(ModulePath, OutputName, ImportLists[ModulePath])))
727 report_fatal_error(Twine("Failed to open ") + OutputName +
728 " to save imports lists\n");
732 * Perform internalization. Index is updated to reflect linkage changes.
734 void ThinLTOCodeGenerator::internalize(Module &TheModule,
735 ModuleSummaryIndex &Index) {
736 initTMBuilder(TMBuilder, Triple(TheModule.getTargetTriple()));
737 auto ModuleCount = Index.modulePaths().size();
738 auto ModuleIdentifier = TheModule.getModuleIdentifier();
740 // Convert the preserved symbols set from string to GUID
741 auto GUIDPreservedSymbols =
742 computeGUIDPreservedSymbols(PreservedSymbols, TMBuilder.TheTriple);
744 // Collect for each module the list of function it defines (GUID -> Summary).
745 StringMap<GVSummaryMapTy> ModuleToDefinedGVSummaries(ModuleCount);
746 Index.collectDefinedGVSummariesPerModule(ModuleToDefinedGVSummaries);
748 // Compute "dead" symbols, we don't want to import/export these!
749 auto DeadSymbols = computeDeadSymbols(Index, GUIDPreservedSymbols);
751 // Generate import/export list
752 StringMap<FunctionImporter::ImportMapTy> ImportLists(ModuleCount);
753 StringMap<FunctionImporter::ExportSetTy> ExportLists(ModuleCount);
754 ComputeCrossModuleImport(Index, ModuleToDefinedGVSummaries, ImportLists,
755 ExportLists, &DeadSymbols);
756 auto &ExportList = ExportLists[ModuleIdentifier];
758 // Be friendly and don't nuke totally the module when the client didn't
759 // supply anything to preserve.
760 if (ExportList.empty() && GUIDPreservedSymbols.empty())
764 auto isExported = [&](StringRef ModuleIdentifier, GlobalValue::GUID GUID) {
765 const auto &ExportList = ExportLists.find(ModuleIdentifier);
766 return (ExportList != ExportLists.end() &&
767 ExportList->second.count(GUID)) ||
768 GUIDPreservedSymbols.count(GUID);
770 thinLTOInternalizeAndPromoteInIndex(Index, isExported);
771 thinLTOInternalizeModule(TheModule,
772 ModuleToDefinedGVSummaries[ModuleIdentifier]);
776 * Perform post-importing ThinLTO optimizations.
778 void ThinLTOCodeGenerator::optimize(Module &TheModule) {
779 initTMBuilder(TMBuilder, Triple(TheModule.getTargetTriple()));
782 optimizeModule(TheModule, *TMBuilder.create(), OptLevel);
786 * Perform ThinLTO CodeGen.
788 std::unique_ptr<MemoryBuffer> ThinLTOCodeGenerator::codegen(Module &TheModule) {
789 initTMBuilder(TMBuilder, Triple(TheModule.getTargetTriple()));
790 return codegenModule(TheModule, *TMBuilder.create());
793 /// Write out the generated object file, either from CacheEntryPath or from
794 /// OutputBuffer, preferring hard-link when possible.
795 /// Returns the path to the generated file in SavedObjectsDirectoryPath.
796 static std::string writeGeneratedObject(int count, StringRef CacheEntryPath,
797 StringRef SavedObjectsDirectoryPath,
798 const MemoryBuffer &OutputBuffer) {
799 SmallString<128> OutputPath(SavedObjectsDirectoryPath);
800 llvm::sys::path::append(OutputPath, Twine(count) + ".thinlto.o");
801 OutputPath.c_str(); // Ensure the string is null terminated.
802 if (sys::fs::exists(OutputPath))
803 sys::fs::remove(OutputPath);
805 // We don't return a memory buffer to the linker, just a list of files.
806 if (!CacheEntryPath.empty()) {
807 // Cache is enabled, hard-link the entry (or copy if hard-link fails).
808 auto Err = sys::fs::create_hard_link(CacheEntryPath, OutputPath);
810 return OutputPath.str();
811 // Hard linking failed, try to copy.
812 Err = sys::fs::copy_file(CacheEntryPath, OutputPath);
814 return OutputPath.str();
815 // Copy failed (could be because the CacheEntry was removed from the cache
816 // in the meantime by another process), fall back and try to write down the
817 // buffer to the output.
818 errs() << "error: can't link or copy from cached entry '" << CacheEntryPath
819 << "' to '" << OutputPath << "'\n";
821 // No cache entry, just write out the buffer.
823 raw_fd_ostream OS(OutputPath, Err, sys::fs::F_None);
825 report_fatal_error("Can't open output '" + OutputPath + "'\n");
826 OS << OutputBuffer.getBuffer();
827 return OutputPath.str();
830 // Main entry point for the ThinLTO processing
831 void ThinLTOCodeGenerator::run() {
833 // Perform only parallel codegen and return.
835 assert(ProducedBinaries.empty() && "The generator should not be reused");
836 ProducedBinaries.resize(Modules.size());
838 for (auto &ModuleBuffer : Modules) {
839 Pool.async([&](int count) {
841 Context.setDiscardValueNames(LTODiscardValueNames);
844 auto TheModule = loadModuleFromBuffer(ModuleBuffer, Context, false,
845 /*IsImporting*/ false);
848 ProducedBinaries[count] = codegen(*TheModule);
855 // Sequential linking phase
856 auto Index = linkCombinedIndex();
858 // Save temps: index.
859 if (!SaveTempsDir.empty()) {
860 auto SaveTempPath = SaveTempsDir + "index.bc";
862 raw_fd_ostream OS(SaveTempPath, EC, sys::fs::F_None);
864 report_fatal_error(Twine("Failed to open ") + SaveTempPath +
865 " to save optimized bitcode\n");
866 WriteIndexToFile(*Index, OS);
869 // Prepare the resulting object vector
870 assert(ProducedBinaries.empty() && "The generator should not be reused");
871 if (SavedObjectsDirectoryPath.empty())
872 ProducedBinaries.resize(Modules.size());
874 sys::fs::create_directories(SavedObjectsDirectoryPath);
876 sys::fs::is_directory(SavedObjectsDirectoryPath, IsDir);
878 report_fatal_error("Unexistent dir: '" + SavedObjectsDirectoryPath + "'");
879 ProducedBinaryFiles.resize(Modules.size());
882 // Prepare the module map.
883 auto ModuleMap = generateModuleMap(Modules);
884 auto ModuleCount = Modules.size();
886 // Collect for each module the list of function it defines (GUID -> Summary).
887 StringMap<GVSummaryMapTy> ModuleToDefinedGVSummaries(ModuleCount);
888 Index->collectDefinedGVSummariesPerModule(ModuleToDefinedGVSummaries);
890 // Convert the preserved symbols set from string to GUID, this is needed for
891 // computing the caching hash and the internalization.
892 auto GUIDPreservedSymbols =
893 computeGUIDPreservedSymbols(PreservedSymbols, TMBuilder.TheTriple);
895 // Compute "dead" symbols, we don't want to import/export these!
896 auto DeadSymbols = computeDeadSymbols(*Index, GUIDPreservedSymbols);
898 // Collect the import/export lists for all modules from the call-graph in the
900 StringMap<FunctionImporter::ImportMapTy> ImportLists(ModuleCount);
901 StringMap<FunctionImporter::ExportSetTy> ExportLists(ModuleCount);
902 ComputeCrossModuleImport(*Index, ModuleToDefinedGVSummaries, ImportLists,
903 ExportLists, &DeadSymbols);
905 // We use a std::map here to be able to have a defined ordering when
906 // producing a hash for the cache entry.
907 // FIXME: we should be able to compute the caching hash for the entry based
908 // on the index, and nuke this map.
909 StringMap<std::map<GlobalValue::GUID, GlobalValue::LinkageTypes>> ResolvedODR;
911 // Resolve LinkOnce/Weak symbols, this has to be computed early because it
912 // impacts the caching.
913 resolveWeakForLinkerInIndex(*Index, ResolvedODR);
915 auto isExported = [&](StringRef ModuleIdentifier, GlobalValue::GUID GUID) {
916 const auto &ExportList = ExportLists.find(ModuleIdentifier);
917 return (ExportList != ExportLists.end() &&
918 ExportList->second.count(GUID)) ||
919 GUIDPreservedSymbols.count(GUID);
922 // Use global summary-based analysis to identify symbols that can be
923 // internalized (because they aren't exported or preserved as per callback).
924 // Changes are made in the index, consumed in the ThinLTO backends.
925 thinLTOInternalizeAndPromoteInIndex(*Index, isExported);
927 // Make sure that every module has an entry in the ExportLists and
928 // ResolvedODR maps to enable threaded access to these maps below.
929 for (auto &DefinedGVSummaries : ModuleToDefinedGVSummaries) {
930 ExportLists[DefinedGVSummaries.first()];
931 ResolvedODR[DefinedGVSummaries.first()];
934 // Compute the ordering we will process the inputs: the rough heuristic here
935 // is to sort them per size so that the largest module get schedule as soon as
936 // possible. This is purely a compile-time optimization.
937 std::vector<int> ModulesOrdering;
938 ModulesOrdering.resize(Modules.size());
939 std::iota(ModulesOrdering.begin(), ModulesOrdering.end(), 0);
940 std::sort(ModulesOrdering.begin(), ModulesOrdering.end(),
941 [&](int LeftIndex, int RightIndex) {
942 auto LSize = Modules[LeftIndex].getBufferSize();
943 auto RSize = Modules[RightIndex].getBufferSize();
944 return LSize > RSize;
947 // Parallel optimizer + codegen
949 ThreadPool Pool(ThreadCount);
950 for (auto IndexCount : ModulesOrdering) {
951 auto &ModuleBuffer = Modules[IndexCount];
952 Pool.async([&](int count) {
953 auto ModuleIdentifier = ModuleBuffer.getBufferIdentifier();
954 auto &ExportList = ExportLists[ModuleIdentifier];
956 auto &DefinedFunctions = ModuleToDefinedGVSummaries[ModuleIdentifier];
958 // The module may be cached, this helps handling it.
959 ModuleCacheEntry CacheEntry(CacheOptions.Path, *Index, ModuleIdentifier,
960 ImportLists[ModuleIdentifier], ExportList,
961 ResolvedODR[ModuleIdentifier],
962 DefinedFunctions, GUIDPreservedSymbols,
963 OptLevel, TMBuilder);
964 auto CacheEntryPath = CacheEntry.getEntryPath();
967 auto ErrOrBuffer = CacheEntry.tryLoadingBuffer();
968 DEBUG(dbgs() << "Cache " << (ErrOrBuffer ? "hit" : "miss") << " '"
969 << CacheEntryPath << "' for buffer " << count << " "
970 << ModuleIdentifier << "\n");
974 if (SavedObjectsDirectoryPath.empty())
975 ProducedBinaries[count] = std::move(ErrOrBuffer.get());
977 ProducedBinaryFiles[count] = writeGeneratedObject(
978 count, CacheEntryPath, SavedObjectsDirectoryPath,
985 Context.setDiscardValueNames(LTODiscardValueNames);
986 Context.enableDebugTypeODRUniquing();
987 auto DiagFileOrErr = setupOptimizationRemarks(Context, count);
988 if (!DiagFileOrErr) {
989 errs() << "Error: " << toString(DiagFileOrErr.takeError()) << "\n";
990 report_fatal_error("ThinLTO: Can't get an output file for the "
995 auto TheModule = loadModuleFromBuffer(ModuleBuffer, Context, false,
996 /*IsImporting*/ false);
998 // Save temps: original file.
999 saveTempBitcode(*TheModule, SaveTempsDir, count, ".0.original.bc");
1001 auto &ImportList = ImportLists[ModuleIdentifier];
1002 // Run the main process now, and generates a binary
1003 auto OutputBuffer = ProcessThinLTOModule(
1004 *TheModule, *Index, ModuleMap, *TMBuilder.create(), ImportList,
1005 ExportList, GUIDPreservedSymbols,
1006 ModuleToDefinedGVSummaries[ModuleIdentifier], CacheOptions,
1007 DisableCodeGen, SaveTempsDir, OptLevel, count);
1009 // Commit to the cache (if enabled)
1010 CacheEntry.write(*OutputBuffer);
1012 if (SavedObjectsDirectoryPath.empty()) {
1013 // We need to generated a memory buffer for the linker.
1014 if (!CacheEntryPath.empty()) {
1015 // Cache is enabled, reload from the cache
1016 // We do this to lower memory pressuree: the buffer is on the heap
1017 // and releasing it frees memory that can be used for the next input
1018 // file. The final binary link will read from the VFS cache
1019 // (hopefully!) or from disk if the memory pressure wasn't too high.
1020 auto ReloadedBufferOrErr = CacheEntry.tryLoadingBuffer();
1021 if (auto EC = ReloadedBufferOrErr.getError()) {
1022 // On error, keeping the preexisting buffer and printing a
1023 // diagnostic is more friendly than just crashing.
1024 errs() << "error: can't reload cached file '" << CacheEntryPath
1025 << "': " << EC.message() << "\n";
1027 OutputBuffer = std::move(*ReloadedBufferOrErr);
1030 ProducedBinaries[count] = std::move(OutputBuffer);
1033 ProducedBinaryFiles[count] = writeGeneratedObject(
1034 count, CacheEntryPath, SavedObjectsDirectoryPath, *OutputBuffer);
1039 CachePruning(CacheOptions.Path)
1040 .setPruningInterval(std::chrono::seconds(CacheOptions.PruningInterval))
1041 .setEntryExpiration(std::chrono::seconds(CacheOptions.Expiration))
1042 .setMaxSize(CacheOptions.MaxPercentageOfAvailableSpace)
1045 // If statistics were requested, print them out now.
1046 if (llvm::AreStatisticsEnabled())
1047 llvm::PrintStatistics();