LLVM  10.0.0svn
ThinLTOCodeGenerator.cpp
Go to the documentation of this file.
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 
15 
16 #include "llvm/ADT/Statistic.h"
17 #include "llvm/ADT/StringExtras.h"
25 #include "llvm/Config/llvm-config.h"
26 #include "llvm/IR/DebugInfo.h"
28 #include "llvm/IR/LLVMContext.h"
30 #include "llvm/IR/Mangler.h"
31 #include "llvm/IR/PassTimingInfo.h"
32 #include "llvm/IR/RemarkStreamer.h"
33 #include "llvm/IR/Verifier.h"
34 #include "llvm/IRReader/IRReader.h"
35 #include "llvm/LTO/LTO.h"
40 #include "llvm/Support/Debug.h"
41 #include "llvm/Support/Error.h"
42 #include "llvm/Support/Path.h"
43 #include "llvm/Support/SHA1.h"
47 #include "llvm/Support/Threading.h"
49 #include "llvm/Support/VCSRevision.h"
51 #include "llvm/Transforms/IPO.h"
57 
58 #include <numeric>
59 
60 #if !defined(_MSC_VER) && !defined(__MINGW32__)
61 #include <unistd.h>
62 #else
63 #include <io.h>
64 #endif
65 
66 using namespace llvm;
67 
68 #define DEBUG_TYPE "thinlto"
69 
70 namespace llvm {
71 // Flags -discard-value-names, defined in LTOCodeGenerator.cpp
77 }
78 
79 namespace {
80 
81 static cl::opt<int>
82  ThreadCount("threads", cl::init(llvm::heavyweight_hardware_concurrency()));
83 
84 // Simple helper to save temporary files for debug.
85 static void saveTempBitcode(const Module &TheModule, StringRef TempDir,
86  unsigned count, StringRef Suffix) {
87  if (TempDir.empty())
88  return;
89  // User asked to save temps, let dump the bitcode file after import.
90  std::string SaveTempPath = (TempDir + llvm::Twine(count) + Suffix).str();
91  std::error_code EC;
92  raw_fd_ostream OS(SaveTempPath, EC, sys::fs::OF_None);
93  if (EC)
94  report_fatal_error(Twine("Failed to open ") + SaveTempPath +
95  " to save optimized bitcode\n");
96  WriteBitcodeToFile(TheModule, OS, /* ShouldPreserveUseListOrder */ true);
97 }
98 
99 static const GlobalValueSummary *
100 getFirstDefinitionForLinker(const GlobalValueSummaryList &GVSummaryList) {
101  // If there is any strong definition anywhere, get it.
102  auto StrongDefForLinker = llvm::find_if(
103  GVSummaryList, [](const std::unique_ptr<GlobalValueSummary> &Summary) {
104  auto Linkage = Summary->linkage();
107  });
108  if (StrongDefForLinker != GVSummaryList.end())
109  return StrongDefForLinker->get();
110  // Get the first *linker visible* definition for this global in the summary
111  // list.
112  auto FirstDefForLinker = llvm::find_if(
113  GVSummaryList, [](const std::unique_ptr<GlobalValueSummary> &Summary) {
114  auto Linkage = Summary->linkage();
116  });
117  // Extern templates can be emitted as available_externally.
118  if (FirstDefForLinker == GVSummaryList.end())
119  return nullptr;
120  return FirstDefForLinker->get();
121 }
122 
123 // Populate map of GUID to the prevailing copy for any multiply defined
124 // symbols. Currently assume first copy is prevailing, or any strong
125 // definition. Can be refined with Linker information in the future.
126 static void computePrevailingCopies(
127  const ModuleSummaryIndex &Index,
129  auto HasMultipleCopies = [&](const GlobalValueSummaryList &GVSummaryList) {
130  return GVSummaryList.size() > 1;
131  };
132 
133  for (auto &I : Index) {
134  if (HasMultipleCopies(I.second.SummaryList))
135  PrevailingCopy[I.first] =
136  getFirstDefinitionForLinker(I.second.SummaryList);
137  }
138 }
139 
141 generateModuleMap(std::vector<std::unique_ptr<lto::InputFile>> &Modules) {
142  StringMap<lto::InputFile *> ModuleMap;
143  for (auto &M : Modules) {
144  assert(ModuleMap.find(M->getName()) == ModuleMap.end() &&
145  "Expect unique Buffer Identifier");
146  ModuleMap[M->getName()] = M.get();
147  }
148  return ModuleMap;
149 }
150 
151 static void promoteModule(Module &TheModule, const ModuleSummaryIndex &Index) {
152  if (renameModuleForThinLTO(TheModule, Index))
153  report_fatal_error("renameModuleForThinLTO failed");
154 }
155 
156 namespace {
157 class ThinLTODiagnosticInfo : public DiagnosticInfo {
158  const Twine &Msg;
159 public:
160  ThinLTODiagnosticInfo(const Twine &DiagMsg,
161  DiagnosticSeverity Severity = DS_Error)
162  : DiagnosticInfo(DK_Linker, Severity), Msg(DiagMsg) {}
163  void print(DiagnosticPrinter &DP) const override { DP << Msg; }
164 };
165 }
166 
167 /// Verify the module and strip broken debug info.
168 static void verifyLoadedModule(Module &TheModule) {
169  bool BrokenDebugInfo = false;
170  if (verifyModule(TheModule, &dbgs(), &BrokenDebugInfo))
171  report_fatal_error("Broken module found, compilation aborted!");
172  if (BrokenDebugInfo) {
173  TheModule.getContext().diagnose(ThinLTODiagnosticInfo(
174  "Invalid debug info found, debug info will be stripped", DS_Warning));
175  StripDebugInfo(TheModule);
176  }
177 }
178 
179 static std::unique_ptr<Module> loadModuleFromInput(lto::InputFile *Input,
181  bool Lazy,
182  bool IsImporting) {
183  auto &Mod = Input->getSingleBitcodeModule();
184  SMDiagnostic Err;
185  Expected<std::unique_ptr<Module>> ModuleOrErr =
186  Lazy ? Mod.getLazyModule(Context,
187  /* ShouldLazyLoadMetadata */ true, IsImporting)
188  : Mod.parseModule(Context);
189  if (!ModuleOrErr) {
190  handleAllErrors(ModuleOrErr.takeError(), [&](ErrorInfoBase &EIB) {
191  SMDiagnostic Err = SMDiagnostic(Mod.getModuleIdentifier(),
192  SourceMgr::DK_Error, EIB.message());
193  Err.print("ThinLTO", errs());
194  });
195  report_fatal_error("Can't load module, abort.");
196  }
197  if (!Lazy)
198  verifyLoadedModule(*ModuleOrErr.get());
199  return std::move(*ModuleOrErr);
200 }
201 
202 static void
203 crossImportIntoModule(Module &TheModule, const ModuleSummaryIndex &Index,
204  StringMap<lto::InputFile*> &ModuleMap,
205  const FunctionImporter::ImportMapTy &ImportList) {
206  auto Loader = [&](StringRef Identifier) {
207  auto &Input = ModuleMap[Identifier];
208  return loadModuleFromInput(Input, TheModule.getContext(),
209  /*Lazy=*/true, /*IsImporting*/ true);
210  };
211 
212  FunctionImporter Importer(Index, Loader);
213  Expected<bool> Result = Importer.importFunctions(TheModule, ImportList);
214  if (!Result) {
215  handleAllErrors(Result.takeError(), [&](ErrorInfoBase &EIB) {
217  SourceMgr::DK_Error, EIB.message());
218  Err.print("ThinLTO", errs());
219  });
220  report_fatal_error("importFunctions failed");
221  }
222  // Verify again after cross-importing.
223  verifyLoadedModule(TheModule);
224 }
225 
226 static void optimizeModule(Module &TheModule, TargetMachine &TM,
227  unsigned OptLevel, bool Freestanding) {
228  // Populate the PassManager
229  PassManagerBuilder PMB;
231  if (Freestanding)
234  // FIXME: should get it from the bitcode?
235  PMB.OptLevel = OptLevel;
236  PMB.LoopVectorize = true;
237  PMB.SLPVectorize = true;
238  // Already did this in verifyLoadedModule().
239  PMB.VerifyInput = false;
240  PMB.VerifyOutput = false;
241 
243 
244  // Add the TTI (required to inform the vectorizer about register size for
245  // instance)
247 
248  // Add optimizations
250 
251  PM.run(TheModule);
252 }
253 
254 static void
255 addUsedSymbolToPreservedGUID(const lto::InputFile &File,
256  DenseSet<GlobalValue::GUID> &PreservedGUID) {
257  for (const auto &Sym : File.symbols()) {
258  if (Sym.isUsed())
259  PreservedGUID.insert(GlobalValue::getGUID(Sym.getIRName()));
260  }
261 }
262 
263 // Convert the PreservedSymbols map from "Name" based to "GUID" based.
265 computeGUIDPreservedSymbols(const StringSet<> &PreservedSymbols,
266  const Triple &TheTriple) {
267  DenseSet<GlobalValue::GUID> GUIDPreservedSymbols(PreservedSymbols.size());
268  for (auto &Entry : PreservedSymbols) {
269  StringRef Name = Entry.first();
270  if (TheTriple.isOSBinFormatMachO() && Name.size() > 0 && Name[0] == '_')
271  Name = Name.drop_front();
272  GUIDPreservedSymbols.insert(GlobalValue::getGUID(Name));
273  }
274  return GUIDPreservedSymbols;
275 }
276 
277 std::unique_ptr<MemoryBuffer> codegenModule(Module &TheModule,
278  TargetMachine &TM) {
279  SmallVector<char, 128> OutputBuffer;
280 
281  // CodeGen
282  {
283  raw_svector_ostream OS(OutputBuffer);
285 
286  // If the bitcode files contain ARC code and were compiled with optimization,
287  // the ObjCARCContractPass must be run, so do it unconditionally here.
289 
290  // Setup the codegen now.
291  if (TM.addPassesToEmitFile(PM, OS, nullptr, TargetMachine::CGFT_ObjectFile,
292  /* DisableVerify */ true))
293  report_fatal_error("Failed to setup codegen");
294 
295  // Run codegen now. resulting binary is in OutputBuffer.
296  PM.run(TheModule);
297  }
298  return std::make_unique<SmallVectorMemoryBuffer>(std::move(OutputBuffer));
299 }
300 
301 /// Manage caching for a single Module.
302 class ModuleCacheEntry {
303  SmallString<128> EntryPath;
304 
305 public:
306  // Create a cache entry. This compute a unique hash for the Module considering
307  // the current list of export/import, and offer an interface to query to
308  // access the content in the cache.
309  ModuleCacheEntry(
310  StringRef CachePath, const ModuleSummaryIndex &Index, StringRef ModuleID,
311  const FunctionImporter::ImportMapTy &ImportList,
312  const FunctionImporter::ExportSetTy &ExportList,
313  const std::map<GlobalValue::GUID, GlobalValue::LinkageTypes> &ResolvedODR,
314  const GVSummaryMapTy &DefinedGVSummaries, unsigned OptLevel,
315  bool Freestanding, const TargetMachineBuilder &TMBuilder) {
316  if (CachePath.empty())
317  return;
318 
319  if (!Index.modulePaths().count(ModuleID))
320  // The module does not have an entry, it can't have a hash at all
321  return;
322 
323  if (all_of(Index.getModuleHash(ModuleID),
324  [](uint32_t V) { return V == 0; }))
325  // No hash entry, no caching!
326  return;
327 
328  llvm::lto::Config Conf;
329  Conf.OptLevel = OptLevel;
330  Conf.Options = TMBuilder.Options;
331  Conf.CPU = TMBuilder.MCpu;
332  Conf.MAttrs.push_back(TMBuilder.MAttr);
333  Conf.RelocModel = TMBuilder.RelocModel;
334  Conf.CGOptLevel = TMBuilder.CGOptLevel;
335  Conf.Freestanding = Freestanding;
337  computeLTOCacheKey(Key, Conf, Index, ModuleID, ImportList, ExportList,
338  ResolvedODR, DefinedGVSummaries);
339 
340  // This choice of file name allows the cache to be pruned (see pruneCache()
341  // in include/llvm/Support/CachePruning.h).
342  sys::path::append(EntryPath, CachePath, "llvmcache-" + Key);
343  }
344 
345  // Access the path to this entry in the cache.
346  StringRef getEntryPath() { return EntryPath; }
347 
348  // Try loading the buffer for this cache entry.
349  ErrorOr<std::unique_ptr<MemoryBuffer>> tryLoadingBuffer() {
350  if (EntryPath.empty())
351  return std::error_code();
352  SmallString<64> ResultPath;
354  Twine(EntryPath), sys::fs::OF_UpdateAtime, &ResultPath);
355  if (!FDOrErr)
356  return errorToErrorCode(FDOrErr.takeError());
358  *FDOrErr, EntryPath, /*FileSize=*/-1, /*RequiresNullTerminator=*/false);
359  sys::fs::closeFile(*FDOrErr);
360  return MBOrErr;
361  }
362 
363  // Cache the Produced object file
364  void write(const MemoryBuffer &OutputBuffer) {
365  if (EntryPath.empty())
366  return;
367 
368  // Write to a temporary to avoid race condition
369  SmallString<128> TempFilename;
370  SmallString<128> CachePath(EntryPath);
371  int TempFD;
373  sys::path::append(TempFilename, CachePath, "Thin-%%%%%%.tmp.o");
374  std::error_code EC =
375  sys::fs::createUniqueFile(TempFilename, TempFD, TempFilename);
376  if (EC) {
377  errs() << "Error: " << EC.message() << "\n";
378  report_fatal_error("ThinLTO: Can't get a temporary file");
379  }
380  {
381  raw_fd_ostream OS(TempFD, /* ShouldClose */ true);
382  OS << OutputBuffer.getBuffer();
383  }
384  // Rename temp file to final destination; rename is atomic
385  EC = sys::fs::rename(TempFilename, EntryPath);
386  if (EC)
387  sys::fs::remove(TempFilename);
388  }
389 };
390 
391 static std::unique_ptr<MemoryBuffer>
392 ProcessThinLTOModule(Module &TheModule, ModuleSummaryIndex &Index,
394  const FunctionImporter::ImportMapTy &ImportList,
395  const FunctionImporter::ExportSetTy &ExportList,
396  const DenseSet<GlobalValue::GUID> &GUIDPreservedSymbols,
397  const GVSummaryMapTy &DefinedGlobals,
398  const ThinLTOCodeGenerator::CachingOptions &CacheOptions,
399  bool DisableCodeGen, StringRef SaveTempsDir,
400  bool Freestanding, unsigned OptLevel, unsigned count) {
401 
402  // "Benchmark"-like optimization: single-source case
403  bool SingleModule = (ModuleMap.size() == 1);
404 
405  if (!SingleModule) {
406  promoteModule(TheModule, Index);
407 
408  // Apply summary-based prevailing-symbol resolution decisions.
409  thinLTOResolvePrevailingInModule(TheModule, DefinedGlobals);
410 
411  // Save temps: after promotion.
412  saveTempBitcode(TheModule, SaveTempsDir, count, ".1.promoted.bc");
413  }
414 
415  // Be friendly and don't nuke totally the module when the client didn't
416  // supply anything to preserve.
417  if (!ExportList.empty() || !GUIDPreservedSymbols.empty()) {
418  // Apply summary-based internalization decisions.
419  thinLTOInternalizeModule(TheModule, DefinedGlobals);
420  }
421 
422  // Save internalized bitcode
423  saveTempBitcode(TheModule, SaveTempsDir, count, ".2.internalized.bc");
424 
425  if (!SingleModule) {
426  crossImportIntoModule(TheModule, Index, ModuleMap, ImportList);
427 
428  // Save temps: after cross-module import.
429  saveTempBitcode(TheModule, SaveTempsDir, count, ".3.imported.bc");
430  }
431 
432  optimizeModule(TheModule, TM, OptLevel, Freestanding);
433 
434  saveTempBitcode(TheModule, SaveTempsDir, count, ".4.opt.bc");
435 
436  if (DisableCodeGen) {
437  // Configured to stop before CodeGen, serialize the bitcode and return.
438  SmallVector<char, 128> OutputBuffer;
439  {
440  raw_svector_ostream OS(OutputBuffer);
441  ProfileSummaryInfo PSI(TheModule);
442  auto Index = buildModuleSummaryIndex(TheModule, nullptr, &PSI);
443  WriteBitcodeToFile(TheModule, OS, true, &Index);
444  }
445  return std::make_unique<SmallVectorMemoryBuffer>(std::move(OutputBuffer));
446  }
447 
448  return codegenModule(TheModule, TM);
449 }
450 
451 /// Resolve prevailing symbols. Record resolutions in the \p ResolvedODR map
452 /// for caching, and in the \p Index for application during the ThinLTO
453 /// backends. This is needed for correctness for exported symbols (ensure
454 /// at least one copy kept) and a compile-time optimization (to drop duplicate
455 /// copies when possible).
456 static void resolvePrevailingInIndex(
457  ModuleSummaryIndex &Index,
458  StringMap<std::map<GlobalValue::GUID, GlobalValue::LinkageTypes>>
459  &ResolvedODR,
460  const DenseSet<GlobalValue::GUID> &GUIDPreservedSymbols) {
461 
463  computePrevailingCopies(Index, PrevailingCopy);
464 
465  auto isPrevailing = [&](GlobalValue::GUID GUID, const GlobalValueSummary *S) {
466  const auto &Prevailing = PrevailingCopy.find(GUID);
467  // Not in map means that there was only one copy, which must be prevailing.
468  if (Prevailing == PrevailingCopy.end())
469  return true;
470  return Prevailing->second == S;
471  };
472 
473  auto recordNewLinkage = [&](StringRef ModuleIdentifier,
474  GlobalValue::GUID GUID,
475  GlobalValue::LinkageTypes NewLinkage) {
476  ResolvedODR[ModuleIdentifier][GUID] = NewLinkage;
477  };
478 
479  thinLTOResolvePrevailingInIndex(Index, isPrevailing, recordNewLinkage,
480  GUIDPreservedSymbols);
481 }
482 
483 // Initialize the TargetMachine builder for a given Triple
484 static void initTMBuilder(TargetMachineBuilder &TMBuilder,
485  const Triple &TheTriple) {
486  // Set a default CPU for Darwin triples (copied from LTOCodeGenerator).
487  // FIXME this looks pretty terrible...
488  if (TMBuilder.MCpu.empty() && TheTriple.isOSDarwin()) {
489  if (TheTriple.getArch() == llvm::Triple::x86_64)
490  TMBuilder.MCpu = "core2";
491  else if (TheTriple.getArch() == llvm::Triple::x86)
492  TMBuilder.MCpu = "yonah";
493  else if (TheTriple.getArch() == llvm::Triple::aarch64)
494  TMBuilder.MCpu = "cyclone";
495  }
496  TMBuilder.TheTriple = std::move(TheTriple);
497 }
498 
499 } // end anonymous namespace
500 
502  MemoryBufferRef Buffer(Data, Identifier);
503 
504  auto InputOrError = lto::InputFile::create(Buffer);
505  if (!InputOrError)
506  report_fatal_error("ThinLTO cannot create input file: " +
507  toString(InputOrError.takeError()));
508 
509  auto TripleStr = (*InputOrError)->getTargetTriple();
510  Triple TheTriple(TripleStr);
511 
512  if (Modules.empty())
513  initTMBuilder(TMBuilder, Triple(TheTriple));
514  else if (TMBuilder.TheTriple != TheTriple) {
515  if (!TMBuilder.TheTriple.isCompatibleWith(TheTriple))
516  report_fatal_error("ThinLTO modules with incompatible triples not "
517  "supported");
518  initTMBuilder(TMBuilder, Triple(TMBuilder.TheTriple.merge(TheTriple)));
519  }
520 
521  Modules.emplace_back(std::move(*InputOrError));
522 }
523 
525  PreservedSymbols.insert(Name);
526 }
527 
529  // FIXME: At the moment, we don't take advantage of this extra information,
530  // we're conservatively considering cross-references as preserved.
531  // CrossReferencedSymbols.insert(Name);
532  PreservedSymbols.insert(Name);
533 }
534 
535 // TargetMachine factory
536 std::unique_ptr<TargetMachine> TargetMachineBuilder::create() const {
537  std::string ErrMsg;
538  const Target *TheTarget =
539  TargetRegistry::lookupTarget(TheTriple.str(), ErrMsg);
540  if (!TheTarget) {
541  report_fatal_error("Can't load target for this Triple: " + ErrMsg);
542  }
543 
544  // Use MAttr as the default set of features.
545  SubtargetFeatures Features(MAttr);
546  Features.getDefaultSubtargetFeatures(TheTriple);
547  std::string FeatureStr = Features.getString();
548 
549  return std::unique_ptr<TargetMachine>(
550  TheTarget->createTargetMachine(TheTriple.str(), MCpu, FeatureStr, Options,
551  RelocModel, None, CGOptLevel));
552 }
553 
554 /**
555  * Produce the combined summary index from all the bitcode files:
556  * "thin-link".
557  */
558 std::unique_ptr<ModuleSummaryIndex> ThinLTOCodeGenerator::linkCombinedIndex() {
559  std::unique_ptr<ModuleSummaryIndex> CombinedIndex =
560  std::make_unique<ModuleSummaryIndex>(/*HaveGVs=*/false);
561  uint64_t NextModuleId = 0;
562  for (auto &Mod : Modules) {
563  auto &M = Mod->getSingleBitcodeModule();
564  if (Error Err =
565  M.readSummary(*CombinedIndex, Mod->getName(), NextModuleId++)) {
566  // FIXME diagnose
568  std::move(Err), errs(),
569  "error: can't create module summary index for buffer: ");
570  return nullptr;
571  }
572  }
573  return CombinedIndex;
574 }
575 
577  const StringMap<FunctionImporter::ExportSetTy> &ExportLists,
578  const DenseSet<GlobalValue::GUID> &GUIDPreservedSymbols,
580  auto isExported = [&](StringRef ModuleIdentifier, GlobalValue::GUID GUID) {
581  const auto &ExportList = ExportLists.find(ModuleIdentifier);
582  return (ExportList != ExportLists.end() &&
583  ExportList->second.count(GUID)) ||
584  GUIDPreservedSymbols.count(GUID);
585  };
586 
587  thinLTOInternalizeAndPromoteInIndex(Index, isExported);
588 }
589 
592  const DenseSet<GlobalValue::GUID> &GUIDPreservedSymbols) {
593  // We have no symbols resolution available. And can't do any better now in the
594  // case where the prevailing symbol is in a native object. It can be refined
595  // with linker information in the future.
596  auto isPrevailing = [&](GlobalValue::GUID G) {
598  };
599  computeDeadSymbolsWithConstProp(Index, GUIDPreservedSymbols, isPrevailing,
600  /* ImportEnabled = */ true);
601 }
602 
603 /**
604  * Perform promotion and renaming of exported internal functions.
605  * Index is updated to reflect linkage changes from weak resolution.
606  */
608  const lto::InputFile &File) {
609  auto ModuleCount = Index.modulePaths().size();
610  auto ModuleIdentifier = TheModule.getModuleIdentifier();
611 
612  // Collect for each module the list of function it defines (GUID -> Summary).
613  StringMap<GVSummaryMapTy> ModuleToDefinedGVSummaries;
614  Index.collectDefinedGVSummariesPerModule(ModuleToDefinedGVSummaries);
615 
616  // Convert the preserved symbols set from string to GUID
617  auto GUIDPreservedSymbols = computeGUIDPreservedSymbols(
618  PreservedSymbols, Triple(TheModule.getTargetTriple()));
619 
620  // Add used symbol to the preserved symbols.
621  addUsedSymbolToPreservedGUID(File, GUIDPreservedSymbols);
622 
623  // Compute "dead" symbols, we don't want to import/export these!
624  computeDeadSymbolsInIndex(Index, GUIDPreservedSymbols);
625 
626  // Generate import/export list
627  StringMap<FunctionImporter::ImportMapTy> ImportLists(ModuleCount);
628  StringMap<FunctionImporter::ExportSetTy> ExportLists(ModuleCount);
629  ComputeCrossModuleImport(Index, ModuleToDefinedGVSummaries, ImportLists,
630  ExportLists);
631 
632  // Resolve prevailing symbols
634  resolvePrevailingInIndex(Index, ResolvedODR, GUIDPreservedSymbols);
635 
637  TheModule, ModuleToDefinedGVSummaries[ModuleIdentifier]);
638 
639  // Promote the exported values in the index, so that they are promoted
640  // in the module.
641  internalizeAndPromoteInIndex(ExportLists, GUIDPreservedSymbols, Index);
642 
643  promoteModule(TheModule, Index);
644 }
645 
646 /**
647  * Perform cross-module importing for the module identified by ModuleIdentifier.
648  */
651  const lto::InputFile &File) {
652  auto ModuleMap = generateModuleMap(Modules);
653  auto ModuleCount = Index.modulePaths().size();
654 
655  // Collect for each module the list of function it defines (GUID -> Summary).
656  StringMap<GVSummaryMapTy> ModuleToDefinedGVSummaries(ModuleCount);
657  Index.collectDefinedGVSummariesPerModule(ModuleToDefinedGVSummaries);
658 
659  // Convert the preserved symbols set from string to GUID
660  auto GUIDPreservedSymbols = computeGUIDPreservedSymbols(
661  PreservedSymbols, Triple(TheModule.getTargetTriple()));
662 
663  addUsedSymbolToPreservedGUID(File, GUIDPreservedSymbols);
664 
665  // Compute "dead" symbols, we don't want to import/export these!
666  computeDeadSymbolsInIndex(Index, GUIDPreservedSymbols);
667 
668  // Generate import/export list
669  StringMap<FunctionImporter::ImportMapTy> ImportLists(ModuleCount);
670  StringMap<FunctionImporter::ExportSetTy> ExportLists(ModuleCount);
671  ComputeCrossModuleImport(Index, ModuleToDefinedGVSummaries, ImportLists,
672  ExportLists);
673  auto &ImportList = ImportLists[TheModule.getModuleIdentifier()];
674 
675  crossImportIntoModule(TheModule, Index, ModuleMap, ImportList);
676 }
677 
678 /**
679  * Compute the list of summaries needed for importing into module.
680  */
682  Module &TheModule, ModuleSummaryIndex &Index,
683  std::map<std::string, GVSummaryMapTy> &ModuleToSummariesForIndex,
684  const lto::InputFile &File) {
685  auto ModuleCount = Index.modulePaths().size();
686  auto ModuleIdentifier = TheModule.getModuleIdentifier();
687 
688  // Collect for each module the list of function it defines (GUID -> Summary).
689  StringMap<GVSummaryMapTy> ModuleToDefinedGVSummaries(ModuleCount);
690  Index.collectDefinedGVSummariesPerModule(ModuleToDefinedGVSummaries);
691 
692  // Convert the preserved symbols set from string to GUID
693  auto GUIDPreservedSymbols = computeGUIDPreservedSymbols(
694  PreservedSymbols, Triple(TheModule.getTargetTriple()));
695 
696  addUsedSymbolToPreservedGUID(File, GUIDPreservedSymbols);
697 
698  // Compute "dead" symbols, we don't want to import/export these!
699  computeDeadSymbolsInIndex(Index, GUIDPreservedSymbols);
700 
701  // Generate import/export list
702  StringMap<FunctionImporter::ImportMapTy> ImportLists(ModuleCount);
703  StringMap<FunctionImporter::ExportSetTy> ExportLists(ModuleCount);
704  ComputeCrossModuleImport(Index, ModuleToDefinedGVSummaries, ImportLists,
705  ExportLists);
706 
708  ModuleIdentifier, ModuleToDefinedGVSummaries,
709  ImportLists[ModuleIdentifier], ModuleToSummariesForIndex);
710 }
711 
712 /**
713  * Emit the list of files needed for importing into module.
714  */
717  const lto::InputFile &File) {
718  auto ModuleCount = Index.modulePaths().size();
719  auto ModuleIdentifier = TheModule.getModuleIdentifier();
720 
721  // Collect for each module the list of function it defines (GUID -> Summary).
722  StringMap<GVSummaryMapTy> ModuleToDefinedGVSummaries(ModuleCount);
723  Index.collectDefinedGVSummariesPerModule(ModuleToDefinedGVSummaries);
724 
725  // Convert the preserved symbols set from string to GUID
726  auto GUIDPreservedSymbols = computeGUIDPreservedSymbols(
727  PreservedSymbols, Triple(TheModule.getTargetTriple()));
728 
729  addUsedSymbolToPreservedGUID(File, GUIDPreservedSymbols);
730 
731  // Compute "dead" symbols, we don't want to import/export these!
732  computeDeadSymbolsInIndex(Index, GUIDPreservedSymbols);
733 
734  // Generate import/export list
735  StringMap<FunctionImporter::ImportMapTy> ImportLists(ModuleCount);
736  StringMap<FunctionImporter::ExportSetTy> ExportLists(ModuleCount);
737  ComputeCrossModuleImport(Index, ModuleToDefinedGVSummaries, ImportLists,
738  ExportLists);
739 
740  std::map<std::string, GVSummaryMapTy> ModuleToSummariesForIndex;
742  ModuleIdentifier, ModuleToDefinedGVSummaries,
743  ImportLists[ModuleIdentifier], ModuleToSummariesForIndex);
744 
745  std::error_code EC;
746  if ((EC = EmitImportsFiles(ModuleIdentifier, OutputName,
747  ModuleToSummariesForIndex)))
748  report_fatal_error(Twine("Failed to open ") + OutputName +
749  " to save imports lists\n");
750 }
751 
752 /**
753  * Perform internalization. Runs promote and internalization together.
754  * Index is updated to reflect linkage changes.
755  */
758  const lto::InputFile &File) {
759  initTMBuilder(TMBuilder, Triple(TheModule.getTargetTriple()));
760  auto ModuleCount = Index.modulePaths().size();
761  auto ModuleIdentifier = TheModule.getModuleIdentifier();
762 
763  // Convert the preserved symbols set from string to GUID
764  auto GUIDPreservedSymbols =
765  computeGUIDPreservedSymbols(PreservedSymbols, TMBuilder.TheTriple);
766 
767  addUsedSymbolToPreservedGUID(File, GUIDPreservedSymbols);
768 
769  // Collect for each module the list of function it defines (GUID -> Summary).
770  StringMap<GVSummaryMapTy> ModuleToDefinedGVSummaries(ModuleCount);
771  Index.collectDefinedGVSummariesPerModule(ModuleToDefinedGVSummaries);
772 
773  // Compute "dead" symbols, we don't want to import/export these!
774  computeDeadSymbolsInIndex(Index, GUIDPreservedSymbols);
775 
776  // Generate import/export list
777  StringMap<FunctionImporter::ImportMapTy> ImportLists(ModuleCount);
778  StringMap<FunctionImporter::ExportSetTy> ExportLists(ModuleCount);
779  ComputeCrossModuleImport(Index, ModuleToDefinedGVSummaries, ImportLists,
780  ExportLists);
781  auto &ExportList = ExportLists[ModuleIdentifier];
782 
783  // Be friendly and don't nuke totally the module when the client didn't
784  // supply anything to preserve.
785  if (ExportList.empty() && GUIDPreservedSymbols.empty())
786  return;
787 
788  // Resolve prevailing symbols
790  resolvePrevailingInIndex(Index, ResolvedODR, GUIDPreservedSymbols);
791 
792  // Promote the exported values in the index, so that they are promoted
793  // in the module.
794  internalizeAndPromoteInIndex(ExportLists, GUIDPreservedSymbols, Index);
795 
796  promoteModule(TheModule, Index);
797 
798  // Internalization
800  TheModule, ModuleToDefinedGVSummaries[ModuleIdentifier]);
801 
802  thinLTOInternalizeModule(TheModule,
803  ModuleToDefinedGVSummaries[ModuleIdentifier]);
804 }
805 
806 /**
807  * Perform post-importing ThinLTO optimizations.
808  */
810  initTMBuilder(TMBuilder, Triple(TheModule.getTargetTriple()));
811 
812  // Optimize now
813  optimizeModule(TheModule, *TMBuilder.create(), OptLevel, Freestanding);
814 }
815 
816 /// Write out the generated object file, either from CacheEntryPath or from
817 /// OutputBuffer, preferring hard-link when possible.
818 /// Returns the path to the generated file in SavedObjectsDirectoryPath.
819 std::string
821  const MemoryBuffer &OutputBuffer) {
822  auto ArchName = TMBuilder.TheTriple.getArchName();
823  SmallString<128> OutputPath(SavedObjectsDirectoryPath);
824  llvm::sys::path::append(OutputPath,
825  Twine(count) + "." + ArchName + ".thinlto.o");
826  OutputPath.c_str(); // Ensure the string is null terminated.
827  if (sys::fs::exists(OutputPath))
828  sys::fs::remove(OutputPath);
829 
830  // We don't return a memory buffer to the linker, just a list of files.
831  if (!CacheEntryPath.empty()) {
832  // Cache is enabled, hard-link the entry (or copy if hard-link fails).
833  auto Err = sys::fs::create_hard_link(CacheEntryPath, OutputPath);
834  if (!Err)
835  return OutputPath.str();
836  // Hard linking failed, try to copy.
837  Err = sys::fs::copy_file(CacheEntryPath, OutputPath);
838  if (!Err)
839  return OutputPath.str();
840  // Copy failed (could be because the CacheEntry was removed from the cache
841  // in the meantime by another process), fall back and try to write down the
842  // buffer to the output.
843  errs() << "error: can't link or copy from cached entry '" << CacheEntryPath
844  << "' to '" << OutputPath << "'\n";
845  }
846  // No cache entry, just write out the buffer.
847  std::error_code Err;
848  raw_fd_ostream OS(OutputPath, Err, sys::fs::OF_None);
849  if (Err)
850  report_fatal_error("Can't open output '" + OutputPath + "'\n");
851  OS << OutputBuffer.getBuffer();
852  return OutputPath.str();
853 }
854 
855 // Main entry point for the ThinLTO processing
857  // Prepare the resulting object vector
858  assert(ProducedBinaries.empty() && "The generator should not be reused");
859  if (SavedObjectsDirectoryPath.empty())
860  ProducedBinaries.resize(Modules.size());
861  else {
862  sys::fs::create_directories(SavedObjectsDirectoryPath);
863  bool IsDir;
864  sys::fs::is_directory(SavedObjectsDirectoryPath, IsDir);
865  if (!IsDir)
866  report_fatal_error("Unexistent dir: '" + SavedObjectsDirectoryPath + "'");
867  ProducedBinaryFiles.resize(Modules.size());
868  }
869 
870  if (CodeGenOnly) {
871  // Perform only parallel codegen and return.
872  ThreadPool Pool;
873  int count = 0;
874  for (auto &Mod : Modules) {
875  Pool.async([&](int count) {
878 
879  // Parse module now
880  auto TheModule = loadModuleFromInput(Mod.get(), Context, false,
881  /*IsImporting*/ false);
882 
883  // CodeGen
884  auto OutputBuffer = codegenModule(*TheModule, *TMBuilder.create());
885  if (SavedObjectsDirectoryPath.empty())
886  ProducedBinaries[count] = std::move(OutputBuffer);
887  else
888  ProducedBinaryFiles[count] =
889  writeGeneratedObject(count, "", *OutputBuffer);
890  }, count++);
891  }
892 
893  return;
894  }
895 
896  // Sequential linking phase
897  auto Index = linkCombinedIndex();
898 
899  // Save temps: index.
900  if (!SaveTempsDir.empty()) {
901  auto SaveTempPath = SaveTempsDir + "index.bc";
902  std::error_code EC;
903  raw_fd_ostream OS(SaveTempPath, EC, sys::fs::OF_None);
904  if (EC)
905  report_fatal_error(Twine("Failed to open ") + SaveTempPath +
906  " to save optimized bitcode\n");
907  WriteIndexToFile(*Index, OS);
908  }
909 
910 
911  // Prepare the module map.
912  auto ModuleMap = generateModuleMap(Modules);
913  auto ModuleCount = Modules.size();
914 
915  // Collect for each module the list of function it defines (GUID -> Summary).
916  StringMap<GVSummaryMapTy> ModuleToDefinedGVSummaries(ModuleCount);
917  Index->collectDefinedGVSummariesPerModule(ModuleToDefinedGVSummaries);
918 
919  // Convert the preserved symbols set from string to GUID, this is needed for
920  // computing the caching hash and the internalization.
921  auto GUIDPreservedSymbols =
922  computeGUIDPreservedSymbols(PreservedSymbols, TMBuilder.TheTriple);
923 
924  // Add used symbol from inputs to the preserved symbols.
925  for (const auto &M : Modules)
926  addUsedSymbolToPreservedGUID(*M, GUIDPreservedSymbols);
927 
928  // Compute "dead" symbols, we don't want to import/export these!
929  computeDeadSymbolsInIndex(*Index, GUIDPreservedSymbols);
930 
931  // Synthesize entry counts for functions in the combined index.
933 
934  // Collect the import/export lists for all modules from the call-graph in the
935  // combined index.
936  StringMap<FunctionImporter::ImportMapTy> ImportLists(ModuleCount);
937  StringMap<FunctionImporter::ExportSetTy> ExportLists(ModuleCount);
938  ComputeCrossModuleImport(*Index, ModuleToDefinedGVSummaries, ImportLists,
939  ExportLists);
940 
941  // We use a std::map here to be able to have a defined ordering when
942  // producing a hash for the cache entry.
943  // FIXME: we should be able to compute the caching hash for the entry based
944  // on the index, and nuke this map.
946 
947  // Resolve prevailing symbols, this has to be computed early because it
948  // impacts the caching.
949  resolvePrevailingInIndex(*Index, ResolvedODR, GUIDPreservedSymbols);
950 
951  // Use global summary-based analysis to identify symbols that can be
952  // internalized (because they aren't exported or preserved as per callback).
953  // Changes are made in the index, consumed in the ThinLTO backends.
954  internalizeAndPromoteInIndex(ExportLists, GUIDPreservedSymbols, *Index);
955 
956  // Make sure that every module has an entry in the ExportLists, ImportList,
957  // GVSummary and ResolvedODR maps to enable threaded access to these maps
958  // below.
959  for (auto &Module : Modules) {
960  auto ModuleIdentifier = Module->getName();
961  ExportLists[ModuleIdentifier];
962  ImportLists[ModuleIdentifier];
963  ResolvedODR[ModuleIdentifier];
964  ModuleToDefinedGVSummaries[ModuleIdentifier];
965  }
966 
967  // Compute the ordering we will process the inputs: the rough heuristic here
968  // is to sort them per size so that the largest module get schedule as soon as
969  // possible. This is purely a compile-time optimization.
970  std::vector<int> ModulesOrdering;
971  ModulesOrdering.resize(Modules.size());
972  std::iota(ModulesOrdering.begin(), ModulesOrdering.end(), 0);
973  llvm::sort(ModulesOrdering, [&](int LeftIndex, int RightIndex) {
974  auto LSize =
975  Modules[LeftIndex]->getSingleBitcodeModule().getBuffer().size();
976  auto RSize =
977  Modules[RightIndex]->getSingleBitcodeModule().getBuffer().size();
978  return LSize > RSize;
979  });
980 
981  // Parallel optimizer + codegen
982  {
983  ThreadPool Pool(ThreadCount);
984  for (auto IndexCount : ModulesOrdering) {
985  auto &Mod = Modules[IndexCount];
986  Pool.async([&](int count) {
987  auto ModuleIdentifier = Mod->getName();
988  auto &ExportList = ExportLists[ModuleIdentifier];
989 
990  auto &DefinedGVSummaries = ModuleToDefinedGVSummaries[ModuleIdentifier];
991 
992  // The module may be cached, this helps handling it.
993  ModuleCacheEntry CacheEntry(CacheOptions.Path, *Index, ModuleIdentifier,
994  ImportLists[ModuleIdentifier], ExportList,
995  ResolvedODR[ModuleIdentifier],
996  DefinedGVSummaries, OptLevel, Freestanding,
997  TMBuilder);
998  auto CacheEntryPath = CacheEntry.getEntryPath();
999 
1000  {
1001  auto ErrOrBuffer = CacheEntry.tryLoadingBuffer();
1002  LLVM_DEBUG(dbgs() << "Cache " << (ErrOrBuffer ? "hit" : "miss")
1003  << " '" << CacheEntryPath << "' for buffer "
1004  << count << " " << ModuleIdentifier << "\n");
1005 
1006  if (ErrOrBuffer) {
1007  // Cache Hit!
1008  if (SavedObjectsDirectoryPath.empty())
1009  ProducedBinaries[count] = std::move(ErrOrBuffer.get());
1010  else
1011  ProducedBinaryFiles[count] = writeGeneratedObject(
1012  count, CacheEntryPath, *ErrOrBuffer.get());
1013  return;
1014  }
1015  }
1016 
1019  Context.enableDebugTypeODRUniquing();
1020  auto DiagFileOrErr = lto::setupOptimizationRemarks(
1022  RemarksWithHotness, count);
1023  if (!DiagFileOrErr) {
1024  errs() << "Error: " << toString(DiagFileOrErr.takeError()) << "\n";
1025  report_fatal_error("ThinLTO: Can't get an output file for the "
1026  "remarks");
1027  }
1028 
1029  // Parse module now
1030  auto TheModule = loadModuleFromInput(Mod.get(), Context, false,
1031  /*IsImporting*/ false);
1032 
1033  // Save temps: original file.
1034  saveTempBitcode(*TheModule, SaveTempsDir, count, ".0.original.bc");
1035 
1036  auto &ImportList = ImportLists[ModuleIdentifier];
1037  // Run the main process now, and generates a binary
1038  auto OutputBuffer = ProcessThinLTOModule(
1039  *TheModule, *Index, ModuleMap, *TMBuilder.create(), ImportList,
1040  ExportList, GUIDPreservedSymbols,
1041  ModuleToDefinedGVSummaries[ModuleIdentifier], CacheOptions,
1042  DisableCodeGen, SaveTempsDir, Freestanding, OptLevel, count);
1043 
1044  // Commit to the cache (if enabled)
1045  CacheEntry.write(*OutputBuffer);
1046 
1047  if (SavedObjectsDirectoryPath.empty()) {
1048  // We need to generated a memory buffer for the linker.
1049  if (!CacheEntryPath.empty()) {
1050  // When cache is enabled, reload from the cache if possible.
1051  // Releasing the buffer from the heap and reloading it from the
1052  // cache file with mmap helps us to lower memory pressure.
1053  // The freed memory can be used for the next input file.
1054  // The final binary link will read from the VFS cache (hopefully!)
1055  // or from disk (if the memory pressure was too high).
1056  auto ReloadedBufferOrErr = CacheEntry.tryLoadingBuffer();
1057  if (auto EC = ReloadedBufferOrErr.getError()) {
1058  // On error, keep the preexisting buffer and print a diagnostic.
1059  errs() << "error: can't reload cached file '" << CacheEntryPath
1060  << "': " << EC.message() << "\n";
1061  } else {
1062  OutputBuffer = std::move(*ReloadedBufferOrErr);
1063  }
1064  }
1065  ProducedBinaries[count] = std::move(OutputBuffer);
1066  return;
1067  }
1068  ProducedBinaryFiles[count] = writeGeneratedObject(
1069  count, CacheEntryPath, *OutputBuffer);
1070  }, IndexCount);
1071  }
1072  }
1073 
1074  pruneCache(CacheOptions.Path, CacheOptions.Policy);
1075 
1076  // If statistics were requested, print them out now.
1080 }
bool isOSDarwin() const
isOSDarwin - Is this a "Darwin" OS (OS X, iOS, or watchOS).
Definition: Triple.h:481
std::error_code create_directories(const Twine &path, bool IgnoreExisting=true, perms Perms=owner_all|group_all)
Create all the non-existent directories in path.
Definition: Path.cpp:915
ArrayRef< Symbol > symbols() const
A range over the symbols in this InputFile.
Definition: LTO.h:155
const std::string & getTargetTriple() const
Get the target triple which is a string describing the target host.
Definition: Module.h:240
Represents either an error or a value T.
Definition: ErrorOr.h:56
void remove_filename(SmallVectorImpl< char > &path, Style style=Style::native)
Remove the last component from path unless it is the root dir.
Definition: Path.cpp:474
raw_ostream & errs()
This returns a reference to a raw_ostream for standard error.
void thinLTOResolvePrevailingInModule(Module &TheModule, const GVSummaryMapTy &DefinedGlobals)
Resolve prevailing symbol linkages in TheModule based on the information recorded in the summaries du...
std::string CPU
Definition: Config.h:38
LLVMContext & Context
CodeGenOpt::Level CGOptLevel
Definition: Config.h:43
PassManagerBuilder - This class is used to set up a standard optimization sequence for languages like...
BitcodeModule & getSingleBitcodeModule()
Definition: LTO.cpp:455
StringRef getBuffer() const
Definition: MemoryBuffer.h:63
std::vector< std::unique_ptr< GlobalValueSummary > > GlobalValueSummaryList
uint64_t GUID
Declare a type to represent a global unique identifier for a global value.
Definition: GlobalValue.h:502
LLVM_ATTRIBUTE_NORETURN void report_fatal_error(Error Err, bool gen_crash_diag=true)
Report a serious error, calling any installed error handler.
Definition: Error.cpp:139
This class represents lattice values for constants.
Definition: AllocatorList.h:23
void getDefaultSubtargetFeatures(const Triple &Triple)
Adds the default features for the specified target triple.
void print(const char *ProgName, raw_ostream &S, bool ShowColors=true, bool ShowKindLabel=true) const
Definition: SourceMgr.cpp:368
unsigned heavyweight_hardware_concurrency()
Get the amount of currency to use for tasks requiring significant memory or other resources...
Definition: Threading.cpp:62
cl::opt< bool > RemarksWithHotness("lto-pass-remarks-with-hotness", cl::desc("With PGO, include profile count in optimization remarks"), cl::Hidden)
A Module instance is used to store all the information related to an LLVM module. ...
Definition: Module.h:65
TargetMachine * createTargetMachine(StringRef TT, StringRef CPU, StringRef Features, const TargetOptions &Options, Optional< Reloc::Model > RM, Optional< CodeModel::Model > CM=None, CodeGenOpt::Level OL=CodeGenOpt::Default, bool JIT=false) const
createTargetMachine - Create a target specific machine implementation for the specified Triple...
std::error_code createUniqueFile(const Twine &Model, int &ResultFD, SmallVectorImpl< char > &ResultPath, unsigned Mode=all_read|all_write)
Create a uniquely named file.
Definition: Path.cpp:767
amdgpu Simplify well known AMD library false FunctionCallee Value const Twine & Name
This is the interface to build a ModuleSummaryIndex for a module.
std::error_code remove(const Twine &path, bool IgnoreNonExisting=true)
Remove path.
This file provides a bitcode writing pass.
Implements a dense probed hash-table based set.
Definition: DenseSet.h:249
void populateThinLTOPassManager(legacy::PassManagerBase &PM)
std::error_code EmitImportsFiles(StringRef ModulePath, StringRef OutputFilename, const std::map< std::string, GVSummaryMapTy > &ModuleToSummariesForIndex)
Emit into OutputFilename the files module ModulePath will import from.
Analysis providing profile information.
void enableDebugTypeODRUniquing()
void disableAllFunctions()
Disables all builtins.
ImmutablePass * createTargetTransformInfoWrapperPass(TargetIRAnalysis TIRA)
Create an analysis pass wrapper around a TTI object.
std::string getString() const
Returns features as a string.
iterator find(StringRef Key)
Definition: StringMap.h:332
const StringMap< std::pair< uint64_t, ModuleHash > > & modulePaths() const
Table of modules, containing module hash and id.
A raw_ostream that writes to an SmallVector or SmallString.
Definition: raw_ostream.h:530
bool is_directory(const basic_file_status &status)
Does status represent a directory?
Definition: Path.cpp:1040
Implementation of the target library information.
bool all_of(R &&range, UnaryPredicate P)
Provide wrappers to std::all_of which take ranges instead of having to pass begin/end explicitly...
Definition: STLExtras.h:1177
StringRef getName() const
Get a short "name" for the module.
Definition: Module.h:227
static Expected< std::unique_ptr< InputFile > > create(MemoryBufferRef Object)
Create an InputFile.
Definition: LTO.cpp:422
Error takeError()
Take ownership of the stored error.
Definition: Error.h:552
static const Target * lookupTarget(const std::string &Triple, std::string &Error)
lookupTarget - Lookup a target based on a target triple.
void addModule(StringRef Identifier, StringRef Data)
Add given module to the code generator.
Base class for error info classes.
Definition: Error.h:48
std::shared_future< void > async(Function &&F, Args &&... ArgList)
Asynchronous submission of a task to the pool.
Definition: ThreadPool.h:54
Pass * Inliner
Inliner - Specifies the inliner to use.
void setDiscardValueNames(bool Discard)
Set the Context runtime configuration to discard all value name (but GlobalValue).
ModuleSummaryIndex buildModuleSummaryIndex(const Module &M, std::function< BlockFrequencyInfo *(const Function &F)> GetBFICallback, ProfileSummaryInfo *PSI)
Direct function to compute a ModuleSummaryIndex from a given module.
virtual bool addPassesToEmitFile(PassManagerBase &, raw_pwrite_stream &, raw_pwrite_stream *, CodeGenFileType, bool=true, MachineModuleInfo *MMI=nullptr)
Add passes to the specified pass manager to get the specified file emitted.
void crossModuleImport(Module &Module, ModuleSummaryIndex &Index, const lto::InputFile &File)
Perform cross-module importing for the module identified by ModuleIdentifier.
cl::opt< std::string > RemarksPasses("lto-pass-remarks-filter", cl::desc("Only record optimization remarks from passes whose " "names match the given regular expression"), cl::value_desc("regex"))
void append(SmallVectorImpl< char > &path, const Twine &a, const Twine &b="", const Twine &c="", const Twine &d="")
Append to path.
Definition: Path.cpp:455
std::string toString(Error E)
Write all error messages (if any) in E to a string.
Definition: Error.h:966
void add(Pass *P) override
Add a pass to the queue of passes to run.
DiagnosticSeverity
Defines the different supported severity of a diagnostic.
unsigned size() const
Definition: StringMap.h:111
Twine - A lightweight data structure for efficiently representing the concatenation of temporary valu...
Definition: Twine.h:80
LLVM_NODISCARD StringRef drop_front(size_t N=1) const
Return a StringRef equal to &#39;this&#39; but with the first N elements dropped.
Definition: StringRef.h:620
std::vector< std::string > MAttrs
Definition: Config.h:40
LLVMContext & getContext() const
Get the global data context.
Definition: Module.h:244
static ErrorOr< std::unique_ptr< MemoryBuffer > > getOpenFile(sys::fs::file_t FD, const Twine &Filename, uint64_t FileSize, bool RequiresNullTerminator=true, bool IsVolatile=false)
Given an already-open file descriptor, read the file and return a MemoryBuffer.
void emitImports(Module &Module, StringRef OutputName, ModuleSummaryIndex &Index, const lto::InputFile &File)
Compute and emit the imported files for module at ModulePath.
std::unordered_set< GlobalValue::GUID > ExportSetTy
The set contains an entry for every global value the module exports.
Tagged union holding either a T or a Error.
Definition: yaml2obj.h:21
Interface for custom diagnostic printing.
This header defines classes/functions to handle pass execution timing information with interfaces for...
LLVM_NODISCARD bool empty() const
empty - Check if the string is empty.
Definition: StringRef.h:126
std::string writeGeneratedObject(int count, StringRef CacheEntryPath, const MemoryBuffer &OutputBuffer)
Write temporary object file to SavedObjectDirectoryPath, write symlink to Cache directory if needed...
StringRef str() const
Explicit conversion to StringRef.
Definition: SmallString.h:266
void run()
Process all the modules that were added to the code generator in parallel.
Key
PAL metadata keys.
ArchType getArch() const
getArch - Get the parsed architecture type of this triple.
Definition: Triple.h:296
Pass * createObjCARCContractPass()
unsigned OptLevel
The Optimization Level - Specify the basic optimization level.
bool StripDebugInfo(Module &M)
Strip debug info in the module if it exists.
Definition: DebugInfo.cpp:350
TargetOptions Options
Definition: Config.h:39
LLVM_NODISCARD size_t size() const
size - Get the string size.
Definition: StringRef.h:130
An input file.
Definition: LTO.h:102
A ThreadPool for asynchronous parallel execution on a defined number of threads.
Definition: ThreadPool.h:36
void reportAndResetTimings(raw_ostream *OutStream=nullptr)
If -time-passes has been specified, report the timings immediately and then reset the timers to zero...
Class to hold module path string table and global value map, and encapsulate methods for operating on...
auto count(R &&Range, const E &Element) -> typename std::iterator_traits< decltype(adl_begin(Range))>::difference_type
Wrapper function around std::count to count the number of times an element Element occurs in the give...
Definition: STLExtras.h:1243
PassManager manages ModulePassManagers.
std::error_code copy_file(const Twine &From, const Twine &To)
Copy the contents of From to To.
Definition: Path.cpp:964
void crossReferenceSymbol(StringRef Name)
Adds to a list of all global symbols that are cross-referenced between ThinLTO files.
initializer< Ty > init(const Ty &Val)
Definition: CommandLine.h:432
This is the base abstract class for diagnostic reporting in the backend.
* if(!EatIfPresent(lltok::kw_thread_local)) return false
ParseOptionalThreadLocal := /*empty.
bool renameModuleForThinLTO(Module &M, const ModuleSummaryIndex &Index, SetVector< GlobalValue *> *GlobalsToImport=nullptr)
Perform in-place global value handling on the given Module for exported local functions renamed and p...
void gatherImportedSummariesForModule(StringRef ModulePath, const StringMap< GVSummaryMapTy > &ModuleToDefinedGVSummaries, const FunctionImporter::ImportMapTy &ImportList, std::map< std::string, GVSummaryMapTy > &ModuleToSummariesForIndex)
Compute the set of summaries needed for a ThinLTO backend compilation of ModulePath.
void optimize(Module &Module)
Perform post-importing ThinLTO optimizations.
size_type count(StringRef Key) const
count - Return 1 if the element is in the map, 0 otherwise.
Definition: StringMap.h:358
This is an important class for using LLVM in a threaded context.
Definition: LLVMContext.h:64
Instrumentation for Order File
TargetLibraryInfoImpl * LibraryInfo
LibraryInfo - Specifies information about the runtime library for the optimizer.
Helper to gather options relevant to the target machine creation.
void internalize(Module &Module, ModuleSummaryIndex &Index, const lto::InputFile &File)
Perform internalization.
TargetIRAnalysis getTargetIRAnalysis()
Get a TargetIRAnalysis appropriate for the target.
unsigned OptLevel
Definition: Config.h:45
void WriteBitcodeToFile(const Module &M, raw_ostream &Out, bool ShouldPreserveUseListOrder=false, const ModuleSummaryIndex *Index=nullptr, bool GenerateHash=false, ModuleHash *ModHash=nullptr)
Write the specified module to the specified raw output stream.
bool isWeakForLinker() const
Definition: GlobalValue.h:466
LTO configuration.
Definition: Config.h:35
const Triple & getTargetTriple() const
static void print(raw_ostream &Out, object::Archive::Kind Kind, T Val)
const ModuleHash & getModuleHash(const StringRef ModPath) const
Get the module SHA1 hash recorded for the given module path.
void thinLTOResolvePrevailingInIndex(ModuleSummaryIndex &Index, function_ref< bool(GlobalValue::GUID, const GlobalValueSummary *)> isPrevailing, function_ref< void(StringRef, GlobalValue::GUID, GlobalValue::LinkageTypes)> recordNewLinkage, const DenseSet< GlobalValue::GUID > &GUIDPreservedSymbols)
Resolve linkage for prevailing symbols in the Index.
Definition: LTO.cpp:355
auto find_if(R &&Range, UnaryPredicate P) -> decltype(adl_begin(Range))
Provide wrappers to std::find_if which take ranges instead of having to pass begin/end explicitly...
Definition: STLExtras.h:1205
std::error_code create_hard_link(const Twine &to, const Twine &from)
Create a hard link from from to to, or return an error.
bool isOSBinFormatMachO() const
Tests whether the environment is MachO.
Definition: Triple.h:633
Function and variable summary information to aid decisions and implementation of importing.
static void internalizeAndPromoteInIndex(const StringMap< FunctionImporter::ExportSetTy > &ExportLists, const DenseSet< GlobalValue::GUID > &GUIDPreservedSymbols, ModuleSummaryIndex &Index)
static void write(bool isBE, void *P, T V)
void gatherImportedSummariesForModule(Module &Module, ModuleSummaryIndex &Index, std::map< std::string, GVSummaryMapTy > &ModuleToSummariesForIndex, const lto::InputFile &File)
Compute the list of summaries needed for importing into module.
bool pruneCache(StringRef Path, CachePruningPolicy Policy)
Peform pruning using the supplied policy, returns true if pruning occurred, i.e.
bool run(Module &M)
run - Execute all of the passes scheduled for execution.
void logAllUnhandledErrors(Error E, raw_ostream &OS, Twine ErrorBanner={})
Log all errors (if any) in E to OS.
Definition: Error.cpp:61
const std::string & getModuleIdentifier() const
Get the module identifier which is, essentially, the name of the module.
Definition: Module.h:210
Force files Atime to be updated on access. Only makes a difference on windows.
Definition: FileSystem.h:782
Triple - Helper class for working with autoconf configuration names.
Definition: Triple.h:43
void sort(IteratorTy Start, IteratorTy End)
Definition: STLExtras.h:1107
void computeSyntheticCounts(ModuleSummaryIndex &Index)
Compute synthetic function entry counts.
std::error_code rename(const Twine &from, const Twine &to)
Rename from to to.
void handleAllErrors(Error E, HandlerTs &&... Handlers)
Behaves the same as handleErrors, except that by contract all errors must be handled by the given han...
Definition: Error.h:904
void computeLTOCacheKey(SmallString< 40 > &Key, const lto::Config &Conf, const ModuleSummaryIndex &Index, StringRef ModuleID, const FunctionImporter::ImportMapTy &ImportList, const FunctionImporter::ExportSetTy &ExportList, const std::map< GlobalValue::GUID, GlobalValue::LinkageTypes > &ResolvedODR, const GVSummaryMapTy &DefinedGlobals, const std::set< GlobalValue::GUID > &CfiFunctionDefs={}, const std::set< GlobalValue::GUID > &CfiFunctionDecls={})
Computes a unique hash for the Module considering the current list of export/import and other global ...
Definition: LTO.cpp:71
This is a &#39;vector&#39; (really, a variable-sized array), optimized for the case when the array is small...
Definition: SmallVector.h:837
const DataFlowGraph & G
Definition: RDFGraph.cpp:202
static bool isAvailableExternallyLinkage(LinkageTypes Linkage)
Definition: GlobalValue.h:303
Optional< Reloc::Model > RelocModel
Definition: Config.h:41
Expected< file_t > openNativeFileForRead(const Twine &Name, OpenFlags Flags=OF_None, SmallVectorImpl< char > *RealPath=nullptr)
Opens the file with the given name in a read-only mode, returning its open file descriptor.
reference get()
Returns a reference to the stored T value.
Definition: Error.h:532
std::error_code closeFile(file_t &F)
Close the file object.
This interface provides simple read-only access to a block of memory, and provides simple methods for...
Definition: MemoryBuffer.h:41
raw_ostream & dbgs()
dbgs() - This returns a reference to a raw_ostream for debugging messages.
Definition: Debug.cpp:132
The access may modify the value stored in memory.
StringMap - This is an unconventional map that is specialized for handling keys that are "strings"...
Definition: StringMap.h:219
Target - Wrapper for Target specific information.
Manages the enabling and disabling of subtarget specific features.
void promote(Module &Module, ModuleSummaryIndex &Index, const lto::InputFile &File)
Perform promotion and renaming of exported internal functions, and additionally resolve weak and link...
LinkageTypes
An enumeration for the kinds of linkage for global values.
Definition: GlobalValue.h:47
void WriteIndexToFile(const ModuleSummaryIndex &Index, raw_ostream &Out, const std::map< std::string, GVSummaryMapTy > *ModuleToSummariesForIndex=nullptr)
Write the specified module summary index to the given raw output stream, where it will be written in ...
std::unique_ptr< ModuleSummaryIndex > linkCombinedIndex()
Produce the combined summary index from all the bitcode files: "thin-link".
GUID getGUID() const
Return a 64-bit global unique ID constructed from global value name (i.e.
Definition: GlobalValue.h:510
static void computeDeadSymbolsInIndex(ModuleSummaryIndex &Index, const DenseSet< GlobalValue::GUID > &GUIDPreservedSymbols)
bool verifyModule(const Module &M, raw_ostream *OS=nullptr, bool *BrokenDebugInfo=nullptr)
Check a module for errors.
Definition: Verifier.cpp:5035
Expected< bool > importFunctions(Module &M, const ImportMapTy &ImportList)
Import functions in Module M based on the supplied import list.
std::unique_ptr< TargetMachine > create() const
void ComputeCrossModuleImport(const ModuleSummaryIndex &Index, const StringMap< GVSummaryMapTy > &ModuleToDefinedGVSummaries, StringMap< FunctionImporter::ImportMapTy > &ImportLists, StringMap< FunctionImporter::ExportSetTy > &ExportLists)
Compute all the imports and exports for every module in the Index.
cl::opt< bool > LTODiscardValueNames("lto-discard-value-names", cl::desc("Strip names from Value during LTO (other than GlobalValue)."), cl::init(false), cl::Hidden)
A raw_ostream that writes to a file descriptor.
Definition: raw_ostream.h:384
const char * c_str()
Definition: SmallString.h:269
size_t size() const
Definition: Module.h:606
#define I(x, y, z)
Definition: MD5.cpp:58
void thinLTOInternalizeModule(Module &TheModule, const GVSummaryMapTy &DefinedGlobals)
Internalize TheModule based on the information recorded in the summaries during global summary-based ...
size_type count(const_arg_type_t< ValueT > V) const
Return 1 if the specified key is in the set, 0 otherwise.
Definition: DenseSet.h:91
void preserveSymbol(StringRef Name)
Adds to a list of all global symbols that must exist in the final generated code. ...
void diagnose(const DiagnosticInfo &DI)
Report a message to the currently installed diagnostic handler.
void computeDeadSymbolsWithConstProp(ModuleSummaryIndex &Index, const DenseSet< GlobalValue::GUID > &GUIDPreservedSymbols, function_ref< PrevailingType(GlobalValue::GUID)> isPrevailing, bool ImportEnabled)
Compute dead symbols and run constant propagation in combined index after that.
void PrintStatistics()
Print statistics to the file returned by CreateInfoOutputFile().
Definition: Statistic.cpp:227
Optional< Reloc::Model > RelocModel
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
void collectDefinedGVSummariesPerModule(Map &ModuleToDefinedGVSummaries) const
Collect for each module the list of Summaries it defines (GUID -> Summary).
Expected< std::unique_ptr< ToolOutputFile > > setupOptimizationRemarks(LLVMContext &Context, StringRef RemarksFilename, StringRef RemarksPasses, StringRef RemarksFormat, bool RemarksWithHotness, int Count=-1)
Setup optimization remarks.
Definition: LTO.cpp:1358
cl::opt< std::string > RemarksFormat("lto-pass-remarks-format", cl::desc("The format used for serializing remarks (default: YAML)"), cl::value_desc("format"), cl::init("yaml"))
Pass * createFunctionInliningPass()
createFunctionInliningPass - Return a new pass object that uses a heuristic to inline direct function...
The function importer is automatically importing function from other modules based on the provided su...
cl::opt< std::string > RemarksFilename("lto-pass-remarks-output", cl::desc("Output filename for pass remarks"), cl::value_desc("filename"))
Lightweight error class with error context and mandatory checking.
Definition: Error.h:157
StringSet - A wrapper for StringMap that provides set-like functionality.
Definition: StringSet.h:27
Primary interface to the complete machine description for the target machine.
Definition: TargetMachine.h:65
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:48
bool Freestanding
Flag to indicate that the optimizer should not assume builtins are present on the target...
Definition: Config.h:53
This pass exposes codegen information to IR-level passes.
#define LLVM_DEBUG(X)
Definition: Debug.h:122
bool exists(const basic_file_status &status)
Does file exist?
Definition: Path.cpp:1025
bool AreStatisticsEnabled()
Check if statistics are enabled.
Definition: Statistic.cpp:133
std::error_code errorToErrorCode(Error Err)
Helper for converting an ECError to a std::error_code.
Definition: Error.cpp:93
iterator end()
Definition: StringMap.h:317
void thinLTOInternalizeAndPromoteInIndex(ModuleSummaryIndex &Index, function_ref< bool(StringRef, GlobalValue::GUID)> isExported)
Update the linkages in the given Index to mark exported values as external and non-exported values as...
Definition: LTO.cpp:412
Instances of this class encapsulate one diagnostic report, allowing printing to a raw_ostream as a ca...
Definition: SourceMgr.h:261