LLVM  10.0.0svn
ThinLTOCodeGenerator.cpp
Go to the documentation of this file.
1 //===-ThinLTOCodeGenerator.cpp - LLVM Link Time Optimizer -----------------===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 //
9 // This file implements the Thin Link Time Optimization library. This library is
10 // intended to be used by linker to optimize code at link time.
11 //
12 //===----------------------------------------------------------------------===//
13 
15 
16 #include "llvm/ADT/Statistic.h"
17 #include "llvm/ADT/StringExtras.h"
25 #include "llvm/Config/llvm-config.h"
26 #include "llvm/IR/DebugInfo.h"
28 #include "llvm/IR/LLVMContext.h"
30 #include "llvm/IR/Mangler.h"
31 #include "llvm/IR/PassTimingInfo.h"
32 #include "llvm/IR/RemarkStreamer.h"
33 #include "llvm/IR/Verifier.h"
34 #include "llvm/IRReader/IRReader.h"
35 #include "llvm/LTO/LTO.h"
40 #include "llvm/Support/Debug.h"
41 #include "llvm/Support/Error.h"
43 #include "llvm/Support/Path.h"
44 #include "llvm/Support/SHA1.h"
48 #include "llvm/Support/Threading.h"
50 #include "llvm/Support/VCSRevision.h"
52 #include "llvm/Transforms/IPO.h"
59 
60 #include <numeric>
61 
62 #if !defined(_MSC_VER) && !defined(__MINGW32__)
63 #include <unistd.h>
64 #else
65 #include <io.h>
66 #endif
67 
68 using namespace llvm;
69 
70 #define DEBUG_TYPE "thinlto"
71 
72 namespace llvm {
73 // Flags -discard-value-names, defined in LTOCodeGenerator.cpp
79 }
80 
81 namespace {
82 
83 static cl::opt<int>
84  ThreadCount("threads", cl::init(llvm::heavyweight_hardware_concurrency()));
85 
86 // Simple helper to save temporary files for debug.
87 static void saveTempBitcode(const Module &TheModule, StringRef TempDir,
88  unsigned count, StringRef Suffix) {
89  if (TempDir.empty())
90  return;
91  // User asked to save temps, let dump the bitcode file after import.
92  std::string SaveTempPath = (TempDir + llvm::Twine(count) + Suffix).str();
93  std::error_code EC;
94  raw_fd_ostream OS(SaveTempPath, EC, sys::fs::OF_None);
95  if (EC)
96  report_fatal_error(Twine("Failed to open ") + SaveTempPath +
97  " to save optimized bitcode\n");
98  WriteBitcodeToFile(TheModule, OS, /* ShouldPreserveUseListOrder */ true);
99 }
100 
101 static const GlobalValueSummary *
102 getFirstDefinitionForLinker(const GlobalValueSummaryList &GVSummaryList) {
103  // If there is any strong definition anywhere, get it.
104  auto StrongDefForLinker = llvm::find_if(
105  GVSummaryList, [](const std::unique_ptr<GlobalValueSummary> &Summary) {
106  auto Linkage = Summary->linkage();
109  });
110  if (StrongDefForLinker != GVSummaryList.end())
111  return StrongDefForLinker->get();
112  // Get the first *linker visible* definition for this global in the summary
113  // list.
114  auto FirstDefForLinker = llvm::find_if(
115  GVSummaryList, [](const std::unique_ptr<GlobalValueSummary> &Summary) {
116  auto Linkage = Summary->linkage();
118  });
119  // Extern templates can be emitted as available_externally.
120  if (FirstDefForLinker == GVSummaryList.end())
121  return nullptr;
122  return FirstDefForLinker->get();
123 }
124 
125 // Populate map of GUID to the prevailing copy for any multiply defined
126 // symbols. Currently assume first copy is prevailing, or any strong
127 // definition. Can be refined with Linker information in the future.
128 static void computePrevailingCopies(
129  const ModuleSummaryIndex &Index,
131  auto HasMultipleCopies = [&](const GlobalValueSummaryList &GVSummaryList) {
132  return GVSummaryList.size() > 1;
133  };
134 
135  for (auto &I : Index) {
136  if (HasMultipleCopies(I.second.SummaryList))
137  PrevailingCopy[I.first] =
138  getFirstDefinitionForLinker(I.second.SummaryList);
139  }
140 }
141 
143 generateModuleMap(std::vector<std::unique_ptr<lto::InputFile>> &Modules) {
144  StringMap<lto::InputFile *> ModuleMap;
145  for (auto &M : Modules) {
146  assert(ModuleMap.find(M->getName()) == ModuleMap.end() &&
147  "Expect unique Buffer Identifier");
148  ModuleMap[M->getName()] = M.get();
149  }
150  return ModuleMap;
151 }
152 
153 static void promoteModule(Module &TheModule, const ModuleSummaryIndex &Index) {
154  if (renameModuleForThinLTO(TheModule, Index))
155  report_fatal_error("renameModuleForThinLTO failed");
156 }
157 
158 namespace {
159 class ThinLTODiagnosticInfo : public DiagnosticInfo {
160  const Twine &Msg;
161 public:
162  ThinLTODiagnosticInfo(const Twine &DiagMsg,
163  DiagnosticSeverity Severity = DS_Error)
164  : DiagnosticInfo(DK_Linker, Severity), Msg(DiagMsg) {}
165  void print(DiagnosticPrinter &DP) const override { DP << Msg; }
166 };
167 }
168 
169 /// Verify the module and strip broken debug info.
170 static void verifyLoadedModule(Module &TheModule) {
171  bool BrokenDebugInfo = false;
172  if (verifyModule(TheModule, &dbgs(), &BrokenDebugInfo))
173  report_fatal_error("Broken module found, compilation aborted!");
174  if (BrokenDebugInfo) {
175  TheModule.getContext().diagnose(ThinLTODiagnosticInfo(
176  "Invalid debug info found, debug info will be stripped", DS_Warning));
177  StripDebugInfo(TheModule);
178  }
179 }
180 
181 static std::unique_ptr<Module> loadModuleFromInput(lto::InputFile *Input,
183  bool Lazy,
184  bool IsImporting) {
185  auto &Mod = Input->getSingleBitcodeModule();
186  SMDiagnostic Err;
187  Expected<std::unique_ptr<Module>> ModuleOrErr =
188  Lazy ? Mod.getLazyModule(Context,
189  /* ShouldLazyLoadMetadata */ true, IsImporting)
190  : Mod.parseModule(Context);
191  if (!ModuleOrErr) {
192  handleAllErrors(ModuleOrErr.takeError(), [&](ErrorInfoBase &EIB) {
193  SMDiagnostic Err = SMDiagnostic(Mod.getModuleIdentifier(),
194  SourceMgr::DK_Error, EIB.message());
195  Err.print("ThinLTO", errs());
196  });
197  report_fatal_error("Can't load module, abort.");
198  }
199  if (!Lazy)
200  verifyLoadedModule(*ModuleOrErr.get());
201  return std::move(*ModuleOrErr);
202 }
203 
204 static void
205 crossImportIntoModule(Module &TheModule, const ModuleSummaryIndex &Index,
206  StringMap<lto::InputFile*> &ModuleMap,
207  const FunctionImporter::ImportMapTy &ImportList) {
208  auto Loader = [&](StringRef Identifier) {
209  auto &Input = ModuleMap[Identifier];
210  return loadModuleFromInput(Input, TheModule.getContext(),
211  /*Lazy=*/true, /*IsImporting*/ true);
212  };
213 
214  FunctionImporter Importer(Index, Loader);
215  Expected<bool> Result = Importer.importFunctions(TheModule, ImportList);
216  if (!Result) {
217  handleAllErrors(Result.takeError(), [&](ErrorInfoBase &EIB) {
219  SourceMgr::DK_Error, EIB.message());
220  Err.print("ThinLTO", errs());
221  });
222  report_fatal_error("importFunctions failed");
223  }
224  // Verify again after cross-importing.
225  verifyLoadedModule(TheModule);
226 }
227 
228 static void optimizeModule(Module &TheModule, TargetMachine &TM,
229  unsigned OptLevel, bool Freestanding,
230  ModuleSummaryIndex *Index) {
231  // Populate the PassManager
232  PassManagerBuilder PMB;
234  if (Freestanding)
237  // FIXME: should get it from the bitcode?
238  PMB.OptLevel = OptLevel;
239  PMB.LoopVectorize = true;
240  PMB.SLPVectorize = true;
241  // Already did this in verifyLoadedModule().
242  PMB.VerifyInput = false;
243  PMB.VerifyOutput = false;
244  PMB.ImportSummary = Index;
245 
247 
248  // Add the TTI (required to inform the vectorizer about register size for
249  // instance)
251 
252  // Add optimizations
254 
255  PM.run(TheModule);
256 }
257 
258 static void
259 addUsedSymbolToPreservedGUID(const lto::InputFile &File,
260  DenseSet<GlobalValue::GUID> &PreservedGUID) {
261  for (const auto &Sym : File.symbols()) {
262  if (Sym.isUsed())
263  PreservedGUID.insert(GlobalValue::getGUID(Sym.getIRName()));
264  }
265 }
266 
267 // Convert the PreservedSymbols map from "Name" based to "GUID" based.
269 computeGUIDPreservedSymbols(const StringSet<> &PreservedSymbols,
270  const Triple &TheTriple) {
271  DenseSet<GlobalValue::GUID> GUIDPreservedSymbols(PreservedSymbols.size());
272  for (auto &Entry : PreservedSymbols) {
273  StringRef Name = Entry.first();
274  if (TheTriple.isOSBinFormatMachO() && Name.size() > 0 && Name[0] == '_')
275  Name = Name.drop_front();
276  GUIDPreservedSymbols.insert(GlobalValue::getGUID(Name));
277  }
278  return GUIDPreservedSymbols;
279 }
280 
281 std::unique_ptr<MemoryBuffer> codegenModule(Module &TheModule,
282  TargetMachine &TM) {
283  SmallVector<char, 128> OutputBuffer;
284 
285  // CodeGen
286  {
287  raw_svector_ostream OS(OutputBuffer);
289 
290  // If the bitcode files contain ARC code and were compiled with optimization,
291  // the ObjCARCContractPass must be run, so do it unconditionally here.
293 
294  // Setup the codegen now.
295  if (TM.addPassesToEmitFile(PM, OS, nullptr, TargetMachine::CGFT_ObjectFile,
296  /* DisableVerify */ true))
297  report_fatal_error("Failed to setup codegen");
298 
299  // Run codegen now. resulting binary is in OutputBuffer.
300  PM.run(TheModule);
301  }
302  return std::make_unique<SmallVectorMemoryBuffer>(std::move(OutputBuffer));
303 }
304 
305 /// Manage caching for a single Module.
306 class ModuleCacheEntry {
307  SmallString<128> EntryPath;
308 
309 public:
310  // Create a cache entry. This compute a unique hash for the Module considering
311  // the current list of export/import, and offer an interface to query to
312  // access the content in the cache.
313  ModuleCacheEntry(
314  StringRef CachePath, const ModuleSummaryIndex &Index, StringRef ModuleID,
315  const FunctionImporter::ImportMapTy &ImportList,
316  const FunctionImporter::ExportSetTy &ExportList,
317  const std::map<GlobalValue::GUID, GlobalValue::LinkageTypes> &ResolvedODR,
318  const GVSummaryMapTy &DefinedGVSummaries, unsigned OptLevel,
319  bool Freestanding, const TargetMachineBuilder &TMBuilder) {
320  if (CachePath.empty())
321  return;
322 
323  if (!Index.modulePaths().count(ModuleID))
324  // The module does not have an entry, it can't have a hash at all
325  return;
326 
327  if (all_of(Index.getModuleHash(ModuleID),
328  [](uint32_t V) { return V == 0; }))
329  // No hash entry, no caching!
330  return;
331 
332  llvm::lto::Config Conf;
333  Conf.OptLevel = OptLevel;
334  Conf.Options = TMBuilder.Options;
335  Conf.CPU = TMBuilder.MCpu;
336  Conf.MAttrs.push_back(TMBuilder.MAttr);
337  Conf.RelocModel = TMBuilder.RelocModel;
338  Conf.CGOptLevel = TMBuilder.CGOptLevel;
339  Conf.Freestanding = Freestanding;
341  computeLTOCacheKey(Key, Conf, Index, ModuleID, ImportList, ExportList,
342  ResolvedODR, DefinedGVSummaries);
343 
344  // This choice of file name allows the cache to be pruned (see pruneCache()
345  // in include/llvm/Support/CachePruning.h).
346  sys::path::append(EntryPath, CachePath, "llvmcache-" + Key);
347  }
348 
349  // Access the path to this entry in the cache.
350  StringRef getEntryPath() { return EntryPath; }
351 
352  // Try loading the buffer for this cache entry.
353  ErrorOr<std::unique_ptr<MemoryBuffer>> tryLoadingBuffer() {
354  if (EntryPath.empty())
355  return std::error_code();
356  SmallString<64> ResultPath;
358  Twine(EntryPath), sys::fs::OF_UpdateAtime, &ResultPath);
359  if (!FDOrErr)
360  return errorToErrorCode(FDOrErr.takeError());
362  *FDOrErr, EntryPath, /*FileSize=*/-1, /*RequiresNullTerminator=*/false);
363  sys::fs::closeFile(*FDOrErr);
364  return MBOrErr;
365  }
366 
367  // Cache the Produced object file
368  void write(const MemoryBuffer &OutputBuffer) {
369  if (EntryPath.empty())
370  return;
371 
372  // Write to a temporary to avoid race condition
373  SmallString<128> TempFilename;
374  SmallString<128> CachePath(EntryPath);
376  sys::path::append(TempFilename, CachePath, "Thin-%%%%%%.tmp.o");
377 
378  if (auto Err = handleErrors(
379  llvm::writeFileAtomically(TempFilename, EntryPath,
380  OutputBuffer.getBuffer()),
381  [](const llvm::AtomicFileWriteError &E) {
382  std::string ErrorMsgBuffer;
383  llvm::raw_string_ostream S(ErrorMsgBuffer);
384  E.log(S);
385 
386  if (E.Error ==
388  errs() << "Error: " << ErrorMsgBuffer << "\n";
389  report_fatal_error("ThinLTO: Can't get a temporary file");
390  }
391  })) {
392  // FIXME
393  consumeError(std::move(Err));
394  }
395  }
396 };
397 
398 static std::unique_ptr<MemoryBuffer>
399 ProcessThinLTOModule(Module &TheModule, ModuleSummaryIndex &Index,
401  const FunctionImporter::ImportMapTy &ImportList,
402  const FunctionImporter::ExportSetTy &ExportList,
403  const DenseSet<GlobalValue::GUID> &GUIDPreservedSymbols,
404  const GVSummaryMapTy &DefinedGlobals,
405  const ThinLTOCodeGenerator::CachingOptions &CacheOptions,
406  bool DisableCodeGen, StringRef SaveTempsDir,
407  bool Freestanding, unsigned OptLevel, unsigned count) {
408 
409  // "Benchmark"-like optimization: single-source case
410  bool SingleModule = (ModuleMap.size() == 1);
411 
412  if (!SingleModule) {
413  promoteModule(TheModule, Index);
414 
415  // Apply summary-based prevailing-symbol resolution decisions.
416  thinLTOResolvePrevailingInModule(TheModule, DefinedGlobals);
417 
418  // Save temps: after promotion.
419  saveTempBitcode(TheModule, SaveTempsDir, count, ".1.promoted.bc");
420  }
421 
422  // Be friendly and don't nuke totally the module when the client didn't
423  // supply anything to preserve.
424  if (!ExportList.empty() || !GUIDPreservedSymbols.empty()) {
425  // Apply summary-based internalization decisions.
426  thinLTOInternalizeModule(TheModule, DefinedGlobals);
427  }
428 
429  // Save internalized bitcode
430  saveTempBitcode(TheModule, SaveTempsDir, count, ".2.internalized.bc");
431 
432  if (!SingleModule) {
433  crossImportIntoModule(TheModule, Index, ModuleMap, ImportList);
434 
435  // Save temps: after cross-module import.
436  saveTempBitcode(TheModule, SaveTempsDir, count, ".3.imported.bc");
437  }
438 
439  optimizeModule(TheModule, TM, OptLevel, Freestanding, &Index);
440 
441  saveTempBitcode(TheModule, SaveTempsDir, count, ".4.opt.bc");
442 
443  if (DisableCodeGen) {
444  // Configured to stop before CodeGen, serialize the bitcode and return.
445  SmallVector<char, 128> OutputBuffer;
446  {
447  raw_svector_ostream OS(OutputBuffer);
448  ProfileSummaryInfo PSI(TheModule);
449  auto Index = buildModuleSummaryIndex(TheModule, nullptr, &PSI);
450  WriteBitcodeToFile(TheModule, OS, true, &Index);
451  }
452  return std::make_unique<SmallVectorMemoryBuffer>(std::move(OutputBuffer));
453  }
454 
455  return codegenModule(TheModule, TM);
456 }
457 
458 /// Resolve prevailing symbols. Record resolutions in the \p ResolvedODR map
459 /// for caching, and in the \p Index for application during the ThinLTO
460 /// backends. This is needed for correctness for exported symbols (ensure
461 /// at least one copy kept) and a compile-time optimization (to drop duplicate
462 /// copies when possible).
463 static void resolvePrevailingInIndex(
464  ModuleSummaryIndex &Index,
465  StringMap<std::map<GlobalValue::GUID, GlobalValue::LinkageTypes>>
466  &ResolvedODR,
467  const DenseSet<GlobalValue::GUID> &GUIDPreservedSymbols,
469  &PrevailingCopy) {
470 
471  auto isPrevailing = [&](GlobalValue::GUID GUID, const GlobalValueSummary *S) {
472  const auto &Prevailing = PrevailingCopy.find(GUID);
473  // Not in map means that there was only one copy, which must be prevailing.
474  if (Prevailing == PrevailingCopy.end())
475  return true;
476  return Prevailing->second == S;
477  };
478 
479  auto recordNewLinkage = [&](StringRef ModuleIdentifier,
480  GlobalValue::GUID GUID,
481  GlobalValue::LinkageTypes NewLinkage) {
482  ResolvedODR[ModuleIdentifier][GUID] = NewLinkage;
483  };
484 
485  thinLTOResolvePrevailingInIndex(Index, isPrevailing, recordNewLinkage,
486  GUIDPreservedSymbols);
487 }
488 
489 // Initialize the TargetMachine builder for a given Triple
490 static void initTMBuilder(TargetMachineBuilder &TMBuilder,
491  const Triple &TheTriple) {
492  // Set a default CPU for Darwin triples (copied from LTOCodeGenerator).
493  // FIXME this looks pretty terrible...
494  if (TMBuilder.MCpu.empty() && TheTriple.isOSDarwin()) {
495  if (TheTriple.getArch() == llvm::Triple::x86_64)
496  TMBuilder.MCpu = "core2";
497  else if (TheTriple.getArch() == llvm::Triple::x86)
498  TMBuilder.MCpu = "yonah";
499  else if (TheTriple.getArch() == llvm::Triple::aarch64 ||
500  TheTriple.getArch() == llvm::Triple::aarch64_32)
501  TMBuilder.MCpu = "cyclone";
502  }
503  TMBuilder.TheTriple = std::move(TheTriple);
504 }
505 
506 } // end anonymous namespace
507 
509  MemoryBufferRef Buffer(Data, Identifier);
510 
511  auto InputOrError = lto::InputFile::create(Buffer);
512  if (!InputOrError)
513  report_fatal_error("ThinLTO cannot create input file: " +
514  toString(InputOrError.takeError()));
515 
516  auto TripleStr = (*InputOrError)->getTargetTriple();
517  Triple TheTriple(TripleStr);
518 
519  if (Modules.empty())
520  initTMBuilder(TMBuilder, Triple(TheTriple));
521  else if (TMBuilder.TheTriple != TheTriple) {
522  if (!TMBuilder.TheTriple.isCompatibleWith(TheTriple))
523  report_fatal_error("ThinLTO modules with incompatible triples not "
524  "supported");
525  initTMBuilder(TMBuilder, Triple(TMBuilder.TheTriple.merge(TheTriple)));
526  }
527 
528  Modules.emplace_back(std::move(*InputOrError));
529 }
530 
532  PreservedSymbols.insert(Name);
533 }
534 
536  // FIXME: At the moment, we don't take advantage of this extra information,
537  // we're conservatively considering cross-references as preserved.
538  // CrossReferencedSymbols.insert(Name);
539  PreservedSymbols.insert(Name);
540 }
541 
542 // TargetMachine factory
543 std::unique_ptr<TargetMachine> TargetMachineBuilder::create() const {
544  std::string ErrMsg;
545  const Target *TheTarget =
546  TargetRegistry::lookupTarget(TheTriple.str(), ErrMsg);
547  if (!TheTarget) {
548  report_fatal_error("Can't load target for this Triple: " + ErrMsg);
549  }
550 
551  // Use MAttr as the default set of features.
552  SubtargetFeatures Features(MAttr);
553  Features.getDefaultSubtargetFeatures(TheTriple);
554  std::string FeatureStr = Features.getString();
555 
556  return std::unique_ptr<TargetMachine>(
557  TheTarget->createTargetMachine(TheTriple.str(), MCpu, FeatureStr, Options,
558  RelocModel, None, CGOptLevel));
559 }
560 
561 /**
562  * Produce the combined summary index from all the bitcode files:
563  * "thin-link".
564  */
565 std::unique_ptr<ModuleSummaryIndex> ThinLTOCodeGenerator::linkCombinedIndex() {
566  std::unique_ptr<ModuleSummaryIndex> CombinedIndex =
567  std::make_unique<ModuleSummaryIndex>(/*HaveGVs=*/false);
568  uint64_t NextModuleId = 0;
569  for (auto &Mod : Modules) {
570  auto &M = Mod->getSingleBitcodeModule();
571  if (Error Err =
572  M.readSummary(*CombinedIndex, Mod->getName(), NextModuleId++)) {
573  // FIXME diagnose
575  std::move(Err), errs(),
576  "error: can't create module summary index for buffer: ");
577  return nullptr;
578  }
579  }
580  return CombinedIndex;
581 }
582 
583 struct IsExported {
586 
588  const DenseSet<GlobalValue::GUID> &GUIDPreservedSymbols)
589  : ExportLists(ExportLists), GUIDPreservedSymbols(GUIDPreservedSymbols) {}
590 
591  bool operator()(StringRef ModuleIdentifier, GlobalValue::GUID GUID) const {
592  const auto &ExportList = ExportLists.find(ModuleIdentifier);
593  return (ExportList != ExportLists.end() &&
594  ExportList->second.count(GUID)) ||
595  GUIDPreservedSymbols.count(GUID);
596  }
597 };
598 
599 struct IsPrevailing {
602  &PrevailingCopy)
603  : PrevailingCopy(PrevailingCopy) {}
604 
605  bool operator()(GlobalValue::GUID GUID, const GlobalValueSummary *S) const {
606  const auto &Prevailing = PrevailingCopy.find(GUID);
607  // Not in map means that there was only one copy, which must be prevailing.
608  if (Prevailing == PrevailingCopy.end())
609  return true;
610  return Prevailing->second == S;
611  };
612 };
613 
616  const DenseSet<GlobalValue::GUID> &GUIDPreservedSymbols) {
617  // We have no symbols resolution available. And can't do any better now in the
618  // case where the prevailing symbol is in a native object. It can be refined
619  // with linker information in the future.
620  auto isPrevailing = [&](GlobalValue::GUID G) {
622  };
623  computeDeadSymbolsWithConstProp(Index, GUIDPreservedSymbols, isPrevailing,
624  /* ImportEnabled = */ true);
625 }
626 
627 /**
628  * Perform promotion and renaming of exported internal functions.
629  * Index is updated to reflect linkage changes from weak resolution.
630  */
632  const lto::InputFile &File) {
633  auto ModuleCount = Index.modulePaths().size();
634  auto ModuleIdentifier = TheModule.getModuleIdentifier();
635 
636  // Collect for each module the list of function it defines (GUID -> Summary).
637  StringMap<GVSummaryMapTy> ModuleToDefinedGVSummaries;
638  Index.collectDefinedGVSummariesPerModule(ModuleToDefinedGVSummaries);
639 
640  // Convert the preserved symbols set from string to GUID
641  auto GUIDPreservedSymbols = computeGUIDPreservedSymbols(
642  PreservedSymbols, Triple(TheModule.getTargetTriple()));
643 
644  // Add used symbol to the preserved symbols.
645  addUsedSymbolToPreservedGUID(File, GUIDPreservedSymbols);
646 
647  // Compute "dead" symbols, we don't want to import/export these!
648  computeDeadSymbolsInIndex(Index, GUIDPreservedSymbols);
649 
650  // Generate import/export list
651  StringMap<FunctionImporter::ImportMapTy> ImportLists(ModuleCount);
652  StringMap<FunctionImporter::ExportSetTy> ExportLists(ModuleCount);
653  ComputeCrossModuleImport(Index, ModuleToDefinedGVSummaries, ImportLists,
654  ExportLists);
655 
657  computePrevailingCopies(Index, PrevailingCopy);
658 
659  // Resolve prevailing symbols
661  resolvePrevailingInIndex(Index, ResolvedODR, GUIDPreservedSymbols,
662  PrevailingCopy);
663 
665  TheModule, ModuleToDefinedGVSummaries[ModuleIdentifier]);
666 
667  // Promote the exported values in the index, so that they are promoted
668  // in the module.
670  Index, IsExported(ExportLists, GUIDPreservedSymbols),
671  IsPrevailing(PrevailingCopy));
672 
673  promoteModule(TheModule, Index);
674 }
675 
676 /**
677  * Perform cross-module importing for the module identified by ModuleIdentifier.
678  */
681  const lto::InputFile &File) {
682  auto ModuleMap = generateModuleMap(Modules);
683  auto ModuleCount = Index.modulePaths().size();
684 
685  // Collect for each module the list of function it defines (GUID -> Summary).
686  StringMap<GVSummaryMapTy> ModuleToDefinedGVSummaries(ModuleCount);
687  Index.collectDefinedGVSummariesPerModule(ModuleToDefinedGVSummaries);
688 
689  // Convert the preserved symbols set from string to GUID
690  auto GUIDPreservedSymbols = computeGUIDPreservedSymbols(
691  PreservedSymbols, Triple(TheModule.getTargetTriple()));
692 
693  addUsedSymbolToPreservedGUID(File, GUIDPreservedSymbols);
694 
695  // Compute "dead" symbols, we don't want to import/export these!
696  computeDeadSymbolsInIndex(Index, GUIDPreservedSymbols);
697 
698  // Generate import/export list
699  StringMap<FunctionImporter::ImportMapTy> ImportLists(ModuleCount);
700  StringMap<FunctionImporter::ExportSetTy> ExportLists(ModuleCount);
701  ComputeCrossModuleImport(Index, ModuleToDefinedGVSummaries, ImportLists,
702  ExportLists);
703  auto &ImportList = ImportLists[TheModule.getModuleIdentifier()];
704 
705  crossImportIntoModule(TheModule, Index, ModuleMap, ImportList);
706 }
707 
708 /**
709  * Compute the list of summaries needed for importing into module.
710  */
712  Module &TheModule, ModuleSummaryIndex &Index,
713  std::map<std::string, GVSummaryMapTy> &ModuleToSummariesForIndex,
714  const lto::InputFile &File) {
715  auto ModuleCount = Index.modulePaths().size();
716  auto ModuleIdentifier = TheModule.getModuleIdentifier();
717 
718  // Collect for each module the list of function it defines (GUID -> Summary).
719  StringMap<GVSummaryMapTy> ModuleToDefinedGVSummaries(ModuleCount);
720  Index.collectDefinedGVSummariesPerModule(ModuleToDefinedGVSummaries);
721 
722  // Convert the preserved symbols set from string to GUID
723  auto GUIDPreservedSymbols = computeGUIDPreservedSymbols(
724  PreservedSymbols, Triple(TheModule.getTargetTriple()));
725 
726  addUsedSymbolToPreservedGUID(File, GUIDPreservedSymbols);
727 
728  // Compute "dead" symbols, we don't want to import/export these!
729  computeDeadSymbolsInIndex(Index, GUIDPreservedSymbols);
730 
731  // Generate import/export list
732  StringMap<FunctionImporter::ImportMapTy> ImportLists(ModuleCount);
733  StringMap<FunctionImporter::ExportSetTy> ExportLists(ModuleCount);
734  ComputeCrossModuleImport(Index, ModuleToDefinedGVSummaries, ImportLists,
735  ExportLists);
736 
738  ModuleIdentifier, ModuleToDefinedGVSummaries,
739  ImportLists[ModuleIdentifier], ModuleToSummariesForIndex);
740 }
741 
742 /**
743  * Emit the list of files needed for importing into module.
744  */
747  const lto::InputFile &File) {
748  auto ModuleCount = Index.modulePaths().size();
749  auto ModuleIdentifier = TheModule.getModuleIdentifier();
750 
751  // Collect for each module the list of function it defines (GUID -> Summary).
752  StringMap<GVSummaryMapTy> ModuleToDefinedGVSummaries(ModuleCount);
753  Index.collectDefinedGVSummariesPerModule(ModuleToDefinedGVSummaries);
754 
755  // Convert the preserved symbols set from string to GUID
756  auto GUIDPreservedSymbols = computeGUIDPreservedSymbols(
757  PreservedSymbols, Triple(TheModule.getTargetTriple()));
758 
759  addUsedSymbolToPreservedGUID(File, GUIDPreservedSymbols);
760 
761  // Compute "dead" symbols, we don't want to import/export these!
762  computeDeadSymbolsInIndex(Index, GUIDPreservedSymbols);
763 
764  // Generate import/export list
765  StringMap<FunctionImporter::ImportMapTy> ImportLists(ModuleCount);
766  StringMap<FunctionImporter::ExportSetTy> ExportLists(ModuleCount);
767  ComputeCrossModuleImport(Index, ModuleToDefinedGVSummaries, ImportLists,
768  ExportLists);
769 
770  std::map<std::string, GVSummaryMapTy> ModuleToSummariesForIndex;
772  ModuleIdentifier, ModuleToDefinedGVSummaries,
773  ImportLists[ModuleIdentifier], ModuleToSummariesForIndex);
774 
775  std::error_code EC;
776  if ((EC = EmitImportsFiles(ModuleIdentifier, OutputName,
777  ModuleToSummariesForIndex)))
778  report_fatal_error(Twine("Failed to open ") + OutputName +
779  " to save imports lists\n");
780 }
781 
782 /**
783  * Perform internalization. Runs promote and internalization together.
784  * Index is updated to reflect linkage changes.
785  */
788  const lto::InputFile &File) {
789  initTMBuilder(TMBuilder, Triple(TheModule.getTargetTriple()));
790  auto ModuleCount = Index.modulePaths().size();
791  auto ModuleIdentifier = TheModule.getModuleIdentifier();
792 
793  // Convert the preserved symbols set from string to GUID
794  auto GUIDPreservedSymbols =
795  computeGUIDPreservedSymbols(PreservedSymbols, TMBuilder.TheTriple);
796 
797  addUsedSymbolToPreservedGUID(File, GUIDPreservedSymbols);
798 
799  // Collect for each module the list of function it defines (GUID -> Summary).
800  StringMap<GVSummaryMapTy> ModuleToDefinedGVSummaries(ModuleCount);
801  Index.collectDefinedGVSummariesPerModule(ModuleToDefinedGVSummaries);
802 
803  // Compute "dead" symbols, we don't want to import/export these!
804  computeDeadSymbolsInIndex(Index, GUIDPreservedSymbols);
805 
806  // Generate import/export list
807  StringMap<FunctionImporter::ImportMapTy> ImportLists(ModuleCount);
808  StringMap<FunctionImporter::ExportSetTy> ExportLists(ModuleCount);
809  ComputeCrossModuleImport(Index, ModuleToDefinedGVSummaries, ImportLists,
810  ExportLists);
811  auto &ExportList = ExportLists[ModuleIdentifier];
812 
813  // Be friendly and don't nuke totally the module when the client didn't
814  // supply anything to preserve.
815  if (ExportList.empty() && GUIDPreservedSymbols.empty())
816  return;
817 
819  computePrevailingCopies(Index, PrevailingCopy);
820 
821  // Resolve prevailing symbols
823  resolvePrevailingInIndex(Index, ResolvedODR, GUIDPreservedSymbols,
824  PrevailingCopy);
825 
826  // Promote the exported values in the index, so that they are promoted
827  // in the module.
829  Index, IsExported(ExportLists, GUIDPreservedSymbols),
830  IsPrevailing(PrevailingCopy));
831 
832  promoteModule(TheModule, Index);
833 
834  // Internalization
836  TheModule, ModuleToDefinedGVSummaries[ModuleIdentifier]);
837 
838  thinLTOInternalizeModule(TheModule,
839  ModuleToDefinedGVSummaries[ModuleIdentifier]);
840 }
841 
842 /**
843  * Perform post-importing ThinLTO optimizations.
844  */
846  initTMBuilder(TMBuilder, Triple(TheModule.getTargetTriple()));
847 
848  // Optimize now
849  optimizeModule(TheModule, *TMBuilder.create(), OptLevel, Freestanding,
850  nullptr);
851 }
852 
853 /// Write out the generated object file, either from CacheEntryPath or from
854 /// OutputBuffer, preferring hard-link when possible.
855 /// Returns the path to the generated file in SavedObjectsDirectoryPath.
856 std::string
858  const MemoryBuffer &OutputBuffer) {
859  auto ArchName = TMBuilder.TheTriple.getArchName();
860  SmallString<128> OutputPath(SavedObjectsDirectoryPath);
861  llvm::sys::path::append(OutputPath,
862  Twine(count) + "." + ArchName + ".thinlto.o");
863  OutputPath.c_str(); // Ensure the string is null terminated.
864  if (sys::fs::exists(OutputPath))
865  sys::fs::remove(OutputPath);
866 
867  // We don't return a memory buffer to the linker, just a list of files.
868  if (!CacheEntryPath.empty()) {
869  // Cache is enabled, hard-link the entry (or copy if hard-link fails).
870  auto Err = sys::fs::create_hard_link(CacheEntryPath, OutputPath);
871  if (!Err)
872  return OutputPath.str();
873  // Hard linking failed, try to copy.
874  Err = sys::fs::copy_file(CacheEntryPath, OutputPath);
875  if (!Err)
876  return OutputPath.str();
877  // Copy failed (could be because the CacheEntry was removed from the cache
878  // in the meantime by another process), fall back and try to write down the
879  // buffer to the output.
880  errs() << "error: can't link or copy from cached entry '" << CacheEntryPath
881  << "' to '" << OutputPath << "'\n";
882  }
883  // No cache entry, just write out the buffer.
884  std::error_code Err;
885  raw_fd_ostream OS(OutputPath, Err, sys::fs::OF_None);
886  if (Err)
887  report_fatal_error("Can't open output '" + OutputPath + "'\n");
888  OS << OutputBuffer.getBuffer();
889  return OutputPath.str();
890 }
891 
892 // Main entry point for the ThinLTO processing
894  // Prepare the resulting object vector
895  assert(ProducedBinaries.empty() && "The generator should not be reused");
896  if (SavedObjectsDirectoryPath.empty())
897  ProducedBinaries.resize(Modules.size());
898  else {
899  sys::fs::create_directories(SavedObjectsDirectoryPath);
900  bool IsDir;
901  sys::fs::is_directory(SavedObjectsDirectoryPath, IsDir);
902  if (!IsDir)
903  report_fatal_error("Unexistent dir: '" + SavedObjectsDirectoryPath + "'");
904  ProducedBinaryFiles.resize(Modules.size());
905  }
906 
907  if (CodeGenOnly) {
908  // Perform only parallel codegen and return.
909  ThreadPool Pool;
910  int count = 0;
911  for (auto &Mod : Modules) {
912  Pool.async([&](int count) {
915 
916  // Parse module now
917  auto TheModule = loadModuleFromInput(Mod.get(), Context, false,
918  /*IsImporting*/ false);
919 
920  // CodeGen
921  auto OutputBuffer = codegenModule(*TheModule, *TMBuilder.create());
922  if (SavedObjectsDirectoryPath.empty())
923  ProducedBinaries[count] = std::move(OutputBuffer);
924  else
925  ProducedBinaryFiles[count] =
926  writeGeneratedObject(count, "", *OutputBuffer);
927  }, count++);
928  }
929 
930  return;
931  }
932 
933  // Sequential linking phase
934  auto Index = linkCombinedIndex();
935 
936  // Save temps: index.
937  if (!SaveTempsDir.empty()) {
938  auto SaveTempPath = SaveTempsDir + "index.bc";
939  std::error_code EC;
940  raw_fd_ostream OS(SaveTempPath, EC, sys::fs::OF_None);
941  if (EC)
942  report_fatal_error(Twine("Failed to open ") + SaveTempPath +
943  " to save optimized bitcode\n");
944  WriteIndexToFile(*Index, OS);
945  }
946 
947 
948  // Prepare the module map.
949  auto ModuleMap = generateModuleMap(Modules);
950  auto ModuleCount = Modules.size();
951 
952  // Collect for each module the list of function it defines (GUID -> Summary).
953  StringMap<GVSummaryMapTy> ModuleToDefinedGVSummaries(ModuleCount);
954  Index->collectDefinedGVSummariesPerModule(ModuleToDefinedGVSummaries);
955 
956  // Convert the preserved symbols set from string to GUID, this is needed for
957  // computing the caching hash and the internalization.
958  auto GUIDPreservedSymbols =
959  computeGUIDPreservedSymbols(PreservedSymbols, TMBuilder.TheTriple);
960 
961  // Add used symbol from inputs to the preserved symbols.
962  for (const auto &M : Modules)
963  addUsedSymbolToPreservedGUID(*M, GUIDPreservedSymbols);
964 
965  // Compute "dead" symbols, we don't want to import/export these!
966  computeDeadSymbolsInIndex(*Index, GUIDPreservedSymbols);
967 
968  // Synthesize entry counts for functions in the combined index.
970 
971  // Perform index-based WPD. This will return immediately if there are
972  // no index entries in the typeIdMetadata map (e.g. if we are instead
973  // performing IR-based WPD in hybrid regular/thin LTO mode).
974  std::map<ValueInfo, std::vector<VTableSlotSummary>> LocalWPDTargetsMap;
975  std::set<GlobalValue::GUID> ExportedGUIDs;
976  runWholeProgramDevirtOnIndex(*Index, ExportedGUIDs, LocalWPDTargetsMap);
977  for (auto GUID : ExportedGUIDs)
978  GUIDPreservedSymbols.insert(GUID);
979 
980  // Collect the import/export lists for all modules from the call-graph in the
981  // combined index.
982  StringMap<FunctionImporter::ImportMapTy> ImportLists(ModuleCount);
983  StringMap<FunctionImporter::ExportSetTy> ExportLists(ModuleCount);
984  ComputeCrossModuleImport(*Index, ModuleToDefinedGVSummaries, ImportLists,
985  ExportLists);
986 
987  // We use a std::map here to be able to have a defined ordering when
988  // producing a hash for the cache entry.
989  // FIXME: we should be able to compute the caching hash for the entry based
990  // on the index, and nuke this map.
992 
994  computePrevailingCopies(*Index, PrevailingCopy);
995 
996  // Resolve prevailing symbols, this has to be computed early because it
997  // impacts the caching.
998  resolvePrevailingInIndex(*Index, ResolvedODR, GUIDPreservedSymbols,
999  PrevailingCopy);
1000 
1001  // Use global summary-based analysis to identify symbols that can be
1002  // internalized (because they aren't exported or preserved as per callback).
1003  // Changes are made in the index, consumed in the ThinLTO backends.
1005  IsExported(ExportLists, GUIDPreservedSymbols),
1006  LocalWPDTargetsMap);
1008  *Index, IsExported(ExportLists, GUIDPreservedSymbols),
1009  IsPrevailing(PrevailingCopy));
1010 
1011  // Make sure that every module has an entry in the ExportLists, ImportList,
1012  // GVSummary and ResolvedODR maps to enable threaded access to these maps
1013  // below.
1014  for (auto &Module : Modules) {
1015  auto ModuleIdentifier = Module->getName();
1016  ExportLists[ModuleIdentifier];
1017  ImportLists[ModuleIdentifier];
1018  ResolvedODR[ModuleIdentifier];
1019  ModuleToDefinedGVSummaries[ModuleIdentifier];
1020  }
1021 
1022  // Compute the ordering we will process the inputs: the rough heuristic here
1023  // is to sort them per size so that the largest module get schedule as soon as
1024  // possible. This is purely a compile-time optimization.
1025  std::vector<int> ModulesOrdering;
1026  ModulesOrdering.resize(Modules.size());
1027  std::iota(ModulesOrdering.begin(), ModulesOrdering.end(), 0);
1028  llvm::sort(ModulesOrdering, [&](int LeftIndex, int RightIndex) {
1029  auto LSize =
1030  Modules[LeftIndex]->getSingleBitcodeModule().getBuffer().size();
1031  auto RSize =
1032  Modules[RightIndex]->getSingleBitcodeModule().getBuffer().size();
1033  return LSize > RSize;
1034  });
1035 
1036  // Parallel optimizer + codegen
1037  {
1038  ThreadPool Pool(ThreadCount);
1039  for (auto IndexCount : ModulesOrdering) {
1040  auto &Mod = Modules[IndexCount];
1041  Pool.async([&](int count) {
1042  auto ModuleIdentifier = Mod->getName();
1043  auto &ExportList = ExportLists[ModuleIdentifier];
1044 
1045  auto &DefinedGVSummaries = ModuleToDefinedGVSummaries[ModuleIdentifier];
1046 
1047  // The module may be cached, this helps handling it.
1048  ModuleCacheEntry CacheEntry(CacheOptions.Path, *Index, ModuleIdentifier,
1049  ImportLists[ModuleIdentifier], ExportList,
1050  ResolvedODR[ModuleIdentifier],
1051  DefinedGVSummaries, OptLevel, Freestanding,
1052  TMBuilder);
1053  auto CacheEntryPath = CacheEntry.getEntryPath();
1054 
1055  {
1056  auto ErrOrBuffer = CacheEntry.tryLoadingBuffer();
1057  LLVM_DEBUG(dbgs() << "Cache " << (ErrOrBuffer ? "hit" : "miss")
1058  << " '" << CacheEntryPath << "' for buffer "
1059  << count << " " << ModuleIdentifier << "\n");
1060 
1061  if (ErrOrBuffer) {
1062  // Cache Hit!
1063  if (SavedObjectsDirectoryPath.empty())
1064  ProducedBinaries[count] = std::move(ErrOrBuffer.get());
1065  else
1066  ProducedBinaryFiles[count] = writeGeneratedObject(
1067  count, CacheEntryPath, *ErrOrBuffer.get());
1068  return;
1069  }
1070  }
1071 
1074  Context.enableDebugTypeODRUniquing();
1075  auto DiagFileOrErr = lto::setupOptimizationRemarks(
1077  RemarksWithHotness, count);
1078  if (!DiagFileOrErr) {
1079  errs() << "Error: " << toString(DiagFileOrErr.takeError()) << "\n";
1080  report_fatal_error("ThinLTO: Can't get an output file for the "
1081  "remarks");
1082  }
1083 
1084  // Parse module now
1085  auto TheModule = loadModuleFromInput(Mod.get(), Context, false,
1086  /*IsImporting*/ false);
1087 
1088  // Save temps: original file.
1089  saveTempBitcode(*TheModule, SaveTempsDir, count, ".0.original.bc");
1090 
1091  auto &ImportList = ImportLists[ModuleIdentifier];
1092  // Run the main process now, and generates a binary
1093  auto OutputBuffer = ProcessThinLTOModule(
1094  *TheModule, *Index, ModuleMap, *TMBuilder.create(), ImportList,
1095  ExportList, GUIDPreservedSymbols,
1096  ModuleToDefinedGVSummaries[ModuleIdentifier], CacheOptions,
1097  DisableCodeGen, SaveTempsDir, Freestanding, OptLevel, count);
1098 
1099  // Commit to the cache (if enabled)
1100  CacheEntry.write(*OutputBuffer);
1101 
1102  if (SavedObjectsDirectoryPath.empty()) {
1103  // We need to generated a memory buffer for the linker.
1104  if (!CacheEntryPath.empty()) {
1105  // When cache is enabled, reload from the cache if possible.
1106  // Releasing the buffer from the heap and reloading it from the
1107  // cache file with mmap helps us to lower memory pressure.
1108  // The freed memory can be used for the next input file.
1109  // The final binary link will read from the VFS cache (hopefully!)
1110  // or from disk (if the memory pressure was too high).
1111  auto ReloadedBufferOrErr = CacheEntry.tryLoadingBuffer();
1112  if (auto EC = ReloadedBufferOrErr.getError()) {
1113  // On error, keep the preexisting buffer and print a diagnostic.
1114  errs() << "error: can't reload cached file '" << CacheEntryPath
1115  << "': " << EC.message() << "\n";
1116  } else {
1117  OutputBuffer = std::move(*ReloadedBufferOrErr);
1118  }
1119  }
1120  ProducedBinaries[count] = std::move(OutputBuffer);
1121  return;
1122  }
1123  ProducedBinaryFiles[count] = writeGeneratedObject(
1124  count, CacheEntryPath, *OutputBuffer);
1125  }, IndexCount);
1126  }
1127  }
1128 
1129  pruneCache(CacheOptions.Path, CacheOptions.Policy);
1130 
1131  // If statistics were requested, print them out now.
1135 }
bool isOSDarwin() const
isOSDarwin - Is this a "Darwin" OS (OS X, iOS, 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:915
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:38
LLVMContext & Context
CodeGenOpt::Level CGOptLevel
Definition: Config.h:43
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:462
StringRef getBuffer() const
Definition: MemoryBuffer.h:63
std::vector< std::unique_ptr< GlobalValueSummary > > GlobalValueSummaryList
uint64_t GUID
Declare a type to represent a global unique identifier for a global value.
Definition: GlobalValue.h:502
LLVM_ATTRIBUTE_NORETURN void report_fatal_error(Error Err, bool gen_crash_diag=true)
Report a serious error, calling any installed error handler.
Definition: Error.cpp:139
This class represents lattice values for constants.
Definition: AllocatorList.h:23
void getDefaultSubtargetFeatures(const Triple &Triple)
Adds the default features for the specified target triple.
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:62
cl::opt< bool > RemarksWithHotness("lto-pass-remarks-with-hotness", cl::desc("With PGO, include profile count in optimization remarks"), cl::Hidden)
A Module instance is used to store all the information related to an LLVM module. ...
Definition: Module.h: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.
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
IsExported(const StringMap< FunctionImporter::ExportSetTy > &ExportLists, const DenseSet< GlobalValue::GUID > &GUIDPreservedSymbols)
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:530
bool operator()(GlobalValue::GUID GUID, const GlobalValueSummary *S) const
bool is_directory(const basic_file_status &status)
Does status represent a directory?
Definition: Path.cpp:1040
Implementation of the target library information.
bool all_of(R &&range, UnaryPredicate P)
Provide wrappers to std::all_of which take ranges instead of having to pass begin/end explicitly...
Definition: STLExtras.h:1165
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:429
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
void thinLTOInternalizeAndPromoteInIndex(ModuleSummaryIndex &Index, function_ref< bool(StringRef, GlobalValue::GUID)> 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:416
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
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
IsPrevailing(const DenseMap< GlobalValue::GUID, const GlobalValueSummary *> &PrevailingCopy)
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:634
std::vector< std::string > MAttrs
Definition: Config.h:40
LLVMContext & getContext() const
Get the global data context.
Definition: Module.h:245
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.
std::unordered_set< GlobalValue::GUID > ExportSetTy
The set contains an entry for every global value the module exports.
Tagged union holding either a T or a Error.
Definition: yaml2obj.h:21
Interface for custom diagnostic printing.
This header defines classes/functions to handle pass execution timing information with interfaces for...
const StringMap< FunctionImporter::ExportSetTy > & ExportLists
LLVM_NODISCARD bool empty() const
empty - Check if the string is empty.
Definition: StringRef.h:140
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:296
Pass * createObjCARCContractPass()
unsigned OptLevel
The Optimization Level - Specify the basic optimization level.
bool StripDebugInfo(Module &M)
Strip debug info in the module if it exists.
Definition: DebugInfo.cpp:350
TargetOptions Options
Definition: Config.h:39
LLVM_NODISCARD size_t size() const
size - Get the string size.
Definition: StringRef.h:144
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:1231
PassManager manages ModulePassManagers.
const DenseSet< GlobalValue::GUID > & GUIDPreservedSymbols
std::error_code copy_file(const Twine &From, const Twine &To)
Copy the contents of From to To.
Definition: Path.cpp:964
void crossReferenceSymbol(StringRef Name)
Adds to a list of all global symbols that are cross-referenced between ThinLTO files.
initializer< Ty > init(const Ty &Val)
Definition: CommandLine.h:432
This is the base abstract class for diagnostic reporting in the backend.
* if(!EatIfPresent(lltok::kw_thread_local)) return false
ParseOptionalThreadLocal := /*empty.
bool renameModuleForThinLTO(Module &M, const ModuleSummaryIndex &Index, SetVector< GlobalValue *> *GlobalsToImport=nullptr)
Perform in-place global value handling on the given Module for exported local functions renamed and p...
void gatherImportedSummariesForModule(StringRef ModulePath, const StringMap< GVSummaryMapTy > &ModuleToDefinedGVSummaries, const FunctionImporter::ImportMapTy &ImportList, std::map< std::string, GVSummaryMapTy > &ModuleToSummariesForIndex)
Compute the set of summaries needed for a ThinLTO backend compilation of ModulePath.
void optimize(Module &Module)
Perform post-importing ThinLTO optimizations.
size_type count(StringRef Key) const
count - Return 1 if the element is in the map, 0 otherwise.
Definition: StringMap.h: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:64
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.
std::pair< iterator, bool > insert(const ValueT &V)
Definition: DenseSet.h:187
TargetIRAnalysis getTargetIRAnalysis()
Get a TargetIRAnalysis appropriate for the target.
unsigned OptLevel
Definition: Config.h:45
void WriteBitcodeToFile(const Module &M, raw_ostream &Out, bool ShouldPreserveUseListOrder=false, const ModuleSummaryIndex *Index=nullptr, bool GenerateHash=false, ModuleHash *ModHash=nullptr)
Write the specified module to the specified raw output stream.
bool isWeakForLinker() const
Definition: GlobalValue.h:466
LTO configuration.
Definition: Config.h:35
const Triple & getTargetTriple() const
static void print(raw_ostream &Out, object::Archive::Kind Kind, T Val)
const ModuleHash & getModuleHash(const StringRef ModPath) const
Get the module SHA1 hash recorded for the given module path.
void thinLTOResolvePrevailingInIndex(ModuleSummaryIndex &Index, function_ref< bool(GlobalValue::GUID, const GlobalValueSummary *)> isPrevailing, function_ref< void(StringRef, GlobalValue::GUID, GlobalValue::LinkageTypes)> recordNewLinkage, const DenseSet< GlobalValue::GUID > &GUIDPreservedSymbols)
Resolve linkage for prevailing symbols in the Index.
Definition: LTO.cpp:355
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:1193
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:633
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:1095
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:71
This is a &#39;vector&#39; (really, a variable-sized array), optimized for the case when the array is small...
Definition: SmallVector.h:837
const DataFlowGraph & G
Definition: RDFGraph.cpp:202
static bool isAvailableExternallyLinkage(LinkageTypes Linkage)
Definition: GlobalValue.h:303
Optional< Reloc::Model > RelocModel
Definition: Config.h:41
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
bool operator()(StringRef ModuleIdentifier, GlobalValue::GUID GUID) const
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:5114
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)
void updateIndexWPDForExports(ModuleSummaryIndex &Summary, function_ref< bool(StringRef, GlobalValue::GUID)> 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...
A raw_ostream that writes to a file descriptor.
Definition: raw_ostream.h:384
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.
const DenseMap< GlobalValue::GUID, const GlobalValueSummary * > & PrevailingCopy
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:1379
A raw_ostream that writes to an std::string.
Definition: raw_ostream.h:503
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:48
bool Freestanding
Flag to indicate that the optimizer should not assume builtins are present on the target...
Definition: Config.h:53
This pass exposes codegen information to IR-level passes.
#define LLVM_DEBUG(X)
Definition: Debug.h:122
bool exists(const basic_file_status &status)
Does file exist?
Definition: Path.cpp:1025
bool AreStatisticsEnabled()
Check if statistics are enabled.
Definition: Statistic.cpp: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