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 #include "llvm/ADT/Statistic.h"
18 #include "llvm/ADT/StringExtras.h"
19 #include "llvm/Analysis/ModuleSummaryAnalysis.h"
20 #include "llvm/Analysis/ProfileSummaryInfo.h"
21 #include "llvm/Analysis/TargetLibraryInfo.h"
22 #include "llvm/Analysis/TargetTransformInfo.h"
23 #include "llvm/Bitcode/BitcodeReader.h"
24 #include "llvm/Bitcode/BitcodeWriter.h"
25 #include "llvm/Bitcode/BitcodeWriterPass.h"
26 #include "llvm/Config/llvm-config.h"
27 #include "llvm/IR/DebugInfo.h"
28 #include "llvm/IR/DiagnosticPrinter.h"
29 #include "llvm/IR/LLVMContext.h"
30 #include "llvm/IR/LegacyPassManager.h"
31 #include "llvm/IR/Mangler.h"
32 #include "llvm/IR/PassTimingInfo.h"
33 #include "llvm/IR/Verifier.h"
34 #include "llvm/IRReader/IRReader.h"
35 #include "llvm/LTO/LTO.h"
36 #include "llvm/MC/SubtargetFeature.h"
37 #include "llvm/Object/IRObjectFile.h"
38 #include "llvm/Support/CachePruning.h"
39 #include "llvm/Support/Debug.h"
40 #include "llvm/Support/Error.h"
41 #include "llvm/Support/Path.h"
42 #include "llvm/Support/SHA1.h"
43 #include "llvm/Support/SmallVectorMemoryBuffer.h"
44 #include "llvm/Support/TargetRegistry.h"
45 #include "llvm/Support/ThreadPool.h"
46 #include "llvm/Support/Threading.h"
47 #include "llvm/Support/ToolOutputFile.h"
48 #include "llvm/Support/VCSRevision.h"
49 #include "llvm/Target/TargetMachine.h"
50 #include "llvm/Transforms/IPO.h"
51 #include "llvm/Transforms/IPO/FunctionImport.h"
52 #include "llvm/Transforms/IPO/Internalize.h"
53 #include "llvm/Transforms/IPO/PassManagerBuilder.h"
54 #include "llvm/Transforms/ObjCARC.h"
55 #include "llvm/Transforms/Utils/FunctionImportUtils.h"
56 
57 #include <numeric>
58 
59 #if !defined(_MSC_VER) && !defined(__MINGW32__)
60 #include <unistd.h>
61 #else
62 #include <io.h>
63 #endif
64 
65 using namespace llvm;
66 
67 #define DEBUG_TYPE "thinlto"
68 
69 namespace llvm {
70 // Flags -discard-value-names, defined in LTOCodeGenerator.cpp
71 extern cl::opt<bool> LTODiscardValueNames;
72 extern cl::opt<std::string> LTORemarksFilename;
73 extern cl::opt<bool> LTOPassRemarksWithHotness;
74 }
75 
76 namespace {
77 
78 static cl::opt<int>
79     ThreadCount("threads", cl::init(llvm::heavyweight_hardware_concurrency()));
80 
81 // Simple helper to save temporary files for debug.
82 static void saveTempBitcode(const Module &TheModule, StringRef TempDir,
83                             unsigned count, StringRef Suffix) {
84   if (TempDir.empty())
85     return;
86   // User asked to save temps, let dump the bitcode file after import.
87   std::string SaveTempPath = (TempDir + llvm::Twine(count) + Suffix).str();
88   std::error_code EC;
89   raw_fd_ostream OS(SaveTempPath, EC, sys::fs::F_None);
90   if (EC)
91     report_fatal_error(Twine("Failed to open ") + SaveTempPath +
92                        " to save optimized bitcode\n");
93   WriteBitcodeToFile(TheModule, OS, /* ShouldPreserveUseListOrder */ true);
94 }
95 
96 static const GlobalValueSummary *
97 getFirstDefinitionForLinker(const GlobalValueSummaryList &GVSummaryList) {
98   // If there is any strong definition anywhere, get it.
99   auto StrongDefForLinker = llvm::find_if(
100       GVSummaryList, [](const std::unique_ptr<GlobalValueSummary> &Summary) {
101         auto Linkage = Summary->linkage();
102         return !GlobalValue::isAvailableExternallyLinkage(Linkage) &&
103                !GlobalValue::isWeakForLinker(Linkage);
104       });
105   if (StrongDefForLinker != GVSummaryList.end())
106     return StrongDefForLinker->get();
107   // Get the first *linker visible* definition for this global in the summary
108   // list.
109   auto FirstDefForLinker = llvm::find_if(
110       GVSummaryList, [](const std::unique_ptr<GlobalValueSummary> &Summary) {
111         auto Linkage = Summary->linkage();
112         return !GlobalValue::isAvailableExternallyLinkage(Linkage);
113       });
114   // Extern templates can be emitted as available_externally.
115   if (FirstDefForLinker == GVSummaryList.end())
116     return nullptr;
117   return FirstDefForLinker->get();
118 }
119 
120 // Populate map of GUID to the prevailing copy for any multiply defined
121 // symbols. Currently assume first copy is prevailing, or any strong
122 // definition. Can be refined with Linker information in the future.
123 static void computePrevailingCopies(
124     const ModuleSummaryIndex &Index,
125     DenseMap<GlobalValue::GUID, const GlobalValueSummary *> &PrevailingCopy) {
126   auto HasMultipleCopies = [&](const GlobalValueSummaryList &GVSummaryList) {
127     return GVSummaryList.size() > 1;
128   };
129 
130   for (auto &I : Index) {
131     if (HasMultipleCopies(I.second.SummaryList))
132       PrevailingCopy[I.first] =
133           getFirstDefinitionForLinker(I.second.SummaryList);
134   }
135 }
136 
137 static StringMap<MemoryBufferRef>
138 generateModuleMap(const std::vector<ThinLTOBuffer> &Modules) {
139   StringMap<MemoryBufferRef> ModuleMap;
140   for (auto &ModuleBuffer : Modules) {
141     assert(ModuleMap.find(ModuleBuffer.getBufferIdentifier()) ==
142                ModuleMap.end() &&
143            "Expect unique Buffer Identifier");
144     ModuleMap[ModuleBuffer.getBufferIdentifier()] = ModuleBuffer.getMemBuffer();
145   }
146   return ModuleMap;
147 }
148 
149 static void promoteModule(Module &TheModule, const ModuleSummaryIndex &Index) {
150   if (renameModuleForThinLTO(TheModule, Index))
151     report_fatal_error("renameModuleForThinLTO failed");
152 }
153 
154 namespace {
155 class ThinLTODiagnosticInfo : public DiagnosticInfo {
156   const Twine &Msg;
157 public:
158   ThinLTODiagnosticInfo(const Twine &DiagMsg,
159                         DiagnosticSeverity Severity = DS_Error)
160       : DiagnosticInfo(DK_Linker, Severity), Msg(DiagMsg) {}
161   void print(DiagnosticPrinter &DP) const override { DP << Msg; }
162 };
163 }
164 
165 /// Verify the module and strip broken debug info.
166 static void verifyLoadedModule(Module &TheModule) {
167   bool BrokenDebugInfo = false;
168   if (verifyModule(TheModule, &dbgs(), &BrokenDebugInfo))
169     report_fatal_error("Broken module found, compilation aborted!");
170   if (BrokenDebugInfo) {
171     TheModule.getContext().diagnose(ThinLTODiagnosticInfo(
172         "Invalid debug info found, debug info will be stripped", DS_Warning));
173     StripDebugInfo(TheModule);
174   }
175 }
176 
177 static std::unique_ptr<Module>
178 loadModuleFromBuffer(const MemoryBufferRef &Buffer, LLVMContext &Context,
179                      bool Lazy, bool IsImporting) {
180   SMDiagnostic Err;
181   Expected<std::unique_ptr<Module>> ModuleOrErr =
182       Lazy
183           ? getLazyBitcodeModule(Buffer, Context,
184                                  /* ShouldLazyLoadMetadata */ true, IsImporting)
185           : parseBitcodeFile(Buffer, Context);
186   if (!ModuleOrErr) {
187     handleAllErrors(ModuleOrErr.takeError(), [&](ErrorInfoBase &EIB) {
188       SMDiagnostic Err = SMDiagnostic(Buffer.getBufferIdentifier(),
189                                       SourceMgr::DK_Error, EIB.message());
190       Err.print("ThinLTO", errs());
191     });
192     report_fatal_error("Can't load module, abort.");
193   }
194   if (!Lazy)
195     verifyLoadedModule(*ModuleOrErr.get());
196   return std::move(ModuleOrErr.get());
197 }
198 
199 static void
200 crossImportIntoModule(Module &TheModule, const ModuleSummaryIndex &Index,
201                       StringMap<MemoryBufferRef> &ModuleMap,
202                       const FunctionImporter::ImportMapTy &ImportList) {
203   auto Loader = [&](StringRef Identifier) {
204     return loadModuleFromBuffer(ModuleMap[Identifier], TheModule.getContext(),
205                                 /*Lazy=*/true, /*IsImporting*/ true);
206   };
207 
208   FunctionImporter Importer(Index, Loader);
209   Expected<bool> Result = Importer.importFunctions(TheModule, ImportList);
210   if (!Result) {
211     handleAllErrors(Result.takeError(), [&](ErrorInfoBase &EIB) {
212       SMDiagnostic Err = SMDiagnostic(TheModule.getModuleIdentifier(),
213                                       SourceMgr::DK_Error, EIB.message());
214       Err.print("ThinLTO", errs());
215     });
216     report_fatal_error("importFunctions failed");
217   }
218   // Verify again after cross-importing.
219   verifyLoadedModule(TheModule);
220 }
221 
222 static void optimizeModule(Module &TheModule, TargetMachine &TM,
223                            unsigned OptLevel, bool Freestanding) {
224   // Populate the PassManager
225   PassManagerBuilder PMB;
226   PMB.LibraryInfo = new TargetLibraryInfoImpl(TM.getTargetTriple());
227   if (Freestanding)
228     PMB.LibraryInfo->disableAllFunctions();
229   PMB.Inliner = createFunctionInliningPass();
230   // FIXME: should get it from the bitcode?
231   PMB.OptLevel = OptLevel;
232   PMB.LoopVectorize = true;
233   PMB.SLPVectorize = true;
234   // Already did this in verifyLoadedModule().
235   PMB.VerifyInput = false;
236   PMB.VerifyOutput = false;
237 
238   legacy::PassManager PM;
239 
240   // Add the TTI (required to inform the vectorizer about register size for
241   // instance)
242   PM.add(createTargetTransformInfoWrapperPass(TM.getTargetIRAnalysis()));
243 
244   // Add optimizations
245   PMB.populateThinLTOPassManager(PM);
246 
247   PM.run(TheModule);
248 }
249 
250 // Convert the PreservedSymbols map from "Name" based to "GUID" based.
251 static DenseSet<GlobalValue::GUID>
252 computeGUIDPreservedSymbols(const StringSet<> &PreservedSymbols,
253                             const Triple &TheTriple) {
254   DenseSet<GlobalValue::GUID> GUIDPreservedSymbols(PreservedSymbols.size());
255   for (auto &Entry : PreservedSymbols) {
256     StringRef Name = Entry.first();
257     if (TheTriple.isOSBinFormatMachO() && Name.size() > 0 && Name[0] == '_')
258       Name = Name.drop_front();
259     GUIDPreservedSymbols.insert(GlobalValue::getGUID(Name));
260   }
261   return GUIDPreservedSymbols;
262 }
263 
264 std::unique_ptr<MemoryBuffer> codegenModule(Module &TheModule,
265                                             TargetMachine &TM) {
266   SmallVector<char, 128> OutputBuffer;
267 
268   // CodeGen
269   {
270     raw_svector_ostream OS(OutputBuffer);
271     legacy::PassManager PM;
272 
273     // If the bitcode files contain ARC code and were compiled with optimization,
274     // the ObjCARCContractPass must be run, so do it unconditionally here.
275     PM.add(createObjCARCContractPass());
276 
277     // Setup the codegen now.
278     if (TM.addPassesToEmitFile(PM, OS, nullptr, TargetMachine::CGFT_ObjectFile,
279                                /* DisableVerify */ true))
280       report_fatal_error("Failed to setup codegen");
281 
282     // Run codegen now. resulting binary is in OutputBuffer.
283     PM.run(TheModule);
284   }
285   return make_unique<SmallVectorMemoryBuffer>(std::move(OutputBuffer));
286 }
287 
288 /// Manage caching for a single Module.
289 class ModuleCacheEntry {
290   SmallString<128> EntryPath;
291 
292 public:
293   // Create a cache entry. This compute a unique hash for the Module considering
294   // the current list of export/import, and offer an interface to query to
295   // access the content in the cache.
296   ModuleCacheEntry(
297       StringRef CachePath, const ModuleSummaryIndex &Index, StringRef ModuleID,
298       const FunctionImporter::ImportMapTy &ImportList,
299       const FunctionImporter::ExportSetTy &ExportList,
300       const std::map<GlobalValue::GUID, GlobalValue::LinkageTypes> &ResolvedODR,
301       const GVSummaryMapTy &DefinedFunctions,
302       const DenseSet<GlobalValue::GUID> &PreservedSymbols, unsigned OptLevel,
303       bool Freestanding, const TargetMachineBuilder &TMBuilder) {
304     if (CachePath.empty())
305       return;
306 
307     if (!Index.modulePaths().count(ModuleID))
308       // The module does not have an entry, it can't have a hash at all
309       return;
310 
311     // Compute the unique hash for this entry
312     // This is based on the current compiler version, the module itself, the
313     // export list, the hash for every single module in the import list, the
314     // list of ResolvedODR for the module, and the list of preserved symbols.
315 
316     // Include the hash for the current module
317     auto ModHash = Index.getModuleHash(ModuleID);
318 
319     if (all_of(ModHash, [](uint32_t V) { return V == 0; }))
320       // No hash entry, no caching!
321       return;
322 
323     SHA1 Hasher;
324 
325     // Include the parts of the LTO configuration that affect code generation.
326     auto AddString = [&](StringRef Str) {
327       Hasher.update(Str);
328       Hasher.update(ArrayRef<uint8_t>{0});
329     };
330     auto AddUnsigned = [&](unsigned I) {
331       uint8_t Data[4];
332       Data[0] = I;
333       Data[1] = I >> 8;
334       Data[2] = I >> 16;
335       Data[3] = I >> 24;
336       Hasher.update(ArrayRef<uint8_t>{Data, 4});
337     };
338 
339     // Start with the compiler revision
340     Hasher.update(LLVM_VERSION_STRING);
341 #ifdef LLVM_REVISION
342     Hasher.update(LLVM_REVISION);
343 #endif
344 
345     // Hash the optimization level and the target machine settings.
346     AddString(TMBuilder.MCpu);
347     // FIXME: Hash more of Options. For now all clients initialize Options from
348     // command-line flags (which is unsupported in production), but may set
349     // RelaxELFRelocations. The clang driver can also pass FunctionSections,
350     // DataSections and DebuggerTuning via command line flags.
351     AddUnsigned(TMBuilder.Options.RelaxELFRelocations);
352     AddUnsigned(TMBuilder.Options.FunctionSections);
353     AddUnsigned(TMBuilder.Options.DataSections);
354     AddUnsigned((unsigned)TMBuilder.Options.DebuggerTuning);
355     AddString(TMBuilder.MAttr);
356     if (TMBuilder.RelocModel)
357       AddUnsigned(*TMBuilder.RelocModel);
358     AddUnsigned(TMBuilder.CGOptLevel);
359     AddUnsigned(OptLevel);
360     AddUnsigned(Freestanding);
361 
362     Hasher.update(ArrayRef<uint8_t>((uint8_t *)&ModHash[0], sizeof(ModHash)));
363     for (auto F : ExportList)
364       // The export list can impact the internalization, be conservative here
365       Hasher.update(ArrayRef<uint8_t>((uint8_t *)&F, sizeof(F)));
366 
367     // Include the hash for every module we import functions from
368     for (auto &Entry : ImportList) {
369       auto ModHash = Index.getModuleHash(Entry.first());
370       Hasher.update(ArrayRef<uint8_t>((uint8_t *)&ModHash[0], sizeof(ModHash)));
371     }
372 
373     // Include the hash for the resolved ODR.
374     for (auto &Entry : ResolvedODR) {
375       Hasher.update(ArrayRef<uint8_t>((const uint8_t *)&Entry.first,
376                                       sizeof(GlobalValue::GUID)));
377       Hasher.update(ArrayRef<uint8_t>((const uint8_t *)&Entry.second,
378                                       sizeof(GlobalValue::LinkageTypes)));
379     }
380 
381     // Include the hash for the preserved symbols.
382     for (auto &Entry : PreservedSymbols) {
383       if (DefinedFunctions.count(Entry))
384         Hasher.update(
385             ArrayRef<uint8_t>((const uint8_t *)&Entry, sizeof(GlobalValue::GUID)));
386     }
387 
388     // This choice of file name allows the cache to be pruned (see pruneCache()
389     // in include/llvm/Support/CachePruning.h).
390     sys::path::append(EntryPath, CachePath,
391                       "llvmcache-" + toHex(Hasher.result()));
392   }
393 
394   // Access the path to this entry in the cache.
395   StringRef getEntryPath() { return EntryPath; }
396 
397   // Try loading the buffer for this cache entry.
398   ErrorOr<std::unique_ptr<MemoryBuffer>> tryLoadingBuffer() {
399     if (EntryPath.empty())
400       return std::error_code();
401     int FD;
402     SmallString<64> ResultPath;
403     std::error_code EC = sys::fs::openFileForRead(
404         Twine(EntryPath), FD, sys::fs::OF_UpdateAtime, &ResultPath);
405     if (EC)
406       return EC;
407     ErrorOr<std::unique_ptr<MemoryBuffer>> MBOrErr =
408         MemoryBuffer::getOpenFile(FD, EntryPath,
409                                   /*FileSize*/ -1,
410                                   /*RequiresNullTerminator*/ false);
411     close(FD);
412     return MBOrErr;
413   }
414 
415   // Cache the Produced object file
416   void write(const MemoryBuffer &OutputBuffer) {
417     if (EntryPath.empty())
418       return;
419 
420     // Write to a temporary to avoid race condition
421     SmallString<128> TempFilename;
422     SmallString<128> CachePath(EntryPath);
423     int TempFD;
424     llvm::sys::path::remove_filename(CachePath);
425     sys::path::append(TempFilename, CachePath, "Thin-%%%%%%.tmp.o");
426     std::error_code EC =
427       sys::fs::createUniqueFile(TempFilename, TempFD, TempFilename);
428     if (EC) {
429       errs() << "Error: " << EC.message() << "\n";
430       report_fatal_error("ThinLTO: Can't get a temporary file");
431     }
432     {
433       raw_fd_ostream OS(TempFD, /* ShouldClose */ true);
434       OS << OutputBuffer.getBuffer();
435     }
436     // Rename temp file to final destination; rename is atomic
437     EC = sys::fs::rename(TempFilename, EntryPath);
438     if (EC)
439       sys::fs::remove(TempFilename);
440   }
441 };
442 
443 static std::unique_ptr<MemoryBuffer>
444 ProcessThinLTOModule(Module &TheModule, ModuleSummaryIndex &Index,
445                      StringMap<MemoryBufferRef> &ModuleMap, TargetMachine &TM,
446                      const FunctionImporter::ImportMapTy &ImportList,
447                      const FunctionImporter::ExportSetTy &ExportList,
448                      const DenseSet<GlobalValue::GUID> &GUIDPreservedSymbols,
449                      const GVSummaryMapTy &DefinedGlobals,
450                      const ThinLTOCodeGenerator::CachingOptions &CacheOptions,
451                      bool DisableCodeGen, StringRef SaveTempsDir,
452                      bool Freestanding, unsigned OptLevel, unsigned count) {
453 
454   // "Benchmark"-like optimization: single-source case
455   bool SingleModule = (ModuleMap.size() == 1);
456 
457   if (!SingleModule) {
458     promoteModule(TheModule, Index);
459 
460     // Apply summary-based prevailing-symbol resolution decisions.
461     thinLTOResolvePrevailingInModule(TheModule, DefinedGlobals);
462 
463     // Save temps: after promotion.
464     saveTempBitcode(TheModule, SaveTempsDir, count, ".1.promoted.bc");
465   }
466 
467   // Be friendly and don't nuke totally the module when the client didn't
468   // supply anything to preserve.
469   if (!ExportList.empty() || !GUIDPreservedSymbols.empty()) {
470     // Apply summary-based internalization decisions.
471     thinLTOInternalizeModule(TheModule, DefinedGlobals);
472   }
473 
474   // Save internalized bitcode
475   saveTempBitcode(TheModule, SaveTempsDir, count, ".2.internalized.bc");
476 
477   if (!SingleModule) {
478     crossImportIntoModule(TheModule, Index, ModuleMap, ImportList);
479 
480     // Save temps: after cross-module import.
481     saveTempBitcode(TheModule, SaveTempsDir, count, ".3.imported.bc");
482   }
483 
484   optimizeModule(TheModule, TM, OptLevel, Freestanding);
485 
486   saveTempBitcode(TheModule, SaveTempsDir, count, ".4.opt.bc");
487 
488   if (DisableCodeGen) {
489     // Configured to stop before CodeGen, serialize the bitcode and return.
490     SmallVector<char, 128> OutputBuffer;
491     {
492       raw_svector_ostream OS(OutputBuffer);
493       ProfileSummaryInfo PSI(TheModule);
494       auto Index = buildModuleSummaryIndex(TheModule, nullptr, &PSI);
495       WriteBitcodeToFile(TheModule, OS, true, &Index);
496     }
497     return make_unique<SmallVectorMemoryBuffer>(std::move(OutputBuffer));
498   }
499 
500   return codegenModule(TheModule, TM);
501 }
502 
503 /// Resolve prevailing symbols. Record resolutions in the \p ResolvedODR map
504 /// for caching, and in the \p Index for application during the ThinLTO
505 /// backends. This is needed for correctness for exported symbols (ensure
506 /// at least one copy kept) and a compile-time optimization (to drop duplicate
507 /// copies when possible).
508 static void resolvePrevailingInIndex(
509     ModuleSummaryIndex &Index,
510     StringMap<std::map<GlobalValue::GUID, GlobalValue::LinkageTypes>>
511         &ResolvedODR) {
512 
513   DenseMap<GlobalValue::GUID, const GlobalValueSummary *> PrevailingCopy;
514   computePrevailingCopies(Index, PrevailingCopy);
515 
516   auto isPrevailing = [&](GlobalValue::GUID GUID, const GlobalValueSummary *S) {
517     const auto &Prevailing = PrevailingCopy.find(GUID);
518     // Not in map means that there was only one copy, which must be prevailing.
519     if (Prevailing == PrevailingCopy.end())
520       return true;
521     return Prevailing->second == S;
522   };
523 
524   auto recordNewLinkage = [&](StringRef ModuleIdentifier,
525                               GlobalValue::GUID GUID,
526                               GlobalValue::LinkageTypes NewLinkage) {
527     ResolvedODR[ModuleIdentifier][GUID] = NewLinkage;
528   };
529 
530   thinLTOResolvePrevailingInIndex(Index, isPrevailing, recordNewLinkage);
531 }
532 
533 // Initialize the TargetMachine builder for a given Triple
534 static void initTMBuilder(TargetMachineBuilder &TMBuilder,
535                           const Triple &TheTriple) {
536   // Set a default CPU for Darwin triples (copied from LTOCodeGenerator).
537   // FIXME this looks pretty terrible...
538   if (TMBuilder.MCpu.empty() && TheTriple.isOSDarwin()) {
539     if (TheTriple.getArch() == llvm::Triple::x86_64)
540       TMBuilder.MCpu = "core2";
541     else if (TheTriple.getArch() == llvm::Triple::x86)
542       TMBuilder.MCpu = "yonah";
543     else if (TheTriple.getArch() == llvm::Triple::aarch64)
544       TMBuilder.MCpu = "cyclone";
545   }
546   TMBuilder.TheTriple = std::move(TheTriple);
547 }
548 
549 } // end anonymous namespace
550 
551 void ThinLTOCodeGenerator::addModule(StringRef Identifier, StringRef Data) {
552   ThinLTOBuffer Buffer(Data, Identifier);
553   LLVMContext Context;
554   StringRef TripleStr;
555   ErrorOr<std::string> TripleOrErr = expectedToErrorOrAndEmitErrors(
556       Context, getBitcodeTargetTriple(Buffer.getMemBuffer()));
557 
558   if (TripleOrErr)
559     TripleStr = *TripleOrErr;
560 
561   Triple TheTriple(TripleStr);
562 
563   if (Modules.empty())
564     initTMBuilder(TMBuilder, Triple(TheTriple));
565   else if (TMBuilder.TheTriple != TheTriple) {
566     if (!TMBuilder.TheTriple.isCompatibleWith(TheTriple))
567       report_fatal_error("ThinLTO modules with incompatible triples not "
568                          "supported");
569     initTMBuilder(TMBuilder, Triple(TMBuilder.TheTriple.merge(TheTriple)));
570   }
571 
572   Modules.push_back(Buffer);
573 }
574 
575 void ThinLTOCodeGenerator::preserveSymbol(StringRef Name) {
576   PreservedSymbols.insert(Name);
577 }
578 
579 void ThinLTOCodeGenerator::crossReferenceSymbol(StringRef Name) {
580   // FIXME: At the moment, we don't take advantage of this extra information,
581   // we're conservatively considering cross-references as preserved.
582   //  CrossReferencedSymbols.insert(Name);
583   PreservedSymbols.insert(Name);
584 }
585 
586 // TargetMachine factory
587 std::unique_ptr<TargetMachine> TargetMachineBuilder::create() const {
588   std::string ErrMsg;
589   const Target *TheTarget =
590       TargetRegistry::lookupTarget(TheTriple.str(), ErrMsg);
591   if (!TheTarget) {
592     report_fatal_error("Can't load target for this Triple: " + ErrMsg);
593   }
594 
595   // Use MAttr as the default set of features.
596   SubtargetFeatures Features(MAttr);
597   Features.getDefaultSubtargetFeatures(TheTriple);
598   std::string FeatureStr = Features.getString();
599 
600   return std::unique_ptr<TargetMachine>(
601       TheTarget->createTargetMachine(TheTriple.str(), MCpu, FeatureStr, Options,
602                                      RelocModel, None, CGOptLevel));
603 }
604 
605 /**
606  * Produce the combined summary index from all the bitcode files:
607  * "thin-link".
608  */
609 std::unique_ptr<ModuleSummaryIndex> ThinLTOCodeGenerator::linkCombinedIndex() {
610   std::unique_ptr<ModuleSummaryIndex> CombinedIndex =
611       llvm::make_unique<ModuleSummaryIndex>(/*HaveGVs=*/false);
612   uint64_t NextModuleId = 0;
613   for (auto &ModuleBuffer : Modules) {
614     if (Error Err = readModuleSummaryIndex(ModuleBuffer.getMemBuffer(),
615                                            *CombinedIndex, NextModuleId++)) {
616       // FIXME diagnose
617       logAllUnhandledErrors(
618           std::move(Err), errs(),
619           "error: can't create module summary index for buffer: ");
620       return nullptr;
621     }
622   }
623   return CombinedIndex;
624 }
625 
626 static void internalizeAndPromoteInIndex(
627     const StringMap<FunctionImporter::ExportSetTy> &ExportLists,
628     const DenseSet<GlobalValue::GUID> &GUIDPreservedSymbols,
629     ModuleSummaryIndex &Index) {
630   auto isExported = [&](StringRef ModuleIdentifier, GlobalValue::GUID GUID) {
631     const auto &ExportList = ExportLists.find(ModuleIdentifier);
632     return (ExportList != ExportLists.end() &&
633             ExportList->second.count(GUID)) ||
634            GUIDPreservedSymbols.count(GUID);
635   };
636 
637   thinLTOInternalizeAndPromoteInIndex(Index, isExported);
638 }
639 
640 static void computeDeadSymbolsInIndex(
641     ModuleSummaryIndex &Index,
642     const DenseSet<GlobalValue::GUID> &GUIDPreservedSymbols) {
643   // We have no symbols resolution available. And can't do any better now in the
644   // case where the prevailing symbol is in a native object. It can be refined
645   // with linker information in the future.
646   auto isPrevailing = [&](GlobalValue::GUID G) {
647     return PrevailingType::Unknown;
648   };
649   computeDeadSymbols(Index, GUIDPreservedSymbols, isPrevailing);
650 }
651 
652 /**
653  * Perform promotion and renaming of exported internal functions.
654  * Index is updated to reflect linkage changes from weak resolution.
655  */
656 void ThinLTOCodeGenerator::promote(Module &TheModule,
657                                    ModuleSummaryIndex &Index) {
658   auto ModuleCount = Index.modulePaths().size();
659   auto ModuleIdentifier = TheModule.getModuleIdentifier();
660 
661   // Collect for each module the list of function it defines (GUID -> Summary).
662   StringMap<GVSummaryMapTy> ModuleToDefinedGVSummaries;
663   Index.collectDefinedGVSummariesPerModule(ModuleToDefinedGVSummaries);
664 
665   // Convert the preserved symbols set from string to GUID
666   auto GUIDPreservedSymbols = computeGUIDPreservedSymbols(
667       PreservedSymbols, Triple(TheModule.getTargetTriple()));
668 
669   // Compute "dead" symbols, we don't want to import/export these!
670   computeDeadSymbolsInIndex(Index, GUIDPreservedSymbols);
671 
672   // Generate import/export list
673   StringMap<FunctionImporter::ImportMapTy> ImportLists(ModuleCount);
674   StringMap<FunctionImporter::ExportSetTy> ExportLists(ModuleCount);
675   ComputeCrossModuleImport(Index, ModuleToDefinedGVSummaries, ImportLists,
676                            ExportLists);
677 
678   // Resolve prevailing symbols
679   StringMap<std::map<GlobalValue::GUID, GlobalValue::LinkageTypes>> ResolvedODR;
680   resolvePrevailingInIndex(Index, ResolvedODR);
681 
682   thinLTOResolvePrevailingInModule(
683       TheModule, ModuleToDefinedGVSummaries[ModuleIdentifier]);
684 
685   // Promote the exported values in the index, so that they are promoted
686   // in the module.
687   internalizeAndPromoteInIndex(ExportLists, GUIDPreservedSymbols, Index);
688 
689   promoteModule(TheModule, Index);
690 }
691 
692 /**
693  * Perform cross-module importing for the module identified by ModuleIdentifier.
694  */
695 void ThinLTOCodeGenerator::crossModuleImport(Module &TheModule,
696                                              ModuleSummaryIndex &Index) {
697   auto ModuleMap = generateModuleMap(Modules);
698   auto ModuleCount = Index.modulePaths().size();
699 
700   // Collect for each module the list of function it defines (GUID -> Summary).
701   StringMap<GVSummaryMapTy> ModuleToDefinedGVSummaries(ModuleCount);
702   Index.collectDefinedGVSummariesPerModule(ModuleToDefinedGVSummaries);
703 
704   // Convert the preserved symbols set from string to GUID
705   auto GUIDPreservedSymbols = computeGUIDPreservedSymbols(
706       PreservedSymbols, Triple(TheModule.getTargetTriple()));
707 
708   // Compute "dead" symbols, we don't want to import/export these!
709   computeDeadSymbolsInIndex(Index, GUIDPreservedSymbols);
710 
711   // Generate import/export list
712   StringMap<FunctionImporter::ImportMapTy> ImportLists(ModuleCount);
713   StringMap<FunctionImporter::ExportSetTy> ExportLists(ModuleCount);
714   ComputeCrossModuleImport(Index, ModuleToDefinedGVSummaries, ImportLists,
715                            ExportLists);
716   auto &ImportList = ImportLists[TheModule.getModuleIdentifier()];
717 
718   crossImportIntoModule(TheModule, Index, ModuleMap, ImportList);
719 }
720 
721 /**
722  * Compute the list of summaries needed for importing into module.
723  */
724 void ThinLTOCodeGenerator::gatherImportedSummariesForModule(
725     StringRef ModulePath, ModuleSummaryIndex &Index,
726     std::map<std::string, GVSummaryMapTy> &ModuleToSummariesForIndex) {
727   auto ModuleCount = Index.modulePaths().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   // Generate import/export list
734   StringMap<FunctionImporter::ImportMapTy> ImportLists(ModuleCount);
735   StringMap<FunctionImporter::ExportSetTy> ExportLists(ModuleCount);
736   ComputeCrossModuleImport(Index, ModuleToDefinedGVSummaries, ImportLists,
737                            ExportLists);
738 
739   llvm::gatherImportedSummariesForModule(ModulePath, ModuleToDefinedGVSummaries,
740                                          ImportLists[ModulePath],
741                                          ModuleToSummariesForIndex);
742 }
743 
744 /**
745  * Emit the list of files needed for importing into module.
746  */
747 void ThinLTOCodeGenerator::emitImports(StringRef ModulePath,
748                                        StringRef OutputName,
749                                        ModuleSummaryIndex &Index) {
750   auto ModuleCount = Index.modulePaths().size();
751 
752   // Collect for each module the list of function it defines (GUID -> Summary).
753   StringMap<GVSummaryMapTy> ModuleToDefinedGVSummaries(ModuleCount);
754   Index.collectDefinedGVSummariesPerModule(ModuleToDefinedGVSummaries);
755 
756   // Generate import/export list
757   StringMap<FunctionImporter::ImportMapTy> ImportLists(ModuleCount);
758   StringMap<FunctionImporter::ExportSetTy> ExportLists(ModuleCount);
759   ComputeCrossModuleImport(Index, ModuleToDefinedGVSummaries, ImportLists,
760                            ExportLists);
761 
762   std::map<std::string, GVSummaryMapTy> ModuleToSummariesForIndex;
763   llvm::gatherImportedSummariesForModule(ModulePath, ModuleToDefinedGVSummaries,
764                                          ImportLists[ModulePath],
765                                          ModuleToSummariesForIndex);
766 
767   std::error_code EC;
768   if ((EC =
769            EmitImportsFiles(ModulePath, OutputName, ModuleToSummariesForIndex)))
770     report_fatal_error(Twine("Failed to open ") + OutputName +
771                        " to save imports lists\n");
772 }
773 
774 /**
775  * Perform internalization. Index is updated to reflect linkage changes.
776  */
777 void ThinLTOCodeGenerator::internalize(Module &TheModule,
778                                        ModuleSummaryIndex &Index) {
779   initTMBuilder(TMBuilder, Triple(TheModule.getTargetTriple()));
780   auto ModuleCount = Index.modulePaths().size();
781   auto ModuleIdentifier = TheModule.getModuleIdentifier();
782 
783   // Convert the preserved symbols set from string to GUID
784   auto GUIDPreservedSymbols =
785       computeGUIDPreservedSymbols(PreservedSymbols, TMBuilder.TheTriple);
786 
787   // Collect for each module the list of function it defines (GUID -> Summary).
788   StringMap<GVSummaryMapTy> ModuleToDefinedGVSummaries(ModuleCount);
789   Index.collectDefinedGVSummariesPerModule(ModuleToDefinedGVSummaries);
790 
791   // Compute "dead" symbols, we don't want to import/export these!
792   computeDeadSymbolsInIndex(Index, GUIDPreservedSymbols);
793 
794   // Generate import/export list
795   StringMap<FunctionImporter::ImportMapTy> ImportLists(ModuleCount);
796   StringMap<FunctionImporter::ExportSetTy> ExportLists(ModuleCount);
797   ComputeCrossModuleImport(Index, ModuleToDefinedGVSummaries, ImportLists,
798                            ExportLists);
799   auto &ExportList = ExportLists[ModuleIdentifier];
800 
801   // Be friendly and don't nuke totally the module when the client didn't
802   // supply anything to preserve.
803   if (ExportList.empty() && GUIDPreservedSymbols.empty())
804     return;
805 
806   // Internalization
807   internalizeAndPromoteInIndex(ExportLists, GUIDPreservedSymbols, Index);
808   thinLTOInternalizeModule(TheModule,
809                            ModuleToDefinedGVSummaries[ModuleIdentifier]);
810 }
811 
812 /**
813  * Perform post-importing ThinLTO optimizations.
814  */
815 void ThinLTOCodeGenerator::optimize(Module &TheModule) {
816   initTMBuilder(TMBuilder, Triple(TheModule.getTargetTriple()));
817 
818   // Optimize now
819   optimizeModule(TheModule, *TMBuilder.create(), OptLevel, Freestanding);
820 }
821 
822 /// Write out the generated object file, either from CacheEntryPath or from
823 /// OutputBuffer, preferring hard-link when possible.
824 /// Returns the path to the generated file in SavedObjectsDirectoryPath.
825 static std::string writeGeneratedObject(int count, StringRef CacheEntryPath,
826                                         StringRef SavedObjectsDirectoryPath,
827                                         const MemoryBuffer &OutputBuffer) {
828   SmallString<128> OutputPath(SavedObjectsDirectoryPath);
829   llvm::sys::path::append(OutputPath, Twine(count) + ".thinlto.o");
830   OutputPath.c_str(); // Ensure the string is null terminated.
831   if (sys::fs::exists(OutputPath))
832     sys::fs::remove(OutputPath);
833 
834   // We don't return a memory buffer to the linker, just a list of files.
835   if (!CacheEntryPath.empty()) {
836     // Cache is enabled, hard-link the entry (or copy if hard-link fails).
837     auto Err = sys::fs::create_hard_link(CacheEntryPath, OutputPath);
838     if (!Err)
839       return OutputPath.str();
840     // Hard linking failed, try to copy.
841     Err = sys::fs::copy_file(CacheEntryPath, OutputPath);
842     if (!Err)
843       return OutputPath.str();
844     // Copy failed (could be because the CacheEntry was removed from the cache
845     // in the meantime by another process), fall back and try to write down the
846     // buffer to the output.
847     errs() << "error: can't link or copy from cached entry '" << CacheEntryPath
848            << "' to '" << OutputPath << "'\n";
849   }
850   // No cache entry, just write out the buffer.
851   std::error_code Err;
852   raw_fd_ostream OS(OutputPath, Err, sys::fs::F_None);
853   if (Err)
854     report_fatal_error("Can't open output '" + OutputPath + "'\n");
855   OS << OutputBuffer.getBuffer();
856   return OutputPath.str();
857 }
858 
859 // Main entry point for the ThinLTO processing
860 void ThinLTOCodeGenerator::run() {
861   // Prepare the resulting object vector
862   assert(ProducedBinaries.empty() && "The generator should not be reused");
863   if (SavedObjectsDirectoryPath.empty())
864     ProducedBinaries.resize(Modules.size());
865   else {
866     sys::fs::create_directories(SavedObjectsDirectoryPath);
867     bool IsDir;
868     sys::fs::is_directory(SavedObjectsDirectoryPath, IsDir);
869     if (!IsDir)
870       report_fatal_error("Unexistent dir: '" + SavedObjectsDirectoryPath + "'");
871     ProducedBinaryFiles.resize(Modules.size());
872   }
873 
874   if (CodeGenOnly) {
875     // Perform only parallel codegen and return.
876     ThreadPool Pool;
877     int count = 0;
878     for (auto &ModuleBuffer : Modules) {
879       Pool.async([&](int count) {
880         LLVMContext Context;
881         Context.setDiscardValueNames(LTODiscardValueNames);
882 
883         // Parse module now
884         auto TheModule =
885             loadModuleFromBuffer(ModuleBuffer.getMemBuffer(), Context, false,
886                                  /*IsImporting*/ false);
887 
888         // CodeGen
889         auto OutputBuffer = codegenModule(*TheModule, *TMBuilder.create());
890         if (SavedObjectsDirectoryPath.empty())
891           ProducedBinaries[count] = std::move(OutputBuffer);
892         else
893           ProducedBinaryFiles[count] = writeGeneratedObject(
894               count, "", SavedObjectsDirectoryPath, *OutputBuffer);
895       }, count++);
896     }
897 
898     return;
899   }
900 
901   // Sequential linking phase
902   auto Index = linkCombinedIndex();
903 
904   // Save temps: index.
905   if (!SaveTempsDir.empty()) {
906     auto SaveTempPath = SaveTempsDir + "index.bc";
907     std::error_code EC;
908     raw_fd_ostream OS(SaveTempPath, EC, sys::fs::F_None);
909     if (EC)
910       report_fatal_error(Twine("Failed to open ") + SaveTempPath +
911                          " to save optimized bitcode\n");
912     WriteIndexToFile(*Index, OS);
913   }
914 
915 
916   // Prepare the module map.
917   auto ModuleMap = generateModuleMap(Modules);
918   auto ModuleCount = Modules.size();
919 
920   // Collect for each module the list of function it defines (GUID -> Summary).
921   StringMap<GVSummaryMapTy> ModuleToDefinedGVSummaries(ModuleCount);
922   Index->collectDefinedGVSummariesPerModule(ModuleToDefinedGVSummaries);
923 
924   // Convert the preserved symbols set from string to GUID, this is needed for
925   // computing the caching hash and the internalization.
926   auto GUIDPreservedSymbols =
927       computeGUIDPreservedSymbols(PreservedSymbols, TMBuilder.TheTriple);
928 
929   // Compute "dead" symbols, we don't want to import/export these!
930   computeDeadSymbolsInIndex(*Index, GUIDPreservedSymbols);
931 
932   // Collect the import/export lists for all modules from the call-graph in the
933   // combined index.
934   StringMap<FunctionImporter::ImportMapTy> ImportLists(ModuleCount);
935   StringMap<FunctionImporter::ExportSetTy> ExportLists(ModuleCount);
936   ComputeCrossModuleImport(*Index, ModuleToDefinedGVSummaries, ImportLists,
937                            ExportLists);
938 
939   // We use a std::map here to be able to have a defined ordering when
940   // producing a hash for the cache entry.
941   // FIXME: we should be able to compute the caching hash for the entry based
942   // on the index, and nuke this map.
943   StringMap<std::map<GlobalValue::GUID, GlobalValue::LinkageTypes>> ResolvedODR;
944 
945   // Resolve prevailing symbols, this has to be computed early because it
946   // impacts the caching.
947   resolvePrevailingInIndex(*Index, ResolvedODR);
948 
949   // Use global summary-based analysis to identify symbols that can be
950   // internalized (because they aren't exported or preserved as per callback).
951   // Changes are made in the index, consumed in the ThinLTO backends.
952   internalizeAndPromoteInIndex(ExportLists, GUIDPreservedSymbols, *Index);
953 
954   // Make sure that every module has an entry in the ExportLists, ImportList,
955   // GVSummary and ResolvedODR maps to enable threaded access to these maps
956   // below.
957   for (auto &Module : Modules) {
958     auto ModuleIdentifier = Module.getBufferIdentifier();
959     ExportLists[ModuleIdentifier];
960     ImportLists[ModuleIdentifier];
961     ResolvedODR[ModuleIdentifier];
962     ModuleToDefinedGVSummaries[ModuleIdentifier];
963   }
964 
965   // Compute the ordering we will process the inputs: the rough heuristic here
966   // is to sort them per size so that the largest module get schedule as soon as
967   // possible. This is purely a compile-time optimization.
968   std::vector<int> ModulesOrdering;
969   ModulesOrdering.resize(Modules.size());
970   std::iota(ModulesOrdering.begin(), ModulesOrdering.end(), 0);
971   llvm::sort(ModulesOrdering, [&](int LeftIndex, int RightIndex) {
972     auto LSize = Modules[LeftIndex].getBuffer().size();
973     auto RSize = Modules[RightIndex].getBuffer().size();
974     return LSize > RSize;
975   });
976 
977   // Parallel optimizer + codegen
978   {
979     ThreadPool Pool(ThreadCount);
980     for (auto IndexCount : ModulesOrdering) {
981       auto &ModuleBuffer = Modules[IndexCount];
982       Pool.async([&](int count) {
983         auto ModuleIdentifier = ModuleBuffer.getBufferIdentifier();
984         auto &ExportList = ExportLists[ModuleIdentifier];
985 
986         auto &DefinedFunctions = ModuleToDefinedGVSummaries[ModuleIdentifier];
987 
988         // The module may be cached, this helps handling it.
989         ModuleCacheEntry CacheEntry(CacheOptions.Path, *Index, ModuleIdentifier,
990                                     ImportLists[ModuleIdentifier], ExportList,
991                                     ResolvedODR[ModuleIdentifier],
992                                     DefinedFunctions, GUIDPreservedSymbols,
993                                     OptLevel, Freestanding, TMBuilder);
994         auto CacheEntryPath = CacheEntry.getEntryPath();
995 
996         {
997           auto ErrOrBuffer = CacheEntry.tryLoadingBuffer();
998           LLVM_DEBUG(dbgs() << "Cache " << (ErrOrBuffer ? "hit" : "miss")
999                             << " '" << CacheEntryPath << "' for buffer "
1000                             << count << " " << ModuleIdentifier << "\n");
1001 
1002           if (ErrOrBuffer) {
1003             // Cache Hit!
1004             if (SavedObjectsDirectoryPath.empty())
1005               ProducedBinaries[count] = std::move(ErrOrBuffer.get());
1006             else
1007               ProducedBinaryFiles[count] = writeGeneratedObject(
1008                   count, CacheEntryPath, SavedObjectsDirectoryPath,
1009                   *ErrOrBuffer.get());
1010             return;
1011           }
1012         }
1013 
1014         LLVMContext Context;
1015         Context.setDiscardValueNames(LTODiscardValueNames);
1016         Context.enableDebugTypeODRUniquing();
1017         auto DiagFileOrErr = lto::setupOptimizationRemarks(
1018             Context, LTORemarksFilename, LTOPassRemarksWithHotness, count);
1019         if (!DiagFileOrErr) {
1020           errs() << "Error: " << toString(DiagFileOrErr.takeError()) << "\n";
1021           report_fatal_error("ThinLTO: Can't get an output file for the "
1022                              "remarks");
1023         }
1024 
1025         // Parse module now
1026         auto TheModule =
1027             loadModuleFromBuffer(ModuleBuffer.getMemBuffer(), Context, false,
1028                                  /*IsImporting*/ false);
1029 
1030         // Save temps: original file.
1031         saveTempBitcode(*TheModule, SaveTempsDir, count, ".0.original.bc");
1032 
1033         auto &ImportList = ImportLists[ModuleIdentifier];
1034         // Run the main process now, and generates a binary
1035         auto OutputBuffer = ProcessThinLTOModule(
1036             *TheModule, *Index, ModuleMap, *TMBuilder.create(), ImportList,
1037             ExportList, GUIDPreservedSymbols,
1038             ModuleToDefinedGVSummaries[ModuleIdentifier], CacheOptions,
1039             DisableCodeGen, SaveTempsDir, Freestanding, OptLevel, count);
1040 
1041         // Commit to the cache (if enabled)
1042         CacheEntry.write(*OutputBuffer);
1043 
1044         if (SavedObjectsDirectoryPath.empty()) {
1045           // We need to generated a memory buffer for the linker.
1046           if (!CacheEntryPath.empty()) {
1047             // When cache is enabled, reload from the cache if possible.
1048             // Releasing the buffer from the heap and reloading it from the
1049             // cache file with mmap helps us to lower memory pressure.
1050             // The freed memory can be used for the next input file.
1051             // The final binary link will read from the VFS cache (hopefully!)
1052             // or from disk (if the memory pressure was too high).
1053             auto ReloadedBufferOrErr = CacheEntry.tryLoadingBuffer();
1054             if (auto EC = ReloadedBufferOrErr.getError()) {
1055               // On error, keep the preexisting buffer and print a diagnostic.
1056               errs() << "error: can't reload cached file '" << CacheEntryPath
1057                      << "': " << EC.message() << "\n";
1058             } else {
1059               OutputBuffer = std::move(*ReloadedBufferOrErr);
1060             }
1061           }
1062           ProducedBinaries[count] = std::move(OutputBuffer);
1063           return;
1064         }
1065         ProducedBinaryFiles[count] = writeGeneratedObject(
1066             count, CacheEntryPath, SavedObjectsDirectoryPath, *OutputBuffer);
1067       }, IndexCount);
1068     }
1069   }
1070 
1071   pruneCache(CacheOptions.Path, CacheOptions.Policy);
1072 
1073   // If statistics were requested, print them out now.
1074   if (llvm::AreStatisticsEnabled())
1075     llvm::PrintStatistics();
1076   reportAndResetTimings();
1077 }
1078