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/Path.h"
44 #include "llvm/Support/SHA1.h"
45 #include "llvm/Support/TargetRegistry.h"
46 #include "llvm/Support/ThreadPool.h"
47 #include "llvm/Support/Threading.h"
48 #include "llvm/Target/TargetMachine.h"
49 #include "llvm/Transforms/IPO.h"
50 #include "llvm/Transforms/IPO/FunctionImport.h"
51 #include "llvm/Transforms/IPO/Internalize.h"
52 #include "llvm/Transforms/IPO/PassManagerBuilder.h"
53 #include "llvm/Transforms/ObjCARC.h"
54 #include "llvm/Transforms/Utils/FunctionImportUtils.h"
60 #define DEBUG_TYPE "thinlto"
63 // Flags -discard-value-names, defined in LTOCodeGenerator.cpp
64 extern cl::opt<bool> LTODiscardValueNames;
70 ThreadCount("threads", cl::init(llvm::heavyweight_hardware_concurrency()));
72 // Simple helper to save temporary files for debug.
73 static void saveTempBitcode(const Module &TheModule, StringRef TempDir,
74 unsigned count, StringRef Suffix) {
77 // User asked to save temps, let dump the bitcode file after import.
78 std::string SaveTempPath = (TempDir + llvm::utostr(count) + Suffix).str();
80 raw_fd_ostream OS(SaveTempPath, EC, sys::fs::F_None);
82 report_fatal_error(Twine("Failed to open ") + SaveTempPath +
83 " to save optimized bitcode\n");
84 WriteBitcodeToFile(&TheModule, OS, /* ShouldPreserveUseListOrder */ true);
87 static const GlobalValueSummary *
88 getFirstDefinitionForLinker(const GlobalValueSummaryList &GVSummaryList) {
89 // If there is any strong definition anywhere, get it.
90 auto StrongDefForLinker = llvm::find_if(
91 GVSummaryList, [](const std::unique_ptr<GlobalValueSummary> &Summary) {
92 auto Linkage = Summary->linkage();
93 return !GlobalValue::isAvailableExternallyLinkage(Linkage) &&
94 !GlobalValue::isWeakForLinker(Linkage);
96 if (StrongDefForLinker != GVSummaryList.end())
97 return StrongDefForLinker->get();
98 // Get the first *linker visible* definition for this global in the summary
100 auto FirstDefForLinker = llvm::find_if(
101 GVSummaryList, [](const std::unique_ptr<GlobalValueSummary> &Summary) {
102 auto Linkage = Summary->linkage();
103 return !GlobalValue::isAvailableExternallyLinkage(Linkage);
105 // Extern templates can be emitted as available_externally.
106 if (FirstDefForLinker == GVSummaryList.end())
108 return FirstDefForLinker->get();
111 // Populate map of GUID to the prevailing copy for any multiply defined
112 // symbols. Currently assume first copy is prevailing, or any strong
113 // definition. Can be refined with Linker information in the future.
114 static void computePrevailingCopies(
115 const ModuleSummaryIndex &Index,
116 DenseMap<GlobalValue::GUID, const GlobalValueSummary *> &PrevailingCopy) {
117 auto HasMultipleCopies = [&](const GlobalValueSummaryList &GVSummaryList) {
118 return GVSummaryList.size() > 1;
121 for (auto &I : Index) {
122 if (HasMultipleCopies(I.second))
123 PrevailingCopy[I.first] = getFirstDefinitionForLinker(I.second);
127 static StringMap<MemoryBufferRef>
128 generateModuleMap(const std::vector<MemoryBufferRef> &Modules) {
129 StringMap<MemoryBufferRef> ModuleMap;
130 for (auto &ModuleBuffer : Modules) {
131 assert(ModuleMap.find(ModuleBuffer.getBufferIdentifier()) ==
133 "Expect unique Buffer Identifier");
134 ModuleMap[ModuleBuffer.getBufferIdentifier()] = ModuleBuffer;
139 static void promoteModule(Module &TheModule, const ModuleSummaryIndex &Index) {
140 if (renameModuleForThinLTO(TheModule, Index))
141 report_fatal_error("renameModuleForThinLTO failed");
145 crossImportIntoModule(Module &TheModule, const ModuleSummaryIndex &Index,
146 StringMap<MemoryBufferRef> &ModuleMap,
147 const FunctionImporter::ImportMapTy &ImportList) {
148 ModuleLoader Loader(TheModule.getContext(), ModuleMap);
149 FunctionImporter Importer(Index, Loader);
150 if (!Importer.importFunctions(TheModule, ImportList))
151 report_fatal_error("importFunctions failed");
154 static void optimizeModule(Module &TheModule, TargetMachine &TM) {
155 // Populate the PassManager
156 PassManagerBuilder PMB;
157 PMB.LibraryInfo = new TargetLibraryInfoImpl(TM.getTargetTriple());
158 PMB.Inliner = createFunctionInliningPass();
159 // FIXME: should get it from the bitcode?
161 PMB.LoopVectorize = true;
162 PMB.SLPVectorize = true;
163 PMB.VerifyInput = true;
164 PMB.VerifyOutput = false;
166 legacy::PassManager PM;
168 // Add the TTI (required to inform the vectorizer about register size for
170 PM.add(createTargetTransformInfoWrapperPass(TM.getTargetIRAnalysis()));
173 PMB.populateThinLTOPassManager(PM);
178 // Convert the PreservedSymbols map from "Name" based to "GUID" based.
179 static DenseSet<GlobalValue::GUID>
180 computeGUIDPreservedSymbols(const StringSet<> &PreservedSymbols,
181 const Triple &TheTriple) {
182 DenseSet<GlobalValue::GUID> GUIDPreservedSymbols(PreservedSymbols.size());
183 for (auto &Entry : PreservedSymbols) {
184 StringRef Name = Entry.first();
185 if (TheTriple.isOSBinFormatMachO() && Name.size() > 0 && Name[0] == '_')
186 Name = Name.drop_front();
187 GUIDPreservedSymbols.insert(GlobalValue::getGUID(Name));
189 return GUIDPreservedSymbols;
192 std::unique_ptr<MemoryBuffer> codegenModule(Module &TheModule,
194 SmallVector<char, 128> OutputBuffer;
198 raw_svector_ostream OS(OutputBuffer);
199 legacy::PassManager PM;
201 // If the bitcode files contain ARC code and were compiled with optimization,
202 // the ObjCARCContractPass must be run, so do it unconditionally here.
203 PM.add(createObjCARCContractPass());
205 // Setup the codegen now.
206 if (TM.addPassesToEmitFile(PM, OS, TargetMachine::CGFT_ObjectFile,
207 /* DisableVerify */ true))
208 report_fatal_error("Failed to setup codegen");
210 // Run codegen now. resulting binary is in OutputBuffer.
213 return make_unique<ObjectMemoryBuffer>(std::move(OutputBuffer));
216 /// Manage caching for a single Module.
217 class ModuleCacheEntry {
218 SmallString<128> EntryPath;
221 // Create a cache entry. This compute a unique hash for the Module considering
222 // the current list of export/import, and offer an interface to query to
223 // access the content in the cache.
225 StringRef CachePath, const ModuleSummaryIndex &Index, StringRef ModuleID,
226 const FunctionImporter::ImportMapTy &ImportList,
227 const FunctionImporter::ExportSetTy &ExportList,
228 const std::map<GlobalValue::GUID, GlobalValue::LinkageTypes> &ResolvedODR,
229 const GVSummaryMapTy &DefinedFunctions,
230 const DenseSet<GlobalValue::GUID> &PreservedSymbols) {
231 if (CachePath.empty())
234 if (!Index.modulePaths().count(ModuleID))
235 // The module does not have an entry, it can't have a hash at all
238 // Compute the unique hash for this entry
239 // This is based on the current compiler version, the module itself, the
240 // export list, the hash for every single module in the import list, the
241 // list of ResolvedODR for the module, and the list of preserved symbols.
243 // Include the hash for the current module
244 auto ModHash = Index.getModuleHash(ModuleID);
246 if (all_of(ModHash, [](uint32_t V) { return V == 0; }))
247 // No hash entry, no caching!
252 // Start with the compiler revision
253 Hasher.update(LLVM_VERSION_STRING);
254 #ifdef HAVE_LLVM_REVISION
255 Hasher.update(LLVM_REVISION);
258 Hasher.update(ArrayRef<uint8_t>((uint8_t *)&ModHash[0], sizeof(ModHash)));
259 for (auto F : ExportList)
260 // The export list can impact the internalization, be conservative here
261 Hasher.update(ArrayRef<uint8_t>((uint8_t *)&F, sizeof(F)));
263 // Include the hash for every module we import functions from
264 for (auto &Entry : ImportList) {
265 auto ModHash = Index.getModuleHash(Entry.first());
266 Hasher.update(ArrayRef<uint8_t>((uint8_t *)&ModHash[0], sizeof(ModHash)));
269 // Include the hash for the resolved ODR.
270 for (auto &Entry : ResolvedODR) {
271 Hasher.update(ArrayRef<uint8_t>((const uint8_t *)&Entry.first,
272 sizeof(GlobalValue::GUID)));
273 Hasher.update(ArrayRef<uint8_t>((const uint8_t *)&Entry.second,
274 sizeof(GlobalValue::LinkageTypes)));
277 // Include the hash for the preserved symbols.
278 for (auto &Entry : PreservedSymbols) {
279 if (DefinedFunctions.count(Entry))
281 ArrayRef<uint8_t>((const uint8_t *)&Entry, sizeof(GlobalValue::GUID)));
284 sys::path::append(EntryPath, CachePath, toHex(Hasher.result()));
287 // Access the path to this entry in the cache.
288 StringRef getEntryPath() { return EntryPath; }
290 // Try loading the buffer for this cache entry.
291 ErrorOr<std::unique_ptr<MemoryBuffer>> tryLoadingBuffer() {
292 if (EntryPath.empty())
293 return std::error_code();
294 return MemoryBuffer::getFile(EntryPath);
297 // Cache the Produced object file
298 std::unique_ptr<MemoryBuffer>
299 write(std::unique_ptr<MemoryBuffer> OutputBuffer) {
300 if (EntryPath.empty())
303 // Write to a temporary to avoid race condition
304 SmallString<128> TempFilename;
307 sys::fs::createTemporaryFile("Thin", "tmp.o", TempFD, TempFilename);
309 errs() << "Error: " << EC.message() << "\n";
310 report_fatal_error("ThinLTO: Can't get a temporary file");
313 raw_fd_ostream OS(TempFD, /* ShouldClose */ true);
314 OS << OutputBuffer->getBuffer();
316 // Rename to final destination (hopefully race condition won't matter here)
317 EC = sys::fs::rename(TempFilename, EntryPath);
319 sys::fs::remove(TempFilename);
320 raw_fd_ostream OS(EntryPath, EC, sys::fs::F_None);
322 report_fatal_error(Twine("Failed to open ") + EntryPath +
323 " to save cached entry\n");
324 OS << OutputBuffer->getBuffer();
326 auto ReloadedBufferOrErr = MemoryBuffer::getFile(EntryPath);
327 if (auto EC = ReloadedBufferOrErr.getError()) {
329 errs() << "error: can't reload cached file '" << EntryPath
330 << "': " << EC.message() << "\n";
333 return std::move(*ReloadedBufferOrErr);
337 static std::unique_ptr<MemoryBuffer>
338 ProcessThinLTOModule(Module &TheModule, ModuleSummaryIndex &Index,
339 StringMap<MemoryBufferRef> &ModuleMap, TargetMachine &TM,
340 const FunctionImporter::ImportMapTy &ImportList,
341 const FunctionImporter::ExportSetTy &ExportList,
342 const DenseSet<GlobalValue::GUID> &GUIDPreservedSymbols,
343 const GVSummaryMapTy &DefinedGlobals,
344 const ThinLTOCodeGenerator::CachingOptions &CacheOptions,
345 bool DisableCodeGen, StringRef SaveTempsDir,
348 // "Benchmark"-like optimization: single-source case
349 bool SingleModule = (ModuleMap.size() == 1);
352 promoteModule(TheModule, Index);
354 // Apply summary-based LinkOnce/Weak resolution decisions.
355 thinLTOResolveWeakForLinkerModule(TheModule, DefinedGlobals);
357 // Save temps: after promotion.
358 saveTempBitcode(TheModule, SaveTempsDir, count, ".1.promoted.bc");
361 // Be friendly and don't nuke totally the module when the client didn't
362 // supply anything to preserve.
363 if (!ExportList.empty() || !GUIDPreservedSymbols.empty()) {
364 // Apply summary-based internalization decisions.
365 thinLTOInternalizeModule(TheModule, DefinedGlobals);
368 // Save internalized bitcode
369 saveTempBitcode(TheModule, SaveTempsDir, count, ".2.internalized.bc");
372 crossImportIntoModule(TheModule, Index, ModuleMap, ImportList);
374 // Save temps: after cross-module import.
375 saveTempBitcode(TheModule, SaveTempsDir, count, ".3.imported.bc");
378 optimizeModule(TheModule, TM);
380 saveTempBitcode(TheModule, SaveTempsDir, count, ".4.opt.bc");
382 if (DisableCodeGen) {
383 // Configured to stop before CodeGen, serialize the bitcode and return.
384 SmallVector<char, 128> OutputBuffer;
386 raw_svector_ostream OS(OutputBuffer);
387 ProfileSummaryInfo PSI(TheModule);
388 auto Index = buildModuleSummaryIndex(TheModule, nullptr, nullptr);
389 WriteBitcodeToFile(&TheModule, OS, true, &Index);
391 return make_unique<ObjectMemoryBuffer>(std::move(OutputBuffer));
394 return codegenModule(TheModule, TM);
397 /// Resolve LinkOnce/Weak symbols. Record resolutions in the \p ResolvedODR map
398 /// for caching, and in the \p Index for application during the ThinLTO
399 /// backends. This is needed for correctness for exported symbols (ensure
400 /// at least one copy kept) and a compile-time optimization (to drop duplicate
401 /// copies when possible).
402 static void resolveWeakForLinkerInIndex(
403 ModuleSummaryIndex &Index,
404 StringMap<std::map<GlobalValue::GUID, GlobalValue::LinkageTypes>>
407 DenseMap<GlobalValue::GUID, const GlobalValueSummary *> PrevailingCopy;
408 computePrevailingCopies(Index, PrevailingCopy);
410 auto isPrevailing = [&](GlobalValue::GUID GUID, const GlobalValueSummary *S) {
411 const auto &Prevailing = PrevailingCopy.find(GUID);
412 // Not in map means that there was only one copy, which must be prevailing.
413 if (Prevailing == PrevailingCopy.end())
415 return Prevailing->second == S;
418 auto recordNewLinkage = [&](StringRef ModuleIdentifier,
419 GlobalValue::GUID GUID,
420 GlobalValue::LinkageTypes NewLinkage) {
421 ResolvedODR[ModuleIdentifier][GUID] = NewLinkage;
424 thinLTOResolveWeakForLinkerInIndex(Index, isPrevailing, recordNewLinkage);
427 // Initialize the TargetMachine builder for a given Triple
428 static void initTMBuilder(TargetMachineBuilder &TMBuilder,
429 const Triple &TheTriple) {
430 // Set a default CPU for Darwin triples (copied from LTOCodeGenerator).
431 // FIXME this looks pretty terrible...
432 if (TMBuilder.MCpu.empty() && TheTriple.isOSDarwin()) {
433 if (TheTriple.getArch() == llvm::Triple::x86_64)
434 TMBuilder.MCpu = "core2";
435 else if (TheTriple.getArch() == llvm::Triple::x86)
436 TMBuilder.MCpu = "yonah";
437 else if (TheTriple.getArch() == llvm::Triple::aarch64)
438 TMBuilder.MCpu = "cyclone";
440 TMBuilder.TheTriple = std::move(TheTriple);
443 } // end anonymous namespace
445 void ThinLTOCodeGenerator::addModule(StringRef Identifier, StringRef Data) {
446 MemoryBufferRef Buffer(Data, Identifier);
447 if (Modules.empty()) {
448 // First module added, so initialize the triple and some options
451 ErrorOr<std::string> TripleOrErr =
452 expectedToErrorOrAndEmitErrors(Context, getBitcodeTargetTriple(Buffer));
454 TripleStr = *TripleOrErr;
455 Triple TheTriple(TripleStr);
456 initTMBuilder(TMBuilder, Triple(TheTriple));
462 ErrorOr<std::string> TripleOrErr =
463 expectedToErrorOrAndEmitErrors(Context, getBitcodeTargetTriple(Buffer));
465 TripleStr = *TripleOrErr;
466 assert(TMBuilder.TheTriple.str() == TripleStr &&
467 "ThinLTO modules with different triple not supported");
470 Modules.push_back(Buffer);
473 void ThinLTOCodeGenerator::preserveSymbol(StringRef Name) {
474 PreservedSymbols.insert(Name);
477 void ThinLTOCodeGenerator::crossReferenceSymbol(StringRef Name) {
478 // FIXME: At the moment, we don't take advantage of this extra information,
479 // we're conservatively considering cross-references as preserved.
480 // CrossReferencedSymbols.insert(Name);
481 PreservedSymbols.insert(Name);
484 // TargetMachine factory
485 std::unique_ptr<TargetMachine> TargetMachineBuilder::create() const {
487 const Target *TheTarget =
488 TargetRegistry::lookupTarget(TheTriple.str(), ErrMsg);
490 report_fatal_error("Can't load target for this Triple: " + ErrMsg);
493 // Use MAttr as the default set of features.
494 SubtargetFeatures Features(MAttr);
495 Features.getDefaultSubtargetFeatures(TheTriple);
496 std::string FeatureStr = Features.getString();
497 return std::unique_ptr<TargetMachine>(TheTarget->createTargetMachine(
498 TheTriple.str(), MCpu, FeatureStr, Options, RelocModel,
499 CodeModel::Default, CGOptLevel));
503 * Produce the combined summary index from all the bitcode files:
506 std::unique_ptr<ModuleSummaryIndex> ThinLTOCodeGenerator::linkCombinedIndex() {
507 std::unique_ptr<ModuleSummaryIndex> CombinedIndex;
508 uint64_t NextModuleId = 0;
509 for (auto &ModuleBuffer : Modules) {
510 Expected<std::unique_ptr<object::ModuleSummaryIndexObjectFile>> ObjOrErr =
511 object::ModuleSummaryIndexObjectFile::create(ModuleBuffer);
514 logAllUnhandledErrors(
515 ObjOrErr.takeError(), errs(),
516 "error: can't create ModuleSummaryIndexObjectFile for buffer: ");
519 auto Index = (*ObjOrErr)->takeIndex();
521 CombinedIndex->mergeFrom(std::move(Index), ++NextModuleId);
523 CombinedIndex = std::move(Index);
526 return CombinedIndex;
530 * Perform promotion and renaming of exported internal functions.
531 * Index is updated to reflect linkage changes from weak resolution.
533 void ThinLTOCodeGenerator::promote(Module &TheModule,
534 ModuleSummaryIndex &Index) {
535 auto ModuleCount = Index.modulePaths().size();
536 auto ModuleIdentifier = TheModule.getModuleIdentifier();
538 // Collect for each module the list of function it defines (GUID -> Summary).
539 StringMap<GVSummaryMapTy> ModuleToDefinedGVSummaries;
540 Index.collectDefinedGVSummariesPerModule(ModuleToDefinedGVSummaries);
542 // Generate import/export list
543 StringMap<FunctionImporter::ImportMapTy> ImportLists(ModuleCount);
544 StringMap<FunctionImporter::ExportSetTy> ExportLists(ModuleCount);
545 ComputeCrossModuleImport(Index, ModuleToDefinedGVSummaries, ImportLists,
548 // Resolve LinkOnce/Weak symbols.
549 StringMap<std::map<GlobalValue::GUID, GlobalValue::LinkageTypes>> ResolvedODR;
550 resolveWeakForLinkerInIndex(Index, ResolvedODR);
552 thinLTOResolveWeakForLinkerModule(
553 TheModule, ModuleToDefinedGVSummaries[ModuleIdentifier]);
555 // Convert the preserved symbols set from string to GUID
556 auto GUIDPreservedSymbols = computeGUIDPreservedSymbols(
557 PreservedSymbols, Triple(TheModule.getTargetTriple()));
559 // Promote the exported values in the index, so that they are promoted
561 auto isExported = [&](StringRef ModuleIdentifier, GlobalValue::GUID GUID) {
562 const auto &ExportList = ExportLists.find(ModuleIdentifier);
563 return (ExportList != ExportLists.end() &&
564 ExportList->second.count(GUID)) ||
565 GUIDPreservedSymbols.count(GUID);
567 thinLTOInternalizeAndPromoteInIndex(Index, isExported);
569 promoteModule(TheModule, Index);
573 * Perform cross-module importing for the module identified by ModuleIdentifier.
575 void ThinLTOCodeGenerator::crossModuleImport(Module &TheModule,
576 ModuleSummaryIndex &Index) {
577 auto ModuleMap = generateModuleMap(Modules);
578 auto ModuleCount = Index.modulePaths().size();
580 // Collect for each module the list of function it defines (GUID -> Summary).
581 StringMap<GVSummaryMapTy> ModuleToDefinedGVSummaries(ModuleCount);
582 Index.collectDefinedGVSummariesPerModule(ModuleToDefinedGVSummaries);
584 // Generate import/export list
585 StringMap<FunctionImporter::ImportMapTy> ImportLists(ModuleCount);
586 StringMap<FunctionImporter::ExportSetTy> ExportLists(ModuleCount);
587 ComputeCrossModuleImport(Index, ModuleToDefinedGVSummaries, ImportLists,
589 auto &ImportList = ImportLists[TheModule.getModuleIdentifier()];
591 crossImportIntoModule(TheModule, Index, ModuleMap, ImportList);
595 * Compute the list of summaries needed for importing into module.
597 void ThinLTOCodeGenerator::gatherImportedSummariesForModule(
598 StringRef ModulePath, ModuleSummaryIndex &Index,
599 std::map<std::string, GVSummaryMapTy> &ModuleToSummariesForIndex) {
600 auto ModuleCount = Index.modulePaths().size();
602 // Collect for each module the list of function it defines (GUID -> Summary).
603 StringMap<GVSummaryMapTy> ModuleToDefinedGVSummaries(ModuleCount);
604 Index.collectDefinedGVSummariesPerModule(ModuleToDefinedGVSummaries);
606 // Generate import/export list
607 StringMap<FunctionImporter::ImportMapTy> ImportLists(ModuleCount);
608 StringMap<FunctionImporter::ExportSetTy> ExportLists(ModuleCount);
609 ComputeCrossModuleImport(Index, ModuleToDefinedGVSummaries, ImportLists,
612 llvm::gatherImportedSummariesForModule(ModulePath, ModuleToDefinedGVSummaries,
613 ImportLists[ModulePath],
614 ModuleToSummariesForIndex);
618 * Emit the list of files needed for importing into module.
620 void ThinLTOCodeGenerator::emitImports(StringRef ModulePath,
621 StringRef OutputName,
622 ModuleSummaryIndex &Index) {
623 auto ModuleCount = Index.modulePaths().size();
625 // Collect for each module the list of function it defines (GUID -> Summary).
626 StringMap<GVSummaryMapTy> ModuleToDefinedGVSummaries(ModuleCount);
627 Index.collectDefinedGVSummariesPerModule(ModuleToDefinedGVSummaries);
629 // Generate import/export list
630 StringMap<FunctionImporter::ImportMapTy> ImportLists(ModuleCount);
631 StringMap<FunctionImporter::ExportSetTy> ExportLists(ModuleCount);
632 ComputeCrossModuleImport(Index, ModuleToDefinedGVSummaries, ImportLists,
636 if ((EC = EmitImportsFiles(ModulePath, OutputName, ImportLists[ModulePath])))
637 report_fatal_error(Twine("Failed to open ") + OutputName +
638 " to save imports lists\n");
642 * Perform internalization. Index is updated to reflect linkage changes.
644 void ThinLTOCodeGenerator::internalize(Module &TheModule,
645 ModuleSummaryIndex &Index) {
646 initTMBuilder(TMBuilder, Triple(TheModule.getTargetTriple()));
647 auto ModuleCount = Index.modulePaths().size();
648 auto ModuleIdentifier = TheModule.getModuleIdentifier();
650 // Convert the preserved symbols set from string to GUID
651 auto GUIDPreservedSymbols =
652 computeGUIDPreservedSymbols(PreservedSymbols, TMBuilder.TheTriple);
654 // Collect for each module the list of function it defines (GUID -> Summary).
655 StringMap<GVSummaryMapTy> ModuleToDefinedGVSummaries(ModuleCount);
656 Index.collectDefinedGVSummariesPerModule(ModuleToDefinedGVSummaries);
658 // Generate import/export list
659 StringMap<FunctionImporter::ImportMapTy> ImportLists(ModuleCount);
660 StringMap<FunctionImporter::ExportSetTy> ExportLists(ModuleCount);
661 ComputeCrossModuleImport(Index, ModuleToDefinedGVSummaries, ImportLists,
663 auto &ExportList = ExportLists[ModuleIdentifier];
665 // Be friendly and don't nuke totally the module when the client didn't
666 // supply anything to preserve.
667 if (ExportList.empty() && GUIDPreservedSymbols.empty())
671 auto isExported = [&](StringRef ModuleIdentifier, GlobalValue::GUID GUID) {
672 const auto &ExportList = ExportLists.find(ModuleIdentifier);
673 return (ExportList != ExportLists.end() &&
674 ExportList->second.count(GUID)) ||
675 GUIDPreservedSymbols.count(GUID);
677 thinLTOInternalizeAndPromoteInIndex(Index, isExported);
678 thinLTOInternalizeModule(TheModule,
679 ModuleToDefinedGVSummaries[ModuleIdentifier]);
683 * Perform post-importing ThinLTO optimizations.
685 void ThinLTOCodeGenerator::optimize(Module &TheModule) {
686 initTMBuilder(TMBuilder, Triple(TheModule.getTargetTriple()));
689 optimizeModule(TheModule, *TMBuilder.create());
693 * Perform ThinLTO CodeGen.
695 std::unique_ptr<MemoryBuffer> ThinLTOCodeGenerator::codegen(Module &TheModule) {
696 initTMBuilder(TMBuilder, Triple(TheModule.getTargetTriple()));
697 return codegenModule(TheModule, *TMBuilder.create());
700 // Main entry point for the ThinLTO processing
701 void ThinLTOCodeGenerator::run() {
703 // Perform only parallel codegen and return.
705 assert(ProducedBinaries.empty() && "The generator should not be reused");
706 ProducedBinaries.resize(Modules.size());
708 for (auto &ModuleBuffer : Modules) {
709 Pool.async([&](int count) {
711 Context.setDiscardValueNames(LTODiscardValueNames);
714 auto TheModule = loadModuleFromBuffer(ModuleBuffer, Context, false);
717 ProducedBinaries[count] = codegen(*TheModule);
724 // Sequential linking phase
725 auto Index = linkCombinedIndex();
727 // Save temps: index.
728 if (!SaveTempsDir.empty()) {
729 auto SaveTempPath = SaveTempsDir + "index.bc";
731 raw_fd_ostream OS(SaveTempPath, EC, sys::fs::F_None);
733 report_fatal_error(Twine("Failed to open ") + SaveTempPath +
734 " to save optimized bitcode\n");
735 WriteIndexToFile(*Index, OS);
738 // Prepare the resulting object vector
739 assert(ProducedBinaries.empty() && "The generator should not be reused");
740 ProducedBinaries.resize(Modules.size());
742 // Prepare the module map.
743 auto ModuleMap = generateModuleMap(Modules);
744 auto ModuleCount = Modules.size();
746 // Collect for each module the list of function it defines (GUID -> Summary).
747 StringMap<GVSummaryMapTy> ModuleToDefinedGVSummaries(ModuleCount);
748 Index->collectDefinedGVSummariesPerModule(ModuleToDefinedGVSummaries);
750 // Collect the import/export lists for all modules from the call-graph in the
752 StringMap<FunctionImporter::ImportMapTy> ImportLists(ModuleCount);
753 StringMap<FunctionImporter::ExportSetTy> ExportLists(ModuleCount);
754 ComputeCrossModuleImport(*Index, ModuleToDefinedGVSummaries, ImportLists,
757 // Convert the preserved symbols set from string to GUID, this is needed for
758 // computing the caching hash and the internalization.
759 auto GUIDPreservedSymbols =
760 computeGUIDPreservedSymbols(PreservedSymbols, TMBuilder.TheTriple);
762 // We use a std::map here to be able to have a defined ordering when
763 // producing a hash for the cache entry.
764 // FIXME: we should be able to compute the caching hash for the entry based
765 // on the index, and nuke this map.
766 StringMap<std::map<GlobalValue::GUID, GlobalValue::LinkageTypes>> ResolvedODR;
768 // Resolve LinkOnce/Weak symbols, this has to be computed early because it
769 // impacts the caching.
770 resolveWeakForLinkerInIndex(*Index, ResolvedODR);
772 auto isExported = [&](StringRef ModuleIdentifier, GlobalValue::GUID GUID) {
773 const auto &ExportList = ExportLists.find(ModuleIdentifier);
774 return (ExportList != ExportLists.end() &&
775 ExportList->second.count(GUID)) ||
776 GUIDPreservedSymbols.count(GUID);
779 // Use global summary-based analysis to identify symbols that can be
780 // internalized (because they aren't exported or preserved as per callback).
781 // Changes are made in the index, consumed in the ThinLTO backends.
782 thinLTOInternalizeAndPromoteInIndex(*Index, isExported);
784 // Make sure that every module has an entry in the ExportLists and
785 // ResolvedODR maps to enable threaded access to these maps below.
786 for (auto &DefinedGVSummaries : ModuleToDefinedGVSummaries) {
787 ExportLists[DefinedGVSummaries.first()];
788 ResolvedODR[DefinedGVSummaries.first()];
791 // Compute the ordering we will process the inputs: the rough heuristic here
792 // is to sort them per size so that the largest module get schedule as soon as
793 // possible. This is purely a compile-time optimization.
794 std::vector<int> ModulesOrdering;
795 ModulesOrdering.resize(Modules.size());
796 std::iota(ModulesOrdering.begin(), ModulesOrdering.end(), 0);
797 std::sort(ModulesOrdering.begin(), ModulesOrdering.end(),
798 [&](int LeftIndex, int RightIndex) {
799 auto LSize = Modules[LeftIndex].getBufferSize();
800 auto RSize = Modules[RightIndex].getBufferSize();
801 return LSize > RSize;
804 // Parallel optimizer + codegen
806 ThreadPool Pool(ThreadCount);
807 for (auto IndexCount : ModulesOrdering) {
808 auto &ModuleBuffer = Modules[IndexCount];
809 Pool.async([&](int count) {
810 auto ModuleIdentifier = ModuleBuffer.getBufferIdentifier();
811 auto &ExportList = ExportLists[ModuleIdentifier];
813 auto &DefinedFunctions = ModuleToDefinedGVSummaries[ModuleIdentifier];
815 // The module may be cached, this helps handling it.
816 ModuleCacheEntry CacheEntry(CacheOptions.Path, *Index, ModuleIdentifier,
817 ImportLists[ModuleIdentifier], ExportList,
818 ResolvedODR[ModuleIdentifier],
819 DefinedFunctions, GUIDPreservedSymbols);
822 auto ErrOrBuffer = CacheEntry.tryLoadingBuffer();
823 DEBUG(dbgs() << "Cache " << (ErrOrBuffer ? "hit" : "miss") << " '"
824 << CacheEntry.getEntryPath() << "' for buffer " << count
825 << " " << ModuleIdentifier << "\n");
829 ProducedBinaries[count] = std::move(ErrOrBuffer.get());
835 Context.setDiscardValueNames(LTODiscardValueNames);
836 Context.enableDebugTypeODRUniquing();
839 auto TheModule = loadModuleFromBuffer(ModuleBuffer, Context, false);
841 // Save temps: original file.
842 saveTempBitcode(*TheModule, SaveTempsDir, count, ".0.original.bc");
844 auto &ImportList = ImportLists[ModuleIdentifier];
845 // Run the main process now, and generates a binary
846 auto OutputBuffer = ProcessThinLTOModule(
847 *TheModule, *Index, ModuleMap, *TMBuilder.create(), ImportList,
848 ExportList, GUIDPreservedSymbols,
849 ModuleToDefinedGVSummaries[ModuleIdentifier], CacheOptions,
850 DisableCodeGen, SaveTempsDir, count);
852 OutputBuffer = CacheEntry.write(std::move(OutputBuffer));
853 ProducedBinaries[count] = std::move(OutputBuffer);
858 CachePruning(CacheOptions.Path)
859 .setPruningInterval(std::chrono::seconds(CacheOptions.PruningInterval))
860 .setEntryExpiration(std::chrono::seconds(CacheOptions.Expiration))
861 .setMaxSize(CacheOptions.MaxPercentageOfAvailableSpace)
864 // If statistics were requested, print them out now.
865 if (llvm::AreStatisticsEnabled())
866 llvm::PrintStatistics();