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