LLVM  12.0.0git
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"
32 #include "llvm/IR/Mangler.h"
33 #include "llvm/IR/PassTimingInfo.h"
34 #include "llvm/IR/Verifier.h"
35 #include "llvm/IRReader/IRReader.h"
36 #include "llvm/LTO/LTO.h"
42 #include "llvm/Support/Debug.h"
43 #include "llvm/Support/Error.h"
45 #include "llvm/Support/Path.h"
46 #include "llvm/Support/SHA1.h"
50 #include "llvm/Support/Threading.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
82 }
83 
84 namespace {
85 
86 // Default to using all available threads in the system, but using only one
87 // thred per core, as indicated by the usage of
88 // heavyweight_hardware_concurrency() below.
89 static cl::opt<int> ThreadCount("threads", cl::init(0));
90 
91 // Simple helper to save temporary files for debug.
92 static void saveTempBitcode(const Module &TheModule, StringRef TempDir,
93  unsigned count, StringRef Suffix) {
94  if (TempDir.empty())
95  return;
96  // User asked to save temps, let dump the bitcode file after import.
97  std::string SaveTempPath = (TempDir + llvm::Twine(count) + Suffix).str();
98  std::error_code EC;
99  raw_fd_ostream OS(SaveTempPath, EC, sys::fs::OF_None);
100  if (EC)
101  report_fatal_error(Twine("Failed to open ") + SaveTempPath +
102  " to save optimized bitcode\n");
103  WriteBitcodeToFile(TheModule, OS, /* ShouldPreserveUseListOrder */ true);
104 }
105 
106 static const GlobalValueSummary *
107 getFirstDefinitionForLinker(const GlobalValueSummaryList &GVSummaryList) {
108  // If there is any strong definition anywhere, get it.
109  auto StrongDefForLinker = llvm::find_if(
110  GVSummaryList, [](const std::unique_ptr<GlobalValueSummary> &Summary) {
111  auto Linkage = Summary->linkage();
114  });
115  if (StrongDefForLinker != GVSummaryList.end())
116  return StrongDefForLinker->get();
117  // Get the first *linker visible* definition for this global in the summary
118  // list.
119  auto FirstDefForLinker = llvm::find_if(
120  GVSummaryList, [](const std::unique_ptr<GlobalValueSummary> &Summary) {
121  auto Linkage = Summary->linkage();
123  });
124  // Extern templates can be emitted as available_externally.
125  if (FirstDefForLinker == GVSummaryList.end())
126  return nullptr;
127  return FirstDefForLinker->get();
128 }
129 
130 // Populate map of GUID to the prevailing copy for any multiply defined
131 // symbols. Currently assume first copy is prevailing, or any strong
132 // definition. Can be refined with Linker information in the future.
133 static void computePrevailingCopies(
134  const ModuleSummaryIndex &Index,
136  auto HasMultipleCopies = [&](const GlobalValueSummaryList &GVSummaryList) {
137  return GVSummaryList.size() > 1;
138  };
139 
140  for (auto &I : Index) {
141  if (HasMultipleCopies(I.second.SummaryList))
142  PrevailingCopy[I.first] =
143  getFirstDefinitionForLinker(I.second.SummaryList);
144  }
145 }
146 
148 generateModuleMap(std::vector<std::unique_ptr<lto::InputFile>> &Modules) {
149  StringMap<lto::InputFile *> ModuleMap;
150  for (auto &M : Modules) {
151  assert(ModuleMap.find(M->getName()) == ModuleMap.end() &&
152  "Expect unique Buffer Identifier");
153  ModuleMap[M->getName()] = M.get();
154  }
155  return ModuleMap;
156 }
157 
158 static void promoteModule(Module &TheModule, const ModuleSummaryIndex &Index,
159  bool ClearDSOLocalOnDeclarations) {
160  if (renameModuleForThinLTO(TheModule, Index, ClearDSOLocalOnDeclarations))
161  report_fatal_error("renameModuleForThinLTO failed");
162 }
163 
164 namespace {
165 class ThinLTODiagnosticInfo : public DiagnosticInfo {
166  const Twine &Msg;
167 public:
168  ThinLTODiagnosticInfo(const Twine &DiagMsg,
169  DiagnosticSeverity Severity = DS_Error)
170  : DiagnosticInfo(DK_Linker, Severity), Msg(DiagMsg) {}
171  void print(DiagnosticPrinter &DP) const override { DP << Msg; }
172 };
173 }
174 
175 /// Verify the module and strip broken debug info.
176 static void verifyLoadedModule(Module &TheModule) {
177  bool BrokenDebugInfo = false;
178  if (verifyModule(TheModule, &dbgs(), &BrokenDebugInfo))
179  report_fatal_error("Broken module found, compilation aborted!");
180  if (BrokenDebugInfo) {
181  TheModule.getContext().diagnose(ThinLTODiagnosticInfo(
182  "Invalid debug info found, debug info will be stripped", DS_Warning));
183  StripDebugInfo(TheModule);
184  }
185 }
186 
187 static std::unique_ptr<Module> loadModuleFromInput(lto::InputFile *Input,
189  bool Lazy,
190  bool IsImporting) {
191  auto &Mod = Input->getSingleBitcodeModule();
192  SMDiagnostic Err;
193  Expected<std::unique_ptr<Module>> ModuleOrErr =
194  Lazy ? Mod.getLazyModule(Context,
195  /* ShouldLazyLoadMetadata */ true, IsImporting)
196  : Mod.parseModule(Context);
197  if (!ModuleOrErr) {
198  handleAllErrors(ModuleOrErr.takeError(), [&](ErrorInfoBase &EIB) {
199  SMDiagnostic Err = SMDiagnostic(Mod.getModuleIdentifier(),
200  SourceMgr::DK_Error, EIB.message());
201  Err.print("ThinLTO", errs());
202  });
203  report_fatal_error("Can't load module, abort.");
204  }
205  if (!Lazy)
206  verifyLoadedModule(*ModuleOrErr.get());
207  return std::move(*ModuleOrErr);
208 }
209 
210 static void
211 crossImportIntoModule(Module &TheModule, const ModuleSummaryIndex &Index,
212  StringMap<lto::InputFile *> &ModuleMap,
213  const FunctionImporter::ImportMapTy &ImportList,
214  bool ClearDSOLocalOnDeclarations) {
215  auto Loader = [&](StringRef Identifier) {
216  auto &Input = ModuleMap[Identifier];
217  return loadModuleFromInput(Input, TheModule.getContext(),
218  /*Lazy=*/true, /*IsImporting*/ true);
219  };
220 
221  FunctionImporter Importer(Index, Loader, ClearDSOLocalOnDeclarations);
222  Expected<bool> Result = Importer.importFunctions(TheModule, ImportList);
223  if (!Result) {
224  handleAllErrors(Result.takeError(), [&](ErrorInfoBase &EIB) {
226  SourceMgr::DK_Error, EIB.message());
227  Err.print("ThinLTO", errs());
228  });
229  report_fatal_error("importFunctions failed");
230  }
231  // Verify again after cross-importing.
232  verifyLoadedModule(TheModule);
233 }
234 
235 static void optimizeModule(Module &TheModule, TargetMachine &TM,
236  unsigned OptLevel, bool Freestanding,
238  // Populate the PassManager
239  PassManagerBuilder PMB;
240  PMB.LibraryInfo = new TargetLibraryInfoImpl(TM.getTargetTriple());
241  if (Freestanding)
244  // FIXME: should get it from the bitcode?
245  PMB.OptLevel = OptLevel;
246  PMB.LoopVectorize = true;
247  PMB.SLPVectorize = true;
248  // Already did this in verifyLoadedModule().
249  PMB.VerifyInput = false;
250  PMB.VerifyOutput = false;
251  PMB.ImportSummary = Index;
252 
254 
255  // Add the TTI (required to inform the vectorizer about register size for
256  // instance)
257  PM.add(createTargetTransformInfoWrapperPass(TM.getTargetIRAnalysis()));
258 
259  // Add optimizations
261 
262  PM.run(TheModule);
263 }
264 
265 static void
266 addUsedSymbolToPreservedGUID(const lto::InputFile &File,
267  DenseSet<GlobalValue::GUID> &PreservedGUID) {
268  for (const auto &Sym : File.symbols()) {
269  if (Sym.isUsed())
270  PreservedGUID.insert(GlobalValue::getGUID(Sym.getIRName()));
271  }
272 }
273 
274 // Convert the PreservedSymbols map from "Name" based to "GUID" based.
275 static void computeGUIDPreservedSymbols(const lto::InputFile &File,
276  const StringSet<> &PreservedSymbols,
277  const Triple &TheTriple,
279  // Iterate the symbols in the input file and if the input has preserved symbol
280  // compute the GUID for the symbol.
281  for (const auto &Sym : File.symbols()) {
282  if (PreservedSymbols.count(Sym.getName()) && !Sym.getIRName().empty())
284  Sym.getIRName(), GlobalValue::ExternalLinkage, "")));
285  }
286 }
287 
289 computeGUIDPreservedSymbols(const lto::InputFile &File,
290  const StringSet<> &PreservedSymbols,
291  const Triple &TheTriple) {
292  DenseSet<GlobalValue::GUID> GUIDPreservedSymbols(PreservedSymbols.size());
293  computeGUIDPreservedSymbols(File, PreservedSymbols, TheTriple,
294  GUIDPreservedSymbols);
295  return GUIDPreservedSymbols;
296 }
297 
298 std::unique_ptr<MemoryBuffer> codegenModule(Module &TheModule,
299  TargetMachine &TM) {
300  SmallVector<char, 128> OutputBuffer;
301 
302  // CodeGen
303  {
304  raw_svector_ostream OS(OutputBuffer);
306 
307  // If the bitcode files contain ARC code and were compiled with optimization,
308  // the ObjCARCContractPass must be run, so do it unconditionally here.
310 
311  // Setup the codegen now.
312  if (TM.addPassesToEmitFile(PM, OS, nullptr, CGFT_ObjectFile,
313  /* DisableVerify */ true))
314  report_fatal_error("Failed to setup codegen");
315 
316  // Run codegen now. resulting binary is in OutputBuffer.
317  PM.run(TheModule);
318  }
319  return std::make_unique<SmallVectorMemoryBuffer>(std::move(OutputBuffer));
320 }
321 
322 /// Manage caching for a single Module.
323 class ModuleCacheEntry {
324  SmallString<128> EntryPath;
325 
326 public:
327  // Create a cache entry. This compute a unique hash for the Module considering
328  // the current list of export/import, and offer an interface to query to
329  // access the content in the cache.
330  ModuleCacheEntry(
331  StringRef CachePath, const ModuleSummaryIndex &Index, StringRef ModuleID,
332  const FunctionImporter::ImportMapTy &ImportList,
333  const FunctionImporter::ExportSetTy &ExportList,
334  const std::map<GlobalValue::GUID, GlobalValue::LinkageTypes> &ResolvedODR,
335  const GVSummaryMapTy &DefinedGVSummaries, unsigned OptLevel,
336  bool Freestanding, const TargetMachineBuilder &TMBuilder) {
337  if (CachePath.empty())
338  return;
339 
340  if (!Index.modulePaths().count(ModuleID))
341  // The module does not have an entry, it can't have a hash at all
342  return;
343 
344  if (all_of(Index.getModuleHash(ModuleID),
345  [](uint32_t V) { return V == 0; }))
346  // No hash entry, no caching!
347  return;
348 
349  llvm::lto::Config Conf;
350  Conf.OptLevel = OptLevel;
351  Conf.Options = TMBuilder.Options;
352  Conf.CPU = TMBuilder.MCpu;
353  Conf.MAttrs.push_back(TMBuilder.MAttr);
354  Conf.RelocModel = TMBuilder.RelocModel;
355  Conf.CGOptLevel = TMBuilder.CGOptLevel;
356  Conf.Freestanding = Freestanding;
358  computeLTOCacheKey(Key, Conf, Index, ModuleID, ImportList, ExportList,
359  ResolvedODR, DefinedGVSummaries);
360 
361  // This choice of file name allows the cache to be pruned (see pruneCache()
362  // in include/llvm/Support/CachePruning.h).
363  sys::path::append(EntryPath, CachePath, "llvmcache-" + Key);
364  }
365 
366  // Access the path to this entry in the cache.
367  StringRef getEntryPath() { return EntryPath; }
368 
369  // Try loading the buffer for this cache entry.
370  ErrorOr<std::unique_ptr<MemoryBuffer>> tryLoadingBuffer() {
371  if (EntryPath.empty())
372  return std::error_code();
373  SmallString<64> ResultPath;
375  Twine(EntryPath), sys::fs::OF_UpdateAtime, &ResultPath);
376  if (!FDOrErr)
377  return errorToErrorCode(FDOrErr.takeError());
379  *FDOrErr, EntryPath, /*FileSize=*/-1, /*RequiresNullTerminator=*/false);
380  sys::fs::closeFile(*FDOrErr);
381  return MBOrErr;
382  }
383 
384  // Cache the Produced object file
385  void write(const MemoryBuffer &OutputBuffer) {
386  if (EntryPath.empty())
387  return;
388 
389  // Write to a temporary to avoid race condition
390  SmallString<128> TempFilename;
391  SmallString<128> CachePath(EntryPath);
393  sys::path::append(TempFilename, CachePath, "Thin-%%%%%%.tmp.o");
394 
395  if (auto Err = handleErrors(
396  llvm::writeFileAtomically(TempFilename, EntryPath,
397  OutputBuffer.getBuffer()),
398  [](const llvm::AtomicFileWriteError &E) {
399  std::string ErrorMsgBuffer;
400  llvm::raw_string_ostream S(ErrorMsgBuffer);
401  E.log(S);
402 
403  if (E.Error ==
405  errs() << "Error: " << ErrorMsgBuffer << "\n";
406  report_fatal_error("ThinLTO: Can't get a temporary file");
407  }
408  })) {
409  // FIXME
410  consumeError(std::move(Err));
411  }
412  }
413 };
414 
415 static std::unique_ptr<MemoryBuffer>
416 ProcessThinLTOModule(Module &TheModule, ModuleSummaryIndex &Index,
418  const FunctionImporter::ImportMapTy &ImportList,
419  const FunctionImporter::ExportSetTy &ExportList,
420  const DenseSet<GlobalValue::GUID> &GUIDPreservedSymbols,
421  const GVSummaryMapTy &DefinedGlobals,
422  const ThinLTOCodeGenerator::CachingOptions &CacheOptions,
423  bool DisableCodeGen, StringRef SaveTempsDir,
424  bool Freestanding, unsigned OptLevel, unsigned count) {
425 
426  // "Benchmark"-like optimization: single-source case
427  bool SingleModule = (ModuleMap.size() == 1);
428 
429  // When linking an ELF shared object, dso_local should be dropped. We
430  // conservatively do this for -fpic.
431  bool ClearDSOLocalOnDeclarations =
432  TM.getTargetTriple().isOSBinFormatELF() &&
433  TM.getRelocationModel() != Reloc::Static &&
434  TheModule.getPIELevel() == PIELevel::Default;
435 
436  if (!SingleModule) {
437  promoteModule(TheModule, Index, ClearDSOLocalOnDeclarations);
438 
439  // Apply summary-based prevailing-symbol resolution decisions.
440  thinLTOResolvePrevailingInModule(TheModule, DefinedGlobals);
441 
442  // Save temps: after promotion.
443  saveTempBitcode(TheModule, SaveTempsDir, count, ".1.promoted.bc");
444  }
445 
446  // Be friendly and don't nuke totally the module when the client didn't
447  // supply anything to preserve.
448  if (!ExportList.empty() || !GUIDPreservedSymbols.empty()) {
449  // Apply summary-based internalization decisions.
450  thinLTOInternalizeModule(TheModule, DefinedGlobals);
451  }
452 
453  // Save internalized bitcode
454  saveTempBitcode(TheModule, SaveTempsDir, count, ".2.internalized.bc");
455 
456  if (!SingleModule) {
457  crossImportIntoModule(TheModule, Index, ModuleMap, ImportList,
458  ClearDSOLocalOnDeclarations);
459 
460  // Save temps: after cross-module import.
461  saveTempBitcode(TheModule, SaveTempsDir, count, ".3.imported.bc");
462  }
463 
464  optimizeModule(TheModule, TM, OptLevel, Freestanding, &Index);
465 
466  saveTempBitcode(TheModule, SaveTempsDir, count, ".4.opt.bc");
467 
468  if (DisableCodeGen) {
469  // Configured to stop before CodeGen, serialize the bitcode and return.
470  SmallVector<char, 128> OutputBuffer;
471  {
472  raw_svector_ostream OS(OutputBuffer);
473  ProfileSummaryInfo PSI(TheModule);
474  auto Index = buildModuleSummaryIndex(TheModule, nullptr, &PSI);
475  WriteBitcodeToFile(TheModule, OS, true, &Index);
476  }
477  return std::make_unique<SmallVectorMemoryBuffer>(std::move(OutputBuffer));
478  }
479 
480  return codegenModule(TheModule, TM);
481 }
482 
483 /// Resolve prevailing symbols. Record resolutions in the \p ResolvedODR map
484 /// for caching, and in the \p Index for application during the ThinLTO
485 /// backends. This is needed for correctness for exported symbols (ensure
486 /// at least one copy kept) and a compile-time optimization (to drop duplicate
487 /// copies when possible).
488 static void resolvePrevailingInIndex(
490  StringMap<std::map<GlobalValue::GUID, GlobalValue::LinkageTypes>>
491  &ResolvedODR,
492  const DenseSet<GlobalValue::GUID> &GUIDPreservedSymbols,
494  &PrevailingCopy) {
495 
496  auto isPrevailing = [&](GlobalValue::GUID GUID, const GlobalValueSummary *S) {
497  const auto &Prevailing = PrevailingCopy.find(GUID);
498  // Not in map means that there was only one copy, which must be prevailing.
499  if (Prevailing == PrevailingCopy.end())
500  return true;
501  return Prevailing->second == S;
502  };
503 
504  auto recordNewLinkage = [&](StringRef ModuleIdentifier,
505  GlobalValue::GUID GUID,
506  GlobalValue::LinkageTypes NewLinkage) {
507  ResolvedODR[ModuleIdentifier][GUID] = NewLinkage;
508  };
509 
510  thinLTOResolvePrevailingInIndex(Index, isPrevailing, recordNewLinkage,
511  GUIDPreservedSymbols);
512 }
513 
514 // Initialize the TargetMachine builder for a given Triple
515 static void initTMBuilder(TargetMachineBuilder &TMBuilder,
516  const Triple &TheTriple) {
517  // Set a default CPU for Darwin triples (copied from LTOCodeGenerator).
518  // FIXME this looks pretty terrible...
519  if (TMBuilder.MCpu.empty() && TheTriple.isOSDarwin()) {
520  if (TheTriple.getArch() == llvm::Triple::x86_64)
521  TMBuilder.MCpu = "core2";
522  else if (TheTriple.getArch() == llvm::Triple::x86)
523  TMBuilder.MCpu = "yonah";
524  else if (TheTriple.getArch() == llvm::Triple::aarch64 ||
525  TheTriple.getArch() == llvm::Triple::aarch64_32)
526  TMBuilder.MCpu = "cyclone";
527  }
528  TMBuilder.TheTriple = std::move(TheTriple);
529 }
530 
531 } // end anonymous namespace
532 
534  MemoryBufferRef Buffer(Data, Identifier);
535 
536  auto InputOrError = lto::InputFile::create(Buffer);
537  if (!InputOrError)
538  report_fatal_error("ThinLTO cannot create input file: " +
539  toString(InputOrError.takeError()));
540 
541  auto TripleStr = (*InputOrError)->getTargetTriple();
542  Triple TheTriple(TripleStr);
543 
544  if (Modules.empty())
545  initTMBuilder(TMBuilder, Triple(TheTriple));
546  else if (TMBuilder.TheTriple != TheTriple) {
547  if (!TMBuilder.TheTriple.isCompatibleWith(TheTriple))
548  report_fatal_error("ThinLTO modules with incompatible triples not "
549  "supported");
550  initTMBuilder(TMBuilder, Triple(TMBuilder.TheTriple.merge(TheTriple)));
551  }
552 
553  Modules.emplace_back(std::move(*InputOrError));
554 }
555 
557  PreservedSymbols.insert(Name);
558 }
559 
561  // FIXME: At the moment, we don't take advantage of this extra information,
562  // we're conservatively considering cross-references as preserved.
563  // CrossReferencedSymbols.insert(Name);
564  PreservedSymbols.insert(Name);
565 }
566 
567 // TargetMachine factory
568 std::unique_ptr<TargetMachine> TargetMachineBuilder::create() const {
569  std::string ErrMsg;
570  const Target *TheTarget =
572  if (!TheTarget) {
573  report_fatal_error("Can't load target for this Triple: " + ErrMsg);
574  }
575 
576  // Use MAttr as the default set of features.
577  SubtargetFeatures Features(MAttr);
579  std::string FeatureStr = Features.getString();
580 
581  std::unique_ptr<TargetMachine> TM(
582  TheTarget->createTargetMachine(TheTriple.str(), MCpu, FeatureStr, Options,
584  assert(TM && "Cannot create target machine");
585 
586  return TM;
587 }
588 
589 /**
590  * Produce the combined summary index from all the bitcode files:
591  * "thin-link".
592  */
593 std::unique_ptr<ModuleSummaryIndex> ThinLTOCodeGenerator::linkCombinedIndex() {
594  std::unique_ptr<ModuleSummaryIndex> CombinedIndex =
595  std::make_unique<ModuleSummaryIndex>(/*HaveGVs=*/false);
596  uint64_t NextModuleId = 0;
597  for (auto &Mod : Modules) {
598  auto &M = Mod->getSingleBitcodeModule();
599  if (Error Err =
600  M.readSummary(*CombinedIndex, Mod->getName(), NextModuleId++)) {
601  // FIXME diagnose
603  std::move(Err), errs(),
604  "error: can't create module summary index for buffer: ");
605  return nullptr;
606  }
607  }
608  return CombinedIndex;
609 }
610 
611 namespace {
612 struct IsExported {
613  const StringMap<FunctionImporter::ExportSetTy> &ExportLists;
614  const DenseSet<GlobalValue::GUID> &GUIDPreservedSymbols;
615 
616  IsExported(const StringMap<FunctionImporter::ExportSetTy> &ExportLists,
617  const DenseSet<GlobalValue::GUID> &GUIDPreservedSymbols)
618  : ExportLists(ExportLists), GUIDPreservedSymbols(GUIDPreservedSymbols) {}
619 
620  bool operator()(StringRef ModuleIdentifier, ValueInfo VI) const {
621  const auto &ExportList = ExportLists.find(ModuleIdentifier);
622  return (ExportList != ExportLists.end() && ExportList->second.count(VI)) ||
623  GUIDPreservedSymbols.count(VI.getGUID());
624  }
625 };
626 
627 struct IsPrevailing {
630  &PrevailingCopy)
631  : PrevailingCopy(PrevailingCopy) {}
632 
633  bool operator()(GlobalValue::GUID GUID, const GlobalValueSummary *S) const {
634  const auto &Prevailing = PrevailingCopy.find(GUID);
635  // Not in map means that there was only one copy, which must be prevailing.
636  if (Prevailing == PrevailingCopy.end())
637  return true;
638  return Prevailing->second == S;
639  };
640 };
641 } // namespace
642 
645  const DenseSet<GlobalValue::GUID> &GUIDPreservedSymbols) {
646  // We have no symbols resolution available. And can't do any better now in the
647  // case where the prevailing symbol is in a native object. It can be refined
648  // with linker information in the future.
649  auto isPrevailing = [&](GlobalValue::GUID G) {
651  };
652  computeDeadSymbolsWithConstProp(Index, GUIDPreservedSymbols, isPrevailing,
653  /* ImportEnabled = */ true);
654 }
655 
656 /**
657  * Perform promotion and renaming of exported internal functions.
658  * Index is updated to reflect linkage changes from weak resolution.
659  */
661  const lto::InputFile &File) {
662  auto ModuleCount = Index.modulePaths().size();
663  auto ModuleIdentifier = TheModule.getModuleIdentifier();
664 
665  // Collect for each module the list of function it defines (GUID -> Summary).
666  StringMap<GVSummaryMapTy> ModuleToDefinedGVSummaries;
667  Index.collectDefinedGVSummariesPerModule(ModuleToDefinedGVSummaries);
668 
669  // Convert the preserved symbols set from string to GUID
670  auto GUIDPreservedSymbols = computeGUIDPreservedSymbols(
671  File, PreservedSymbols, Triple(TheModule.getTargetTriple()));
672 
673  // Add used symbol to the preserved symbols.
674  addUsedSymbolToPreservedGUID(File, GUIDPreservedSymbols);
675 
676  // Compute "dead" symbols, we don't want to import/export these!
677  computeDeadSymbolsInIndex(Index, GUIDPreservedSymbols);
678 
679  // Generate import/export list
680  StringMap<FunctionImporter::ImportMapTy> ImportLists(ModuleCount);
681  StringMap<FunctionImporter::ExportSetTy> ExportLists(ModuleCount);
682  ComputeCrossModuleImport(Index, ModuleToDefinedGVSummaries, ImportLists,
683  ExportLists);
684 
686  computePrevailingCopies(Index, PrevailingCopy);
687 
688  // Resolve prevailing symbols
690  resolvePrevailingInIndex(Index, ResolvedODR, GUIDPreservedSymbols,
691  PrevailingCopy);
692 
694  TheModule, ModuleToDefinedGVSummaries[ModuleIdentifier]);
695 
696  // Promote the exported values in the index, so that they are promoted
697  // in the module.
699  Index, IsExported(ExportLists, GUIDPreservedSymbols),
700  IsPrevailing(PrevailingCopy));
701 
702  // FIXME Set ClearDSOLocalOnDeclarations.
703  promoteModule(TheModule, Index, /*ClearDSOLocalOnDeclarations=*/false);
704 }
705 
706 /**
707  * Perform cross-module importing for the module identified by ModuleIdentifier.
708  */
711  const lto::InputFile &File) {
712  auto ModuleMap = generateModuleMap(Modules);
713  auto ModuleCount = Index.modulePaths().size();
714 
715  // Collect for each module the list of function it defines (GUID -> Summary).
716  StringMap<GVSummaryMapTy> ModuleToDefinedGVSummaries(ModuleCount);
717  Index.collectDefinedGVSummariesPerModule(ModuleToDefinedGVSummaries);
718 
719  // Convert the preserved symbols set from string to GUID
720  auto GUIDPreservedSymbols = computeGUIDPreservedSymbols(
721  File, PreservedSymbols, Triple(TheModule.getTargetTriple()));
722 
723  addUsedSymbolToPreservedGUID(File, GUIDPreservedSymbols);
724 
725  // Compute "dead" symbols, we don't want to import/export these!
726  computeDeadSymbolsInIndex(Index, GUIDPreservedSymbols);
727 
728  // Generate import/export list
729  StringMap<FunctionImporter::ImportMapTy> ImportLists(ModuleCount);
730  StringMap<FunctionImporter::ExportSetTy> ExportLists(ModuleCount);
731  ComputeCrossModuleImport(Index, ModuleToDefinedGVSummaries, ImportLists,
732  ExportLists);
733  auto &ImportList = ImportLists[TheModule.getModuleIdentifier()];
734 
735  // FIXME Set ClearDSOLocalOnDeclarations.
736  crossImportIntoModule(TheModule, Index, ModuleMap, ImportList,
737  /*ClearDSOLocalOnDeclarations=*/false);
738 }
739 
740 /**
741  * Compute the list of summaries needed for importing into module.
742  */
744  Module &TheModule, ModuleSummaryIndex &Index,
745  std::map<std::string, GVSummaryMapTy> &ModuleToSummariesForIndex,
746  const lto::InputFile &File) {
747  auto ModuleCount = Index.modulePaths().size();
748  auto ModuleIdentifier = TheModule.getModuleIdentifier();
749 
750  // Collect for each module the list of function it defines (GUID -> Summary).
751  StringMap<GVSummaryMapTy> ModuleToDefinedGVSummaries(ModuleCount);
752  Index.collectDefinedGVSummariesPerModule(ModuleToDefinedGVSummaries);
753 
754  // Convert the preserved symbols set from string to GUID
755  auto GUIDPreservedSymbols = computeGUIDPreservedSymbols(
756  File, PreservedSymbols, Triple(TheModule.getTargetTriple()));
757 
758  addUsedSymbolToPreservedGUID(File, GUIDPreservedSymbols);
759 
760  // Compute "dead" symbols, we don't want to import/export these!
761  computeDeadSymbolsInIndex(Index, GUIDPreservedSymbols);
762 
763  // Generate import/export list
764  StringMap<FunctionImporter::ImportMapTy> ImportLists(ModuleCount);
765  StringMap<FunctionImporter::ExportSetTy> ExportLists(ModuleCount);
766  ComputeCrossModuleImport(Index, ModuleToDefinedGVSummaries, ImportLists,
767  ExportLists);
768 
770  ModuleIdentifier, ModuleToDefinedGVSummaries,
771  ImportLists[ModuleIdentifier], ModuleToSummariesForIndex);
772 }
773 
774 /**
775  * Emit the list of files needed for importing into module.
776  */
779  const lto::InputFile &File) {
780  auto ModuleCount = Index.modulePaths().size();
781  auto ModuleIdentifier = TheModule.getModuleIdentifier();
782 
783  // Collect for each module the list of function it defines (GUID -> Summary).
784  StringMap<GVSummaryMapTy> ModuleToDefinedGVSummaries(ModuleCount);
785  Index.collectDefinedGVSummariesPerModule(ModuleToDefinedGVSummaries);
786 
787  // Convert the preserved symbols set from string to GUID
788  auto GUIDPreservedSymbols = computeGUIDPreservedSymbols(
789  File, PreservedSymbols, Triple(TheModule.getTargetTriple()));
790 
791  addUsedSymbolToPreservedGUID(File, GUIDPreservedSymbols);
792 
793  // Compute "dead" symbols, we don't want to import/export these!
794  computeDeadSymbolsInIndex(Index, GUIDPreservedSymbols);
795 
796  // Generate import/export list
797  StringMap<FunctionImporter::ImportMapTy> ImportLists(ModuleCount);
798  StringMap<FunctionImporter::ExportSetTy> ExportLists(ModuleCount);
799  ComputeCrossModuleImport(Index, ModuleToDefinedGVSummaries, ImportLists,
800  ExportLists);
801 
802  std::map<std::string, GVSummaryMapTy> ModuleToSummariesForIndex;
804  ModuleIdentifier, ModuleToDefinedGVSummaries,
805  ImportLists[ModuleIdentifier], ModuleToSummariesForIndex);
806 
807  std::error_code EC;
808  if ((EC = EmitImportsFiles(ModuleIdentifier, OutputName,
809  ModuleToSummariesForIndex)))
810  report_fatal_error(Twine("Failed to open ") + OutputName +
811  " to save imports lists\n");
812 }
813 
814 /**
815  * Perform internalization. Runs promote and internalization together.
816  * Index is updated to reflect linkage changes.
817  */
820  const lto::InputFile &File) {
821  initTMBuilder(TMBuilder, Triple(TheModule.getTargetTriple()));
822  auto ModuleCount = Index.modulePaths().size();
823  auto ModuleIdentifier = TheModule.getModuleIdentifier();
824 
825  // Convert the preserved symbols set from string to GUID
826  auto GUIDPreservedSymbols =
827  computeGUIDPreservedSymbols(File, PreservedSymbols, TMBuilder.TheTriple);
828 
829  addUsedSymbolToPreservedGUID(File, GUIDPreservedSymbols);
830 
831  // Collect for each module the list of function it defines (GUID -> Summary).
832  StringMap<GVSummaryMapTy> ModuleToDefinedGVSummaries(ModuleCount);
833  Index.collectDefinedGVSummariesPerModule(ModuleToDefinedGVSummaries);
834 
835  // Compute "dead" symbols, we don't want to import/export these!
836  computeDeadSymbolsInIndex(Index, GUIDPreservedSymbols);
837 
838  // Generate import/export list
839  StringMap<FunctionImporter::ImportMapTy> ImportLists(ModuleCount);
840  StringMap<FunctionImporter::ExportSetTy> ExportLists(ModuleCount);
841  ComputeCrossModuleImport(Index, ModuleToDefinedGVSummaries, ImportLists,
842  ExportLists);
843  auto &ExportList = ExportLists[ModuleIdentifier];
844 
845  // Be friendly and don't nuke totally the module when the client didn't
846  // supply anything to preserve.
847  if (ExportList.empty() && GUIDPreservedSymbols.empty())
848  return;
849 
851  computePrevailingCopies(Index, PrevailingCopy);
852 
853  // Resolve prevailing symbols
855  resolvePrevailingInIndex(Index, ResolvedODR, GUIDPreservedSymbols,
856  PrevailingCopy);
857 
858  // Promote the exported values in the index, so that they are promoted
859  // in the module.
861  Index, IsExported(ExportLists, GUIDPreservedSymbols),
862  IsPrevailing(PrevailingCopy));
863 
864  // FIXME Set ClearDSOLocalOnDeclarations.
865  promoteModule(TheModule, Index, /*ClearDSOLocalOnDeclarations=*/false);
866 
867  // Internalization
869  TheModule, ModuleToDefinedGVSummaries[ModuleIdentifier]);
870 
871  thinLTOInternalizeModule(TheModule,
872  ModuleToDefinedGVSummaries[ModuleIdentifier]);
873 }
874 
875 /**
876  * Perform post-importing ThinLTO optimizations.
877  */
879  initTMBuilder(TMBuilder, Triple(TheModule.getTargetTriple()));
880 
881  // Optimize now
882  optimizeModule(TheModule, *TMBuilder.create(), OptLevel, Freestanding,
883  nullptr);
884 }
885 
886 /// Write out the generated object file, either from CacheEntryPath or from
887 /// OutputBuffer, preferring hard-link when possible.
888 /// Returns the path to the generated file in SavedObjectsDirectoryPath.
889 std::string
891  const MemoryBuffer &OutputBuffer) {
892  auto ArchName = TMBuilder.TheTriple.getArchName();
893  SmallString<128> OutputPath(SavedObjectsDirectoryPath);
894  llvm::sys::path::append(OutputPath,
895  Twine(count) + "." + ArchName + ".thinlto.o");
896  OutputPath.c_str(); // Ensure the string is null terminated.
897  if (sys::fs::exists(OutputPath))
898  sys::fs::remove(OutputPath);
899 
900  // We don't return a memory buffer to the linker, just a list of files.
901  if (!CacheEntryPath.empty()) {
902  // Cache is enabled, hard-link the entry (or copy if hard-link fails).
903  auto Err = sys::fs::create_hard_link(CacheEntryPath, OutputPath);
904  if (!Err)
905  return std::string(OutputPath.str());
906  // Hard linking failed, try to copy.
907  Err = sys::fs::copy_file(CacheEntryPath, OutputPath);
908  if (!Err)
909  return std::string(OutputPath.str());
910  // Copy failed (could be because the CacheEntry was removed from the cache
911  // in the meantime by another process), fall back and try to write down the
912  // buffer to the output.
913  errs() << "remark: can't link or copy from cached entry '" << CacheEntryPath
914  << "' to '" << OutputPath << "'\n";
915  }
916  // No cache entry, just write out the buffer.
917  std::error_code Err;
918  raw_fd_ostream OS(OutputPath, Err, sys::fs::OF_None);
919  if (Err)
920  report_fatal_error("Can't open output '" + OutputPath + "'\n");
921  OS << OutputBuffer.getBuffer();
922  return std::string(OutputPath.str());
923 }
924 
925 // Main entry point for the ThinLTO processing
927  // Prepare the resulting object vector
928  assert(ProducedBinaries.empty() && "The generator should not be reused");
929  if (SavedObjectsDirectoryPath.empty())
930  ProducedBinaries.resize(Modules.size());
931  else {
932  sys::fs::create_directories(SavedObjectsDirectoryPath);
933  bool IsDir;
934  sys::fs::is_directory(SavedObjectsDirectoryPath, IsDir);
935  if (!IsDir)
936  report_fatal_error("Unexistent dir: '" + SavedObjectsDirectoryPath + "'");
937  ProducedBinaryFiles.resize(Modules.size());
938  }
939 
940  if (CodeGenOnly) {
941  // Perform only parallel codegen and return.
942  ThreadPool Pool;
943  int count = 0;
944  for (auto &Mod : Modules) {
945  Pool.async([&](int count) {
948 
949  // Parse module now
950  auto TheModule = loadModuleFromInput(Mod.get(), Context, false,
951  /*IsImporting*/ false);
952 
953  // CodeGen
954  auto OutputBuffer = codegenModule(*TheModule, *TMBuilder.create());
955  if (SavedObjectsDirectoryPath.empty())
956  ProducedBinaries[count] = std::move(OutputBuffer);
957  else
958  ProducedBinaryFiles[count] =
959  writeGeneratedObject(count, "", *OutputBuffer);
960  }, count++);
961  }
962 
963  return;
964  }
965 
966  // Sequential linking phase
967  auto Index = linkCombinedIndex();
968 
969  // Save temps: index.
970  if (!SaveTempsDir.empty()) {
971  auto SaveTempPath = SaveTempsDir + "index.bc";
972  std::error_code EC;
973  raw_fd_ostream OS(SaveTempPath, EC, sys::fs::OF_None);
974  if (EC)
975  report_fatal_error(Twine("Failed to open ") + SaveTempPath +
976  " to save optimized bitcode\n");
977  WriteIndexToFile(*Index, OS);
978  }
979 
980 
981  // Prepare the module map.
982  auto ModuleMap = generateModuleMap(Modules);
983  auto ModuleCount = Modules.size();
984 
985  // Collect for each module the list of function it defines (GUID -> Summary).
986  StringMap<GVSummaryMapTy> ModuleToDefinedGVSummaries(ModuleCount);
987  Index->collectDefinedGVSummariesPerModule(ModuleToDefinedGVSummaries);
988 
989  // Convert the preserved symbols set from string to GUID, this is needed for
990  // computing the caching hash and the internalization.
991  DenseSet<GlobalValue::GUID> GUIDPreservedSymbols;
992  for (const auto &M : Modules)
993  computeGUIDPreservedSymbols(*M, PreservedSymbols, TMBuilder.TheTriple,
994  GUIDPreservedSymbols);
995 
996  // Add used symbol from inputs to the preserved symbols.
997  for (const auto &M : Modules)
998  addUsedSymbolToPreservedGUID(*M, GUIDPreservedSymbols);
999 
1000  // Compute "dead" symbols, we don't want to import/export these!
1001  computeDeadSymbolsInIndex(*Index, GUIDPreservedSymbols);
1002 
1003  // Synthesize entry counts for functions in the combined index.
1005 
1006  // Currently there is no support for enabling whole program visibility via a
1007  // linker option in the old LTO API, but this call allows it to be specified
1008  // via the internal option. Must be done before WPD below.
1010  /* WholeProgramVisibilityEnabledInLTO */ false);
1011 
1012  // Perform index-based WPD. This will return immediately if there are
1013  // no index entries in the typeIdMetadata map (e.g. if we are instead
1014  // performing IR-based WPD in hybrid regular/thin LTO mode).
1015  std::map<ValueInfo, std::vector<VTableSlotSummary>> LocalWPDTargetsMap;
1016  std::set<GlobalValue::GUID> ExportedGUIDs;
1017  runWholeProgramDevirtOnIndex(*Index, ExportedGUIDs, LocalWPDTargetsMap);
1018  for (auto GUID : ExportedGUIDs)
1019  GUIDPreservedSymbols.insert(GUID);
1020 
1021  // Collect the import/export lists for all modules from the call-graph in the
1022  // combined index.
1023  StringMap<FunctionImporter::ImportMapTy> ImportLists(ModuleCount);
1024  StringMap<FunctionImporter::ExportSetTy> ExportLists(ModuleCount);
1025  ComputeCrossModuleImport(*Index, ModuleToDefinedGVSummaries, ImportLists,
1026  ExportLists);
1027 
1028  // We use a std::map here to be able to have a defined ordering when
1029  // producing a hash for the cache entry.
1030  // FIXME: we should be able to compute the caching hash for the entry based
1031  // on the index, and nuke this map.
1033 
1035  computePrevailingCopies(*Index, PrevailingCopy);
1036 
1037  // Resolve prevailing symbols, this has to be computed early because it
1038  // impacts the caching.
1039  resolvePrevailingInIndex(*Index, ResolvedODR, GUIDPreservedSymbols,
1040  PrevailingCopy);
1041 
1042  // Use global summary-based analysis to identify symbols that can be
1043  // internalized (because they aren't exported or preserved as per callback).
1044  // Changes are made in the index, consumed in the ThinLTO backends.
1046  IsExported(ExportLists, GUIDPreservedSymbols),
1047  LocalWPDTargetsMap);
1049  *Index, IsExported(ExportLists, GUIDPreservedSymbols),
1050  IsPrevailing(PrevailingCopy));
1051 
1052  // Make sure that every module has an entry in the ExportLists, ImportList,
1053  // GVSummary and ResolvedODR maps to enable threaded access to these maps
1054  // below.
1055  for (auto &Module : Modules) {
1056  auto ModuleIdentifier = Module->getName();
1057  ExportLists[ModuleIdentifier];
1058  ImportLists[ModuleIdentifier];
1059  ResolvedODR[ModuleIdentifier];
1060  ModuleToDefinedGVSummaries[ModuleIdentifier];
1061  }
1062 
1063  std::vector<BitcodeModule *> ModulesVec;
1064  ModulesVec.reserve(Modules.size());
1065  for (auto &Mod : Modules)
1066  ModulesVec.push_back(&Mod->getSingleBitcodeModule());
1067  std::vector<int> ModulesOrdering = lto::generateModulesOrdering(ModulesVec);
1068 
1069  // Parallel optimizer + codegen
1070  {
1071  ThreadPool Pool(heavyweight_hardware_concurrency(ThreadCount));
1072  for (auto IndexCount : ModulesOrdering) {
1073  auto &Mod = Modules[IndexCount];
1074  Pool.async([&](int count) {
1075  auto ModuleIdentifier = Mod->getName();
1076  auto &ExportList = ExportLists[ModuleIdentifier];
1077 
1078  auto &DefinedGVSummaries = ModuleToDefinedGVSummaries[ModuleIdentifier];
1079 
1080  // The module may be cached, this helps handling it.
1081  ModuleCacheEntry CacheEntry(CacheOptions.Path, *Index, ModuleIdentifier,
1082  ImportLists[ModuleIdentifier], ExportList,
1083  ResolvedODR[ModuleIdentifier],
1084  DefinedGVSummaries, OptLevel, Freestanding,
1085  TMBuilder);
1086  auto CacheEntryPath = CacheEntry.getEntryPath();
1087 
1088  {
1089  auto ErrOrBuffer = CacheEntry.tryLoadingBuffer();
1090  LLVM_DEBUG(dbgs() << "Cache " << (ErrOrBuffer ? "hit" : "miss")
1091  << " '" << CacheEntryPath << "' for buffer "
1092  << count << " " << ModuleIdentifier << "\n");
1093 
1094  if (ErrOrBuffer) {
1095  // Cache Hit!
1096  if (SavedObjectsDirectoryPath.empty())
1097  ProducedBinaries[count] = std::move(ErrOrBuffer.get());
1098  else
1099  ProducedBinaryFiles[count] = writeGeneratedObject(
1100  count, CacheEntryPath, *ErrOrBuffer.get());
1101  return;
1102  }
1103  }
1104 
1108  auto DiagFileOrErr = lto::setupLLVMOptimizationRemarks(
1111  if (!DiagFileOrErr) {
1112  errs() << "Error: " << toString(DiagFileOrErr.takeError()) << "\n";
1113  report_fatal_error("ThinLTO: Can't get an output file for the "
1114  "remarks");
1115  }
1116 
1117  // Parse module now
1118  auto TheModule = loadModuleFromInput(Mod.get(), Context, false,
1119  /*IsImporting*/ false);
1120 
1121  // Save temps: original file.
1122  saveTempBitcode(*TheModule, SaveTempsDir, count, ".0.original.bc");
1123 
1124  auto &ImportList = ImportLists[ModuleIdentifier];
1125  // Run the main process now, and generates a binary
1126  auto OutputBuffer = ProcessThinLTOModule(
1127  *TheModule, *Index, ModuleMap, *TMBuilder.create(), ImportList,
1128  ExportList, GUIDPreservedSymbols,
1129  ModuleToDefinedGVSummaries[ModuleIdentifier], CacheOptions,
1130  DisableCodeGen, SaveTempsDir, Freestanding, OptLevel, count);
1131 
1132  // Commit to the cache (if enabled)
1133  CacheEntry.write(*OutputBuffer);
1134 
1135  if (SavedObjectsDirectoryPath.empty()) {
1136  // We need to generated a memory buffer for the linker.
1137  if (!CacheEntryPath.empty()) {
1138  // When cache is enabled, reload from the cache if possible.
1139  // Releasing the buffer from the heap and reloading it from the
1140  // cache file with mmap helps us to lower memory pressure.
1141  // The freed memory can be used for the next input file.
1142  // The final binary link will read from the VFS cache (hopefully!)
1143  // or from disk (if the memory pressure was too high).
1144  auto ReloadedBufferOrErr = CacheEntry.tryLoadingBuffer();
1145  if (auto EC = ReloadedBufferOrErr.getError()) {
1146  // On error, keep the preexisting buffer and print a diagnostic.
1147  errs() << "remark: can't reload cached file '" << CacheEntryPath
1148  << "': " << EC.message() << "\n";
1149  } else {
1150  OutputBuffer = std::move(*ReloadedBufferOrErr);
1151  }
1152  }
1153  ProducedBinaries[count] = std::move(OutputBuffer);
1154  return;
1155  }
1156  ProducedBinaryFiles[count] = writeGeneratedObject(
1157  count, CacheEntryPath, *OutputBuffer);
1158  }, IndexCount);
1159  }
1160  }
1161 
1162  pruneCache(CacheOptions.Path, CacheOptions.Policy);
1163 
1164  // If statistics were requested, print them out now.
1168 }
bool isOSDarwin() const
isOSDarwin - Is this a "Darwin" OS (macOS, iOS, tvOS or watchOS).
Definition: Triple.h:481
std::error_code create_directories(const Twine &path, bool IgnoreExisting=true, perms Perms=owner_all|group_all)
Create all the non-existent directories in path.
Definition: Path.cpp:965
const std::string & getTargetTriple() const
Get the target triple which is a string describing the target host.
Definition: Module.h:249
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:472
void thinLTOResolvePrevailingInModule(Module &TheModule, const GVSummaryMapTy &DefinedGlobals)
Resolve prevailing symbol linkages in TheModule based on the information recorded in the summaries du...
ModuleSummaryIndex buildModuleSummaryIndex(const Module &M, std::function< BlockFrequencyInfo *(const Function &F)> GetBFICallback, ProfileSummaryInfo *PSI, std::function< const StackSafetyInfo *(const Function &F)> GetSSICallback=[](const Function &F) -> const StackSafetyInfo *{ return nullptr;})
Direct function to compute a ModuleSummaryIndex from a given module.
std::string CPU
Definition: Config.h:42
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:431
LLVMContext & Context
CodeGenOpt::Level CGOptLevel
Definition: Config.h:48
PassManagerBuilder - This class is used to set up a standard optimization sequence for languages like...
BitcodeModule & getSingleBitcodeModule()
Definition: LTO.cpp:477
StringRef getBuffer() const
Definition: MemoryBuffer.h:69
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:503
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.
DEBUG_TYPE to vector
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:67
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.
LLVM_NODISCARD bool empty() const
Definition: SmallVector.h:75
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:268
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:218
Externally visible function.
Definition: GlobalValue.h:48
const ModuleSummaryIndex * ImportSummary
The module summary index to use for importing information to the thin LTO backends,...
A raw_ostream that writes to an SmallVector or SmallString.
Definition: raw_ostream.h:636
bool is_directory(const basic_file_status &status)
Does status represent a directory?
Definition: Path.cpp:1090
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:1498
bool run(Module &M)
run - Execute all of the passes scheduled for execution.
StringRef getName() const
Get a short "name" for the module.
Definition: Module.h:236
static Expected< std::unique_ptr< InputFile > > create(MemoryBufferRef Object)
Create an InputFile.
Definition: LTO.cpp:444
Error takeError()
Take ownership of the stored error.
Definition: Error.h:557
static const Target * lookupTarget(const std::string &Triple, std::string &Error)
lookupTarget - Lookup a target based on a target triple.
std::string getGlobalIdentifier() const
Return the modified name for this global value suitable to be used as the key for a global lookup (e....
Definition: Globals.cpp:157
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).
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:454
std::string toString(Error E)
Write all error messages (if any) in E to a string.
Definition: Error.h:991
DiagnosticSeverity
Defines the different supported severity of a diagnostic.
unsigned size() const
Definition: StringMap.h:93
Twine - A lightweight data structure for efficiently representing the concatenation of temporary valu...
Definition: Twine.h:80
std::vector< std::string > MAttrs
Definition: Config.h:44
LLVMContext & getContext() const
Get the global data context.
Definition: Module.h:253
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:42
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:156
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:292
void run()
Process all the modules that were added to the code generator in parallel.
Key
PAL metadata keys.
void updateVCallVisibilityInIndex(ModuleSummaryIndex &Index, bool WholeProgramVisibilityEnabledInLTO)
If whole program visibility asserted, then upgrade all public vcall visibility metadata on vtable def...
ArchType getArch() const
getArch - Get the parsed architecture type of this triple.
Definition: Triple.h:304
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:358
TargetOptions Options
Definition: Config.h:43
An input file.
Definition: LTO.h:104
A ThreadPool for asynchronous parallel execution on a defined number of threads.
Definition: ThreadPool.h:37
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...
const std::string & str() const
Definition: Triple.h:373
PassManager manages ModulePassManagers.
std::error_code copy_file(const Twine &From, const Twine &To)
Copy the contents of From to To.
Definition: Path.cpp:1014
std::vector< int > generateModulesOrdering(ArrayRef< BitcodeModule * > R)
Produces a container ordering for optimal multi-threaded processing.
Definition: LTO.cpp:1534
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
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.
PIELevel::Level getPIELevel() const
Returns the PIE level (small or large model)
Definition: Module.cpp:546
size_type count(StringRef Key) const
count - Return 1 if the element is in the map, 0 otherwise.
Definition: StringMap.h:246
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:68
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.
unsigned OptLevel
Definition: Config.h:50
bool isCompatibleWith(const Triple &Other) const
Test whether target triples are compatible.
Definition: Triple.cpp:1606
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.
cl::opt< Optional< uint64_t >, false, remarks::HotnessThresholdParser > RemarksHotnessThreshold("lto-pass-remarks-hotness-threshold", cl::desc("Minimum profile count required for an " "optimization remark to be output." " Use 'auto' to apply the threshold from profile summary."), cl::value_desc("uint or 'auto'"), cl::init(0), cl::Hidden)
bool isWeakForLinker() const
Definition: GlobalValue.h:467
bool renameModuleForThinLTO(Module &M, const ModuleSummaryIndex &Index, bool ClearDSOLocalOnDeclarations, SetVector< GlobalValue * > *GlobalsToImport=nullptr)
Perform in-place global value handling on the given Module for exported local functions renamed and p...
LTO configuration.
Definition: Config.h:39
static void print(raw_ostream &Out, object::Archive::Kind Kind, T Val)
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:371
raw_fd_ostream & errs()
This returns a reference to a raw_ostream for standard error.
std::error_code create_hard_link(const Twine &to, const Twine &from)
Create a hard link from from to to, or return an error.
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:1006
bool pruneCache(StringRef Path, CachePruningPolicy Policy)
Peform pruning using the supplied policy, returns true if pruning occurred, i.e.
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:219
Force files Atime to be updated on access. Only makes a difference on windows.
Definition: FileSystem.h:762
std::pair< typename Base::iterator, bool > insert(StringRef key)
Definition: StringSet.h:33
Triple - Helper class for working with autoconf configuration names.
Definition: Triple.h:45
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:929
StringRef getArchName() const
getArchName - Get the architecture (first) component of the triple.
Definition: Triple.cpp:1012
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:75
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small.
Definition: SmallVector.h:1116
uint32_t Index
const DataFlowGraph & G
Definition: RDFGraph.cpp:202
static bool isAvailableExternallyLinkage(LinkageTypes Linkage)
Definition: GlobalValue.h:302
Optional< Reloc::Model > RelocModel
Definition: Config.h:46
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:537
Expected< std::unique_ptr< ToolOutputFile > > setupLLVMOptimizationRemarks(LLVMContext &Context, StringRef RemarksFilename, StringRef RemarksPasses, StringRef RemarksFormat, bool RemarksWithHotness, Optional< uint64_t > RemarksHotnessThreshold=0, int Count=-1)
Setup optimization remarks.
Definition: LTO.cpp:1490
std::error_code closeFile(file_t &F)
Close the file object.
std::string merge(const Triple &Other) const
Merge target triples.
Definition: Triple.cpp:1630
This interface provides simple read-only access to a block of memory, and provides simple methods for...
Definition: MemoryBuffer.h:50
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:108
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 ...
void add(Pass *P) override
Add a pass to the queue of passes to run.
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:511
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:5531
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:425
const char * c_str()
Definition: SmallString.h:295
size_t size() const
Definition: Module.h:614
#define I(x, y, z)
Definition: MD5.cpp:59
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:97
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.
auto count(R &&Range, const E &Element)
Wrapper function around std::count to count the number of times an element Element occurs in the give...
Definition: STLExtras.h:1581
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())
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...
A raw_ostream that writes to an std::string.
Definition: raw_ostream.h:607
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:22
Error handleErrors(Error E, HandlerTs &&... Hs)
Pass the ErrorInfo(s) contained in E to their respective handlers.
Definition: Error.h:906
Primary interface to the complete machine description for the target machine.
Definition: TargetMachine.h:77
This file implements a simple parser to decode commandline option for remarks hotness threshold that ...
ThreadPoolStrategy heavyweight_hardware_concurrency(unsigned ThreadCount=0)
Returns a thread strategy for tasks requiring significant memory or other resources.
Definition: Threading.h:194
OutputIt move(R &&Range, OutputIt Out)
Provide wrappers to std::move which take ranges instead of having to pass begin/end explicitly.
Definition: STLExtras.h:1556
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:57
bool Freestanding
Flag to indicate that the optimizer should not assume builtins are present on the target.
Definition: Config.h:58
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:1075
auto find_if(R &&Range, UnaryPredicate P)
Provide wrappers to std::find_if which take ranges instead of having to pass begin/end explicitly.
Definition: STLExtras.h:1525
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:205
Instances of this class encapsulate one diagnostic report, allowing printing to a raw_ostream as a ca...
Definition: SourceMgr.h:251