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"
58 
59 #include <numeric>
60 
61 #if !defined(_MSC_VER) && !defined(__MINGW32__)
62 #include <unistd.h>
63 #else
64 #include <io.h>
65 #endif
66 
67 using namespace llvm;
68 
69 #define DEBUG_TYPE "thinlto"
70 
71 namespace llvm {
72 // Flags -discard-value-names, defined in LTOCodeGenerator.cpp
78 }
79 
80 namespace {
81 
82 static cl::opt<int>
83  ThreadCount("threads", cl::init(llvm::heavyweight_hardware_concurrency()));
84 
85 // Simple helper to save temporary files for debug.
86 static void saveTempBitcode(const Module &TheModule, StringRef TempDir,
87  unsigned count, StringRef Suffix) {
88  if (TempDir.empty())
89  return;
90  // User asked to save temps, let dump the bitcode file after import.
91  std::string SaveTempPath = (TempDir + llvm::Twine(count) + Suffix).str();
92  std::error_code EC;
93  raw_fd_ostream OS(SaveTempPath, EC, sys::fs::OF_None);
94  if (EC)
95  report_fatal_error(Twine("Failed to open ") + SaveTempPath +
96  " to save optimized bitcode\n");
97  WriteBitcodeToFile(TheModule, OS, /* ShouldPreserveUseListOrder */ true);
98 }
99 
100 static const GlobalValueSummary *
101 getFirstDefinitionForLinker(const GlobalValueSummaryList &GVSummaryList) {
102  // If there is any strong definition anywhere, get it.
103  auto StrongDefForLinker = llvm::find_if(
104  GVSummaryList, [](const std::unique_ptr<GlobalValueSummary> &Summary) {
105  auto Linkage = Summary->linkage();
108  });
109  if (StrongDefForLinker != GVSummaryList.end())
110  return StrongDefForLinker->get();
111  // Get the first *linker visible* definition for this global in the summary
112  // list.
113  auto FirstDefForLinker = llvm::find_if(
114  GVSummaryList, [](const std::unique_ptr<GlobalValueSummary> &Summary) {
115  auto Linkage = Summary->linkage();
117  });
118  // Extern templates can be emitted as available_externally.
119  if (FirstDefForLinker == GVSummaryList.end())
120  return nullptr;
121  return FirstDefForLinker->get();
122 }
123 
124 // Populate map of GUID to the prevailing copy for any multiply defined
125 // symbols. Currently assume first copy is prevailing, or any strong
126 // definition. Can be refined with Linker information in the future.
127 static void computePrevailingCopies(
128  const ModuleSummaryIndex &Index,
130  auto HasMultipleCopies = [&](const GlobalValueSummaryList &GVSummaryList) {
131  return GVSummaryList.size() > 1;
132  };
133 
134  for (auto &I : Index) {
135  if (HasMultipleCopies(I.second.SummaryList))
136  PrevailingCopy[I.first] =
137  getFirstDefinitionForLinker(I.second.SummaryList);
138  }
139 }
140 
142 generateModuleMap(std::vector<std::unique_ptr<lto::InputFile>> &Modules) {
143  StringMap<lto::InputFile *> ModuleMap;
144  for (auto &M : Modules) {
145  assert(ModuleMap.find(M->getName()) == ModuleMap.end() &&
146  "Expect unique Buffer Identifier");
147  ModuleMap[M->getName()] = M.get();
148  }
149  return ModuleMap;
150 }
151 
152 static void promoteModule(Module &TheModule, const ModuleSummaryIndex &Index) {
153  if (renameModuleForThinLTO(TheModule, Index))
154  report_fatal_error("renameModuleForThinLTO failed");
155 }
156 
157 namespace {
158 class ThinLTODiagnosticInfo : public DiagnosticInfo {
159  const Twine &Msg;
160 public:
161  ThinLTODiagnosticInfo(const Twine &DiagMsg,
162  DiagnosticSeverity Severity = DS_Error)
163  : DiagnosticInfo(DK_Linker, Severity), Msg(DiagMsg) {}
164  void print(DiagnosticPrinter &DP) const override { DP << Msg; }
165 };
166 }
167 
168 /// Verify the module and strip broken debug info.
169 static void verifyLoadedModule(Module &TheModule) {
170  bool BrokenDebugInfo = false;
171  if (verifyModule(TheModule, &dbgs(), &BrokenDebugInfo))
172  report_fatal_error("Broken module found, compilation aborted!");
173  if (BrokenDebugInfo) {
174  TheModule.getContext().diagnose(ThinLTODiagnosticInfo(
175  "Invalid debug info found, debug info will be stripped", DS_Warning));
176  StripDebugInfo(TheModule);
177  }
178 }
179 
180 static std::unique_ptr<Module> loadModuleFromInput(lto::InputFile *Input,
182  bool Lazy,
183  bool IsImporting) {
184  auto &Mod = Input->getSingleBitcodeModule();
185  SMDiagnostic Err;
186  Expected<std::unique_ptr<Module>> ModuleOrErr =
187  Lazy ? Mod.getLazyModule(Context,
188  /* ShouldLazyLoadMetadata */ true, IsImporting)
189  : Mod.parseModule(Context);
190  if (!ModuleOrErr) {
191  handleAllErrors(ModuleOrErr.takeError(), [&](ErrorInfoBase &EIB) {
192  SMDiagnostic Err = SMDiagnostic(Mod.getModuleIdentifier(),
193  SourceMgr::DK_Error, EIB.message());
194  Err.print("ThinLTO", errs());
195  });
196  report_fatal_error("Can't load module, abort.");
197  }
198  if (!Lazy)
199  verifyLoadedModule(*ModuleOrErr.get());
200  return std::move(*ModuleOrErr);
201 }
202 
203 static void
204 crossImportIntoModule(Module &TheModule, const ModuleSummaryIndex &Index,
205  StringMap<lto::InputFile*> &ModuleMap,
206  const FunctionImporter::ImportMapTy &ImportList) {
207  auto Loader = [&](StringRef Identifier) {
208  auto &Input = ModuleMap[Identifier];
209  return loadModuleFromInput(Input, TheModule.getContext(),
210  /*Lazy=*/true, /*IsImporting*/ true);
211  };
212 
213  FunctionImporter Importer(Index, Loader);
214  Expected<bool> Result = Importer.importFunctions(TheModule, ImportList);
215  if (!Result) {
216  handleAllErrors(Result.takeError(), [&](ErrorInfoBase &EIB) {
218  SourceMgr::DK_Error, EIB.message());
219  Err.print("ThinLTO", errs());
220  });
221  report_fatal_error("importFunctions failed");
222  }
223  // Verify again after cross-importing.
224  verifyLoadedModule(TheModule);
225 }
226 
227 static void optimizeModule(Module &TheModule, TargetMachine &TM,
228  unsigned OptLevel, bool Freestanding) {
229  // Populate the PassManager
230  PassManagerBuilder PMB;
232  if (Freestanding)
235  // FIXME: should get it from the bitcode?
236  PMB.OptLevel = OptLevel;
237  PMB.LoopVectorize = true;
238  PMB.SLPVectorize = true;
239  // Already did this in verifyLoadedModule().
240  PMB.VerifyInput = false;
241  PMB.VerifyOutput = false;
242 
244 
245  // Add the TTI (required to inform the vectorizer about register size for
246  // instance)
248 
249  // Add optimizations
251 
252  PM.run(TheModule);
253 }
254 
255 static void
256 addUsedSymbolToPreservedGUID(const lto::InputFile &File,
257  DenseSet<GlobalValue::GUID> &PreservedGUID) {
258  for (const auto &Sym : File.symbols()) {
259  if (Sym.isUsed())
260  PreservedGUID.insert(GlobalValue::getGUID(Sym.getIRName()));
261  }
262 }
263 
264 // Convert the PreservedSymbols map from "Name" based to "GUID" based.
266 computeGUIDPreservedSymbols(const StringSet<> &PreservedSymbols,
267  const Triple &TheTriple) {
268  DenseSet<GlobalValue::GUID> GUIDPreservedSymbols(PreservedSymbols.size());
269  for (auto &Entry : PreservedSymbols) {
270  StringRef Name = Entry.first();
271  if (TheTriple.isOSBinFormatMachO() && Name.size() > 0 && Name[0] == '_')
272  Name = Name.drop_front();
273  GUIDPreservedSymbols.insert(GlobalValue::getGUID(Name));
274  }
275  return GUIDPreservedSymbols;
276 }
277 
278 std::unique_ptr<MemoryBuffer> codegenModule(Module &TheModule,
279  TargetMachine &TM) {
280  SmallVector<char, 128> OutputBuffer;
281 
282  // CodeGen
283  {
284  raw_svector_ostream OS(OutputBuffer);
286 
287  // If the bitcode files contain ARC code and were compiled with optimization,
288  // the ObjCARCContractPass must be run, so do it unconditionally here.
290 
291  // Setup the codegen now.
292  if (TM.addPassesToEmitFile(PM, OS, nullptr, TargetMachine::CGFT_ObjectFile,
293  /* DisableVerify */ true))
294  report_fatal_error("Failed to setup codegen");
295 
296  // Run codegen now. resulting binary is in OutputBuffer.
297  PM.run(TheModule);
298  }
299  return std::make_unique<SmallVectorMemoryBuffer>(std::move(OutputBuffer));
300 }
301 
302 /// Manage caching for a single Module.
303 class ModuleCacheEntry {
304  SmallString<128> EntryPath;
305 
306 public:
307  // Create a cache entry. This compute a unique hash for the Module considering
308  // the current list of export/import, and offer an interface to query to
309  // access the content in the cache.
310  ModuleCacheEntry(
311  StringRef CachePath, const ModuleSummaryIndex &Index, StringRef ModuleID,
312  const FunctionImporter::ImportMapTy &ImportList,
313  const FunctionImporter::ExportSetTy &ExportList,
314  const std::map<GlobalValue::GUID, GlobalValue::LinkageTypes> &ResolvedODR,
315  const GVSummaryMapTy &DefinedGVSummaries, unsigned OptLevel,
316  bool Freestanding, const TargetMachineBuilder &TMBuilder) {
317  if (CachePath.empty())
318  return;
319 
320  if (!Index.modulePaths().count(ModuleID))
321  // The module does not have an entry, it can't have a hash at all
322  return;
323 
324  if (all_of(Index.getModuleHash(ModuleID),
325  [](uint32_t V) { return V == 0; }))
326  // No hash entry, no caching!
327  return;
328 
329  llvm::lto::Config Conf;
330  Conf.OptLevel = OptLevel;
331  Conf.Options = TMBuilder.Options;
332  Conf.CPU = TMBuilder.MCpu;
333  Conf.MAttrs.push_back(TMBuilder.MAttr);
334  Conf.RelocModel = TMBuilder.RelocModel;
335  Conf.CGOptLevel = TMBuilder.CGOptLevel;
336  Conf.Freestanding = Freestanding;
338  computeLTOCacheKey(Key, Conf, Index, ModuleID, ImportList, ExportList,
339  ResolvedODR, DefinedGVSummaries);
340 
341  // This choice of file name allows the cache to be pruned (see pruneCache()
342  // in include/llvm/Support/CachePruning.h).
343  sys::path::append(EntryPath, CachePath, "llvmcache-" + Key);
344  }
345 
346  // Access the path to this entry in the cache.
347  StringRef getEntryPath() { return EntryPath; }
348 
349  // Try loading the buffer for this cache entry.
350  ErrorOr<std::unique_ptr<MemoryBuffer>> tryLoadingBuffer() {
351  if (EntryPath.empty())
352  return std::error_code();
353  SmallString<64> ResultPath;
355  Twine(EntryPath), sys::fs::OF_UpdateAtime, &ResultPath);
356  if (!FDOrErr)
357  return errorToErrorCode(FDOrErr.takeError());
359  *FDOrErr, EntryPath, /*FileSize=*/-1, /*RequiresNullTerminator=*/false);
360  sys::fs::closeFile(*FDOrErr);
361  return MBOrErr;
362  }
363 
364  // Cache the Produced object file
365  void write(const MemoryBuffer &OutputBuffer) {
366  if (EntryPath.empty())
367  return;
368 
369  // Write to a temporary to avoid race condition
370  SmallString<128> TempFilename;
371  SmallString<128> CachePath(EntryPath);
373  sys::path::append(TempFilename, CachePath, "Thin-%%%%%%.tmp.o");
374 
375  if (auto Err = handleErrors(
376  llvm::writeFileAtomically(TempFilename, EntryPath,
377  OutputBuffer.getBuffer()),
378  [](const llvm::AtomicFileWriteError &E) {
379  std::string ErrorMsgBuffer;
380  llvm::raw_string_ostream S(ErrorMsgBuffer);
381  E.log(S);
382 
383  if (E.Error ==
385  errs() << "Error: " << ErrorMsgBuffer << "\n";
386  report_fatal_error("ThinLTO: Can't get a temporary file");
387  }
388  })) {
389  // FIXME
390  consumeError(std::move(Err));
391  }
392  }
393 };
394 
395 static std::unique_ptr<MemoryBuffer>
396 ProcessThinLTOModule(Module &TheModule, ModuleSummaryIndex &Index,
398  const FunctionImporter::ImportMapTy &ImportList,
399  const FunctionImporter::ExportSetTy &ExportList,
400  const DenseSet<GlobalValue::GUID> &GUIDPreservedSymbols,
401  const GVSummaryMapTy &DefinedGlobals,
402  const ThinLTOCodeGenerator::CachingOptions &CacheOptions,
403  bool DisableCodeGen, StringRef SaveTempsDir,
404  bool Freestanding, unsigned OptLevel, unsigned count) {
405 
406  // "Benchmark"-like optimization: single-source case
407  bool SingleModule = (ModuleMap.size() == 1);
408 
409  if (!SingleModule) {
410  promoteModule(TheModule, Index);
411 
412  // Apply summary-based prevailing-symbol resolution decisions.
413  thinLTOResolvePrevailingInModule(TheModule, DefinedGlobals);
414 
415  // Save temps: after promotion.
416  saveTempBitcode(TheModule, SaveTempsDir, count, ".1.promoted.bc");
417  }
418 
419  // Be friendly and don't nuke totally the module when the client didn't
420  // supply anything to preserve.
421  if (!ExportList.empty() || !GUIDPreservedSymbols.empty()) {
422  // Apply summary-based internalization decisions.
423  thinLTOInternalizeModule(TheModule, DefinedGlobals);
424  }
425 
426  // Save internalized bitcode
427  saveTempBitcode(TheModule, SaveTempsDir, count, ".2.internalized.bc");
428 
429  if (!SingleModule) {
430  crossImportIntoModule(TheModule, Index, ModuleMap, ImportList);
431 
432  // Save temps: after cross-module import.
433  saveTempBitcode(TheModule, SaveTempsDir, count, ".3.imported.bc");
434  }
435 
436  optimizeModule(TheModule, TM, OptLevel, Freestanding);
437 
438  saveTempBitcode(TheModule, SaveTempsDir, count, ".4.opt.bc");
439 
440  if (DisableCodeGen) {
441  // Configured to stop before CodeGen, serialize the bitcode and return.
442  SmallVector<char, 128> OutputBuffer;
443  {
444  raw_svector_ostream OS(OutputBuffer);
445  ProfileSummaryInfo PSI(TheModule);
446  auto Index = buildModuleSummaryIndex(TheModule, nullptr, &PSI);
447  WriteBitcodeToFile(TheModule, OS, true, &Index);
448  }
449  return std::make_unique<SmallVectorMemoryBuffer>(std::move(OutputBuffer));
450  }
451 
452  return codegenModule(TheModule, TM);
453 }
454 
455 /// Resolve prevailing symbols. Record resolutions in the \p ResolvedODR map
456 /// for caching, and in the \p Index for application during the ThinLTO
457 /// backends. This is needed for correctness for exported symbols (ensure
458 /// at least one copy kept) and a compile-time optimization (to drop duplicate
459 /// copies when possible).
460 static void resolvePrevailingInIndex(
461  ModuleSummaryIndex &Index,
462  StringMap<std::map<GlobalValue::GUID, GlobalValue::LinkageTypes>>
463  &ResolvedODR,
464  const DenseSet<GlobalValue::GUID> &GUIDPreservedSymbols,
466  &PrevailingCopy) {
467 
468  auto isPrevailing = [&](GlobalValue::GUID GUID, const GlobalValueSummary *S) {
469  const auto &Prevailing = PrevailingCopy.find(GUID);
470  // Not in map means that there was only one copy, which must be prevailing.
471  if (Prevailing == PrevailingCopy.end())
472  return true;
473  return Prevailing->second == S;
474  };
475 
476  auto recordNewLinkage = [&](StringRef ModuleIdentifier,
477  GlobalValue::GUID GUID,
478  GlobalValue::LinkageTypes NewLinkage) {
479  ResolvedODR[ModuleIdentifier][GUID] = NewLinkage;
480  };
481 
482  thinLTOResolvePrevailingInIndex(Index, isPrevailing, recordNewLinkage,
483  GUIDPreservedSymbols);
484 }
485 
486 // Initialize the TargetMachine builder for a given Triple
487 static void initTMBuilder(TargetMachineBuilder &TMBuilder,
488  const Triple &TheTriple) {
489  // Set a default CPU for Darwin triples (copied from LTOCodeGenerator).
490  // FIXME this looks pretty terrible...
491  if (TMBuilder.MCpu.empty() && TheTriple.isOSDarwin()) {
492  if (TheTriple.getArch() == llvm::Triple::x86_64)
493  TMBuilder.MCpu = "core2";
494  else if (TheTriple.getArch() == llvm::Triple::x86)
495  TMBuilder.MCpu = "yonah";
496  else if (TheTriple.getArch() == llvm::Triple::aarch64 ||
497  TheTriple.getArch() == llvm::Triple::aarch64_32)
498  TMBuilder.MCpu = "cyclone";
499  }
500  TMBuilder.TheTriple = std::move(TheTriple);
501 }
502 
503 } // end anonymous namespace
504 
506  MemoryBufferRef Buffer(Data, Identifier);
507 
508  auto InputOrError = lto::InputFile::create(Buffer);
509  if (!InputOrError)
510  report_fatal_error("ThinLTO cannot create input file: " +
511  toString(InputOrError.takeError()));
512 
513  auto TripleStr = (*InputOrError)->getTargetTriple();
514  Triple TheTriple(TripleStr);
515 
516  if (Modules.empty())
517  initTMBuilder(TMBuilder, Triple(TheTriple));
518  else if (TMBuilder.TheTriple != TheTriple) {
519  if (!TMBuilder.TheTriple.isCompatibleWith(TheTriple))
520  report_fatal_error("ThinLTO modules with incompatible triples not "
521  "supported");
522  initTMBuilder(TMBuilder, Triple(TMBuilder.TheTriple.merge(TheTriple)));
523  }
524 
525  Modules.emplace_back(std::move(*InputOrError));
526 }
527 
529  PreservedSymbols.insert(Name);
530 }
531 
533  // FIXME: At the moment, we don't take advantage of this extra information,
534  // we're conservatively considering cross-references as preserved.
535  // CrossReferencedSymbols.insert(Name);
536  PreservedSymbols.insert(Name);
537 }
538 
539 // TargetMachine factory
540 std::unique_ptr<TargetMachine> TargetMachineBuilder::create() const {
541  std::string ErrMsg;
542  const Target *TheTarget =
543  TargetRegistry::lookupTarget(TheTriple.str(), ErrMsg);
544  if (!TheTarget) {
545  report_fatal_error("Can't load target for this Triple: " + ErrMsg);
546  }
547 
548  // Use MAttr as the default set of features.
549  SubtargetFeatures Features(MAttr);
550  Features.getDefaultSubtargetFeatures(TheTriple);
551  std::string FeatureStr = Features.getString();
552 
553  return std::unique_ptr<TargetMachine>(
554  TheTarget->createTargetMachine(TheTriple.str(), MCpu, FeatureStr, Options,
555  RelocModel, None, CGOptLevel));
556 }
557 
558 /**
559  * Produce the combined summary index from all the bitcode files:
560  * "thin-link".
561  */
562 std::unique_ptr<ModuleSummaryIndex> ThinLTOCodeGenerator::linkCombinedIndex() {
563  std::unique_ptr<ModuleSummaryIndex> CombinedIndex =
564  std::make_unique<ModuleSummaryIndex>(/*HaveGVs=*/false);
565  uint64_t NextModuleId = 0;
566  for (auto &Mod : Modules) {
567  auto &M = Mod->getSingleBitcodeModule();
568  if (Error Err =
569  M.readSummary(*CombinedIndex, Mod->getName(), NextModuleId++)) {
570  // FIXME diagnose
572  std::move(Err), errs(),
573  "error: can't create module summary index for buffer: ");
574  return nullptr;
575  }
576  }
577  return CombinedIndex;
578 }
579 
581  const StringMap<FunctionImporter::ExportSetTy> &ExportLists,
582  const DenseSet<GlobalValue::GUID> &GUIDPreservedSymbols,
584  &PrevailingCopy,
586  auto isExported = [&](StringRef ModuleIdentifier, GlobalValue::GUID GUID) {
587  const auto &ExportList = ExportLists.find(ModuleIdentifier);
588  return (ExportList != ExportLists.end() &&
589  ExportList->second.count(GUID)) ||
590  GUIDPreservedSymbols.count(GUID);
591  };
592 
593  auto isPrevailing = [&](GlobalValue::GUID GUID, const GlobalValueSummary *S) {
594  const auto &Prevailing = PrevailingCopy.find(GUID);
595  // Not in map means that there was only one copy, which must be prevailing.
596  if (Prevailing == PrevailingCopy.end())
597  return true;
598  return Prevailing->second == S;
599  };
600 
601  thinLTOInternalizeAndPromoteInIndex(Index, isExported, isPrevailing);
602 }
603 
606  const DenseSet<GlobalValue::GUID> &GUIDPreservedSymbols) {
607  // We have no symbols resolution available. And can't do any better now in the
608  // case where the prevailing symbol is in a native object. It can be refined
609  // with linker information in the future.
610  auto isPrevailing = [&](GlobalValue::GUID G) {
612  };
613  computeDeadSymbolsWithConstProp(Index, GUIDPreservedSymbols, isPrevailing,
614  /* ImportEnabled = */ true);
615 }
616 
617 /**
618  * Perform promotion and renaming of exported internal functions.
619  * Index is updated to reflect linkage changes from weak resolution.
620  */
622  const lto::InputFile &File) {
623  auto ModuleCount = Index.modulePaths().size();
624  auto ModuleIdentifier = TheModule.getModuleIdentifier();
625 
626  // Collect for each module the list of function it defines (GUID -> Summary).
627  StringMap<GVSummaryMapTy> ModuleToDefinedGVSummaries;
628  Index.collectDefinedGVSummariesPerModule(ModuleToDefinedGVSummaries);
629 
630  // Convert the preserved symbols set from string to GUID
631  auto GUIDPreservedSymbols = computeGUIDPreservedSymbols(
632  PreservedSymbols, Triple(TheModule.getTargetTriple()));
633 
634  // Add used symbol to the preserved symbols.
635  addUsedSymbolToPreservedGUID(File, GUIDPreservedSymbols);
636 
637  // Compute "dead" symbols, we don't want to import/export these!
638  computeDeadSymbolsInIndex(Index, GUIDPreservedSymbols);
639 
640  // Generate import/export list
641  StringMap<FunctionImporter::ImportMapTy> ImportLists(ModuleCount);
642  StringMap<FunctionImporter::ExportSetTy> ExportLists(ModuleCount);
643  ComputeCrossModuleImport(Index, ModuleToDefinedGVSummaries, ImportLists,
644  ExportLists);
645 
647  computePrevailingCopies(Index, PrevailingCopy);
648 
649  // Resolve prevailing symbols
651  resolvePrevailingInIndex(Index, ResolvedODR, GUIDPreservedSymbols,
652  PrevailingCopy);
653 
655  TheModule, ModuleToDefinedGVSummaries[ModuleIdentifier]);
656 
657  // Promote the exported values in the index, so that they are promoted
658  // in the module.
659  internalizeAndPromoteInIndex(ExportLists, GUIDPreservedSymbols,
660  PrevailingCopy, Index);
661 
662  promoteModule(TheModule, Index);
663 }
664 
665 /**
666  * Perform cross-module importing for the module identified by ModuleIdentifier.
667  */
670  const lto::InputFile &File) {
671  auto ModuleMap = generateModuleMap(Modules);
672  auto ModuleCount = Index.modulePaths().size();
673 
674  // Collect for each module the list of function it defines (GUID -> Summary).
675  StringMap<GVSummaryMapTy> ModuleToDefinedGVSummaries(ModuleCount);
676  Index.collectDefinedGVSummariesPerModule(ModuleToDefinedGVSummaries);
677 
678  // Convert the preserved symbols set from string to GUID
679  auto GUIDPreservedSymbols = computeGUIDPreservedSymbols(
680  PreservedSymbols, Triple(TheModule.getTargetTriple()));
681 
682  addUsedSymbolToPreservedGUID(File, GUIDPreservedSymbols);
683 
684  // Compute "dead" symbols, we don't want to import/export these!
685  computeDeadSymbolsInIndex(Index, GUIDPreservedSymbols);
686 
687  // Generate import/export list
688  StringMap<FunctionImporter::ImportMapTy> ImportLists(ModuleCount);
689  StringMap<FunctionImporter::ExportSetTy> ExportLists(ModuleCount);
690  ComputeCrossModuleImport(Index, ModuleToDefinedGVSummaries, ImportLists,
691  ExportLists);
692  auto &ImportList = ImportLists[TheModule.getModuleIdentifier()];
693 
694  crossImportIntoModule(TheModule, Index, ModuleMap, ImportList);
695 }
696 
697 /**
698  * Compute the list of summaries needed for importing into module.
699  */
701  Module &TheModule, ModuleSummaryIndex &Index,
702  std::map<std::string, GVSummaryMapTy> &ModuleToSummariesForIndex,
703  const lto::InputFile &File) {
704  auto ModuleCount = Index.modulePaths().size();
705  auto ModuleIdentifier = TheModule.getModuleIdentifier();
706 
707  // Collect for each module the list of function it defines (GUID -> Summary).
708  StringMap<GVSummaryMapTy> ModuleToDefinedGVSummaries(ModuleCount);
709  Index.collectDefinedGVSummariesPerModule(ModuleToDefinedGVSummaries);
710 
711  // Convert the preserved symbols set from string to GUID
712  auto GUIDPreservedSymbols = computeGUIDPreservedSymbols(
713  PreservedSymbols, Triple(TheModule.getTargetTriple()));
714 
715  addUsedSymbolToPreservedGUID(File, GUIDPreservedSymbols);
716 
717  // Compute "dead" symbols, we don't want to import/export these!
718  computeDeadSymbolsInIndex(Index, GUIDPreservedSymbols);
719 
720  // Generate import/export list
721  StringMap<FunctionImporter::ImportMapTy> ImportLists(ModuleCount);
722  StringMap<FunctionImporter::ExportSetTy> ExportLists(ModuleCount);
723  ComputeCrossModuleImport(Index, ModuleToDefinedGVSummaries, ImportLists,
724  ExportLists);
725 
727  ModuleIdentifier, ModuleToDefinedGVSummaries,
728  ImportLists[ModuleIdentifier], ModuleToSummariesForIndex);
729 }
730 
731 /**
732  * Emit the list of files needed for importing into module.
733  */
736  const lto::InputFile &File) {
737  auto ModuleCount = Index.modulePaths().size();
738  auto ModuleIdentifier = TheModule.getModuleIdentifier();
739 
740  // Collect for each module the list of function it defines (GUID -> Summary).
741  StringMap<GVSummaryMapTy> ModuleToDefinedGVSummaries(ModuleCount);
742  Index.collectDefinedGVSummariesPerModule(ModuleToDefinedGVSummaries);
743 
744  // Convert the preserved symbols set from string to GUID
745  auto GUIDPreservedSymbols = computeGUIDPreservedSymbols(
746  PreservedSymbols, Triple(TheModule.getTargetTriple()));
747 
748  addUsedSymbolToPreservedGUID(File, GUIDPreservedSymbols);
749 
750  // Compute "dead" symbols, we don't want to import/export these!
751  computeDeadSymbolsInIndex(Index, GUIDPreservedSymbols);
752 
753  // Generate import/export list
754  StringMap<FunctionImporter::ImportMapTy> ImportLists(ModuleCount);
755  StringMap<FunctionImporter::ExportSetTy> ExportLists(ModuleCount);
756  ComputeCrossModuleImport(Index, ModuleToDefinedGVSummaries, ImportLists,
757  ExportLists);
758 
759  std::map<std::string, GVSummaryMapTy> ModuleToSummariesForIndex;
761  ModuleIdentifier, ModuleToDefinedGVSummaries,
762  ImportLists[ModuleIdentifier], ModuleToSummariesForIndex);
763 
764  std::error_code EC;
765  if ((EC = EmitImportsFiles(ModuleIdentifier, OutputName,
766  ModuleToSummariesForIndex)))
767  report_fatal_error(Twine("Failed to open ") + OutputName +
768  " to save imports lists\n");
769 }
770 
771 /**
772  * Perform internalization. Runs promote and internalization together.
773  * Index is updated to reflect linkage changes.
774  */
777  const lto::InputFile &File) {
778  initTMBuilder(TMBuilder, Triple(TheModule.getTargetTriple()));
779  auto ModuleCount = Index.modulePaths().size();
780  auto ModuleIdentifier = TheModule.getModuleIdentifier();
781 
782  // Convert the preserved symbols set from string to GUID
783  auto GUIDPreservedSymbols =
784  computeGUIDPreservedSymbols(PreservedSymbols, TMBuilder.TheTriple);
785 
786  addUsedSymbolToPreservedGUID(File, GUIDPreservedSymbols);
787 
788  // Collect for each module the list of function it defines (GUID -> Summary).
789  StringMap<GVSummaryMapTy> ModuleToDefinedGVSummaries(ModuleCount);
790  Index.collectDefinedGVSummariesPerModule(ModuleToDefinedGVSummaries);
791 
792  // Compute "dead" symbols, we don't want to import/export these!
793  computeDeadSymbolsInIndex(Index, GUIDPreservedSymbols);
794 
795  // Generate import/export list
796  StringMap<FunctionImporter::ImportMapTy> ImportLists(ModuleCount);
797  StringMap<FunctionImporter::ExportSetTy> ExportLists(ModuleCount);
798  ComputeCrossModuleImport(Index, ModuleToDefinedGVSummaries, ImportLists,
799  ExportLists);
800  auto &ExportList = ExportLists[ModuleIdentifier];
801 
802  // Be friendly and don't nuke totally the module when the client didn't
803  // supply anything to preserve.
804  if (ExportList.empty() && GUIDPreservedSymbols.empty())
805  return;
806 
808  computePrevailingCopies(Index, PrevailingCopy);
809 
810  // Resolve prevailing symbols
812  resolvePrevailingInIndex(Index, ResolvedODR, GUIDPreservedSymbols,
813  PrevailingCopy);
814 
815  // Promote the exported values in the index, so that they are promoted
816  // in the module.
817  internalizeAndPromoteInIndex(ExportLists, GUIDPreservedSymbols,
818  PrevailingCopy, Index);
819 
820  promoteModule(TheModule, Index);
821 
822  // Internalization
824  TheModule, ModuleToDefinedGVSummaries[ModuleIdentifier]);
825 
826  thinLTOInternalizeModule(TheModule,
827  ModuleToDefinedGVSummaries[ModuleIdentifier]);
828 }
829 
830 /**
831  * Perform post-importing ThinLTO optimizations.
832  */
834  initTMBuilder(TMBuilder, Triple(TheModule.getTargetTriple()));
835 
836  // Optimize now
837  optimizeModule(TheModule, *TMBuilder.create(), OptLevel, Freestanding);
838 }
839 
840 /// Write out the generated object file, either from CacheEntryPath or from
841 /// OutputBuffer, preferring hard-link when possible.
842 /// Returns the path to the generated file in SavedObjectsDirectoryPath.
843 std::string
845  const MemoryBuffer &OutputBuffer) {
846  auto ArchName = TMBuilder.TheTriple.getArchName();
847  SmallString<128> OutputPath(SavedObjectsDirectoryPath);
848  llvm::sys::path::append(OutputPath,
849  Twine(count) + "." + ArchName + ".thinlto.o");
850  OutputPath.c_str(); // Ensure the string is null terminated.
851  if (sys::fs::exists(OutputPath))
852  sys::fs::remove(OutputPath);
853 
854  // We don't return a memory buffer to the linker, just a list of files.
855  if (!CacheEntryPath.empty()) {
856  // Cache is enabled, hard-link the entry (or copy if hard-link fails).
857  auto Err = sys::fs::create_hard_link(CacheEntryPath, OutputPath);
858  if (!Err)
859  return OutputPath.str();
860  // Hard linking failed, try to copy.
861  Err = sys::fs::copy_file(CacheEntryPath, OutputPath);
862  if (!Err)
863  return OutputPath.str();
864  // Copy failed (could be because the CacheEntry was removed from the cache
865  // in the meantime by another process), fall back and try to write down the
866  // buffer to the output.
867  errs() << "error: can't link or copy from cached entry '" << CacheEntryPath
868  << "' to '" << OutputPath << "'\n";
869  }
870  // No cache entry, just write out the buffer.
871  std::error_code Err;
872  raw_fd_ostream OS(OutputPath, Err, sys::fs::OF_None);
873  if (Err)
874  report_fatal_error("Can't open output '" + OutputPath + "'\n");
875  OS << OutputBuffer.getBuffer();
876  return OutputPath.str();
877 }
878 
879 // Main entry point for the ThinLTO processing
881  // Prepare the resulting object vector
882  assert(ProducedBinaries.empty() && "The generator should not be reused");
883  if (SavedObjectsDirectoryPath.empty())
884  ProducedBinaries.resize(Modules.size());
885  else {
886  sys::fs::create_directories(SavedObjectsDirectoryPath);
887  bool IsDir;
888  sys::fs::is_directory(SavedObjectsDirectoryPath, IsDir);
889  if (!IsDir)
890  report_fatal_error("Unexistent dir: '" + SavedObjectsDirectoryPath + "'");
891  ProducedBinaryFiles.resize(Modules.size());
892  }
893 
894  if (CodeGenOnly) {
895  // Perform only parallel codegen and return.
896  ThreadPool Pool;
897  int count = 0;
898  for (auto &Mod : Modules) {
899  Pool.async([&](int count) {
902 
903  // Parse module now
904  auto TheModule = loadModuleFromInput(Mod.get(), Context, false,
905  /*IsImporting*/ false);
906 
907  // CodeGen
908  auto OutputBuffer = codegenModule(*TheModule, *TMBuilder.create());
909  if (SavedObjectsDirectoryPath.empty())
910  ProducedBinaries[count] = std::move(OutputBuffer);
911  else
912  ProducedBinaryFiles[count] =
913  writeGeneratedObject(count, "", *OutputBuffer);
914  }, count++);
915  }
916 
917  return;
918  }
919 
920  // Sequential linking phase
921  auto Index = linkCombinedIndex();
922 
923  // Save temps: index.
924  if (!SaveTempsDir.empty()) {
925  auto SaveTempPath = SaveTempsDir + "index.bc";
926  std::error_code EC;
927  raw_fd_ostream OS(SaveTempPath, EC, sys::fs::OF_None);
928  if (EC)
929  report_fatal_error(Twine("Failed to open ") + SaveTempPath +
930  " to save optimized bitcode\n");
931  WriteIndexToFile(*Index, OS);
932  }
933 
934 
935  // Prepare the module map.
936  auto ModuleMap = generateModuleMap(Modules);
937  auto ModuleCount = Modules.size();
938 
939  // Collect for each module the list of function it defines (GUID -> Summary).
940  StringMap<GVSummaryMapTy> ModuleToDefinedGVSummaries(ModuleCount);
941  Index->collectDefinedGVSummariesPerModule(ModuleToDefinedGVSummaries);
942 
943  // Convert the preserved symbols set from string to GUID, this is needed for
944  // computing the caching hash and the internalization.
945  auto GUIDPreservedSymbols =
946  computeGUIDPreservedSymbols(PreservedSymbols, TMBuilder.TheTriple);
947 
948  // Add used symbol from inputs to the preserved symbols.
949  for (const auto &M : Modules)
950  addUsedSymbolToPreservedGUID(*M, GUIDPreservedSymbols);
951 
952  // Compute "dead" symbols, we don't want to import/export these!
953  computeDeadSymbolsInIndex(*Index, GUIDPreservedSymbols);
954 
955  // Synthesize entry counts for functions in the combined index.
957 
958  // Collect the import/export lists for all modules from the call-graph in the
959  // combined index.
960  StringMap<FunctionImporter::ImportMapTy> ImportLists(ModuleCount);
961  StringMap<FunctionImporter::ExportSetTy> ExportLists(ModuleCount);
962  ComputeCrossModuleImport(*Index, ModuleToDefinedGVSummaries, ImportLists,
963  ExportLists);
964 
965  // We use a std::map here to be able to have a defined ordering when
966  // producing a hash for the cache entry.
967  // FIXME: we should be able to compute the caching hash for the entry based
968  // on the index, and nuke this map.
970 
972  computePrevailingCopies(*Index, PrevailingCopy);
973 
974  // Resolve prevailing symbols, this has to be computed early because it
975  // impacts the caching.
976  resolvePrevailingInIndex(*Index, ResolvedODR, GUIDPreservedSymbols,
977  PrevailingCopy);
978 
979  // Use global summary-based analysis to identify symbols that can be
980  // internalized (because they aren't exported or preserved as per callback).
981  // Changes are made in the index, consumed in the ThinLTO backends.
982  internalizeAndPromoteInIndex(ExportLists, GUIDPreservedSymbols,
983  PrevailingCopy, *Index);
984 
985  // Make sure that every module has an entry in the ExportLists, ImportList,
986  // GVSummary and ResolvedODR maps to enable threaded access to these maps
987  // below.
988  for (auto &Module : Modules) {
989  auto ModuleIdentifier = Module->getName();
990  ExportLists[ModuleIdentifier];
991  ImportLists[ModuleIdentifier];
992  ResolvedODR[ModuleIdentifier];
993  ModuleToDefinedGVSummaries[ModuleIdentifier];
994  }
995 
996  // Compute the ordering we will process the inputs: the rough heuristic here
997  // is to sort them per size so that the largest module get schedule as soon as
998  // possible. This is purely a compile-time optimization.
999  std::vector<int> ModulesOrdering;
1000  ModulesOrdering.resize(Modules.size());
1001  std::iota(ModulesOrdering.begin(), ModulesOrdering.end(), 0);
1002  llvm::sort(ModulesOrdering, [&](int LeftIndex, int RightIndex) {
1003  auto LSize =
1004  Modules[LeftIndex]->getSingleBitcodeModule().getBuffer().size();
1005  auto RSize =
1006  Modules[RightIndex]->getSingleBitcodeModule().getBuffer().size();
1007  return LSize > RSize;
1008  });
1009 
1010  // Parallel optimizer + codegen
1011  {
1012  ThreadPool Pool(ThreadCount);
1013  for (auto IndexCount : ModulesOrdering) {
1014  auto &Mod = Modules[IndexCount];
1015  Pool.async([&](int count) {
1016  auto ModuleIdentifier = Mod->getName();
1017  auto &ExportList = ExportLists[ModuleIdentifier];
1018 
1019  auto &DefinedGVSummaries = ModuleToDefinedGVSummaries[ModuleIdentifier];
1020 
1021  // The module may be cached, this helps handling it.
1022  ModuleCacheEntry CacheEntry(CacheOptions.Path, *Index, ModuleIdentifier,
1023  ImportLists[ModuleIdentifier], ExportList,
1024  ResolvedODR[ModuleIdentifier],
1025  DefinedGVSummaries, OptLevel, Freestanding,
1026  TMBuilder);
1027  auto CacheEntryPath = CacheEntry.getEntryPath();
1028 
1029  {
1030  auto ErrOrBuffer = CacheEntry.tryLoadingBuffer();
1031  LLVM_DEBUG(dbgs() << "Cache " << (ErrOrBuffer ? "hit" : "miss")
1032  << " '" << CacheEntryPath << "' for buffer "
1033  << count << " " << ModuleIdentifier << "\n");
1034 
1035  if (ErrOrBuffer) {
1036  // Cache Hit!
1037  if (SavedObjectsDirectoryPath.empty())
1038  ProducedBinaries[count] = std::move(ErrOrBuffer.get());
1039  else
1040  ProducedBinaryFiles[count] = writeGeneratedObject(
1041  count, CacheEntryPath, *ErrOrBuffer.get());
1042  return;
1043  }
1044  }
1045 
1048  Context.enableDebugTypeODRUniquing();
1049  auto DiagFileOrErr = lto::setupOptimizationRemarks(
1051  RemarksWithHotness, count);
1052  if (!DiagFileOrErr) {
1053  errs() << "Error: " << toString(DiagFileOrErr.takeError()) << "\n";
1054  report_fatal_error("ThinLTO: Can't get an output file for the "
1055  "remarks");
1056  }
1057 
1058  // Parse module now
1059  auto TheModule = loadModuleFromInput(Mod.get(), Context, false,
1060  /*IsImporting*/ false);
1061 
1062  // Save temps: original file.
1063  saveTempBitcode(*TheModule, SaveTempsDir, count, ".0.original.bc");
1064 
1065  auto &ImportList = ImportLists[ModuleIdentifier];
1066  // Run the main process now, and generates a binary
1067  auto OutputBuffer = ProcessThinLTOModule(
1068  *TheModule, *Index, ModuleMap, *TMBuilder.create(), ImportList,
1069  ExportList, GUIDPreservedSymbols,
1070  ModuleToDefinedGVSummaries[ModuleIdentifier], CacheOptions,
1071  DisableCodeGen, SaveTempsDir, Freestanding, OptLevel, count);
1072 
1073  // Commit to the cache (if enabled)
1074  CacheEntry.write(*OutputBuffer);
1075 
1076  if (SavedObjectsDirectoryPath.empty()) {
1077  // We need to generated a memory buffer for the linker.
1078  if (!CacheEntryPath.empty()) {
1079  // When cache is enabled, reload from the cache if possible.
1080  // Releasing the buffer from the heap and reloading it from the
1081  // cache file with mmap helps us to lower memory pressure.
1082  // The freed memory can be used for the next input file.
1083  // The final binary link will read from the VFS cache (hopefully!)
1084  // or from disk (if the memory pressure was too high).
1085  auto ReloadedBufferOrErr = CacheEntry.tryLoadingBuffer();
1086  if (auto EC = ReloadedBufferOrErr.getError()) {
1087  // On error, keep the preexisting buffer and print a diagnostic.
1088  errs() << "error: can't reload cached file '" << CacheEntryPath
1089  << "': " << EC.message() << "\n";
1090  } else {
1091  OutputBuffer = std::move(*ReloadedBufferOrErr);
1092  }
1093  }
1094  ProducedBinaries[count] = std::move(OutputBuffer);
1095  return;
1096  }
1097  ProducedBinaryFiles[count] = writeGeneratedObject(
1098  count, CacheEntryPath, *OutputBuffer);
1099  }, IndexCount);
1100  }
1101  }
1102 
1103  pruneCache(CacheOptions.Path, CacheOptions.Policy);
1104 
1105  // If statistics were requested, print them out now.
1109 }
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:240
Represents either an error or a value T.
Definition: ErrorOr.h:56
void remove_filename(SmallVectorImpl< char > &path, Style style=Style::native)
Remove the last component from path unless it is the root dir.
Definition: Path.cpp:474
raw_ostream & errs()
This returns a reference to a raw_ostream for standard error.
void thinLTOResolvePrevailingInModule(Module &TheModule, const GVSummaryMapTy &DefinedGlobals)
Resolve prevailing symbol linkages in TheModule based on the information recorded in the summaries du...
std::string CPU
Definition: Config.h:38
LLVMContext & Context
CodeGenOpt::Level CGOptLevel
Definition: Config.h:43
PassManagerBuilder - This class is used to set up a standard optimization sequence for languages like...
BitcodeModule & getSingleBitcodeModule()
Definition: LTO.cpp: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:65
TargetMachine * createTargetMachine(StringRef TT, StringRef CPU, StringRef Features, const TargetOptions &Options, Optional< Reloc::Model > RM, Optional< CodeModel::Model > CM=None, CodeGenOpt::Level OL=CodeGenOpt::Default, bool JIT=false) const
createTargetMachine - Create a target specific machine implementation for the specified Triple...
amdgpu Simplify well known AMD library false FunctionCallee Value const Twine & Name
This is the interface to build a ModuleSummaryIndex for a module.
std::error_code remove(const Twine &path, bool IgnoreNonExisting=true)
Remove path.
This file provides a bitcode writing pass.
Implements a dense probed hash-table based set.
Definition: DenseSet.h:249
void populateThinLTOPassManager(legacy::PassManagerBase &PM)
std::error_code EmitImportsFiles(StringRef ModulePath, StringRef OutputFilename, const std::map< std::string, GVSummaryMapTy > &ModuleToSummariesForIndex)
Emit into OutputFilename the files module ModulePath will import from.
Analysis providing profile information.
void enableDebugTypeODRUniquing()
void disableAllFunctions()
Disables all builtins.
ImmutablePass * createTargetTransformInfoWrapperPass(TargetIRAnalysis TIRA)
Create an analysis pass wrapper around a TTI object.
std::string getString() const
Returns features as a string.
iterator find(StringRef Key)
Definition: StringMap.h:332
const StringMap< std::pair< uint64_t, ModuleHash > > & modulePaths() const
Table of modules, containing module hash and id.
A raw_ostream that writes to an SmallVector or SmallString.
Definition: raw_ostream.h:530
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:227
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.
virtual bool addPassesToEmitFile(PassManagerBase &, raw_pwrite_stream &, raw_pwrite_stream *, CodeGenFileType, bool=true, MachineModuleInfo *MMI=nullptr)
Add passes to the specified pass manager to get the specified file emitted.
void crossModuleImport(Module &Module, ModuleSummaryIndex &Index, const lto::InputFile &File)
Perform cross-module importing for the module identified by ModuleIdentifier.
cl::opt< std::string > RemarksPasses("lto-pass-remarks-filter", cl::desc("Only record optimization remarks from passes whose " "names match the given regular expression"), cl::value_desc("regex"))
void append(SmallVectorImpl< char > &path, const Twine &a, const Twine &b="", const Twine &c="", const Twine &d="")
Append to path.
Definition: Path.cpp:455
std::string toString(Error E)
Write all error messages (if any) in E to a string.
Definition: Error.h:966
void add(Pass *P) override
Add a pass to the queue of passes to run.
DiagnosticSeverity
Defines the different supported severity of a diagnostic.
unsigned size() const
Definition: StringMap.h:111
Twine - A lightweight data structure for efficiently representing the concatenation of temporary valu...
Definition: Twine.h:80
LLVM_NODISCARD StringRef drop_front(size_t N=1) const
Return a StringRef equal to &#39;this&#39; but with the first N elements dropped.
Definition: StringRef.h:634
std::vector< std::string > MAttrs
Definition: Config.h:40
LLVMContext & getContext() const
Get the global data context.
Definition: Module.h:244
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...
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.
std::error_code copy_file(const Twine &From, const Twine &To)
Copy the contents of From to To.
Definition: Path.cpp:964
void crossReferenceSymbol(StringRef Name)
Adds to a list of all global symbols that are cross-referenced between ThinLTO files.
initializer< Ty > init(const Ty &Val)
Definition: CommandLine.h:432
This is the base abstract class for diagnostic reporting in the backend.
* if(!EatIfPresent(lltok::kw_thread_local)) return false
ParseOptionalThreadLocal := /*empty.
bool renameModuleForThinLTO(Module &M, const ModuleSummaryIndex &Index, SetVector< GlobalValue *> *GlobalsToImport=nullptr)
Perform in-place global value handling on the given Module for exported local functions renamed and p...
void gatherImportedSummariesForModule(StringRef ModulePath, const StringMap< GVSummaryMapTy > &ModuleToDefinedGVSummaries, const FunctionImporter::ImportMapTy &ImportList, std::map< std::string, GVSummaryMapTy > &ModuleToSummariesForIndex)
Compute the set of summaries needed for a ThinLTO backend compilation of ModulePath.
void optimize(Module &Module)
Perform post-importing ThinLTO optimizations.
size_type count(StringRef Key) const
count - Return 1 if the element is in the map, 0 otherwise.
Definition: StringMap.h:358
This is an important class for using LLVM in a threaded context.
Definition: LLVMContext.h:64
Instrumentation for Order File
static GCRegistry::Add< CoreCLRGC > E("coreclr", "CoreCLR-compatible GC")
TargetLibraryInfoImpl * LibraryInfo
LibraryInfo - Specifies information about the runtime library for the optimizer.
Helper to gather options relevant to the target machine creation.
void internalize(Module &Module, ModuleSummaryIndex &Index, const lto::InputFile &File)
Perform internalization.
TargetIRAnalysis getTargetIRAnalysis()
Get a TargetIRAnalysis appropriate for the target.
unsigned OptLevel
Definition: Config.h: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:981
bool pruneCache(StringRef Path, CachePruningPolicy Policy)
Peform pruning using the supplied policy, returns true if pruning occurred, i.e.
bool run(Module &M)
run - Execute all of the passes scheduled for execution.
void logAllUnhandledErrors(Error E, raw_ostream &OS, Twine ErrorBanner={})
Log all errors (if any) in E to OS.
Definition: Error.cpp:61
const std::string & getModuleIdentifier() const
Get the module identifier which is, essentially, the name of the module.
Definition: Module.h:210
Force files Atime to be updated on access. Only makes a difference on windows.
Definition: FileSystem.h: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:904
void computeLTOCacheKey(SmallString< 40 > &Key, const lto::Config &Conf, const ModuleSummaryIndex &Index, StringRef ModuleID, const FunctionImporter::ImportMapTy &ImportList, const FunctionImporter::ExportSetTy &ExportList, const std::map< GlobalValue::GUID, GlobalValue::LinkageTypes > &ResolvedODR, const GVSummaryMapTy &DefinedGlobals, const std::set< GlobalValue::GUID > &CfiFunctionDefs={}, const std::set< GlobalValue::GUID > &CfiFunctionDecls={})
Computes a unique hash for the Module considering the current list of export/import and other global ...
Definition: LTO.cpp: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 void internalizeAndPromoteInIndex(const StringMap< FunctionImporter::ExportSetTy > &ExportLists, const DenseSet< GlobalValue::GUID > &GUIDPreservedSymbols, const DenseMap< GlobalValue::GUID, const GlobalValueSummary *> &PrevailingCopy, ModuleSummaryIndex &Index)
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
raw_ostream & dbgs()
dbgs() - This returns a reference to a raw_ostream for debugging messages.
Definition: Debug.cpp:132
The access may modify the value stored in memory.
StringMap - This is an unconventional map that is specialized for handling keys that are "strings"...
Definition: StringMap.h:219
Target - Wrapper for Target specific information.
Manages the enabling and disabling of subtarget specific features.
void promote(Module &Module, ModuleSummaryIndex &Index, const lto::InputFile &File)
Perform promotion and renaming of exported internal functions, and additionally resolve weak and link...
LinkageTypes
An enumeration for the kinds of linkage for global values.
Definition: GlobalValue.h:47
void WriteIndexToFile(const ModuleSummaryIndex &Index, raw_ostream &Out, const std::map< std::string, GVSummaryMapTy > *ModuleToSummariesForIndex=nullptr)
Write the specified module summary index to the given raw output stream, where it will be written in ...
std::unique_ptr< ModuleSummaryIndex > linkCombinedIndex()
Produce the combined summary index from all the bitcode files: "thin-link".
GUID getGUID() const
Return a 64-bit global unique ID constructed from global value name (i.e.
Definition: GlobalValue.h:510
static void computeDeadSymbolsInIndex(ModuleSummaryIndex &Index, const DenseSet< GlobalValue::GUID > &GUIDPreservedSymbols)
bool verifyModule(const Module &M, raw_ostream *OS=nullptr, bool *BrokenDebugInfo=nullptr)
Check a module for errors.
Definition: Verifier.cpp:5063
Expected< bool > importFunctions(Module &M, const ImportMapTy &ImportList)
Import functions in Module M based on the supplied import list.
std::unique_ptr< TargetMachine > create() const
void ComputeCrossModuleImport(const ModuleSummaryIndex &Index, const StringMap< GVSummaryMapTy > &ModuleToDefinedGVSummaries, StringMap< FunctionImporter::ImportMapTy > &ImportLists, StringMap< FunctionImporter::ExportSetTy > &ExportLists)
Compute all the imports and exports for every module in the Index.
cl::opt< bool > LTODiscardValueNames("lto-discard-value-names", cl::desc("Strip names from Value during LTO (other than GlobalValue)."), cl::init(false), cl::Hidden)
A raw_ostream that writes to a file descriptor.
Definition: raw_ostream.h:384
const char * c_str()
Definition: SmallString.h:269
size_t size() const
Definition: Module.h:606
#define I(x, y, z)
Definition: MD5.cpp:58
void thinLTOInternalizeModule(Module &TheModule, const GVSummaryMapTy &DefinedGlobals)
Internalize TheModule based on the information recorded in the summaries during global summary-based ...
size_type count(const_arg_type_t< ValueT > V) const
Return 1 if the specified key is in the set, 0 otherwise.
Definition: DenseSet.h:91
void preserveSymbol(StringRef Name)
Adds to a list of all global symbols that must exist in the final generated code. ...
void diagnose(const DiagnosticInfo &DI)
Report a message to the currently installed diagnostic handler.
void computeDeadSymbolsWithConstProp(ModuleSummaryIndex &Index, const DenseSet< GlobalValue::GUID > &GUIDPreservedSymbols, function_ref< PrevailingType(GlobalValue::GUID)> isPrevailing, bool ImportEnabled)
Compute dead symbols and run constant propagation in combined index after that.
void PrintStatistics()
Print statistics to the file returned by CreateInfoOutputFile().
Definition: Statistic.cpp:227
Optional< Reloc::Model > RelocModel
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
void collectDefinedGVSummariesPerModule(Map &ModuleToDefinedGVSummaries) const
Collect for each module the list of Summaries it defines (GUID -> Summary).
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:1375
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:881
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:133
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:317
Instances of this class encapsulate one diagnostic report, allowing printing to a raw_ostream as a ca...
Definition: SourceMgr.h:261