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/TargetLibraryInfo.h"
25 #include "llvm/Analysis/TargetTransformInfo.h"
26 #include "llvm/Bitcode/BitcodeWriterPass.h"
27 #include "llvm/Bitcode/ReaderWriter.h"
28 #include "llvm/ExecutionEngine/ObjectMemoryBuffer.h"
29 #include "llvm/IR/DiagnosticPrinter.h"
30 #include "llvm/IR/LLVMContext.h"
31 #include "llvm/IR/LegacyPassManager.h"
32 #include "llvm/IR/Mangler.h"
33 #include "llvm/IRReader/IRReader.h"
34 #include "llvm/LTO/LTO.h"
35 #include "llvm/Linker/Linker.h"
36 #include "llvm/MC/SubtargetFeature.h"
37 #include "llvm/Object/IRObjectFile.h"
38 #include "llvm/Object/ModuleSummaryIndexObjectFile.h"
39 #include "llvm/Support/CachePruning.h"
40 #include "llvm/Support/Debug.h"
41 #include "llvm/Support/Path.h"
42 #include "llvm/Support/SHA1.h"
43 #include "llvm/Support/TargetRegistry.h"
44 #include "llvm/Support/ThreadPool.h"
45 #include "llvm/Target/TargetMachine.h"
46 #include "llvm/Transforms/IPO.h"
47 #include "llvm/Transforms/IPO/FunctionImport.h"
48 #include "llvm/Transforms/IPO/Internalize.h"
49 #include "llvm/Transforms/IPO/PassManagerBuilder.h"
50 #include "llvm/Transforms/ObjCARC.h"
51 #include "llvm/Transforms/Utils/FunctionImportUtils.h"
57 #define DEBUG_TYPE "thinlto"
60 // Flags -discard-value-names, defined in LTOCodeGenerator.cpp
61 extern cl::opt<bool> LTODiscardValueNames;
66 static cl::opt<int> ThreadCount("threads",
67 cl::init(std::thread::hardware_concurrency()));
69 static void diagnosticHandler(const DiagnosticInfo &DI) {
70 DiagnosticPrinterRawOStream DP(errs());
75 // Simple helper to save temporary files for debug.
76 static void saveTempBitcode(const Module &TheModule, StringRef TempDir,
77 unsigned count, StringRef Suffix) {
80 // User asked to save temps, let dump the bitcode file after import.
81 auto SaveTempPath = TempDir + llvm::utostr(count) + Suffix;
83 raw_fd_ostream OS(SaveTempPath.str(), EC, sys::fs::F_None);
85 report_fatal_error(Twine("Failed to open ") + SaveTempPath +
86 " to save optimized bitcode\n");
87 WriteBitcodeToFile(&TheModule, OS, /* ShouldPreserveUseListOrder */ true);
90 static const GlobalValueSummary *
91 getFirstDefinitionForLinker(const GlobalValueSummaryList &GVSummaryList) {
92 // If there is any strong definition anywhere, get it.
93 auto StrongDefForLinker = llvm::find_if(
94 GVSummaryList, [](const std::unique_ptr<GlobalValueSummary> &Summary) {
95 auto Linkage = Summary->linkage();
96 return !GlobalValue::isAvailableExternallyLinkage(Linkage) &&
97 !GlobalValue::isWeakForLinker(Linkage);
99 if (StrongDefForLinker != GVSummaryList.end())
100 return StrongDefForLinker->get();
101 // Get the first *linker visible* definition for this global in the summary
103 auto FirstDefForLinker = llvm::find_if(
104 GVSummaryList, [](const std::unique_ptr<GlobalValueSummary> &Summary) {
105 auto Linkage = Summary->linkage();
106 return !GlobalValue::isAvailableExternallyLinkage(Linkage);
108 // Extern templates can be emitted as available_externally.
109 if (FirstDefForLinker == GVSummaryList.end())
111 return FirstDefForLinker->get();
114 // Populate map of GUID to the prevailing copy for any multiply defined
115 // symbols. Currently assume first copy is prevailing, or any strong
116 // definition. Can be refined with Linker information in the future.
117 static void computePrevailingCopies(
118 const ModuleSummaryIndex &Index,
119 DenseMap<GlobalValue::GUID, const GlobalValueSummary *> &PrevailingCopy) {
120 auto HasMultipleCopies = [&](const GlobalValueSummaryList &GVSummaryList) {
121 return GVSummaryList.size() > 1;
124 for (auto &I : Index) {
125 if (HasMultipleCopies(I.second))
126 PrevailingCopy[I.first] = getFirstDefinitionForLinker(I.second);
130 static StringMap<MemoryBufferRef>
131 generateModuleMap(const std::vector<MemoryBufferRef> &Modules) {
132 StringMap<MemoryBufferRef> ModuleMap;
133 for (auto &ModuleBuffer : Modules) {
134 assert(ModuleMap.find(ModuleBuffer.getBufferIdentifier()) ==
136 "Expect unique Buffer Identifier");
137 ModuleMap[ModuleBuffer.getBufferIdentifier()] = ModuleBuffer;
142 static void promoteModule(Module &TheModule, const ModuleSummaryIndex &Index) {
143 if (renameModuleForThinLTO(TheModule, Index))
144 report_fatal_error("renameModuleForThinLTO failed");
148 crossImportIntoModule(Module &TheModule, const ModuleSummaryIndex &Index,
149 StringMap<MemoryBufferRef> &ModuleMap,
150 const FunctionImporter::ImportMapTy &ImportList) {
151 ModuleLoader Loader(TheModule.getContext(), ModuleMap);
152 FunctionImporter Importer(Index, Loader);
153 Importer.importFunctions(TheModule, ImportList);
156 static void optimizeModule(Module &TheModule, TargetMachine &TM) {
157 // Populate the PassManager
158 PassManagerBuilder PMB;
159 PMB.LibraryInfo = new TargetLibraryInfoImpl(TM.getTargetTriple());
160 PMB.Inliner = createFunctionInliningPass();
161 // FIXME: should get it from the bitcode?
163 PMB.LoopVectorize = true;
164 PMB.SLPVectorize = true;
165 PMB.VerifyInput = true;
166 PMB.VerifyOutput = false;
168 legacy::PassManager PM;
170 // Add the TTI (required to inform the vectorizer about register size for
172 PM.add(createTargetTransformInfoWrapperPass(TM.getTargetIRAnalysis()));
175 PMB.populateThinLTOPassManager(PM);
180 // Convert the PreservedSymbols map from "Name" based to "GUID" based.
181 static DenseSet<GlobalValue::GUID>
182 computeGUIDPreservedSymbols(const StringSet<> &PreservedSymbols,
183 const Triple &TheTriple) {
184 DenseSet<GlobalValue::GUID> GUIDPreservedSymbols(PreservedSymbols.size());
185 for (auto &Entry : PreservedSymbols) {
186 StringRef Name = Entry.first();
187 if (TheTriple.isOSBinFormatMachO() && Name.size() > 0 && Name[0] == '_')
188 Name = Name.drop_front();
189 GUIDPreservedSymbols.insert(GlobalValue::getGUID(Name));
191 return GUIDPreservedSymbols;
194 std::unique_ptr<MemoryBuffer> codegenModule(Module &TheModule,
196 SmallVector<char, 128> OutputBuffer;
200 raw_svector_ostream OS(OutputBuffer);
201 legacy::PassManager PM;
203 // If the bitcode files contain ARC code and were compiled with optimization,
204 // the ObjCARCContractPass must be run, so do it unconditionally here.
205 PM.add(createObjCARCContractPass());
207 // Setup the codegen now.
208 if (TM.addPassesToEmitFile(PM, OS, TargetMachine::CGFT_ObjectFile,
209 /* DisableVerify */ true))
210 report_fatal_error("Failed to setup codegen");
212 // Run codegen now. resulting binary is in OutputBuffer.
215 return make_unique<ObjectMemoryBuffer>(std::move(OutputBuffer));
218 /// Manage caching for a single Module.
219 class ModuleCacheEntry {
220 SmallString<128> EntryPath;
223 // Create a cache entry. This compute a unique hash for the Module considering
224 // the current list of export/import, and offer an interface to query to
225 // access the content in the cache.
227 StringRef CachePath, const ModuleSummaryIndex &Index, StringRef ModuleID,
228 const FunctionImporter::ImportMapTy &ImportList,
229 const FunctionImporter::ExportSetTy &ExportList,
230 const std::map<GlobalValue::GUID, GlobalValue::LinkageTypes> &ResolvedODR,
231 const GVSummaryMapTy &DefinedFunctions,
232 const DenseSet<GlobalValue::GUID> &PreservedSymbols) {
233 if (CachePath.empty())
236 // Compute the unique hash for this entry
237 // This is based on the current compiler version, the module itself, the
238 // export list, the hash for every single module in the import list, the
239 // list of ResolvedODR for the module, and the list of preserved symbols.
243 // Start with the compiler revision
244 Hasher.update(LLVM_VERSION_STRING);
245 #ifdef HAVE_LLVM_REVISION
246 Hasher.update(LLVM_REVISION);
249 // Include the hash for the current module
250 auto ModHash = Index.getModuleHash(ModuleID);
251 Hasher.update(ArrayRef<uint8_t>((uint8_t *)&ModHash[0], sizeof(ModHash)));
252 for (auto F : ExportList)
253 // The export list can impact the internalization, be conservative here
254 Hasher.update(ArrayRef<uint8_t>((uint8_t *)&F, sizeof(F)));
256 // Include the hash for every module we import functions from
257 for (auto &Entry : ImportList) {
258 auto ModHash = Index.getModuleHash(Entry.first());
259 Hasher.update(ArrayRef<uint8_t>((uint8_t *)&ModHash[0], sizeof(ModHash)));
262 // Include the hash for the resolved ODR.
263 for (auto &Entry : ResolvedODR) {
264 Hasher.update(ArrayRef<uint8_t>((const uint8_t *)&Entry.first,
265 sizeof(GlobalValue::GUID)));
266 Hasher.update(ArrayRef<uint8_t>((const uint8_t *)&Entry.second,
267 sizeof(GlobalValue::LinkageTypes)));
270 // Include the hash for the preserved symbols.
271 for (auto &Entry : PreservedSymbols) {
272 if (DefinedFunctions.count(Entry))
274 ArrayRef<uint8_t>((const uint8_t *)&Entry, sizeof(GlobalValue::GUID)));
277 sys::path::append(EntryPath, CachePath, toHex(Hasher.result()));
280 // Access the path to this entry in the cache.
281 StringRef getEntryPath() { return EntryPath; }
283 // Try loading the buffer for this cache entry.
284 ErrorOr<std::unique_ptr<MemoryBuffer>> tryLoadingBuffer() {
285 if (EntryPath.empty())
286 return std::error_code();
287 return MemoryBuffer::getFile(EntryPath);
290 // Cache the Produced object file
291 std::unique_ptr<MemoryBuffer>
292 write(std::unique_ptr<MemoryBuffer> OutputBuffer) {
293 if (EntryPath.empty())
296 // Write to a temporary to avoid race condition
297 SmallString<128> TempFilename;
300 sys::fs::createTemporaryFile("Thin", "tmp.o", TempFD, TempFilename);
302 errs() << "Error: " << EC.message() << "\n";
303 report_fatal_error("ThinLTO: Can't get a temporary file");
306 raw_fd_ostream OS(TempFD, /* ShouldClose */ true);
307 OS << OutputBuffer->getBuffer();
309 // Rename to final destination (hopefully race condition won't matter here)
310 EC = sys::fs::rename(TempFilename, EntryPath);
312 sys::fs::remove(TempFilename);
313 raw_fd_ostream OS(EntryPath, EC, sys::fs::F_None);
315 report_fatal_error(Twine("Failed to open ") + EntryPath +
316 " to save cached entry\n");
317 OS << OutputBuffer->getBuffer();
319 auto ReloadedBufferOrErr = MemoryBuffer::getFile(EntryPath);
320 if (auto EC = ReloadedBufferOrErr.getError()) {
322 errs() << "error: can't reload cached file '" << EntryPath
323 << "': " << EC.message() << "\n";
326 return std::move(*ReloadedBufferOrErr);
330 static std::unique_ptr<MemoryBuffer>
331 ProcessThinLTOModule(Module &TheModule, ModuleSummaryIndex &Index,
332 StringMap<MemoryBufferRef> &ModuleMap, TargetMachine &TM,
333 const FunctionImporter::ImportMapTy &ImportList,
334 const FunctionImporter::ExportSetTy &ExportList,
335 const DenseSet<GlobalValue::GUID> &GUIDPreservedSymbols,
336 const GVSummaryMapTy &DefinedGlobals,
337 const ThinLTOCodeGenerator::CachingOptions &CacheOptions,
338 bool DisableCodeGen, StringRef SaveTempsDir,
341 // "Benchmark"-like optimization: single-source case
342 bool SingleModule = (ModuleMap.size() == 1);
345 promoteModule(TheModule, Index);
347 // Apply summary-based LinkOnce/Weak resolution decisions.
348 thinLTOResolveWeakForLinkerModule(TheModule, DefinedGlobals);
350 // Save temps: after promotion.
351 saveTempBitcode(TheModule, SaveTempsDir, count, ".1.promoted.bc");
354 // Be friendly and don't nuke totally the module when the client didn't
355 // supply anything to preserve.
356 if (!ExportList.empty() || !GUIDPreservedSymbols.empty()) {
357 // Apply summary-based internalization decisions.
358 thinLTOInternalizeModule(TheModule, DefinedGlobals);
361 // Save internalized bitcode
362 saveTempBitcode(TheModule, SaveTempsDir, count, ".2.internalized.bc");
365 crossImportIntoModule(TheModule, Index, ModuleMap, ImportList);
367 // Save temps: after cross-module import.
368 saveTempBitcode(TheModule, SaveTempsDir, count, ".3.imported.bc");
371 optimizeModule(TheModule, TM);
373 saveTempBitcode(TheModule, SaveTempsDir, count, ".4.opt.bc");
375 if (DisableCodeGen) {
376 // Configured to stop before CodeGen, serialize the bitcode and return.
377 SmallVector<char, 128> OutputBuffer;
379 raw_svector_ostream OS(OutputBuffer);
380 ModuleSummaryIndexBuilder IndexBuilder(&TheModule);
381 WriteBitcodeToFile(&TheModule, OS, true, &IndexBuilder.getIndex());
383 return make_unique<ObjectMemoryBuffer>(std::move(OutputBuffer));
386 return codegenModule(TheModule, TM);
389 /// Resolve LinkOnce/Weak symbols. Record resolutions in the \p ResolvedODR map
390 /// for caching, and in the \p Index for application during the ThinLTO
391 /// backends. This is needed for correctness for exported symbols (ensure
392 /// at least one copy kept) and a compile-time optimization (to drop duplicate
393 /// copies when possible).
394 static void resolveWeakForLinkerInIndex(
395 ModuleSummaryIndex &Index,
396 StringMap<std::map<GlobalValue::GUID, GlobalValue::LinkageTypes>>
399 DenseMap<GlobalValue::GUID, const GlobalValueSummary *> PrevailingCopy;
400 computePrevailingCopies(Index, PrevailingCopy);
402 auto isPrevailing = [&](GlobalValue::GUID GUID, const GlobalValueSummary *S) {
403 const auto &Prevailing = PrevailingCopy.find(GUID);
404 // Not in map means that there was only one copy, which must be prevailing.
405 if (Prevailing == PrevailingCopy.end())
407 return Prevailing->second == S;
410 auto recordNewLinkage = [&](StringRef ModuleIdentifier,
411 GlobalValue::GUID GUID,
412 GlobalValue::LinkageTypes NewLinkage) {
413 ResolvedODR[ModuleIdentifier][GUID] = NewLinkage;
416 thinLTOResolveWeakForLinkerInIndex(Index, isPrevailing, recordNewLinkage);
419 // Initialize the TargetMachine builder for a given Triple
420 static void initTMBuilder(TargetMachineBuilder &TMBuilder,
421 const Triple &TheTriple) {
422 // Set a default CPU for Darwin triples (copied from LTOCodeGenerator).
423 // FIXME this looks pretty terrible...
424 if (TMBuilder.MCpu.empty() && TheTriple.isOSDarwin()) {
425 if (TheTriple.getArch() == llvm::Triple::x86_64)
426 TMBuilder.MCpu = "core2";
427 else if (TheTriple.getArch() == llvm::Triple::x86)
428 TMBuilder.MCpu = "yonah";
429 else if (TheTriple.getArch() == llvm::Triple::aarch64)
430 TMBuilder.MCpu = "cyclone";
432 TMBuilder.TheTriple = std::move(TheTriple);
435 } // end anonymous namespace
437 void ThinLTOCodeGenerator::addModule(StringRef Identifier, StringRef Data) {
438 MemoryBufferRef Buffer(Data, Identifier);
439 if (Modules.empty()) {
440 // First module added, so initialize the triple and some options
442 Triple TheTriple(getBitcodeTargetTriple(Buffer, Context));
443 initTMBuilder(TMBuilder, Triple(TheTriple));
448 assert(TMBuilder.TheTriple.str() ==
449 getBitcodeTargetTriple(Buffer, Context) &&
450 "ThinLTO modules with different triple not supported");
453 Modules.push_back(Buffer);
456 void ThinLTOCodeGenerator::preserveSymbol(StringRef Name) {
457 PreservedSymbols.insert(Name);
460 void ThinLTOCodeGenerator::crossReferenceSymbol(StringRef Name) {
461 // FIXME: At the moment, we don't take advantage of this extra information,
462 // we're conservatively considering cross-references as preserved.
463 // CrossReferencedSymbols.insert(Name);
464 PreservedSymbols.insert(Name);
467 // TargetMachine factory
468 std::unique_ptr<TargetMachine> TargetMachineBuilder::create() const {
470 const Target *TheTarget =
471 TargetRegistry::lookupTarget(TheTriple.str(), ErrMsg);
473 report_fatal_error("Can't load target for this Triple: " + ErrMsg);
476 // Use MAttr as the default set of features.
477 SubtargetFeatures Features(MAttr);
478 Features.getDefaultSubtargetFeatures(TheTriple);
479 std::string FeatureStr = Features.getString();
480 return std::unique_ptr<TargetMachine>(TheTarget->createTargetMachine(
481 TheTriple.str(), MCpu, FeatureStr, Options, RelocModel,
482 CodeModel::Default, CGOptLevel));
486 * Produce the combined summary index from all the bitcode files:
489 std::unique_ptr<ModuleSummaryIndex> ThinLTOCodeGenerator::linkCombinedIndex() {
490 std::unique_ptr<ModuleSummaryIndex> CombinedIndex;
491 uint64_t NextModuleId = 0;
492 for (auto &ModuleBuffer : Modules) {
493 ErrorOr<std::unique_ptr<object::ModuleSummaryIndexObjectFile>> ObjOrErr =
494 object::ModuleSummaryIndexObjectFile::create(ModuleBuffer,
496 if (std::error_code EC = ObjOrErr.getError()) {
498 errs() << "error: can't create ModuleSummaryIndexObjectFile for buffer: "
499 << EC.message() << "\n";
502 auto Index = (*ObjOrErr)->takeIndex();
504 CombinedIndex->mergeFrom(std::move(Index), ++NextModuleId);
506 CombinedIndex = std::move(Index);
509 return CombinedIndex;
513 * Perform promotion and renaming of exported internal functions.
514 * Index is updated to reflect linkage changes from weak resolution.
516 void ThinLTOCodeGenerator::promote(Module &TheModule,
517 ModuleSummaryIndex &Index) {
518 auto ModuleCount = Index.modulePaths().size();
519 auto ModuleIdentifier = TheModule.getModuleIdentifier();
520 // Collect for each module the list of function it defines (GUID -> Summary).
521 StringMap<GVSummaryMapTy> ModuleToDefinedGVSummaries;
522 Index.collectDefinedGVSummariesPerModule(ModuleToDefinedGVSummaries);
524 // Generate import/export list
525 StringMap<FunctionImporter::ImportMapTy> ImportLists(ModuleCount);
526 StringMap<FunctionImporter::ExportSetTy> ExportLists(ModuleCount);
527 ComputeCrossModuleImport(Index, ModuleToDefinedGVSummaries, ImportLists,
530 // Resolve LinkOnce/Weak symbols.
531 StringMap<std::map<GlobalValue::GUID, GlobalValue::LinkageTypes>> ResolvedODR;
532 resolveWeakForLinkerInIndex(Index, ResolvedODR);
534 thinLTOResolveWeakForLinkerModule(
535 TheModule, ModuleToDefinedGVSummaries[ModuleIdentifier]);
537 promoteModule(TheModule, Index);
541 * Perform cross-module importing for the module identified by ModuleIdentifier.
543 void ThinLTOCodeGenerator::crossModuleImport(Module &TheModule,
544 ModuleSummaryIndex &Index) {
545 auto ModuleMap = generateModuleMap(Modules);
546 auto ModuleCount = Index.modulePaths().size();
548 // Collect for each module the list of function it defines (GUID -> Summary).
549 StringMap<GVSummaryMapTy> ModuleToDefinedGVSummaries(ModuleCount);
550 Index.collectDefinedGVSummariesPerModule(ModuleToDefinedGVSummaries);
552 // Generate import/export list
553 StringMap<FunctionImporter::ImportMapTy> ImportLists(ModuleCount);
554 StringMap<FunctionImporter::ExportSetTy> ExportLists(ModuleCount);
555 ComputeCrossModuleImport(Index, ModuleToDefinedGVSummaries, ImportLists,
557 auto &ImportList = ImportLists[TheModule.getModuleIdentifier()];
559 crossImportIntoModule(TheModule, Index, ModuleMap, ImportList);
563 * Compute the list of summaries needed for importing into module.
565 void ThinLTOCodeGenerator::gatherImportedSummariesForModule(
566 StringRef ModulePath, ModuleSummaryIndex &Index,
567 std::map<std::string, GVSummaryMapTy> &ModuleToSummariesForIndex) {
568 auto ModuleCount = Index.modulePaths().size();
570 // Collect for each module the list of function it defines (GUID -> Summary).
571 StringMap<GVSummaryMapTy> ModuleToDefinedGVSummaries(ModuleCount);
572 Index.collectDefinedGVSummariesPerModule(ModuleToDefinedGVSummaries);
574 // Generate import/export list
575 StringMap<FunctionImporter::ImportMapTy> ImportLists(ModuleCount);
576 StringMap<FunctionImporter::ExportSetTy> ExportLists(ModuleCount);
577 ComputeCrossModuleImport(Index, ModuleToDefinedGVSummaries, ImportLists,
580 llvm::gatherImportedSummariesForModule(ModulePath, ModuleToDefinedGVSummaries,
582 ModuleToSummariesForIndex);
586 * Emit the list of files needed for importing into module.
588 void ThinLTOCodeGenerator::emitImports(StringRef ModulePath,
589 StringRef OutputName,
590 ModuleSummaryIndex &Index) {
591 auto ModuleCount = Index.modulePaths().size();
593 // Collect for each module the list of function it defines (GUID -> Summary).
594 StringMap<GVSummaryMapTy> ModuleToDefinedGVSummaries(ModuleCount);
595 Index.collectDefinedGVSummariesPerModule(ModuleToDefinedGVSummaries);
597 // Generate import/export list
598 StringMap<FunctionImporter::ImportMapTy> ImportLists(ModuleCount);
599 StringMap<FunctionImporter::ExportSetTy> ExportLists(ModuleCount);
600 ComputeCrossModuleImport(Index, ModuleToDefinedGVSummaries, ImportLists,
604 if ((EC = EmitImportsFiles(ModulePath, OutputName, ImportLists)))
605 report_fatal_error(Twine("Failed to open ") + OutputName +
606 " to save imports lists\n");
610 * Perform internalization. Index is updated to reflect linkage changes.
612 void ThinLTOCodeGenerator::internalize(Module &TheModule,
613 ModuleSummaryIndex &Index) {
614 initTMBuilder(TMBuilder, Triple(TheModule.getTargetTriple()));
615 auto ModuleCount = Index.modulePaths().size();
616 auto ModuleIdentifier = TheModule.getModuleIdentifier();
618 // Convert the preserved symbols set from string to GUID
619 auto GUIDPreservedSymbols =
620 computeGUIDPreservedSymbols(PreservedSymbols, TMBuilder.TheTriple);
622 // Collect for each module the list of function it defines (GUID -> Summary).
623 StringMap<GVSummaryMapTy> ModuleToDefinedGVSummaries(ModuleCount);
624 Index.collectDefinedGVSummariesPerModule(ModuleToDefinedGVSummaries);
626 // Generate import/export list
627 StringMap<FunctionImporter::ImportMapTy> ImportLists(ModuleCount);
628 StringMap<FunctionImporter::ExportSetTy> ExportLists(ModuleCount);
629 ComputeCrossModuleImport(Index, ModuleToDefinedGVSummaries, ImportLists,
631 auto &ExportList = ExportLists[ModuleIdentifier];
633 // Be friendly and don't nuke totally the module when the client didn't
634 // supply anything to preserve.
635 if (ExportList.empty() && GUIDPreservedSymbols.empty())
639 auto isExported = [&](StringRef ModuleIdentifier, GlobalValue::GUID GUID) {
640 const auto &ExportList = ExportLists.find(ModuleIdentifier);
641 return (ExportList != ExportLists.end() &&
642 ExportList->second.count(GUID)) ||
643 GUIDPreservedSymbols.count(GUID);
645 thinLTOInternalizeAndPromoteInIndex(Index, isExported);
646 thinLTOInternalizeModule(TheModule,
647 ModuleToDefinedGVSummaries[ModuleIdentifier]);
651 * Perform post-importing ThinLTO optimizations.
653 void ThinLTOCodeGenerator::optimize(Module &TheModule) {
654 initTMBuilder(TMBuilder, Triple(TheModule.getTargetTriple()));
657 optimizeModule(TheModule, *TMBuilder.create());
661 * Perform ThinLTO CodeGen.
663 std::unique_ptr<MemoryBuffer> ThinLTOCodeGenerator::codegen(Module &TheModule) {
664 initTMBuilder(TMBuilder, Triple(TheModule.getTargetTriple()));
665 return codegenModule(TheModule, *TMBuilder.create());
668 // Main entry point for the ThinLTO processing
669 void ThinLTOCodeGenerator::run() {
671 // Perform only parallel codegen and return.
673 assert(ProducedBinaries.empty() && "The generator should not be reused");
674 ProducedBinaries.resize(Modules.size());
676 for (auto &ModuleBuffer : Modules) {
677 Pool.async([&](int count) {
679 Context.setDiscardValueNames(LTODiscardValueNames);
682 auto TheModule = loadModuleFromBuffer(ModuleBuffer, Context, false);
685 ProducedBinaries[count] = codegen(*TheModule);
692 // Sequential linking phase
693 auto Index = linkCombinedIndex();
695 // Save temps: index.
696 if (!SaveTempsDir.empty()) {
697 auto SaveTempPath = SaveTempsDir + "index.bc";
699 raw_fd_ostream OS(SaveTempPath, EC, sys::fs::F_None);
701 report_fatal_error(Twine("Failed to open ") + SaveTempPath +
702 " to save optimized bitcode\n");
703 WriteIndexToFile(*Index, OS);
706 // Prepare the resulting object vector
707 assert(ProducedBinaries.empty() && "The generator should not be reused");
708 ProducedBinaries.resize(Modules.size());
710 // Prepare the module map.
711 auto ModuleMap = generateModuleMap(Modules);
712 auto ModuleCount = Modules.size();
714 // Collect for each module the list of function it defines (GUID -> Summary).
715 StringMap<GVSummaryMapTy> ModuleToDefinedGVSummaries(ModuleCount);
716 Index->collectDefinedGVSummariesPerModule(ModuleToDefinedGVSummaries);
718 // Collect the import/export lists for all modules from the call-graph in the
720 StringMap<FunctionImporter::ImportMapTy> ImportLists(ModuleCount);
721 StringMap<FunctionImporter::ExportSetTy> ExportLists(ModuleCount);
722 ComputeCrossModuleImport(*Index, ModuleToDefinedGVSummaries, ImportLists,
725 // Convert the preserved symbols set from string to GUID, this is needed for
726 // computing the caching hash and the internalization.
727 auto GUIDPreservedSymbols =
728 computeGUIDPreservedSymbols(PreservedSymbols, TMBuilder.TheTriple);
730 // We use a std::map here to be able to have a defined ordering when
731 // producing a hash for the cache entry.
732 // FIXME: we should be able to compute the caching hash for the entry based
733 // on the index, and nuke this map.
734 StringMap<std::map<GlobalValue::GUID, GlobalValue::LinkageTypes>> ResolvedODR;
736 // Resolve LinkOnce/Weak symbols, this has to be computed early because it
737 // impacts the caching.
738 resolveWeakForLinkerInIndex(*Index, ResolvedODR);
740 auto isExported = [&](StringRef ModuleIdentifier, GlobalValue::GUID GUID) {
741 const auto &ExportList = ExportLists.find(ModuleIdentifier);
742 return (ExportList != ExportLists.end() &&
743 ExportList->second.count(GUID)) ||
744 GUIDPreservedSymbols.count(GUID);
747 // Use global summary-based analysis to identify symbols that can be
748 // internalized (because they aren't exported or preserved as per callback).
749 // Changes are made in the index, consumed in the ThinLTO backends.
750 thinLTOInternalizeAndPromoteInIndex(*Index, isExported);
752 // Make sure that every module has an entry in the ExportLists and
753 // ResolvedODR maps to enable threaded access to these maps below.
754 for (auto &DefinedGVSummaries : ModuleToDefinedGVSummaries) {
755 ExportLists[DefinedGVSummaries.first()];
756 ResolvedODR[DefinedGVSummaries.first()];
759 // Compute the ordering we will process the inputs: the rough heuristic here
760 // is to sort them per size so that the largest module get schedule as soon as
761 // possible. This is purely a compile-time optimization.
762 std::vector<int> ModulesOrdering;
763 ModulesOrdering.resize(Modules.size());
764 std::iota(ModulesOrdering.begin(), ModulesOrdering.end(), 0);
765 std::sort(ModulesOrdering.begin(), ModulesOrdering.end(),
766 [&](int LeftIndex, int RightIndex) {
767 auto LSize = Modules[LeftIndex].getBufferSize();
768 auto RSize = Modules[RightIndex].getBufferSize();
769 return LSize > RSize;
772 // Parallel optimizer + codegen
774 ThreadPool Pool(ThreadCount);
775 for (auto IndexCount : ModulesOrdering) {
776 auto &ModuleBuffer = Modules[IndexCount];
777 Pool.async([&](int count) {
778 auto ModuleIdentifier = ModuleBuffer.getBufferIdentifier();
779 auto &ExportList = ExportLists[ModuleIdentifier];
781 auto &DefinedFunctions = ModuleToDefinedGVSummaries[ModuleIdentifier];
783 // The module may be cached, this helps handling it.
784 ModuleCacheEntry CacheEntry(CacheOptions.Path, *Index, ModuleIdentifier,
785 ImportLists[ModuleIdentifier], ExportList,
786 ResolvedODR[ModuleIdentifier],
787 DefinedFunctions, GUIDPreservedSymbols);
790 auto ErrOrBuffer = CacheEntry.tryLoadingBuffer();
791 DEBUG(dbgs() << "Cache " << (ErrOrBuffer ? "hit" : "miss") << " '"
792 << CacheEntry.getEntryPath() << "' for buffer " << count
793 << " " << ModuleIdentifier << "\n");
797 ProducedBinaries[count] = std::move(ErrOrBuffer.get());
803 Context.setDiscardValueNames(LTODiscardValueNames);
804 Context.enableDebugTypeODRUniquing();
807 auto TheModule = loadModuleFromBuffer(ModuleBuffer, Context, false);
809 // Save temps: original file.
810 saveTempBitcode(*TheModule, SaveTempsDir, count, ".0.original.bc");
812 auto &ImportList = ImportLists[ModuleIdentifier];
813 // Run the main process now, and generates a binary
814 auto OutputBuffer = ProcessThinLTOModule(
815 *TheModule, *Index, ModuleMap, *TMBuilder.create(), ImportList,
816 ExportList, GUIDPreservedSymbols,
817 ModuleToDefinedGVSummaries[ModuleIdentifier], CacheOptions,
818 DisableCodeGen, SaveTempsDir, count);
820 OutputBuffer = CacheEntry.write(std::move(OutputBuffer));
821 ProducedBinaries[count] = std::move(OutputBuffer);
826 CachePruning(CacheOptions.Path)
827 .setPruningInterval(CacheOptions.PruningInterval)
828 .setEntryExpiration(CacheOptions.Expiration)
829 .setMaxSize(CacheOptions.MaxPercentageOfAvailableSpace)
832 // If statistics were requested, print them out now.
833 if (llvm::AreStatisticsEnabled())
834 llvm::PrintStatistics();