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