LLVM  4.0.0
LTO.cpp
Go to the documentation of this file.
1 //===-LTO.cpp - LLVM Link Time Optimizer ----------------------------------===//
2 //
3 // The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
10 // This file implements functions and classes used to support LTO.
11 //
12 //===----------------------------------------------------------------------===//
13 
14 #include "llvm/LTO/LTO.h"
19 #include "llvm/CodeGen/Analysis.h"
20 #include "llvm/IR/AutoUpgrade.h"
23 #include "llvm/LTO/LTOBackend.h"
24 #include "llvm/Linker/IRMover.h"
28 #include "llvm/Support/Path.h"
29 #include "llvm/Support/SHA1.h"
30 #include "llvm/Support/SourceMgr.h"
33 #include "llvm/Support/Threading.h"
37 #include "llvm/Transforms/IPO.h"
40 
41 #include <set>
42 
43 using namespace llvm;
44 using namespace lto;
45 using namespace object;
46 
47 #define DEBUG_TYPE "lto"
48 
49 // Returns a unique hash for the Module considering the current list of
50 // export/import and other global analysis results.
51 // The hash is produced in \p Key.
52 static void computeCacheKey(
53  SmallString<40> &Key, const Config &Conf, const ModuleSummaryIndex &Index,
54  StringRef ModuleID, const FunctionImporter::ImportMapTy &ImportList,
55  const FunctionImporter::ExportSetTy &ExportList,
56  const std::map<GlobalValue::GUID, GlobalValue::LinkageTypes> &ResolvedODR,
57  const GVSummaryMapTy &DefinedGlobals) {
58  // Compute the unique hash for this entry.
59  // This is based on the current compiler version, the module itself, the
60  // export list, the hash for every single module in the import list, the
61  // list of ResolvedODR for the module, and the list of preserved symbols.
62  SHA1 Hasher;
63 
64  // Start with the compiler revision
65  Hasher.update(LLVM_VERSION_STRING);
66 #ifdef HAVE_LLVM_REVISION
67  Hasher.update(LLVM_REVISION);
68 #endif
69 
70  // Include the parts of the LTO configuration that affect code generation.
71  auto AddString = [&](StringRef Str) {
72  Hasher.update(Str);
73  Hasher.update(ArrayRef<uint8_t>{0});
74  };
75  auto AddUnsigned = [&](unsigned I) {
76  uint8_t Data[4];
77  Data[0] = I;
78  Data[1] = I >> 8;
79  Data[2] = I >> 16;
80  Data[3] = I >> 24;
81  Hasher.update(ArrayRef<uint8_t>{Data, 4});
82  };
83  AddString(Conf.CPU);
84  // FIXME: Hash more of Options. For now all clients initialize Options from
85  // command-line flags (which is unsupported in production), but may set
86  // RelaxELFRelocations. The clang driver can also pass FunctionSections,
87  // DataSections and DebuggerTuning via command line flags.
88  AddUnsigned(Conf.Options.RelaxELFRelocations);
89  AddUnsigned(Conf.Options.FunctionSections);
90  AddUnsigned(Conf.Options.DataSections);
91  AddUnsigned((unsigned)Conf.Options.DebuggerTuning);
92  for (auto &A : Conf.MAttrs)
93  AddString(A);
94  AddUnsigned(Conf.RelocModel);
95  AddUnsigned(Conf.CodeModel);
96  AddUnsigned(Conf.CGOptLevel);
97  AddUnsigned(Conf.OptLevel);
98  AddString(Conf.OptPipeline);
99  AddString(Conf.AAPipeline);
100  AddString(Conf.OverrideTriple);
101  AddString(Conf.DefaultTriple);
102 
103  // Include the hash for the current module
104  auto ModHash = Index.getModuleHash(ModuleID);
105  Hasher.update(ArrayRef<uint8_t>((uint8_t *)&ModHash[0], sizeof(ModHash)));
106  for (auto F : ExportList)
107  // The export list can impact the internalization, be conservative here
108  Hasher.update(ArrayRef<uint8_t>((uint8_t *)&F, sizeof(F)));
109 
110  // Include the hash for every module we import functions from
111  for (auto &Entry : ImportList) {
112  auto ModHash = Index.getModuleHash(Entry.first());
113  Hasher.update(ArrayRef<uint8_t>((uint8_t *)&ModHash[0], sizeof(ModHash)));
114  }
115 
116  // Include the hash for the resolved ODR.
117  for (auto &Entry : ResolvedODR) {
118  Hasher.update(ArrayRef<uint8_t>((const uint8_t *)&Entry.first,
119  sizeof(GlobalValue::GUID)));
120  Hasher.update(ArrayRef<uint8_t>((const uint8_t *)&Entry.second,
121  sizeof(GlobalValue::LinkageTypes)));
122  }
123 
124  // Include the hash for the linkage type to reflect internalization and weak
125  // resolution.
126  for (auto &GS : DefinedGlobals) {
127  GlobalValue::LinkageTypes Linkage = GS.second->linkage();
128  Hasher.update(
129  ArrayRef<uint8_t>((const uint8_t *)&Linkage, sizeof(Linkage)));
130  }
131 
132  if (!Conf.SampleProfile.empty()) {
133  auto FileOrErr = MemoryBuffer::getFile(Conf.SampleProfile);
134  if (FileOrErr)
135  Hasher.update(FileOrErr.get()->getBuffer());
136  }
137 
138  Key = toHex(Hasher.result());
139 }
140 
142  GlobalValueSummaryList &GVSummaryList, GlobalValue::GUID GUID,
143  DenseSet<GlobalValueSummary *> &GlobalInvolvedWithAlias,
145  isPrevailing,
147  recordNewLinkage) {
148  for (auto &S : GVSummaryList) {
149  GlobalValue::LinkageTypes OriginalLinkage = S->linkage();
150  if (!GlobalValue::isWeakForLinker(OriginalLinkage))
151  continue;
152  // We need to emit only one of these. The prevailing module will keep it,
153  // but turned into a weak, while the others will drop it when possible.
154  // This is both a compile-time optimization and a correctness
155  // transformation. This is necessary for correctness when we have exported
156  // a reference - we need to convert the linkonce to weak to
157  // ensure a copy is kept to satisfy the exported reference.
158  // FIXME: We may want to split the compile time and correctness
159  // aspects into separate routines.
160  if (isPrevailing(GUID, S.get())) {
161  if (GlobalValue::isLinkOnceLinkage(OriginalLinkage))
162  S->setLinkage(GlobalValue::getWeakLinkage(
163  GlobalValue::isLinkOnceODRLinkage(OriginalLinkage)));
164  }
165  // Alias and aliasee can't be turned into available_externally.
166  else if (!isa<AliasSummary>(S.get()) &&
167  !GlobalInvolvedWithAlias.count(S.get()) &&
168  (GlobalValue::isLinkOnceODRLinkage(OriginalLinkage) ||
169  GlobalValue::isWeakODRLinkage(OriginalLinkage)))
171  if (S->linkage() != OriginalLinkage)
172  recordNewLinkage(S->modulePath(), GUID, S->linkage());
173  }
174 }
175 
176 // Resolve Weak and LinkOnce values in the \p Index.
177 //
178 // We'd like to drop these functions if they are no longer referenced in the
179 // current module. However there is a chance that another module is still
180 // referencing them because of the import. We make sure we always emit at least
181 // one copy.
183  ModuleSummaryIndex &Index,
185  isPrevailing,
187  recordNewLinkage) {
188  // We won't optimize the globals that are referenced by an alias for now
189  // Ideally we should turn the alias into a global and duplicate the definition
190  // when needed.
191  DenseSet<GlobalValueSummary *> GlobalInvolvedWithAlias;
192  for (auto &I : Index)
193  for (auto &S : I.second)
194  if (auto AS = dyn_cast<AliasSummary>(S.get()))
195  GlobalInvolvedWithAlias.insert(&AS->getAliasee());
196 
197  for (auto &I : Index)
198  thinLTOResolveWeakForLinkerGUID(I.second, I.first, GlobalInvolvedWithAlias,
199  isPrevailing, recordNewLinkage);
200 }
201 
203  GlobalValueSummaryList &GVSummaryList, GlobalValue::GUID GUID,
204  function_ref<bool(StringRef, GlobalValue::GUID)> isExported) {
205  for (auto &S : GVSummaryList) {
206  if (isExported(S->modulePath(), GUID)) {
207  if (GlobalValue::isLocalLinkage(S->linkage()))
208  S->setLinkage(GlobalValue::ExternalLinkage);
209  } else if (!GlobalValue::isLocalLinkage(S->linkage()))
210  S->setLinkage(GlobalValue::InternalLinkage);
211  }
212 }
213 
214 // Update the linkages in the given \p Index to mark exported values
215 // as external and non-exported values as internal.
217  ModuleSummaryIndex &Index,
218  function_ref<bool(StringRef, GlobalValue::GUID)> isExported) {
219  for (auto &I : Index)
220  thinLTOInternalizeAndPromoteGUID(I.second, I.first, isExported);
221 }
222 
225  std::unique_ptr<Module> Mod;
226 
227  // The range of ModuleSymbolTable entries for this input module.
228  size_t SymBegin, SymEnd;
229 };
230 
231 // Requires a destructor for std::vector<InputModule>.
232 InputFile::~InputFile() = default;
233 
235  std::unique_ptr<InputFile> File(new InputFile);
236 
237  ErrorOr<MemoryBufferRef> BCOrErr =
239  if (!BCOrErr)
240  return errorCodeToError(BCOrErr.getError());
241 
243  getBitcodeModuleList(*BCOrErr);
244  if (!BMsOrErr)
245  return BMsOrErr.takeError();
246 
247  if (BMsOrErr->empty())
248  return make_error<StringError>("Bitcode file does not contain any modules",
250 
251  // Create an InputModule for each module in the InputFile, and add it to the
252  // ModuleSymbolTable.
253  for (auto BM : *BMsOrErr) {
255  BM.getLazyModule(File->Ctx, /*ShouldLazyLoadMetadata*/ true,
256  /*IsImporting*/ false);
257  if (!MOrErr)
258  return MOrErr.takeError();
259 
260  size_t SymBegin = File->SymTab.symbols().size();
261  File->SymTab.addModule(MOrErr->get());
262  size_t SymEnd = File->SymTab.symbols().size();
263 
264  for (const auto &C : (*MOrErr)->getComdatSymbolTable()) {
265  auto P = File->ComdatMap.insert(
266  std::make_pair(&C.second, File->Comdats.size()));
267  assert(P.second);
268  (void)P;
269  File->Comdats.push_back(C.first());
270  }
271 
272  File->Mods.push_back({BM, std::move(*MOrErr), SymBegin, SymEnd});
273  }
274 
275  return std::move(File);
276 }
277 
279  if (!isGV())
280  return -1;
281  const GlobalObject *GO = getGV()->getBaseObject();
282  if (!GO)
283  return make_error<StringError>("Unable to determine comdat of alias!",
285  if (const Comdat *C = GO->getComdat()) {
286  auto I = File->ComdatMap.find(C);
287  assert(I != File->ComdatMap.end());
288  return I->second;
289  }
290  return -1;
291 }
292 
294  return Mods[0].BM.getModuleIdentifier();
295 }
296 
298  return Mods[0].Mod->getSourceFileName();
299 }
300 
302 InputFile::module_symbols(InputModule &IM) {
303  return llvm::make_range(
304  symbol_iterator(SymTab.symbols().data() + IM.SymBegin, SymTab, this),
305  symbol_iterator(SymTab.symbols().data() + IM.SymEnd, SymTab, this));
306 }
307 
308 LTO::RegularLTOState::RegularLTOState(unsigned ParallelCodeGenParallelismLevel,
309  Config &Conf)
310  : ParallelCodeGenParallelismLevel(ParallelCodeGenParallelismLevel),
311  Ctx(Conf) {}
312 
313 LTO::ThinLTOState::ThinLTOState(ThinBackend Backend) : Backend(Backend) {
314  if (!Backend)
315  this->Backend =
317 }
318 
320  unsigned ParallelCodeGenParallelismLevel)
321  : Conf(std::move(Conf)),
322  RegularLTO(ParallelCodeGenParallelismLevel, this->Conf),
323  ThinLTO(std::move(Backend)) {}
324 
325 // Requires a destructor for MapVector<BitcodeModule>.
326 LTO::~LTO() = default;
327 
328 // Add the given symbol to the GlobalResolutions map, and resolve its partition.
329 void LTO::addSymbolToGlobalRes(SmallPtrSet<GlobalValue *, 8> &Used,
330  const InputFile::Symbol &Sym,
331  SymbolResolution Res, unsigned Partition) {
332  GlobalValue *GV = Sym.isGV() ? Sym.getGV() : nullptr;
333 
334  auto &GlobalRes = GlobalResolutions[Sym.getName()];
335  if (GV) {
336  GlobalRes.UnnamedAddr &= GV->hasGlobalUnnamedAddr();
337  if (Res.Prevailing)
338  GlobalRes.IRName = GV->getName();
339  }
340  // Set the partition to external if we know it is used elsewhere, e.g.
341  // it is visible to a regular object, is referenced from llvm.compiler_used,
342  // or was already recorded as being referenced from a different partition.
343  if (Res.VisibleToRegularObj || (GV && Used.count(GV)) ||
344  (GlobalRes.Partition != GlobalResolution::Unknown &&
345  GlobalRes.Partition != Partition)) {
346  GlobalRes.Partition = GlobalResolution::External;
347  } else
348  // First recorded reference, save the current partition.
349  GlobalRes.Partition = Partition;
350 
351  // Flag as visible outside of ThinLTO if visible from a regular object or
352  // if this is a reference in the regular LTO partition.
353  GlobalRes.VisibleOutsideThinLTO |=
354  (Res.VisibleToRegularObj || (Partition == GlobalResolution::RegularLTO));
355 }
356 
359  StringRef Path = Input->getName();
360  OS << Path << '\n';
361  auto ResI = Res.begin();
362  for (const InputFile::Symbol &Sym : Input->symbols()) {
363  assert(ResI != Res.end());
364  SymbolResolution Res = *ResI++;
365 
366  OS << "-r=" << Path << ',' << Sym.getName() << ',';
367  if (Res.Prevailing)
368  OS << 'p';
369  if (Res.FinalDefinitionInLinkageUnit)
370  OS << 'l';
371  if (Res.VisibleToRegularObj)
372  OS << 'x';
373  OS << '\n';
374  }
375  assert(ResI == Res.end());
376 }
377 
378 Error LTO::add(std::unique_ptr<InputFile> Input,
380  assert(!CalledGetMaxTasks);
381 
382  if (Conf.ResolutionFile)
383  writeToResolutionFile(*Conf.ResolutionFile, Input.get(), Res);
384 
385  const SymbolResolution *ResI = Res.begin();
386  for (InputFile::InputModule &IM : Input->Mods)
387  if (Error Err = addModule(*Input, IM, ResI, Res.end()))
388  return Err;
389 
390  assert(ResI == Res.end());
391  return Error::success();
392 }
393 
394 Error LTO::addModule(InputFile &Input, InputFile::InputModule &IM,
395  const SymbolResolution *&ResI,
396  const SymbolResolution *ResE) {
397  // FIXME: move to backend
398  Module &M = *IM.Mod;
399 
400  if (M.getDataLayoutStr().empty())
401  return make_error<StringError>("input module has no datalayout",
403 
404  if (!Conf.OverrideTriple.empty())
406  else if (M.getTargetTriple().empty())
408 
409  Expected<bool> HasThinLTOSummary = IM.BM.hasSummary();
410  if (!HasThinLTOSummary)
411  return HasThinLTOSummary.takeError();
412 
413  if (*HasThinLTOSummary)
414  return addThinLTO(IM.BM, M, Input.module_symbols(IM), ResI, ResE);
415  else
416  return addRegularLTO(IM.BM, ResI, ResE);
417 }
418 
419 // Add a regular LTO object to the link.
420 Error LTO::addRegularLTO(BitcodeModule BM, const SymbolResolution *&ResI,
421  const SymbolResolution *ResE) {
422  if (!RegularLTO.CombinedModule) {
423  RegularLTO.CombinedModule =
424  llvm::make_unique<Module>("ld-temp.o", RegularLTO.Ctx);
425  RegularLTO.Mover = llvm::make_unique<IRMover>(*RegularLTO.CombinedModule);
426  }
428  BM.getLazyModule(RegularLTO.Ctx, /*ShouldLazyLoadMetadata*/ true,
429  /*IsImporting*/ false);
430  if (!MOrErr)
431  return MOrErr.takeError();
432 
433  Module &M = **MOrErr;
434  if (Error Err = M.materializeMetadata())
435  return Err;
436  UpgradeDebugInfo(M);
437 
438  ModuleSymbolTable SymTab;
439  SymTab.addModule(&M);
440 
442  collectUsedGlobalVariables(M, Used, /*CompilerUsed*/ false);
443 
444  std::vector<GlobalValue *> Keep;
445 
446  for (GlobalVariable &GV : M.globals())
447  if (GV.hasAppendingLinkage())
448  Keep.push_back(&GV);
449 
450  for (const InputFile::Symbol &Sym :
451  make_range(InputFile::symbol_iterator(SymTab.symbols().begin(), SymTab,
452  nullptr),
453  InputFile::symbol_iterator(SymTab.symbols().end(), SymTab,
454  nullptr))) {
455  assert(ResI != ResE);
456  SymbolResolution Res = *ResI++;
457  addSymbolToGlobalRes(Used, Sym, Res, 0);
458 
460  continue;
461  if (Res.Prevailing && Sym.isGV()) {
462  GlobalValue *GV = Sym.getGV();
463  Keep.push_back(GV);
464  switch (GV->getLinkage()) {
465  default:
466  break;
469  break;
472  break;
473  }
474  }
475  // Common resolution: collect the maximum size/alignment over all commons.
476  // We also record if we see an instance of a common as prevailing, so that
477  // if none is prevailing we can ignore it later.
479  // FIXME: We should figure out what to do about commons defined by asm.
480  // For now they aren't reported correctly by ModuleSymbolTable.
481  auto &CommonRes = RegularLTO.Commons[Sym.getGV()->getName()];
482  CommonRes.Size = std::max(CommonRes.Size, Sym.getCommonSize());
483  CommonRes.Align = std::max(CommonRes.Align, Sym.getCommonAlignment());
484  CommonRes.Prevailing |= Res.Prevailing;
485  }
486 
487  // FIXME: use proposed local attribute for FinalDefinitionInLinkageUnit.
488  }
489 
490  return RegularLTO.Mover->move(std::move(*MOrErr), Keep,
492  /* LinkModuleInlineAsm */ true,
493  /* IsPerformingImport */ false);
494 }
495 
496 // Add a ThinLTO object to the link.
497 // FIXME: This function should not need to take as many parameters once we have
498 // a bitcode symbol table.
499 Error LTO::addThinLTO(BitcodeModule BM, Module &M,
501  const SymbolResolution *&ResI,
502  const SymbolResolution *ResE) {
504  collectUsedGlobalVariables(M, Used, /*CompilerUsed*/ false);
505 
507  if (!SummaryOrErr)
508  return SummaryOrErr.takeError();
509  ThinLTO.CombinedIndex.mergeFrom(std::move(*SummaryOrErr),
510  ThinLTO.ModuleMap.size());
511 
512  for (const InputFile::Symbol &Sym : Syms) {
513  assert(ResI != ResE);
514  SymbolResolution Res = *ResI++;
515  addSymbolToGlobalRes(Used, Sym, Res, ThinLTO.ModuleMap.size() + 1);
516 
517  if (Res.Prevailing && Sym.isGV())
518  ThinLTO.PrevailingModuleForGUID[Sym.getGV()->getGUID()] =
519  BM.getModuleIdentifier();
520  }
521 
522  if (!ThinLTO.ModuleMap.insert({BM.getModuleIdentifier(), BM}).second)
523  return make_error<StringError>(
524  "Expected at most one ThinLTO module per bitcode file",
526 
527  return Error::success();
528 }
529 
530 unsigned LTO::getMaxTasks() const {
531  CalledGetMaxTasks = true;
532  return RegularLTO.ParallelCodeGenParallelismLevel + ThinLTO.ModuleMap.size();
533 }
534 
535 Error LTO::run(AddStreamFn AddStream, NativeObjectCache Cache) {
536  // Save the status of having a regularLTO combined module, as
537  // this is needed for generating the ThinLTO Task ID, and
538  // the CombinedModule will be moved at the end of runRegularLTO.
539  bool HasRegularLTO = RegularLTO.CombinedModule != nullptr;
540  // Invoke regular LTO if there was a regular LTO module to start with.
541  if (HasRegularLTO)
542  if (auto E = runRegularLTO(AddStream))
543  return E;
544  return runThinLTO(AddStream, Cache, HasRegularLTO);
545 }
546 
547 Error LTO::runRegularLTO(AddStreamFn AddStream) {
548  // Make sure commons have the right size/alignment: we kept the largest from
549  // all the prevailing when adding the inputs, and we apply it here.
550  const DataLayout &DL = RegularLTO.CombinedModule->getDataLayout();
551  for (auto &I : RegularLTO.Commons) {
552  if (!I.second.Prevailing)
553  // Don't do anything if no instance of this common was prevailing.
554  continue;
555  GlobalVariable *OldGV = RegularLTO.CombinedModule->getNamedGlobal(I.first);
556  if (OldGV && DL.getTypeAllocSize(OldGV->getValueType()) == I.second.Size) {
557  // Don't create a new global if the type is already correct, just make
558  // sure the alignment is correct.
559  OldGV->setAlignment(I.second.Align);
560  continue;
561  }
562  ArrayType *Ty =
563  ArrayType::get(Type::getInt8Ty(RegularLTO.Ctx), I.second.Size);
564  auto *GV = new GlobalVariable(*RegularLTO.CombinedModule, Ty, false,
567  GV->setAlignment(I.second.Align);
568  if (OldGV) {
570  GV->takeName(OldGV);
571  OldGV->eraseFromParent();
572  } else {
573  GV->setName(I.first);
574  }
575  }
576 
577  if (Conf.PreOptModuleHook &&
578  !Conf.PreOptModuleHook(0, *RegularLTO.CombinedModule))
579  return Error::success();
580 
581  if (!Conf.CodeGenOnly) {
582  for (const auto &R : GlobalResolutions) {
583  if (R.second.IRName.empty())
584  continue;
585  if (R.second.Partition != 0 &&
586  R.second.Partition != GlobalResolution::External)
587  continue;
588 
589  GlobalValue *GV =
590  RegularLTO.CombinedModule->getNamedValue(R.second.IRName);
591  // Ignore symbols defined in other partitions.
592  if (!GV || GV->hasLocalLinkage())
593  continue;
594  GV->setUnnamedAddr(R.second.UnnamedAddr ? GlobalValue::UnnamedAddr::Global
596  if (R.second.Partition == 0)
598  }
599 
600  if (Conf.PostInternalizeModuleHook &&
601  !Conf.PostInternalizeModuleHook(0, *RegularLTO.CombinedModule))
602  return Error::success();
603  }
604  return backend(Conf, AddStream, RegularLTO.ParallelCodeGenParallelismLevel,
605  std::move(RegularLTO.CombinedModule));
606 }
607 
608 /// This class defines the interface to the ThinLTO backend.
610 protected:
614 
615 public:
618  : Conf(Conf), CombinedIndex(CombinedIndex),
619  ModuleToDefinedGVSummaries(ModuleToDefinedGVSummaries) {}
620 
621  virtual ~ThinBackendProc() {}
622  virtual Error start(
623  unsigned Task, BitcodeModule BM,
624  const FunctionImporter::ImportMapTy &ImportList,
625  const FunctionImporter::ExportSetTy &ExportList,
626  const std::map<GlobalValue::GUID, GlobalValue::LinkageTypes> &ResolvedODR,
627  MapVector<StringRef, BitcodeModule> &ModuleMap) = 0;
628  virtual Error wait() = 0;
629 };
630 
631 namespace {
632 class InProcessThinBackend : public ThinBackendProc {
633  ThreadPool BackendThreadPool;
634  AddStreamFn AddStream;
635  NativeObjectCache Cache;
636 
637  Optional<Error> Err;
638  std::mutex ErrMu;
639 
640 public:
641  InProcessThinBackend(
642  Config &Conf, ModuleSummaryIndex &CombinedIndex,
643  unsigned ThinLTOParallelismLevel,
644  const StringMap<GVSummaryMapTy> &ModuleToDefinedGVSummaries,
645  AddStreamFn AddStream, NativeObjectCache Cache)
646  : ThinBackendProc(Conf, CombinedIndex, ModuleToDefinedGVSummaries),
647  BackendThreadPool(ThinLTOParallelismLevel),
648  AddStream(std::move(AddStream)), Cache(std::move(Cache)) {}
649 
650  Error runThinLTOBackendThread(
651  AddStreamFn AddStream, NativeObjectCache Cache, unsigned Task,
652  BitcodeModule BM, ModuleSummaryIndex &CombinedIndex,
653  const FunctionImporter::ImportMapTy &ImportList,
654  const FunctionImporter::ExportSetTy &ExportList,
655  const std::map<GlobalValue::GUID, GlobalValue::LinkageTypes> &ResolvedODR,
656  const GVSummaryMapTy &DefinedGlobals,
658  auto RunThinBackend = [&](AddStreamFn AddStream) {
659  LTOLLVMContext BackendContext(Conf);
660  Expected<std::unique_ptr<Module>> MOrErr = BM.parseModule(BackendContext);
661  if (!MOrErr)
662  return MOrErr.takeError();
663 
664  return thinBackend(Conf, Task, AddStream, **MOrErr, CombinedIndex,
665  ImportList, DefinedGlobals, ModuleMap);
666  };
667 
668  auto ModuleID = BM.getModuleIdentifier();
669 
670  if (!Cache || !CombinedIndex.modulePaths().count(ModuleID) ||
671  all_of(CombinedIndex.getModuleHash(ModuleID),
672  [](uint32_t V) { return V == 0; }))
673  // Cache disabled or no entry for this module in the combined index or
674  // no module hash.
675  return RunThinBackend(AddStream);
676 
677  SmallString<40> Key;
678  // The module may be cached, this helps handling it.
679  computeCacheKey(Key, Conf, CombinedIndex, ModuleID, ImportList, ExportList,
680  ResolvedODR, DefinedGlobals);
681  if (AddStreamFn CacheAddStream = Cache(Task, Key))
682  return RunThinBackend(CacheAddStream);
683 
684  return Error::success();
685  }
686 
687  Error start(
688  unsigned Task, BitcodeModule BM,
689  const FunctionImporter::ImportMapTy &ImportList,
690  const FunctionImporter::ExportSetTy &ExportList,
691  const std::map<GlobalValue::GUID, GlobalValue::LinkageTypes> &ResolvedODR,
692  MapVector<StringRef, BitcodeModule> &ModuleMap) override {
693  StringRef ModulePath = BM.getModuleIdentifier();
694  assert(ModuleToDefinedGVSummaries.count(ModulePath));
695  const GVSummaryMapTy &DefinedGlobals =
696  ModuleToDefinedGVSummaries.find(ModulePath)->second;
697  BackendThreadPool.async(
698  [=](BitcodeModule BM, ModuleSummaryIndex &CombinedIndex,
699  const FunctionImporter::ImportMapTy &ImportList,
700  const FunctionImporter::ExportSetTy &ExportList,
701  const std::map<GlobalValue::GUID, GlobalValue::LinkageTypes>
702  &ResolvedODR,
703  const GVSummaryMapTy &DefinedGlobals,
705  Error E = runThinLTOBackendThread(
706  AddStream, Cache, Task, BM, CombinedIndex, ImportList,
707  ExportList, ResolvedODR, DefinedGlobals, ModuleMap);
708  if (E) {
709  std::unique_lock<std::mutex> L(ErrMu);
710  if (Err)
711  Err = joinErrors(std::move(*Err), std::move(E));
712  else
713  Err = std::move(E);
714  }
715  },
716  BM, std::ref(CombinedIndex), std::ref(ImportList),
717  std::ref(ExportList), std::ref(ResolvedODR), std::ref(DefinedGlobals),
718  std::ref(ModuleMap));
719  return Error::success();
720  }
721 
722  Error wait() override {
723  BackendThreadPool.wait();
724  if (Err)
725  return std::move(*Err);
726  else
727  return Error::success();
728  }
729 };
730 } // end anonymous namespace
731 
732 ThinBackend lto::createInProcessThinBackend(unsigned ParallelismLevel) {
733  return [=](Config &Conf, ModuleSummaryIndex &CombinedIndex,
734  const StringMap<GVSummaryMapTy> &ModuleToDefinedGVSummaries,
735  AddStreamFn AddStream, NativeObjectCache Cache) {
736  return llvm::make_unique<InProcessThinBackend>(
737  Conf, CombinedIndex, ParallelismLevel, ModuleToDefinedGVSummaries,
738  AddStream, Cache);
739  };
740 }
741 
742 // Given the original \p Path to an output file, replace any path
743 // prefix matching \p OldPrefix with \p NewPrefix. Also, create the
744 // resulting directory if it does not yet exist.
745 std::string lto::getThinLTOOutputFile(const std::string &Path,
746  const std::string &OldPrefix,
747  const std::string &NewPrefix) {
748  if (OldPrefix.empty() && NewPrefix.empty())
749  return Path;
750  SmallString<128> NewPath(Path);
751  llvm::sys::path::replace_path_prefix(NewPath, OldPrefix, NewPrefix);
752  StringRef ParentPath = llvm::sys::path::parent_path(NewPath.str());
753  if (!ParentPath.empty()) {
754  // Make sure the new directory exists, creating it if necessary.
755  if (std::error_code EC = llvm::sys::fs::create_directories(ParentPath))
756  llvm::errs() << "warning: could not create directory '" << ParentPath
757  << "': " << EC.message() << '\n';
758  }
759  return NewPath.str();
760 }
761 
762 namespace {
763 class WriteIndexesThinBackend : public ThinBackendProc {
764  std::string OldPrefix, NewPrefix;
765  bool ShouldEmitImportsFiles;
766 
767  std::string LinkedObjectsFileName;
768  std::unique_ptr<llvm::raw_fd_ostream> LinkedObjectsFile;
769 
770 public:
771  WriteIndexesThinBackend(
772  Config &Conf, ModuleSummaryIndex &CombinedIndex,
773  const StringMap<GVSummaryMapTy> &ModuleToDefinedGVSummaries,
774  std::string OldPrefix, std::string NewPrefix, bool ShouldEmitImportsFiles,
775  std::string LinkedObjectsFileName)
776  : ThinBackendProc(Conf, CombinedIndex, ModuleToDefinedGVSummaries),
777  OldPrefix(OldPrefix), NewPrefix(NewPrefix),
778  ShouldEmitImportsFiles(ShouldEmitImportsFiles),
779  LinkedObjectsFileName(LinkedObjectsFileName) {}
780 
781  Error start(
782  unsigned Task, BitcodeModule BM,
783  const FunctionImporter::ImportMapTy &ImportList,
784  const FunctionImporter::ExportSetTy &ExportList,
785  const std::map<GlobalValue::GUID, GlobalValue::LinkageTypes> &ResolvedODR,
786  MapVector<StringRef, BitcodeModule> &ModuleMap) override {
787  StringRef ModulePath = BM.getModuleIdentifier();
788  std::string NewModulePath =
789  getThinLTOOutputFile(ModulePath, OldPrefix, NewPrefix);
790 
791  std::error_code EC;
792  if (!LinkedObjectsFileName.empty()) {
793  if (!LinkedObjectsFile) {
794  LinkedObjectsFile = llvm::make_unique<raw_fd_ostream>(
795  LinkedObjectsFileName, EC, sys::fs::OpenFlags::F_None);
796  if (EC)
797  return errorCodeToError(EC);
798  }
799  *LinkedObjectsFile << NewModulePath << '\n';
800  }
801 
802  std::map<std::string, GVSummaryMapTy> ModuleToSummariesForIndex;
803  gatherImportedSummariesForModule(ModulePath, ModuleToDefinedGVSummaries,
804  ImportList, ModuleToSummariesForIndex);
805 
806  raw_fd_ostream OS(NewModulePath + ".thinlto.bc", EC,
808  if (EC)
809  return errorCodeToError(EC);
810  WriteIndexToFile(CombinedIndex, OS, &ModuleToSummariesForIndex);
811 
812  if (ShouldEmitImportsFiles)
813  return errorCodeToError(
814  EmitImportsFiles(ModulePath, NewModulePath + ".imports", ImportList));
815  return Error::success();
816  }
817 
818  Error wait() override { return Error::success(); }
819 };
820 } // end anonymous namespace
821 
823  std::string NewPrefix,
824  bool ShouldEmitImportsFiles,
825  std::string LinkedObjectsFile) {
826  return [=](Config &Conf, ModuleSummaryIndex &CombinedIndex,
827  const StringMap<GVSummaryMapTy> &ModuleToDefinedGVSummaries,
828  AddStreamFn AddStream, NativeObjectCache Cache) {
829  return llvm::make_unique<WriteIndexesThinBackend>(
830  Conf, CombinedIndex, ModuleToDefinedGVSummaries, OldPrefix, NewPrefix,
831  ShouldEmitImportsFiles, LinkedObjectsFile);
832  };
833 }
834 
835 Error LTO::runThinLTO(AddStreamFn AddStream, NativeObjectCache Cache,
836  bool HasRegularLTO) {
837  if (ThinLTO.ModuleMap.empty())
838  return Error::success();
839 
840  if (Conf.CombinedIndexHook && !Conf.CombinedIndexHook(ThinLTO.CombinedIndex))
841  return Error::success();
842 
843  // Collect for each module the list of function it defines (GUID ->
844  // Summary).
846  ModuleToDefinedGVSummaries(ThinLTO.ModuleMap.size());
847  ThinLTO.CombinedIndex.collectDefinedGVSummariesPerModule(
848  ModuleToDefinedGVSummaries);
849  // Create entries for any modules that didn't have any GV summaries
850  // (either they didn't have any GVs to start with, or we suppressed
851  // generation of the summaries because they e.g. had inline assembly
852  // uses that couldn't be promoted/renamed on export). This is so
853  // InProcessThinBackend::start can still launch a backend thread, which
854  // is passed the map of summaries for the module, without any special
855  // handling for this case.
856  for (auto &Mod : ThinLTO.ModuleMap)
857  if (!ModuleToDefinedGVSummaries.count(Mod.first))
858  ModuleToDefinedGVSummaries.try_emplace(Mod.first);
859 
860  // Compute "dead" symbols, we don't want to import/export these!
861  DenseSet<GlobalValue::GUID> GUIDPreservedSymbols;
862  for (auto &Res : GlobalResolutions) {
863  if (Res.second.VisibleOutsideThinLTO &&
864  // IRName will be defined if we have seen the prevailing copy of
865  // this value. If not, no need to preserve any ThinLTO copies.
866  !Res.second.IRName.empty())
867  GUIDPreservedSymbols.insert(GlobalValue::getGUID(Res.second.IRName));
868  }
869 
870  auto DeadSymbols =
871  computeDeadSymbols(ThinLTO.CombinedIndex, GUIDPreservedSymbols);
872 
874  ThinLTO.ModuleMap.size());
876  ThinLTO.ModuleMap.size());
878 
879  if (Conf.OptLevel > 0) {
880  ComputeCrossModuleImport(ThinLTO.CombinedIndex, ModuleToDefinedGVSummaries,
881  ImportLists, ExportLists, &DeadSymbols);
882 
883  std::set<GlobalValue::GUID> ExportedGUIDs;
884  for (auto &Res : GlobalResolutions) {
885  // First check if the symbol was flagged as having external references.
886  if (Res.second.Partition != GlobalResolution::External)
887  continue;
888  // IRName will be defined if we have seen the prevailing copy of
889  // this value. If not, no need to mark as exported from a ThinLTO
890  // partition (and we can't get the GUID).
891  if (Res.second.IRName.empty())
892  continue;
893  auto GUID = GlobalValue::getGUID(Res.second.IRName);
894  // Mark exported unless index-based analysis determined it to be dead.
895  if (!DeadSymbols.count(GUID))
896  ExportedGUIDs.insert(GlobalValue::getGUID(Res.second.IRName));
897  }
898 
899  auto isPrevailing = [&](GlobalValue::GUID GUID,
900  const GlobalValueSummary *S) {
901  return ThinLTO.PrevailingModuleForGUID[GUID] == S->modulePath();
902  };
903  auto isExported = [&](StringRef ModuleIdentifier, GlobalValue::GUID GUID) {
904  const auto &ExportList = ExportLists.find(ModuleIdentifier);
905  return (ExportList != ExportLists.end() &&
906  ExportList->second.count(GUID)) ||
907  ExportedGUIDs.count(GUID);
908  };
909  thinLTOInternalizeAndPromoteInIndex(ThinLTO.CombinedIndex, isExported);
910 
911  auto recordNewLinkage = [&](StringRef ModuleIdentifier,
912  GlobalValue::GUID GUID,
913  GlobalValue::LinkageTypes NewLinkage) {
914  ResolvedODR[ModuleIdentifier][GUID] = NewLinkage;
915  };
916 
917  thinLTOResolveWeakForLinkerInIndex(ThinLTO.CombinedIndex, isPrevailing,
918  recordNewLinkage);
919  }
920 
921  std::unique_ptr<ThinBackendProc> BackendProc =
922  ThinLTO.Backend(Conf, ThinLTO.CombinedIndex, ModuleToDefinedGVSummaries,
923  AddStream, Cache);
924 
925  // Task numbers start at ParallelCodeGenParallelismLevel if an LTO
926  // module is present, as tasks 0 through ParallelCodeGenParallelismLevel-1
927  // are reserved for parallel code generation partitions.
928  unsigned Task =
929  HasRegularLTO ? RegularLTO.ParallelCodeGenParallelismLevel : 0;
930  for (auto &Mod : ThinLTO.ModuleMap) {
931  if (Error E = BackendProc->start(Task, Mod.second, ImportLists[Mod.first],
932  ExportLists[Mod.first],
933  ResolvedODR[Mod.first], ThinLTO.ModuleMap))
934  return E;
935  ++Task;
936  }
937 
938  return BackendProc->wait();
939 }
MachineLoop * L
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:882
A parsed version of the target data layout string in and methods for querying it. ...
Definition: DataLayout.h:102
std::error_code getError() const
Definition: ErrorOr.h:169
LinkageTypes getLinkage() const
Definition: GlobalValue.h:429
Represents either an error or a value T.
Definition: ErrorOr.h:68
raw_ostream & errs()
This returns a reference to a raw_ostream for standard error.
This class defines the interface to the ThinLTO backend.
Definition: LTO.cpp:609
std::string CPU
Definition: Config.h:38
std::string AAPipeline
Definition: Config.h:58
std::vector< std::unique_ptr< GlobalValueSummary > > GlobalValueSummaryList
List of global value summary structures for a particular value held in the GlobalValueMap.
unsigned Prevailing
The linker has chosen this definition of the symbol.
Definition: LTO.h:451
CodeGenOpt::Level CGOptLevel
Definition: Config.h:43
uint64_t GUID
Declare a type to represent a global unique identifier for a global value.
Definition: GlobalValue.h:465
const ModuleHash & getModuleHash(const StringRef ModPath) const
Get the module SHA1 hash recorded for the given module path.
unsigned heavyweight_hardware_concurrency()
Get the amount of currency to use for tasks requiring significant memory or other resources...
Definition: Threading.cpp:121
GlobalVariable * collectUsedGlobalVariables(const Module &M, SmallPtrSetImpl< GlobalValue * > &Set, bool CompilerUsed)
Given "llvm.used" or "llvm.compiler.used" as a global name, collect the initializer elements of that ...
Definition: Module.cpp:528
A Module instance is used to store all the information related to an LLVM module. ...
Definition: Module.h:52
GUID getGUID() const
Return a 64-bit global unique ID constructed from global value name (i.e.
Definition: GlobalValue.h:473
Same, but only replaced by something equivalent.
Definition: GlobalValue.h:54
This is a wrapper for ArrayRef<ModuleSymbolTable::Symbol>::iterator that exposes only the information...
Definition: LTO.h:105
Implements a dense probed hash-table based set.
Definition: DenseSet.h:202
std::string OverrideTriple
Setting this field will replace target triples in input files with this triple.
Definition: Config.h:62
Available for inspection, not emission.
Definition: GlobalValue.h:50
unsigned DataSections
Emit data into separate sections.
static ConstantAggregateZero * get(Type *Ty)
Definition: Constants.cpp:1254
iterator end() const
Definition: ArrayRef.h:130
iterator_range< symbol_iterator > symbols()
A range over the symbols in this InputFile.
Definition: LTO.h:217
Type * getValueType() const
Definition: GlobalValue.h:261
bool hasAppendingLinkage() const
Definition: GlobalValue.h:412
size_type count(PtrType Ptr) const
count - Return 1 if the specified pointer is in the set, 0 otherwise.
Definition: SmallPtrSet.h:380
std::unique_ptr< raw_ostream > ResolutionFile
If this field is set, LTO will write input file paths and symbol resolutions here in llvm-lto2 comman...
Definition: Config.h:78
An efficient, type-erasing, non-owning reference to a callable.
Definition: STLExtras.h:83
Expected< bool > hasSummary()
Check if the given bitcode buffer contains a summary block.
Expected< std::unique_ptr< ModuleSummaryIndex > > getSummary()
Parse the specified bitcode buffer, returning the module summary index.
Externally visible function.
Definition: GlobalValue.h:49
The resolution for a symbol.
Definition: LTO.h:446
This class implements a map that also provides access to all stored values in a deterministic order...
Definition: MapVector.h:32
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:736
const StringMap< GVSummaryMapTy > & ModuleToDefinedGVSummaries
Definition: LTO.cpp:613
A class that wrap the SHA1 algorithm.
Definition: SHA1.h:29
static Expected< std::unique_ptr< InputFile > > create(MemoryBufferRef Object)
Create an InputFile.
Definition: LTO.cpp:234
Error takeError()
Take ownership of the stored error.
void setAlignment(unsigned Align)
Definition: Globals.cpp:86
const std::string & getTargetTriple() const
Get the target triple which is a string describing the target host.
Definition: Module.h:218
Tentative definitions.
Definition: GlobalValue.h:59
static bool isLocalLinkage(LinkageTypes Linkage)
Definition: GlobalValue.h:300
static bool isLinkOnceLinkage(LinkageTypes Linkage)
Definition: GlobalValue.h:279
StringRef getName() const
Return a constant reference to the value's name.
Definition: Value.cpp:191
std::unique_ptr< Module > Mod
Definition: LTO.cpp:225
std::function< std::unique_ptr< NativeObjectStream >unsigned Task)> AddStreamFn
This type defines the callback to add a native object that is generated on the fly.
Definition: LTO.h:253
std::unordered_set< GlobalValue::GUID > ExportSetTy
The set contains an entry for every global value the module exports.
Expected< std::vector< BitcodeModule > > getBitcodeModuleList(MemoryBufferRef Buffer)
Returns a list of modules in the specified bitcode buffer.
Represents a module in a bitcode file.
Definition: BitcodeReader.h:44
virtual Error start(unsigned Task, BitcodeModule BM, const FunctionImporter::ImportMapTy &ImportList, const FunctionImporter::ExportSetTy &ExportList, const std::map< GlobalValue::GUID, GlobalValue::LinkageTypes > &ResolvedODR, MapVector< StringRef, BitcodeModule > &ModuleMap)=0
std::vector< std::string > MAttrs
Definition: Config.h:40
ModuleSummaryIndex & CombinedIndex
Definition: LTO.cpp:612
void setName(const Twine &Name)
Change the name of the value.
Definition: Value.cpp:257
Tagged union holding either a T or a Error.
size_type count(StringRef Key) const
count - Return 1 if the element is in the map, 0 otherwise.
Definition: StringMap.h:341
StringRef result()
Return a reference to the current raw 160-bits SHA1 for the digested data since the last call to init...
Definition: SHA1.cpp:261
void eraseFromParent() override
eraseFromParent - This method unlinks 'this' from the containing module and deletes it...
Definition: Globals.cpp:319
Expected< int > getComdatIndex() const
Definition: LTO.cpp:278
static bool isLinkOnceODRLinkage(LinkageTypes Linkage)
Definition: GlobalValue.h:276
static std::string toHex(StringRef Input)
Convert buffer Input to its hexadecimal representation.
Definition: StringExtras.h:65
unsigned getCommonAlignment() const
Definition: LTO.h:180
#define F(x, y, z)
Definition: MD5.cpp:51
Class to represent array types.
Definition: DerivedTypes.h:345
std::string SampleProfile
Sample PGO profile path.
Definition: Config.h:69
TargetOptions Options
Definition: Config.h:39
const StringMap< std::pair< uint64_t, ModuleHash > > & modulePaths() const
Table of modules, containing module hash and id.
unsigned FunctionSections
Emit functions into separate sections.
SmallString - A SmallString is just a SmallVector with methods and accessors that make it work better...
Definition: SmallString.h:26
An input file.
Definition: LTO.h:77
A ThreadPool for asynchronous parallel execution on a defined number of threads.
Definition: ThreadPool.h:51
void replaceAllUsesWith(Value *V)
Change all uses of this to point to a new Value.
Definition: Value.cpp:401
static ErrorOr< MemoryBufferRef > findBitcodeInMemBuffer(MemoryBufferRef Object)
Finds and returns bitcode in the given memory buffer (which may be either a bitcode file or a native ...
void takeName(Value *V)
Transfer the name from V to this value.
Definition: Value.cpp:263
Class to hold module path string table and global value map, and encapsulate methods for operating on...
void update(ArrayRef< uint8_t > Data)
Digest more data.
Definition: SHA1.cpp:213
static GCRegistry::Add< CoreCLRGC > E("coreclr","CoreCLR-compatible GC")
bool isWeakForLinker() const
Definition: GlobalValue.h:435
static Constant * getBitCast(Constant *C, Type *Ty, bool OnlyIfReduced=false)
Definition: Constants.cpp:1695
Error add(std::unique_ptr< InputFile > Obj, ArrayRef< SymbolResolution > Res)
Add an input file to the LTO link, using the provided symbol resolutions.
Definition: LTO.cpp:378
DebuggerKind DebuggerTuning
Which debugger to tune for.
#define P(N)
Same, but only replaced by something equivalent.
Definition: GlobalValue.h:52
StringRef getSourceFileName() const
Returns the source file path specified at compile time.
Definition: LTO.cpp:297
std::function< AddStreamFn(unsigned Task, StringRef Key)> NativeObjectCache
This is the type of a native object cache.
Definition: LTO.h:268
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.
static bool isWeakODRLinkage(LinkageTypes Linkage)
Definition: GlobalValue.h:285
const Comdat * getComdat() const
Definition: GlobalObject.h:92
Error errorCodeToError(std::error_code EC)
Helper for converting an std::error_code to a Error.
ThinBackend createWriteIndexesThinBackend(std::string OldPrefix, std::string NewPrefix, bool ShouldEmitImportsFiles, std::string LinkedObjectsFile)
This ThinBackend writes individual module indexes to files, instead of running the individual backend...
Definition: LTO.cpp:822
bool UpgradeDebugInfo(Module &M)
Check the debug info version number, if it is out-dated, drop the debug info.
ThinBackendProc(Config &Conf, ModuleSummaryIndex &CombinedIndex, const StringMap< GVSummaryMapTy > &ModuleToDefinedGVSummaries)
Definition: LTO.cpp:616
unsigned OptLevel
Definition: Config.h:44
void replace_path_prefix(SmallVectorImpl< char > &Path, const StringRef &OldPrefix, const StringRef &NewPrefix)
Replace matching path prefix with another path.
Definition: Path.cpp:525
LTO configuration.
Definition: Config.h:35
std::string DefaultTriple
Setting this field will replace unspecified target triples in input files with this triple...
Definition: Config.h:66
iterator begin() const
Definition: ArrayRef.h:129
StringRef getName() const
Returns the path to the InputFile.
Definition: LTO.cpp:293
StringRef getName() const
Returns the mangled name of the global.
Definition: LTO.h:149
Function and variable summary information to aid decisions and implementation of importing.
static GUID getGUID(StringRef GlobalName)
Return a 64-bit global unique ID constructed from global value name (i.e.
Definition: GlobalValue.h:469
void ComputeCrossModuleImport(const ModuleSummaryIndex &Index, const StringMap< GVSummaryMapTy > &ModuleToDefinedGVSummaries, StringMap< FunctionImporter::ImportMapTy > &ImportLists, StringMap< FunctionImporter::ExportSetTy > &ExportLists, const DenseSet< GlobalValue::GUID > *DeadSymbols=nullptr)
Compute all the imports and exports for every module in the Index.
static LinkageTypes getWeakLinkage(bool ODR)
Definition: GlobalValue.h:266
static void writeToResolutionFile(raw_ostream &OS, InputFile *Input, ArrayRef< SymbolResolution > Res)
Definition: LTO.cpp:357
std::pair< iterator, bool > try_emplace(StringRef Key, ArgsTy &&...Args)
Emplace a new element for the specified key into the map if the key isn't already in the map...
Definition: StringMap.h:377
Error thinBackend(Config &C, unsigned Task, AddStreamFn AddStream, Module &M, ModuleSummaryIndex &CombinedIndex, const FunctionImporter::ImportMapTy &ImportList, const GVSummaryMapTy &DefinedGlobals, MapVector< StringRef, BitcodeModule > &ModuleMap)
Runs a ThinLTO backend.
Definition: LTOBackend.cpp:317
iterator_range< T > make_range(T x, T y)
Convenience function for iterating over sub-ranges.
static ErrorSuccess success()
Create a success value.
bool hasGlobalUnnamedAddr() const
Definition: GlobalValue.h:187
SmallPtrSet - This class implements a set which is optimized for holding SmallSize or less elements...
Definition: SmallPtrSet.h:425
std::function< std::unique_ptr< ThinBackendProc > Config &C, ModuleSummaryIndex &CombinedIndex, StringMap< GVSummaryMapTy > &ModuleToDefinedGVSummaries, AddStreamFn AddStream, NativeObjectCache Cache)> ThinBackend
A ThinBackend defines what happens after the thin-link phase during ThinLTO.
Definition: LTO.h:277
uint64_t getTypeAllocSize(Type *Ty) const
Returns the offset in bytes between successive objects of the specified type, including alignment pad...
Definition: DataLayout.h:408
static void computeCacheKey(SmallString< 40 > &Key, const 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)
Definition: LTO.cpp:52
Keep one copy of function when linking (inline)
Definition: GlobalValue.h:51
static void thinLTOInternalizeAndPromoteGUID(GlobalValueSummaryList &GVSummaryList, GlobalValue::GUID GUID, function_ref< bool(StringRef, GlobalValue::GUID)> isExported)
Definition: LTO.cpp:202
virtual ~ThinBackendProc()
Definition: LTO.cpp:621
reference get()
Returns a reference to the stored T value.
std::error_code EmitImportsFiles(StringRef ModulePath, StringRef OutputFilename, const FunctionImporter::ImportMapTy &ModuleImports)
Emit into OutputFilename the files module ModulePath will import from.
static void thinLTOResolveWeakForLinkerGUID(GlobalValueSummaryList &GVSummaryList, GlobalValue::GUID GUID, DenseSet< GlobalValueSummary * > &GlobalInvolvedWithAlias, function_ref< bool(GlobalValue::GUID, const GlobalValueSummary *)> isPrevailing, function_ref< void(StringRef, GlobalValue::GUID, GlobalValue::LinkageTypes)> recordNewLinkage)
Definition: LTO.cpp:141
void setLinkage(LinkageTypes LT)
Definition: GlobalValue.h:424
static GCRegistry::Add< ShadowStackGC > C("shadow-stack","Very portable GC for uncooperative code generators")
StringRef getModuleIdentifier() const
Definition: BitcodeReader.h:74
virtual Error wait()=0
A range adaptor for a pair of iterators.
StringMap - This is an unconventional map that is specialized for handling keys that are "strings"...
Definition: StringMap.h:223
StringRef parent_path(StringRef path)
Get parent path.
Definition: Path.cpp:493
StringRef str() const
Explicit conversion to StringRef.
Definition: SmallString.h:267
LinkageTypes
An enumeration for the kinds of linkage for global values.
Definition: GlobalValue.h:48
std::string OptPipeline
If this field is set, the set of passes run in the middle-end optimizer will be the one specified by ...
Definition: Config.h:53
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 ...
const GlobalObject * getBaseObject() const
Definition: GlobalValue.h:517
unsigned RelaxELFRelocations
LLVM_NODISCARD LLVM_ATTRIBUTE_ALWAYS_INLINE bool empty() const
empty - Check if the string is empty.
Definition: StringRef.h:130
ThinBackend createInProcessThinBackend(unsigned ParallelismLevel)
This ThinBackend runs the individual backend jobs in-process.
Definition: LTO.cpp:732
PointerType * getType() const
Global values are always pointers.
Definition: GlobalValue.h:259
std::map< GlobalValue::GUID, GlobalValueSummary * > GVSummaryMapTy
Map of global value GUID to its summary, used to identify values defined in a particular module...
A raw_ostream that writes to a file descriptor.
Definition: raw_ostream.h:357
LTO(Config Conf, ThinBackend Backend=nullptr, unsigned ParallelCodeGenParallelismLevel=1)
Create an LTO object.
Definition: LTO.cpp:319
void setUnnamedAddr(UnnamedAddr Val)
Definition: GlobalValue.h:203
size_type count(const ValueT &V) const
Return 1 if the specified key is in the set, 0 otherwise.
Definition: DenseSet.h:81
void thinLTOResolveWeakForLinkerInIndex(ModuleSummaryIndex &Index, function_ref< bool(GlobalValue::GUID, const GlobalValueSummary *)> isPrevailing, function_ref< void(StringRef, GlobalValue::GUID, GlobalValue::LinkageTypes)> recordNewLinkage)
Resolve Weak and LinkOnce values in the Index.
Definition: LTO.cpp:182
static ErrorOr< std::unique_ptr< MemoryBuffer > > getFile(const Twine &Filename, int64_t FileSize=-1, bool RequiresNullTerminator=true, bool IsVolatileSize=false)
Open the specified file as a MemoryBuffer, returning a new MemoryBuffer if successful, otherwise returning null.
DenseSet< GlobalValue::GUID > computeDeadSymbols(const ModuleSummaryIndex &Index, const DenseSet< GlobalValue::GUID > &GUIDPreservedSymbols)
Compute all the symbols that are "dead": i.e these that can't be reached in the graph from any of t...
Error joinErrors(Error E1, Error E2)
Concatenate errors.
Reloc::Model RelocModel
Definition: Config.h:41
Error backend(Config &C, AddStreamFn AddStream, unsigned ParallelCodeGenParallelismLevel, std::unique_ptr< Module > M)
Runs a regular LTO backend.
Definition: LTOBackend.cpp:292
#define I(x, y, z)
Definition: MD5.cpp:54
ArrayRef< Symbol > symbols() const
static ArrayType * get(Type *ElementType, uint64_t NumElements)
This static method is the primary way to construct an ArrayType.
Definition: Type.cpp:606
void setTargetTriple(StringRef T)
Set the target triple.
Definition: Module.h:254
Keep one copy of named function when linking (weak)
Definition: GlobalValue.h:53
Rename collisions when linking (static functions).
Definition: GlobalValue.h:56
CodeModel::Model CodeModel
Definition: Config.h:42
unsigned VisibleToRegularObj
The definition of this symbol is visible outside of the LTO unit.
Definition: LTO.h:458
bool hasLocalLinkage() const
Definition: GlobalValue.h:415
uint32_t getFlags() const
Definition: LTO.h:151
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
std::function< void(GlobalValue &)> ValueAdder
Definition: IRMover.h:65
Lightweight error class with error context and mandatory checking.
This class implements an extremely fast bulk output stream that can only output to a stream...
Definition: raw_ostream.h:44
Expected< std::unique_ptr< Module > > getLazyModule(LLVMContext &Context, bool ShouldLazyLoadMetadata, bool IsImporting)
Read the bitcode module and prepare for lazy deserialization of function bodies.
Expected< std::unique_ptr< Module > > parseModule(LLVMContext &Context)
Read the entire bitcode module and return it.
iterator_range< global_iterator > globals()
Definition: Module.h:524
A derived class of LLVMContext that initializes itself according to a given Config object...
Definition: Config.h:164
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:47
uint64_t getCommonSize() const
Definition: LTO.h:173
const std::string & getDataLayoutStr() const
Get the data layout string for the module's target platform.
Definition: Module.h:209
This pass exposes codegen information to IR-level passes.
static GCRegistry::Add< ErlangGC > A("erlang","erlang-compatible garbage collector")
llvm::Error materializeMetadata()
Definition: Module.cpp:423
static IntegerType * getInt8Ty(LLVMContext &C)
Definition: Type.cpp:167
std::string getThinLTOOutputFile(const std::string &Path, const std::string &OldPrefix, const std::string &NewPrefix)
Given the original Path to an output file, replace any path prefix matching OldPrefix with NewPrefix...
Definition: LTO.cpp:745
void thinLTOInternalizeAndPromoteInIndex(ModuleSummaryIndex &Index, function_ref< bool(StringRef, GlobalValue::GUID)> isExported)
Update the linkages in the given Index to mark exported values as external and non-exported values as...
Definition: LTO.cpp:216
std::error_code inconvertibleErrorCode()
The value returned by this function can be returned from convertToErrorCode for Error values where no...