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