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