LLVM  15.0.0git
FunctionImport.cpp
Go to the documentation of this file.
1 //===- FunctionImport.cpp - ThinLTO Summary-based Function Import ---------===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 //
9 // This file implements Function import based on summaries.
10 //
11 //===----------------------------------------------------------------------===//
12 
14 #include "llvm/ADT/ArrayRef.h"
15 #include "llvm/ADT/STLExtras.h"
16 #include "llvm/ADT/SetVector.h"
17 #include "llvm/ADT/SmallVector.h"
18 #include "llvm/ADT/Statistic.h"
19 #include "llvm/ADT/StringMap.h"
20 #include "llvm/ADT/StringRef.h"
22 #include "llvm/IR/AutoUpgrade.h"
23 #include "llvm/IR/Constants.h"
24 #include "llvm/IR/Function.h"
25 #include "llvm/IR/GlobalAlias.h"
26 #include "llvm/IR/GlobalObject.h"
27 #include "llvm/IR/GlobalValue.h"
28 #include "llvm/IR/GlobalVariable.h"
29 #include "llvm/IR/Metadata.h"
30 #include "llvm/IR/Module.h"
32 #include "llvm/IRReader/IRReader.h"
33 #include "llvm/InitializePasses.h"
34 #include "llvm/Linker/IRMover.h"
35 #include "llvm/Pass.h"
36 #include "llvm/Support/Casting.h"
38 #include "llvm/Support/Debug.h"
39 #include "llvm/Support/Errc.h"
40 #include "llvm/Support/Error.h"
43 #include "llvm/Support/SourceMgr.h"
49 #include <cassert>
50 #include <memory>
51 #include <set>
52 #include <string>
53 #include <system_error>
54 #include <tuple>
55 #include <utility>
56 
57 using namespace llvm;
58 
59 #define DEBUG_TYPE "function-import"
60 
61 STATISTIC(NumImportedFunctionsThinLink,
62  "Number of functions thin link decided to import");
63 STATISTIC(NumImportedHotFunctionsThinLink,
64  "Number of hot functions thin link decided to import");
65 STATISTIC(NumImportedCriticalFunctionsThinLink,
66  "Number of critical functions thin link decided to import");
67 STATISTIC(NumImportedGlobalVarsThinLink,
68  "Number of global variables thin link decided to import");
69 STATISTIC(NumImportedFunctions, "Number of functions imported in backend");
70 STATISTIC(NumImportedGlobalVars,
71  "Number of global variables imported in backend");
72 STATISTIC(NumImportedModules, "Number of modules imported from");
73 STATISTIC(NumDeadSymbols, "Number of dead stripped symbols in index");
74 STATISTIC(NumLiveSymbols, "Number of live symbols in index");
75 
76 /// Limit on instruction count of imported functions.
78  "import-instr-limit", cl::init(100), cl::Hidden, cl::value_desc("N"),
79  cl::desc("Only import functions with less than N instructions"));
80 
82  "import-cutoff", cl::init(-1), cl::Hidden, cl::value_desc("N"),
83  cl::desc("Only import first N functions if N>=0 (default -1)"));
84 
85 static cl::opt<bool>
86  ForceImportAll("force-import-all", cl::init(false), cl::Hidden,
87  cl::desc("Import functions with noinline attribute"));
88 
89 static cl::opt<float>
90  ImportInstrFactor("import-instr-evolution-factor", cl::init(0.7),
92  cl::desc("As we import functions, multiply the "
93  "`import-instr-limit` threshold by this factor "
94  "before processing newly imported functions"));
95 
97  "import-hot-evolution-factor", cl::init(1.0), cl::Hidden,
98  cl::value_desc("x"),
99  cl::desc("As we import functions called from hot callsite, multiply the "
100  "`import-instr-limit` threshold by this factor "
101  "before processing newly imported functions"));
102 
104  "import-hot-multiplier", cl::init(10.0), cl::Hidden, cl::value_desc("x"),
105  cl::desc("Multiply the `import-instr-limit` threshold for hot callsites"));
106 
108  "import-critical-multiplier", cl::init(100.0), cl::Hidden,
109  cl::value_desc("x"),
110  cl::desc(
111  "Multiply the `import-instr-limit` threshold for critical callsites"));
112 
113 // FIXME: This multiplier was not really tuned up.
115  "import-cold-multiplier", cl::init(0), cl::Hidden, cl::value_desc("N"),
116  cl::desc("Multiply the `import-instr-limit` threshold for cold callsites"));
117 
118 static cl::opt<bool> PrintImports("print-imports", cl::init(false), cl::Hidden,
119  cl::desc("Print imported functions"));
120 
122  "print-import-failures", cl::init(false), cl::Hidden,
123  cl::desc("Print information for functions rejected for importing"));
124 
125 static cl::opt<bool> ComputeDead("compute-dead", cl::init(true), cl::Hidden,
126  cl::desc("Compute dead symbols"));
127 
129  "enable-import-metadata", cl::init(false), cl::Hidden,
130  cl::desc("Enable import metadata like 'thinlto_src_module'"));
131 
132 /// Summary file to use for function importing when using -function-import from
133 /// the command line.
135  SummaryFile("summary-file",
136  cl::desc("The summary file to use for function importing."));
137 
138 /// Used when testing importing from distributed indexes via opt
139 // -function-import.
140 static cl::opt<bool>
141  ImportAllIndex("import-all-index",
142  cl::desc("Import all external functions in index."));
143 
144 // Load lazily a module from \p FileName in \p Context.
145 static std::unique_ptr<Module> loadFile(const std::string &FileName,
146  LLVMContext &Context) {
147  SMDiagnostic Err;
148  LLVM_DEBUG(dbgs() << "Loading '" << FileName << "'\n");
149  // Metadata isn't loaded until functions are imported, to minimize
150  // the memory overhead.
151  std::unique_ptr<Module> Result =
152  getLazyIRFileModule(FileName, Err, Context,
153  /* ShouldLazyLoadMetadata = */ true);
154  if (!Result) {
155  Err.print("function-import", errs());
156  report_fatal_error("Abort");
157  }
158 
159  return Result;
160 }
161 
162 /// Given a list of possible callee implementation for a call site, select one
163 /// that fits the \p Threshold.
164 ///
165 /// FIXME: select "best" instead of first that fits. But what is "best"?
166 /// - The smallest: more likely to be inlined.
167 /// - The one with the least outgoing edges (already well optimized).
168 /// - One from a module already being imported from in order to reduce the
169 /// number of source modules parsed/linked.
170 /// - One that has PGO data attached.
171 /// - [insert you fancy metric here]
172 static const GlobalValueSummary *
174  ArrayRef<std::unique_ptr<GlobalValueSummary>> CalleeSummaryList,
175  unsigned Threshold, StringRef CallerModulePath,
177  GlobalValue::GUID GUID) {
179  auto It = llvm::find_if(
180  CalleeSummaryList,
181  [&](const std::unique_ptr<GlobalValueSummary> &SummaryPtr) {
182  auto *GVSummary = SummaryPtr.get();
183  if (!Index.isGlobalValueLive(GVSummary)) {
184  Reason = FunctionImporter::ImportFailureReason::NotLive;
185  return false;
186  }
187 
188  if (GlobalValue::isInterposableLinkage(GVSummary->linkage())) {
189  Reason = FunctionImporter::ImportFailureReason::InterposableLinkage;
190  // There is no point in importing these, we can't inline them
191  return false;
192  }
193 
194  auto *Summary = cast<FunctionSummary>(GVSummary->getBaseObject());
195 
196  // If this is a local function, make sure we import the copy
197  // in the caller's module. The only time a local function can
198  // share an entry in the index is if there is a local with the same name
199  // in another module that had the same source file name (in a different
200  // directory), where each was compiled in their own directory so there
201  // was not distinguishing path.
202  // However, do the import from another module if there is only one
203  // entry in the list - in that case this must be a reference due
204  // to indirect call profile data, since a function pointer can point to
205  // a local in another module.
206  if (GlobalValue::isLocalLinkage(Summary->linkage()) &&
207  CalleeSummaryList.size() > 1 &&
208  Summary->modulePath() != CallerModulePath) {
209  Reason =
210  FunctionImporter::ImportFailureReason::LocalLinkageNotInModule;
211  return false;
212  }
213 
214  if ((Summary->instCount() > Threshold) &&
215  !Summary->fflags().AlwaysInline && !ForceImportAll) {
216  Reason = FunctionImporter::ImportFailureReason::TooLarge;
217  return false;
218  }
219 
220  // Skip if it isn't legal to import (e.g. may reference unpromotable
221  // locals).
222  if (Summary->notEligibleToImport()) {
223  Reason = FunctionImporter::ImportFailureReason::NotEligible;
224  return false;
225  }
226 
227  // Don't bother importing if we can't inline it anyway.
228  if (Summary->fflags().NoInline && !ForceImportAll) {
229  Reason = FunctionImporter::ImportFailureReason::NoInline;
230  return false;
231  }
232 
233  return true;
234  });
235  if (It == CalleeSummaryList.end())
236  return nullptr;
237 
238  return cast<GlobalValueSummary>(It->get());
239 }
240 
241 namespace {
242 
243 using EdgeInfo =
244  std::tuple<const GlobalValueSummary *, unsigned /* Threshold */>;
245 
246 } // anonymous namespace
247 
248 static bool shouldImportGlobal(const ValueInfo &VI,
249  const GVSummaryMapTy &DefinedGVSummaries) {
250  const auto &GVS = DefinedGVSummaries.find(VI.getGUID());
251  if (GVS == DefinedGVSummaries.end())
252  return true;
253  // We should not skip import if the module contains a definition with
254  // interposable linkage type. This is required for correctness in
255  // the situation with two following conditions:
256  // * the def with interposable linkage is non-prevailing,
257  // * there is a prevailing def available for import and marked read-only.
258  // In this case, the non-prevailing def will be converted to a declaration,
259  // while the prevailing one becomes internal, thus no definitions will be
260  // available for linking. In order to prevent undefined symbol link error,
261  // the prevailing definition must be imported.
262  // FIXME: Consider adding a check that the suitable prevailing definition
263  // exists and marked read-only.
264  if (VI.getSummaryList().size() > 1 &&
265  GlobalValue::isInterposableLinkage(GVS->second->linkage()))
266  return true;
267 
268  return false;
269 }
270 
272  const GlobalValueSummary &Summary, const ModuleSummaryIndex &Index,
273  const GVSummaryMapTy &DefinedGVSummaries,
274  SmallVectorImpl<EdgeInfo> &Worklist,
275  FunctionImporter::ImportMapTy &ImportList,
277  for (auto &VI : Summary.refs()) {
278  if (!shouldImportGlobal(VI, DefinedGVSummaries)) {
279  LLVM_DEBUG(
280  dbgs() << "Ref ignored! Target already in destination module.\n");
281  continue;
282  }
283 
284  LLVM_DEBUG(dbgs() << " ref -> " << VI << "\n");
285 
286  // If this is a local variable, make sure we import the copy
287  // in the caller's module. The only time a local variable can
288  // share an entry in the index is if there is a local with the same name
289  // in another module that had the same source file name (in a different
290  // directory), where each was compiled in their own directory so there
291  // was not distinguishing path.
292  auto LocalNotInModule = [&](const GlobalValueSummary *RefSummary) -> bool {
293  return GlobalValue::isLocalLinkage(RefSummary->linkage()) &&
294  RefSummary->modulePath() != Summary.modulePath();
295  };
296 
297  for (auto &RefSummary : VI.getSummaryList())
298  if (isa<GlobalVarSummary>(RefSummary.get()) &&
299  Index.canImportGlobalVar(RefSummary.get(), /* AnalyzeRefs */ true) &&
300  !LocalNotInModule(RefSummary.get())) {
301  auto ILI = ImportList[RefSummary->modulePath()].insert(VI.getGUID());
302  // Only update stat and exports if we haven't already imported this
303  // variable.
304  if (!ILI.second)
305  break;
306  NumImportedGlobalVarsThinLink++;
307  // Any references made by this variable will be marked exported later,
308  // in ComputeCrossModuleImport, after import decisions are complete,
309  // which is more efficient than adding them here.
310  if (ExportLists)
311  (*ExportLists)[RefSummary->modulePath()].insert(VI);
312 
313  // If variable is not writeonly we attempt to recursively analyze
314  // its references in order to import referenced constants.
315  if (!Index.isWriteOnly(cast<GlobalVarSummary>(RefSummary.get())))
316  Worklist.emplace_back(RefSummary.get(), 0);
317  break;
318  }
319  }
320 }
321 
322 static const char *
324  switch (Reason) {
326  return "None";
328  return "GlobalVar";
329  case FunctionImporter::ImportFailureReason::NotLive:
330  return "NotLive";
331  case FunctionImporter::ImportFailureReason::TooLarge:
332  return "TooLarge";
333  case FunctionImporter::ImportFailureReason::InterposableLinkage:
334  return "InterposableLinkage";
335  case FunctionImporter::ImportFailureReason::LocalLinkageNotInModule:
336  return "LocalLinkageNotInModule";
337  case FunctionImporter::ImportFailureReason::NotEligible:
338  return "NotEligible";
339  case FunctionImporter::ImportFailureReason::NoInline:
340  return "NoInline";
341  }
342  llvm_unreachable("invalid reason");
343 }
344 
345 /// Compute the list of functions to import for a given caller. Mark these
346 /// imported functions and the symbols they reference in their source module as
347 /// exported from their source module.
349  const FunctionSummary &Summary, const ModuleSummaryIndex &Index,
350  const unsigned Threshold, const GVSummaryMapTy &DefinedGVSummaries,
351  SmallVectorImpl<EdgeInfo> &Worklist,
352  FunctionImporter::ImportMapTy &ImportList,
354  FunctionImporter::ImportThresholdsTy &ImportThresholds) {
355  computeImportForReferencedGlobals(Summary, Index, DefinedGVSummaries,
356  Worklist, ImportList, ExportLists);
357  static int ImportCount = 0;
358  for (auto &Edge : Summary.calls()) {
359  ValueInfo VI = Edge.first;
360  LLVM_DEBUG(dbgs() << " edge -> " << VI << " Threshold:" << Threshold
361  << "\n");
362 
363  if (ImportCutoff >= 0 && ImportCount >= ImportCutoff) {
364  LLVM_DEBUG(dbgs() << "ignored! import-cutoff value of " << ImportCutoff
365  << " reached.\n");
366  continue;
367  }
368 
369  if (DefinedGVSummaries.count(VI.getGUID())) {
370  // FIXME: Consider not skipping import if the module contains
371  // a non-prevailing def with interposable linkage. The prevailing copy
372  // can safely be imported (see shouldImportGlobal()).
373  LLVM_DEBUG(dbgs() << "ignored! Target already in destination module.\n");
374  continue;
375  }
376 
377  auto GetBonusMultiplier = [](CalleeInfo::HotnessType Hotness) -> float {
378  if (Hotness == CalleeInfo::HotnessType::Hot)
379  return ImportHotMultiplier;
380  if (Hotness == CalleeInfo::HotnessType::Cold)
381  return ImportColdMultiplier;
382  if (Hotness == CalleeInfo::HotnessType::Critical)
384  return 1.0;
385  };
386 
387  const auto NewThreshold =
388  Threshold * GetBonusMultiplier(Edge.second.getHotness());
389 
390  auto IT = ImportThresholds.insert(std::make_pair(
391  VI.getGUID(), std::make_tuple(NewThreshold, nullptr, nullptr)));
392  bool PreviouslyVisited = !IT.second;
393  auto &ProcessedThreshold = std::get<0>(IT.first->second);
394  auto &CalleeSummary = std::get<1>(IT.first->second);
395  auto &FailureInfo = std::get<2>(IT.first->second);
396 
397  bool IsHotCallsite =
398  Edge.second.getHotness() == CalleeInfo::HotnessType::Hot;
399  bool IsCriticalCallsite =
400  Edge.second.getHotness() == CalleeInfo::HotnessType::Critical;
401 
402  const FunctionSummary *ResolvedCalleeSummary = nullptr;
403  if (CalleeSummary) {
404  assert(PreviouslyVisited);
405  // Since the traversal of the call graph is DFS, we can revisit a function
406  // a second time with a higher threshold. In this case, it is added back
407  // to the worklist with the new threshold (so that its own callee chains
408  // can be considered with the higher threshold).
409  if (NewThreshold <= ProcessedThreshold) {
410  LLVM_DEBUG(
411  dbgs() << "ignored! Target was already imported with Threshold "
412  << ProcessedThreshold << "\n");
413  continue;
414  }
415  // Update with new larger threshold.
416  ProcessedThreshold = NewThreshold;
417  ResolvedCalleeSummary = cast<FunctionSummary>(CalleeSummary);
418  } else {
419  // If we already rejected importing a callee at the same or higher
420  // threshold, don't waste time calling selectCallee.
421  if (PreviouslyVisited && NewThreshold <= ProcessedThreshold) {
422  LLVM_DEBUG(
423  dbgs() << "ignored! Target was already rejected with Threshold "
424  << ProcessedThreshold << "\n");
425  if (PrintImportFailures) {
426  assert(FailureInfo &&
427  "Expected FailureInfo for previously rejected candidate");
428  FailureInfo->Attempts++;
429  }
430  continue;
431  }
432 
434  CalleeSummary = selectCallee(Index, VI.getSummaryList(), NewThreshold,
435  Summary.modulePath(), Reason, VI.getGUID());
436  if (!CalleeSummary) {
437  // Update with new larger threshold if this was a retry (otherwise
438  // we would have already inserted with NewThreshold above). Also
439  // update failure info if requested.
440  if (PreviouslyVisited) {
441  ProcessedThreshold = NewThreshold;
442  if (PrintImportFailures) {
443  assert(FailureInfo &&
444  "Expected FailureInfo for previously rejected candidate");
445  FailureInfo->Reason = Reason;
446  FailureInfo->Attempts++;
447  FailureInfo->MaxHotness =
448  std::max(FailureInfo->MaxHotness, Edge.second.getHotness());
449  }
450  } else if (PrintImportFailures) {
451  assert(!FailureInfo &&
452  "Expected no FailureInfo for newly rejected candidate");
453  FailureInfo = std::make_unique<FunctionImporter::ImportFailureInfo>(
454  VI, Edge.second.getHotness(), Reason, 1);
455  }
456  if (ForceImportAll) {
457  std::string Msg = std::string("Failed to import function ") +
458  VI.name().str() + " due to " +
459  getFailureName(Reason);
460  auto Error = make_error<StringError>(
463  "Error importing module: ");
464  break;
465  } else {
466  LLVM_DEBUG(dbgs()
467  << "ignored! No qualifying callee with summary found.\n");
468  continue;
469  }
470  }
471 
472  // "Resolve" the summary
473  CalleeSummary = CalleeSummary->getBaseObject();
474  ResolvedCalleeSummary = cast<FunctionSummary>(CalleeSummary);
475 
476  assert((ResolvedCalleeSummary->fflags().AlwaysInline || ForceImportAll ||
477  (ResolvedCalleeSummary->instCount() <= NewThreshold)) &&
478  "selectCallee() didn't honor the threshold");
479 
480  auto ExportModulePath = ResolvedCalleeSummary->modulePath();
481  auto ILI = ImportList[ExportModulePath].insert(VI.getGUID());
482  // We previously decided to import this GUID definition if it was already
483  // inserted in the set of imports from the exporting module.
484  bool PreviouslyImported = !ILI.second;
485  if (!PreviouslyImported) {
486  NumImportedFunctionsThinLink++;
487  if (IsHotCallsite)
488  NumImportedHotFunctionsThinLink++;
489  if (IsCriticalCallsite)
490  NumImportedCriticalFunctionsThinLink++;
491  }
492 
493  // Any calls/references made by this function will be marked exported
494  // later, in ComputeCrossModuleImport, after import decisions are
495  // complete, which is more efficient than adding them here.
496  if (ExportLists)
497  (*ExportLists)[ExportModulePath].insert(VI);
498  }
499 
500  auto GetAdjustedThreshold = [](unsigned Threshold, bool IsHotCallsite) {
501  // Adjust the threshold for next level of imported functions.
502  // The threshold is different for hot callsites because we can then
503  // inline chains of hot calls.
504  if (IsHotCallsite)
505  return Threshold * ImportHotInstrFactor;
506  return Threshold * ImportInstrFactor;
507  };
508 
509  const auto AdjThreshold = GetAdjustedThreshold(Threshold, IsHotCallsite);
510 
511  ImportCount++;
512 
513  // Insert the newly imported function to the worklist.
514  Worklist.emplace_back(ResolvedCalleeSummary, AdjThreshold);
515  }
516 }
517 
518 /// Given the list of globals defined in a module, compute the list of imports
519 /// as well as the list of "exports", i.e. the list of symbols referenced from
520 /// another module (that may require promotion).
522  const GVSummaryMapTy &DefinedGVSummaries, const ModuleSummaryIndex &Index,
523  StringRef ModName, FunctionImporter::ImportMapTy &ImportList,
524  StringMap<FunctionImporter::ExportSetTy> *ExportLists = nullptr) {
525  // Worklist contains the list of function imported in this module, for which
526  // we will analyse the callees and may import further down the callgraph.
528  FunctionImporter::ImportThresholdsTy ImportThresholds;
529 
530  // Populate the worklist with the import for the functions in the current
531  // module
532  for (auto &GVSummary : DefinedGVSummaries) {
533 #ifndef NDEBUG
534  // FIXME: Change the GVSummaryMapTy to hold ValueInfo instead of GUID
535  // so this map look up (and possibly others) can be avoided.
536  auto VI = Index.getValueInfo(GVSummary.first);
537 #endif
538  if (!Index.isGlobalValueLive(GVSummary.second)) {
539  LLVM_DEBUG(dbgs() << "Ignores Dead GUID: " << VI << "\n");
540  continue;
541  }
542  auto *FuncSummary =
543  dyn_cast<FunctionSummary>(GVSummary.second->getBaseObject());
544  if (!FuncSummary)
545  // Skip import for global variables
546  continue;
547  LLVM_DEBUG(dbgs() << "Initialize import for " << VI << "\n");
549  DefinedGVSummaries, Worklist, ImportList,
550  ExportLists, ImportThresholds);
551  }
552 
553  // Process the newly imported functions and add callees to the worklist.
554  while (!Worklist.empty()) {
555  auto GVInfo = Worklist.pop_back_val();
556  auto *Summary = std::get<0>(GVInfo);
557  auto Threshold = std::get<1>(GVInfo);
558 
559  if (auto *FS = dyn_cast<FunctionSummary>(Summary))
560  computeImportForFunction(*FS, Index, Threshold, DefinedGVSummaries,
561  Worklist, ImportList, ExportLists,
562  ImportThresholds);
563  else
564  computeImportForReferencedGlobals(*Summary, Index, DefinedGVSummaries,
565  Worklist, ImportList, ExportLists);
566  }
567 
568  // Print stats about functions considered but rejected for importing
569  // when requested.
570  if (PrintImportFailures) {
571  dbgs() << "Missed imports into module " << ModName << "\n";
572  for (auto &I : ImportThresholds) {
573  auto &ProcessedThreshold = std::get<0>(I.second);
574  auto &CalleeSummary = std::get<1>(I.second);
575  auto &FailureInfo = std::get<2>(I.second);
576  if (CalleeSummary)
577  continue; // We are going to import.
578  assert(FailureInfo);
579  FunctionSummary *FS = nullptr;
580  if (!FailureInfo->VI.getSummaryList().empty())
581  FS = dyn_cast<FunctionSummary>(
582  FailureInfo->VI.getSummaryList()[0]->getBaseObject());
583  dbgs() << FailureInfo->VI
584  << ": Reason = " << getFailureName(FailureInfo->Reason)
585  << ", Threshold = " << ProcessedThreshold
586  << ", Size = " << (FS ? (int)FS->instCount() : -1)
587  << ", MaxHotness = " << getHotnessName(FailureInfo->MaxHotness)
588  << ", Attempts = " << FailureInfo->Attempts << "\n";
589  }
590  }
591 }
592 
593 #ifndef NDEBUG
595  auto SL = VI.getSummaryList();
596  return SL.empty()
597  ? false
598  : SL[0]->getSummaryKind() == GlobalValueSummary::GlobalVarKind;
599 }
600 
603  if (const auto &VI = Index.getValueInfo(G))
604  return isGlobalVarSummary(Index, VI);
605  return false;
606 }
607 
608 template <class T>
610  T &Cont) {
611  unsigned NumGVS = 0;
612  for (auto &V : Cont)
613  if (isGlobalVarSummary(Index, V))
614  ++NumGVS;
615  return NumGVS;
616 }
617 #endif
618 
619 #ifndef NDEBUG
620 static bool
624 
625  DenseSet<GlobalValue::GUID> FlattenedImports;
626 
627  for (auto &ImportPerModule : ImportLists)
628  for (auto &ExportPerModule : ImportPerModule.second)
629  FlattenedImports.insert(ExportPerModule.second.begin(),
630  ExportPerModule.second.end());
631 
632  // Checks that all GUIDs of read/writeonly vars we see in export lists
633  // are also in the import lists. Otherwise we my face linker undefs,
634  // because readonly and writeonly vars are internalized in their
635  // source modules.
636  auto IsReadOrWriteOnlyVar = [&](StringRef ModulePath, const ValueInfo &VI) {
637  auto *GVS = dyn_cast_or_null<GlobalVarSummary>(
638  Index.findSummaryInModule(VI, ModulePath));
639  return GVS && (Index.isReadOnly(GVS) || Index.isWriteOnly(GVS));
640  };
641 
642  for (auto &ExportPerModule : ExportLists)
643  for (auto &VI : ExportPerModule.second)
644  if (!FlattenedImports.count(VI.getGUID()) &&
645  IsReadOrWriteOnlyVar(ExportPerModule.first(), VI))
646  return false;
647 
648  return true;
649 }
650 #endif
651 
652 /// Compute all the import and export for every module using the Index.
654  const ModuleSummaryIndex &Index,
655  const StringMap<GVSummaryMapTy> &ModuleToDefinedGVSummaries,
658  // For each module that has function defined, compute the import/export lists.
659  for (auto &DefinedGVSummaries : ModuleToDefinedGVSummaries) {
660  auto &ImportList = ImportLists[DefinedGVSummaries.first()];
661  LLVM_DEBUG(dbgs() << "Computing import for Module '"
662  << DefinedGVSummaries.first() << "'\n");
663  ComputeImportForModule(DefinedGVSummaries.second, Index,
664  DefinedGVSummaries.first(), ImportList,
665  &ExportLists);
666  }
667 
668  // When computing imports we only added the variables and functions being
669  // imported to the export list. We also need to mark any references and calls
670  // they make as exported as well. We do this here, as it is more efficient
671  // since we may import the same values multiple times into different modules
672  // during the import computation.
673  for (auto &ELI : ExportLists) {
675  const auto &DefinedGVSummaries =
676  ModuleToDefinedGVSummaries.lookup(ELI.first());
677  for (auto &EI : ELI.second) {
678  // Find the copy defined in the exporting module so that we can mark the
679  // values it references in that specific definition as exported.
680  // Below we will add all references and called values, without regard to
681  // whether they are also defined in this module. We subsequently prune the
682  // list to only include those defined in the exporting module, see comment
683  // there as to why.
684  auto DS = DefinedGVSummaries.find(EI.getGUID());
685  // Anything marked exported during the import computation must have been
686  // defined in the exporting module.
687  assert(DS != DefinedGVSummaries.end());
688  auto *S = DS->getSecond();
689  S = S->getBaseObject();
690  if (auto *GVS = dyn_cast<GlobalVarSummary>(S)) {
691  // Export referenced functions and variables. We don't export/promote
692  // objects referenced by writeonly variable initializer, because
693  // we convert such variables initializers to "zeroinitializer".
694  // See processGlobalForThinLTO.
695  if (!Index.isWriteOnly(GVS))
696  for (const auto &VI : GVS->refs())
697  NewExports.insert(VI);
698  } else {
699  auto *FS = cast<FunctionSummary>(S);
700  for (auto &Edge : FS->calls())
701  NewExports.insert(Edge.first);
702  for (auto &Ref : FS->refs())
703  NewExports.insert(Ref);
704  }
705  }
706  // Prune list computed above to only include values defined in the exporting
707  // module. We do this after the above insertion since we may hit the same
708  // ref/call target multiple times in above loop, and it is more efficient to
709  // avoid a set lookup each time.
710  for (auto EI = NewExports.begin(); EI != NewExports.end();) {
711  if (!DefinedGVSummaries.count(EI->getGUID()))
712  NewExports.erase(EI++);
713  else
714  ++EI;
715  }
716  ELI.second.insert(NewExports.begin(), NewExports.end());
717  }
718 
719  assert(checkVariableImport(Index, ImportLists, ExportLists));
720 #ifndef NDEBUG
721  LLVM_DEBUG(dbgs() << "Import/Export lists for " << ImportLists.size()
722  << " modules:\n");
723  for (auto &ModuleImports : ImportLists) {
724  auto ModName = ModuleImports.first();
725  auto &Exports = ExportLists[ModName];
726  unsigned NumGVS = numGlobalVarSummaries(Index, Exports);
727  LLVM_DEBUG(dbgs() << "* Module " << ModName << " exports "
728  << Exports.size() - NumGVS << " functions and " << NumGVS
729  << " vars. Imports from " << ModuleImports.second.size()
730  << " modules.\n");
731  for (auto &Src : ModuleImports.second) {
732  auto SrcModName = Src.first();
733  unsigned NumGVSPerMod = numGlobalVarSummaries(Index, Src.second);
734  LLVM_DEBUG(dbgs() << " - " << Src.second.size() - NumGVSPerMod
735  << " functions imported from " << SrcModName << "\n");
736  LLVM_DEBUG(dbgs() << " - " << NumGVSPerMod
737  << " global vars imported from " << SrcModName << "\n");
738  }
739  }
740 #endif
741 }
742 
743 #ifndef NDEBUG
745  StringRef ModulePath,
746  FunctionImporter::ImportMapTy &ImportList) {
747  LLVM_DEBUG(dbgs() << "* Module " << ModulePath << " imports from "
748  << ImportList.size() << " modules.\n");
749  for (auto &Src : ImportList) {
750  auto SrcModName = Src.first();
751  unsigned NumGVSPerMod = numGlobalVarSummaries(Index, Src.second);
752  LLVM_DEBUG(dbgs() << " - " << Src.second.size() - NumGVSPerMod
753  << " functions imported from " << SrcModName << "\n");
754  LLVM_DEBUG(dbgs() << " - " << NumGVSPerMod << " vars imported from "
755  << SrcModName << "\n");
756  }
757 }
758 #endif
759 
760 /// Compute all the imports for the given module in the Index.
762  StringRef ModulePath, const ModuleSummaryIndex &Index,
763  FunctionImporter::ImportMapTy &ImportList) {
764  // Collect the list of functions this module defines.
765  // GUID -> Summary
766  GVSummaryMapTy FunctionSummaryMap;
767  Index.collectDefinedFunctionsForModule(ModulePath, FunctionSummaryMap);
768 
769  // Compute the import list for this module.
770  LLVM_DEBUG(dbgs() << "Computing import for Module '" << ModulePath << "'\n");
771  ComputeImportForModule(FunctionSummaryMap, Index, ModulePath, ImportList);
772 
773 #ifndef NDEBUG
774  dumpImportListForModule(Index, ModulePath, ImportList);
775 #endif
776 }
777 
778 // Mark all external summaries in Index for import into the given module.
779 // Used for distributed builds using a distributed index.
781  StringRef ModulePath, const ModuleSummaryIndex &Index,
782  FunctionImporter::ImportMapTy &ImportList) {
783  for (auto &GlobalList : Index) {
784  // Ignore entries for undefined references.
785  if (GlobalList.second.SummaryList.empty())
786  continue;
787 
788  auto GUID = GlobalList.first;
789  assert(GlobalList.second.SummaryList.size() == 1 &&
790  "Expected individual combined index to have one summary per GUID");
791  auto &Summary = GlobalList.second.SummaryList[0];
792  // Skip the summaries for the importing module. These are included to
793  // e.g. record required linkage changes.
794  if (Summary->modulePath() == ModulePath)
795  continue;
796  // Add an entry to provoke importing by thinBackend.
797  ImportList[Summary->modulePath()].insert(GUID);
798  }
799 #ifndef NDEBUG
800  dumpImportListForModule(Index, ModulePath, ImportList);
801 #endif
802 }
803 
804 // For SamplePGO, the indirect call targets for local functions will
805 // have its original name annotated in profile. We try to find the
806 // corresponding PGOFuncName as the GUID, and fix up the edges
807 // accordingly.
809  FunctionSummary *FS) {
810  for (auto &EI : FS->mutableCalls()) {
811  if (!EI.first.getSummaryList().empty())
812  continue;
813  auto GUID = Index.getGUIDFromOriginalID(EI.first.getGUID());
814  if (GUID == 0)
815  continue;
816  // Update the edge to point directly to the correct GUID.
817  auto VI = Index.getValueInfo(GUID);
818  if (llvm::any_of(
819  VI.getSummaryList(),
820  [&](const std::unique_ptr<GlobalValueSummary> &SummaryPtr) {
821  // The mapping from OriginalId to GUID may return a GUID
822  // that corresponds to a static variable. Filter it out here.
823  // This can happen when
824  // 1) There is a call to a library function which is not defined
825  // in the index.
826  // 2) There is a static variable with the OriginalGUID identical
827  // to the GUID of the library function in 1);
828  // When this happens the static variable in 2) will be found,
829  // which needs to be filtered out.
830  return SummaryPtr->getSummaryKind() ==
831  GlobalValueSummary::GlobalVarKind;
832  }))
833  continue;
834  EI.first = VI;
835  }
836 }
837 
839  for (const auto &Entry : Index) {
840  for (auto &S : Entry.second.SummaryList) {
841  if (auto *FS = dyn_cast<FunctionSummary>(S.get()))
843  }
844  }
845 }
846 
849  const DenseSet<GlobalValue::GUID> &GUIDPreservedSymbols,
851  assert(!Index.withGlobalValueDeadStripping());
852  if (!ComputeDead ||
853  // Don't do anything when nothing is live, this is friendly with tests.
854  GUIDPreservedSymbols.empty()) {
855  // Still need to update indirect calls.
857  return;
858  }
859  unsigned LiveSymbols = 0;
861  Worklist.reserve(GUIDPreservedSymbols.size() * 2);
862  for (auto GUID : GUIDPreservedSymbols) {
863  ValueInfo VI = Index.getValueInfo(GUID);
864  if (!VI)
865  continue;
866  for (auto &S : VI.getSummaryList())
867  S->setLive(true);
868  }
869 
870  // Add values flagged in the index as live roots to the worklist.
871  for (const auto &Entry : Index) {
872  auto VI = Index.getValueInfo(Entry);
873  for (auto &S : Entry.second.SummaryList) {
874  if (auto *FS = dyn_cast<FunctionSummary>(S.get()))
876  if (S->isLive()) {
877  LLVM_DEBUG(dbgs() << "Live root: " << VI << "\n");
878  Worklist.push_back(VI);
879  ++LiveSymbols;
880  break;
881  }
882  }
883  }
884 
885  // Make value live and add it to the worklist if it was not live before.
886  auto visit = [&](ValueInfo VI, bool IsAliasee) {
887  // FIXME: If we knew which edges were created for indirect call profiles,
888  // we could skip them here. Any that are live should be reached via
889  // other edges, e.g. reference edges. Otherwise, using a profile collected
890  // on a slightly different binary might provoke preserving, importing
891  // and ultimately promoting calls to functions not linked into this
892  // binary, which increases the binary size unnecessarily. Note that
893  // if this code changes, the importer needs to change so that edges
894  // to functions marked dead are skipped.
895 
896  if (llvm::any_of(VI.getSummaryList(),
897  [](const std::unique_ptr<llvm::GlobalValueSummary> &S) {
898  return S->isLive();
899  }))
900  return;
901 
902  // We only keep live symbols that are known to be non-prevailing if any are
903  // available_externally, linkonceodr, weakodr. Those symbols are discarded
904  // later in the EliminateAvailableExternally pass and setting them to
905  // not-live could break downstreams users of liveness information (PR36483)
906  // or limit optimization opportunities.
907  if (isPrevailing(VI.getGUID()) == PrevailingType::No) {
908  bool KeepAliveLinkage = false;
909  bool Interposable = false;
910  for (auto &S : VI.getSummaryList()) {
911  if (S->linkage() == GlobalValue::AvailableExternallyLinkage ||
912  S->linkage() == GlobalValue::WeakODRLinkage ||
913  S->linkage() == GlobalValue::LinkOnceODRLinkage)
914  KeepAliveLinkage = true;
915  else if (GlobalValue::isInterposableLinkage(S->linkage()))
916  Interposable = true;
917  }
918 
919  if (!IsAliasee) {
920  if (!KeepAliveLinkage)
921  return;
922 
923  if (Interposable)
925  "Interposable and available_externally/linkonce_odr/weak_odr "
926  "symbol");
927  }
928  }
929 
930  for (auto &S : VI.getSummaryList())
931  S->setLive(true);
932  ++LiveSymbols;
933  Worklist.push_back(VI);
934  };
935 
936  while (!Worklist.empty()) {
937  auto VI = Worklist.pop_back_val();
938  for (auto &Summary : VI.getSummaryList()) {
939  if (auto *AS = dyn_cast<AliasSummary>(Summary.get())) {
940  // If this is an alias, visit the aliasee VI to ensure that all copies
941  // are marked live and it is added to the worklist for further
942  // processing of its references.
943  visit(AS->getAliaseeVI(), true);
944  continue;
945  }
946  for (auto Ref : Summary->refs())
947  visit(Ref, false);
948  if (auto *FS = dyn_cast<FunctionSummary>(Summary.get()))
949  for (auto Call : FS->calls())
950  visit(Call.first, false);
951  }
952  }
953  Index.setWithGlobalValueDeadStripping();
954 
955  unsigned DeadSymbols = Index.size() - LiveSymbols;
956  LLVM_DEBUG(dbgs() << LiveSymbols << " symbols Live, and " << DeadSymbols
957  << " symbols Dead \n");
958  NumDeadSymbols += DeadSymbols;
959  NumLiveSymbols += LiveSymbols;
960 }
961 
962 // Compute dead symbols and propagate constants in combined index.
965  const DenseSet<GlobalValue::GUID> &GUIDPreservedSymbols,
967  bool ImportEnabled) {
968  computeDeadSymbolsAndUpdateIndirectCalls(Index, GUIDPreservedSymbols,
969  isPrevailing);
970  if (ImportEnabled)
971  Index.propagateAttributes(GUIDPreservedSymbols);
972 }
973 
974 /// Compute the set of summaries needed for a ThinLTO backend compilation of
975 /// \p ModulePath.
977  StringRef ModulePath,
978  const StringMap<GVSummaryMapTy> &ModuleToDefinedGVSummaries,
979  const FunctionImporter::ImportMapTy &ImportList,
980  std::map<std::string, GVSummaryMapTy> &ModuleToSummariesForIndex) {
981  // Include all summaries from the importing module.
982  ModuleToSummariesForIndex[std::string(ModulePath)] =
983  ModuleToDefinedGVSummaries.lookup(ModulePath);
984  // Include summaries for imports.
985  for (auto &ILI : ImportList) {
986  auto &SummariesForIndex =
987  ModuleToSummariesForIndex[std::string(ILI.first())];
988  const auto &DefinedGVSummaries =
989  ModuleToDefinedGVSummaries.lookup(ILI.first());
990  for (auto &GI : ILI.second) {
991  const auto &DS = DefinedGVSummaries.find(GI);
992  assert(DS != DefinedGVSummaries.end() &&
993  "Expected a defined summary for imported global value");
994  SummariesForIndex[GI] = DS->second;
995  }
996  }
997 }
998 
999 /// Emit the files \p ModulePath will import from into \p OutputFilename.
1000 std::error_code llvm::EmitImportsFiles(
1001  StringRef ModulePath, StringRef OutputFilename,
1002  const std::map<std::string, GVSummaryMapTy> &ModuleToSummariesForIndex) {
1003  std::error_code EC;
1005  if (EC)
1006  return EC;
1007  for (auto &ILI : ModuleToSummariesForIndex)
1008  // The ModuleToSummariesForIndex map includes an entry for the current
1009  // Module (needed for writing out the index files). We don't want to
1010  // include it in the imports file, however, so filter it out.
1011  if (ILI.first != ModulePath)
1012  ImportsOS << ILI.first << "\n";
1013  return std::error_code();
1014 }
1015 
1017  LLVM_DEBUG(dbgs() << "Converting to a declaration: `" << GV.getName()
1018  << "\n");
1019  if (Function *F = dyn_cast<Function>(&GV)) {
1020  F->deleteBody();
1021  F->clearMetadata();
1022  F->setComdat(nullptr);
1023  } else if (GlobalVariable *V = dyn_cast<GlobalVariable>(&GV)) {
1024  V->setInitializer(nullptr);
1025  V->setLinkage(GlobalValue::ExternalLinkage);
1026  V->clearMetadata();
1027  V->setComdat(nullptr);
1028  } else {
1029  GlobalValue *NewGV;
1030  if (GV.getValueType()->isFunctionTy())
1031  NewGV =
1032  Function::Create(cast<FunctionType>(GV.getValueType()),
1034  "", GV.getParent());
1035  else
1036  NewGV =
1037  new GlobalVariable(*GV.getParent(), GV.getValueType(),
1038  /*isConstant*/ false, GlobalValue::ExternalLinkage,
1039  /*init*/ nullptr, "",
1040  /*insertbefore*/ nullptr, GV.getThreadLocalMode(),
1041  GV.getType()->getAddressSpace());
1042  NewGV->takeName(&GV);
1043  GV.replaceAllUsesWith(NewGV);
1044  return false;
1045  }
1046  if (!GV.isImplicitDSOLocal())
1047  GV.setDSOLocal(false);
1048  return true;
1049 }
1050 
1052  const GVSummaryMapTy &DefinedGlobals,
1053  bool PropagateAttrs) {
1054  auto FinalizeInModule = [&](GlobalValue &GV, bool Propagate = false) {
1055  // See if the global summary analysis computed a new resolved linkage.
1056  const auto &GS = DefinedGlobals.find(GV.getGUID());
1057  if (GS == DefinedGlobals.end())
1058  return;
1059 
1060  if (Propagate)
1061  if (FunctionSummary *FS = dyn_cast<FunctionSummary>(GS->second)) {
1062  if (Function *F = dyn_cast<Function>(&GV)) {
1063  // TODO: propagate ReadNone and ReadOnly.
1064  if (FS->fflags().ReadNone && !F->doesNotAccessMemory())
1065  F->setDoesNotAccessMemory();
1066 
1067  if (FS->fflags().ReadOnly && !F->onlyReadsMemory())
1068  F->setOnlyReadsMemory();
1069 
1070  if (FS->fflags().NoRecurse && !F->doesNotRecurse())
1071  F->setDoesNotRecurse();
1072 
1073  if (FS->fflags().NoUnwind && !F->doesNotThrow())
1074  F->setDoesNotThrow();
1075  }
1076  }
1077 
1078  auto NewLinkage = GS->second->linkage();
1080  // Don't internalize anything here, because the code below
1081  // lacks necessary correctness checks. Leave this job to
1082  // LLVM 'internalize' pass.
1083  GlobalValue::isLocalLinkage(NewLinkage) ||
1084  // In case it was dead and already converted to declaration.
1085  GV.isDeclaration())
1086  return;
1087 
1088  // Set the potentially more constraining visibility computed from summaries.
1089  // The DefaultVisibility condition is because older GlobalValueSummary does
1090  // not record DefaultVisibility and we don't want to change protected/hidden
1091  // to default.
1092  if (GS->second->getVisibility() != GlobalValue::DefaultVisibility)
1093  GV.setVisibility(GS->second->getVisibility());
1094 
1095  if (NewLinkage == GV.getLinkage())
1096  return;
1097 
1098  // Check for a non-prevailing def that has interposable linkage
1099  // (e.g. non-odr weak or linkonce). In that case we can't simply
1100  // convert to available_externally, since it would lose the
1101  // interposable property and possibly get inlined. Simply drop
1102  // the definition in that case.
1105  if (!convertToDeclaration(GV))
1106  // FIXME: Change this to collect replaced GVs and later erase
1107  // them from the parent module once thinLTOResolvePrevailingGUID is
1108  // changed to enable this for aliases.
1109  llvm_unreachable("Expected GV to be converted");
1110  } else {
1111  // If all copies of the original symbol had global unnamed addr and
1112  // linkonce_odr linkage, or if all of them had local unnamed addr linkage
1113  // and are constants, then it should be an auto hide symbol. In that case
1114  // the thin link would have marked it as CanAutoHide. Add hidden
1115  // visibility to the symbol to preserve the property.
1116  if (NewLinkage == GlobalValue::WeakODRLinkage &&
1117  GS->second->canAutoHide()) {
1120  }
1121 
1122  LLVM_DEBUG(dbgs() << "ODR fixing up linkage for `" << GV.getName()
1123  << "` from " << GV.getLinkage() << " to " << NewLinkage
1124  << "\n");
1125  GV.setLinkage(NewLinkage);
1126  }
1127  // Remove declarations from comdats, including available_externally
1128  // as this is a declaration for the linker, and will be dropped eventually.
1129  // It is illegal for comdats to contain declarations.
1130  auto *GO = dyn_cast_or_null<GlobalObject>(&GV);
1131  if (GO && GO->isDeclarationForLinker() && GO->hasComdat())
1132  GO->setComdat(nullptr);
1133  };
1134 
1135  // Process functions and global now
1136  for (auto &GV : TheModule)
1137  FinalizeInModule(GV, PropagateAttrs);
1138  for (auto &GV : TheModule.globals())
1139  FinalizeInModule(GV);
1140  for (auto &GV : TheModule.aliases())
1141  FinalizeInModule(GV);
1142 }
1143 
1144 /// Run internalization on \p TheModule based on symmary analysis.
1146  const GVSummaryMapTy &DefinedGlobals) {
1147  // Declare a callback for the internalize pass that will ask for every
1148  // candidate GlobalValue if it can be internalized or not.
1149  auto MustPreserveGV = [&](const GlobalValue &GV) -> bool {
1150  // Lookup the linkage recorded in the summaries during global analysis.
1151  auto GS = DefinedGlobals.find(GV.getGUID());
1152  if (GS == DefinedGlobals.end()) {
1153  // Must have been promoted (possibly conservatively). Find original
1154  // name so that we can access the correct summary and see if it can
1155  // be internalized again.
1156  // FIXME: Eventually we should control promotion instead of promoting
1157  // and internalizing again.
1158  StringRef OrigName =
1160  std::string OrigId = GlobalValue::getGlobalIdentifier(
1161  OrigName, GlobalValue::InternalLinkage,
1162  TheModule.getSourceFileName());
1163  GS = DefinedGlobals.find(GlobalValue::getGUID(OrigId));
1164  if (GS == DefinedGlobals.end()) {
1165  // Also check the original non-promoted non-globalized name. In some
1166  // cases a preempted weak value is linked in as a local copy because
1167  // it is referenced by an alias (IRLinker::linkGlobalValueProto).
1168  // In that case, since it was originally not a local value, it was
1169  // recorded in the index using the original name.
1170  // FIXME: This may not be needed once PR27866 is fixed.
1171  GS = DefinedGlobals.find(GlobalValue::getGUID(OrigName));
1172  assert(GS != DefinedGlobals.end());
1173  }
1174  }
1175  return !GlobalValue::isLocalLinkage(GS->second->linkage());
1176  };
1177 
1178  // FIXME: See if we can just internalize directly here via linkage changes
1179  // based on the index, rather than invoking internalizeModule.
1180  internalizeModule(TheModule, MustPreserveGV);
1181 }
1182 
1183 /// Make alias a clone of its aliasee.
1185  Function *Fn = cast<Function>(GA->getAliaseeObject());
1186 
1187  ValueToValueMapTy VMap;
1188  Function *NewFn = CloneFunction(Fn, VMap);
1189  // Clone should use the original alias's linkage, visibility and name, and we
1190  // ensure all uses of alias instead use the new clone (casted if necessary).
1191  NewFn->setLinkage(GA->getLinkage());
1192  NewFn->setVisibility(GA->getVisibility());
1194  NewFn->takeName(GA);
1195  return NewFn;
1196 }
1197 
1198 // Internalize values that we marked with specific attribute
1199 // in processGlobalForThinLTO.
1201  for (auto &GV : M.globals())
1202  // Skip GVs which have been converted to declarations
1203  // by dropDeadSymbols.
1204  if (!GV.isDeclaration() && GV.hasAttribute("thinlto-internalize")) {
1205  GV.setLinkage(GlobalValue::InternalLinkage);
1206  GV.setVisibility(GlobalValue::DefaultVisibility);
1207  }
1208 }
1209 
1210 // Automatically import functions in Module \p DestModule based on the summaries
1211 // index.
1213  Module &DestModule, const FunctionImporter::ImportMapTy &ImportList) {
1214  LLVM_DEBUG(dbgs() << "Starting import for Module "
1215  << DestModule.getModuleIdentifier() << "\n");
1216  unsigned ImportedCount = 0, ImportedGVCount = 0;
1217 
1218  IRMover Mover(DestModule);
1219  // Do the actual import of functions now, one Module at a time
1220  std::set<StringRef> ModuleNameOrderedList;
1221  for (auto &FunctionsToImportPerModule : ImportList) {
1222  ModuleNameOrderedList.insert(FunctionsToImportPerModule.first());
1223  }
1224  for (auto &Name : ModuleNameOrderedList) {
1225  // Get the module for the import
1226  const auto &FunctionsToImportPerModule = ImportList.find(Name);
1227  assert(FunctionsToImportPerModule != ImportList.end());
1228  Expected<std::unique_ptr<Module>> SrcModuleOrErr = ModuleLoader(Name);
1229  if (!SrcModuleOrErr)
1230  return SrcModuleOrErr.takeError();
1231  std::unique_ptr<Module> SrcModule = std::move(*SrcModuleOrErr);
1232  assert(&DestModule.getContext() == &SrcModule->getContext() &&
1233  "Context mismatch");
1234 
1235  // If modules were created with lazy metadata loading, materialize it
1236  // now, before linking it (otherwise this will be a noop).
1237  if (Error Err = SrcModule->materializeMetadata())
1238  return std::move(Err);
1239 
1240  auto &ImportGUIDs = FunctionsToImportPerModule->second;
1241  // Find the globals to import
1242  SetVector<GlobalValue *> GlobalsToImport;
1243  for (Function &F : *SrcModule) {
1244  if (!F.hasName())
1245  continue;
1246  auto GUID = F.getGUID();
1247  auto Import = ImportGUIDs.count(GUID);
1248  LLVM_DEBUG(dbgs() << (Import ? "Is" : "Not") << " importing function "
1249  << GUID << " " << F.getName() << " from "
1250  << SrcModule->getSourceFileName() << "\n");
1251  if (Import) {
1252  if (Error Err = F.materialize())
1253  return std::move(Err);
1254  if (EnableImportMetadata) {
1255  // Add 'thinlto_src_module' metadata for statistics and debugging.
1256  F.setMetadata(
1257  "thinlto_src_module",
1258  MDNode::get(DestModule.getContext(),
1259  {MDString::get(DestModule.getContext(),
1260  SrcModule->getSourceFileName())}));
1261  }
1262  GlobalsToImport.insert(&F);
1263  }
1264  }
1265  for (GlobalVariable &GV : SrcModule->globals()) {
1266  if (!GV.hasName())
1267  continue;
1268  auto GUID = GV.getGUID();
1269  auto Import = ImportGUIDs.count(GUID);
1270  LLVM_DEBUG(dbgs() << (Import ? "Is" : "Not") << " importing global "
1271  << GUID << " " << GV.getName() << " from "
1272  << SrcModule->getSourceFileName() << "\n");
1273  if (Import) {
1274  if (Error Err = GV.materialize())
1275  return std::move(Err);
1276  ImportedGVCount += GlobalsToImport.insert(&GV);
1277  }
1278  }
1279  for (GlobalAlias &GA : SrcModule->aliases()) {
1280  if (!GA.hasName())
1281  continue;
1282  auto GUID = GA.getGUID();
1283  auto Import = ImportGUIDs.count(GUID);
1284  LLVM_DEBUG(dbgs() << (Import ? "Is" : "Not") << " importing alias "
1285  << GUID << " " << GA.getName() << " from "
1286  << SrcModule->getSourceFileName() << "\n");
1287  if (Import) {
1288  if (Error Err = GA.materialize())
1289  return std::move(Err);
1290  // Import alias as a copy of its aliasee.
1291  GlobalObject *GO = GA.getAliaseeObject();
1292  if (Error Err = GO->materialize())
1293  return std::move(Err);
1294  auto *Fn = replaceAliasWithAliasee(SrcModule.get(), &GA);
1295  LLVM_DEBUG(dbgs() << "Is importing aliasee fn " << GO->getGUID() << " "
1296  << GO->getName() << " from "
1297  << SrcModule->getSourceFileName() << "\n");
1298  if (EnableImportMetadata) {
1299  // Add 'thinlto_src_module' metadata for statistics and debugging.
1300  Fn->setMetadata(
1301  "thinlto_src_module",
1302  MDNode::get(DestModule.getContext(),
1303  {MDString::get(DestModule.getContext(),
1304  SrcModule->getSourceFileName())}));
1305  }
1306  GlobalsToImport.insert(Fn);
1307  }
1308  }
1309 
1310  // Upgrade debug info after we're done materializing all the globals and we
1311  // have loaded all the required metadata!
1312  UpgradeDebugInfo(*SrcModule);
1313 
1314  // Set the partial sample profile ratio in the profile summary module flag
1315  // of the imported source module, if applicable, so that the profile summary
1316  // module flag will match with that of the destination module when it's
1317  // imported.
1318  SrcModule->setPartialSampleProfileRatio(Index);
1319 
1320  // Link in the specified functions.
1321  if (renameModuleForThinLTO(*SrcModule, Index, ClearDSOLocalOnDeclarations,
1322  &GlobalsToImport))
1323  return true;
1324 
1325  if (PrintImports) {
1326  for (const auto *GV : GlobalsToImport)
1327  dbgs() << DestModule.getSourceFileName() << ": Import " << GV->getName()
1328  << " from " << SrcModule->getSourceFileName() << "\n";
1329  }
1330 
1331  if (Error Err = Mover.move(std::move(SrcModule),
1332  GlobalsToImport.getArrayRef(), nullptr,
1333  /*IsPerformingImport=*/true))
1334  report_fatal_error(Twine("Function Import: link error: ") +
1335  toString(std::move(Err)));
1336 
1337  ImportedCount += GlobalsToImport.size();
1338  NumImportedModules++;
1339  }
1340 
1341  internalizeGVsAfterImport(DestModule);
1342 
1343  NumImportedFunctions += (ImportedCount - ImportedGVCount);
1344  NumImportedGlobalVars += ImportedGVCount;
1345 
1346  LLVM_DEBUG(dbgs() << "Imported " << ImportedCount - ImportedGVCount
1347  << " functions for Module "
1348  << DestModule.getModuleIdentifier() << "\n");
1349  LLVM_DEBUG(dbgs() << "Imported " << ImportedGVCount
1350  << " global variables for Module "
1351  << DestModule.getModuleIdentifier() << "\n");
1352  return ImportedCount;
1353 }
1354 
1355 static bool doImportingForModule(Module &M) {
1356  if (SummaryFile.empty())
1357  report_fatal_error("error: -function-import requires -summary-file\n");
1360  if (!IndexPtrOrErr) {
1361  logAllUnhandledErrors(IndexPtrOrErr.takeError(), errs(),
1362  "Error loading file '" + SummaryFile + "': ");
1363  return false;
1364  }
1365  std::unique_ptr<ModuleSummaryIndex> Index = std::move(*IndexPtrOrErr);
1366 
1367  // First step is collecting the import list.
1368  FunctionImporter::ImportMapTy ImportList;
1369  // If requested, simply import all functions in the index. This is used
1370  // when testing distributed backend handling via the opt tool, when
1371  // we have distributed indexes containing exactly the summaries to import.
1372  if (ImportAllIndex)
1373  ComputeCrossModuleImportForModuleFromIndex(M.getModuleIdentifier(), *Index,
1374  ImportList);
1375  else
1376  ComputeCrossModuleImportForModule(M.getModuleIdentifier(), *Index,
1377  ImportList);
1378 
1379  // Conservatively mark all internal values as promoted. This interface is
1380  // only used when doing importing via the function importing pass. The pass
1381  // is only enabled when testing importing via the 'opt' tool, which does
1382  // not do the ThinLink that would normally determine what values to promote.
1383  for (auto &I : *Index) {
1384  for (auto &S : I.second.SummaryList) {
1385  if (GlobalValue::isLocalLinkage(S->linkage()))
1386  S->setLinkage(GlobalValue::ExternalLinkage);
1387  }
1388  }
1389 
1390  // Next we need to promote to global scope and rename any local values that
1391  // are potentially exported to other modules.
1392  if (renameModuleForThinLTO(M, *Index, /*ClearDSOLocalOnDeclarations=*/false,
1393  /*GlobalsToImport=*/nullptr)) {
1394  errs() << "Error renaming module\n";
1395  return false;
1396  }
1397 
1398  // Perform the import now.
1399  auto ModuleLoader = [&M](StringRef Identifier) {
1400  return loadFile(std::string(Identifier), M.getContext());
1401  };
1402  FunctionImporter Importer(*Index, ModuleLoader,
1403  /*ClearDSOLocalOnDeclarations=*/false);
1404  Expected<bool> Result = Importer.importFunctions(M, ImportList);
1405 
1406  // FIXME: Probably need to propagate Errors through the pass manager.
1407  if (!Result) {
1408  logAllUnhandledErrors(Result.takeError(), errs(),
1409  "Error importing module: ");
1410  return false;
1411  }
1412 
1413  return *Result;
1414 }
1415 
1416 namespace {
1417 
1418 /// Pass that performs cross-module function import provided a summary file.
1419 class FunctionImportLegacyPass : public ModulePass {
1420 public:
1421  /// Pass identification, replacement for typeid
1422  static char ID;
1423 
1424  explicit FunctionImportLegacyPass() : ModulePass(ID) {}
1425 
1426  /// Specify pass name for debug output
1427  StringRef getPassName() const override { return "Function Importing"; }
1428 
1429  bool runOnModule(Module &M) override {
1430  if (skipModule(M))
1431  return false;
1432 
1433  return doImportingForModule(M);
1434  }
1435 };
1436 
1437 } // end anonymous namespace
1438 
1440  ModuleAnalysisManager &AM) {
1441  if (!doImportingForModule(M))
1442  return PreservedAnalyses::all();
1443 
1444  return PreservedAnalyses::none();
1445 }
1446 
1448 INITIALIZE_PASS(FunctionImportLegacyPass, "function-import",
1449  "Summary Based Function Import", false, false)
1450 
1451 namespace llvm {
1452 
1454  return new FunctionImportLegacyPass();
1455 }
1456 
1457 } // end namespace llvm
ComputeDead
static cl::opt< bool > ComputeDead("compute-dead", cl::init(true), cl::Hidden, cl::desc("Compute dead symbols"))
llvm::GlobalValue::GUID
uint64_t GUID
Declare a type to represent a global unique identifier for a global value.
Definition: GlobalValue.h:551
ImportInstrLimit
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.
llvm::PreservedAnalyses
A set of analyses that are preserved following a run of a transformation pass.
Definition: PassManager.h:152
FunctionImportUtils.h
llvm
This is an optimization pass for GlobalISel generic memory operations.
Definition: AddressRanges.h:17
ImportHotInstrFactor
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"))
M
We currently emits eax Perhaps this is what we really should generate is Is imull three or four cycles eax eax The current instruction priority is based on pattern complexity The former is more complex because it folds a load so the latter will not be emitted Perhaps we should use AddedComplexity to give LEA32r a higher priority We should always try to match LEA first since the LEA matching code does some estimate to determine whether the match is profitable if we care more about code then imull is better It s two bytes shorter than movl leal On a Pentium M
Definition: README.txt:252
llvm::thinLTOInternalizeModule
void thinLTOInternalizeModule(Module &TheModule, const GVSummaryMapTy &DefinedGlobals)
Internalize TheModule based on the information recorded in the summaries during global summary-based ...
Definition: FunctionImport.cpp:1145
ValueMapper.h
FileSystem.h
llvm::sys::fs::OF_None
@ OF_None
Definition: FileSystem.h:757
llvm::GlobalValue::getGlobalIdentifier
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:166
Metadata.h
llvm::UpgradeDebugInfo
bool UpgradeDebugInfo(Module &M)
Check the debug info version number, if it is out-dated, drop the debug info.
Definition: AutoUpgrade.cpp:4187
llvm::ModulePass
ModulePass class - This class is used to implement unstructured interprocedural optimizations and ana...
Definition: Pass.h:248
llvm::FunctionSummary::calls
ArrayRef< EdgeTy > calls() const
Return the list of <CalleeValueInfo, CalleeInfo> pairs.
Definition: ModuleSummaryIndex.h:758
llvm::GlobalValue::getLinkage
LinkageTypes getLinkage() const
Definition: GlobalValue.h:509
llvm::GlobalValue::HiddenVisibility
@ HiddenVisibility
The GV is hidden.
Definition: GlobalValue.h:64
T
llvm::FunctionSummary::fflags
FFlags fflags() const
Get function summary flags.
Definition: ModuleSummaryIndex.h:742
llvm::GlobalValue::isImplicitDSOLocal
bool isImplicitDSOLocal() const
Definition: GlobalValue.h:280
llvm::Function
Definition: Function.h:60
StringRef.h
llvm::renameModuleForThinLTO
bool renameModuleForThinLTO(Module &M, const ModuleSummaryIndex &Index, bool ClearDSOLocalOnDeclarations, SetVector< GlobalValue * > *GlobalsToImport=nullptr)
Perform in-place global value handling on the given Module for exported local functions renamed and p...
Definition: FunctionImportUtils.cpp:328
Pass.h
llvm::FunctionSummary::FFlags::AlwaysInline
unsigned AlwaysInline
Definition: ModuleSummaryIndex.h:573
llvm::SetVector::size
size_type size() const
Determine the number of elements in the SetVector.
Definition: SetVector.h:77
isGlobalVarSummary
static bool isGlobalVarSummary(const ModuleSummaryIndex &Index, ValueInfo VI)
Definition: FunctionImport.cpp:594
llvm::SmallVector
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small.
Definition: SmallVector.h:1185
Statistic.h
FunctionImport.h
llvm::GlobalValue::isLocalLinkage
static bool isLocalLinkage(LinkageTypes Linkage)
Definition: GlobalValue.h:374
llvm::GlobalValue::materialize
Error materialize()
Make sure this GlobalValue is fully read.
Definition: Globals.cpp:47
ErrorHandling.h
llvm::CalleeInfo::HotnessType::Cold
@ Cold
llvm::GlobalVariable
Definition: GlobalVariable.h:39
llvm::PointerType::getAddressSpace
unsigned getAddressSpace() const
Return the address space of the Pointer type.
Definition: DerivedTypes.h:682
computeImportForFunction
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, FunctionImporter::ImportThresholdsTy &ImportThresholds)
Compute the list of functions to import for a given caller.
Definition: FunctionImport.cpp:348
llvm::ConstantExpr::getBitCast
static Constant * getBitCast(Constant *C, Type *Ty, bool OnlyIfReduced=false)
Definition: Constants.cpp:2266
llvm::GlobalAlias
Definition: GlobalAlias.h:28
Error.h
llvm::cl::Hidden
@ Hidden
Definition: CommandLine.h:139
IT
static cl::opt< ITMode > IT(cl::desc("IT block support"), cl::Hidden, cl::init(DefaultIT), cl::values(clEnumValN(DefaultIT, "arm-default-it", "Generate any type of IT block"), clEnumValN(RestrictedIT, "arm-restrict-it", "Disallow complex IT blocks")))
Errc.h
llvm::PreservedAnalyses::none
static PreservedAnalyses none()
Convenience factory function for the empty preserved set.
Definition: PassManager.h:155
llvm::SetVector::getArrayRef
ArrayRef< T > getArrayRef() const
Definition: SetVector.h:63
Module.h
PropagateAttrs
static cl::opt< bool > PropagateAttrs("propagate-attrs", cl::init(true), cl::Hidden, cl::desc("Propagate attributes in index"))
llvm::DenseMapBase::count
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:147
llvm::GlobalValue::DefaultVisibility
@ DefaultVisibility
The GV is visible.
Definition: GlobalValue.h:63
GlobalObject.h
llvm::max
Expected< ExpressionValue > max(const ExpressionValue &Lhs, const ExpressionValue &Rhs)
Definition: FileCheck.cpp:337
llvm::errs
raw_fd_ostream & errs()
This returns a reference to a raw_ostream for standard error.
Definition: raw_ostream.cpp:893
llvm::FunctionImporter
The function importer is automatically importing function from other modules based on the provided su...
Definition: FunctionImport.h:33
llvm::Expected
Tagged union holding either a T or a Error.
Definition: APFloat.h:41
STLExtras.h
llvm::SmallVectorImpl::pop_back_val
LLVM_NODISCARD T pop_back_val()
Definition: SmallVector.h:654
llvm::ModuleSummaryIndex::getOriginalNameBeforePromote
static StringRef getOriginalNameBeforePromote(StringRef Name)
Helper to obtain the unpromoted name for a global value (or the original name if not promoted).
Definition: ModuleSummaryIndex.h:1473
llvm::gatherImportedSummariesForModule
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.
Definition: FunctionImport.cpp:976
shouldImportGlobal
static bool shouldImportGlobal(const ValueInfo &VI, const GVSummaryMapTy &DefinedGVSummaries)
Definition: FunctionImport.cpp:248
llvm::detail::DenseSetImpl< ValueT, DenseMap< ValueT, detail::DenseSetEmpty, DenseMapInfo< ValueT >, detail::DenseSetPair< ValueT > >, DenseMapInfo< ValueT > >::insert
std::pair< iterator, bool > insert(const ValueT &V)
Definition: DenseSet.h:206
llvm::detail::DenseSetImpl< ValueT, DenseMap< ValueT, detail::DenseSetEmpty, DenseMapInfo< ValueT >, detail::DenseSetPair< ValueT > >, DenseMapInfo< ValueT > >::count
size_type count(const_arg_type_t< ValueT > V) const
Return 1 if the specified key is in the set, 0 otherwise.
Definition: DenseSet.h:97
Internalize.h
llvm::EmitImportsFiles
std::error_code EmitImportsFiles(StringRef ModulePath, StringRef OutputFilename, const std::map< std::string, GVSummaryMapTy > &ModuleToSummariesForIndex)
Emit into OutputFilename the files module ModulePath will import from.
Definition: FunctionImport.cpp:1000
llvm::GlobalValue::setDSOLocal
void setDSOLocal(bool Local)
Definition: GlobalValue.h:285
ModuleSummaryIndex.h
llvm::updateIndirectCalls
void updateIndirectCalls(ModuleSummaryIndex &Index)
Update call edges for indirect calls to local functions added from SamplePGO when needed.
Definition: FunctionImport.cpp:838
LLVM_DEBUG
#define LLVM_DEBUG(X)
Definition: Debug.h:101
llvm::MDNode::get
static MDTuple * get(LLVMContext &Context, ArrayRef< Metadata * > MDs)
Definition: Metadata.h:1300
F
#define F(x, y, z)
Definition: MD5.cpp:55
ImportCutoff
static cl::opt< int > ImportCutoff("import-cutoff", cl::init(-1), cl::Hidden, cl::value_desc("N"), cl::desc("Only import first N functions if N>=0 (default -1)"))
llvm::GlobalValueSummary::refs
ArrayRef< ValueInfo > refs() const
Return the list of values referenced by this global value definition.
Definition: ModuleSummaryIndex.h:437
Context
LLVMContext & Context
Definition: NVVMIntrRange.cpp:66
llvm::dbgs
raw_ostream & dbgs()
dbgs() - This returns a reference to a raw_ostream for debugging messages.
Definition: Debug.cpp:163
llvm::FunctionImporter::importFunctions
Expected< bool > importFunctions(Module &M, const ImportMapTy &ImportList)
Import functions in Module M based on the supplied import list.
Definition: FunctionImport.cpp:1212
llvm::detail::DenseSetImpl::end
iterator end()
Definition: DenseSet.h:174
CommandLine.h
ImportHotMultiplier
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"))
llvm::GlobalValueSummary
Function and variable summary information to aid decisions and implementation of importing.
Definition: ModuleSummaryIndex.h:289
GlobalValue.h
INITIALIZE_PASS
INITIALIZE_PASS(FunctionImportLegacyPass, "function-import", "Summary Based Function Import", false, false) namespace llvm
Definition: FunctionImport.cpp:1448
PrintImportFailures
static cl::opt< bool > PrintImportFailures("print-import-failures", cl::init(false), cl::Hidden, cl::desc("Print information for functions rejected for importing"))
llvm::GlobalValue::isDeclaration
bool isDeclaration() const
Return true if the primary definition of this global value is outside of the current translation unit...
Definition: Globals.cpp:264
llvm::ModRefInfo::Ref
@ Ref
The access may reference the value stored in memory.
Constants.h
loadFile
static std::unique_ptr< Module > loadFile(const std::string &FileName, LLVMContext &Context)
Definition: FunctionImport.cpp:145
llvm::GlobalValue::getThreadLocalMode
ThreadLocalMode getThreadLocalMode() const
Definition: GlobalValue.h:257
int
Clang compiles this i1 i64 store i64 i64 store i64 i64 store i64 i64 store i64 align Which gets codegen d xmm0 movaps rbp movaps rbp movaps rbp movaps rbp rbp rbp rbp rbp It would be better to have movq s of instead of the movaps s LLVM produces ret int
Definition: README.txt:536
llvm::StringMap::insert
bool insert(MapEntryTy *KeyValue)
insert - Insert the specified key/value pair into the map.
Definition: StringMap.h:276
llvm::GlobalObject
Definition: GlobalObject.h:27
llvm::SMDiagnostic
Instances of this class encapsulate one diagnostic report, allowing printing to a raw_ostream as a ca...
Definition: SourceMgr.h:281
llvm::PrevailingType
PrevailingType
PrevailingType enum used as a return type of callback passed to computeDeadSymbolsAndUpdateIndirectCa...
Definition: FunctionImport.h:172
llvm::GlobalValueSummary::GlobalVarKind
@ GlobalVarKind
Definition: ModuleSummaryIndex.h:292
llvm::CalleeInfo::HotnessType
HotnessType
Definition: ModuleSummaryIndex.h:57
llvm::dwarf::Index
Index
Definition: Dwarf.h:472
llvm::detail::DenseSetImpl< ValueT, DenseMap< ValueT, detail::DenseSetEmpty, DenseMapInfo< ValueT >, detail::DenseSetPair< ValueT > >, DenseMapInfo< ValueT > >::size
size_type size() const
Definition: DenseSet.h:81
llvm::getLazyIRFileModule
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:52
llvm::IRMover::move
Error move(std::unique_ptr< Module > Src, ArrayRef< GlobalValue * > ValuesToLink, LazyCallback AddLazyFor, bool IsPerformingImport)
Move in the provide values in ValuesToLink from Src.
Definition: IRMover.cpp:1714
llvm::thinLTOFinalizeInModule
void thinLTOFinalizeInModule(Module &TheModule, const GVSummaryMapTy &DefinedGlobals, bool PropagateAttrs)
Based on the information recorded in the summaries during global summary-based analysis:
Definition: FunctionImport.cpp:1051
llvm::report_fatal_error
void report_fatal_error(Error Err, bool gen_crash_diag=true)
Report a serious error, calling any installed error handler.
Definition: Error.cpp:143
llvm::STATISTIC
STATISTIC(NumFunctions, "Total number of functions")
llvm::getHotnessName
const char * getHotnessName(CalleeInfo::HotnessType HT)
Definition: ModuleSummaryIndex.h:105
ImportAllIndex
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.
llvm::ComputeCrossModuleImportForModule
void ComputeCrossModuleImportForModule(StringRef ModulePath, const ModuleSummaryIndex &Index, FunctionImporter::ImportMapTy &ImportList)
Compute all the imports for the given module using the Index.
Definition: FunctionImport.cpp:761
computeImportForReferencedGlobals
static void computeImportForReferencedGlobals(const GlobalValueSummary &Summary, const ModuleSummaryIndex &Index, const GVSummaryMapTy &DefinedGVSummaries, SmallVectorImpl< EdgeInfo > &Worklist, FunctionImporter::ImportMapTy &ImportList, StringMap< FunctionImporter::ExportSetTy > *ExportLists)
Definition: FunctionImport.cpp:271
StringMap.h
llvm::GlobalValue::InternalLinkage
@ InternalLinkage
Rename collisions when linking (static functions).
Definition: GlobalValue.h:55
llvm::Type::isFunctionTy
bool isFunctionTy() const
True if this is an instance of FunctionType.
Definition: Type.h:209
llvm::None
const NoneType None
Definition: None.h:24
llvm::PassSummaryAction::Import
@ Import
Import information from summary.
llvm::GlobalValue::getGUID
static GUID getGUID(StringRef GlobalName)
Return a 64-bit global unique ID constructed from global value name (i.e.
Definition: GlobalValue.h:555
llvm::CallingConv::ID
unsigned ID
LLVM IR allows to use arbitrary numbers as calling convention identifiers.
Definition: CallingConv.h:24
SourceMgr.h
selectCallee
static const GlobalValueSummary * selectCallee(const ModuleSummaryIndex &Index, ArrayRef< std::unique_ptr< GlobalValueSummary >> CalleeSummaryList, unsigned Threshold, StringRef CallerModulePath, FunctionImporter::ImportFailureReason &Reason, GlobalValue::GUID GUID)
Given a list of possible callee implementation for a call site, select one that fits the Threshold.
Definition: FunctionImport.cpp:173
llvm::StringMap
StringMap - This is an unconventional map that is specialized for handling keys that are "strings",...
Definition: StringMap.h:110
llvm::ValueInfo
Struct that holds a reference to a particular GUID in a global value summary.
Definition: ModuleSummaryIndex.h:167
llvm::convertToDeclaration
bool convertToDeclaration(GlobalValue &GV)
Converts value GV to declaration, or replaces with a declaration if it is an alias.
Definition: FunctionImport.cpp:1016
llvm::function_ref
An efficient, type-erasing, non-owning reference to a callable.
Definition: STLFunctionalExtras.h:36
G
const DataFlowGraph & G
Definition: RDFGraph.cpp:200
llvm::DenseSet
Implements a dense probed hash-table based set.
Definition: DenseSet.h:268
llvm::cl::opt
Definition: CommandLine.h:1392
llvm::CalleeInfo::HotnessType::Hot
@ Hot
llvm::IRMover
Definition: IRMover.h:26
ImportCriticalMultiplier
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"))
llvm::GlobalValue
Definition: GlobalValue.h:44
VI
@ VI
Definition: SIInstrInfo.cpp:7824
uint64_t
llvm::GlobalValue::getVisibility
VisibilityTypes getVisibility() const
Definition: GlobalValue.h:234
llvm::GlobalValue::getParent
Module * getParent()
Get the module that this global value is contained inside of...
Definition: GlobalValue.h:620
llvm::computeDeadSymbolsWithConstProp
void computeDeadSymbolsWithConstProp(ModuleSummaryIndex &Index, const DenseSet< GlobalValue::GUID > &GUIDPreservedSymbols, function_ref< PrevailingType(GlobalValue::GUID)> isPrevailing, bool ImportEnabled)
Compute dead symbols and run constant propagation in combined index after that.
Definition: FunctionImport.cpp:963
move
compiles ldr LCPI1_0 ldr ldr mov lsr tst moveq r1 ldr LCPI1_1 and r0 bx lr It would be better to do something like to fold the shift into the conditional move
Definition: README.txt:546
llvm::LLVMContext
This is an important class for using LLVM in a threaded context.
Definition: LLVMContext.h:68
llvm::DenseMap
Definition: DenseMap.h:716
numGlobalVarSummaries
static unsigned numGlobalVarSummaries(const ModuleSummaryIndex &Index, T &Cont)
Definition: FunctionImport.cpp:609
I
#define I(x, y, z)
Definition: MD5.cpp:58
llvm::StringMap::lookup
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:234
Cloning.h
llvm::detail::DenseSetImpl< ValueT, DenseMap< ValueT, detail::DenseSetEmpty, DenseMapInfo< ValueT >, detail::DenseSetPair< ValueT > >, DenseMapInfo< ValueT > >::empty
bool empty() const
Definition: DenseSet.h:80
llvm::cl::init
initializer< Ty > init(const Ty &Val)
Definition: CommandLine.h:432
llvm::GlobalValue::setLinkage
void setLinkage(LinkageTypes LT)
Definition: GlobalValue.h:502
ArrayRef.h
ImportColdMultiplier
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"))
replaceAliasWithAliasee
static Function * replaceAliasWithAliasee(Module *SrcModule, GlobalAlias *GA)
Make alias a clone of its aliasee.
Definition: FunctionImport.cpp:1184
llvm::detail::DenseSetImpl::begin
iterator begin()
Definition: DenseSet.h:173
llvm::getModuleSummaryIndexForFile
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,...
Definition: BitcodeReader.cpp:7608
llvm::DenseMapBase::find
iterator find(const_arg_type_t< KeyT > Val)
Definition: DenseMap.h:152
llvm::Function::Create
static Function * Create(FunctionType *Ty, LinkageTypes Linkage, unsigned AddrSpace, const Twine &N="", Module *M=nullptr)
Definition: Function.h:137
assert
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
llvm::X86AS::FS
@ FS
Definition: X86.h:192
internalizeGVsAfterImport
static void internalizeGVsAfterImport(Module &M)
Definition: FunctionImport.cpp:1200
llvm::Module::getModuleIdentifier
const std::string & getModuleIdentifier() const
Get the module identifier which is, essentially, the name of the module.
Definition: Module.h:228
llvm::GlobalValue::isAvailableExternallyLinkage
static bool isAvailableExternallyLinkage(LinkageTypes Linkage)
Definition: GlobalValue.h:344
ImportInstrFactor
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"))
llvm::Module
A Module instance is used to store all the information related to an LLVM module.
Definition: Module.h:65
IRMover.h
llvm::StringMapImpl::size
unsigned size() const
Definition: StringMap.h:95
PrintImports
static cl::opt< bool > PrintImports("print-imports", cl::init(false), cl::Hidden, cl::desc("Print imported functions"))
llvm::SIInstrFlags::DS
@ DS
Definition: SIDefines.h:60
llvm::internalizeModule
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:79
llvm::SetVector::insert
bool insert(const value_type &X)
Insert a new element into the SetVector.
Definition: SetVector.h:141
llvm::FunctionSummary::instCount
unsigned instCount() const
Get the instruction count recorded for this function.
Definition: ModuleSummaryIndex.h:749
llvm::GlobalValue::canBeOmittedFromSymbolTable
bool canBeOmittedFromSymbolTable() const
True if GV can be left out of the object symbol table.
Definition: Globals.cpp:377
llvm::ArrayRef
ArrayRef - Represent a constant reference to an array (0 or more elements consecutively in memory),...
Definition: APInt.h:32
llvm::any_of
bool any_of(R &&range, UnaryPredicate P)
Provide wrappers to std::any_of which take ranges instead of having to pass begin/end explicitly.
Definition: STLExtras.h:1624
llvm::StringRef
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:58
llvm::errc::not_supported
@ not_supported
llvm_unreachable
#define llvm_unreachable(msg)
Marks that the current location is not supposed to be reachable.
Definition: ErrorHandling.h:143
if
if(llvm_vc STREQUAL "") set(fake_version_inc "$
Definition: CMakeLists.txt:14
llvm::GlobalValue::WeakODRLinkage
@ WeakODRLinkage
Same, but only replaced by something equivalent.
Definition: GlobalValue.h:53
llvm::Value::replaceAllUsesWith
void replaceAllUsesWith(Value *V)
Change all uses of this to point to a new Value.
Definition: Value.cpp:529
getFailureName
static const char * getFailureName(FunctionImporter::ImportFailureReason Reason)
Definition: FunctionImport.cpp:323
S
add sub stmia L5 ldr r0 bl L_printf $stub Instead of a and a wouldn t it be better to do three moves *Return an aggregate type is even return S
Definition: README.txt:210
llvm::CloneFunction
Function * CloneFunction(Function *F, ValueToValueMapTy &VMap, ClonedCodeInfo *CodeInfo=nullptr)
Return a copy of the specified function and add it to that function's module.
Definition: CloneFunction.cpp:281
llvm::GlobalValue::AvailableExternallyLinkage
@ AvailableExternallyLinkage
Available for inspection, not emission.
Definition: GlobalValue.h:49
llvm::GlobalValue::getAliaseeObject
const GlobalObject * getAliaseeObject() const
Definition: Globals.cpp:352
llvm::Value::getName
StringRef getName() const
Return a constant reference to the value's name.
Definition: Value.cpp:305
llvm::ValueMap< const Value *, WeakTrackingVH >
llvm::DenseMapBase::insert
std::pair< iterator, bool > insert(const std::pair< KeyT, ValueT > &KV)
Definition: DenseMap.h:209
llvm::Module::getSourceFileName
const std::string & getSourceFileName() const
Get the module's original source file name.
Definition: Module.h:239
llvm::raw_fd_ostream
A raw_ostream that writes to a file descriptor.
Definition: raw_ostream.h:444
doImportingForModule
static bool doImportingForModule(Module &M)
Definition: FunctionImport.cpp:1355
EnableImportMetadata
static cl::opt< bool > EnableImportMetadata("enable-import-metadata", cl::init(false), cl::Hidden, cl::desc("Enable import metadata like 'thinlto_src_module'"))
llvm::createFunctionImportPass
Pass * createFunctionImportPass()
This pass performs iterative function importing from other modules.
llvm::make_error_code
std::error_code make_error_code(BitcodeError E)
Definition: BitcodeReader.h:271
llvm::find_if
auto find_if(R &&Range, UnaryPredicate P)
Provide wrappers to std::find_if which take ranges instead of having to pass begin/end explicitly.
Definition: STLExtras.h:1644
llvm::AMDGPU::SendMsg::Msg
const CustomOperand< const MCSubtargetInfo & > Msg[]
Definition: AMDGPUAsmUtils.cpp:39
llvm::logAllUnhandledErrors
void logAllUnhandledErrors(Error E, raw_ostream &OS, Twine ErrorBanner={})
Log all errors (if any) in E to OS.
Definition: Error.cpp:61
dumpImportListForModule
static void dumpImportListForModule(const ModuleSummaryIndex &Index, StringRef ModulePath, FunctionImporter::ImportMapTy &ImportList)
Definition: FunctionImport.cpp:744
llvm::CalleeInfo::HotnessType::Critical
@ Critical
llvm::Twine
Twine - A lightweight data structure for efficiently representing the concatenation of temporary valu...
Definition: Twine.h:83
llvm::FunctionSummary
Function summary information to aid decisions and implementation of importing.
Definition: ModuleSummaryIndex.h:512
llvm::ComputeCrossModuleImportForModuleFromIndex
void ComputeCrossModuleImportForModuleFromIndex(StringRef ModulePath, const ModuleSummaryIndex &Index, FunctionImporter::ImportMapTy &ImportList)
Mark all external summaries in Index for import into the given module.
Definition: FunctionImport.cpp:780
ForceImportAll
static cl::opt< bool > ForceImportAll("force-import-all", cl::init(false), cl::Hidden, cl::desc("Import functions with noinline attribute"))
llvm::GraphProgram::Name
Name
Definition: GraphWriter.h:50
llvm::FunctionImporter::ImportFailureReason
ImportFailureReason
The different reasons selectCallee will chose not to import a candidate.
Definition: FunctionImport.h:41
llvm::GlobalValue::isInterposableLinkage
static bool isInterposableLinkage(LinkageTypes Linkage)
Whether the definition of this global may be replaced by something non-equivalent at link time.
Definition: GlobalValue.h:390
llvm::DenseMapBase::end
iterator end()
Definition: DenseMap.h:84
llvm::toString
const char * toString(DWARFSectionKind Kind)
Definition: DWARFUnitIndex.h:67
llvm::PreservedAnalyses::all
static PreservedAnalyses all()
Construct a special preserved set that preserves all passes.
Definition: PassManager.h:158
llvm::X86AS::GS
@ GS
Definition: X86.h:191
llvm::Error
Lightweight error class with error context and mandatory checking.
Definition: Error.h:155
GlobalVariable.h
llvm::Module::getContext
LLVMContext & getContext() const
Get the global data context.
Definition: Module.h:262
llvm::GlobalValue::getGUID
GUID getGUID() const
Return a 64-bit global unique ID constructed from global value name (i.e.
Definition: GlobalValue.h:559
Casting.h
Function.h
llvm::cl::value_desc
Definition: CommandLine.h:414
llvm::PrevailingType::No
@ No
GlobalAlias.h
llvm::GlobalValue::getAddressSpace
unsigned getAddressSpace() const
Definition: Globals.cpp:121
llvm::GlobalValue::ExternalLinkage
@ ExternalLinkage
Externally visible function.
Definition: GlobalValue.h:48
llvm::Expected::takeError
Error takeError()
Take ownership of the stored error.
Definition: Error.h:597
updateValueInfoForIndirectCalls
void updateValueInfoForIndirectCalls(ModuleSummaryIndex &Index, FunctionSummary *FS)
Definition: FunctionImport.cpp:808
llvm::FunctionImportPass::run
PreservedAnalyses run(Module &M, ModuleAnalysisManager &AM)
Definition: FunctionImport.cpp:1439
false
Function Alias Analysis false
Definition: AliasAnalysis.cpp:848
llvm::computeDeadSymbolsAndUpdateIndirectCalls
void computeDeadSymbolsAndUpdateIndirectCalls(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't be reached in the graph from any of the...
Definition: FunctionImport.cpp:847
llvm::Pass
Pass interface - Implemented by all 'passes'.
Definition: Pass.h:91
SummaryFile
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.
llvm::GlobalAlias::getAliaseeObject
const GlobalObject * getAliaseeObject() const
Definition: Globals.cpp:545
SmallVector.h
OutputFilename
static cl::opt< std::string > OutputFilename("o", cl::desc("Output filename"), cl::value_desc("filename"), cl::init("-"))
llvm::ModuleSummaryIndex
Class to hold module path string table and global value map, and encapsulate methods for operating on...
Definition: ModuleSummaryIndex.h:1087
llvm::GlobalValueSummary::modulePath
StringRef modulePath() const
Get the path to the module containing this function.
Definition: ModuleSummaryIndex.h:395
IRReader.h
AutoUpgrade.h
llvm::detail::DenseSetImpl::erase
bool erase(const ValueT &V)
Definition: DenseSet.h:101
llvm::SmallVectorImpl
This class consists of common code factored out of the SmallVector class to reduce code duplication b...
Definition: APFloat.h:42
llvm::lltok::GlobalVar
@ GlobalVar
Definition: LLToken.h:416
llvm::ComputeCrossModuleImport
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.
Definition: FunctionImport.cpp:653
llvm::GlobalValue::getType
PointerType * getType() const
Global values are always pointers.
Definition: GlobalValue.h:276
llvm::AnalysisManager
A container for analyses that lazily runs them and caches their results.
Definition: InstructionSimplify.h:42
llvm::GlobalValue::getValueType
Type * getValueType() const
Definition: GlobalValue.h:278
llvm::Value::takeName
void takeName(Value *V)
Transfer the name from V to this value.
Definition: Value.cpp:378
checkVariableImport
static bool checkVariableImport(const ModuleSummaryIndex &Index, StringMap< FunctionImporter::ImportMapTy > &ImportLists, StringMap< FunctionImporter::ExportSetTy > &ExportLists)
Definition: FunctionImport.cpp:621
llvm::cl::desc
Definition: CommandLine.h:405
BitcodeReader.h
raw_ostream.h
llvm::SetVector
A vector that has set insertion semantics.
Definition: SetVector.h:40
llvm::SmallVectorImpl::reserve
void reserve(size_type N)
Definition: SmallVector.h:644
llvm::GlobalValue::LinkOnceODRLinkage
@ LinkOnceODRLinkage
Same, but only replaced by something equivalent.
Definition: GlobalValue.h:51
llvm::GlobalValue::setVisibility
void setVisibility(VisibilityTypes V)
Definition: GlobalValue.h:240
InitializePasses.h
Debug.h
SetVector.h
llvm::SmallVectorImpl::emplace_back
reference emplace_back(ArgTypes &&... Args)
Definition: SmallVector.h:927
llvm::Intrinsic::ID
unsigned ID
Definition: TargetTransformInfo.h:38
ComputeImportForModule
static void ComputeImportForModule(const GVSummaryMapTy &DefinedGVSummaries, const ModuleSummaryIndex &Index, StringRef ModName, 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...
Definition: FunctionImport.cpp:521