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