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