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