LLVM  10.0.0
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 
16 
17 #include "llvm/ADT/Statistic.h"
18 #include "llvm/ADT/StringExtras.h"
26 #include "llvm/Config/llvm-config.h"
27 #include "llvm/IR/DebugInfo.h"
29 #include "llvm/IR/LLVMContext.h"
31 #include "llvm/IR/Mangler.h"
32 #include "llvm/IR/PassTimingInfo.h"
33 #include "llvm/IR/RemarkStreamer.h"
34 #include "llvm/IR/Verifier.h"
35 #include "llvm/IRReader/IRReader.h"
36 #include "llvm/LTO/LTO.h"
41 #include "llvm/Support/Debug.h"
42 #include "llvm/Support/Error.h"
44 #include "llvm/Support/Path.h"
45 #include "llvm/Support/SHA1.h"
49 #include "llvm/Support/Threading.h"
51 #include "llvm/Support/VCSRevision.h"
53 #include "llvm/Transforms/IPO.h"
60 
61 #include <numeric>
62 
63 #if !defined(_MSC_VER) && !defined(__MINGW32__)
64 #include <unistd.h>
65 #else
66 #include <io.h>
67 #endif
68 
69 using namespace llvm;
70 
71 #define DEBUG_TYPE "thinlto"
72 
73 namespace llvm {
74 // Flags -discard-value-names, defined in LTOCodeGenerator.cpp
80 }
81 
82 namespace {
83 
84 static cl::opt<int>
85  ThreadCount("threads", cl::init(llvm::heavyweight_hardware_concurrency()));
86 
87 // Simple helper to save temporary files for debug.
88 static void saveTempBitcode(const Module &TheModule, StringRef TempDir,
89  unsigned count, StringRef Suffix) {
90  if (TempDir.empty())
91  return;
92  // User asked to save temps, let dump the bitcode file after import.
93  std::string SaveTempPath = (TempDir + llvm::Twine(count) + Suffix).str();
94  std::error_code EC;
95  raw_fd_ostream OS(SaveTempPath, EC, sys::fs::OF_None);
96  if (EC)
97  report_fatal_error(Twine("Failed to open ") + SaveTempPath +
98  " to save optimized bitcode\n");
99  WriteBitcodeToFile(TheModule, OS, /* ShouldPreserveUseListOrder */ true);
100 }
101 
102 static const GlobalValueSummary *
103 getFirstDefinitionForLinker(const GlobalValueSummaryList &GVSummaryList) {
104  // If there is any strong definition anywhere, get it.
105  auto StrongDefForLinker = llvm::find_if(
106  GVSummaryList, [](const std::unique_ptr<GlobalValueSummary> &Summary) {
107  auto Linkage = Summary->linkage();
110  });
111  if (StrongDefForLinker != GVSummaryList.end())
112  return StrongDefForLinker->get();
113  // Get the first *linker visible* definition for this global in the summary
114  // list.
115  auto FirstDefForLinker = llvm::find_if(
116  GVSummaryList, [](const std::unique_ptr<GlobalValueSummary> &Summary) {
117  auto Linkage = Summary->linkage();
119  });
120  // Extern templates can be emitted as available_externally.
121  if (FirstDefForLinker == GVSummaryList.end())
122  return nullptr;
123  return FirstDefForLinker->get();
124 }
125 
126 // Populate map of GUID to the prevailing copy for any multiply defined
127 // symbols. Currently assume first copy is prevailing, or any strong
128 // definition. Can be refined with Linker information in the future.
129 static void computePrevailingCopies(
130  const ModuleSummaryIndex &Index,
132  auto HasMultipleCopies = [&](const GlobalValueSummaryList &GVSummaryList) {
133  return GVSummaryList.size() > 1;
134  };
135 
136  for (auto &I : Index) {
137  if (HasMultipleCopies(I.second.SummaryList))
138  PrevailingCopy[I.first] =
139  getFirstDefinitionForLinker(I.second.SummaryList);
140  }
141 }
142 
144 generateModuleMap(std::vector<std::unique_ptr<lto::InputFile>> &Modules) {
145  StringMap<lto::InputFile *> ModuleMap;
146  for (auto &M : Modules) {
147  assert(ModuleMap.find(M->getName()) == ModuleMap.end() &&
148  "Expect unique Buffer Identifier");
149  ModuleMap[M->getName()] = M.get();
150  }
151  return ModuleMap;
152 }
153 
154 static void promoteModule(Module &TheModule, const ModuleSummaryIndex &Index) {
155  if (renameModuleForThinLTO(TheModule, Index))
156  report_fatal_error("renameModuleForThinLTO failed");
157 }
158 
159 namespace {
160 class ThinLTODiagnosticInfo : public DiagnosticInfo {
161  const Twine &Msg;
162 public:
163  ThinLTODiagnosticInfo(const Twine &DiagMsg,
164  DiagnosticSeverity Severity = DS_Error)
165  : DiagnosticInfo(DK_Linker, Severity), Msg(DiagMsg) {}
166  void print(DiagnosticPrinter &DP) const override { DP << Msg; }
167 };
168 }
169 
170 /// Verify the module and strip broken debug info.
171 static void verifyLoadedModule(Module &TheModule) {
172  bool BrokenDebugInfo = false;
173  if (verifyModule(TheModule, &dbgs(), &BrokenDebugInfo))
174  report_fatal_error("Broken module found, compilation aborted!");
175  if (BrokenDebugInfo) {
176  TheModule.getContext().diagnose(ThinLTODiagnosticInfo(
177  "Invalid debug info found, debug info will be stripped", DS_Warning));
178  StripDebugInfo(TheModule);
179  }
180 }
181 
182 static std::unique_ptr<Module> loadModuleFromInput(lto::InputFile *Input,
184  bool Lazy,
185  bool IsImporting) {
186  auto &Mod = Input->getSingleBitcodeModule();
187  SMDiagnostic Err;
188  Expected<std::unique_ptr<Module>> ModuleOrErr =
189  Lazy ? Mod.getLazyModule(Context,
190  /* ShouldLazyLoadMetadata */ true, IsImporting)
191  : Mod.parseModule(Context);
192  if (!ModuleOrErr) {
193  handleAllErrors(ModuleOrErr.takeError(), [&](ErrorInfoBase &EIB) {
194  SMDiagnostic Err = SMDiagnostic(Mod.getModuleIdentifier(),
195  SourceMgr::DK_Error, EIB.message());
196  Err.print("ThinLTO", errs());
197  });
198  report_fatal_error("Can't load module, abort.");
199  }
200  if (!Lazy)
201  verifyLoadedModule(*ModuleOrErr.get());
202  return std::move(*ModuleOrErr);
203 }
204 
205 static void
206 crossImportIntoModule(Module &TheModule, const ModuleSummaryIndex &Index,
207  StringMap<lto::InputFile*> &ModuleMap,
208  const FunctionImporter::ImportMapTy &ImportList) {
209  auto Loader = [&](StringRef Identifier) {
210  auto &Input = ModuleMap[Identifier];
211  return loadModuleFromInput(Input, TheModule.getContext(),
212  /*Lazy=*/true, /*IsImporting*/ true);
213  };
214 
215  FunctionImporter Importer(Index, Loader);
216  Expected<bool> Result = Importer.importFunctions(TheModule, ImportList);
217  if (!Result) {
218  handleAllErrors(Result.takeError(), [&](ErrorInfoBase &EIB) {
220  SourceMgr::DK_Error, EIB.message());
221  Err.print("ThinLTO", errs());
222  });
223  report_fatal_error("importFunctions failed");
224  }
225  // Verify again after cross-importing.
226  verifyLoadedModule(TheModule);
227 }
228 
229 static void optimizeModule(Module &TheModule, TargetMachine &TM,
230  unsigned OptLevel, bool Freestanding,
231  ModuleSummaryIndex *Index) {
232  // Populate the PassManager
233  PassManagerBuilder PMB;
235  if (Freestanding)
238  // FIXME: should get it from the bitcode?
239  PMB.OptLevel = OptLevel;
240  PMB.LoopVectorize = true;
241  PMB.SLPVectorize = true;
242  // Already did this in verifyLoadedModule().
243  PMB.VerifyInput = false;
244  PMB.VerifyOutput = false;
245  PMB.ImportSummary = Index;
246 
248 
249  // Add the TTI (required to inform the vectorizer about register size for
250  // instance)
252 
253  // Add optimizations
255 
256  PM.run(TheModule);
257 }
258 
259 static void
260 addUsedSymbolToPreservedGUID(const lto::InputFile &File,
261  DenseSet<GlobalValue::GUID> &PreservedGUID) {
262  for (const auto &Sym : File.symbols()) {
263  if (Sym.isUsed())
264  PreservedGUID.insert(GlobalValue::getGUID(Sym.getIRName()));
265  }
266 }
267 
268 // Convert the PreservedSymbols map from "Name" based to "GUID" based.
270 computeGUIDPreservedSymbols(const StringSet<> &PreservedSymbols,
271  const Triple &TheTriple) {
272  DenseSet<GlobalValue::GUID> GUIDPreservedSymbols(PreservedSymbols.size());
273  for (auto &Entry : PreservedSymbols) {
274  StringRef Name = Entry.first();
275  if (TheTriple.isOSBinFormatMachO() && Name.size() > 0 && Name[0] == '_')
276  Name = Name.drop_front();
277  GUIDPreservedSymbols.insert(GlobalValue::getGUID(Name));
278  }
279  return GUIDPreservedSymbols;
280 }
281 
282 std::unique_ptr<MemoryBuffer> codegenModule(Module &TheModule,
283  TargetMachine &TM) {
284  SmallVector<char, 128> OutputBuffer;
285 
286  // CodeGen
287  {
288  raw_svector_ostream OS(OutputBuffer);
290 
291  // If the bitcode files contain ARC code and were compiled with optimization,
292  // the ObjCARCContractPass must be run, so do it unconditionally here.
294 
295  // Setup the codegen now.
296  if (TM.addPassesToEmitFile(PM, OS, nullptr, CGFT_ObjectFile,
297  /* DisableVerify */ true))
298  report_fatal_error("Failed to setup codegen");
299 
300  // Run codegen now. resulting binary is in OutputBuffer.
301  PM.run(TheModule);
302  }
303  return std::make_unique<SmallVectorMemoryBuffer>(std::move(OutputBuffer));
304 }
305 
306 /// Manage caching for a single Module.
307 class ModuleCacheEntry {
308  SmallString<128> EntryPath;
309 
310 public:
311  // Create a cache entry. This compute a unique hash for the Module considering
312  // the current list of export/import, and offer an interface to query to
313  // access the content in the cache.
314  ModuleCacheEntry(
315  StringRef CachePath, const ModuleSummaryIndex &Index, StringRef ModuleID,
316  const FunctionImporter::ImportMapTy &ImportList,
317  const FunctionImporter::ExportSetTy &ExportList,
318  const std::map<GlobalValue::GUID, GlobalValue::LinkageTypes> &ResolvedODR,
319  const GVSummaryMapTy &DefinedGVSummaries, unsigned OptLevel,
320  bool Freestanding, const TargetMachineBuilder &TMBuilder) {
321  if (CachePath.empty())
322  return;
323 
324  if (!Index.modulePaths().count(ModuleID))
325  // The module does not have an entry, it can't have a hash at all
326  return;
327 
328  if (all_of(Index.getModuleHash(ModuleID),
329  [](uint32_t V) { return V == 0; }))
330  // No hash entry, no caching!
331  return;
332 
333  llvm::lto::Config Conf;
334  Conf.OptLevel = OptLevel;
335  Conf.Options = TMBuilder.Options;
336  Conf.CPU = TMBuilder.MCpu;
337  Conf.MAttrs.push_back(TMBuilder.MAttr);
338  Conf.RelocModel = TMBuilder.RelocModel;
339  Conf.CGOptLevel = TMBuilder.CGOptLevel;
340  Conf.Freestanding = Freestanding;
342  computeLTOCacheKey(Key, Conf, Index, ModuleID, ImportList, ExportList,
343  ResolvedODR, DefinedGVSummaries);
344 
345  // This choice of file name allows the cache to be pruned (see pruneCache()
346  // in include/llvm/Support/CachePruning.h).
347  sys::path::append(EntryPath, CachePath, "llvmcache-" + Key);
348  }
349 
350  // Access the path to this entry in the cache.
351  StringRef getEntryPath() { return EntryPath; }
352 
353  // Try loading the buffer for this cache entry.
354  ErrorOr<std::unique_ptr<MemoryBuffer>> tryLoadingBuffer() {
355  if (EntryPath.empty())
356  return std::error_code();
357  SmallString<64> ResultPath;
359  Twine(EntryPath), sys::fs::OF_UpdateAtime, &ResultPath);
360  if (!FDOrErr)
361  return errorToErrorCode(FDOrErr.takeError());
363  *FDOrErr, EntryPath, /*FileSize=*/-1, /*RequiresNullTerminator=*/false);
364  sys::fs::closeFile(*FDOrErr);
365  return MBOrErr;
366  }
367 
368  // Cache the Produced object file
369  void write(const MemoryBuffer &OutputBuffer) {
370  if (EntryPath.empty())
371  return;
372 
373  // Write to a temporary to avoid race condition
374  SmallString<128> TempFilename;
375  SmallString<128> CachePath(EntryPath);
377  sys::path::append(TempFilename, CachePath, "Thin-%%%%%%.tmp.o");
378 
379  if (auto Err = handleErrors(
380  llvm::writeFileAtomically(TempFilename, EntryPath,
381  OutputBuffer.getBuffer()),
382  [](const llvm::AtomicFileWriteError &E) {
383  std::string ErrorMsgBuffer;
384  llvm::raw_string_ostream S(ErrorMsgBuffer);
385  E.log(S);
386 
387  if (E.Error ==
389  errs() << "Error: " << ErrorMsgBuffer << "\n";
390  report_fatal_error("ThinLTO: Can't get a temporary file");
391  }
392  })) {
393  // FIXME
394  consumeError(std::move(Err));
395  }
396  }
397 };
398 
399 static std::unique_ptr<MemoryBuffer>
400 ProcessThinLTOModule(Module &TheModule, ModuleSummaryIndex &Index,
402  const FunctionImporter::ImportMapTy &ImportList,
403  const FunctionImporter::ExportSetTy &ExportList,
404  const DenseSet<GlobalValue::GUID> &GUIDPreservedSymbols,
405  const GVSummaryMapTy &DefinedGlobals,
406  const ThinLTOCodeGenerator::CachingOptions &CacheOptions,
407  bool DisableCodeGen, StringRef SaveTempsDir,
408  bool Freestanding, unsigned OptLevel, unsigned count) {
409 
410  // "Benchmark"-like optimization: single-source case
411  bool SingleModule = (ModuleMap.size() == 1);
412 
413  if (!SingleModule) {
414  promoteModule(TheModule, Index);
415 
416  // Apply summary-based prevailing-symbol resolution decisions.
417  thinLTOResolvePrevailingInModule(TheModule, DefinedGlobals);
418 
419  // Save temps: after promotion.
420  saveTempBitcode(TheModule, SaveTempsDir, count, ".1.promoted.bc");
421  }
422 
423  // Be friendly and don't nuke totally the module when the client didn't
424  // supply anything to preserve.
425  if (!ExportList.empty() || !GUIDPreservedSymbols.empty()) {
426  // Apply summary-based internalization decisions.
427  thinLTOInternalizeModule(TheModule, DefinedGlobals);
428  }
429 
430  // Save internalized bitcode
431  saveTempBitcode(TheModule, SaveTempsDir, count, ".2.internalized.bc");
432 
433  if (!SingleModule) {
434  crossImportIntoModule(TheModule, Index, ModuleMap, ImportList);
435 
436  // Save temps: after cross-module import.
437  saveTempBitcode(TheModule, SaveTempsDir, count, ".3.imported.bc");
438  }
439 
440  optimizeModule(TheModule, TM, OptLevel, Freestanding, &Index);
441 
442  saveTempBitcode(TheModule, SaveTempsDir, count, ".4.opt.bc");
443 
444  if (DisableCodeGen) {
445  // Configured to stop before CodeGen, serialize the bitcode and return.
446  SmallVector<char, 128> OutputBuffer;
447  {
448  raw_svector_ostream OS(OutputBuffer);
449  ProfileSummaryInfo PSI(TheModule);
450  auto Index = buildModuleSummaryIndex(TheModule, nullptr, &PSI);
451  WriteBitcodeToFile(TheModule, OS, true, &Index);
452  }
453  return std::make_unique<SmallVectorMemoryBuffer>(std::move(OutputBuffer));
454  }
455 
456  return codegenModule(TheModule, TM);
457 }
458 
459 /// Resolve prevailing symbols. Record resolutions in the \p ResolvedODR map
460 /// for caching, and in the \p Index for application during the ThinLTO
461 /// backends. This is needed for correctness for exported symbols (ensure
462 /// at least one copy kept) and a compile-time optimization (to drop duplicate
463 /// copies when possible).
464 static void resolvePrevailingInIndex(
465  ModuleSummaryIndex &Index,
466  StringMap<std::map<GlobalValue::GUID, GlobalValue::LinkageTypes>>
467  &ResolvedODR,
468  const DenseSet<GlobalValue::GUID> &GUIDPreservedSymbols,
470  &PrevailingCopy) {
471 
472  auto isPrevailing = [&](GlobalValue::GUID GUID, const GlobalValueSummary *S) {
473  const auto &Prevailing = PrevailingCopy.find(GUID);
474  // Not in map means that there was only one copy, which must be prevailing.
475  if (Prevailing == PrevailingCopy.end())
476  return true;
477  return Prevailing->second == S;
478  };
479 
480  auto recordNewLinkage = [&](StringRef ModuleIdentifier,
481  GlobalValue::GUID GUID,
482  GlobalValue::LinkageTypes NewLinkage) {
483  ResolvedODR[ModuleIdentifier][GUID] = NewLinkage;
484  };
485 
486  thinLTOResolvePrevailingInIndex(Index, isPrevailing, recordNewLinkage,
487  GUIDPreservedSymbols);
488 }
489 
490 // Initialize the TargetMachine builder for a given Triple
491 static void initTMBuilder(TargetMachineBuilder &TMBuilder,
492  const Triple &TheTriple) {
493  // Set a default CPU for Darwin triples (copied from LTOCodeGenerator).
494  // FIXME this looks pretty terrible...
495  if (TMBuilder.MCpu.empty() && TheTriple.isOSDarwin()) {
496  if (TheTriple.getArch() == llvm::Triple::x86_64)
497  TMBuilder.MCpu = "core2";
498  else if (TheTriple.getArch() == llvm::Triple::x86)
499  TMBuilder.MCpu = "yonah";
500  else if (TheTriple.getArch() == llvm::Triple::aarch64 ||
501  TheTriple.getArch() == llvm::Triple::aarch64_32)
502  TMBuilder.MCpu = "cyclone";
503  }
504  TMBuilder.TheTriple = std::move(TheTriple);
505 }
506 
507 } // end anonymous namespace
508 
510  MemoryBufferRef Buffer(Data, Identifier);
511 
512  auto InputOrError = lto::InputFile::create(Buffer);
513  if (!InputOrError)
514  report_fatal_error("ThinLTO cannot create input file: " +
515  toString(InputOrError.takeError()));
516 
517  auto TripleStr = (*InputOrError)->getTargetTriple();
518  Triple TheTriple(TripleStr);
519 
520  if (Modules.empty())
521  initTMBuilder(TMBuilder, Triple(TheTriple));
522  else if (TMBuilder.TheTriple != TheTriple) {
523  if (!TMBuilder.TheTriple.isCompatibleWith(TheTriple))
524  report_fatal_error("ThinLTO modules with incompatible triples not "
525  "supported");
526  initTMBuilder(TMBuilder, Triple(TMBuilder.TheTriple.merge(TheTriple)));
527  }
528 
529  Modules.emplace_back(std::move(*InputOrError));
530 }
531 
533  PreservedSymbols.insert(Name);
534 }
535 
537  // FIXME: At the moment, we don't take advantage of this extra information,
538  // we're conservatively considering cross-references as preserved.
539  // CrossReferencedSymbols.insert(Name);
540  PreservedSymbols.insert(Name);
541 }
542 
543 // TargetMachine factory
544 std::unique_ptr<TargetMachine> TargetMachineBuilder::create() const {
545  std::string ErrMsg;
546  const Target *TheTarget =
547  TargetRegistry::lookupTarget(TheTriple.str(), ErrMsg);
548  if (!TheTarget) {
549  report_fatal_error("Can't load target for this Triple: " + ErrMsg);
550  }
551 
552  // Use MAttr as the default set of features.
553  SubtargetFeatures Features(MAttr);
554  Features.getDefaultSubtargetFeatures(TheTriple);
555  std::string FeatureStr = Features.getString();
556 
557  return std::unique_ptr<TargetMachine>(
558  TheTarget->createTargetMachine(TheTriple.str(), MCpu, FeatureStr, Options,
559  RelocModel, None, CGOptLevel));
560 }
561 
562 /**
563  * Produce the combined summary index from all the bitcode files:
564  * "thin-link".
565  */
566 std::unique_ptr<ModuleSummaryIndex> ThinLTOCodeGenerator::linkCombinedIndex() {
567  std::unique_ptr<ModuleSummaryIndex> CombinedIndex =
568  std::make_unique<ModuleSummaryIndex>(/*HaveGVs=*/false);
569  uint64_t NextModuleId = 0;
570  for (auto &Mod : Modules) {
571  auto &M = Mod->getSingleBitcodeModule();
572  if (Error Err =
573  M.readSummary(*CombinedIndex, Mod->getName(), NextModuleId++)) {
574  // FIXME diagnose
576  std::move(Err), errs(),
577  "error: can't create module summary index for buffer: ");
578  return nullptr;
579  }
580  }
581  return CombinedIndex;
582 }
583 
584 namespace {
585 struct IsExported {
586  const StringMap<FunctionImporter::ExportSetTy> &ExportLists;
587  const DenseSet<GlobalValue::GUID> &GUIDPreservedSymbols;
588 
589  IsExported(const StringMap<FunctionImporter::ExportSetTy> &ExportLists,
590  const DenseSet<GlobalValue::GUID> &GUIDPreservedSymbols)
591  : ExportLists(ExportLists), GUIDPreservedSymbols(GUIDPreservedSymbols) {}
592 
593  bool operator()(StringRef ModuleIdentifier, ValueInfo VI) const {
594  const auto &ExportList = ExportLists.find(ModuleIdentifier);
595  return (ExportList != ExportLists.end() && ExportList->second.count(VI)) ||
596  GUIDPreservedSymbols.count(VI.getGUID());
597  }
598 };
599 
600 struct IsPrevailing {
603  &PrevailingCopy)
604  : PrevailingCopy(PrevailingCopy) {}
605 
606  bool operator()(GlobalValue::GUID GUID, const GlobalValueSummary *S) const {
607  const auto &Prevailing = PrevailingCopy.find(GUID);
608  // Not in map means that there was only one copy, which must be prevailing.
609  if (Prevailing == PrevailingCopy.end())
610  return true;
611  return Prevailing->second == S;
612  };
613 };
614 } // namespace
615 
618  const DenseSet<GlobalValue::GUID> &GUIDPreservedSymbols) {
619  // We have no symbols resolution available. And can't do any better now in the
620  // case where the prevailing symbol is in a native object. It can be refined
621  // with linker information in the future.
622  auto isPrevailing = [&](GlobalValue::GUID G) {
624  };
625  computeDeadSymbolsWithConstProp(Index, GUIDPreservedSymbols, isPrevailing,
626  /* ImportEnabled = */ true);
627 }
628 
629 /**
630  * Perform promotion and renaming of exported internal functions.
631  * Index is updated to reflect linkage changes from weak resolution.
632  */
634  const lto::InputFile &File) {
635  auto ModuleCount = Index.modulePaths().size();
636  auto ModuleIdentifier = TheModule.getModuleIdentifier();
637 
638  // Collect for each module the list of function it defines (GUID -> Summary).
639  StringMap<GVSummaryMapTy> ModuleToDefinedGVSummaries;
640  Index.collectDefinedGVSummariesPerModule(ModuleToDefinedGVSummaries);
641 
642  // Convert the preserved symbols set from string to GUID
643  auto GUIDPreservedSymbols = computeGUIDPreservedSymbols(
644  PreservedSymbols, Triple(TheModule.getTargetTriple()));
645 
646  // Add used symbol to the preserved symbols.
647  addUsedSymbolToPreservedGUID(File, GUIDPreservedSymbols);
648 
649  // Compute "dead" symbols, we don't want to import/export these!
650  computeDeadSymbolsInIndex(Index, GUIDPreservedSymbols);
651 
652  // Generate import/export list
653  StringMap<FunctionImporter::ImportMapTy> ImportLists(ModuleCount);
654  StringMap<FunctionImporter::ExportSetTy> ExportLists(ModuleCount);
655  ComputeCrossModuleImport(Index, ModuleToDefinedGVSummaries, ImportLists,
656  ExportLists);
657 
659  computePrevailingCopies(Index, PrevailingCopy);
660 
661  // Resolve prevailing symbols
663  resolvePrevailingInIndex(Index, ResolvedODR, GUIDPreservedSymbols,
664  PrevailingCopy);
665 
667  TheModule, ModuleToDefinedGVSummaries[ModuleIdentifier]);
668 
669  // Promote the exported values in the index, so that they are promoted
670  // in the module.
672  Index, IsExported(ExportLists, GUIDPreservedSymbols),
673  IsPrevailing(PrevailingCopy));
674 
675  promoteModule(TheModule, Index);
676 }
677 
678 /**
679  * Perform cross-module importing for the module identified by ModuleIdentifier.
680  */
683  const lto::InputFile &File) {
684  auto ModuleMap = generateModuleMap(Modules);
685  auto ModuleCount = Index.modulePaths().size();
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  auto &ImportList = ImportLists[TheModule.getModuleIdentifier()];
706 
707  crossImportIntoModule(TheModule, Index, ModuleMap, ImportList);
708 }
709 
710 /**
711  * Compute the list of summaries needed for importing into module.
712  */
714  Module &TheModule, ModuleSummaryIndex &Index,
715  std::map<std::string, GVSummaryMapTy> &ModuleToSummariesForIndex,
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 
740  ModuleIdentifier, ModuleToDefinedGVSummaries,
741  ImportLists[ModuleIdentifier], ModuleToSummariesForIndex);
742 }
743 
744 /**
745  * Emit the list of files needed for importing into module.
746  */
749  const lto::InputFile &File) {
750  auto ModuleCount = Index.modulePaths().size();
751  auto ModuleIdentifier = TheModule.getModuleIdentifier();
752 
753  // Collect for each module the list of function it defines (GUID -> Summary).
754  StringMap<GVSummaryMapTy> ModuleToDefinedGVSummaries(ModuleCount);
755  Index.collectDefinedGVSummariesPerModule(ModuleToDefinedGVSummaries);
756 
757  // Convert the preserved symbols set from string to GUID
758  auto GUIDPreservedSymbols = computeGUIDPreservedSymbols(
759  PreservedSymbols, Triple(TheModule.getTargetTriple()));
760 
761  addUsedSymbolToPreservedGUID(File, GUIDPreservedSymbols);
762 
763  // Compute "dead" symbols, we don't want to import/export these!
764  computeDeadSymbolsInIndex(Index, GUIDPreservedSymbols);
765 
766  // Generate import/export list
767  StringMap<FunctionImporter::ImportMapTy> ImportLists(ModuleCount);
768  StringMap<FunctionImporter::ExportSetTy> ExportLists(ModuleCount);
769  ComputeCrossModuleImport(Index, ModuleToDefinedGVSummaries, ImportLists,
770  ExportLists);
771 
772  std::map<std::string, GVSummaryMapTy> ModuleToSummariesForIndex;
774  ModuleIdentifier, ModuleToDefinedGVSummaries,
775  ImportLists[ModuleIdentifier], ModuleToSummariesForIndex);
776 
777  std::error_code EC;
778  if ((EC = EmitImportsFiles(ModuleIdentifier, OutputName,
779  ModuleToSummariesForIndex)))
780  report_fatal_error(Twine("Failed to open ") + OutputName +
781  " to save imports lists\n");
782 }
783 
784 /**
785  * Perform internalization. Runs promote and internalization together.
786  * Index is updated to reflect linkage changes.
787  */
790  const lto::InputFile &File) {
791  initTMBuilder(TMBuilder, Triple(TheModule.getTargetTriple()));
792  auto ModuleCount = Index.modulePaths().size();
793  auto ModuleIdentifier = TheModule.getModuleIdentifier();
794 
795  // Convert the preserved symbols set from string to GUID
796  auto GUIDPreservedSymbols =
797  computeGUIDPreservedSymbols(PreservedSymbols, TMBuilder.TheTriple);
798 
799  addUsedSymbolToPreservedGUID(File, GUIDPreservedSymbols);
800 
801  // Collect for each module the list of function it defines (GUID -> Summary).
802  StringMap<GVSummaryMapTy> ModuleToDefinedGVSummaries(ModuleCount);
803  Index.collectDefinedGVSummariesPerModule(ModuleToDefinedGVSummaries);
804 
805  // Compute "dead" symbols, we don't want to import/export these!
806  computeDeadSymbolsInIndex(Index, GUIDPreservedSymbols);
807 
808  // Generate import/export list
809  StringMap<FunctionImporter::ImportMapTy> ImportLists(ModuleCount);
810  StringMap<FunctionImporter::ExportSetTy> ExportLists(ModuleCount);
811  ComputeCrossModuleImport(Index, ModuleToDefinedGVSummaries, ImportLists,
812  ExportLists);
813  auto &ExportList = ExportLists[ModuleIdentifier];
814 
815  // Be friendly and don't nuke totally the module when the client didn't
816  // supply anything to preserve.
817  if (ExportList.empty() && GUIDPreservedSymbols.empty())
818  return;
819 
821  computePrevailingCopies(Index, PrevailingCopy);
822 
823  // Resolve prevailing symbols
825  resolvePrevailingInIndex(Index, ResolvedODR, GUIDPreservedSymbols,
826  PrevailingCopy);
827 
828  // Promote the exported values in the index, so that they are promoted
829  // in the module.
831  Index, IsExported(ExportLists, GUIDPreservedSymbols),
832  IsPrevailing(PrevailingCopy));
833 
834  promoteModule(TheModule, Index);
835 
836  // Internalization
838  TheModule, ModuleToDefinedGVSummaries[ModuleIdentifier]);
839 
840  thinLTOInternalizeModule(TheModule,
841  ModuleToDefinedGVSummaries[ModuleIdentifier]);
842 }
843 
844 /**
845  * Perform post-importing ThinLTO optimizations.
846  */
848  initTMBuilder(TMBuilder, Triple(TheModule.getTargetTriple()));
849 
850  // Optimize now
851  optimizeModule(TheModule, *TMBuilder.create(), OptLevel, Freestanding,
852  nullptr);
853 }
854 
855 /// Write out the generated object file, either from CacheEntryPath or from
856 /// OutputBuffer, preferring hard-link when possible.
857 /// Returns the path to the generated file in SavedObjectsDirectoryPath.
858 std::string
860  const MemoryBuffer &OutputBuffer) {
861  auto ArchName = TMBuilder.TheTriple.getArchName();
862  SmallString<128> OutputPath(SavedObjectsDirectoryPath);
863  llvm::sys::path::append(OutputPath,
864  Twine(count) + "." + ArchName + ".thinlto.o");
865  OutputPath.c_str(); // Ensure the string is null terminated.
866  if (sys::fs::exists(OutputPath))
867  sys::fs::remove(OutputPath);
868 
869  // We don't return a memory buffer to the linker, just a list of files.
870  if (!CacheEntryPath.empty()) {
871  // Cache is enabled, hard-link the entry (or copy if hard-link fails).
872  auto Err = sys::fs::create_hard_link(CacheEntryPath, OutputPath);
873  if (!Err)
874  return OutputPath.str();
875  // Hard linking failed, try to copy.
876  Err = sys::fs::copy_file(CacheEntryPath, OutputPath);
877  if (!Err)
878  return OutputPath.str();
879  // Copy failed (could be because the CacheEntry was removed from the cache
880  // in the meantime by another process), fall back and try to write down the
881  // buffer to the output.
882  errs() << "error: can't link or copy from cached entry '" << CacheEntryPath
883  << "' to '" << OutputPath << "'\n";
884  }
885  // No cache entry, just write out the buffer.
886  std::error_code Err;
887  raw_fd_ostream OS(OutputPath, Err, sys::fs::OF_None);
888  if (Err)
889  report_fatal_error("Can't open output '" + OutputPath + "'\n");
890  OS << OutputBuffer.getBuffer();
891  return OutputPath.str();
892 }
893 
894 // Main entry point for the ThinLTO processing
896  // Prepare the resulting object vector
897  assert(ProducedBinaries.empty() && "The generator should not be reused");
898  if (SavedObjectsDirectoryPath.empty())
899  ProducedBinaries.resize(Modules.size());
900  else {
901  sys::fs::create_directories(SavedObjectsDirectoryPath);
902  bool IsDir;
903  sys::fs::is_directory(SavedObjectsDirectoryPath, IsDir);
904  if (!IsDir)
905  report_fatal_error("Unexistent dir: '" + SavedObjectsDirectoryPath + "'");
906  ProducedBinaryFiles.resize(Modules.size());
907  }
908 
909  if (CodeGenOnly) {
910  // Perform only parallel codegen and return.
911  ThreadPool Pool;
912  int count = 0;
913  for (auto &Mod : Modules) {
914  Pool.async([&](int count) {
917 
918  // Parse module now
919  auto TheModule = loadModuleFromInput(Mod.get(), Context, false,
920  /*IsImporting*/ false);
921 
922  // CodeGen
923  auto OutputBuffer = codegenModule(*TheModule, *TMBuilder.create());
924  if (SavedObjectsDirectoryPath.empty())
925  ProducedBinaries[count] = std::move(OutputBuffer);
926  else
927  ProducedBinaryFiles[count] =
928  writeGeneratedObject(count, "", *OutputBuffer);
929  }, count++);
930  }
931 
932  return;
933  }
934 
935  // Sequential linking phase
936  auto Index = linkCombinedIndex();
937 
938  // Save temps: index.
939  if (!SaveTempsDir.empty()) {
940  auto SaveTempPath = SaveTempsDir + "index.bc";
941  std::error_code EC;
942  raw_fd_ostream OS(SaveTempPath, EC, sys::fs::OF_None);
943  if (EC)
944  report_fatal_error(Twine("Failed to open ") + SaveTempPath +
945  " to save optimized bitcode\n");
946  WriteIndexToFile(*Index, OS);
947  }
948 
949 
950  // Prepare the module map.
951  auto ModuleMap = generateModuleMap(Modules);
952  auto ModuleCount = Modules.size();
953 
954  // Collect for each module the list of function it defines (GUID -> Summary).
955  StringMap<GVSummaryMapTy> ModuleToDefinedGVSummaries(ModuleCount);
956  Index->collectDefinedGVSummariesPerModule(ModuleToDefinedGVSummaries);
957 
958  // Convert the preserved symbols set from string to GUID, this is needed for
959  // computing the caching hash and the internalization.
960  auto GUIDPreservedSymbols =
961  computeGUIDPreservedSymbols(PreservedSymbols, TMBuilder.TheTriple);
962 
963  // Add used symbol from inputs to the preserved symbols.
964  for (const auto &M : Modules)
965  addUsedSymbolToPreservedGUID(*M, GUIDPreservedSymbols);
966 
967  // Compute "dead" symbols, we don't want to import/export these!
968  computeDeadSymbolsInIndex(*Index, GUIDPreservedSymbols);
969 
970  // Synthesize entry counts for functions in the combined index.
972 
973  // Perform index-based WPD. This will return immediately if there are
974  // no index entries in the typeIdMetadata map (e.g. if we are instead
975  // performing IR-based WPD in hybrid regular/thin LTO mode).
976  std::map<ValueInfo, std::vector<VTableSlotSummary>> LocalWPDTargetsMap;
977  std::set<GlobalValue::GUID> ExportedGUIDs;
978  runWholeProgramDevirtOnIndex(*Index, ExportedGUIDs, LocalWPDTargetsMap);
979  for (auto GUID : ExportedGUIDs)
980  GUIDPreservedSymbols.insert(GUID);
981 
982  // Collect the import/export lists for all modules from the call-graph in the
983  // combined index.
984  StringMap<FunctionImporter::ImportMapTy> ImportLists(ModuleCount);
985  StringMap<FunctionImporter::ExportSetTy> ExportLists(ModuleCount);
986  ComputeCrossModuleImport(*Index, ModuleToDefinedGVSummaries, ImportLists,
987  ExportLists);
988 
989  // We use a std::map here to be able to have a defined ordering when
990  // producing a hash for the cache entry.
991  // FIXME: we should be able to compute the caching hash for the entry based
992  // on the index, and nuke this map.
994 
996  computePrevailingCopies(*Index, PrevailingCopy);
997 
998  // Resolve prevailing symbols, this has to be computed early because it
999  // impacts the caching.
1000  resolvePrevailingInIndex(*Index, ResolvedODR, GUIDPreservedSymbols,
1001  PrevailingCopy);
1002 
1003  // Use global summary-based analysis to identify symbols that can be
1004  // internalized (because they aren't exported or preserved as per callback).
1005  // Changes are made in the index, consumed in the ThinLTO backends.
1007  IsExported(ExportLists, GUIDPreservedSymbols),
1008  LocalWPDTargetsMap);
1010  *Index, IsExported(ExportLists, GUIDPreservedSymbols),
1011  IsPrevailing(PrevailingCopy));
1012 
1013  // Make sure that every module has an entry in the ExportLists, ImportList,
1014  // GVSummary and ResolvedODR maps to enable threaded access to these maps
1015  // below.
1016  for (auto &Module : Modules) {
1017  auto ModuleIdentifier = Module->getName();
1018  ExportLists[ModuleIdentifier];
1019  ImportLists[ModuleIdentifier];
1020  ResolvedODR[ModuleIdentifier];
1021  ModuleToDefinedGVSummaries[ModuleIdentifier];
1022  }
1023 
1024  // Compute the ordering we will process the inputs: the rough heuristic here
1025  // is to sort them per size so that the largest module get schedule as soon as
1026  // possible. This is purely a compile-time optimization.
1027  std::vector<int> ModulesOrdering;
1028  ModulesOrdering.resize(Modules.size());
1029  std::iota(ModulesOrdering.begin(), ModulesOrdering.end(), 0);
1030  llvm::sort(ModulesOrdering, [&](int LeftIndex, int RightIndex) {
1031  auto LSize =
1032  Modules[LeftIndex]->getSingleBitcodeModule().getBuffer().size();
1033  auto RSize =
1034  Modules[RightIndex]->getSingleBitcodeModule().getBuffer().size();
1035  return LSize > RSize;
1036  });
1037 
1038  // Parallel optimizer + codegen
1039  {
1040  ThreadPool Pool(ThreadCount);
1041  for (auto IndexCount : ModulesOrdering) {
1042  auto &Mod = Modules[IndexCount];
1043  Pool.async([&](int count) {
1044  auto ModuleIdentifier = Mod->getName();
1045  auto &ExportList = ExportLists[ModuleIdentifier];
1046 
1047  auto &DefinedGVSummaries = ModuleToDefinedGVSummaries[ModuleIdentifier];
1048 
1049  // The module may be cached, this helps handling it.
1050  ModuleCacheEntry CacheEntry(CacheOptions.Path, *Index, ModuleIdentifier,
1051  ImportLists[ModuleIdentifier], ExportList,
1052  ResolvedODR[ModuleIdentifier],
1053  DefinedGVSummaries, OptLevel, Freestanding,
1054  TMBuilder);
1055  auto CacheEntryPath = CacheEntry.getEntryPath();
1056 
1057  {
1058  auto ErrOrBuffer = CacheEntry.tryLoadingBuffer();
1059  LLVM_DEBUG(dbgs() << "Cache " << (ErrOrBuffer ? "hit" : "miss")
1060  << " '" << CacheEntryPath << "' for buffer "
1061  << count << " " << ModuleIdentifier << "\n");
1062 
1063  if (ErrOrBuffer) {
1064  // Cache Hit!
1065  if (SavedObjectsDirectoryPath.empty())
1066  ProducedBinaries[count] = std::move(ErrOrBuffer.get());
1067  else
1068  ProducedBinaryFiles[count] = writeGeneratedObject(
1069  count, CacheEntryPath, *ErrOrBuffer.get());
1070  return;
1071  }
1072  }
1073 
1076  Context.enableDebugTypeODRUniquing();
1077  auto DiagFileOrErr = lto::setupOptimizationRemarks(
1079  RemarksWithHotness, count);
1080  if (!DiagFileOrErr) {
1081  errs() << "Error: " << toString(DiagFileOrErr.takeError()) << "\n";
1082  report_fatal_error("ThinLTO: Can't get an output file for the "
1083  "remarks");
1084  }
1085 
1086  // Parse module now
1087  auto TheModule = loadModuleFromInput(Mod.get(), Context, false,
1088  /*IsImporting*/ false);
1089 
1090  // Save temps: original file.
1091  saveTempBitcode(*TheModule, SaveTempsDir, count, ".0.original.bc");
1092 
1093  auto &ImportList = ImportLists[ModuleIdentifier];
1094  // Run the main process now, and generates a binary
1095  auto OutputBuffer = ProcessThinLTOModule(
1096  *TheModule, *Index, ModuleMap, *TMBuilder.create(), ImportList,
1097  ExportList, GUIDPreservedSymbols,
1098  ModuleToDefinedGVSummaries[ModuleIdentifier], CacheOptions,
1099  DisableCodeGen, SaveTempsDir, Freestanding, OptLevel, count);
1100 
1101  // Commit to the cache (if enabled)
1102  CacheEntry.write(*OutputBuffer);
1103 
1104  if (SavedObjectsDirectoryPath.empty()) {
1105  // We need to generated a memory buffer for the linker.
1106  if (!CacheEntryPath.empty()) {
1107  // When cache is enabled, reload from the cache if possible.
1108  // Releasing the buffer from the heap and reloading it from the
1109  // cache file with mmap helps us to lower memory pressure.
1110  // The freed memory can be used for the next input file.
1111  // The final binary link will read from the VFS cache (hopefully!)
1112  // or from disk (if the memory pressure was too high).
1113  auto ReloadedBufferOrErr = CacheEntry.tryLoadingBuffer();
1114  if (auto EC = ReloadedBufferOrErr.getError()) {
1115  // On error, keep the preexisting buffer and print a diagnostic.
1116  errs() << "error: can't reload cached file '" << CacheEntryPath
1117  << "': " << EC.message() << "\n";
1118  } else {
1119  OutputBuffer = std::move(*ReloadedBufferOrErr);
1120  }
1121  }
1122  ProducedBinaries[count] = std::move(OutputBuffer);
1123  return;
1124  }
1125  ProducedBinaryFiles[count] = writeGeneratedObject(
1126  count, CacheEntryPath, *OutputBuffer);
1127  }, IndexCount);
1128  }
1129  }
1130 
1131  pruneCache(CacheOptions.Path, CacheOptions.Policy);
1132 
1133  // If statistics were requested, print them out now.
1137 }
bool isOSDarwin() const
isOSDarwin - Is this a "Darwin" OS (macOS, iOS, tvOS or watchOS).
Definition: Triple.h:482
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:938
ArrayRef< Symbol > symbols() const
A range over the symbols in this InputFile.
Definition: LTO.h:157
const std::string & getTargetTriple() const
Get the target triple which is a string describing the target host.
Definition: Module.h:241
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:41
void thinLTOInternalizeAndPromoteInIndex(ModuleSummaryIndex &Index, function_ref< bool(StringRef, ValueInfo)> isExported, function_ref< bool(GlobalValue::GUID, const GlobalValueSummary *)> isPrevailing)
Update the linkages in the given Index to mark exported values as external and non-exported values as...
Definition: LTO.cpp:418
LLVMContext & Context
CodeGenOpt::Level CGOptLevel
Definition: Config.h:46
virtual bool addPassesToEmitFile(PassManagerBase &, raw_pwrite_stream &, raw_pwrite_stream *, CodeGenFileType, bool=true, MachineModuleInfoWrapperPass *MMIWP=nullptr)
Add passes to the specified pass manager to get the specified file emitted.
PassManagerBuilder - This class is used to set up a standard optimization sequence for languages like...
BitcodeModule & getSingleBitcodeModule()
Definition: LTO.cpp:464
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:140
This class represents lattice values for constants.
Definition: AllocatorList.h:23
void getDefaultSubtargetFeatures(const Triple &Triple)
Adds the default features for the specified target triple.
void print(const char *ProgName, raw_ostream &S, bool ShowColors=true, bool ShowKindLabel=true) const
Definition: SourceMgr.cpp:368
unsigned heavyweight_hardware_concurrency()
Get the amount of currency to use for tasks requiring significant memory or other resources...
Definition: Threading.cpp:82
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:66
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...
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:355
const StringMap< std::pair< uint64_t, ModuleHash > > & modulePaths() const
Table of modules, containing module hash and id.
const ModuleSummaryIndex * ImportSummary
The module summary index to use for importing information to the thin LTO backends, for example for the CFI and devirtualization type tests.
A raw_ostream that writes to an SmallVector or SmallString.
Definition: raw_ostream.h:543
bool is_directory(const basic_file_status &status)
Does status represent a directory?
Definition: Path.cpp:1063
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:1182
StringRef getName() const
Get a short "name" for the module.
Definition: Module.h:228
static Expected< std::unique_ptr< InputFile > > create(MemoryBufferRef Object)
Create an InputFile.
Definition: LTO.cpp:431
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.
const atomic_write_error Error
Definition: FileUtilities.h:91
void setDiscardValueNames(bool Discard)
Set the Context runtime configuration to discard all value name (but GlobalValue).
void log(raw_ostream &OS) const override
Print an error message to an output stream.
ModuleSummaryIndex buildModuleSummaryIndex(const Module &M, std::function< BlockFrequencyInfo *(const Function &F)> GetBFICallback, ProfileSummaryInfo *PSI)
Direct function to compute a ModuleSummaryIndex from a given module.
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
GlobalValue::GUID getGUID() const
std::string toString(Error E)
Write all error messages (if any) in E to a string.
Definition: Error.h:986
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:642
std::vector< std::string > MAttrs
Definition: Config.h:43
LLVMContext & getContext() const
Get the global data context.
Definition: Module.h:245
void updateIndexWPDForExports(ModuleSummaryIndex &Summary, function_ref< bool(StringRef, ValueInfo)> isExported, std::map< ValueInfo, std::vector< VTableSlotSummary >> &LocalWPDTargetsMap)
Call after cross-module importing to update the recorded single impl devirt target names for any loca...
static ErrorOr< std::unique_ptr< MemoryBuffer > > getOpenFile(sys::fs::file_t FD, const Twine &Filename, uint64_t FileSize, bool RequiresNullTerminator=true, bool IsVolatile=false)
Given an already-open file descriptor, read the file and return a MemoryBuffer.
void emitImports(Module &Module, StringRef OutputName, ModuleSummaryIndex &Index, const lto::InputFile &File)
Compute and emit the imported files for module at ModulePath.
Tagged union holding either a T or a Error.
Definition: APFloat.h:41
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:147
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:297
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:42
LLVM_NODISCARD size_t size() const
size - Get the string size.
Definition: StringRef.h:151
An input file.
Definition: LTO.h:104
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:1248
PassManager manages ModulePassManagers.
std::error_code copy_file(const Twine &From, const Twine &To)
Copy the contents of From to To.
Definition: Path.cpp:987
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:435
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:381
void runWholeProgramDevirtOnIndex(ModuleSummaryIndex &Summary, std::set< GlobalValue::GUID > &ExportedGUIDs, std::map< ValueInfo, std::vector< VTableSlotSummary >> &LocalWPDTargetsMap)
Perform index-based whole program devirtualization on the Summary index.
This is an important class for using LLVM in a threaded context.
Definition: LLVMContext.h:63
Instrumentation for Order File
static GCRegistry::Add< CoreCLRGC > E("coreclr", "CoreCLR-compatible GC")
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:48
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:38
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:358
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:1210
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:634
Function and variable summary information to aid decisions and implementation of importing.
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.
void consumeError(Error Err)
Consume a Error without doing anything.
Definition: Error.h:1001
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:211
Force files Atime to be updated on access. Only makes a difference on windows.
Definition: FileSystem.h:782
Triple - Helper class for working with autoconf configuration names.
Definition: Triple.h:43
void sort(IteratorTy Start, IteratorTy End)
Definition: STLExtras.h:1114
Struct that holds a reference to a particular GUID in a global value summary.
void computeSyntheticCounts(ModuleSummaryIndex &Index)
Compute synthetic function entry counts.
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:924
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:72
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:44
Expected< file_t > openNativeFileForRead(const Twine &Name, OpenFlags Flags=OF_None, SmallVectorImpl< char > *RealPath=nullptr)
Opens the file with the given name in a read-only mode, returning its open file descriptor.
reference get()
Returns a reference to the stored T value.
Definition: Error.h:532
std::error_code closeFile(file_t &F)
Close the file object.
This interface provides simple read-only access to a block of memory, and provides simple methods for...
Definition: MemoryBuffer.h:41
raw_ostream & dbgs()
dbgs() - This returns a reference to a raw_ostream for debugging messages.
Definition: Debug.cpp:132
The access may modify the value stored in memory.
StringMap - This is an unconventional map that is specialized for handling keys that are "strings"...
Definition: StringMap.h:242
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:5106
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:397
const char * c_str()
Definition: SmallString.h:269
size_t size() const
Definition: Module.h:607
#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:226
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).
llvm::Error writeFileAtomically(StringRef TempPathModel, StringRef FinalPath, StringRef Buffer)
Creates a unique file with name according to the given TempPathModel, writes content of Buffer to the...
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:1381
A raw_ostream that writes to an std::string.
Definition: raw_ostream.h:516
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
Error handleErrors(Error E, HandlerTs &&... Hs)
Pass the ErrorInfo(s) contained in E to their respective handlers.
Definition: Error.h:901
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:54
bool Freestanding
Flag to indicate that the optimizer should not assume builtins are present on the target...
Definition: Config.h:56
This pass exposes codegen information to IR-level passes.
#define LLVM_DEBUG(X)
Definition: Debug.h:122
bool exists(const basic_file_status &status)
Does file exist?
Definition: Path.cpp:1048
bool AreStatisticsEnabled()
Check if statistics are enabled.
Definition: Statistic.cpp:131
std::error_code errorToErrorCode(Error Err)
Helper for converting an ECError to a std::error_code.
Definition: Error.cpp:93
iterator end()
Definition: StringMap.h:340
Instances of this class encapsulate one diagnostic report, allowing printing to a raw_ostream as a ca...
Definition: SourceMgr.h:261