LLVM  7.0.0svn
FunctionImport.cpp
Go to the documentation of this file.
1 //===- FunctionImport.cpp - ThinLTO Summary-based Function Import ---------===//
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 Function import based on summaries.
11 //
12 //===----------------------------------------------------------------------===//
13 
15 #include "llvm/ADT/ArrayRef.h"
16 #include "llvm/ADT/STLExtras.h"
17 #include "llvm/ADT/SetVector.h"
18 #include "llvm/ADT/SmallVector.h"
19 #include "llvm/ADT/Statistic.h"
20 #include "llvm/ADT/StringMap.h"
21 #include "llvm/ADT/StringRef.h"
22 #include "llvm/ADT/StringSet.h"
24 #include "llvm/IR/AutoUpgrade.h"
25 #include "llvm/IR/Constants.h"
26 #include "llvm/IR/Function.h"
27 #include "llvm/IR/GlobalAlias.h"
28 #include "llvm/IR/GlobalObject.h"
29 #include "llvm/IR/GlobalValue.h"
30 #include "llvm/IR/GlobalVariable.h"
31 #include "llvm/IR/Metadata.h"
32 #include "llvm/IR/Module.h"
34 #include "llvm/IRReader/IRReader.h"
35 #include "llvm/Linker/IRMover.h"
38 #include "llvm/Pass.h"
39 #include "llvm/Support/Casting.h"
41 #include "llvm/Support/Debug.h"
42 #include "llvm/Support/Error.h"
45 #include "llvm/Support/SourceMgr.h"
51 #include <cassert>
52 #include <memory>
53 #include <set>
54 #include <string>
55 #include <system_error>
56 #include <tuple>
57 #include <utility>
58 
59 using namespace llvm;
60 
61 #define DEBUG_TYPE "function-import"
62 
63 STATISTIC(NumImportedFunctions, "Number of functions imported");
64 STATISTIC(NumImportedGlobalVars, "Number of global variables imported");
65 STATISTIC(NumImportedModules, "Number of modules imported from");
66 STATISTIC(NumDeadSymbols, "Number of dead stripped symbols in index");
67 STATISTIC(NumLiveSymbols, "Number of live symbols in index");
68 
69 /// Limit on instruction count of imported functions.
71  "import-instr-limit", cl::init(100), cl::Hidden, cl::value_desc("N"),
72  cl::desc("Only import functions with less than N instructions"));
73 
74 static cl::opt<float>
75  ImportInstrFactor("import-instr-evolution-factor", cl::init(0.7),
77  cl::desc("As we import functions, multiply the "
78  "`import-instr-limit` threshold by this factor "
79  "before processing newly imported functions"));
80 
82  "import-hot-evolution-factor", cl::init(1.0), cl::Hidden,
83  cl::value_desc("x"),
84  cl::desc("As we import functions called from hot callsite, multiply the "
85  "`import-instr-limit` threshold by this factor "
86  "before processing newly imported functions"));
87 
89  "import-hot-multiplier", cl::init(10.0), cl::Hidden, cl::value_desc("x"),
90  cl::desc("Multiply the `import-instr-limit` threshold for hot callsites"));
91 
93  "import-critical-multiplier", cl::init(100.0), cl::Hidden,
94  cl::value_desc("x"),
95  cl::desc(
96  "Multiply the `import-instr-limit` threshold for critical callsites"));
97 
98 // FIXME: This multiplier was not really tuned up.
100  "import-cold-multiplier", cl::init(0), cl::Hidden, cl::value_desc("N"),
101  cl::desc("Multiply the `import-instr-limit` threshold for cold callsites"));
102 
103 static cl::opt<bool> PrintImports("print-imports", cl::init(false), cl::Hidden,
104  cl::desc("Print imported functions"));
105 
106 static cl::opt<bool> ComputeDead("compute-dead", cl::init(true), cl::Hidden,
107  cl::desc("Compute dead symbols"));
108 
110  "enable-import-metadata", cl::init(
111 #if !defined(NDEBUG)
112  true /*Enabled with asserts.*/
113 #else
114  false
115 #endif
116  ),
117  cl::Hidden, cl::desc("Enable import metadata like 'thinlto_src_module'"));
118 
119 /// Summary file to use for function importing when using -function-import from
120 /// the command line.
122  SummaryFile("summary-file",
123  cl::desc("The summary file to use for function importing."));
124 
125 /// Used when testing importing from distributed indexes via opt
126 // -function-import.
127 static cl::opt<bool>
128  ImportAllIndex("import-all-index",
129  cl::desc("Import all external functions in index."));
130 
131 // Load lazily a module from \p FileName in \p Context.
132 static std::unique_ptr<Module> loadFile(const std::string &FileName,
133  LLVMContext &Context) {
134  SMDiagnostic Err;
135  DEBUG(dbgs() << "Loading '" << FileName << "'\n");
136  // Metadata isn't loaded until functions are imported, to minimize
137  // the memory overhead.
138  std::unique_ptr<Module> Result =
139  getLazyIRFileModule(FileName, Err, Context,
140  /* ShouldLazyLoadMetadata = */ true);
141  if (!Result) {
142  Err.print("function-import", errs());
143  report_fatal_error("Abort");
144  }
145 
146  return Result;
147 }
148 
149 /// Given a list of possible callee implementation for a call site, select one
150 /// that fits the \p Threshold.
151 ///
152 /// FIXME: select "best" instead of first that fits. But what is "best"?
153 /// - The smallest: more likely to be inlined.
154 /// - The one with the least outgoing edges (already well optimized).
155 /// - One from a module already being imported from in order to reduce the
156 /// number of source modules parsed/linked.
157 /// - One that has PGO data attached.
158 /// - [insert you fancy metric here]
159 static const GlobalValueSummary *
161  ArrayRef<std::unique_ptr<GlobalValueSummary>> CalleeSummaryList,
162  unsigned Threshold, StringRef CallerModulePath) {
163  auto It = llvm::find_if(
164  CalleeSummaryList,
165  [&](const std::unique_ptr<GlobalValueSummary> &SummaryPtr) {
166  auto *GVSummary = SummaryPtr.get();
167  if (!Index.isGlobalValueLive(GVSummary))
168  return false;
169 
170  // For SamplePGO, in computeImportForFunction the OriginalId
171  // may have been used to locate the callee summary list (See
172  // comment there).
173  // The mapping from OriginalId to GUID may return a GUID
174  // that corresponds to a static variable. Filter it out here.
175  // This can happen when
176  // 1) There is a call to a library function which is not defined
177  // in the index.
178  // 2) There is a static variable with the OriginalGUID identical
179  // to the GUID of the library function in 1);
180  // When this happens, the logic for SamplePGO kicks in and
181  // the static variable in 2) will be found, which needs to be
182  // filtered out.
183  if (GVSummary->getSummaryKind() == GlobalValueSummary::GlobalVarKind)
184  return false;
185  if (GlobalValue::isInterposableLinkage(GVSummary->linkage()))
186  // There is no point in importing these, we can't inline them
187  return false;
188 
189  auto *Summary = cast<FunctionSummary>(GVSummary->getBaseObject());
190 
191  // If this is a local function, make sure we import the copy
192  // in the caller's module. The only time a local function can
193  // share an entry in the index is if there is a local with the same name
194  // in another module that had the same source file name (in a different
195  // directory), where each was compiled in their own directory so there
196  // was not distinguishing path.
197  // However, do the import from another module if there is only one
198  // entry in the list - in that case this must be a reference due
199  // to indirect call profile data, since a function pointer can point to
200  // a local in another module.
201  if (GlobalValue::isLocalLinkage(Summary->linkage()) &&
202  CalleeSummaryList.size() > 1 &&
203  Summary->modulePath() != CallerModulePath)
204  return false;
205 
206  if (Summary->instCount() > Threshold)
207  return false;
208 
209  if (Summary->notEligibleToImport())
210  return false;
211 
212  return true;
213  });
214  if (It == CalleeSummaryList.end())
215  return nullptr;
216 
217  return cast<GlobalValueSummary>(It->get());
218 }
219 
220 namespace {
221 
222 using EdgeInfo = std::tuple<const FunctionSummary *, unsigned /* Threshold */,
224 
225 } // anonymous namespace
226 
227 static ValueInfo
229  if (!VI.getSummaryList().empty())
230  return VI;
231  // For SamplePGO, the indirect call targets for local functions will
232  // have its original name annotated in profile. We try to find the
233  // corresponding PGOFuncName as the GUID.
234  // FIXME: Consider updating the edges in the graph after building
235  // it, rather than needing to perform this mapping on each walk.
236  auto GUID = Index.getGUIDFromOriginalID(VI.getGUID());
237  if (GUID == 0)
238  return ValueInfo();
239  return Index.getValueInfo(GUID);
240 }
241 
243  const FunctionSummary &Summary, const GVSummaryMapTy &DefinedGVSummaries,
244  FunctionImporter::ImportMapTy &ImportList,
246  for (auto &VI : Summary.refs()) {
247  if (DefinedGVSummaries.count(VI.getGUID())) {
248  DEBUG(dbgs() << "Ref ignored! Target already in destination module.\n");
249  continue;
250  }
251 
252  DEBUG(dbgs() << " ref -> " << VI.getGUID() << "\n");
253 
254  for (auto &RefSummary : VI.getSummaryList())
255  if (RefSummary->getSummaryKind() == GlobalValueSummary::GlobalVarKind &&
256  // Don't try to import regular LTO summaries added to dummy module.
257  !RefSummary->modulePath().empty() &&
258  !GlobalValue::isInterposableLinkage(RefSummary->linkage()) &&
259  // For now we don't import global variables which have outgoing
260  // refs. Otherwise we have to promote referenced vars/functions.
261  RefSummary->refs().empty()) {
262  ImportList[RefSummary->modulePath()][VI.getGUID()] = 1;
263  if (ExportLists)
264  (*ExportLists)[RefSummary->modulePath()].insert(VI.getGUID());
265  }
266  }
267 }
268 
269 /// Compute the list of functions to import for a given caller. Mark these
270 /// imported functions and the symbols they reference in their source module as
271 /// exported from their source module.
273  const FunctionSummary &Summary, const ModuleSummaryIndex &Index,
274  const unsigned Threshold, const GVSummaryMapTy &DefinedGVSummaries,
275  SmallVectorImpl<EdgeInfo> &Worklist,
276  FunctionImporter::ImportMapTy &ImportList,
277  StringMap<FunctionImporter::ExportSetTy> *ExportLists = nullptr) {
278  computeImportForReferencedGlobals(Summary, DefinedGVSummaries, ImportList,
279  ExportLists);
280  for (auto &Edge : Summary.calls()) {
281  ValueInfo VI = Edge.first;
282  DEBUG(dbgs() << " edge -> " << VI.getGUID() << " Threshold:" << Threshold
283  << "\n");
284 
285  VI = updateValueInfoForIndirectCalls(Index, VI);
286  if (!VI)
287  continue;
288 
289  if (DefinedGVSummaries.count(VI.getGUID())) {
290  DEBUG(dbgs() << "ignored! Target already in destination module.\n");
291  continue;
292  }
293 
294  auto GetBonusMultiplier = [](CalleeInfo::HotnessType Hotness) -> float {
295  if (Hotness == CalleeInfo::HotnessType::Hot)
296  return ImportHotMultiplier;
297  if (Hotness == CalleeInfo::HotnessType::Cold)
298  return ImportColdMultiplier;
299  if (Hotness == CalleeInfo::HotnessType::Critical)
301  return 1.0;
302  };
303 
304  const auto NewThreshold =
305  Threshold * GetBonusMultiplier(Edge.second.getHotness());
306 
307  auto *CalleeSummary = selectCallee(Index, VI.getSummaryList(), NewThreshold,
308  Summary.modulePath());
309  if (!CalleeSummary) {
310  DEBUG(dbgs() << "ignored! No qualifying callee with summary found.\n");
311  continue;
312  }
313 
314  // "Resolve" the summary
315  const auto *ResolvedCalleeSummary = cast<FunctionSummary>(CalleeSummary->getBaseObject());
316 
317  assert(ResolvedCalleeSummary->instCount() <= NewThreshold &&
318  "selectCallee() didn't honor the threshold");
319 
320  auto GetAdjustedThreshold = [](unsigned Threshold, bool IsHotCallsite) {
321  // Adjust the threshold for next level of imported functions.
322  // The threshold is different for hot callsites because we can then
323  // inline chains of hot calls.
324  if (IsHotCallsite)
325  return Threshold * ImportHotInstrFactor;
326  return Threshold * ImportInstrFactor;
327  };
328 
329  bool IsHotCallsite =
330  Edge.second.getHotness() == CalleeInfo::HotnessType::Hot;
331  const auto AdjThreshold = GetAdjustedThreshold(Threshold, IsHotCallsite);
332 
333  auto ExportModulePath = ResolvedCalleeSummary->modulePath();
334  auto &ProcessedThreshold = ImportList[ExportModulePath][VI.getGUID()];
335  /// Since the traversal of the call graph is DFS, we can revisit a function
336  /// a second time with a higher threshold. In this case, it is added back to
337  /// the worklist with the new threshold.
338  if (ProcessedThreshold && ProcessedThreshold >= AdjThreshold) {
339  DEBUG(dbgs() << "ignored! Target was already seen with Threshold "
340  << ProcessedThreshold << "\n");
341  continue;
342  }
343  bool PreviouslyImported = ProcessedThreshold != 0;
344  // Mark this function as imported in this module, with the current Threshold
345  ProcessedThreshold = AdjThreshold;
346 
347  // Make exports in the source module.
348  if (ExportLists) {
349  auto &ExportList = (*ExportLists)[ExportModulePath];
350  ExportList.insert(VI.getGUID());
351  if (!PreviouslyImported) {
352  // This is the first time this function was exported from its source
353  // module, so mark all functions and globals it references as exported
354  // to the outside if they are defined in the same source module.
355  // For efficiency, we unconditionally add all the referenced GUIDs
356  // to the ExportList for this module, and will prune out any not
357  // defined in the module later in a single pass.
358  for (auto &Edge : ResolvedCalleeSummary->calls()) {
359  auto CalleeGUID = Edge.first.getGUID();
360  ExportList.insert(CalleeGUID);
361  }
362  for (auto &Ref : ResolvedCalleeSummary->refs()) {
363  auto GUID = Ref.getGUID();
364  ExportList.insert(GUID);
365  }
366  }
367  }
368 
369  // Insert the newly imported function to the worklist.
370  Worklist.emplace_back(ResolvedCalleeSummary, AdjThreshold, VI.getGUID());
371  }
372 }
373 
374 /// Given the list of globals defined in a module, compute the list of imports
375 /// as well as the list of "exports", i.e. the list of symbols referenced from
376 /// another module (that may require promotion).
378  const GVSummaryMapTy &DefinedGVSummaries, const ModuleSummaryIndex &Index,
379  FunctionImporter::ImportMapTy &ImportList,
380  StringMap<FunctionImporter::ExportSetTy> *ExportLists = nullptr) {
381  // Worklist contains the list of function imported in this module, for which
382  // we will analyse the callees and may import further down the callgraph.
384 
385  // Populate the worklist with the import for the functions in the current
386  // module
387  for (auto &GVSummary : DefinedGVSummaries) {
388  if (!Index.isGlobalValueLive(GVSummary.second)) {
389  DEBUG(dbgs() << "Ignores Dead GUID: " << GVSummary.first << "\n");
390  continue;
391  }
392  auto *FuncSummary =
393  dyn_cast<FunctionSummary>(GVSummary.second->getBaseObject());
394  if (!FuncSummary)
395  // Skip import for global variables
396  continue;
397  DEBUG(dbgs() << "Initialize import for " << GVSummary.first << "\n");
398  computeImportForFunction(*FuncSummary, Index, ImportInstrLimit,
399  DefinedGVSummaries, Worklist, ImportList,
400  ExportLists);
401  }
402 
403  // Process the newly imported functions and add callees to the worklist.
404  while (!Worklist.empty()) {
405  auto FuncInfo = Worklist.pop_back_val();
406  auto *Summary = std::get<0>(FuncInfo);
407  auto Threshold = std::get<1>(FuncInfo);
408  auto GUID = std::get<2>(FuncInfo);
409 
410  // Check if we later added this summary with a higher threshold.
411  // If so, skip this entry.
412  auto ExportModulePath = Summary->modulePath();
413  auto &LatestProcessedThreshold = ImportList[ExportModulePath][GUID];
414  if (LatestProcessedThreshold > Threshold)
415  continue;
416 
417  computeImportForFunction(*Summary, Index, Threshold, DefinedGVSummaries,
418  Worklist, ImportList, ExportLists);
419  }
420 }
421 
422 #ifndef NDEBUG
425  if (const auto &VI = Index.getValueInfo(G)) {
426  auto SL = VI.getSummaryList();
427  if (!SL.empty())
428  return SL[0]->getSummaryKind() == GlobalValueSummary::GlobalVarKind;
429  }
430  return false;
431 }
432 
434 
435 static GlobalValue::GUID
436 getGUID(const std::pair<const GlobalValue::GUID, unsigned> &P) {
437  return P.first;
438 }
439 
440 template <class T>
442  unsigned NumGVS = 0;
443  for (auto &V : Cont)
444  if (isGlobalVarSummary(Index, getGUID(V)))
445  ++NumGVS;
446  return NumGVS;
447 }
448 #endif
449 
450 /// Compute all the import and export for every module using the Index.
452  const ModuleSummaryIndex &Index,
453  const StringMap<GVSummaryMapTy> &ModuleToDefinedGVSummaries,
456  // For each module that has function defined, compute the import/export lists.
457  for (auto &DefinedGVSummaries : ModuleToDefinedGVSummaries) {
458  auto &ImportList = ImportLists[DefinedGVSummaries.first()];
459  DEBUG(dbgs() << "Computing import for Module '"
460  << DefinedGVSummaries.first() << "'\n");
461  ComputeImportForModule(DefinedGVSummaries.second, Index, ImportList,
462  &ExportLists);
463  }
464 
465  // When computing imports we added all GUIDs referenced by anything
466  // imported from the module to its ExportList. Now we prune each ExportList
467  // of any not defined in that module. This is more efficient than checking
468  // while computing imports because some of the summary lists may be long
469  // due to linkonce (comdat) copies.
470  for (auto &ELI : ExportLists) {
471  const auto &DefinedGVSummaries =
472  ModuleToDefinedGVSummaries.lookup(ELI.first());
473  for (auto EI = ELI.second.begin(); EI != ELI.second.end();) {
474  if (!DefinedGVSummaries.count(*EI))
475  EI = ELI.second.erase(EI);
476  else
477  ++EI;
478  }
479  }
480 
481 #ifndef NDEBUG
482  DEBUG(dbgs() << "Import/Export lists for " << ImportLists.size()
483  << " modules:\n");
484  for (auto &ModuleImports : ImportLists) {
485  auto ModName = ModuleImports.first();
486  auto &Exports = ExportLists[ModName];
487  unsigned NumGVS = numGlobalVarSummaries(Index, Exports);
488  DEBUG(dbgs() << "* Module " << ModName << " exports "
489  << Exports.size() - NumGVS << " functions and " << NumGVS
490  << " vars. Imports from "
491  << ModuleImports.second.size() << " modules.\n");
492  for (auto &Src : ModuleImports.second) {
493  auto SrcModName = Src.first();
494  unsigned NumGVSPerMod = numGlobalVarSummaries(Index, Src.second);
495  DEBUG(dbgs() << " - " << Src.second.size() - NumGVSPerMod
496  << " functions imported from " << SrcModName << "\n");
497  DEBUG(dbgs() << " - " << NumGVSPerMod << " global vars imported from "
498  << SrcModName << "\n");
499  }
500  }
501 #endif
502 }
503 
504 #ifndef NDEBUG
506  StringRef ModulePath,
507  FunctionImporter::ImportMapTy &ImportList) {
508  DEBUG(dbgs() << "* Module " << ModulePath << " imports from "
509  << ImportList.size() << " modules.\n");
510  for (auto &Src : ImportList) {
511  auto SrcModName = Src.first();
512  unsigned NumGVSPerMod = numGlobalVarSummaries(Index, Src.second);
513  DEBUG(dbgs() << " - " << Src.second.size() - NumGVSPerMod
514  << " functions imported from " << SrcModName << "\n");
515  DEBUG(dbgs() << " - " << NumGVSPerMod << " vars imported from "
516  << SrcModName << "\n");
517  }
518 }
519 #endif
520 
521 /// Compute all the imports for the given module in the Index.
523  StringRef ModulePath, const ModuleSummaryIndex &Index,
524  FunctionImporter::ImportMapTy &ImportList) {
525  // Collect the list of functions this module defines.
526  // GUID -> Summary
527  GVSummaryMapTy FunctionSummaryMap;
528  Index.collectDefinedFunctionsForModule(ModulePath, FunctionSummaryMap);
529 
530  // Compute the import list for this module.
531  DEBUG(dbgs() << "Computing import for Module '" << ModulePath << "'\n");
532  ComputeImportForModule(FunctionSummaryMap, Index, ImportList);
533 
534 #ifndef NDEBUG
535  dumpImportListForModule(Index, ModulePath, ImportList);
536 #endif
537 }
538 
539 // Mark all external summaries in Index for import into the given module.
540 // Used for distributed builds using a distributed index.
542  StringRef ModulePath, const ModuleSummaryIndex &Index,
543  FunctionImporter::ImportMapTy &ImportList) {
544  for (auto &GlobalList : Index) {
545  // Ignore entries for undefined references.
546  if (GlobalList.second.SummaryList.empty())
547  continue;
548 
549  auto GUID = GlobalList.first;
550  assert(GlobalList.second.SummaryList.size() == 1 &&
551  "Expected individual combined index to have one summary per GUID");
552  auto &Summary = GlobalList.second.SummaryList[0];
553  // Skip the summaries for the importing module. These are included to
554  // e.g. record required linkage changes.
555  if (Summary->modulePath() == ModulePath)
556  continue;
557  // Doesn't matter what value we plug in to the map, just needs an entry
558  // to provoke importing by thinBackend.
559  ImportList[Summary->modulePath()][GUID] = 1;
560  }
561 #ifndef NDEBUG
562  dumpImportListForModule(Index, ModulePath, ImportList);
563 #endif
564 }
565 
568  const DenseSet<GlobalValue::GUID> &GUIDPreservedSymbols,
571  if (!ComputeDead)
572  return;
573  if (GUIDPreservedSymbols.empty())
574  // Don't do anything when nothing is live, this is friendly with tests.
575  return;
576  unsigned LiveSymbols = 0;
578  Worklist.reserve(GUIDPreservedSymbols.size() * 2);
579  for (auto GUID : GUIDPreservedSymbols) {
580  ValueInfo VI = Index.getValueInfo(GUID);
581  if (!VI)
582  continue;
583  for (auto &S : VI.getSummaryList())
584  S->setLive(true);
585  }
586 
587  // Add values flagged in the index as live roots to the worklist.
588  for (const auto &Entry : Index)
589  for (auto &S : Entry.second.SummaryList)
590  if (S->isLive()) {
591  DEBUG(dbgs() << "Live root: " << Entry.first << "\n");
592  Worklist.push_back(ValueInfo(/*IsAnalysis=*/false, &Entry));
593  ++LiveSymbols;
594  break;
595  }
596 
597  // Make value live and add it to the worklist if it was not live before.
598  auto visit = [&](ValueInfo VI) {
599  // FIXME: If we knew which edges were created for indirect call profiles,
600  // we could skip them here. Any that are live should be reached via
601  // other edges, e.g. reference edges. Otherwise, using a profile collected
602  // on a slightly different binary might provoke preserving, importing
603  // and ultimately promoting calls to functions not linked into this
604  // binary, which increases the binary size unnecessarily. Note that
605  // if this code changes, the importer needs to change so that edges
606  // to functions marked dead are skipped.
608  if (!VI)
609  return;
610  for (auto &S : VI.getSummaryList())
611  if (S->isLive())
612  return;
613 
614  // We do not keep live symbols that are known to be non-prevailing.
615  if (isPrevailing(VI.getGUID()) == PrevailingType::No)
616  return;
617 
618  for (auto &S : VI.getSummaryList())
619  S->setLive(true);
620  ++LiveSymbols;
621  Worklist.push_back(VI);
622  };
623 
624  while (!Worklist.empty()) {
625  auto VI = Worklist.pop_back_val();
626  for (auto &Summary : VI.getSummaryList()) {
627  GlobalValueSummary *Base = Summary->getBaseObject();
628  // Set base value live in case it is an alias.
629  Base->setLive(true);
630  for (auto Ref : Base->refs())
631  visit(Ref);
632  if (auto *FS = dyn_cast<FunctionSummary>(Base))
633  for (auto Call : FS->calls())
634  visit(Call.first);
635  }
636  }
637  Index.setWithGlobalValueDeadStripping();
638 
639  unsigned DeadSymbols = Index.size() - LiveSymbols;
640  DEBUG(dbgs() << LiveSymbols << " symbols Live, and " << DeadSymbols
641  << " symbols Dead \n");
642  NumDeadSymbols += DeadSymbols;
643  NumLiveSymbols += LiveSymbols;
644 }
645 
646 /// Compute the set of summaries needed for a ThinLTO backend compilation of
647 /// \p ModulePath.
649  StringRef ModulePath,
650  const StringMap<GVSummaryMapTy> &ModuleToDefinedGVSummaries,
651  const FunctionImporter::ImportMapTy &ImportList,
652  std::map<std::string, GVSummaryMapTy> &ModuleToSummariesForIndex) {
653  // Include all summaries from the importing module.
654  ModuleToSummariesForIndex[ModulePath] =
655  ModuleToDefinedGVSummaries.lookup(ModulePath);
656  // Include summaries for imports.
657  for (auto &ILI : ImportList) {
658  auto &SummariesForIndex = ModuleToSummariesForIndex[ILI.first()];
659  const auto &DefinedGVSummaries =
660  ModuleToDefinedGVSummaries.lookup(ILI.first());
661  for (auto &GI : ILI.second) {
662  const auto &DS = DefinedGVSummaries.find(GI.first);
663  assert(DS != DefinedGVSummaries.end() &&
664  "Expected a defined summary for imported global value");
665  SummariesForIndex[GI.first] = DS->second;
666  }
667  }
668 }
669 
670 /// Emit the files \p ModulePath will import from into \p OutputFilename.
671 std::error_code
673  const FunctionImporter::ImportMapTy &ModuleImports) {
674  std::error_code EC;
675  raw_fd_ostream ImportsOS(OutputFilename, EC, sys::fs::OpenFlags::F_None);
676  if (EC)
677  return EC;
678  for (auto &ILI : ModuleImports)
679  ImportsOS << ILI.first() << "\n";
680  return std::error_code();
681 }
682 
684  DEBUG(dbgs() << "Converting to a declaration: `" << GV.getName() << "\n");
685  if (Function *F = dyn_cast<Function>(&GV)) {
686  F->deleteBody();
687  F->clearMetadata();
688  F->setComdat(nullptr);
689  } else if (GlobalVariable *V = dyn_cast<GlobalVariable>(&GV)) {
690  V->setInitializer(nullptr);
691  V->setLinkage(GlobalValue::ExternalLinkage);
692  V->clearMetadata();
693  V->setComdat(nullptr);
694  } else {
695  GlobalValue *NewGV;
696  if (GV.getValueType()->isFunctionTy())
697  NewGV =
698  Function::Create(cast<FunctionType>(GV.getValueType()),
700  else
701  NewGV =
702  new GlobalVariable(*GV.getParent(), GV.getValueType(),
703  /*isConstant*/ false, GlobalValue::ExternalLinkage,
704  /*init*/ nullptr, "",
705  /*insertbefore*/ nullptr, GV.getThreadLocalMode(),
706  GV.getType()->getAddressSpace());
707  NewGV->takeName(&GV);
708  GV.replaceAllUsesWith(NewGV);
709  return false;
710  }
711  return true;
712 }
713 
714 /// Fixup WeakForLinker linkages in \p TheModule based on summary analysis.
716  Module &TheModule, const GVSummaryMapTy &DefinedGlobals) {
717  auto updateLinkage = [&](GlobalValue &GV) {
718  // See if the global summary analysis computed a new resolved linkage.
719  const auto &GS = DefinedGlobals.find(GV.getGUID());
720  if (GS == DefinedGlobals.end())
721  return;
722  auto NewLinkage = GS->second->linkage();
723  if (NewLinkage == GV.getLinkage())
724  return;
725 
726  // Switch the linkage to weakany if asked for, e.g. we do this for
727  // linker redefined symbols (via --wrap or --defsym).
728  // We record that the visibility should be changed here in `addThinLTO`
729  // as we need access to the resolution vectors for each input file in
730  // order to find which symbols have been redefined.
731  // We may consider reorganizing this code and moving the linkage recording
732  // somewhere else, e.g. in thinLTOResolveWeakForLinkerInIndex.
733  if (NewLinkage == GlobalValue::WeakAnyLinkage) {
734  GV.setLinkage(NewLinkage);
735  return;
736  }
737 
738  if (!GlobalValue::isWeakForLinker(GV.getLinkage()))
739  return;
740  // Check for a non-prevailing def that has interposable linkage
741  // (e.g. non-odr weak or linkonce). In that case we can't simply
742  // convert to available_externally, since it would lose the
743  // interposable property and possibly get inlined. Simply drop
744  // the definition in that case.
746  GlobalValue::isInterposableLinkage(GV.getLinkage())) {
747  if (!convertToDeclaration(GV))
748  // FIXME: Change this to collect replaced GVs and later erase
749  // them from the parent module once thinLTOResolveWeakForLinkerGUID is
750  // changed to enable this for aliases.
751  llvm_unreachable("Expected GV to be converted");
752  } else {
753  // If the original symbols has global unnamed addr and linkonce_odr linkage,
754  // it should be an auto hide symbol. Add hidden visibility to the symbol to
755  // preserve the property.
756  if (GV.hasLinkOnceODRLinkage() && GV.hasGlobalUnnamedAddr() &&
757  NewLinkage == GlobalValue::WeakODRLinkage)
758  GV.setVisibility(GlobalValue::HiddenVisibility);
759 
760  DEBUG(dbgs() << "ODR fixing up linkage for `" << GV.getName() << "` from "
761  << GV.getLinkage() << " to " << NewLinkage << "\n");
762  GV.setLinkage(NewLinkage);
763  }
764  // Remove declarations from comdats, including available_externally
765  // as this is a declaration for the linker, and will be dropped eventually.
766  // It is illegal for comdats to contain declarations.
767  auto *GO = dyn_cast_or_null<GlobalObject>(&GV);
768  if (GO && GO->isDeclarationForLinker() && GO->hasComdat())
769  GO->setComdat(nullptr);
770  };
771 
772  // Process functions and global now
773  for (auto &GV : TheModule)
774  updateLinkage(GV);
775  for (auto &GV : TheModule.globals())
776  updateLinkage(GV);
777  for (auto &GV : TheModule.aliases())
778  updateLinkage(GV);
779 }
780 
781 /// Run internalization on \p TheModule based on symmary analysis.
783  const GVSummaryMapTy &DefinedGlobals) {
784  // Declare a callback for the internalize pass that will ask for every
785  // candidate GlobalValue if it can be internalized or not.
786  auto MustPreserveGV = [&](const GlobalValue &GV) -> bool {
787  // Lookup the linkage recorded in the summaries during global analysis.
788  auto GS = DefinedGlobals.find(GV.getGUID());
789  if (GS == DefinedGlobals.end()) {
790  // Must have been promoted (possibly conservatively). Find original
791  // name so that we can access the correct summary and see if it can
792  // be internalized again.
793  // FIXME: Eventually we should control promotion instead of promoting
794  // and internalizing again.
795  StringRef OrigName =
797  std::string OrigId = GlobalValue::getGlobalIdentifier(
799  TheModule.getSourceFileName());
800  GS = DefinedGlobals.find(GlobalValue::getGUID(OrigId));
801  if (GS == DefinedGlobals.end()) {
802  // Also check the original non-promoted non-globalized name. In some
803  // cases a preempted weak value is linked in as a local copy because
804  // it is referenced by an alias (IRLinker::linkGlobalValueProto).
805  // In that case, since it was originally not a local value, it was
806  // recorded in the index using the original name.
807  // FIXME: This may not be needed once PR27866 is fixed.
808  GS = DefinedGlobals.find(GlobalValue::getGUID(OrigName));
809  assert(GS != DefinedGlobals.end());
810  }
811  }
812  return !GlobalValue::isLocalLinkage(GS->second->linkage());
813  };
814 
815  // FIXME: See if we can just internalize directly here via linkage changes
816  // based on the index, rather than invoking internalizeModule.
817  internalizeModule(TheModule, MustPreserveGV);
818 }
819 
820 /// Make alias a clone of its aliasee.
822  Function *Fn = cast<Function>(GA->getBaseObject());
823 
824  ValueToValueMapTy VMap;
825  Function *NewFn = CloneFunction(Fn, VMap);
826  // Clone should use the original alias's linkage and name, and we ensure
827  // all uses of alias instead use the new clone (casted if necessary).
828  NewFn->setLinkage(GA->getLinkage());
830  NewFn->takeName(GA);
831  return NewFn;
832 }
833 
834 // Automatically import functions in Module \p DestModule based on the summaries
835 // index.
837  Module &DestModule, const FunctionImporter::ImportMapTy &ImportList) {
838  DEBUG(dbgs() << "Starting import for Module "
839  << DestModule.getModuleIdentifier() << "\n");
840  unsigned ImportedCount = 0, ImportedGVCount = 0;
841 
842  IRMover Mover(DestModule);
843  // Do the actual import of functions now, one Module at a time
844  std::set<StringRef> ModuleNameOrderedList;
845  for (auto &FunctionsToImportPerModule : ImportList) {
846  ModuleNameOrderedList.insert(FunctionsToImportPerModule.first());
847  }
848  for (auto &Name : ModuleNameOrderedList) {
849  // Get the module for the import
850  const auto &FunctionsToImportPerModule = ImportList.find(Name);
851  assert(FunctionsToImportPerModule != ImportList.end());
852  Expected<std::unique_ptr<Module>> SrcModuleOrErr = ModuleLoader(Name);
853  if (!SrcModuleOrErr)
854  return SrcModuleOrErr.takeError();
855  std::unique_ptr<Module> SrcModule = std::move(*SrcModuleOrErr);
856  assert(&DestModule.getContext() == &SrcModule->getContext() &&
857  "Context mismatch");
858 
859  // If modules were created with lazy metadata loading, materialize it
860  // now, before linking it (otherwise this will be a noop).
861  if (Error Err = SrcModule->materializeMetadata())
862  return std::move(Err);
863 
864  auto &ImportGUIDs = FunctionsToImportPerModule->second;
865  // Find the globals to import
866  SetVector<GlobalValue *> GlobalsToImport;
867  for (Function &F : *SrcModule) {
868  if (!F.hasName())
869  continue;
870  auto GUID = F.getGUID();
871  auto Import = ImportGUIDs.count(GUID);
872  DEBUG(dbgs() << (Import ? "Is" : "Not") << " importing function " << GUID
873  << " " << F.getName() << " from "
874  << SrcModule->getSourceFileName() << "\n");
875  if (Import) {
876  if (Error Err = F.materialize())
877  return std::move(Err);
878  if (EnableImportMetadata) {
879  // Add 'thinlto_src_module' metadata for statistics and debugging.
880  F.setMetadata(
881  "thinlto_src_module",
882  MDNode::get(DestModule.getContext(),
883  {MDString::get(DestModule.getContext(),
884  SrcModule->getSourceFileName())}));
885  }
886  GlobalsToImport.insert(&F);
887  }
888  }
889  for (GlobalVariable &GV : SrcModule->globals()) {
890  if (!GV.hasName())
891  continue;
892  auto GUID = GV.getGUID();
893  auto Import = ImportGUIDs.count(GUID);
894  DEBUG(dbgs() << (Import ? "Is" : "Not") << " importing global " << GUID
895  << " " << GV.getName() << " from "
896  << SrcModule->getSourceFileName() << "\n");
897  if (Import) {
898  if (Error Err = GV.materialize())
899  return std::move(Err);
900  ImportedGVCount += GlobalsToImport.insert(&GV);
901  }
902  }
903  for (GlobalAlias &GA : SrcModule->aliases()) {
904  if (!GA.hasName())
905  continue;
906  auto GUID = GA.getGUID();
907  auto Import = ImportGUIDs.count(GUID);
908  DEBUG(dbgs() << (Import ? "Is" : "Not") << " importing alias " << GUID
909  << " " << GA.getName() << " from "
910  << SrcModule->getSourceFileName() << "\n");
911  if (Import) {
912  if (Error Err = GA.materialize())
913  return std::move(Err);
914  // Import alias as a copy of its aliasee.
915  GlobalObject *Base = GA.getBaseObject();
916  if (Error Err = Base->materialize())
917  return std::move(Err);
918  auto *Fn = replaceAliasWithAliasee(SrcModule.get(), &GA);
919  DEBUG(dbgs() << "Is importing aliasee fn " << Base->getGUID()
920  << " " << Base->getName() << " from "
921  << SrcModule->getSourceFileName() << "\n");
922  if (EnableImportMetadata) {
923  // Add 'thinlto_src_module' metadata for statistics and debugging.
924  Fn->setMetadata(
925  "thinlto_src_module",
926  MDNode::get(DestModule.getContext(),
927  {MDString::get(DestModule.getContext(),
928  SrcModule->getSourceFileName())}));
929  }
930  GlobalsToImport.insert(Fn);
931  }
932  }
933 
934  // Upgrade debug info after we're done materializing all the globals and we
935  // have loaded all the required metadata!
936  UpgradeDebugInfo(*SrcModule);
937 
938  // Link in the specified functions.
939  if (renameModuleForThinLTO(*SrcModule, Index, &GlobalsToImport))
940  return true;
941 
942  if (PrintImports) {
943  for (const auto *GV : GlobalsToImport)
944  dbgs() << DestModule.getSourceFileName() << ": Import " << GV->getName()
945  << " from " << SrcModule->getSourceFileName() << "\n";
946  }
947 
948  if (Mover.move(std::move(SrcModule), GlobalsToImport.getArrayRef(),
950  /*IsPerformingImport=*/true))
951  report_fatal_error("Function Import: link error");
952 
953  ImportedCount += GlobalsToImport.size();
954  NumImportedModules++;
955  }
956 
957  NumImportedFunctions += (ImportedCount - ImportedGVCount);
958  NumImportedGlobalVars += ImportedGVCount;
959 
960  DEBUG(dbgs() << "Imported " << ImportedCount - ImportedGVCount
961  << " functions for Module " << DestModule.getModuleIdentifier()
962  << "\n");
963  DEBUG(dbgs() << "Imported " << ImportedGVCount
964  << " global variables for Module "
965  << DestModule.getModuleIdentifier() << "\n");
966  return ImportedCount;
967 }
968 
969 static bool doImportingForModule(Module &M) {
970  if (SummaryFile.empty())
971  report_fatal_error("error: -function-import requires -summary-file\n");
974  if (!IndexPtrOrErr) {
975  logAllUnhandledErrors(IndexPtrOrErr.takeError(), errs(),
976  "Error loading file '" + SummaryFile + "': ");
977  return false;
978  }
979  std::unique_ptr<ModuleSummaryIndex> Index = std::move(*IndexPtrOrErr);
980 
981  // First step is collecting the import list.
983  // If requested, simply import all functions in the index. This is used
984  // when testing distributed backend handling via the opt tool, when
985  // we have distributed indexes containing exactly the summaries to import.
986  if (ImportAllIndex)
988  ImportList);
989  else
991  ImportList);
992 
993  // Conservatively mark all internal values as promoted. This interface is
994  // only used when doing importing via the function importing pass. The pass
995  // is only enabled when testing importing via the 'opt' tool, which does
996  // not do the ThinLink that would normally determine what values to promote.
997  for (auto &I : *Index) {
998  for (auto &S : I.second.SummaryList) {
999  if (GlobalValue::isLocalLinkage(S->linkage()))
1000  S->setLinkage(GlobalValue::ExternalLinkage);
1001  }
1002  }
1003 
1004  // Next we need to promote to global scope and rename any local values that
1005  // are potentially exported to other modules.
1006  if (renameModuleForThinLTO(M, *Index, nullptr)) {
1007  errs() << "Error renaming module\n";
1008  return false;
1009  }
1010 
1011  // Perform the import now.
1012  auto ModuleLoader = [&M](StringRef Identifier) {
1013  return loadFile(Identifier, M.getContext());
1014  };
1015  FunctionImporter Importer(*Index, ModuleLoader);
1016  Expected<bool> Result = Importer.importFunctions(M, ImportList);
1017 
1018  // FIXME: Probably need to propagate Errors through the pass manager.
1019  if (!Result) {
1020  logAllUnhandledErrors(Result.takeError(), errs(),
1021  "Error importing module: ");
1022  return false;
1023  }
1024 
1025  return *Result;
1026 }
1027 
1028 namespace {
1029 
1030 /// Pass that performs cross-module function import provided a summary file.
1031 class FunctionImportLegacyPass : public ModulePass {
1032 public:
1033  /// Pass identification, replacement for typeid
1034  static char ID;
1035 
1036  explicit FunctionImportLegacyPass() : ModulePass(ID) {}
1037 
1038  /// Specify pass name for debug output
1039  StringRef getPassName() const override { return "Function Importing"; }
1040 
1041  bool runOnModule(Module &M) override {
1042  if (skipModule(M))
1043  return false;
1044 
1045  return doImportingForModule(M);
1046  }
1047 };
1048 
1049 } // end anonymous namespace
1050 
1052  ModuleAnalysisManager &AM) {
1053  if (!doImportingForModule(M))
1054  return PreservedAnalyses::all();
1055 
1056  return PreservedAnalyses::none();
1057 }
1058 
1060 INITIALIZE_PASS(FunctionImportLegacyPass, "function-import",
1061  "Summary Based Function Import", false, false)
1062 
1063 namespace llvm {
1064 
1066  return new FunctionImportLegacyPass();
1067 }
1068 
1069 } // end namespace llvm
static bool isInterposableLinkage(LinkageTypes Linkage)
Whether the definition of this global may be replaced by something non-equivalent at link time...
Definition: GlobalValue.h:336
Pass interface - Implemented by all &#39;passes&#39;.
Definition: Pass.h:81
PreservedAnalyses run(Module &M, ModuleAnalysisManager &AM)
ThreadLocalMode getThreadLocalMode() const
Definition: GlobalValue.h:254
const GlobalObject * getBaseObject() const
raw_ostream & errs()
This returns a reference to a raw_ostream for standard error.
LLVMContext & Context
uint64_t GUID
Declare a type to represent a global unique identifier for a global value.
Definition: GlobalValue.h:492
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:115
Compute iterated dominance frontiers using a linear time algorithm.
Definition: AllocatorList.h:24
void print(const char *ProgName, raw_ostream &S, bool ShowColors=true, bool ShowKindLabel=true) const
Definition: SourceMgr.cpp:345
ArrayRef< T > getArrayRef() const
Definition: SetVector.h:64
size_type size() const
Determine the number of elements in the SetVector.
Definition: SetVector.h:78
ValueInfo getValueInfo(GlobalValue::GUID GUID) const
Return a ValueInfo for GUID if it exists, otherwise return ValueInfo().
A Module instance is used to store all the information related to an LLVM module. ...
Definition: Module.h:63
void logAllUnhandledErrors(Error E, raw_ostream &OS, Twine ErrorBanner)
Log all errors (if any) in E to OS.
Definition: Error.cpp:57
Same, but only replaced by something equivalent.
Definition: GlobalValue.h:54
static MDString * get(LLVMContext &Context, StringRef Str)
Definition: Metadata.cpp:454
Implements a dense probed hash-table based set.
Definition: DenseSet.h:221
ArrayRef< ValueInfo > refs() const
Return the list of values referenced by this global value definition.
This file contains the declarations for metadata subclasses.
Function * CloneFunction(Function *F, ValueToValueMapTy &VMap, ClonedCodeInfo *CodeInfo=nullptr)
CloneFunction - Return a copy of the specified function and add it to that function&#39;s module...
bool convertToDeclaration(GlobalValue &GV)
Converts value GV to declaration, or replaces with a declaration if it is an alias.
An efficient, type-erasing, non-owning reference to a callable.
Definition: STLExtras.h:89
Externally visible function.
Definition: GlobalValue.h:49
static StringRef getOriginalNameBeforePromote(StringRef Name)
Helper to obtain the unpromoted name for a global value (or the original name if not promoted)...
STATISTIC(NumFunctions, "Total number of functions")
bool isGlobalValueLive(const GlobalValueSummary *GVS) const
F(f)
Error takeError()
Take ownership of the stored error.
Definition: Error.h:537
std::string getGlobalIdentifier() const
Return the modified name for this global value suitable to be used as the key for a global lookup (e...
Definition: Globals.cpp:151
void reserve(size_type N)
Definition: SmallVector.h:378
static std::unique_ptr< Module > loadFile(const std::string &FileName, LLVMContext &Context)
static bool isLocalLinkage(LinkageTypes Linkage)
Definition: GlobalValue.h:320
GlobalValue::GUID getGUID() const
unsigned size() const
Definition: StringMap.h:112
static cl::opt< unsigned > ImportInstrLimit("import-instr-limit", cl::init(100), cl::Hidden, cl::value_desc("N"), cl::desc("Only import functions with less than N instructions"))
Limit on instruction count of imported functions.
static cl::opt< float > ImportHotMultiplier("import-hot-multiplier", cl::init(10.0), cl::Hidden, cl::value_desc("x"), cl::desc("Multiply the `import-instr-limit` threshold for hot callsites"))
LLVMContext & getContext() const
Get the global data context.
Definition: Module.h:237
This class consists of common code factored out of the SmallVector class to reduce code duplication b...
Definition: APFloat.h:42
static bool isGlobalVarSummary(const ModuleSummaryIndex &Index, GlobalValue::GUID G)
Tagged union holding either a T or a Error.
Definition: CachePruning.h:23
static cl::opt< std::string > OutputFilename("o", cl::desc("Output filename"), cl::value_desc("filename"), cl::init("-"))
static void computeImportForFunction(const FunctionSummary &Summary, const ModuleSummaryIndex &Index, const unsigned Threshold, const GVSummaryMapTy &DefinedGVSummaries, SmallVectorImpl< EdgeInfo > &Worklist, FunctionImporter::ImportMapTy &ImportList, StringMap< FunctionImporter::ExportSetTy > *ExportLists=nullptr)
Compute the list of functions to import for a given caller.
bool insert(const value_type &X)
Insert a new element into the SetVector.
Definition: SetVector.h:142
Error move(std::unique_ptr< Module > Src, ArrayRef< GlobalValue *> ValuesToLink, std::function< void(GlobalValue &GV, ValueAdder Add)> AddLazyFor, bool IsPerformingImport)
Move in the provide values in ValuesToLink from Src.
Definition: IRMover.cpp:1461
ArrayRef - Represent a constant reference to an array (0 or more elements consecutively in memory)...
Definition: APInt.h:33
static cl::opt< float > ImportCriticalMultiplier("import-critical-multiplier", cl::init(100.0), cl::Hidden, cl::value_desc("x"), cl::desc("Multiply the `import-instr-limit` threshold for critical callsites"))
LinkageTypes getLinkage() const
Definition: GlobalValue.h:450
void replaceAllUsesWith(Value *V)
Change all uses of this to point to a new Value.
Definition: Value.cpp:439
void takeName(Value *V)
Transfer the name from V to this value.
Definition: Value.cpp:301
Class to hold module path string table and global value map, and encapsulate methods for operating on...
ArrayRef< std::unique_ptr< GlobalValueSummary > > getSummaryList() const
const std::string & getSourceFileName() const
Get the module&#39;s original source file name.
Definition: Module.h:214
static PreservedAnalyses none()
Convenience factory function for the empty preserved set.
Definition: PassManager.h:156
iterator find(const_arg_type_t< KeyT > Val)
Definition: DenseMap.h:146
static Constant * getBitCast(Constant *C, Type *Ty, bool OnlyIfReduced=false)
Definition: Constants.cpp:1727
static MDTuple * get(LLVMContext &Context, ArrayRef< Metadata *> MDs)
Definition: Metadata.h:1164
#define P(N)
initializer< Ty > init(const Ty &Val)
Definition: CommandLine.h:406
GlobalValue::GUID getGUIDFromOriginalID(GlobalValue::GUID OriginalID) const
Return the GUID for OriginalId in the OidGuidMap.
A set of analyses that are preserved following a run of a transformation pass.
Definition: PassManager.h:153
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.
Import information from summary.
This is an important class for using LLVM in a threaded context.
Definition: LLVMContext.h:69
Pass * createFunctionImportPass()
This pass performs iterative function importing from other modules.
This file contains the declarations for the subclasses of Constant, which represent the different fla...
PrevailingType
PrevailingType enum used as a return type of callback passed to computeDeadSymbols.
bool UpgradeDebugInfo(Module &M)
Check the debug info version number, if it is out-dated, drop the debug info.
void ComputeCrossModuleImportForModuleFromIndex(StringRef ModulePath, const ModuleSummaryIndex &Index, FunctionImporter::ImportMapTy &ImportList)
Mark all external summaries in Index for import into the given module.
bool isWeakForLinker() const
Definition: GlobalValue.h:456
unsigned getAddressSpace() const
Return the address space of the Pointer type.
Definition: DerivedTypes.h:495
ValueTy lookup(StringRef Key) const
lookup - Return the entry for the specified key, or a default constructed value if no such entry exis...
Definition: StringMap.h:349
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:842
INITIALIZE_PASS(FunctionImportLegacyPass, "function-import", "Summary Based Function Import", false, false) namespace llvm
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:496
Error materialize()
Make sure this GlobalValue is fully read.
Definition: Globals.cpp:50
static cl::opt< bool > ImportAllIndex("import-all-index", cl::desc("Import all external functions in index."))
Used when testing importing from distributed indexes via opt.
static PreservedAnalyses all()
Construct a special preserved set that preserves all passes.
Definition: PassManager.h:159
const std::string & getModuleIdentifier() const
Get the module identifier which is, essentially, the name of the module.
Definition: Module.h:208
#define llvm_unreachable(msg)
Marks that the current location is not supposed to be reachable.
static Function * replaceAliasWithAliasee(Module *SrcModule, GlobalAlias *GA)
Make alias a clone of its aliasee.
Struct that holds a reference to a particular GUID in a global value summary.
std::unique_ptr< Module > getLazyIRFileModule(StringRef Filename, SMDiagnostic &Err, LLVMContext &Context, bool ShouldLazyLoadMetadata=false)
If the given file holds a bitcode image, return a Module for it which does lazy deserialization of fu...
Definition: IRReader.cpp:53
static cl::opt< float > ImportInstrFactor("import-instr-evolution-factor", cl::init(0.7), cl::Hidden, cl::value_desc("x"), cl::desc("As we import functions, multiply the " "`import-instr-limit` threshold by this factor " "before processing newly imported functions"))
This is a &#39;vector&#39; (really, a variable-sized array), optimized for the case when the array is small...
Definition: SmallVector.h:862
Module.h This file contains the declarations for the Module class.
static void ComputeImportForModule(const GVSummaryMapTy &DefinedGVSummaries, const ModuleSummaryIndex &Index, FunctionImporter::ImportMapTy &ImportList, StringMap< FunctionImporter::ExportSetTy > *ExportLists=nullptr)
Given the list of globals defined in a module, compute the list of imports as well as the list of "ex...
static void computeImportForReferencedGlobals(const FunctionSummary &Summary, const GVSummaryMapTy &DefinedGVSummaries, FunctionImporter::ImportMapTy &ImportList, StringMap< FunctionImporter::ExportSetTy > *ExportLists)
const DataFlowGraph & G
Definition: RDFGraph.cpp:211
static bool isAvailableExternallyLinkage(LinkageTypes Linkage)
Definition: GlobalValue.h:293
LLVM_NODISCARD T pop_back_val()
Definition: SmallVector.h:383
static bool doImportingForModule(Module &M)
std::error_code EmitImportsFiles(StringRef ModulePath, StringRef OutputFilename, const FunctionImporter::ImportMapTy &ModuleImports)
Emit into OutputFilename the files module ModulePath will import from.
unsigned numGlobalVarSummaries(const ModuleSummaryIndex &Index, T &Cont)
static ValueInfo updateValueInfoForIndirectCalls(const ModuleSummaryIndex &Index, ValueInfo VI)
void setLinkage(LinkageTypes LT)
Definition: GlobalValue.h:444
bool insert(MapEntryTy *KeyValue)
insert - Insert the specified key/value pair into the map.
Definition: StringMap.h:368
raw_ostream & dbgs()
dbgs() - This returns a reference to a raw_ostream for debugging messages.
Definition: Debug.cpp:132
StringMap - This is an unconventional map that is specialized for handling keys that are "strings"...
Definition: StringMap.h:222
Expected< std::unique_ptr< ModuleSummaryIndex > > getModuleSummaryIndexForFile(StringRef Path, bool IgnoreEmptyThinLTOIndexFile=false)
Parse the module summary index out of an IR file and return the module summary index object if found...
GUID getGUID() const
Return a 64-bit global unique ID constructed from global value name (i.e.
Definition: GlobalValue.h:500
static cl::opt< float > ImportColdMultiplier("import-cold-multiplier", cl::init(0), cl::Hidden, cl::value_desc("N"), cl::desc("Multiply the `import-instr-limit` threshold for cold callsites"))
#define NDEBUG
Definition: regutils.h:48
static cl::opt< bool > ComputeDead("compute-dead", cl::init(true), cl::Hidden, cl::desc("Compute dead symbols"))
bool withGlobalValueDeadStripping() const
Expected< bool > importFunctions(Module &M, const ImportMapTy &ImportList)
Import functions in Module M based on the supplied import list.
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.
bool isFunctionTy() const
True if this is an instance of FunctionType.
Definition: Type.h:212
A raw_ostream that writes to a file descriptor.
Definition: raw_ostream.h:361
void emplace_back(ArgTypes &&... Args)
Definition: SmallVector.h:654
static cl::opt< bool > PrintImports("print-imports", cl::init(false), cl::Hidden, cl::desc("Print imported functions"))
StringRef modulePath() const
Get the path to the module containing this function.
static cl::opt< float > ImportHotInstrFactor("import-hot-evolution-factor", cl::init(1.0), cl::Hidden, cl::value_desc("x"), cl::desc("As we import functions called from hot callsite, multiply the " "`import-instr-limit` threshold by this factor " "before processing newly imported functions"))
LLVM_NODISCARD bool empty() const
Definition: SmallVector.h:61
StringRef getName() const
Return a constant reference to the value&#39;s name.
Definition: Value.cpp:224
#define I(x, y, z)
Definition: MD5.cpp:58
ModulePass class - This class is used to implement unstructured interprocedural optimizations and ana...
Definition: Pass.h:225
iterator end()
Definition: DenseMap.h:79
void thinLTOInternalizeModule(Module &TheModule, const GVSummaryMapTy &DefinedGlobals)
Internalize TheModule based on the information recorded in the summaries during global summary-based ...
LLVM_NODISCARD std::enable_if<!is_simple_type< Y >::value, typename cast_retty< X, const Y >::ret_type >::type dyn_cast(const Y &Val)
Definition: Casting.h:323
ArrayRef< EdgeTy > calls() const
Return the list of <CalleeValueInfo, CalleeInfo> pairs.
Type * getValueType() const
Definition: GlobalValue.h:275
Keep one copy of named function when linking (weak)
Definition: GlobalValue.h:53
Rename collisions when linking (static functions).
Definition: GlobalValue.h:56
void thinLTOResolveWeakForLinkerModule(Module &TheModule, const GVSummaryMapTy &DefinedGlobals)
Resolve WeakForLinker values in TheModule based on the information recorded in the summaries during g...
Function summary information to aid decisions and implementation of importing.
static void dumpImportListForModule(const ModuleSummaryIndex &Index, StringRef ModulePath, FunctionImporter::ImportMapTy &ImportList)
size_type count(const_arg_type_t< KeyT > Val) const
Return 1 if the specified key is in the map, 0 otherwise.
Definition: DenseMap.h:141
static GlobalValue::GUID getGUID(GlobalValue::GUID G)
void ComputeCrossModuleImportForModule(StringRef ModulePath, const ModuleSummaryIndex &Index, FunctionImporter::ImportMapTy &ImportList)
Compute all the imports for the given module using the Index.
static int const Threshold
TODO: Write a new FunctionPass AliasAnalysis so that it can keep a cache.
static cl::opt< std::string > SummaryFile("summary-file", cl::desc("The summary file to use for function importing."))
Summary file to use for function importing when using -function-import from the command line...
ModuleSummaryIndex.h This file contains the declarations the classes that hold the module index and s...
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
std::function< void(GlobalValue &)> ValueAdder
Definition: IRMover.h:65
Module * getParent()
Get the module that this global value is contained inside of...
Definition: GlobalValue.h:565
The function importer is automatically importing function from other modules based on the provided su...
A vector that has set insertion semantics.
Definition: SetVector.h:41
Lightweight error class with error context and mandatory checking.
Definition: Error.h:156
The access may reference the value stored in memory.
void computeDeadSymbols(ModuleSummaryIndex &Index, const DenseSet< GlobalValue::GUID > &GUIDPreservedSymbols, function_ref< PrevailingType(GlobalValue::GUID)> isPrevailing)
Compute all the symbols that are "dead": i.e these that can&#39;t be reached in the graph from any of the...
#define DEBUG(X)
Definition: Debug.h:118
static cl::opt< bool > EnableImportMetadata("enable-import-metadata", cl::init(true), cl::Hidden, cl::desc("Enable import metadata like 'thinlto_src_module'"))
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:49
A container for analyses that lazily runs them and caches their results.
static Function * Create(FunctionType *Ty, LinkageTypes Linkage, const Twine &N="", Module *M=nullptr)
Definition: Function.h:136
static const GlobalValueSummary * selectCallee(const ModuleSummaryIndex &Index, ArrayRef< std::unique_ptr< GlobalValueSummary >> CalleeSummaryList, unsigned Threshold, StringRef CallerModulePath)
Given a list of possible callee implementation for a call site, select one that fits the Threshold...
bool internalizeModule(Module &TheModule, std::function< bool(const GlobalValue &)> MustPreserveGV, CallGraph *CG=nullptr)
Helper function to internalize functions and variables in a Module.
Definition: Internalize.h:71
void collectDefinedFunctionsForModule(StringRef ModulePath, GVSummaryMapTy &GVSummaryMap) const
Collect for the given module the list of function it defines (GUID -> Summary).
PointerType * getType() const
Global values are always pointers.
Definition: GlobalValue.h:273
Instances of this class encapsulate one diagnostic report, allowing printing to a raw_ostream as a ca...
Definition: SourceMgr.h:233