1 //===-ThinLTOCodeGenerator.cpp - LLVM Link Time Optimizer -----------------===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
10 // This file implements the Thin Link Time Optimization library. This library is
11 // intended to be used by linker to optimize code at link time.
12 //
13 //===----------------------------------------------------------------------===//
14 
15 #include "llvm/LTO/legacy/ThinLTOCodeGenerator.h"
16 
17 #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/ProfileSummaryInfo.h"
25 #include "llvm/Analysis/TargetLibraryInfo.h"
26 #include "llvm/Analysis/TargetTransformInfo.h"
27 #include "llvm/Bitcode/BitcodeReader.h"
28 #include "llvm/Bitcode/BitcodeWriter.h"
29 #include "llvm/Bitcode/BitcodeWriterPass.h"
30 #include "llvm/ExecutionEngine/ObjectMemoryBuffer.h"
31 #include "llvm/IR/DiagnosticPrinter.h"
32 #include "llvm/IR/LLVMContext.h"
33 #include "llvm/IR/LegacyPassManager.h"
34 #include "llvm/IR/Mangler.h"
35 #include "llvm/IRReader/IRReader.h"
36 #include "llvm/LTO/LTO.h"
37 #include "llvm/Linker/Linker.h"
38 #include "llvm/MC/SubtargetFeature.h"
39 #include "llvm/Object/IRObjectFile.h"
40 #include "llvm/Object/ModuleSummaryIndexObjectFile.h"
41 #include "llvm/Support/CachePruning.h"
42 #include "llvm/Support/Debug.h"
43 #include "llvm/Support/Error.h"
44 #include "llvm/Support/Path.h"
45 #include "llvm/Support/SHA1.h"
46 #include "llvm/Support/TargetRegistry.h"
47 #include "llvm/Support/ThreadPool.h"
48 #include "llvm/Support/Threading.h"
49 #include "llvm/Support/ToolOutputFile.h"
50 #include "llvm/Target/TargetMachine.h"
51 #include "llvm/Transforms/IPO.h"
52 #include "llvm/Transforms/IPO/FunctionImport.h"
53 #include "llvm/Transforms/IPO/Internalize.h"
54 #include "llvm/Transforms/IPO/PassManagerBuilder.h"
55 #include "llvm/Transforms/ObjCARC.h"
56 #include "llvm/Transforms/Utils/FunctionImportUtils.h"
57 
58 #include <numeric>
59 
60 using namespace llvm;
61 
62 #define DEBUG_TYPE "thinlto"
63 
64 namespace llvm {
65 // Flags -discard-value-names, defined in LTOCodeGenerator.cpp
66 extern cl::opt<bool> LTODiscardValueNames;
67 extern cl::opt<std::string> LTORemarksFilename;
68 extern cl::opt<bool> LTOPassRemarksWithHotness;
69 }
70 
71 namespace {
72 
73 static cl::opt<int>
74     ThreadCount("threads", cl::init(llvm::heavyweight_hardware_concurrency()));
75 
76 Expected<std::unique_ptr<tool_output_file>>
77 setupOptimizationRemarks(LLVMContext &Ctx, int Count) {
78   if (LTOPassRemarksWithHotness)
79     Ctx.setDiagnosticHotnessRequested(true);
80 
81   if (LTORemarksFilename.empty())
82     return nullptr;
83 
84   std::string FileName =
85       LTORemarksFilename + ".thin." + llvm::utostr(Count) + ".yaml";
86   std::error_code EC;
87   auto DiagnosticOutputFile =
88       llvm::make_unique<tool_output_file>(FileName, EC, sys::fs::F_None);
89   if (EC)
90     return errorCodeToError(EC);
91   Ctx.setDiagnosticsOutputFile(
92       llvm::make_unique<yaml::Output>(DiagnosticOutputFile->os()));
93   DiagnosticOutputFile->keep();
94   return std::move(DiagnosticOutputFile);
95 }
96 
97 // Simple helper to save temporary files for debug.
98 static void saveTempBitcode(const Module &TheModule, StringRef TempDir,
99                             unsigned count, StringRef Suffix) {
100   if (TempDir.empty())
101     return;
102   // User asked to save temps, let dump the bitcode file after import.
103   std::string SaveTempPath = (TempDir + llvm::utostr(count) + Suffix).str();
104   std::error_code EC;
105   raw_fd_ostream OS(SaveTempPath, EC, sys::fs::F_None);
106   if (EC)
107     report_fatal_error(Twine("Failed to open ") + SaveTempPath +
108                        " to save optimized bitcode\n");
109   WriteBitcodeToFile(&TheModule, OS, /* ShouldPreserveUseListOrder */ true);
110 }
111 
112 static const GlobalValueSummary *
113 getFirstDefinitionForLinker(const GlobalValueSummaryList &GVSummaryList) {
114   // If there is any strong definition anywhere, get it.
115   auto StrongDefForLinker = llvm::find_if(
116       GVSummaryList, [](const std::unique_ptr<GlobalValueSummary> &Summary) {
117         auto Linkage = Summary->linkage();
118         return !GlobalValue::isAvailableExternallyLinkage(Linkage) &&
119                !GlobalValue::isWeakForLinker(Linkage);
120       });
121   if (StrongDefForLinker != GVSummaryList.end())
122     return StrongDefForLinker->get();
123   // Get the first *linker visible* definition for this global in the summary
124   // list.
125   auto FirstDefForLinker = llvm::find_if(
126       GVSummaryList, [](const std::unique_ptr<GlobalValueSummary> &Summary) {
127         auto Linkage = Summary->linkage();
128         return !GlobalValue::isAvailableExternallyLinkage(Linkage);
129       });
130   // Extern templates can be emitted as available_externally.
131   if (FirstDefForLinker == GVSummaryList.end())
132     return nullptr;
133   return FirstDefForLinker->get();
134 }
135 
136 // Populate map of GUID to the prevailing copy for any multiply defined
137 // symbols. Currently assume first copy is prevailing, or any strong
138 // definition. Can be refined with Linker information in the future.
139 static void computePrevailingCopies(
140     const ModuleSummaryIndex &Index,
141     DenseMap<GlobalValue::GUID, const GlobalValueSummary *> &PrevailingCopy) {
142   auto HasMultipleCopies = [&](const GlobalValueSummaryList &GVSummaryList) {
143     return GVSummaryList.size() > 1;
144   };
145 
146   for (auto &I : Index) {
147     if (HasMultipleCopies(I.second))
148       PrevailingCopy[I.first] = getFirstDefinitionForLinker(I.second);
149   }
150 }
151 
152 static StringMap<MemoryBufferRef>
153 generateModuleMap(const std::vector<MemoryBufferRef> &Modules) {
154   StringMap<MemoryBufferRef> ModuleMap;
155   for (auto &ModuleBuffer : Modules) {
156     assert(ModuleMap.find(ModuleBuffer.getBufferIdentifier()) ==
157                ModuleMap.end() &&
158            "Expect unique Buffer Identifier");
159     ModuleMap[ModuleBuffer.getBufferIdentifier()] = ModuleBuffer;
160   }
161   return ModuleMap;
162 }
163 
164 static void promoteModule(Module &TheModule, const ModuleSummaryIndex &Index) {
165   if (renameModuleForThinLTO(TheModule, Index))
166     report_fatal_error("renameModuleForThinLTO failed");
167 }
168 
169 static std::unique_ptr<Module>
170 loadModuleFromBuffer(const MemoryBufferRef &Buffer, LLVMContext &Context,
171                      bool Lazy) {
172   SMDiagnostic Err;
173   Expected<std::unique_ptr<Module>> ModuleOrErr =
174       Lazy ? getLazyBitcodeModule(Buffer, Context,
175                                   /* ShouldLazyLoadMetadata */ true)
176            : parseBitcodeFile(Buffer, Context);
177   if (!ModuleOrErr) {
178     handleAllErrors(ModuleOrErr.takeError(), [&](ErrorInfoBase &EIB) {
179       SMDiagnostic Err = SMDiagnostic(Buffer.getBufferIdentifier(),
180                                       SourceMgr::DK_Error, EIB.message());
181       Err.print("ThinLTO", errs());
182     });
183     report_fatal_error("Can't load module, abort.");
184   }
185   return std::move(ModuleOrErr.get());
186 }
187 
188 static void
189 crossImportIntoModule(Module &TheModule, const ModuleSummaryIndex &Index,
190                       StringMap<MemoryBufferRef> &ModuleMap,
191                       const FunctionImporter::ImportMapTy &ImportList) {
192   auto Loader = [&](StringRef Identifier) {
193     return loadModuleFromBuffer(ModuleMap[Identifier], TheModule.getContext(),
194                                 /*Lazy=*/true);
195   };
196 
197   FunctionImporter Importer(Index, Loader);
198   if (!Importer.importFunctions(TheModule, ImportList))
199     report_fatal_error("importFunctions failed");
200 }
201 
202 static void optimizeModule(Module &TheModule, TargetMachine &TM) {
203   // Populate the PassManager
204   PassManagerBuilder PMB;
205   PMB.LibraryInfo = new TargetLibraryInfoImpl(TM.getTargetTriple());
206   PMB.Inliner = createFunctionInliningPass();
207   // FIXME: should get it from the bitcode?
208   PMB.OptLevel = 3;
209   PMB.LoopVectorize = true;
210   PMB.SLPVectorize = true;
211   PMB.VerifyInput = true;
212   PMB.VerifyOutput = false;
213 
214   legacy::PassManager PM;
215 
216   // Add the TTI (required to inform the vectorizer about register size for
217   // instance)
218   PM.add(createTargetTransformInfoWrapperPass(TM.getTargetIRAnalysis()));
219 
220   // Add optimizations
221   PMB.populateThinLTOPassManager(PM);
222 
223   PM.run(TheModule);
224 }
225 
226 // Convert the PreservedSymbols map from "Name" based to "GUID" based.
227 static DenseSet<GlobalValue::GUID>
228 computeGUIDPreservedSymbols(const StringSet<> &PreservedSymbols,
229                             const Triple &TheTriple) {
230   DenseSet<GlobalValue::GUID> GUIDPreservedSymbols(PreservedSymbols.size());
231   for (auto &Entry : PreservedSymbols) {
232     StringRef Name = Entry.first();
233     if (TheTriple.isOSBinFormatMachO() && Name.size() > 0 && Name[0] == '_')
234       Name = Name.drop_front();
235     GUIDPreservedSymbols.insert(GlobalValue::getGUID(Name));
236   }
237   return GUIDPreservedSymbols;
238 }
239 
240 std::unique_ptr<MemoryBuffer> codegenModule(Module &TheModule,
241                                             TargetMachine &TM) {
242   SmallVector<char, 128> OutputBuffer;
243 
244   // CodeGen
245   {
246     raw_svector_ostream OS(OutputBuffer);
247     legacy::PassManager PM;
248 
249     // If the bitcode files contain ARC code and were compiled with optimization,
250     // the ObjCARCContractPass must be run, so do it unconditionally here.
251     PM.add(createObjCARCContractPass());
252 
253     // Setup the codegen now.
254     if (TM.addPassesToEmitFile(PM, OS, TargetMachine::CGFT_ObjectFile,
255                                /* DisableVerify */ true))
256       report_fatal_error("Failed to setup codegen");
257 
258     // Run codegen now. resulting binary is in OutputBuffer.
259     PM.run(TheModule);
260   }
261   return make_unique<ObjectMemoryBuffer>(std::move(OutputBuffer));
262 }
263 
264 /// Manage caching for a single Module.
265 class ModuleCacheEntry {
266   SmallString<128> EntryPath;
267 
268 public:
269   // Create a cache entry. This compute a unique hash for the Module considering
270   // the current list of export/import, and offer an interface to query to
271   // access the content in the cache.
272   ModuleCacheEntry(
273       StringRef CachePath, const ModuleSummaryIndex &Index, StringRef ModuleID,
274       const FunctionImporter::ImportMapTy &ImportList,
275       const FunctionImporter::ExportSetTy &ExportList,
276       const std::map<GlobalValue::GUID, GlobalValue::LinkageTypes> &ResolvedODR,
277       const GVSummaryMapTy &DefinedFunctions,
278       const DenseSet<GlobalValue::GUID> &PreservedSymbols) {
279     if (CachePath.empty())
280       return;
281 
282     if (!Index.modulePaths().count(ModuleID))
283       // The module does not have an entry, it can't have a hash at all
284       return;
285 
286     // Compute the unique hash for this entry
287     // This is based on the current compiler version, the module itself, the
288     // export list, the hash for every single module in the import list, the
289     // list of ResolvedODR for the module, and the list of preserved symbols.
290 
291     // Include the hash for the current module
292     auto ModHash = Index.getModuleHash(ModuleID);
293 
294     if (all_of(ModHash, [](uint32_t V) { return V == 0; }))
295       // No hash entry, no caching!
296       return;
297 
298     SHA1 Hasher;
299 
300     // Start with the compiler revision
301     Hasher.update(LLVM_VERSION_STRING);
302 #ifdef HAVE_LLVM_REVISION
303     Hasher.update(LLVM_REVISION);
304 #endif
305 
306     Hasher.update(ArrayRef<uint8_t>((uint8_t *)&ModHash[0], sizeof(ModHash)));
307     for (auto F : ExportList)
308       // The export list can impact the internalization, be conservative here
309       Hasher.update(ArrayRef<uint8_t>((uint8_t *)&F, sizeof(F)));
310 
311     // Include the hash for every module we import functions from
312     for (auto &Entry : ImportList) {
313       auto ModHash = Index.getModuleHash(Entry.first());
314       Hasher.update(ArrayRef<uint8_t>((uint8_t *)&ModHash[0], sizeof(ModHash)));
315     }
316 
317     // Include the hash for the resolved ODR.
318     for (auto &Entry : ResolvedODR) {
319       Hasher.update(ArrayRef<uint8_t>((const uint8_t *)&Entry.first,
320                                       sizeof(GlobalValue::GUID)));
321       Hasher.update(ArrayRef<uint8_t>((const uint8_t *)&Entry.second,
322                                       sizeof(GlobalValue::LinkageTypes)));
323     }
324 
325     // Include the hash for the preserved symbols.
326     for (auto &Entry : PreservedSymbols) {
327       if (DefinedFunctions.count(Entry))
328         Hasher.update(
329             ArrayRef<uint8_t>((const uint8_t *)&Entry, sizeof(GlobalValue::GUID)));
330     }
331 
332     sys::path::append(EntryPath, CachePath, toHex(Hasher.result()));
333   }
334 
335   // Access the path to this entry in the cache.
336   StringRef getEntryPath() { return EntryPath; }
337 
338   // Try loading the buffer for this cache entry.
339   ErrorOr<std::unique_ptr<MemoryBuffer>> tryLoadingBuffer() {
340     if (EntryPath.empty())
341       return std::error_code();
342     return MemoryBuffer::getFile(EntryPath);
343   }
344 
345   // Cache the Produced object file
346   std::unique_ptr<MemoryBuffer>
347   write(std::unique_ptr<MemoryBuffer> OutputBuffer) {
348     if (EntryPath.empty())
349       return OutputBuffer;
350 
351     // Write to a temporary to avoid race condition
352     SmallString<128> TempFilename;
353     int TempFD;
354     std::error_code EC =
355         sys::fs::createTemporaryFile("Thin", "tmp.o", TempFD, TempFilename);
356     if (EC) {
357       errs() << "Error: " << EC.message() << "\n";
358       report_fatal_error("ThinLTO: Can't get a temporary file");
359     }
360     {
361       raw_fd_ostream OS(TempFD, /* ShouldClose */ true);
362       OS << OutputBuffer->getBuffer();
363     }
364     // Rename to final destination (hopefully race condition won't matter here)
365     EC = sys::fs::rename(TempFilename, EntryPath);
366     if (EC) {
367       sys::fs::remove(TempFilename);
368       raw_fd_ostream OS(EntryPath, EC, sys::fs::F_None);
369       if (EC)
370         report_fatal_error(Twine("Failed to open ") + EntryPath +
371                            " to save cached entry\n");
372       OS << OutputBuffer->getBuffer();
373     }
374     auto ReloadedBufferOrErr = MemoryBuffer::getFile(EntryPath);
375     if (auto EC = ReloadedBufferOrErr.getError()) {
376       // FIXME diagnose
377       errs() << "error: can't reload cached file '" << EntryPath
378              << "': " << EC.message() << "\n";
379       return OutputBuffer;
380     }
381     return std::move(*ReloadedBufferOrErr);
382   }
383 };
384 
385 static std::unique_ptr<MemoryBuffer>
386 ProcessThinLTOModule(Module &TheModule, ModuleSummaryIndex &Index,
387                      StringMap<MemoryBufferRef> &ModuleMap, TargetMachine &TM,
388                      const FunctionImporter::ImportMapTy &ImportList,
389                      const FunctionImporter::ExportSetTy &ExportList,
390                      const DenseSet<GlobalValue::GUID> &GUIDPreservedSymbols,
391                      const GVSummaryMapTy &DefinedGlobals,
392                      const ThinLTOCodeGenerator::CachingOptions &CacheOptions,
393                      bool DisableCodeGen, StringRef SaveTempsDir,
394                      unsigned count) {
395 
396   // "Benchmark"-like optimization: single-source case
397   bool SingleModule = (ModuleMap.size() == 1);
398 
399   if (!SingleModule) {
400     promoteModule(TheModule, Index);
401 
402     // Apply summary-based LinkOnce/Weak resolution decisions.
403     thinLTOResolveWeakForLinkerModule(TheModule, DefinedGlobals);
404 
405     // Save temps: after promotion.
406     saveTempBitcode(TheModule, SaveTempsDir, count, ".1.promoted.bc");
407   }
408 
409   // Be friendly and don't nuke totally the module when the client didn't
410   // supply anything to preserve.
411   if (!ExportList.empty() || !GUIDPreservedSymbols.empty()) {
412     // Apply summary-based internalization decisions.
413     thinLTOInternalizeModule(TheModule, DefinedGlobals);
414   }
415 
416   // Save internalized bitcode
417   saveTempBitcode(TheModule, SaveTempsDir, count, ".2.internalized.bc");
418 
419   if (!SingleModule) {
420     crossImportIntoModule(TheModule, Index, ModuleMap, ImportList);
421 
422     // Save temps: after cross-module import.
423     saveTempBitcode(TheModule, SaveTempsDir, count, ".3.imported.bc");
424   }
425 
426   optimizeModule(TheModule, TM);
427 
428   saveTempBitcode(TheModule, SaveTempsDir, count, ".4.opt.bc");
429 
430   if (DisableCodeGen) {
431     // Configured to stop before CodeGen, serialize the bitcode and return.
432     SmallVector<char, 128> OutputBuffer;
433     {
434       raw_svector_ostream OS(OutputBuffer);
435       ProfileSummaryInfo PSI(TheModule);
436       auto Index = buildModuleSummaryIndex(TheModule, nullptr, nullptr);
437       WriteBitcodeToFile(&TheModule, OS, true, &Index);
438     }
439     return make_unique<ObjectMemoryBuffer>(std::move(OutputBuffer));
440   }
441 
442   return codegenModule(TheModule, TM);
443 }
444 
445 /// Resolve LinkOnce/Weak symbols. Record resolutions in the \p ResolvedODR map
446 /// for caching, and in the \p Index for application during the ThinLTO
447 /// backends. This is needed for correctness for exported symbols (ensure
448 /// at least one copy kept) and a compile-time optimization (to drop duplicate
449 /// copies when possible).
450 static void resolveWeakForLinkerInIndex(
451     ModuleSummaryIndex &Index,
452     StringMap<std::map<GlobalValue::GUID, GlobalValue::LinkageTypes>>
453         &ResolvedODR) {
454 
455   DenseMap<GlobalValue::GUID, const GlobalValueSummary *> PrevailingCopy;
456   computePrevailingCopies(Index, PrevailingCopy);
457 
458   auto isPrevailing = [&](GlobalValue::GUID GUID, const GlobalValueSummary *S) {
459     const auto &Prevailing = PrevailingCopy.find(GUID);
460     // Not in map means that there was only one copy, which must be prevailing.
461     if (Prevailing == PrevailingCopy.end())
462       return true;
463     return Prevailing->second == S;
464   };
465 
466   auto recordNewLinkage = [&](StringRef ModuleIdentifier,
467                               GlobalValue::GUID GUID,
468                               GlobalValue::LinkageTypes NewLinkage) {
469     ResolvedODR[ModuleIdentifier][GUID] = NewLinkage;
470   };
471 
472   thinLTOResolveWeakForLinkerInIndex(Index, isPrevailing, recordNewLinkage);
473 }
474 
475 // Initialize the TargetMachine builder for a given Triple
476 static void initTMBuilder(TargetMachineBuilder &TMBuilder,
477                           const Triple &TheTriple) {
478   // Set a default CPU for Darwin triples (copied from LTOCodeGenerator).
479   // FIXME this looks pretty terrible...
480   if (TMBuilder.MCpu.empty() && TheTriple.isOSDarwin()) {
481     if (TheTriple.getArch() == llvm::Triple::x86_64)
482       TMBuilder.MCpu = "core2";
483     else if (TheTriple.getArch() == llvm::Triple::x86)
484       TMBuilder.MCpu = "yonah";
485     else if (TheTriple.getArch() == llvm::Triple::aarch64)
486       TMBuilder.MCpu = "cyclone";
487   }
488   TMBuilder.TheTriple = std::move(TheTriple);
489 }
490 
491 } // end anonymous namespace
492 
493 void ThinLTOCodeGenerator::addModule(StringRef Identifier, StringRef Data) {
494   MemoryBufferRef Buffer(Data, Identifier);
495   if (Modules.empty()) {
496     // First module added, so initialize the triple and some options
497     LLVMContext Context;
498     StringRef TripleStr;
499     ErrorOr<std::string> TripleOrErr =
500         expectedToErrorOrAndEmitErrors(Context, getBitcodeTargetTriple(Buffer));
501     if (TripleOrErr)
502       TripleStr = *TripleOrErr;
503     Triple TheTriple(TripleStr);
504     initTMBuilder(TMBuilder, Triple(TheTriple));
505   }
506 #ifndef NDEBUG
507   else {
508     LLVMContext Context;
509     StringRef TripleStr;
510     ErrorOr<std::string> TripleOrErr =
511         expectedToErrorOrAndEmitErrors(Context, getBitcodeTargetTriple(Buffer));
512     if (TripleOrErr)
513       TripleStr = *TripleOrErr;
514     assert(TMBuilder.TheTriple.str() == TripleStr &&
515            "ThinLTO modules with different triple not supported");
516   }
517 #endif
518   Modules.push_back(Buffer);
519 }
520 
521 void ThinLTOCodeGenerator::preserveSymbol(StringRef Name) {
522   PreservedSymbols.insert(Name);
523 }
524 
525 void ThinLTOCodeGenerator::crossReferenceSymbol(StringRef Name) {
526   // FIXME: At the moment, we don't take advantage of this extra information,
527   // we're conservatively considering cross-references as preserved.
528   //  CrossReferencedSymbols.insert(Name);
529   PreservedSymbols.insert(Name);
530 }
531 
532 // TargetMachine factory
533 std::unique_ptr<TargetMachine> TargetMachineBuilder::create() const {
534   std::string ErrMsg;
535   const Target *TheTarget =
536       TargetRegistry::lookupTarget(TheTriple.str(), ErrMsg);
537   if (!TheTarget) {
538     report_fatal_error("Can't load target for this Triple: " + ErrMsg);
539   }
540 
541   // Use MAttr as the default set of features.
542   SubtargetFeatures Features(MAttr);
543   Features.getDefaultSubtargetFeatures(TheTriple);
544   std::string FeatureStr = Features.getString();
545   return std::unique_ptr<TargetMachine>(TheTarget->createTargetMachine(
546       TheTriple.str(), MCpu, FeatureStr, Options, RelocModel,
547       CodeModel::Default, CGOptLevel));
548 }
549 
550 /**
551  * Produce the combined summary index from all the bitcode files:
552  * "thin-link".
553  */
554 std::unique_ptr<ModuleSummaryIndex> ThinLTOCodeGenerator::linkCombinedIndex() {
555   std::unique_ptr<ModuleSummaryIndex> CombinedIndex;
556   uint64_t NextModuleId = 0;
557   for (auto &ModuleBuffer : Modules) {
558     Expected<std::unique_ptr<object::ModuleSummaryIndexObjectFile>> ObjOrErr =
559         object::ModuleSummaryIndexObjectFile::create(ModuleBuffer);
560     if (!ObjOrErr) {
561       // FIXME diagnose
562       logAllUnhandledErrors(
563           ObjOrErr.takeError(), errs(),
564           "error: can't create ModuleSummaryIndexObjectFile for buffer: ");
565       return nullptr;
566     }
567     auto Index = (*ObjOrErr)->takeIndex();
568     if (CombinedIndex) {
569       CombinedIndex->mergeFrom(std::move(Index), ++NextModuleId);
570     } else {
571       CombinedIndex = std::move(Index);
572     }
573   }
574   return CombinedIndex;
575 }
576 
577 /**
578  * Perform promotion and renaming of exported internal functions.
579  * Index is updated to reflect linkage changes from weak resolution.
580  */
581 void ThinLTOCodeGenerator::promote(Module &TheModule,
582                                    ModuleSummaryIndex &Index) {
583   auto ModuleCount = Index.modulePaths().size();
584   auto ModuleIdentifier = TheModule.getModuleIdentifier();
585 
586   // Collect for each module the list of function it defines (GUID -> Summary).
587   StringMap<GVSummaryMapTy> ModuleToDefinedGVSummaries;
588   Index.collectDefinedGVSummariesPerModule(ModuleToDefinedGVSummaries);
589 
590   // Generate import/export list
591   StringMap<FunctionImporter::ImportMapTy> ImportLists(ModuleCount);
592   StringMap<FunctionImporter::ExportSetTy> ExportLists(ModuleCount);
593   ComputeCrossModuleImport(Index, ModuleToDefinedGVSummaries, ImportLists,
594                            ExportLists);
595 
596   // Resolve LinkOnce/Weak symbols.
597   StringMap<std::map<GlobalValue::GUID, GlobalValue::LinkageTypes>> ResolvedODR;
598   resolveWeakForLinkerInIndex(Index, ResolvedODR);
599 
600   thinLTOResolveWeakForLinkerModule(
601       TheModule, ModuleToDefinedGVSummaries[ModuleIdentifier]);
602 
603   // Convert the preserved symbols set from string to GUID
604   auto GUIDPreservedSymbols = computeGUIDPreservedSymbols(
605       PreservedSymbols, Triple(TheModule.getTargetTriple()));
606 
607   // Promote the exported values in the index, so that they are promoted
608   // in the module.
609   auto isExported = [&](StringRef ModuleIdentifier, GlobalValue::GUID GUID) {
610     const auto &ExportList = ExportLists.find(ModuleIdentifier);
611     return (ExportList != ExportLists.end() &&
612             ExportList->second.count(GUID)) ||
613            GUIDPreservedSymbols.count(GUID);
614   };
615   thinLTOInternalizeAndPromoteInIndex(Index, isExported);
616 
617   promoteModule(TheModule, Index);
618 }
619 
620 /**
621  * Perform cross-module importing for the module identified by ModuleIdentifier.
622  */
623 void ThinLTOCodeGenerator::crossModuleImport(Module &TheModule,
624                                              ModuleSummaryIndex &Index) {
625   auto ModuleMap = generateModuleMap(Modules);
626   auto ModuleCount = Index.modulePaths().size();
627 
628   // Collect for each module the list of function it defines (GUID -> Summary).
629   StringMap<GVSummaryMapTy> ModuleToDefinedGVSummaries(ModuleCount);
630   Index.collectDefinedGVSummariesPerModule(ModuleToDefinedGVSummaries);
631 
632   // Generate import/export list
633   StringMap<FunctionImporter::ImportMapTy> ImportLists(ModuleCount);
634   StringMap<FunctionImporter::ExportSetTy> ExportLists(ModuleCount);
635   ComputeCrossModuleImport(Index, ModuleToDefinedGVSummaries, ImportLists,
636                            ExportLists);
637   auto &ImportList = ImportLists[TheModule.getModuleIdentifier()];
638 
639   crossImportIntoModule(TheModule, Index, ModuleMap, ImportList);
640 }
641 
642 /**
643  * Compute the list of summaries needed for importing into module.
644  */
645 void ThinLTOCodeGenerator::gatherImportedSummariesForModule(
646     StringRef ModulePath, ModuleSummaryIndex &Index,
647     std::map<std::string, GVSummaryMapTy> &ModuleToSummariesForIndex) {
648   auto ModuleCount = Index.modulePaths().size();
649 
650   // Collect for each module the list of function it defines (GUID -> Summary).
651   StringMap<GVSummaryMapTy> ModuleToDefinedGVSummaries(ModuleCount);
652   Index.collectDefinedGVSummariesPerModule(ModuleToDefinedGVSummaries);
653 
654   // Generate import/export list
655   StringMap<FunctionImporter::ImportMapTy> ImportLists(ModuleCount);
656   StringMap<FunctionImporter::ExportSetTy> ExportLists(ModuleCount);
657   ComputeCrossModuleImport(Index, ModuleToDefinedGVSummaries, ImportLists,
658                            ExportLists);
659 
660   llvm::gatherImportedSummariesForModule(ModulePath, ModuleToDefinedGVSummaries,
661                                          ImportLists[ModulePath],
662                                          ModuleToSummariesForIndex);
663 }
664 
665 /**
666  * Emit the list of files needed for importing into module.
667  */
668 void ThinLTOCodeGenerator::emitImports(StringRef ModulePath,
669                                        StringRef OutputName,
670                                        ModuleSummaryIndex &Index) {
671   auto ModuleCount = Index.modulePaths().size();
672 
673   // Collect for each module the list of function it defines (GUID -> Summary).
674   StringMap<GVSummaryMapTy> ModuleToDefinedGVSummaries(ModuleCount);
675   Index.collectDefinedGVSummariesPerModule(ModuleToDefinedGVSummaries);
676 
677   // Generate import/export list
678   StringMap<FunctionImporter::ImportMapTy> ImportLists(ModuleCount);
679   StringMap<FunctionImporter::ExportSetTy> ExportLists(ModuleCount);
680   ComputeCrossModuleImport(Index, ModuleToDefinedGVSummaries, ImportLists,
681                            ExportLists);
682 
683   std::error_code EC;
684   if ((EC = EmitImportsFiles(ModulePath, OutputName, ImportLists[ModulePath])))
685     report_fatal_error(Twine("Failed to open ") + OutputName +
686                        " to save imports lists\n");
687 }
688 
689 /**
690  * Perform internalization. Index is updated to reflect linkage changes.
691  */
692 void ThinLTOCodeGenerator::internalize(Module &TheModule,
693                                        ModuleSummaryIndex &Index) {
694   initTMBuilder(TMBuilder, Triple(TheModule.getTargetTriple()));
695   auto ModuleCount = Index.modulePaths().size();
696   auto ModuleIdentifier = TheModule.getModuleIdentifier();
697 
698   // Convert the preserved symbols set from string to GUID
699   auto GUIDPreservedSymbols =
700       computeGUIDPreservedSymbols(PreservedSymbols, TMBuilder.TheTriple);
701 
702   // Collect for each module the list of function it defines (GUID -> Summary).
703   StringMap<GVSummaryMapTy> ModuleToDefinedGVSummaries(ModuleCount);
704   Index.collectDefinedGVSummariesPerModule(ModuleToDefinedGVSummaries);
705 
706   // Generate import/export list
707   StringMap<FunctionImporter::ImportMapTy> ImportLists(ModuleCount);
708   StringMap<FunctionImporter::ExportSetTy> ExportLists(ModuleCount);
709   ComputeCrossModuleImport(Index, ModuleToDefinedGVSummaries, ImportLists,
710                            ExportLists);
711   auto &ExportList = ExportLists[ModuleIdentifier];
712 
713   // Be friendly and don't nuke totally the module when the client didn't
714   // supply anything to preserve.
715   if (ExportList.empty() && GUIDPreservedSymbols.empty())
716     return;
717 
718   // Internalization
719   auto isExported = [&](StringRef ModuleIdentifier, GlobalValue::GUID GUID) {
720     const auto &ExportList = ExportLists.find(ModuleIdentifier);
721     return (ExportList != ExportLists.end() &&
722             ExportList->second.count(GUID)) ||
723            GUIDPreservedSymbols.count(GUID);
724   };
725   thinLTOInternalizeAndPromoteInIndex(Index, isExported);
726   thinLTOInternalizeModule(TheModule,
727                            ModuleToDefinedGVSummaries[ModuleIdentifier]);
728 }
729 
730 /**
731  * Perform post-importing ThinLTO optimizations.
732  */
733 void ThinLTOCodeGenerator::optimize(Module &TheModule) {
734   initTMBuilder(TMBuilder, Triple(TheModule.getTargetTriple()));
735 
736   // Optimize now
737   optimizeModule(TheModule, *TMBuilder.create());
738 }
739 
740 /**
741  * Perform ThinLTO CodeGen.
742  */
743 std::unique_ptr<MemoryBuffer> ThinLTOCodeGenerator::codegen(Module &TheModule) {
744   initTMBuilder(TMBuilder, Triple(TheModule.getTargetTriple()));
745   return codegenModule(TheModule, *TMBuilder.create());
746 }
747 
748 // Main entry point for the ThinLTO processing
749 void ThinLTOCodeGenerator::run() {
750   if (CodeGenOnly) {
751     // Perform only parallel codegen and return.
752     ThreadPool Pool;
753     assert(ProducedBinaries.empty() && "The generator should not be reused");
754     ProducedBinaries.resize(Modules.size());
755     int count = 0;
756     for (auto &ModuleBuffer : Modules) {
757       Pool.async([&](int count) {
758         LLVMContext Context;
759         Context.setDiscardValueNames(LTODiscardValueNames);
760 
761         // Parse module now
762         auto TheModule = loadModuleFromBuffer(ModuleBuffer, Context, false);
763 
764         // CodeGen
765         ProducedBinaries[count] = codegen(*TheModule);
766       }, count++);
767     }
768 
769     return;
770   }
771 
772   // Sequential linking phase
773   auto Index = linkCombinedIndex();
774 
775   // Save temps: index.
776   if (!SaveTempsDir.empty()) {
777     auto SaveTempPath = SaveTempsDir + "index.bc";
778     std::error_code EC;
779     raw_fd_ostream OS(SaveTempPath, EC, sys::fs::F_None);
780     if (EC)
781       report_fatal_error(Twine("Failed to open ") + SaveTempPath +
782                          " to save optimized bitcode\n");
783     WriteIndexToFile(*Index, OS);
784   }
785 
786   // Prepare the resulting object vector
787   assert(ProducedBinaries.empty() && "The generator should not be reused");
788   ProducedBinaries.resize(Modules.size());
789 
790   // Prepare the module map.
791   auto ModuleMap = generateModuleMap(Modules);
792   auto ModuleCount = Modules.size();
793 
794   // Collect for each module the list of function it defines (GUID -> Summary).
795   StringMap<GVSummaryMapTy> ModuleToDefinedGVSummaries(ModuleCount);
796   Index->collectDefinedGVSummariesPerModule(ModuleToDefinedGVSummaries);
797 
798   // Collect the import/export lists for all modules from the call-graph in the
799   // combined index.
800   StringMap<FunctionImporter::ImportMapTy> ImportLists(ModuleCount);
801   StringMap<FunctionImporter::ExportSetTy> ExportLists(ModuleCount);
802   ComputeCrossModuleImport(*Index, ModuleToDefinedGVSummaries, ImportLists,
803                            ExportLists);
804 
805   // Convert the preserved symbols set from string to GUID, this is needed for
806   // computing the caching hash and the internalization.
807   auto GUIDPreservedSymbols =
808       computeGUIDPreservedSymbols(PreservedSymbols, TMBuilder.TheTriple);
809 
810   // We use a std::map here to be able to have a defined ordering when
811   // producing a hash for the cache entry.
812   // FIXME: we should be able to compute the caching hash for the entry based
813   // on the index, and nuke this map.
814   StringMap<std::map<GlobalValue::GUID, GlobalValue::LinkageTypes>> ResolvedODR;
815 
816   // Resolve LinkOnce/Weak symbols, this has to be computed early because it
817   // impacts the caching.
818   resolveWeakForLinkerInIndex(*Index, ResolvedODR);
819 
820   auto isExported = [&](StringRef ModuleIdentifier, GlobalValue::GUID GUID) {
821     const auto &ExportList = ExportLists.find(ModuleIdentifier);
822     return (ExportList != ExportLists.end() &&
823             ExportList->second.count(GUID)) ||
824            GUIDPreservedSymbols.count(GUID);
825   };
826 
827   // Use global summary-based analysis to identify symbols that can be
828   // internalized (because they aren't exported or preserved as per callback).
829   // Changes are made in the index, consumed in the ThinLTO backends.
830   thinLTOInternalizeAndPromoteInIndex(*Index, isExported);
831 
832   // Make sure that every module has an entry in the ExportLists and
833   // ResolvedODR maps to enable threaded access to these maps below.
834   for (auto &DefinedGVSummaries : ModuleToDefinedGVSummaries) {
835     ExportLists[DefinedGVSummaries.first()];
836     ResolvedODR[DefinedGVSummaries.first()];
837   }
838 
839   // Compute the ordering we will process the inputs: the rough heuristic here
840   // is to sort them per size so that the largest module get schedule as soon as
841   // possible. This is purely a compile-time optimization.
842   std::vector<int> ModulesOrdering;
843   ModulesOrdering.resize(Modules.size());
844   std::iota(ModulesOrdering.begin(), ModulesOrdering.end(), 0);
845   std::sort(ModulesOrdering.begin(), ModulesOrdering.end(),
846             [&](int LeftIndex, int RightIndex) {
847               auto LSize = Modules[LeftIndex].getBufferSize();
848               auto RSize = Modules[RightIndex].getBufferSize();
849               return LSize > RSize;
850             });
851 
852   // Parallel optimizer + codegen
853   {
854     ThreadPool Pool(ThreadCount);
855     for (auto IndexCount : ModulesOrdering) {
856       auto &ModuleBuffer = Modules[IndexCount];
857       Pool.async([&](int count) {
858         auto ModuleIdentifier = ModuleBuffer.getBufferIdentifier();
859         auto &ExportList = ExportLists[ModuleIdentifier];
860 
861         auto &DefinedFunctions = ModuleToDefinedGVSummaries[ModuleIdentifier];
862 
863         // The module may be cached, this helps handling it.
864         ModuleCacheEntry CacheEntry(CacheOptions.Path, *Index, ModuleIdentifier,
865                                     ImportLists[ModuleIdentifier], ExportList,
866                                     ResolvedODR[ModuleIdentifier],
867                                     DefinedFunctions, GUIDPreservedSymbols);
868 
869         {
870           auto ErrOrBuffer = CacheEntry.tryLoadingBuffer();
871           DEBUG(dbgs() << "Cache " << (ErrOrBuffer ? "hit" : "miss") << " '"
872                        << CacheEntry.getEntryPath() << "' for buffer " << count
873                        << " " << ModuleIdentifier << "\n");
874 
875           if (ErrOrBuffer) {
876             // Cache Hit!
877             ProducedBinaries[count] = std::move(ErrOrBuffer.get());
878             return;
879           }
880         }
881 
882         LLVMContext Context;
883         Context.setDiscardValueNames(LTODiscardValueNames);
884         Context.enableDebugTypeODRUniquing();
885         auto DiagFileOrErr = setupOptimizationRemarks(Context, count);
886         if (!DiagFileOrErr) {
887           errs() << "Error: " << toString(DiagFileOrErr.takeError()) << "\n";
888           report_fatal_error("ThinLTO: Can't get an output file for the "
889                              "remarks");
890         }
891 
892         // Parse module now
893         auto TheModule = loadModuleFromBuffer(ModuleBuffer, Context, false);
894 
895         // Save temps: original file.
896         saveTempBitcode(*TheModule, SaveTempsDir, count, ".0.original.bc");
897 
898         auto &ImportList = ImportLists[ModuleIdentifier];
899         // Run the main process now, and generates a binary
900         auto OutputBuffer = ProcessThinLTOModule(
901             *TheModule, *Index, ModuleMap, *TMBuilder.create(), ImportList,
902             ExportList, GUIDPreservedSymbols,
903             ModuleToDefinedGVSummaries[ModuleIdentifier], CacheOptions,
904             DisableCodeGen, SaveTempsDir, count);
905 
906         OutputBuffer = CacheEntry.write(std::move(OutputBuffer));
907         ProducedBinaries[count] = std::move(OutputBuffer);
908       }, IndexCount);
909     }
910   }
911 
912   CachePruning(CacheOptions.Path)
913       .setPruningInterval(std::chrono::seconds(CacheOptions.PruningInterval))
914       .setEntryExpiration(std::chrono::seconds(CacheOptions.Expiration))
915       .setMaxSize(CacheOptions.MaxPercentageOfAvailableSpace)
916       .prune();
917 
918   // If statistics were requested, print them out now.
919   if (llvm::AreStatisticsEnabled())
920     llvm::PrintStatistics();
921 }
922