1 //===-ThinLTOCodeGenerator.cpp - LLVM Link Time Optimizer -----------------===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 //
9 // This file implements the Thin Link Time Optimization library. This library is
10 // intended to be used by linker to optimize code at link time.
11 //
12 //===----------------------------------------------------------------------===//
13 
14 #include "llvm/LTO/legacy/ThinLTOCodeGenerator.h"
15 #include "llvm/Support/CommandLine.h"
16 
17 #include "llvm/ADT/ScopeExit.h"
18 #include "llvm/ADT/Statistic.h"
19 #include "llvm/ADT/StringExtras.h"
20 #include "llvm/Analysis/AliasAnalysis.h"
21 #include "llvm/Analysis/ModuleSummaryAnalysis.h"
22 #include "llvm/Analysis/ProfileSummaryInfo.h"
23 #include "llvm/Analysis/TargetLibraryInfo.h"
24 #include "llvm/Analysis/TargetTransformInfo.h"
25 #include "llvm/Bitcode/BitcodeReader.h"
26 #include "llvm/Bitcode/BitcodeWriter.h"
27 #include "llvm/Bitcode/BitcodeWriterPass.h"
28 #include "llvm/Config/llvm-config.h"
29 #include "llvm/IR/DebugInfo.h"
30 #include "llvm/IR/DiagnosticPrinter.h"
31 #include "llvm/IR/LLVMContext.h"
32 #include "llvm/IR/LLVMRemarkStreamer.h"
33 #include "llvm/IR/Mangler.h"
34 #include "llvm/IR/PassTimingInfo.h"
35 #include "llvm/IR/Verifier.h"
36 #include "llvm/IRReader/IRReader.h"
37 #include "llvm/LTO/LTO.h"
38 #include "llvm/LTO/SummaryBasedOptimizations.h"
39 #include "llvm/MC/SubtargetFeature.h"
40 #include "llvm/MC/TargetRegistry.h"
41 #include "llvm/Object/IRObjectFile.h"
42 #include "llvm/Passes/PassBuilder.h"
43 #include "llvm/Passes/StandardInstrumentations.h"
44 #include "llvm/Remarks/HotnessThresholdParser.h"
45 #include "llvm/Support/CachePruning.h"
46 #include "llvm/Support/Debug.h"
47 #include "llvm/Support/Error.h"
48 #include "llvm/Support/FileUtilities.h"
49 #include "llvm/Support/Path.h"
50 #include "llvm/Support/SHA1.h"
51 #include "llvm/Support/SmallVectorMemoryBuffer.h"
52 #include "llvm/Support/ThreadPool.h"
53 #include "llvm/Support/Threading.h"
54 #include "llvm/Support/ToolOutputFile.h"
55 #include "llvm/Target/TargetMachine.h"
56 #include "llvm/Transforms/IPO.h"
57 #include "llvm/Transforms/IPO/FunctionAttrs.h"
58 #include "llvm/Transforms/IPO/FunctionImport.h"
59 #include "llvm/Transforms/IPO/Internalize.h"
60 #include "llvm/Transforms/IPO/WholeProgramDevirt.h"
61 #include "llvm/Transforms/ObjCARC.h"
62 #include "llvm/Transforms/Utils/FunctionImportUtils.h"
63 
64 #include <numeric>
65 
66 #if !defined(_MSC_VER) && !defined(__MINGW32__)
67 #include <unistd.h>
68 #else
69 #include <io.h>
70 #endif
71 
72 using namespace llvm;
73 
74 #define DEBUG_TYPE "thinlto"
75 
76 namespace llvm {
77 // Flags -discard-value-names, defined in LTOCodeGenerator.cpp
78 extern cl::opt<bool> LTODiscardValueNames;
79 extern cl::opt<std::string> RemarksFilename;
80 extern cl::opt<std::string> RemarksPasses;
81 extern cl::opt<bool> RemarksWithHotness;
82 extern cl::opt<Optional<uint64_t>, false, remarks::HotnessThresholdParser>
83     RemarksHotnessThreshold;
84 extern cl::opt<std::string> RemarksFormat;
85 }
86 
87 namespace {
88 
89 // Default to using all available threads in the system, but using only one
90 // thred per core, as indicated by the usage of
91 // heavyweight_hardware_concurrency() below.
92 static cl::opt<int> ThreadCount("threads", cl::init(0));
93 
94 // Simple helper to save temporary files for debug.
95 static void saveTempBitcode(const Module &TheModule, StringRef TempDir,
96                             unsigned count, StringRef Suffix) {
97   if (TempDir.empty())
98     return;
99   // User asked to save temps, let dump the bitcode file after import.
100   std::string SaveTempPath = (TempDir + llvm::Twine(count) + Suffix).str();
101   std::error_code EC;
102   raw_fd_ostream OS(SaveTempPath, EC, sys::fs::OF_None);
103   if (EC)
104     report_fatal_error(Twine("Failed to open ") + SaveTempPath +
105                        " to save optimized bitcode\n");
106   WriteBitcodeToFile(TheModule, OS, /* ShouldPreserveUseListOrder */ true);
107 }
108 
109 static const GlobalValueSummary *
110 getFirstDefinitionForLinker(const GlobalValueSummaryList &GVSummaryList) {
111   // If there is any strong definition anywhere, get it.
112   auto StrongDefForLinker = llvm::find_if(
113       GVSummaryList, [](const std::unique_ptr<GlobalValueSummary> &Summary) {
114         auto Linkage = Summary->linkage();
115         return !GlobalValue::isAvailableExternallyLinkage(Linkage) &&
116                !GlobalValue::isWeakForLinker(Linkage);
117       });
118   if (StrongDefForLinker != GVSummaryList.end())
119     return StrongDefForLinker->get();
120   // Get the first *linker visible* definition for this global in the summary
121   // list.
122   auto FirstDefForLinker = llvm::find_if(
123       GVSummaryList, [](const std::unique_ptr<GlobalValueSummary> &Summary) {
124         auto Linkage = Summary->linkage();
125         return !GlobalValue::isAvailableExternallyLinkage(Linkage);
126       });
127   // Extern templates can be emitted as available_externally.
128   if (FirstDefForLinker == GVSummaryList.end())
129     return nullptr;
130   return FirstDefForLinker->get();
131 }
132 
133 // Populate map of GUID to the prevailing copy for any multiply defined
134 // symbols. Currently assume first copy is prevailing, or any strong
135 // definition. Can be refined with Linker information in the future.
136 static void computePrevailingCopies(
137     const ModuleSummaryIndex &Index,
138     DenseMap<GlobalValue::GUID, const GlobalValueSummary *> &PrevailingCopy) {
139   auto HasMultipleCopies = [&](const GlobalValueSummaryList &GVSummaryList) {
140     return GVSummaryList.size() > 1;
141   };
142 
143   for (auto &I : Index) {
144     if (HasMultipleCopies(I.second.SummaryList))
145       PrevailingCopy[I.first] =
146           getFirstDefinitionForLinker(I.second.SummaryList);
147   }
148 }
149 
150 static StringMap<lto::InputFile *>
151 generateModuleMap(std::vector<std::unique_ptr<lto::InputFile>> &Modules) {
152   StringMap<lto::InputFile *> ModuleMap;
153   for (auto &M : Modules) {
154     assert(ModuleMap.find(M->getName()) == ModuleMap.end() &&
155            "Expect unique Buffer Identifier");
156     ModuleMap[M->getName()] = M.get();
157   }
158   return ModuleMap;
159 }
160 
161 static void promoteModule(Module &TheModule, const ModuleSummaryIndex &Index,
162                           bool ClearDSOLocalOnDeclarations) {
163   if (renameModuleForThinLTO(TheModule, Index, ClearDSOLocalOnDeclarations))
164     report_fatal_error("renameModuleForThinLTO failed");
165 }
166 
167 namespace {
168 class ThinLTODiagnosticInfo : public DiagnosticInfo {
169   const Twine &Msg;
170 public:
171   ThinLTODiagnosticInfo(const Twine &DiagMsg,
172                         DiagnosticSeverity Severity = DS_Error)
173       : DiagnosticInfo(DK_Linker, Severity), Msg(DiagMsg) {}
174   void print(DiagnosticPrinter &DP) const override { DP << Msg; }
175 };
176 }
177 
178 /// Verify the module and strip broken debug info.
179 static void verifyLoadedModule(Module &TheModule) {
180   bool BrokenDebugInfo = false;
181   if (verifyModule(TheModule, &dbgs(), &BrokenDebugInfo))
182     report_fatal_error("Broken module found, compilation aborted!");
183   if (BrokenDebugInfo) {
184     TheModule.getContext().diagnose(ThinLTODiagnosticInfo(
185         "Invalid debug info found, debug info will be stripped", DS_Warning));
186     StripDebugInfo(TheModule);
187   }
188 }
189 
190 static std::unique_ptr<Module> loadModuleFromInput(lto::InputFile *Input,
191                                                    LLVMContext &Context,
192                                                    bool Lazy,
193                                                    bool IsImporting) {
194   auto &Mod = Input->getSingleBitcodeModule();
195   SMDiagnostic Err;
196   Expected<std::unique_ptr<Module>> ModuleOrErr =
197       Lazy ? Mod.getLazyModule(Context,
198                                /* ShouldLazyLoadMetadata */ true, IsImporting)
199            : Mod.parseModule(Context);
200   if (!ModuleOrErr) {
201     handleAllErrors(ModuleOrErr.takeError(), [&](ErrorInfoBase &EIB) {
202       SMDiagnostic Err = SMDiagnostic(Mod.getModuleIdentifier(),
203                                       SourceMgr::DK_Error, EIB.message());
204       Err.print("ThinLTO", errs());
205     });
206     report_fatal_error("Can't load module, abort.");
207   }
208   if (!Lazy)
209     verifyLoadedModule(*ModuleOrErr.get());
210   return std::move(*ModuleOrErr);
211 }
212 
213 static void
214 crossImportIntoModule(Module &TheModule, const ModuleSummaryIndex &Index,
215                       StringMap<lto::InputFile *> &ModuleMap,
216                       const FunctionImporter::ImportMapTy &ImportList,
217                       bool ClearDSOLocalOnDeclarations) {
218   auto Loader = [&](StringRef Identifier) {
219     auto &Input = ModuleMap[Identifier];
220     return loadModuleFromInput(Input, TheModule.getContext(),
221                                /*Lazy=*/true, /*IsImporting*/ true);
222   };
223 
224   FunctionImporter Importer(Index, Loader, ClearDSOLocalOnDeclarations);
225   Expected<bool> Result = Importer.importFunctions(TheModule, ImportList);
226   if (!Result) {
227     handleAllErrors(Result.takeError(), [&](ErrorInfoBase &EIB) {
228       SMDiagnostic Err = SMDiagnostic(TheModule.getModuleIdentifier(),
229                                       SourceMgr::DK_Error, EIB.message());
230       Err.print("ThinLTO", errs());
231     });
232     report_fatal_error("importFunctions failed");
233   }
234   // Verify again after cross-importing.
235   verifyLoadedModule(TheModule);
236 }
237 
238 static void optimizeModule(Module &TheModule, TargetMachine &TM,
239                            unsigned OptLevel, bool Freestanding,
240                            bool DebugPassManager, ModuleSummaryIndex *Index) {
241   Optional<PGOOptions> PGOOpt;
242   LoopAnalysisManager LAM;
243   FunctionAnalysisManager FAM;
244   CGSCCAnalysisManager CGAM;
245   ModuleAnalysisManager MAM;
246 
247   PassInstrumentationCallbacks PIC;
248   StandardInstrumentations SI(DebugPassManager);
249   SI.registerCallbacks(PIC, &FAM);
250   PipelineTuningOptions PTO;
251   PTO.LoopVectorization = true;
252   PTO.SLPVectorization = true;
253   PassBuilder PB(&TM, PTO, PGOOpt, &PIC);
254 
255   std::unique_ptr<TargetLibraryInfoImpl> TLII(
256       new TargetLibraryInfoImpl(Triple(TM.getTargetTriple())));
257   if (Freestanding)
258     TLII->disableAllFunctions();
259   FAM.registerPass([&] { return TargetLibraryAnalysis(*TLII); });
260 
261   // Register all the basic analyses with the managers.
262   PB.registerModuleAnalyses(MAM);
263   PB.registerCGSCCAnalyses(CGAM);
264   PB.registerFunctionAnalyses(FAM);
265   PB.registerLoopAnalyses(LAM);
266   PB.crossRegisterProxies(LAM, FAM, CGAM, MAM);
267 
268   ModulePassManager MPM;
269 
270   OptimizationLevel OL;
271 
272   switch (OptLevel) {
273   default:
274     llvm_unreachable("Invalid optimization level");
275   case 0:
276     OL = OptimizationLevel::O0;
277     break;
278   case 1:
279     OL = OptimizationLevel::O1;
280     break;
281   case 2:
282     OL = OptimizationLevel::O2;
283     break;
284   case 3:
285     OL = OptimizationLevel::O3;
286     break;
287   }
288 
289   MPM.addPass(PB.buildThinLTODefaultPipeline(OL, Index));
290 
291   MPM.run(TheModule, MAM);
292 }
293 
294 static void
295 addUsedSymbolToPreservedGUID(const lto::InputFile &File,
296                              DenseSet<GlobalValue::GUID> &PreservedGUID) {
297   for (const auto &Sym : File.symbols()) {
298     if (Sym.isUsed())
299       PreservedGUID.insert(GlobalValue::getGUID(Sym.getIRName()));
300   }
301 }
302 
303 // Convert the PreservedSymbols map from "Name" based to "GUID" based.
304 static void computeGUIDPreservedSymbols(const lto::InputFile &File,
305                                         const StringSet<> &PreservedSymbols,
306                                         const Triple &TheTriple,
307                                         DenseSet<GlobalValue::GUID> &GUIDs) {
308   // Iterate the symbols in the input file and if the input has preserved symbol
309   // compute the GUID for the symbol.
310   for (const auto &Sym : File.symbols()) {
311     if (PreservedSymbols.count(Sym.getName()) && !Sym.getIRName().empty())
312       GUIDs.insert(GlobalValue::getGUID(GlobalValue::getGlobalIdentifier(
313           Sym.getIRName(), GlobalValue::ExternalLinkage, "")));
314   }
315 }
316 
317 static DenseSet<GlobalValue::GUID>
318 computeGUIDPreservedSymbols(const lto::InputFile &File,
319                             const StringSet<> &PreservedSymbols,
320                             const Triple &TheTriple) {
321   DenseSet<GlobalValue::GUID> GUIDPreservedSymbols(PreservedSymbols.size());
322   computeGUIDPreservedSymbols(File, PreservedSymbols, TheTriple,
323                               GUIDPreservedSymbols);
324   return GUIDPreservedSymbols;
325 }
326 
327 std::unique_ptr<MemoryBuffer> codegenModule(Module &TheModule,
328                                             TargetMachine &TM) {
329   SmallVector<char, 128> OutputBuffer;
330 
331   // CodeGen
332   {
333     raw_svector_ostream OS(OutputBuffer);
334     legacy::PassManager PM;
335 
336     // If the bitcode files contain ARC code and were compiled with optimization,
337     // the ObjCARCContractPass must be run, so do it unconditionally here.
338     PM.add(createObjCARCContractPass());
339 
340     // Setup the codegen now.
341     if (TM.addPassesToEmitFile(PM, OS, nullptr, CGFT_ObjectFile,
342                                /* DisableVerify */ true))
343       report_fatal_error("Failed to setup codegen");
344 
345     // Run codegen now. resulting binary is in OutputBuffer.
346     PM.run(TheModule);
347   }
348   return std::make_unique<SmallVectorMemoryBuffer>(
349       std::move(OutputBuffer), /*RequiresNullTerminator=*/false);
350 }
351 
352 /// Manage caching for a single Module.
353 class ModuleCacheEntry {
354   SmallString<128> EntryPath;
355 
356 public:
357   // Create a cache entry. This compute a unique hash for the Module considering
358   // the current list of export/import, and offer an interface to query to
359   // access the content in the cache.
360   ModuleCacheEntry(
361       StringRef CachePath, const ModuleSummaryIndex &Index, StringRef ModuleID,
362       const FunctionImporter::ImportMapTy &ImportList,
363       const FunctionImporter::ExportSetTy &ExportList,
364       const std::map<GlobalValue::GUID, GlobalValue::LinkageTypes> &ResolvedODR,
365       const GVSummaryMapTy &DefinedGVSummaries, unsigned OptLevel,
366       bool Freestanding, const TargetMachineBuilder &TMBuilder) {
367     if (CachePath.empty())
368       return;
369 
370     if (!Index.modulePaths().count(ModuleID))
371       // The module does not have an entry, it can't have a hash at all
372       return;
373 
374     if (all_of(Index.getModuleHash(ModuleID),
375                [](uint32_t V) { return V == 0; }))
376       // No hash entry, no caching!
377       return;
378 
379     llvm::lto::Config Conf;
380     Conf.OptLevel = OptLevel;
381     Conf.Options = TMBuilder.Options;
382     Conf.CPU = TMBuilder.MCpu;
383     Conf.MAttrs.push_back(TMBuilder.MAttr);
384     Conf.RelocModel = TMBuilder.RelocModel;
385     Conf.CGOptLevel = TMBuilder.CGOptLevel;
386     Conf.Freestanding = Freestanding;
387     SmallString<40> Key;
388     computeLTOCacheKey(Key, Conf, Index, ModuleID, ImportList, ExportList,
389                        ResolvedODR, DefinedGVSummaries);
390 
391     // This choice of file name allows the cache to be pruned (see pruneCache()
392     // in include/llvm/Support/CachePruning.h).
393     sys::path::append(EntryPath, CachePath, "llvmcache-" + Key);
394   }
395 
396   // Access the path to this entry in the cache.
397   StringRef getEntryPath() { return EntryPath; }
398 
399   // Try loading the buffer for this cache entry.
400   ErrorOr<std::unique_ptr<MemoryBuffer>> tryLoadingBuffer() {
401     if (EntryPath.empty())
402       return std::error_code();
403     SmallString<64> ResultPath;
404     Expected<sys::fs::file_t> FDOrErr = sys::fs::openNativeFileForRead(
405         Twine(EntryPath), sys::fs::OF_UpdateAtime, &ResultPath);
406     if (!FDOrErr)
407       return errorToErrorCode(FDOrErr.takeError());
408     ErrorOr<std::unique_ptr<MemoryBuffer>> MBOrErr = MemoryBuffer::getOpenFile(
409         *FDOrErr, EntryPath, /*FileSize=*/-1, /*RequiresNullTerminator=*/false);
410     sys::fs::closeFile(*FDOrErr);
411     return MBOrErr;
412   }
413 
414   // Cache the Produced object file
415   void write(const MemoryBuffer &OutputBuffer) {
416     if (EntryPath.empty())
417       return;
418 
419     // Write to a temporary to avoid race condition
420     SmallString<128> TempFilename;
421     SmallString<128> CachePath(EntryPath);
422     llvm::sys::path::remove_filename(CachePath);
423     sys::path::append(TempFilename, CachePath, "Thin-%%%%%%.tmp.o");
424 
425     if (auto Err = handleErrors(
426             llvm::writeFileAtomically(TempFilename, EntryPath,
427                                       OutputBuffer.getBuffer()),
428             [](const llvm::AtomicFileWriteError &E) {
429               std::string ErrorMsgBuffer;
430               llvm::raw_string_ostream S(ErrorMsgBuffer);
431               E.log(S);
432 
433               if (E.Error ==
434                   llvm::atomic_write_error::failed_to_create_uniq_file) {
435                 errs() << "Error: " << ErrorMsgBuffer << "\n";
436                 report_fatal_error("ThinLTO: Can't get a temporary file");
437               }
438             })) {
439       // FIXME
440       consumeError(std::move(Err));
441     }
442   }
443 };
444 
445 static std::unique_ptr<MemoryBuffer>
446 ProcessThinLTOModule(Module &TheModule, ModuleSummaryIndex &Index,
447                      StringMap<lto::InputFile *> &ModuleMap, TargetMachine &TM,
448                      const FunctionImporter::ImportMapTy &ImportList,
449                      const FunctionImporter::ExportSetTy &ExportList,
450                      const DenseSet<GlobalValue::GUID> &GUIDPreservedSymbols,
451                      const GVSummaryMapTy &DefinedGlobals,
452                      const ThinLTOCodeGenerator::CachingOptions &CacheOptions,
453                      bool DisableCodeGen, StringRef SaveTempsDir,
454                      bool Freestanding, unsigned OptLevel, unsigned count,
455                      bool DebugPassManager) {
456 
457   // "Benchmark"-like optimization: single-source case
458   bool SingleModule = (ModuleMap.size() == 1);
459 
460   // When linking an ELF shared object, dso_local should be dropped. We
461   // conservatively do this for -fpic.
462   bool ClearDSOLocalOnDeclarations =
463       TM.getTargetTriple().isOSBinFormatELF() &&
464       TM.getRelocationModel() != Reloc::Static &&
465       TheModule.getPIELevel() == PIELevel::Default;
466 
467   if (!SingleModule) {
468     promoteModule(TheModule, Index, ClearDSOLocalOnDeclarations);
469 
470     // Apply summary-based prevailing-symbol resolution decisions.
471     thinLTOFinalizeInModule(TheModule, DefinedGlobals, /*PropagateAttrs=*/true);
472 
473     // Save temps: after promotion.
474     saveTempBitcode(TheModule, SaveTempsDir, count, ".1.promoted.bc");
475   }
476 
477   // Be friendly and don't nuke totally the module when the client didn't
478   // supply anything to preserve.
479   if (!ExportList.empty() || !GUIDPreservedSymbols.empty()) {
480     // Apply summary-based internalization decisions.
481     thinLTOInternalizeModule(TheModule, DefinedGlobals);
482   }
483 
484   // Save internalized bitcode
485   saveTempBitcode(TheModule, SaveTempsDir, count, ".2.internalized.bc");
486 
487   if (!SingleModule) {
488     crossImportIntoModule(TheModule, Index, ModuleMap, ImportList,
489                           ClearDSOLocalOnDeclarations);
490 
491     // Save temps: after cross-module import.
492     saveTempBitcode(TheModule, SaveTempsDir, count, ".3.imported.bc");
493   }
494 
495   optimizeModule(TheModule, TM, OptLevel, Freestanding, DebugPassManager,
496                  &Index);
497 
498   saveTempBitcode(TheModule, SaveTempsDir, count, ".4.opt.bc");
499 
500   if (DisableCodeGen) {
501     // Configured to stop before CodeGen, serialize the bitcode and return.
502     SmallVector<char, 128> OutputBuffer;
503     {
504       raw_svector_ostream OS(OutputBuffer);
505       ProfileSummaryInfo PSI(TheModule);
506       auto Index = buildModuleSummaryIndex(TheModule, nullptr, &PSI);
507       WriteBitcodeToFile(TheModule, OS, true, &Index);
508     }
509     return std::make_unique<SmallVectorMemoryBuffer>(
510         std::move(OutputBuffer), /*RequiresNullTerminator=*/false);
511   }
512 
513   return codegenModule(TheModule, TM);
514 }
515 
516 /// Resolve prevailing symbols. Record resolutions in the \p ResolvedODR map
517 /// for caching, and in the \p Index for application during the ThinLTO
518 /// backends. This is needed for correctness for exported symbols (ensure
519 /// at least one copy kept) and a compile-time optimization (to drop duplicate
520 /// copies when possible).
521 static void resolvePrevailingInIndex(
522     ModuleSummaryIndex &Index,
523     StringMap<std::map<GlobalValue::GUID, GlobalValue::LinkageTypes>>
524         &ResolvedODR,
525     const DenseSet<GlobalValue::GUID> &GUIDPreservedSymbols,
526     const DenseMap<GlobalValue::GUID, const GlobalValueSummary *>
527         &PrevailingCopy) {
528 
529   auto isPrevailing = [&](GlobalValue::GUID GUID, const GlobalValueSummary *S) {
530     const auto &Prevailing = PrevailingCopy.find(GUID);
531     // Not in map means that there was only one copy, which must be prevailing.
532     if (Prevailing == PrevailingCopy.end())
533       return true;
534     return Prevailing->second == S;
535   };
536 
537   auto recordNewLinkage = [&](StringRef ModuleIdentifier,
538                               GlobalValue::GUID GUID,
539                               GlobalValue::LinkageTypes NewLinkage) {
540     ResolvedODR[ModuleIdentifier][GUID] = NewLinkage;
541   };
542 
543   // TODO Conf.VisibilityScheme can be lto::Config::ELF for ELF.
544   lto::Config Conf;
545   thinLTOResolvePrevailingInIndex(Conf, Index, isPrevailing, recordNewLinkage,
546                                   GUIDPreservedSymbols);
547 }
548 
549 // Initialize the TargetMachine builder for a given Triple
550 static void initTMBuilder(TargetMachineBuilder &TMBuilder,
551                           const Triple &TheTriple) {
552   // Set a default CPU for Darwin triples (copied from LTOCodeGenerator).
553   // FIXME this looks pretty terrible...
554   if (TMBuilder.MCpu.empty() && TheTriple.isOSDarwin()) {
555     if (TheTriple.getArch() == llvm::Triple::x86_64)
556       TMBuilder.MCpu = "core2";
557     else if (TheTriple.getArch() == llvm::Triple::x86)
558       TMBuilder.MCpu = "yonah";
559     else if (TheTriple.getArch() == llvm::Triple::aarch64 ||
560              TheTriple.getArch() == llvm::Triple::aarch64_32)
561       TMBuilder.MCpu = "cyclone";
562   }
563   TMBuilder.TheTriple = std::move(TheTriple);
564 }
565 
566 } // end anonymous namespace
567 
568 void ThinLTOCodeGenerator::addModule(StringRef Identifier, StringRef Data) {
569   MemoryBufferRef Buffer(Data, Identifier);
570 
571   auto InputOrError = lto::InputFile::create(Buffer);
572   if (!InputOrError)
573     report_fatal_error(Twine("ThinLTO cannot create input file: ") +
574                        toString(InputOrError.takeError()));
575 
576   auto TripleStr = (*InputOrError)->getTargetTriple();
577   Triple TheTriple(TripleStr);
578 
579   if (Modules.empty())
580     initTMBuilder(TMBuilder, Triple(TheTriple));
581   else if (TMBuilder.TheTriple != TheTriple) {
582     if (!TMBuilder.TheTriple.isCompatibleWith(TheTriple))
583       report_fatal_error("ThinLTO modules with incompatible triples not "
584                          "supported");
585     initTMBuilder(TMBuilder, Triple(TMBuilder.TheTriple.merge(TheTriple)));
586   }
587 
588   Modules.emplace_back(std::move(*InputOrError));
589 }
590 
591 void ThinLTOCodeGenerator::preserveSymbol(StringRef Name) {
592   PreservedSymbols.insert(Name);
593 }
594 
595 void ThinLTOCodeGenerator::crossReferenceSymbol(StringRef Name) {
596   // FIXME: At the moment, we don't take advantage of this extra information,
597   // we're conservatively considering cross-references as preserved.
598   //  CrossReferencedSymbols.insert(Name);
599   PreservedSymbols.insert(Name);
600 }
601 
602 // TargetMachine factory
603 std::unique_ptr<TargetMachine> TargetMachineBuilder::create() const {
604   std::string ErrMsg;
605   const Target *TheTarget =
606       TargetRegistry::lookupTarget(TheTriple.str(), ErrMsg);
607   if (!TheTarget) {
608     report_fatal_error(Twine("Can't load target for this Triple: ") + ErrMsg);
609   }
610 
611   // Use MAttr as the default set of features.
612   SubtargetFeatures Features(MAttr);
613   Features.getDefaultSubtargetFeatures(TheTriple);
614   std::string FeatureStr = Features.getString();
615 
616   std::unique_ptr<TargetMachine> TM(
617       TheTarget->createTargetMachine(TheTriple.str(), MCpu, FeatureStr, Options,
618                                      RelocModel, None, CGOptLevel));
619   assert(TM && "Cannot create target machine");
620 
621   return TM;
622 }
623 
624 /**
625  * Produce the combined summary index from all the bitcode files:
626  * "thin-link".
627  */
628 std::unique_ptr<ModuleSummaryIndex> ThinLTOCodeGenerator::linkCombinedIndex() {
629   std::unique_ptr<ModuleSummaryIndex> CombinedIndex =
630       std::make_unique<ModuleSummaryIndex>(/*HaveGVs=*/false);
631   uint64_t NextModuleId = 0;
632   for (auto &Mod : Modules) {
633     auto &M = Mod->getSingleBitcodeModule();
634     if (Error Err =
635             M.readSummary(*CombinedIndex, Mod->getName(), NextModuleId++)) {
636       // FIXME diagnose
637       logAllUnhandledErrors(
638           std::move(Err), errs(),
639           "error: can't create module summary index for buffer: ");
640       return nullptr;
641     }
642   }
643   return CombinedIndex;
644 }
645 
646 namespace {
647 struct IsExported {
648   const StringMap<FunctionImporter::ExportSetTy> &ExportLists;
649   const DenseSet<GlobalValue::GUID> &GUIDPreservedSymbols;
650 
651   IsExported(const StringMap<FunctionImporter::ExportSetTy> &ExportLists,
652              const DenseSet<GlobalValue::GUID> &GUIDPreservedSymbols)
653       : ExportLists(ExportLists), GUIDPreservedSymbols(GUIDPreservedSymbols) {}
654 
655   bool operator()(StringRef ModuleIdentifier, ValueInfo VI) const {
656     const auto &ExportList = ExportLists.find(ModuleIdentifier);
657     return (ExportList != ExportLists.end() && ExportList->second.count(VI)) ||
658            GUIDPreservedSymbols.count(VI.getGUID());
659   }
660 };
661 
662 struct IsPrevailing {
663   const DenseMap<GlobalValue::GUID, const GlobalValueSummary *> &PrevailingCopy;
664   IsPrevailing(const DenseMap<GlobalValue::GUID, const GlobalValueSummary *>
665                    &PrevailingCopy)
666       : PrevailingCopy(PrevailingCopy) {}
667 
668   bool operator()(GlobalValue::GUID GUID, const GlobalValueSummary *S) const {
669     const auto &Prevailing = PrevailingCopy.find(GUID);
670     // Not in map means that there was only one copy, which must be prevailing.
671     if (Prevailing == PrevailingCopy.end())
672       return true;
673     return Prevailing->second == S;
674   };
675 };
676 } // namespace
677 
678 static void computeDeadSymbolsInIndex(
679     ModuleSummaryIndex &Index,
680     const DenseSet<GlobalValue::GUID> &GUIDPreservedSymbols) {
681   // We have no symbols resolution available. And can't do any better now in the
682   // case where the prevailing symbol is in a native object. It can be refined
683   // with linker information in the future.
684   auto isPrevailing = [&](GlobalValue::GUID G) {
685     return PrevailingType::Unknown;
686   };
687   computeDeadSymbolsWithConstProp(Index, GUIDPreservedSymbols, isPrevailing,
688                                   /* ImportEnabled = */ true);
689 }
690 
691 /**
692  * Perform promotion and renaming of exported internal functions.
693  * Index is updated to reflect linkage changes from weak resolution.
694  */
695 void ThinLTOCodeGenerator::promote(Module &TheModule, ModuleSummaryIndex &Index,
696                                    const lto::InputFile &File) {
697   auto ModuleCount = Index.modulePaths().size();
698   auto ModuleIdentifier = TheModule.getModuleIdentifier();
699 
700   // Collect for each module the list of function it defines (GUID -> Summary).
701   StringMap<GVSummaryMapTy> ModuleToDefinedGVSummaries;
702   Index.collectDefinedGVSummariesPerModule(ModuleToDefinedGVSummaries);
703 
704   // Convert the preserved symbols set from string to GUID
705   auto GUIDPreservedSymbols = computeGUIDPreservedSymbols(
706       File, PreservedSymbols, Triple(TheModule.getTargetTriple()));
707 
708   // Add used symbol to the preserved symbols.
709   addUsedSymbolToPreservedGUID(File, GUIDPreservedSymbols);
710 
711   // Compute "dead" symbols, we don't want to import/export these!
712   computeDeadSymbolsInIndex(Index, GUIDPreservedSymbols);
713 
714   // Generate import/export list
715   StringMap<FunctionImporter::ImportMapTy> ImportLists(ModuleCount);
716   StringMap<FunctionImporter::ExportSetTy> ExportLists(ModuleCount);
717   ComputeCrossModuleImport(Index, ModuleToDefinedGVSummaries, ImportLists,
718                            ExportLists);
719 
720   DenseMap<GlobalValue::GUID, const GlobalValueSummary *> PrevailingCopy;
721   computePrevailingCopies(Index, PrevailingCopy);
722 
723   // Resolve prevailing symbols
724   StringMap<std::map<GlobalValue::GUID, GlobalValue::LinkageTypes>> ResolvedODR;
725   resolvePrevailingInIndex(Index, ResolvedODR, GUIDPreservedSymbols,
726                            PrevailingCopy);
727 
728   thinLTOFinalizeInModule(TheModule,
729                           ModuleToDefinedGVSummaries[ModuleIdentifier],
730                           /*PropagateAttrs=*/false);
731 
732   // Promote the exported values in the index, so that they are promoted
733   // in the module.
734   thinLTOInternalizeAndPromoteInIndex(
735       Index, IsExported(ExportLists, GUIDPreservedSymbols),
736       IsPrevailing(PrevailingCopy));
737 
738   // FIXME Set ClearDSOLocalOnDeclarations.
739   promoteModule(TheModule, Index, /*ClearDSOLocalOnDeclarations=*/false);
740 }
741 
742 /**
743  * Perform cross-module importing for the module identified by ModuleIdentifier.
744  */
745 void ThinLTOCodeGenerator::crossModuleImport(Module &TheModule,
746                                              ModuleSummaryIndex &Index,
747                                              const lto::InputFile &File) {
748   auto ModuleMap = generateModuleMap(Modules);
749   auto ModuleCount = Index.modulePaths().size();
750 
751   // Collect for each module the list of function it defines (GUID -> Summary).
752   StringMap<GVSummaryMapTy> ModuleToDefinedGVSummaries(ModuleCount);
753   Index.collectDefinedGVSummariesPerModule(ModuleToDefinedGVSummaries);
754 
755   // Convert the preserved symbols set from string to GUID
756   auto GUIDPreservedSymbols = computeGUIDPreservedSymbols(
757       File, PreservedSymbols, Triple(TheModule.getTargetTriple()));
758 
759   addUsedSymbolToPreservedGUID(File, GUIDPreservedSymbols);
760 
761   // Compute "dead" symbols, we don't want to import/export these!
762   computeDeadSymbolsInIndex(Index, GUIDPreservedSymbols);
763 
764   // Generate import/export list
765   StringMap<FunctionImporter::ImportMapTy> ImportLists(ModuleCount);
766   StringMap<FunctionImporter::ExportSetTy> ExportLists(ModuleCount);
767   ComputeCrossModuleImport(Index, ModuleToDefinedGVSummaries, ImportLists,
768                            ExportLists);
769   auto &ImportList = ImportLists[TheModule.getModuleIdentifier()];
770 
771   // FIXME Set ClearDSOLocalOnDeclarations.
772   crossImportIntoModule(TheModule, Index, ModuleMap, ImportList,
773                         /*ClearDSOLocalOnDeclarations=*/false);
774 }
775 
776 /**
777  * Compute the list of summaries needed for importing into module.
778  */
779 void ThinLTOCodeGenerator::gatherImportedSummariesForModule(
780     Module &TheModule, ModuleSummaryIndex &Index,
781     std::map<std::string, GVSummaryMapTy> &ModuleToSummariesForIndex,
782     const lto::InputFile &File) {
783   auto ModuleCount = Index.modulePaths().size();
784   auto ModuleIdentifier = TheModule.getModuleIdentifier();
785 
786   // Collect for each module the list of function it defines (GUID -> Summary).
787   StringMap<GVSummaryMapTy> ModuleToDefinedGVSummaries(ModuleCount);
788   Index.collectDefinedGVSummariesPerModule(ModuleToDefinedGVSummaries);
789 
790   // Convert the preserved symbols set from string to GUID
791   auto GUIDPreservedSymbols = computeGUIDPreservedSymbols(
792       File, PreservedSymbols, Triple(TheModule.getTargetTriple()));
793 
794   addUsedSymbolToPreservedGUID(File, GUIDPreservedSymbols);
795 
796   // Compute "dead" symbols, we don't want to import/export these!
797   computeDeadSymbolsInIndex(Index, GUIDPreservedSymbols);
798 
799   // Generate import/export list
800   StringMap<FunctionImporter::ImportMapTy> ImportLists(ModuleCount);
801   StringMap<FunctionImporter::ExportSetTy> ExportLists(ModuleCount);
802   ComputeCrossModuleImport(Index, ModuleToDefinedGVSummaries, ImportLists,
803                            ExportLists);
804 
805   llvm::gatherImportedSummariesForModule(
806       ModuleIdentifier, ModuleToDefinedGVSummaries,
807       ImportLists[ModuleIdentifier], ModuleToSummariesForIndex);
808 }
809 
810 /**
811  * Emit the list of files needed for importing into module.
812  */
813 void ThinLTOCodeGenerator::emitImports(Module &TheModule, StringRef OutputName,
814                                        ModuleSummaryIndex &Index,
815                                        const lto::InputFile &File) {
816   auto ModuleCount = Index.modulePaths().size();
817   auto ModuleIdentifier = TheModule.getModuleIdentifier();
818 
819   // Collect for each module the list of function it defines (GUID -> Summary).
820   StringMap<GVSummaryMapTy> ModuleToDefinedGVSummaries(ModuleCount);
821   Index.collectDefinedGVSummariesPerModule(ModuleToDefinedGVSummaries);
822 
823   // Convert the preserved symbols set from string to GUID
824   auto GUIDPreservedSymbols = computeGUIDPreservedSymbols(
825       File, PreservedSymbols, Triple(TheModule.getTargetTriple()));
826 
827   addUsedSymbolToPreservedGUID(File, GUIDPreservedSymbols);
828 
829   // Compute "dead" symbols, we don't want to import/export these!
830   computeDeadSymbolsInIndex(Index, GUIDPreservedSymbols);
831 
832   // Generate import/export list
833   StringMap<FunctionImporter::ImportMapTy> ImportLists(ModuleCount);
834   StringMap<FunctionImporter::ExportSetTy> ExportLists(ModuleCount);
835   ComputeCrossModuleImport(Index, ModuleToDefinedGVSummaries, ImportLists,
836                            ExportLists);
837 
838   std::map<std::string, GVSummaryMapTy> ModuleToSummariesForIndex;
839   llvm::gatherImportedSummariesForModule(
840       ModuleIdentifier, ModuleToDefinedGVSummaries,
841       ImportLists[ModuleIdentifier], ModuleToSummariesForIndex);
842 
843   std::error_code EC;
844   if ((EC = EmitImportsFiles(ModuleIdentifier, OutputName,
845                              ModuleToSummariesForIndex)))
846     report_fatal_error(Twine("Failed to open ") + OutputName +
847                        " to save imports lists\n");
848 }
849 
850 /**
851  * Perform internalization. Runs promote and internalization together.
852  * Index is updated to reflect linkage changes.
853  */
854 void ThinLTOCodeGenerator::internalize(Module &TheModule,
855                                        ModuleSummaryIndex &Index,
856                                        const lto::InputFile &File) {
857   initTMBuilder(TMBuilder, Triple(TheModule.getTargetTriple()));
858   auto ModuleCount = Index.modulePaths().size();
859   auto ModuleIdentifier = TheModule.getModuleIdentifier();
860 
861   // Convert the preserved symbols set from string to GUID
862   auto GUIDPreservedSymbols =
863       computeGUIDPreservedSymbols(File, PreservedSymbols, TMBuilder.TheTriple);
864 
865   addUsedSymbolToPreservedGUID(File, GUIDPreservedSymbols);
866 
867   // Collect for each module the list of function it defines (GUID -> Summary).
868   StringMap<GVSummaryMapTy> ModuleToDefinedGVSummaries(ModuleCount);
869   Index.collectDefinedGVSummariesPerModule(ModuleToDefinedGVSummaries);
870 
871   // Compute "dead" symbols, we don't want to import/export these!
872   computeDeadSymbolsInIndex(Index, GUIDPreservedSymbols);
873 
874   // Generate import/export list
875   StringMap<FunctionImporter::ImportMapTy> ImportLists(ModuleCount);
876   StringMap<FunctionImporter::ExportSetTy> ExportLists(ModuleCount);
877   ComputeCrossModuleImport(Index, ModuleToDefinedGVSummaries, ImportLists,
878                            ExportLists);
879   auto &ExportList = ExportLists[ModuleIdentifier];
880 
881   // Be friendly and don't nuke totally the module when the client didn't
882   // supply anything to preserve.
883   if (ExportList.empty() && GUIDPreservedSymbols.empty())
884     return;
885 
886   DenseMap<GlobalValue::GUID, const GlobalValueSummary *> PrevailingCopy;
887   computePrevailingCopies(Index, PrevailingCopy);
888 
889   // Resolve prevailing symbols
890   StringMap<std::map<GlobalValue::GUID, GlobalValue::LinkageTypes>> ResolvedODR;
891   resolvePrevailingInIndex(Index, ResolvedODR, GUIDPreservedSymbols,
892                            PrevailingCopy);
893 
894   // Promote the exported values in the index, so that they are promoted
895   // in the module.
896   thinLTOInternalizeAndPromoteInIndex(
897       Index, IsExported(ExportLists, GUIDPreservedSymbols),
898       IsPrevailing(PrevailingCopy));
899 
900   // FIXME Set ClearDSOLocalOnDeclarations.
901   promoteModule(TheModule, Index, /*ClearDSOLocalOnDeclarations=*/false);
902 
903   // Internalization
904   thinLTOFinalizeInModule(TheModule,
905                           ModuleToDefinedGVSummaries[ModuleIdentifier],
906                           /*PropagateAttrs=*/false);
907 
908   thinLTOInternalizeModule(TheModule,
909                            ModuleToDefinedGVSummaries[ModuleIdentifier]);
910 }
911 
912 /**
913  * Perform post-importing ThinLTO optimizations.
914  */
915 void ThinLTOCodeGenerator::optimize(Module &TheModule) {
916   initTMBuilder(TMBuilder, Triple(TheModule.getTargetTriple()));
917 
918   // Optimize now
919   optimizeModule(TheModule, *TMBuilder.create(), OptLevel, Freestanding,
920                  DebugPassManager, nullptr);
921 }
922 
923 /// Write out the generated object file, either from CacheEntryPath or from
924 /// OutputBuffer, preferring hard-link when possible.
925 /// Returns the path to the generated file in SavedObjectsDirectoryPath.
926 std::string
927 ThinLTOCodeGenerator::writeGeneratedObject(int count, StringRef CacheEntryPath,
928                                            const MemoryBuffer &OutputBuffer) {
929   auto ArchName = TMBuilder.TheTriple.getArchName();
930   SmallString<128> OutputPath(SavedObjectsDirectoryPath);
931   llvm::sys::path::append(OutputPath,
932                           Twine(count) + "." + ArchName + ".thinlto.o");
933   OutputPath.c_str(); // Ensure the string is null terminated.
934   if (sys::fs::exists(OutputPath))
935     sys::fs::remove(OutputPath);
936 
937   // We don't return a memory buffer to the linker, just a list of files.
938   if (!CacheEntryPath.empty()) {
939     // Cache is enabled, hard-link the entry (or copy if hard-link fails).
940     auto Err = sys::fs::create_hard_link(CacheEntryPath, OutputPath);
941     if (!Err)
942       return std::string(OutputPath.str());
943     // Hard linking failed, try to copy.
944     Err = sys::fs::copy_file(CacheEntryPath, OutputPath);
945     if (!Err)
946       return std::string(OutputPath.str());
947     // Copy failed (could be because the CacheEntry was removed from the cache
948     // in the meantime by another process), fall back and try to write down the
949     // buffer to the output.
950     errs() << "remark: can't link or copy from cached entry '" << CacheEntryPath
951            << "' to '" << OutputPath << "'\n";
952   }
953   // No cache entry, just write out the buffer.
954   std::error_code Err;
955   raw_fd_ostream OS(OutputPath, Err, sys::fs::OF_None);
956   if (Err)
957     report_fatal_error(Twine("Can't open output '") + OutputPath + "'\n");
958   OS << OutputBuffer.getBuffer();
959   return std::string(OutputPath.str());
960 }
961 
962 // Main entry point for the ThinLTO processing
963 void ThinLTOCodeGenerator::run() {
964   timeTraceProfilerBegin("ThinLink", StringRef(""));
965   auto TimeTraceScopeExit = llvm::make_scope_exit([]() {
966     if (llvm::timeTraceProfilerEnabled())
967       llvm::timeTraceProfilerEnd();
968   });
969   // Prepare the resulting object vector
970   assert(ProducedBinaries.empty() && "The generator should not be reused");
971   if (SavedObjectsDirectoryPath.empty())
972     ProducedBinaries.resize(Modules.size());
973   else {
974     sys::fs::create_directories(SavedObjectsDirectoryPath);
975     bool IsDir;
976     sys::fs::is_directory(SavedObjectsDirectoryPath, IsDir);
977     if (!IsDir)
978       report_fatal_error(Twine("Unexistent dir: '") + SavedObjectsDirectoryPath + "'");
979     ProducedBinaryFiles.resize(Modules.size());
980   }
981 
982   if (CodeGenOnly) {
983     // Perform only parallel codegen and return.
984     ThreadPool Pool;
985     int count = 0;
986     for (auto &Mod : Modules) {
987       Pool.async([&](int count) {
988         LLVMContext Context;
989         Context.setDiscardValueNames(LTODiscardValueNames);
990 
991         // Parse module now
992         auto TheModule = loadModuleFromInput(Mod.get(), Context, false,
993                                              /*IsImporting*/ false);
994 
995         // CodeGen
996         auto OutputBuffer = codegenModule(*TheModule, *TMBuilder.create());
997         if (SavedObjectsDirectoryPath.empty())
998           ProducedBinaries[count] = std::move(OutputBuffer);
999         else
1000           ProducedBinaryFiles[count] =
1001               writeGeneratedObject(count, "", *OutputBuffer);
1002       }, count++);
1003     }
1004 
1005     return;
1006   }
1007 
1008   // Sequential linking phase
1009   auto Index = linkCombinedIndex();
1010 
1011   // Save temps: index.
1012   if (!SaveTempsDir.empty()) {
1013     auto SaveTempPath = SaveTempsDir + "index.bc";
1014     std::error_code EC;
1015     raw_fd_ostream OS(SaveTempPath, EC, sys::fs::OF_None);
1016     if (EC)
1017       report_fatal_error(Twine("Failed to open ") + SaveTempPath +
1018                          " to save optimized bitcode\n");
1019     writeIndexToFile(*Index, OS);
1020   }
1021 
1022 
1023   // Prepare the module map.
1024   auto ModuleMap = generateModuleMap(Modules);
1025   auto ModuleCount = Modules.size();
1026 
1027   // Collect for each module the list of function it defines (GUID -> Summary).
1028   StringMap<GVSummaryMapTy> ModuleToDefinedGVSummaries(ModuleCount);
1029   Index->collectDefinedGVSummariesPerModule(ModuleToDefinedGVSummaries);
1030 
1031   // Convert the preserved symbols set from string to GUID, this is needed for
1032   // computing the caching hash and the internalization.
1033   DenseSet<GlobalValue::GUID> GUIDPreservedSymbols;
1034   for (const auto &M : Modules)
1035     computeGUIDPreservedSymbols(*M, PreservedSymbols, TMBuilder.TheTriple,
1036                                 GUIDPreservedSymbols);
1037 
1038   // Add used symbol from inputs to the preserved symbols.
1039   for (const auto &M : Modules)
1040     addUsedSymbolToPreservedGUID(*M, GUIDPreservedSymbols);
1041 
1042   // Compute "dead" symbols, we don't want to import/export these!
1043   computeDeadSymbolsInIndex(*Index, GUIDPreservedSymbols);
1044 
1045   // Synthesize entry counts for functions in the combined index.
1046   computeSyntheticCounts(*Index);
1047 
1048   // Currently there is no support for enabling whole program visibility via a
1049   // linker option in the old LTO API, but this call allows it to be specified
1050   // via the internal option. Must be done before WPD below.
1051   updateVCallVisibilityInIndex(*Index,
1052                                /* WholeProgramVisibilityEnabledInLTO */ false,
1053                                // FIXME: This needs linker information via a
1054                                // TBD new interface.
1055                                /* DynamicExportSymbols */ {});
1056 
1057   // Perform index-based WPD. This will return immediately if there are
1058   // no index entries in the typeIdMetadata map (e.g. if we are instead
1059   // performing IR-based WPD in hybrid regular/thin LTO mode).
1060   std::map<ValueInfo, std::vector<VTableSlotSummary>> LocalWPDTargetsMap;
1061   std::set<GlobalValue::GUID> ExportedGUIDs;
1062   runWholeProgramDevirtOnIndex(*Index, ExportedGUIDs, LocalWPDTargetsMap);
1063   for (auto GUID : ExportedGUIDs)
1064     GUIDPreservedSymbols.insert(GUID);
1065 
1066   // Collect the import/export lists for all modules from the call-graph in the
1067   // combined index.
1068   StringMap<FunctionImporter::ImportMapTy> ImportLists(ModuleCount);
1069   StringMap<FunctionImporter::ExportSetTy> ExportLists(ModuleCount);
1070   ComputeCrossModuleImport(*Index, ModuleToDefinedGVSummaries, ImportLists,
1071                            ExportLists);
1072 
1073   // We use a std::map here to be able to have a defined ordering when
1074   // producing a hash for the cache entry.
1075   // FIXME: we should be able to compute the caching hash for the entry based
1076   // on the index, and nuke this map.
1077   StringMap<std::map<GlobalValue::GUID, GlobalValue::LinkageTypes>> ResolvedODR;
1078 
1079   DenseMap<GlobalValue::GUID, const GlobalValueSummary *> PrevailingCopy;
1080   computePrevailingCopies(*Index, PrevailingCopy);
1081 
1082   // Resolve prevailing symbols, this has to be computed early because it
1083   // impacts the caching.
1084   resolvePrevailingInIndex(*Index, ResolvedODR, GUIDPreservedSymbols,
1085                            PrevailingCopy);
1086 
1087   // Use global summary-based analysis to identify symbols that can be
1088   // internalized (because they aren't exported or preserved as per callback).
1089   // Changes are made in the index, consumed in the ThinLTO backends.
1090   updateIndexWPDForExports(*Index,
1091                            IsExported(ExportLists, GUIDPreservedSymbols),
1092                            LocalWPDTargetsMap);
1093   thinLTOInternalizeAndPromoteInIndex(
1094       *Index, IsExported(ExportLists, GUIDPreservedSymbols),
1095       IsPrevailing(PrevailingCopy));
1096 
1097   thinLTOPropagateFunctionAttrs(*Index, IsPrevailing(PrevailingCopy));
1098 
1099   // Make sure that every module has an entry in the ExportLists, ImportList,
1100   // GVSummary and ResolvedODR maps to enable threaded access to these maps
1101   // below.
1102   for (auto &Module : Modules) {
1103     auto ModuleIdentifier = Module->getName();
1104     ExportLists[ModuleIdentifier];
1105     ImportLists[ModuleIdentifier];
1106     ResolvedODR[ModuleIdentifier];
1107     ModuleToDefinedGVSummaries[ModuleIdentifier];
1108   }
1109 
1110   std::vector<BitcodeModule *> ModulesVec;
1111   ModulesVec.reserve(Modules.size());
1112   for (auto &Mod : Modules)
1113     ModulesVec.push_back(&Mod->getSingleBitcodeModule());
1114   std::vector<int> ModulesOrdering = lto::generateModulesOrdering(ModulesVec);
1115 
1116   if (llvm::timeTraceProfilerEnabled())
1117     llvm::timeTraceProfilerEnd();
1118 
1119   TimeTraceScopeExit.release();
1120 
1121   // Parallel optimizer + codegen
1122   {
1123     ThreadPool Pool(heavyweight_hardware_concurrency(ThreadCount));
1124     for (auto IndexCount : ModulesOrdering) {
1125       auto &Mod = Modules[IndexCount];
1126       Pool.async([&](int count) {
1127         auto ModuleIdentifier = Mod->getName();
1128         auto &ExportList = ExportLists[ModuleIdentifier];
1129 
1130         auto &DefinedGVSummaries = ModuleToDefinedGVSummaries[ModuleIdentifier];
1131 
1132         // The module may be cached, this helps handling it.
1133         ModuleCacheEntry CacheEntry(CacheOptions.Path, *Index, ModuleIdentifier,
1134                                     ImportLists[ModuleIdentifier], ExportList,
1135                                     ResolvedODR[ModuleIdentifier],
1136                                     DefinedGVSummaries, OptLevel, Freestanding,
1137                                     TMBuilder);
1138         auto CacheEntryPath = CacheEntry.getEntryPath();
1139 
1140         {
1141           auto ErrOrBuffer = CacheEntry.tryLoadingBuffer();
1142           LLVM_DEBUG(dbgs() << "Cache " << (ErrOrBuffer ? "hit" : "miss")
1143                             << " '" << CacheEntryPath << "' for buffer "
1144                             << count << " " << ModuleIdentifier << "\n");
1145 
1146           if (ErrOrBuffer) {
1147             // Cache Hit!
1148             if (SavedObjectsDirectoryPath.empty())
1149               ProducedBinaries[count] = std::move(ErrOrBuffer.get());
1150             else
1151               ProducedBinaryFiles[count] = writeGeneratedObject(
1152                   count, CacheEntryPath, *ErrOrBuffer.get());
1153             return;
1154           }
1155         }
1156 
1157         LLVMContext Context;
1158         Context.setDiscardValueNames(LTODiscardValueNames);
1159         Context.enableDebugTypeODRUniquing();
1160         auto DiagFileOrErr = lto::setupLLVMOptimizationRemarks(
1161             Context, RemarksFilename, RemarksPasses, RemarksFormat,
1162             RemarksWithHotness, RemarksHotnessThreshold, count);
1163         if (!DiagFileOrErr) {
1164           errs() << "Error: " << toString(DiagFileOrErr.takeError()) << "\n";
1165           report_fatal_error("ThinLTO: Can't get an output file for the "
1166                              "remarks");
1167         }
1168 
1169         // Parse module now
1170         auto TheModule = loadModuleFromInput(Mod.get(), Context, false,
1171                                              /*IsImporting*/ false);
1172 
1173         // Save temps: original file.
1174         saveTempBitcode(*TheModule, SaveTempsDir, count, ".0.original.bc");
1175 
1176         auto &ImportList = ImportLists[ModuleIdentifier];
1177         // Run the main process now, and generates a binary
1178         auto OutputBuffer = ProcessThinLTOModule(
1179             *TheModule, *Index, ModuleMap, *TMBuilder.create(), ImportList,
1180             ExportList, GUIDPreservedSymbols,
1181             ModuleToDefinedGVSummaries[ModuleIdentifier], CacheOptions,
1182             DisableCodeGen, SaveTempsDir, Freestanding, OptLevel, count,
1183             DebugPassManager);
1184 
1185         // Commit to the cache (if enabled)
1186         CacheEntry.write(*OutputBuffer);
1187 
1188         if (SavedObjectsDirectoryPath.empty()) {
1189           // We need to generated a memory buffer for the linker.
1190           if (!CacheEntryPath.empty()) {
1191             // When cache is enabled, reload from the cache if possible.
1192             // Releasing the buffer from the heap and reloading it from the
1193             // cache file with mmap helps us to lower memory pressure.
1194             // The freed memory can be used for the next input file.
1195             // The final binary link will read from the VFS cache (hopefully!)
1196             // or from disk (if the memory pressure was too high).
1197             auto ReloadedBufferOrErr = CacheEntry.tryLoadingBuffer();
1198             if (auto EC = ReloadedBufferOrErr.getError()) {
1199               // On error, keep the preexisting buffer and print a diagnostic.
1200               errs() << "remark: can't reload cached file '" << CacheEntryPath
1201                      << "': " << EC.message() << "\n";
1202             } else {
1203               OutputBuffer = std::move(*ReloadedBufferOrErr);
1204             }
1205           }
1206           ProducedBinaries[count] = std::move(OutputBuffer);
1207           return;
1208         }
1209         ProducedBinaryFiles[count] = writeGeneratedObject(
1210             count, CacheEntryPath, *OutputBuffer);
1211       }, IndexCount);
1212     }
1213   }
1214 
1215   pruneCache(CacheOptions.Path, CacheOptions.Policy);
1216 
1217   // If statistics were requested, print them out now.
1218   if (llvm::AreStatisticsEnabled())
1219     llvm::PrintStatistics();
1220   reportAndResetTimings();
1221 }
1222