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
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::F_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 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  int FD;
353  SmallString<64> ResultPath;
354  std::error_code EC = sys::fs::openFileForRead(
355  Twine(EntryPath), FD, sys::fs::OF_UpdateAtime, &ResultPath);
356  if (EC)
357  return EC;
359  MemoryBuffer::getOpenFile(FD, EntryPath,
360  /*FileSize*/ -1,
361  /*RequiresNullTerminator*/ false);
362  close(FD);
363  return MBOrErr;
364  }
365 
366  // Cache the Produced object file
367  void write(const MemoryBuffer &OutputBuffer) {
368  if (EntryPath.empty())
369  return;
370 
371  // Write to a temporary to avoid race condition
372  SmallString<128> TempFilename;
373  SmallString<128> CachePath(EntryPath);
374  int TempFD;
376  sys::path::append(TempFilename, CachePath, "Thin-%%%%%%.tmp.o");
377  std::error_code EC =
378  sys::fs::createUniqueFile(TempFilename, TempFD, TempFilename);
379  if (EC) {
380  errs() << "Error: " << EC.message() << "\n";
381  report_fatal_error("ThinLTO: Can't get a temporary file");
382  }
383  {
384  raw_fd_ostream OS(TempFD, /* ShouldClose */ true);
385  OS << OutputBuffer.getBuffer();
386  }
387  // Rename temp file to final destination; rename is atomic
388  EC = sys::fs::rename(TempFilename, EntryPath);
389  if (EC)
390  sys::fs::remove(TempFilename);
391  }
392 };
393 
394 static std::unique_ptr<MemoryBuffer>
395 ProcessThinLTOModule(Module &TheModule, ModuleSummaryIndex &Index,
397  const FunctionImporter::ImportMapTy &ImportList,
398  const FunctionImporter::ExportSetTy &ExportList,
399  const DenseSet<GlobalValue::GUID> &GUIDPreservedSymbols,
400  const GVSummaryMapTy &DefinedGlobals,
401  const ThinLTOCodeGenerator::CachingOptions &CacheOptions,
402  bool DisableCodeGen, StringRef SaveTempsDir,
403  bool Freestanding, unsigned OptLevel, unsigned count) {
404 
405  // "Benchmark"-like optimization: single-source case
406  bool SingleModule = (ModuleMap.size() == 1);
407 
408  if (!SingleModule) {
409  promoteModule(TheModule, Index);
410 
411  // Apply summary-based prevailing-symbol resolution decisions.
412  thinLTOResolvePrevailingInModule(TheModule, DefinedGlobals);
413 
414  // Save temps: after promotion.
415  saveTempBitcode(TheModule, SaveTempsDir, count, ".1.promoted.bc");
416  }
417 
418  // Be friendly and don't nuke totally the module when the client didn't
419  // supply anything to preserve.
420  if (!ExportList.empty() || !GUIDPreservedSymbols.empty()) {
421  // Apply summary-based internalization decisions.
422  thinLTOInternalizeModule(TheModule, DefinedGlobals);
423  }
424 
425  // Save internalized bitcode
426  saveTempBitcode(TheModule, SaveTempsDir, count, ".2.internalized.bc");
427 
428  if (!SingleModule) {
429  crossImportIntoModule(TheModule, Index, ModuleMap, ImportList);
430 
431  // Save temps: after cross-module import.
432  saveTempBitcode(TheModule, SaveTempsDir, count, ".3.imported.bc");
433  }
434 
435  optimizeModule(TheModule, TM, OptLevel, Freestanding);
436 
437  saveTempBitcode(TheModule, SaveTempsDir, count, ".4.opt.bc");
438 
439  if (DisableCodeGen) {
440  // Configured to stop before CodeGen, serialize the bitcode and return.
441  SmallVector<char, 128> OutputBuffer;
442  {
443  raw_svector_ostream OS(OutputBuffer);
444  ProfileSummaryInfo PSI(TheModule);
445  auto Index = buildModuleSummaryIndex(TheModule, nullptr, &PSI);
446  WriteBitcodeToFile(TheModule, OS, true, &Index);
447  }
448  return make_unique<SmallVectorMemoryBuffer>(std::move(OutputBuffer));
449  }
450 
451  return codegenModule(TheModule, TM);
452 }
453 
454 /// Resolve prevailing symbols. Record resolutions in the \p ResolvedODR map
455 /// for caching, and in the \p Index for application during the ThinLTO
456 /// backends. This is needed for correctness for exported symbols (ensure
457 /// at least one copy kept) and a compile-time optimization (to drop duplicate
458 /// copies when possible).
459 static void resolvePrevailingInIndex(
460  ModuleSummaryIndex &Index,
461  StringMap<std::map<GlobalValue::GUID, GlobalValue::LinkageTypes>>
462  &ResolvedODR,
463  const DenseSet<GlobalValue::GUID> &GUIDPreservedSymbols) {
464 
466  computePrevailingCopies(Index, PrevailingCopy);
467 
468  auto isPrevailing = [&](GlobalValue::GUID GUID, const GlobalValueSummary *S) {
469  const auto &Prevailing = PrevailingCopy.find(GUID);
470  // Not in map means that there was only one copy, which must be prevailing.
471  if (Prevailing == PrevailingCopy.end())
472  return true;
473  return Prevailing->second == S;
474  };
475 
476  auto recordNewLinkage = [&](StringRef ModuleIdentifier,
477  GlobalValue::GUID GUID,
478  GlobalValue::LinkageTypes NewLinkage) {
479  ResolvedODR[ModuleIdentifier][GUID] = NewLinkage;
480  };
481 
482  thinLTOResolvePrevailingInIndex(Index, isPrevailing, recordNewLinkage,
483  GUIDPreservedSymbols);
484 }
485 
486 // Initialize the TargetMachine builder for a given Triple
487 static void initTMBuilder(TargetMachineBuilder &TMBuilder,
488  const Triple &TheTriple) {
489  // Set a default CPU for Darwin triples (copied from LTOCodeGenerator).
490  // FIXME this looks pretty terrible...
491  if (TMBuilder.MCpu.empty() && TheTriple.isOSDarwin()) {
492  if (TheTriple.getArch() == llvm::Triple::x86_64)
493  TMBuilder.MCpu = "core2";
494  else if (TheTriple.getArch() == llvm::Triple::x86)
495  TMBuilder.MCpu = "yonah";
496  else if (TheTriple.getArch() == llvm::Triple::aarch64)
497  TMBuilder.MCpu = "cyclone";
498  }
499  TMBuilder.TheTriple = std::move(TheTriple);
500 }
501 
502 } // end anonymous namespace
503 
505  MemoryBufferRef Buffer(Data, Identifier);
506 
507  auto InputOrError = lto::InputFile::create(Buffer);
508  if (!InputOrError)
509  report_fatal_error("ThinLTO cannot create input file: " +
510  toString(InputOrError.takeError()));
511 
512  auto TripleStr = (*InputOrError)->getTargetTriple();
513  Triple TheTriple(TripleStr);
514 
515  if (Modules.empty())
516  initTMBuilder(TMBuilder, Triple(TheTriple));
517  else if (TMBuilder.TheTriple != TheTriple) {
518  if (!TMBuilder.TheTriple.isCompatibleWith(TheTriple))
519  report_fatal_error("ThinLTO modules with incompatible triples not "
520  "supported");
521  initTMBuilder(TMBuilder, Triple(TMBuilder.TheTriple.merge(TheTriple)));
522  }
523 
524  Modules.emplace_back(std::move(*InputOrError));
525 }
526 
528  PreservedSymbols.insert(Name);
529 }
530 
532  // FIXME: At the moment, we don't take advantage of this extra information,
533  // we're conservatively considering cross-references as preserved.
534  // CrossReferencedSymbols.insert(Name);
535  PreservedSymbols.insert(Name);
536 }
537 
538 // TargetMachine factory
539 std::unique_ptr<TargetMachine> TargetMachineBuilder::create() const {
540  std::string ErrMsg;
541  const Target *TheTarget =
542  TargetRegistry::lookupTarget(TheTriple.str(), ErrMsg);
543  if (!TheTarget) {
544  report_fatal_error("Can't load target for this Triple: " + ErrMsg);
545  }
546 
547  // Use MAttr as the default set of features.
548  SubtargetFeatures Features(MAttr);
549  Features.getDefaultSubtargetFeatures(TheTriple);
550  std::string FeatureStr = Features.getString();
551 
552  return std::unique_ptr<TargetMachine>(
553  TheTarget->createTargetMachine(TheTriple.str(), MCpu, FeatureStr, Options,
554  RelocModel, None, CGOptLevel));
555 }
556 
557 /**
558  * Produce the combined summary index from all the bitcode files:
559  * "thin-link".
560  */
561 std::unique_ptr<ModuleSummaryIndex> ThinLTOCodeGenerator::linkCombinedIndex() {
562  std::unique_ptr<ModuleSummaryIndex> CombinedIndex =
563  llvm::make_unique<ModuleSummaryIndex>(/*HaveGVs=*/false);
564  uint64_t NextModuleId = 0;
565  for (auto &Mod : Modules) {
566  auto &M = Mod->getSingleBitcodeModule();
567  if (Error Err =
568  M.readSummary(*CombinedIndex, Mod->getName(), NextModuleId++)) {
569  // FIXME diagnose
571  std::move(Err), errs(),
572  "error: can't create module summary index for buffer: ");
573  return nullptr;
574  }
575  }
576  return CombinedIndex;
577 }
578 
580  const StringMap<FunctionImporter::ExportSetTy> &ExportLists,
581  const DenseSet<GlobalValue::GUID> &GUIDPreservedSymbols,
583  auto isExported = [&](StringRef ModuleIdentifier, GlobalValue::GUID GUID) {
584  const auto &ExportList = ExportLists.find(ModuleIdentifier);
585  return (ExportList != ExportLists.end() &&
586  ExportList->second.count(GUID)) ||
587  GUIDPreservedSymbols.count(GUID);
588  };
589 
590  thinLTOInternalizeAndPromoteInIndex(Index, isExported);
591 }
592 
595  const DenseSet<GlobalValue::GUID> &GUIDPreservedSymbols) {
596  // We have no symbols resolution available. And can't do any better now in the
597  // case where the prevailing symbol is in a native object. It can be refined
598  // with linker information in the future.
599  auto isPrevailing = [&](GlobalValue::GUID G) {
601  };
602  computeDeadSymbolsWithConstProp(Index, GUIDPreservedSymbols, isPrevailing,
603  /* ImportEnabled = */ true);
604 }
605 
606 /**
607  * Perform promotion and renaming of exported internal functions.
608  * Index is updated to reflect linkage changes from weak resolution.
609  */
611  const lto::InputFile &File) {
612  auto ModuleCount = Index.modulePaths().size();
613  auto ModuleIdentifier = TheModule.getModuleIdentifier();
614 
615  // Collect for each module the list of function it defines (GUID -> Summary).
616  StringMap<GVSummaryMapTy> ModuleToDefinedGVSummaries;
617  Index.collectDefinedGVSummariesPerModule(ModuleToDefinedGVSummaries);
618 
619  // Convert the preserved symbols set from string to GUID
620  auto GUIDPreservedSymbols = computeGUIDPreservedSymbols(
621  PreservedSymbols, Triple(TheModule.getTargetTriple()));
622 
623  // Add used symbol to the preserved symbols.
624  addUsedSymbolToPreservedGUID(File, GUIDPreservedSymbols);
625 
626  // Compute "dead" symbols, we don't want to import/export these!
627  computeDeadSymbolsInIndex(Index, GUIDPreservedSymbols);
628 
629  // Generate import/export list
630  StringMap<FunctionImporter::ImportMapTy> ImportLists(ModuleCount);
631  StringMap<FunctionImporter::ExportSetTy> ExportLists(ModuleCount);
632  ComputeCrossModuleImport(Index, ModuleToDefinedGVSummaries, ImportLists,
633  ExportLists);
634 
635  // Resolve prevailing symbols
637  resolvePrevailingInIndex(Index, ResolvedODR, GUIDPreservedSymbols);
638 
640  TheModule, ModuleToDefinedGVSummaries[ModuleIdentifier]);
641 
642  // Promote the exported values in the index, so that they are promoted
643  // in the module.
644  internalizeAndPromoteInIndex(ExportLists, GUIDPreservedSymbols, Index);
645 
646  promoteModule(TheModule, Index);
647 }
648 
649 /**
650  * Perform cross-module importing for the module identified by ModuleIdentifier.
651  */
654  const lto::InputFile &File) {
655  auto ModuleMap = generateModuleMap(Modules);
656  auto ModuleCount = Index.modulePaths().size();
657 
658  // Collect for each module the list of function it defines (GUID -> Summary).
659  StringMap<GVSummaryMapTy> ModuleToDefinedGVSummaries(ModuleCount);
660  Index.collectDefinedGVSummariesPerModule(ModuleToDefinedGVSummaries);
661 
662  // Convert the preserved symbols set from string to GUID
663  auto GUIDPreservedSymbols = computeGUIDPreservedSymbols(
664  PreservedSymbols, Triple(TheModule.getTargetTriple()));
665 
666  addUsedSymbolToPreservedGUID(File, GUIDPreservedSymbols);
667 
668  // Compute "dead" symbols, we don't want to import/export these!
669  computeDeadSymbolsInIndex(Index, GUIDPreservedSymbols);
670 
671  // Generate import/export list
672  StringMap<FunctionImporter::ImportMapTy> ImportLists(ModuleCount);
673  StringMap<FunctionImporter::ExportSetTy> ExportLists(ModuleCount);
674  ComputeCrossModuleImport(Index, ModuleToDefinedGVSummaries, ImportLists,
675  ExportLists);
676  auto &ImportList = ImportLists[TheModule.getModuleIdentifier()];
677 
678  crossImportIntoModule(TheModule, Index, ModuleMap, ImportList);
679 }
680 
681 /**
682  * Compute the list of summaries needed for importing into module.
683  */
685  Module &TheModule, ModuleSummaryIndex &Index,
686  std::map<std::string, GVSummaryMapTy> &ModuleToSummariesForIndex,
687  const lto::InputFile &File) {
688  auto ModuleCount = Index.modulePaths().size();
689  auto ModuleIdentifier = TheModule.getModuleIdentifier();
690 
691  // Collect for each module the list of function it defines (GUID -> Summary).
692  StringMap<GVSummaryMapTy> ModuleToDefinedGVSummaries(ModuleCount);
693  Index.collectDefinedGVSummariesPerModule(ModuleToDefinedGVSummaries);
694 
695  // Convert the preserved symbols set from string to GUID
696  auto GUIDPreservedSymbols = computeGUIDPreservedSymbols(
697  PreservedSymbols, Triple(TheModule.getTargetTriple()));
698 
699  addUsedSymbolToPreservedGUID(File, GUIDPreservedSymbols);
700 
701  // Compute "dead" symbols, we don't want to import/export these!
702  computeDeadSymbolsInIndex(Index, GUIDPreservedSymbols);
703 
704  // Generate import/export list
705  StringMap<FunctionImporter::ImportMapTy> ImportLists(ModuleCount);
706  StringMap<FunctionImporter::ExportSetTy> ExportLists(ModuleCount);
707  ComputeCrossModuleImport(Index, ModuleToDefinedGVSummaries, ImportLists,
708  ExportLists);
709 
711  ModuleIdentifier, ModuleToDefinedGVSummaries,
712  ImportLists[ModuleIdentifier], ModuleToSummariesForIndex);
713 }
714 
715 /**
716  * Emit the list of files needed for importing into module.
717  */
720  const lto::InputFile &File) {
721  auto ModuleCount = Index.modulePaths().size();
722  auto ModuleIdentifier = TheModule.getModuleIdentifier();
723 
724  // Collect for each module the list of function it defines (GUID -> Summary).
725  StringMap<GVSummaryMapTy> ModuleToDefinedGVSummaries(ModuleCount);
726  Index.collectDefinedGVSummariesPerModule(ModuleToDefinedGVSummaries);
727 
728  // Convert the preserved symbols set from string to GUID
729  auto GUIDPreservedSymbols = computeGUIDPreservedSymbols(
730  PreservedSymbols, Triple(TheModule.getTargetTriple()));
731 
732  addUsedSymbolToPreservedGUID(File, GUIDPreservedSymbols);
733 
734  // Compute "dead" symbols, we don't want to import/export these!
735  computeDeadSymbolsInIndex(Index, GUIDPreservedSymbols);
736 
737  // Generate import/export list
738  StringMap<FunctionImporter::ImportMapTy> ImportLists(ModuleCount);
739  StringMap<FunctionImporter::ExportSetTy> ExportLists(ModuleCount);
740  ComputeCrossModuleImport(Index, ModuleToDefinedGVSummaries, ImportLists,
741  ExportLists);
742 
743  std::map<std::string, GVSummaryMapTy> ModuleToSummariesForIndex;
745  ModuleIdentifier, ModuleToDefinedGVSummaries,
746  ImportLists[ModuleIdentifier], ModuleToSummariesForIndex);
747 
748  std::error_code EC;
749  if ((EC = EmitImportsFiles(ModuleIdentifier, OutputName,
750  ModuleToSummariesForIndex)))
751  report_fatal_error(Twine("Failed to open ") + OutputName +
752  " to save imports lists\n");
753 }
754 
755 /**
756  * Perform internalization. Runs promote and internalization together.
757  * Index is updated to reflect linkage changes.
758  */
761  const lto::InputFile &File) {
762  initTMBuilder(TMBuilder, Triple(TheModule.getTargetTriple()));
763  auto ModuleCount = Index.modulePaths().size();
764  auto ModuleIdentifier = TheModule.getModuleIdentifier();
765 
766  // Convert the preserved symbols set from string to GUID
767  auto GUIDPreservedSymbols =
768  computeGUIDPreservedSymbols(PreservedSymbols, TMBuilder.TheTriple);
769 
770  addUsedSymbolToPreservedGUID(File, GUIDPreservedSymbols);
771 
772  // Collect for each module the list of function it defines (GUID -> Summary).
773  StringMap<GVSummaryMapTy> ModuleToDefinedGVSummaries(ModuleCount);
774  Index.collectDefinedGVSummariesPerModule(ModuleToDefinedGVSummaries);
775 
776  // Compute "dead" symbols, we don't want to import/export these!
777  computeDeadSymbolsInIndex(Index, GUIDPreservedSymbols);
778 
779  // Generate import/export list
780  StringMap<FunctionImporter::ImportMapTy> ImportLists(ModuleCount);
781  StringMap<FunctionImporter::ExportSetTy> ExportLists(ModuleCount);
782  ComputeCrossModuleImport(Index, ModuleToDefinedGVSummaries, ImportLists,
783  ExportLists);
784  auto &ExportList = ExportLists[ModuleIdentifier];
785 
786  // Be friendly and don't nuke totally the module when the client didn't
787  // supply anything to preserve.
788  if (ExportList.empty() && GUIDPreservedSymbols.empty())
789  return;
790 
791  // Resolve prevailing symbols
793  resolvePrevailingInIndex(Index, ResolvedODR, GUIDPreservedSymbols);
794 
795  // Promote the exported values in the index, so that they are promoted
796  // in the module.
797  internalizeAndPromoteInIndex(ExportLists, GUIDPreservedSymbols, Index);
798 
799  promoteModule(TheModule, Index);
800 
801  // Internalization
803  TheModule, ModuleToDefinedGVSummaries[ModuleIdentifier]);
804 
805  thinLTOInternalizeModule(TheModule,
806  ModuleToDefinedGVSummaries[ModuleIdentifier]);
807 }
808 
809 /**
810  * Perform post-importing ThinLTO optimizations.
811  */
813  initTMBuilder(TMBuilder, Triple(TheModule.getTargetTriple()));
814 
815  // Optimize now
816  optimizeModule(TheModule, *TMBuilder.create(), OptLevel, Freestanding);
817 }
818 
819 /// Write out the generated object file, either from CacheEntryPath or from
820 /// OutputBuffer, preferring hard-link when possible.
821 /// Returns the path to the generated file in SavedObjectsDirectoryPath.
822 std::string
824  const MemoryBuffer &OutputBuffer) {
825  auto ArchName = TMBuilder.TheTriple.getArchName();
826  SmallString<128> OutputPath(SavedObjectsDirectoryPath);
827  llvm::sys::path::append(OutputPath,
828  Twine(count) + "." + ArchName + ".thinlto.o");
829  OutputPath.c_str(); // Ensure the string is null terminated.
830  if (sys::fs::exists(OutputPath))
831  sys::fs::remove(OutputPath);
832 
833  // We don't return a memory buffer to the linker, just a list of files.
834  if (!CacheEntryPath.empty()) {
835  // Cache is enabled, hard-link the entry (or copy if hard-link fails).
836  auto Err = sys::fs::create_hard_link(CacheEntryPath, OutputPath);
837  if (!Err)
838  return OutputPath.str();
839  // Hard linking failed, try to copy.
840  Err = sys::fs::copy_file(CacheEntryPath, OutputPath);
841  if (!Err)
842  return OutputPath.str();
843  // Copy failed (could be because the CacheEntry was removed from the cache
844  // in the meantime by another process), fall back and try to write down the
845  // buffer to the output.
846  errs() << "error: can't link or copy from cached entry '" << CacheEntryPath
847  << "' to '" << OutputPath << "'\n";
848  }
849  // No cache entry, just write out the buffer.
850  std::error_code Err;
851  raw_fd_ostream OS(OutputPath, Err, sys::fs::F_None);
852  if (Err)
853  report_fatal_error("Can't open output '" + OutputPath + "'\n");
854  OS << OutputBuffer.getBuffer();
855  return OutputPath.str();
856 }
857 
858 // Main entry point for the ThinLTO processing
860  // Prepare the resulting object vector
861  assert(ProducedBinaries.empty() && "The generator should not be reused");
862  if (SavedObjectsDirectoryPath.empty())
863  ProducedBinaries.resize(Modules.size());
864  else {
865  sys::fs::create_directories(SavedObjectsDirectoryPath);
866  bool IsDir;
867  sys::fs::is_directory(SavedObjectsDirectoryPath, IsDir);
868  if (!IsDir)
869  report_fatal_error("Unexistent dir: '" + SavedObjectsDirectoryPath + "'");
870  ProducedBinaryFiles.resize(Modules.size());
871  }
872 
873  if (CodeGenOnly) {
874  // Perform only parallel codegen and return.
875  ThreadPool Pool;
876  int count = 0;
877  for (auto &Mod : Modules) {
878  Pool.async([&](int count) {
881 
882  // Parse module now
883  auto TheModule = loadModuleFromInput(Mod.get(), Context, false,
884  /*IsImporting*/ false);
885 
886  // CodeGen
887  auto OutputBuffer = codegenModule(*TheModule, *TMBuilder.create());
888  if (SavedObjectsDirectoryPath.empty())
889  ProducedBinaries[count] = std::move(OutputBuffer);
890  else
891  ProducedBinaryFiles[count] =
892  writeGeneratedObject(count, "", *OutputBuffer);
893  }, count++);
894  }
895 
896  return;
897  }
898 
899  // Sequential linking phase
900  auto Index = linkCombinedIndex();
901 
902  // Save temps: index.
903  if (!SaveTempsDir.empty()) {
904  auto SaveTempPath = SaveTempsDir + "index.bc";
905  std::error_code EC;
906  raw_fd_ostream OS(SaveTempPath, EC, sys::fs::F_None);
907  if (EC)
908  report_fatal_error(Twine("Failed to open ") + SaveTempPath +
909  " to save optimized bitcode\n");
910  WriteIndexToFile(*Index, OS);
911  }
912 
913 
914  // Prepare the module map.
915  auto ModuleMap = generateModuleMap(Modules);
916  auto ModuleCount = Modules.size();
917 
918  // Collect for each module the list of function it defines (GUID -> Summary).
919  StringMap<GVSummaryMapTy> ModuleToDefinedGVSummaries(ModuleCount);
920  Index->collectDefinedGVSummariesPerModule(ModuleToDefinedGVSummaries);
921 
922  // Convert the preserved symbols set from string to GUID, this is needed for
923  // computing the caching hash and the internalization.
924  auto GUIDPreservedSymbols =
925  computeGUIDPreservedSymbols(PreservedSymbols, TMBuilder.TheTriple);
926 
927  // Add used symbol from inputs to the preserved symbols.
928  for (const auto &M : Modules)
929  addUsedSymbolToPreservedGUID(*M, GUIDPreservedSymbols);
930 
931  // Compute "dead" symbols, we don't want to import/export these!
932  computeDeadSymbolsInIndex(*Index, GUIDPreservedSymbols);
933 
934  // Synthesize entry counts for functions in the combined index.
936 
937  // Collect the import/export lists for all modules from the call-graph in the
938  // combined index.
939  StringMap<FunctionImporter::ImportMapTy> ImportLists(ModuleCount);
940  StringMap<FunctionImporter::ExportSetTy> ExportLists(ModuleCount);
941  ComputeCrossModuleImport(*Index, ModuleToDefinedGVSummaries, ImportLists,
942  ExportLists);
943 
944  // We use a std::map here to be able to have a defined ordering when
945  // producing a hash for the cache entry.
946  // FIXME: we should be able to compute the caching hash for the entry based
947  // on the index, and nuke this map.
949 
950  // Resolve prevailing symbols, this has to be computed early because it
951  // impacts the caching.
952  resolvePrevailingInIndex(*Index, ResolvedODR, GUIDPreservedSymbols);
953 
954  // Use global summary-based analysis to identify symbols that can be
955  // internalized (because they aren't exported or preserved as per callback).
956  // Changes are made in the index, consumed in the ThinLTO backends.
957  internalizeAndPromoteInIndex(ExportLists, GUIDPreservedSymbols, *Index);
958 
959  // Make sure that every module has an entry in the ExportLists, ImportList,
960  // GVSummary and ResolvedODR maps to enable threaded access to these maps
961  // below.
962  for (auto &Module : Modules) {
963  auto ModuleIdentifier = Module->getName();
964  ExportLists[ModuleIdentifier];
965  ImportLists[ModuleIdentifier];
966  ResolvedODR[ModuleIdentifier];
967  ModuleToDefinedGVSummaries[ModuleIdentifier];
968  }
969 
970  // Compute the ordering we will process the inputs: the rough heuristic here
971  // is to sort them per size so that the largest module get schedule as soon as
972  // possible. This is purely a compile-time optimization.
973  std::vector<int> ModulesOrdering;
974  ModulesOrdering.resize(Modules.size());
975  std::iota(ModulesOrdering.begin(), ModulesOrdering.end(), 0);
976  llvm::sort(ModulesOrdering, [&](int LeftIndex, int RightIndex) {
977  auto LSize =
978  Modules[LeftIndex]->getSingleBitcodeModule().getBuffer().size();
979  auto RSize =
980  Modules[RightIndex]->getSingleBitcodeModule().getBuffer().size();
981  return LSize > RSize;
982  });
983 
984  // Parallel optimizer + codegen
985  {
986  ThreadPool Pool(ThreadCount);
987  for (auto IndexCount : ModulesOrdering) {
988  auto &Mod = Modules[IndexCount];
989  Pool.async([&](int count) {
990  auto ModuleIdentifier = Mod->getName();
991  auto &ExportList = ExportLists[ModuleIdentifier];
992 
993  auto &DefinedGVSummaries = ModuleToDefinedGVSummaries[ModuleIdentifier];
994 
995  // The module may be cached, this helps handling it.
996  ModuleCacheEntry CacheEntry(CacheOptions.Path, *Index, ModuleIdentifier,
997  ImportLists[ModuleIdentifier], ExportList,
998  ResolvedODR[ModuleIdentifier],
999  DefinedGVSummaries, OptLevel, Freestanding,
1000  TMBuilder);
1001  auto CacheEntryPath = CacheEntry.getEntryPath();
1002 
1003  {
1004  auto ErrOrBuffer = CacheEntry.tryLoadingBuffer();
1005  LLVM_DEBUG(dbgs() << "Cache " << (ErrOrBuffer ? "hit" : "miss")
1006  << " '" << CacheEntryPath << "' for buffer "
1007  << count << " " << ModuleIdentifier << "\n");
1008 
1009  if (ErrOrBuffer) {
1010  // Cache Hit!
1011  if (SavedObjectsDirectoryPath.empty())
1012  ProducedBinaries[count] = std::move(ErrOrBuffer.get());
1013  else
1014  ProducedBinaryFiles[count] = writeGeneratedObject(
1015  count, CacheEntryPath, *ErrOrBuffer.get());
1016  return;
1017  }
1018  }
1019 
1022  Context.enableDebugTypeODRUniquing();
1023  auto DiagFileOrErr = lto::setupOptimizationRemarks(
1025  RemarksWithHotness, count);
1026  if (!DiagFileOrErr) {
1027  errs() << "Error: " << toString(DiagFileOrErr.takeError()) << "\n";
1028  report_fatal_error("ThinLTO: Can't get an output file for the "
1029  "remarks");
1030  }
1031 
1032  // Parse module now
1033  auto TheModule = loadModuleFromInput(Mod.get(), Context, false,
1034  /*IsImporting*/ false);
1035 
1036  // Save temps: original file.
1037  saveTempBitcode(*TheModule, SaveTempsDir, count, ".0.original.bc");
1038 
1039  auto &ImportList = ImportLists[ModuleIdentifier];
1040  // Run the main process now, and generates a binary
1041  auto OutputBuffer = ProcessThinLTOModule(
1042  *TheModule, *Index, ModuleMap, *TMBuilder.create(), ImportList,
1043  ExportList, GUIDPreservedSymbols,
1044  ModuleToDefinedGVSummaries[ModuleIdentifier], CacheOptions,
1045  DisableCodeGen, SaveTempsDir, Freestanding, OptLevel, count);
1046 
1047  // Commit to the cache (if enabled)
1048  CacheEntry.write(*OutputBuffer);
1049 
1050  if (SavedObjectsDirectoryPath.empty()) {
1051  // We need to generated a memory buffer for the linker.
1052  if (!CacheEntryPath.empty()) {
1053  // When cache is enabled, reload from the cache if possible.
1054  // Releasing the buffer from the heap and reloading it from the
1055  // cache file with mmap helps us to lower memory pressure.
1056  // The freed memory can be used for the next input file.
1057  // The final binary link will read from the VFS cache (hopefully!)
1058  // or from disk (if the memory pressure was too high).
1059  auto ReloadedBufferOrErr = CacheEntry.tryLoadingBuffer();
1060  if (auto EC = ReloadedBufferOrErr.getError()) {
1061  // On error, keep the preexisting buffer and print a diagnostic.
1062  errs() << "error: can't reload cached file '" << CacheEntryPath
1063  << "': " << EC.message() << "\n";
1064  } else {
1065  OutputBuffer = std::move(*ReloadedBufferOrErr);
1066  }
1067  }
1068  ProducedBinaries[count] = std::move(OutputBuffer);
1069  return;
1070  }
1071  ProducedBinaryFiles[count] = writeGeneratedObject(
1072  count, CacheEntryPath, *OutputBuffer);
1073  }, IndexCount);
1074  }
1075  }
1076 
1077  pruneCache(CacheOptions.Path, CacheOptions.Policy);
1078 
1079  // If statistics were requested, print them out now.
1083 }
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: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: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: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: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:5010
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).
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:1341
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.
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
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