1 //===-ThinLTOCodeGenerator.cpp - LLVM Link Time Optimizer -----------------===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
10 // This file implements the Thin Link Time Optimization library. This library is
11 // intended to be used by linker to optimize code at link time.
12 //
13 //===----------------------------------------------------------------------===//
14 
15 #include "llvm/LTO/ThinLTOCodeGenerator.h"
16 
17 #ifdef HAVE_LLVM_REVISION
18 #include "LLVMLTORevision.h"
19 #endif
20 
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"
52 
53 #include <numeric>
54 
55 using namespace llvm;
56 
57 #define DEBUG_TYPE "thinlto"
58 
59 namespace llvm {
60 // Flags -discard-value-names, defined in LTOCodeGenerator.cpp
61 extern cl::opt<bool> LTODiscardValueNames;
62 }
63 
64 namespace {
65 
66 static cl::opt<int> ThreadCount("threads",
67                                 cl::init(std::thread::hardware_concurrency()));
68 
69 static void diagnosticHandler(const DiagnosticInfo &DI) {
70   DiagnosticPrinterRawOStream DP(errs());
71   DI.print(DP);
72   errs() << '\n';
73 }
74 
75 // Simple helper to save temporary files for debug.
76 static void saveTempBitcode(const Module &TheModule, StringRef TempDir,
77                             unsigned count, StringRef Suffix) {
78   if (TempDir.empty())
79     return;
80   // User asked to save temps, let dump the bitcode file after import.
81   auto SaveTempPath = TempDir + llvm::utostr(count) + Suffix;
82   std::error_code EC;
83   raw_fd_ostream OS(SaveTempPath.str(), EC, sys::fs::F_None);
84   if (EC)
85     report_fatal_error(Twine("Failed to open ") + SaveTempPath +
86                        " to save optimized bitcode\n");
87   WriteBitcodeToFile(&TheModule, OS, /* ShouldPreserveUseListOrder */ true);
88 }
89 
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);
98       });
99   if (StrongDefForLinker != GVSummaryList.end())
100     return StrongDefForLinker->get();
101   // Get the first *linker visible* definition for this global in the summary
102   // list.
103   auto FirstDefForLinker = llvm::find_if(
104       GVSummaryList, [](const std::unique_ptr<GlobalValueSummary> &Summary) {
105         auto Linkage = Summary->linkage();
106         return !GlobalValue::isAvailableExternallyLinkage(Linkage);
107       });
108   // Extern templates can be emitted as available_externally.
109   if (FirstDefForLinker == GVSummaryList.end())
110     return nullptr;
111   return FirstDefForLinker->get();
112 }
113 
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;
122   };
123 
124   for (auto &I : Index) {
125     if (HasMultipleCopies(I.second))
126       PrevailingCopy[I.first] = getFirstDefinitionForLinker(I.second);
127   }
128 }
129 
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()) ==
135                ModuleMap.end() &&
136            "Expect unique Buffer Identifier");
137     ModuleMap[ModuleBuffer.getBufferIdentifier()] = ModuleBuffer;
138   }
139   return ModuleMap;
140 }
141 
142 static void promoteModule(Module &TheModule, const ModuleSummaryIndex &Index) {
143   if (renameModuleForThinLTO(TheModule, Index))
144     report_fatal_error("renameModuleForThinLTO failed");
145 }
146 
147 static void
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);
154 }
155 
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?
162   PMB.OptLevel = 3;
163   PMB.LoopVectorize = true;
164   PMB.SLPVectorize = true;
165   PMB.VerifyInput = true;
166   PMB.VerifyOutput = false;
167 
168   legacy::PassManager PM;
169 
170   // Add the TTI (required to inform the vectorizer about register size for
171   // instance)
172   PM.add(createTargetTransformInfoWrapperPass(TM.getTargetIRAnalysis()));
173 
174   // Add optimizations
175   PMB.populateThinLTOPassManager(PM);
176 
177   PM.run(TheModule);
178 }
179 
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));
190   }
191   return GUIDPreservedSymbols;
192 }
193 
194 std::unique_ptr<MemoryBuffer> codegenModule(Module &TheModule,
195                                             TargetMachine &TM) {
196   SmallVector<char, 128> OutputBuffer;
197 
198   // CodeGen
199   {
200     raw_svector_ostream OS(OutputBuffer);
201     legacy::PassManager PM;
202 
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());
206 
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");
211 
212     // Run codegen now. resulting binary is in OutputBuffer.
213     PM.run(TheModule);
214   }
215   return make_unique<ObjectMemoryBuffer>(std::move(OutputBuffer));
216 }
217 
218 /// Manage caching for a single Module.
219 class ModuleCacheEntry {
220   SmallString<128> EntryPath;
221 
222 public:
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.
226   ModuleCacheEntry(
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())
234       return;
235 
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.
240 
241     SHA1 Hasher;
242 
243     // Start with the compiler revision
244     Hasher.update(LLVM_VERSION_STRING);
245 #ifdef HAVE_LLVM_REVISION
246     Hasher.update(LLVM_REVISION);
247 #endif
248 
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)));
255 
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)));
260     }
261 
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)));
268     }
269 
270     // Include the hash for the preserved symbols.
271     for (auto &Entry : PreservedSymbols) {
272       if (DefinedFunctions.count(Entry))
273         Hasher.update(
274             ArrayRef<uint8_t>((const uint8_t *)&Entry, sizeof(GlobalValue::GUID)));
275     }
276 
277     sys::path::append(EntryPath, CachePath, toHex(Hasher.result()));
278   }
279 
280   // Access the path to this entry in the cache.
281   StringRef getEntryPath() { return EntryPath; }
282 
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);
288   }
289 
290   // Cache the Produced object file
291   std::unique_ptr<MemoryBuffer>
292   write(std::unique_ptr<MemoryBuffer> OutputBuffer) {
293     if (EntryPath.empty())
294       return OutputBuffer;
295 
296     // Write to a temporary to avoid race condition
297     SmallString<128> TempFilename;
298     int TempFD;
299     std::error_code EC =
300         sys::fs::createTemporaryFile("Thin", "tmp.o", TempFD, TempFilename);
301     if (EC) {
302       errs() << "Error: " << EC.message() << "\n";
303       report_fatal_error("ThinLTO: Can't get a temporary file");
304     }
305     {
306       raw_fd_ostream OS(TempFD, /* ShouldClose */ true);
307       OS << OutputBuffer->getBuffer();
308     }
309     // Rename to final destination (hopefully race condition won't matter here)
310     EC = sys::fs::rename(TempFilename, EntryPath);
311     if (EC) {
312       sys::fs::remove(TempFilename);
313       raw_fd_ostream OS(EntryPath, EC, sys::fs::F_None);
314       if (EC)
315         report_fatal_error(Twine("Failed to open ") + EntryPath +
316                            " to save cached entry\n");
317       OS << OutputBuffer->getBuffer();
318     }
319     auto ReloadedBufferOrErr = MemoryBuffer::getFile(EntryPath);
320     if (auto EC = ReloadedBufferOrErr.getError()) {
321       // FIXME diagnose
322       errs() << "error: can't reload cached file '" << EntryPath
323              << "': " << EC.message() << "\n";
324       return OutputBuffer;
325     }
326     return std::move(*ReloadedBufferOrErr);
327   }
328 };
329 
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,
339                      unsigned count) {
340 
341   // "Benchmark"-like optimization: single-source case
342   bool SingleModule = (ModuleMap.size() == 1);
343 
344   if (!SingleModule) {
345     promoteModule(TheModule, Index);
346 
347     // Apply summary-based LinkOnce/Weak resolution decisions.
348     thinLTOResolveWeakForLinkerModule(TheModule, DefinedGlobals);
349 
350     // Save temps: after promotion.
351     saveTempBitcode(TheModule, SaveTempsDir, count, ".1.promoted.bc");
352   }
353 
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);
359   }
360 
361   // Save internalized bitcode
362   saveTempBitcode(TheModule, SaveTempsDir, count, ".2.internalized.bc");
363 
364   if (!SingleModule) {
365     crossImportIntoModule(TheModule, Index, ModuleMap, ImportList);
366 
367     // Save temps: after cross-module import.
368     saveTempBitcode(TheModule, SaveTempsDir, count, ".3.imported.bc");
369   }
370 
371   optimizeModule(TheModule, TM);
372 
373   saveTempBitcode(TheModule, SaveTempsDir, count, ".4.opt.bc");
374 
375   if (DisableCodeGen) {
376     // Configured to stop before CodeGen, serialize the bitcode and return.
377     SmallVector<char, 128> OutputBuffer;
378     {
379       raw_svector_ostream OS(OutputBuffer);
380       ModuleSummaryIndexBuilder IndexBuilder(&TheModule);
381       WriteBitcodeToFile(&TheModule, OS, true, &IndexBuilder.getIndex());
382     }
383     return make_unique<ObjectMemoryBuffer>(std::move(OutputBuffer));
384   }
385 
386   return codegenModule(TheModule, TM);
387 }
388 
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     const StringMap<FunctionImporter::ExportSetTy> &ExportLists,
397     const DenseSet<GlobalValue::GUID> &GUIDPreservedSymbols,
398     StringMap<std::map<GlobalValue::GUID, GlobalValue::LinkageTypes>>
399         &ResolvedODR) {
400 
401   DenseMap<GlobalValue::GUID, const GlobalValueSummary *> PrevailingCopy;
402   computePrevailingCopies(Index, PrevailingCopy);
403 
404   auto isPrevailing = [&](GlobalValue::GUID GUID, const GlobalValueSummary *S) {
405     const auto &Prevailing = PrevailingCopy.find(GUID);
406     // Not in map means that there was only one copy, which must be prevailing.
407     if (Prevailing == PrevailingCopy.end())
408       return true;
409     return Prevailing->second == S;
410   };
411 
412   auto isExported = [&](StringRef ModuleIdentifier, GlobalValue::GUID GUID) {
413     const auto &ExportList = ExportLists.find(ModuleIdentifier);
414     return (ExportList != ExportLists.end() &&
415             ExportList->second.count(GUID)) ||
416            GUIDPreservedSymbols.count(GUID);
417   };
418 
419   auto recordNewLinkage = [&](StringRef ModuleIdentifier,
420                               GlobalValue::GUID GUID,
421                               GlobalValue::LinkageTypes NewLinkage) {
422     ResolvedODR[ModuleIdentifier][GUID] = NewLinkage;
423   };
424 
425   thinLTOResolveWeakForLinkerInIndex(Index, isPrevailing, isExported,
426                                      recordNewLinkage);
427 }
428 
429 // Initialize the TargetMachine builder for a given Triple
430 static void initTMBuilder(TargetMachineBuilder &TMBuilder,
431                           const Triple &TheTriple) {
432   // Set a default CPU for Darwin triples (copied from LTOCodeGenerator).
433   // FIXME this looks pretty terrible...
434   if (TMBuilder.MCpu.empty() && TheTriple.isOSDarwin()) {
435     if (TheTriple.getArch() == llvm::Triple::x86_64)
436       TMBuilder.MCpu = "core2";
437     else if (TheTriple.getArch() == llvm::Triple::x86)
438       TMBuilder.MCpu = "yonah";
439     else if (TheTriple.getArch() == llvm::Triple::aarch64)
440       TMBuilder.MCpu = "cyclone";
441   }
442   TMBuilder.TheTriple = std::move(TheTriple);
443 }
444 
445 } // end anonymous namespace
446 
447 void ThinLTOCodeGenerator::addModule(StringRef Identifier, StringRef Data) {
448   MemoryBufferRef Buffer(Data, Identifier);
449   if (Modules.empty()) {
450     // First module added, so initialize the triple and some options
451     LLVMContext Context;
452     Triple TheTriple(getBitcodeTargetTriple(Buffer, Context));
453     initTMBuilder(TMBuilder, Triple(TheTriple));
454   }
455 #ifndef NDEBUG
456   else {
457     LLVMContext Context;
458     assert(TMBuilder.TheTriple.str() ==
459                getBitcodeTargetTriple(Buffer, Context) &&
460            "ThinLTO modules with different triple not supported");
461   }
462 #endif
463   Modules.push_back(Buffer);
464 }
465 
466 void ThinLTOCodeGenerator::preserveSymbol(StringRef Name) {
467   PreservedSymbols.insert(Name);
468 }
469 
470 void ThinLTOCodeGenerator::crossReferenceSymbol(StringRef Name) {
471   // FIXME: At the moment, we don't take advantage of this extra information,
472   // we're conservatively considering cross-references as preserved.
473   //  CrossReferencedSymbols.insert(Name);
474   PreservedSymbols.insert(Name);
475 }
476 
477 // TargetMachine factory
478 std::unique_ptr<TargetMachine> TargetMachineBuilder::create() const {
479   std::string ErrMsg;
480   const Target *TheTarget =
481       TargetRegistry::lookupTarget(TheTriple.str(), ErrMsg);
482   if (!TheTarget) {
483     report_fatal_error("Can't load target for this Triple: " + ErrMsg);
484   }
485 
486   // Use MAttr as the default set of features.
487   SubtargetFeatures Features(MAttr);
488   Features.getDefaultSubtargetFeatures(TheTriple);
489   std::string FeatureStr = Features.getString();
490   return std::unique_ptr<TargetMachine>(TheTarget->createTargetMachine(
491       TheTriple.str(), MCpu, FeatureStr, Options, RelocModel,
492       CodeModel::Default, CGOptLevel));
493 }
494 
495 /**
496  * Produce the combined summary index from all the bitcode files:
497  * "thin-link".
498  */
499 std::unique_ptr<ModuleSummaryIndex> ThinLTOCodeGenerator::linkCombinedIndex() {
500   std::unique_ptr<ModuleSummaryIndex> CombinedIndex;
501   uint64_t NextModuleId = 0;
502   for (auto &ModuleBuffer : Modules) {
503     ErrorOr<std::unique_ptr<object::ModuleSummaryIndexObjectFile>> ObjOrErr =
504         object::ModuleSummaryIndexObjectFile::create(ModuleBuffer,
505                                                      diagnosticHandler);
506     if (std::error_code EC = ObjOrErr.getError()) {
507       // FIXME diagnose
508       errs() << "error: can't create ModuleSummaryIndexObjectFile for buffer: "
509              << EC.message() << "\n";
510       return nullptr;
511     }
512     auto Index = (*ObjOrErr)->takeIndex();
513     if (CombinedIndex) {
514       CombinedIndex->mergeFrom(std::move(Index), ++NextModuleId);
515     } else {
516       CombinedIndex = std::move(Index);
517     }
518   }
519   return CombinedIndex;
520 }
521 
522 /**
523  * Perform promotion and renaming of exported internal functions.
524  * Index is updated to reflect linkage changes from weak resolution.
525  */
526 void ThinLTOCodeGenerator::promote(Module &TheModule,
527                                    ModuleSummaryIndex &Index) {
528   auto ModuleCount = Index.modulePaths().size();
529   auto ModuleIdentifier = TheModule.getModuleIdentifier();
530   // Collect for each module the list of function it defines (GUID -> Summary).
531   StringMap<GVSummaryMapTy> ModuleToDefinedGVSummaries;
532   Index.collectDefinedGVSummariesPerModule(ModuleToDefinedGVSummaries);
533 
534   // Generate import/export list
535   StringMap<FunctionImporter::ImportMapTy> ImportLists(ModuleCount);
536   StringMap<FunctionImporter::ExportSetTy> ExportLists(ModuleCount);
537   ComputeCrossModuleImport(Index, ModuleToDefinedGVSummaries, ImportLists,
538                            ExportLists);
539 
540   // Convert the preserved symbols set from string to GUID
541   auto GUIDPreservedSymbols =
542   computeGUIDPreservedSymbols(PreservedSymbols, TMBuilder.TheTriple);
543 
544   // Resolve LinkOnce/Weak symbols.
545   StringMap<std::map<GlobalValue::GUID, GlobalValue::LinkageTypes>> ResolvedODR;
546   resolveWeakForLinkerInIndex(Index, ExportLists, GUIDPreservedSymbols,
547                               ResolvedODR);
548 
549   thinLTOResolveWeakForLinkerModule(
550       TheModule, ModuleToDefinedGVSummaries[ModuleIdentifier]);
551 
552   promoteModule(TheModule, Index);
553 }
554 
555 /**
556  * Perform cross-module importing for the module identified by ModuleIdentifier.
557  */
558 void ThinLTOCodeGenerator::crossModuleImport(Module &TheModule,
559                                              ModuleSummaryIndex &Index) {
560   auto ModuleMap = generateModuleMap(Modules);
561   auto ModuleCount = Index.modulePaths().size();
562 
563   // Collect for each module the list of function it defines (GUID -> Summary).
564   StringMap<GVSummaryMapTy> ModuleToDefinedGVSummaries(ModuleCount);
565   Index.collectDefinedGVSummariesPerModule(ModuleToDefinedGVSummaries);
566 
567   // Generate import/export list
568   StringMap<FunctionImporter::ImportMapTy> ImportLists(ModuleCount);
569   StringMap<FunctionImporter::ExportSetTy> ExportLists(ModuleCount);
570   ComputeCrossModuleImport(Index, ModuleToDefinedGVSummaries, ImportLists,
571                            ExportLists);
572   auto &ImportList = ImportLists[TheModule.getModuleIdentifier()];
573 
574   crossImportIntoModule(TheModule, Index, ModuleMap, ImportList);
575 }
576 
577 /**
578  * Compute the list of summaries needed for importing into module.
579  */
580 void ThinLTOCodeGenerator::gatherImportedSummariesForModule(
581     StringRef ModulePath, ModuleSummaryIndex &Index,
582     std::map<std::string, GVSummaryMapTy> &ModuleToSummariesForIndex) {
583   auto ModuleCount = Index.modulePaths().size();
584 
585   // Collect for each module the list of function it defines (GUID -> Summary).
586   StringMap<GVSummaryMapTy> ModuleToDefinedGVSummaries(ModuleCount);
587   Index.collectDefinedGVSummariesPerModule(ModuleToDefinedGVSummaries);
588 
589   // Generate import/export list
590   StringMap<FunctionImporter::ImportMapTy> ImportLists(ModuleCount);
591   StringMap<FunctionImporter::ExportSetTy> ExportLists(ModuleCount);
592   ComputeCrossModuleImport(Index, ModuleToDefinedGVSummaries, ImportLists,
593                            ExportLists);
594 
595   llvm::gatherImportedSummariesForModule(ModulePath, ModuleToDefinedGVSummaries,
596                                          ImportLists,
597                                          ModuleToSummariesForIndex);
598 }
599 
600 /**
601  * Emit the list of files needed for importing into module.
602  */
603 void ThinLTOCodeGenerator::emitImports(StringRef ModulePath,
604                                        StringRef OutputName,
605                                        ModuleSummaryIndex &Index) {
606   auto ModuleCount = Index.modulePaths().size();
607 
608   // Collect for each module the list of function it defines (GUID -> Summary).
609   StringMap<GVSummaryMapTy> ModuleToDefinedGVSummaries(ModuleCount);
610   Index.collectDefinedGVSummariesPerModule(ModuleToDefinedGVSummaries);
611 
612   // Generate import/export list
613   StringMap<FunctionImporter::ImportMapTy> ImportLists(ModuleCount);
614   StringMap<FunctionImporter::ExportSetTy> ExportLists(ModuleCount);
615   ComputeCrossModuleImport(Index, ModuleToDefinedGVSummaries, ImportLists,
616                            ExportLists);
617 
618   std::error_code EC;
619   if ((EC = EmitImportsFiles(ModulePath, OutputName, ImportLists)))
620     report_fatal_error(Twine("Failed to open ") + OutputName +
621                        " to save imports lists\n");
622 }
623 
624 /**
625  * Perform internalization. Index is updated to reflect linkage changes.
626  */
627 void ThinLTOCodeGenerator::internalize(Module &TheModule,
628                                        ModuleSummaryIndex &Index) {
629   initTMBuilder(TMBuilder, Triple(TheModule.getTargetTriple()));
630   auto ModuleCount = Index.modulePaths().size();
631   auto ModuleIdentifier = TheModule.getModuleIdentifier();
632 
633   // Convert the preserved symbols set from string to GUID
634   auto GUIDPreservedSymbols =
635       computeGUIDPreservedSymbols(PreservedSymbols, TMBuilder.TheTriple);
636 
637   // Collect for each module the list of function it defines (GUID -> Summary).
638   StringMap<GVSummaryMapTy> ModuleToDefinedGVSummaries(ModuleCount);
639   Index.collectDefinedGVSummariesPerModule(ModuleToDefinedGVSummaries);
640 
641   // Generate import/export list
642   StringMap<FunctionImporter::ImportMapTy> ImportLists(ModuleCount);
643   StringMap<FunctionImporter::ExportSetTy> ExportLists(ModuleCount);
644   ComputeCrossModuleImport(Index, ModuleToDefinedGVSummaries, ImportLists,
645                            ExportLists);
646   auto &ExportList = ExportLists[ModuleIdentifier];
647 
648   // Be friendly and don't nuke totally the module when the client didn't
649   // supply anything to preserve.
650   if (ExportList.empty() && GUIDPreservedSymbols.empty())
651     return;
652 
653   // Internalization
654   auto isExported = [&](StringRef ModuleIdentifier, GlobalValue::GUID GUID) {
655     const auto &ExportList = ExportLists.find(ModuleIdentifier);
656     return (ExportList != ExportLists.end() &&
657             ExportList->second.count(GUID)) ||
658            GUIDPreservedSymbols.count(GUID);
659   };
660   thinLTOInternalizeAndPromoteInIndex(Index, isExported);
661   thinLTOInternalizeModule(TheModule,
662                            ModuleToDefinedGVSummaries[ModuleIdentifier]);
663 }
664 
665 /**
666  * Perform post-importing ThinLTO optimizations.
667  */
668 void ThinLTOCodeGenerator::optimize(Module &TheModule) {
669   initTMBuilder(TMBuilder, Triple(TheModule.getTargetTriple()));
670 
671   // Optimize now
672   optimizeModule(TheModule, *TMBuilder.create());
673 }
674 
675 /**
676  * Perform ThinLTO CodeGen.
677  */
678 std::unique_ptr<MemoryBuffer> ThinLTOCodeGenerator::codegen(Module &TheModule) {
679   initTMBuilder(TMBuilder, Triple(TheModule.getTargetTriple()));
680   return codegenModule(TheModule, *TMBuilder.create());
681 }
682 
683 // Main entry point for the ThinLTO processing
684 void ThinLTOCodeGenerator::run() {
685   if (CodeGenOnly) {
686     // Perform only parallel codegen and return.
687     ThreadPool Pool;
688     assert(ProducedBinaries.empty() && "The generator should not be reused");
689     ProducedBinaries.resize(Modules.size());
690     int count = 0;
691     for (auto &ModuleBuffer : Modules) {
692       Pool.async([&](int count) {
693         LLVMContext Context;
694         Context.setDiscardValueNames(LTODiscardValueNames);
695 
696         // Parse module now
697         auto TheModule = loadModuleFromBuffer(ModuleBuffer, Context, false);
698 
699         // CodeGen
700         ProducedBinaries[count] = codegen(*TheModule);
701       }, count++);
702     }
703 
704     return;
705   }
706 
707   // Sequential linking phase
708   auto Index = linkCombinedIndex();
709 
710   // Save temps: index.
711   if (!SaveTempsDir.empty()) {
712     auto SaveTempPath = SaveTempsDir + "index.bc";
713     std::error_code EC;
714     raw_fd_ostream OS(SaveTempPath, EC, sys::fs::F_None);
715     if (EC)
716       report_fatal_error(Twine("Failed to open ") + SaveTempPath +
717                          " to save optimized bitcode\n");
718     WriteIndexToFile(*Index, OS);
719   }
720 
721   // Prepare the resulting object vector
722   assert(ProducedBinaries.empty() && "The generator should not be reused");
723   ProducedBinaries.resize(Modules.size());
724 
725   // Prepare the module map.
726   auto ModuleMap = generateModuleMap(Modules);
727   auto ModuleCount = Modules.size();
728 
729   // Collect for each module the list of function it defines (GUID -> Summary).
730   StringMap<GVSummaryMapTy> ModuleToDefinedGVSummaries(ModuleCount);
731   Index->collectDefinedGVSummariesPerModule(ModuleToDefinedGVSummaries);
732 
733   // Collect the import/export lists for all modules from the call-graph in the
734   // combined index.
735   StringMap<FunctionImporter::ImportMapTy> ImportLists(ModuleCount);
736   StringMap<FunctionImporter::ExportSetTy> ExportLists(ModuleCount);
737   ComputeCrossModuleImport(*Index, ModuleToDefinedGVSummaries, ImportLists,
738                            ExportLists);
739 
740   // Convert the preserved symbols set from string to GUID, this is needed for
741   // computing the caching hash and the internalization.
742   auto GUIDPreservedSymbols =
743       computeGUIDPreservedSymbols(PreservedSymbols, TMBuilder.TheTriple);
744 
745   // We use a std::map here to be able to have a defined ordering when
746   // producing a hash for the cache entry.
747   // FIXME: we should be able to compute the caching hash for the entry based
748   // on the index, and nuke this map.
749   StringMap<std::map<GlobalValue::GUID, GlobalValue::LinkageTypes>> ResolvedODR;
750 
751   // Resolve LinkOnce/Weak symbols, this has to be computed early because it
752   // impacts the caching.
753   resolveWeakForLinkerInIndex(*Index, ExportLists, GUIDPreservedSymbols,
754                               ResolvedODR);
755 
756   auto isExported = [&](StringRef ModuleIdentifier, GlobalValue::GUID GUID) {
757     const auto &ExportList = ExportLists.find(ModuleIdentifier);
758     return (ExportList != ExportLists.end() &&
759             ExportList->second.count(GUID)) ||
760            GUIDPreservedSymbols.count(GUID);
761   };
762 
763   // Use global summary-based analysis to identify symbols that can be
764   // internalized (because they aren't exported or preserved as per callback).
765   // Changes are made in the index, consumed in the ThinLTO backends.
766   thinLTOInternalizeAndPromoteInIndex(*Index, isExported);
767 
768   // Make sure that every module has an entry in the ExportLists and
769   // ResolvedODR maps to enable threaded access to these maps below.
770   for (auto &DefinedGVSummaries : ModuleToDefinedGVSummaries) {
771     ExportLists[DefinedGVSummaries.first()];
772     ResolvedODR[DefinedGVSummaries.first()];
773   }
774 
775   // Compute the ordering we will process the inputs: the rough heuristic here
776   // is to sort them per size so that the largest module get schedule as soon as
777   // possible. This is purely a compile-time optimization.
778   std::vector<int> ModulesOrdering;
779   ModulesOrdering.resize(Modules.size());
780   std::iota(ModulesOrdering.begin(), ModulesOrdering.end(), 0);
781   std::sort(ModulesOrdering.begin(), ModulesOrdering.end(),
782             [&](int LeftIndex, int RightIndex) {
783               auto LSize = Modules[LeftIndex].getBufferSize();
784               auto RSize = Modules[RightIndex].getBufferSize();
785               return LSize > RSize;
786             });
787 
788   // Parallel optimizer + codegen
789   {
790     ThreadPool Pool(ThreadCount);
791     for (auto IndexCount : ModulesOrdering) {
792       auto &ModuleBuffer = Modules[IndexCount];
793       Pool.async([&](int count) {
794         auto ModuleIdentifier = ModuleBuffer.getBufferIdentifier();
795         auto &ExportList = ExportLists[ModuleIdentifier];
796 
797         auto &DefinedFunctions = ModuleToDefinedGVSummaries[ModuleIdentifier];
798 
799         // The module may be cached, this helps handling it.
800         ModuleCacheEntry CacheEntry(CacheOptions.Path, *Index, ModuleIdentifier,
801                                     ImportLists[ModuleIdentifier], ExportList,
802                                     ResolvedODR[ModuleIdentifier],
803                                     DefinedFunctions, GUIDPreservedSymbols);
804 
805         {
806           auto ErrOrBuffer = CacheEntry.tryLoadingBuffer();
807           DEBUG(dbgs() << "Cache " << (ErrOrBuffer ? "hit" : "miss") << " '"
808                        << CacheEntry.getEntryPath() << "' for buffer " << count
809                        << " " << ModuleIdentifier << "\n");
810 
811           if (ErrOrBuffer) {
812             // Cache Hit!
813             ProducedBinaries[count] = std::move(ErrOrBuffer.get());
814             return;
815           }
816         }
817 
818         LLVMContext Context;
819         Context.setDiscardValueNames(LTODiscardValueNames);
820         Context.enableDebugTypeODRUniquing();
821 
822         // Parse module now
823         auto TheModule = loadModuleFromBuffer(ModuleBuffer, Context, false);
824 
825         // Save temps: original file.
826         saveTempBitcode(*TheModule, SaveTempsDir, count, ".0.original.bc");
827 
828         auto &ImportList = ImportLists[ModuleIdentifier];
829         // Run the main process now, and generates a binary
830         auto OutputBuffer = ProcessThinLTOModule(
831             *TheModule, *Index, ModuleMap, *TMBuilder.create(), ImportList,
832             ExportList, GUIDPreservedSymbols,
833             ModuleToDefinedGVSummaries[ModuleIdentifier], CacheOptions,
834             DisableCodeGen, SaveTempsDir, count);
835 
836         OutputBuffer = CacheEntry.write(std::move(OutputBuffer));
837         ProducedBinaries[count] = std::move(OutputBuffer);
838       }, IndexCount);
839     }
840   }
841 
842   CachePruning(CacheOptions.Path)
843       .setPruningInterval(CacheOptions.PruningInterval)
844       .setEntryExpiration(CacheOptions.Expiration)
845       .setMaxSize(CacheOptions.MaxPercentageOfAvailableSpace)
846       .prune();
847 
848   // If statistics were requested, print them out now.
849   if (llvm::AreStatisticsEnabled())
850     llvm::PrintStatistics();
851 }
852