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 
59 using namespace llvm;
60 
61 #define DEBUG_TYPE "function-import"
62 
63 STATISTIC(NumImportedFunctionsThinLink,
64           "Number of functions thin link decided to import");
65 STATISTIC(NumImportedHotFunctionsThinLink,
66           "Number of hot functions thin link decided to import");
67 STATISTIC(NumImportedCriticalFunctionsThinLink,
68           "Number of critical functions thin link decided to import");
69 STATISTIC(NumImportedGlobalVarsThinLink,
70           "Number of global variables thin link decided to import");
71 STATISTIC(NumImportedFunctions, "Number of functions imported in backend");
72 STATISTIC(NumImportedGlobalVars,
73           "Number of global variables imported in backend");
74 STATISTIC(NumImportedModules, "Number of modules imported from");
75 STATISTIC(NumDeadSymbols, "Number of dead stripped symbols in index");
76 STATISTIC(NumLiveSymbols, "Number of live symbols in index");
77 
78 /// Limit on instruction count of imported functions.
79 static 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 
83 static 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 
87 static 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 
94 static 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 
101 static 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 
105 static 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.
112 static 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 
116 static cl::opt<bool> PrintImports("print-imports", cl::init(false), cl::Hidden,
117                                   cl::desc("Print imported functions"));
118 
119 static cl::opt<bool> PrintImportFailures(
120     "print-import-failures", cl::init(false), cl::Hidden,
121     cl::desc("Print information for functions rejected for importing"));
122 
123 static cl::opt<bool> ComputeDead("compute-dead", cl::init(true), cl::Hidden,
124                                  cl::desc("Compute dead symbols"));
125 
126 static 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.
138 static 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.
144 static 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.
149 static 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]
176 static const GlobalValueSummary *
177 selectCallee(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 
262 namespace {
263 
264 using EdgeInfo = std::tuple<const FunctionSummary *, unsigned /* Threshold */,
265                             GlobalValue::GUID>;
266 
267 } // anonymous namespace
268 
269 static ValueInfo
270 updateValueInfoForIndirectCalls(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 
284 static 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.getGUID());
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 
336 static const char *
337 getFailureName(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.
362 static 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.getGUID());
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             auto CalleeGUID = Edge.first.getGUID();
510             ExportList.insert(CalleeGUID);
511           }
512           for (auto &Ref : ResolvedCalleeSummary->refs()) {
513             auto GUID = Ref.getGUID();
514             ExportList.insert(GUID);
515           }
516         }
517       }
518     }
519 
520     auto GetAdjustedThreshold = [](unsigned Threshold, bool IsHotCallsite) {
521       // Adjust the threshold for next level of imported functions.
522       // The threshold is different for hot callsites because we can then
523       // inline chains of hot calls.
524       if (IsHotCallsite)
525         return Threshold * ImportHotInstrFactor;
526       return Threshold * ImportInstrFactor;
527     };
528 
529     const auto AdjThreshold = GetAdjustedThreshold(Threshold, IsHotCallsite);
530 
531     ImportCount++;
532 
533     // Insert the newly imported function to the worklist.
534     Worklist.emplace_back(ResolvedCalleeSummary, AdjThreshold, VI.getGUID());
535   }
536 }
537 
538 /// Given the list of globals defined in a module, compute the list of imports
539 /// as well as the list of "exports", i.e. the list of symbols referenced from
540 /// another module (that may require promotion).
541 static void ComputeImportForModule(
542     const GVSummaryMapTy &DefinedGVSummaries, const ModuleSummaryIndex &Index,
543     StringRef ModName, FunctionImporter::ImportMapTy &ImportList,
544     StringMap<FunctionImporter::ExportSetTy> *ExportLists = nullptr) {
545   // Worklist contains the list of function imported in this module, for which
546   // we will analyse the callees and may import further down the callgraph.
547   SmallVector<EdgeInfo, 128> Worklist;
548   FunctionImporter::ImportThresholdsTy ImportThresholds;
549 
550   // Populate the worklist with the import for the functions in the current
551   // module
552   for (auto &GVSummary : DefinedGVSummaries) {
553 #ifndef NDEBUG
554     // FIXME: Change the GVSummaryMapTy to hold ValueInfo instead of GUID
555     // so this map look up (and possibly others) can be avoided.
556     auto VI = Index.getValueInfo(GVSummary.first);
557 #endif
558     if (!Index.isGlobalValueLive(GVSummary.second)) {
559       LLVM_DEBUG(dbgs() << "Ignores Dead GUID: " << VI << "\n");
560       continue;
561     }
562     auto *FuncSummary =
563         dyn_cast<FunctionSummary>(GVSummary.second->getBaseObject());
564     if (!FuncSummary)
565       // Skip import for global variables
566       continue;
567     LLVM_DEBUG(dbgs() << "Initialize import for " << VI << "\n");
568     computeImportForFunction(*FuncSummary, Index, ImportInstrLimit,
569                              DefinedGVSummaries, Worklist, ImportList,
570                              ExportLists, ImportThresholds);
571   }
572 
573   // Process the newly imported functions and add callees to the worklist.
574   while (!Worklist.empty()) {
575     auto FuncInfo = Worklist.pop_back_val();
576     auto *Summary = std::get<0>(FuncInfo);
577     auto Threshold = std::get<1>(FuncInfo);
578 
579     computeImportForFunction(*Summary, Index, Threshold, DefinedGVSummaries,
580                              Worklist, ImportList, ExportLists,
581                              ImportThresholds);
582   }
583 
584   // Print stats about functions considered but rejected for importing
585   // when requested.
586   if (PrintImportFailures) {
587     dbgs() << "Missed imports into module " << ModName << "\n";
588     for (auto &I : ImportThresholds) {
589       auto &ProcessedThreshold = std::get<0>(I.second);
590       auto &CalleeSummary = std::get<1>(I.second);
591       auto &FailureInfo = std::get<2>(I.second);
592       if (CalleeSummary)
593         continue; // We are going to import.
594       assert(FailureInfo);
595       FunctionSummary *FS = nullptr;
596       if (!FailureInfo->VI.getSummaryList().empty())
597         FS = dyn_cast<FunctionSummary>(
598             FailureInfo->VI.getSummaryList()[0]->getBaseObject());
599       dbgs() << FailureInfo->VI
600              << ": Reason = " << getFailureName(FailureInfo->Reason)
601              << ", Threshold = " << ProcessedThreshold
602              << ", Size = " << (FS ? (int)FS->instCount() : -1)
603              << ", MaxHotness = " << getHotnessName(FailureInfo->MaxHotness)
604              << ", Attempts = " << FailureInfo->Attempts << "\n";
605     }
606   }
607 }
608 
609 #ifndef NDEBUG
610 static bool isGlobalVarSummary(const ModuleSummaryIndex &Index,
611                                GlobalValue::GUID G) {
612   if (const auto &VI = Index.getValueInfo(G)) {
613     auto SL = VI.getSummaryList();
614     if (!SL.empty())
615       return SL[0]->getSummaryKind() == GlobalValueSummary::GlobalVarKind;
616   }
617   return false;
618 }
619 
620 static GlobalValue::GUID getGUID(GlobalValue::GUID G) { return G; }
621 
622 template <class T>
623 static unsigned numGlobalVarSummaries(const ModuleSummaryIndex &Index,
624                                       T &Cont) {
625   unsigned NumGVS = 0;
626   for (auto &V : Cont)
627     if (isGlobalVarSummary(Index, getGUID(V)))
628       ++NumGVS;
629   return NumGVS;
630 }
631 #endif
632 
633 /// Compute all the import and export for every module using the Index.
634 void llvm::ComputeCrossModuleImport(
635     const ModuleSummaryIndex &Index,
636     const StringMap<GVSummaryMapTy> &ModuleToDefinedGVSummaries,
637     StringMap<FunctionImporter::ImportMapTy> &ImportLists,
638     StringMap<FunctionImporter::ExportSetTy> &ExportLists) {
639   // For each module that has function defined, compute the import/export lists.
640   for (auto &DefinedGVSummaries : ModuleToDefinedGVSummaries) {
641     auto &ImportList = ImportLists[DefinedGVSummaries.first()];
642     LLVM_DEBUG(dbgs() << "Computing import for Module '"
643                       << DefinedGVSummaries.first() << "'\n");
644     ComputeImportForModule(DefinedGVSummaries.second, Index,
645                            DefinedGVSummaries.first(), ImportList,
646                            &ExportLists);
647   }
648 
649   // When computing imports we added all GUIDs referenced by anything
650   // imported from the module to its ExportList. Now we prune each ExportList
651   // of any not defined in that module. This is more efficient than checking
652   // while computing imports because some of the summary lists may be long
653   // due to linkonce (comdat) copies.
654   for (auto &ELI : ExportLists) {
655     const auto &DefinedGVSummaries =
656         ModuleToDefinedGVSummaries.lookup(ELI.first());
657     for (auto EI = ELI.second.begin(); EI != ELI.second.end();) {
658       if (!DefinedGVSummaries.count(*EI))
659         EI = ELI.second.erase(EI);
660       else
661         ++EI;
662     }
663   }
664 
665 #ifndef NDEBUG
666   LLVM_DEBUG(dbgs() << "Import/Export lists for " << ImportLists.size()
667                     << " modules:\n");
668   for (auto &ModuleImports : ImportLists) {
669     auto ModName = ModuleImports.first();
670     auto &Exports = ExportLists[ModName];
671     unsigned NumGVS = numGlobalVarSummaries(Index, Exports);
672     LLVM_DEBUG(dbgs() << "* Module " << ModName << " exports "
673                       << Exports.size() - NumGVS << " functions and " << NumGVS
674                       << " vars. Imports from " << ModuleImports.second.size()
675                       << " modules.\n");
676     for (auto &Src : ModuleImports.second) {
677       auto SrcModName = Src.first();
678       unsigned NumGVSPerMod = numGlobalVarSummaries(Index, Src.second);
679       LLVM_DEBUG(dbgs() << " - " << Src.second.size() - NumGVSPerMod
680                         << " functions imported from " << SrcModName << "\n");
681       LLVM_DEBUG(dbgs() << " - " << NumGVSPerMod
682                         << " global vars imported from " << SrcModName << "\n");
683     }
684   }
685 #endif
686 }
687 
688 #ifndef NDEBUG
689 static void dumpImportListForModule(const ModuleSummaryIndex &Index,
690                                     StringRef ModulePath,
691                                     FunctionImporter::ImportMapTy &ImportList) {
692   LLVM_DEBUG(dbgs() << "* Module " << ModulePath << " imports from "
693                     << ImportList.size() << " modules.\n");
694   for (auto &Src : ImportList) {
695     auto SrcModName = Src.first();
696     unsigned NumGVSPerMod = numGlobalVarSummaries(Index, Src.second);
697     LLVM_DEBUG(dbgs() << " - " << Src.second.size() - NumGVSPerMod
698                       << " functions imported from " << SrcModName << "\n");
699     LLVM_DEBUG(dbgs() << " - " << NumGVSPerMod << " vars imported from "
700                       << SrcModName << "\n");
701   }
702 }
703 #endif
704 
705 /// Compute all the imports for the given module in the Index.
706 void llvm::ComputeCrossModuleImportForModule(
707     StringRef ModulePath, const ModuleSummaryIndex &Index,
708     FunctionImporter::ImportMapTy &ImportList) {
709   // Collect the list of functions this module defines.
710   // GUID -> Summary
711   GVSummaryMapTy FunctionSummaryMap;
712   Index.collectDefinedFunctionsForModule(ModulePath, FunctionSummaryMap);
713 
714   // Compute the import list for this module.
715   LLVM_DEBUG(dbgs() << "Computing import for Module '" << ModulePath << "'\n");
716   ComputeImportForModule(FunctionSummaryMap, Index, ModulePath, ImportList);
717 
718 #ifndef NDEBUG
719   dumpImportListForModule(Index, ModulePath, ImportList);
720 #endif
721 }
722 
723 // Mark all external summaries in Index for import into the given module.
724 // Used for distributed builds using a distributed index.
725 void llvm::ComputeCrossModuleImportForModuleFromIndex(
726     StringRef ModulePath, const ModuleSummaryIndex &Index,
727     FunctionImporter::ImportMapTy &ImportList) {
728   for (auto &GlobalList : Index) {
729     // Ignore entries for undefined references.
730     if (GlobalList.second.SummaryList.empty())
731       continue;
732 
733     auto GUID = GlobalList.first;
734     assert(GlobalList.second.SummaryList.size() == 1 &&
735            "Expected individual combined index to have one summary per GUID");
736     auto &Summary = GlobalList.second.SummaryList[0];
737     // Skip the summaries for the importing module. These are included to
738     // e.g. record required linkage changes.
739     if (Summary->modulePath() == ModulePath)
740       continue;
741     // Add an entry to provoke importing by thinBackend.
742     ImportList[Summary->modulePath()].insert(GUID);
743   }
744 #ifndef NDEBUG
745   dumpImportListForModule(Index, ModulePath, ImportList);
746 #endif
747 }
748 
749 void llvm::computeDeadSymbols(
750     ModuleSummaryIndex &Index,
751     const DenseSet<GlobalValue::GUID> &GUIDPreservedSymbols,
752     function_ref<PrevailingType(GlobalValue::GUID)> isPrevailing) {
753   assert(!Index.withGlobalValueDeadStripping());
754   if (!ComputeDead)
755     return;
756   if (GUIDPreservedSymbols.empty())
757     // Don't do anything when nothing is live, this is friendly with tests.
758     return;
759   unsigned LiveSymbols = 0;
760   SmallVector<ValueInfo, 128> Worklist;
761   Worklist.reserve(GUIDPreservedSymbols.size() * 2);
762   for (auto GUID : GUIDPreservedSymbols) {
763     ValueInfo VI = Index.getValueInfo(GUID);
764     if (!VI)
765       continue;
766     for (auto &S : VI.getSummaryList())
767       S->setLive(true);
768   }
769 
770   // Add values flagged in the index as live roots to the worklist.
771   for (const auto &Entry : Index) {
772     auto VI = Index.getValueInfo(Entry);
773     for (auto &S : Entry.second.SummaryList)
774       if (S->isLive()) {
775         LLVM_DEBUG(dbgs() << "Live root: " << VI << "\n");
776         Worklist.push_back(VI);
777         ++LiveSymbols;
778         break;
779       }
780   }
781 
782   // Make value live and add it to the worklist if it was not live before.
783   auto visit = [&](ValueInfo VI, bool IsAliasee) {
784     // FIXME: If we knew which edges were created for indirect call profiles,
785     // we could skip them here. Any that are live should be reached via
786     // other edges, e.g. reference edges. Otherwise, using a profile collected
787     // on a slightly different binary might provoke preserving, importing
788     // and ultimately promoting calls to functions not linked into this
789     // binary, which increases the binary size unnecessarily. Note that
790     // if this code changes, the importer needs to change so that edges
791     // to functions marked dead are skipped.
792     VI = updateValueInfoForIndirectCalls(Index, VI);
793     if (!VI)
794       return;
795 
796     if (llvm::any_of(VI.getSummaryList(),
797                      [](const std::unique_ptr<llvm::GlobalValueSummary> &S) {
798                        return S->isLive();
799                      }))
800       return;
801 
802     // We only keep live symbols that are known to be non-prevailing if any are
803     // available_externally, linkonceodr, weakodr. Those symbols are discarded
804     // later in the EliminateAvailableExternally pass and setting them to
805     // not-live could break downstreams users of liveness information (PR36483)
806     // or limit optimization opportunities.
807     if (isPrevailing(VI.getGUID()) == PrevailingType::No) {
808       bool KeepAliveLinkage = false;
809       bool Interposable = false;
810       for (auto &S : VI.getSummaryList()) {
811         if (S->linkage() == GlobalValue::AvailableExternallyLinkage ||
812             S->linkage() == GlobalValue::WeakODRLinkage ||
813             S->linkage() == GlobalValue::LinkOnceODRLinkage)
814           KeepAliveLinkage = true;
815         else if (GlobalValue::isInterposableLinkage(S->linkage()))
816           Interposable = true;
817       }
818 
819       if (!IsAliasee) {
820         if (!KeepAliveLinkage)
821           return;
822 
823         if (Interposable)
824           report_fatal_error(
825               "Interposable and available_externally/linkonce_odr/weak_odr "
826               "symbol");
827       }
828     }
829 
830     for (auto &S : VI.getSummaryList())
831       S->setLive(true);
832     ++LiveSymbols;
833     Worklist.push_back(VI);
834   };
835 
836   while (!Worklist.empty()) {
837     auto VI = Worklist.pop_back_val();
838     for (auto &Summary : VI.getSummaryList()) {
839       if (auto *AS = dyn_cast<AliasSummary>(Summary.get())) {
840         // If this is an alias, visit the aliasee VI to ensure that all copies
841         // are marked live and it is added to the worklist for further
842         // processing of its references.
843         visit(AS->getAliaseeVI(), true);
844         continue;
845       }
846 
847       Summary->setLive(true);
848       for (auto Ref : Summary->refs())
849         visit(Ref, false);
850       if (auto *FS = dyn_cast<FunctionSummary>(Summary.get()))
851         for (auto Call : FS->calls())
852           visit(Call.first, false);
853     }
854   }
855   Index.setWithGlobalValueDeadStripping();
856 
857   unsigned DeadSymbols = Index.size() - LiveSymbols;
858   LLVM_DEBUG(dbgs() << LiveSymbols << " symbols Live, and " << DeadSymbols
859                     << " symbols Dead \n");
860   NumDeadSymbols += DeadSymbols;
861   NumLiveSymbols += LiveSymbols;
862 }
863 
864 // Compute dead symbols and propagate constants in combined index.
865 void llvm::computeDeadSymbolsWithConstProp(
866     ModuleSummaryIndex &Index,
867     const DenseSet<GlobalValue::GUID> &GUIDPreservedSymbols,
868     function_ref<PrevailingType(GlobalValue::GUID)> isPrevailing,
869     bool ImportEnabled) {
870   computeDeadSymbols(Index, GUIDPreservedSymbols, isPrevailing);
871   if (ImportEnabled) {
872     Index.propagateAttributes(GUIDPreservedSymbols);
873   } else {
874     // If import is disabled we should drop read/write-only attribute
875     // from all summaries to prevent internalization.
876     for (auto &P : Index)
877       for (auto &S : P.second.SummaryList)
878         if (auto *GVS = dyn_cast<GlobalVarSummary>(S.get())) {
879           GVS->setReadOnly(false);
880           GVS->setWriteOnly(false);
881         }
882   }
883   Index.setWithAttributePropagation();
884 }
885 
886 /// Compute the set of summaries needed for a ThinLTO backend compilation of
887 /// \p ModulePath.
888 void llvm::gatherImportedSummariesForModule(
889     StringRef ModulePath,
890     const StringMap<GVSummaryMapTy> &ModuleToDefinedGVSummaries,
891     const FunctionImporter::ImportMapTy &ImportList,
892     std::map<std::string, GVSummaryMapTy> &ModuleToSummariesForIndex) {
893   // Include all summaries from the importing module.
894   ModuleToSummariesForIndex[ModulePath] =
895       ModuleToDefinedGVSummaries.lookup(ModulePath);
896   // Include summaries for imports.
897   for (auto &ILI : ImportList) {
898     auto &SummariesForIndex = ModuleToSummariesForIndex[ILI.first()];
899     const auto &DefinedGVSummaries =
900         ModuleToDefinedGVSummaries.lookup(ILI.first());
901     for (auto &GI : ILI.second) {
902       const auto &DS = DefinedGVSummaries.find(GI);
903       assert(DS != DefinedGVSummaries.end() &&
904              "Expected a defined summary for imported global value");
905       SummariesForIndex[GI] = DS->second;
906     }
907   }
908 }
909 
910 /// Emit the files \p ModulePath will import from into \p OutputFilename.
911 std::error_code llvm::EmitImportsFiles(
912     StringRef ModulePath, StringRef OutputFilename,
913     const std::map<std::string, GVSummaryMapTy> &ModuleToSummariesForIndex) {
914   std::error_code EC;
915   raw_fd_ostream ImportsOS(OutputFilename, EC, sys::fs::OpenFlags::OF_None);
916   if (EC)
917     return EC;
918   for (auto &ILI : ModuleToSummariesForIndex)
919     // The ModuleToSummariesForIndex map includes an entry for the current
920     // Module (needed for writing out the index files). We don't want to
921     // include it in the imports file, however, so filter it out.
922     if (ILI.first != ModulePath)
923       ImportsOS << ILI.first << "\n";
924   return std::error_code();
925 }
926 
927 bool llvm::convertToDeclaration(GlobalValue &GV) {
928   LLVM_DEBUG(dbgs() << "Converting to a declaration: `" << GV.getName()
929                     << "\n");
930   if (Function *F = dyn_cast<Function>(&GV)) {
931     F->deleteBody();
932     F->clearMetadata();
933     F->setComdat(nullptr);
934   } else if (GlobalVariable *V = dyn_cast<GlobalVariable>(&GV)) {
935     V->setInitializer(nullptr);
936     V->setLinkage(GlobalValue::ExternalLinkage);
937     V->clearMetadata();
938     V->setComdat(nullptr);
939   } else {
940     GlobalValue *NewGV;
941     if (GV.getValueType()->isFunctionTy())
942       NewGV =
943           Function::Create(cast<FunctionType>(GV.getValueType()),
944                            GlobalValue::ExternalLinkage, GV.getAddressSpace(),
945                            "", GV.getParent());
946     else
947       NewGV =
948           new GlobalVariable(*GV.getParent(), GV.getValueType(),
949                              /*isConstant*/ false, GlobalValue::ExternalLinkage,
950                              /*init*/ nullptr, "",
951                              /*insertbefore*/ nullptr, GV.getThreadLocalMode(),
952                              GV.getType()->getAddressSpace());
953     NewGV->takeName(&GV);
954     GV.replaceAllUsesWith(NewGV);
955     return false;
956   }
957   return true;
958 }
959 
960 /// Fixup prevailing symbol linkages in \p TheModule based on summary analysis.
961 void llvm::thinLTOResolvePrevailingInModule(
962     Module &TheModule, const GVSummaryMapTy &DefinedGlobals) {
963   auto updateLinkage = [&](GlobalValue &GV) {
964     // See if the global summary analysis computed a new resolved linkage.
965     const auto &GS = DefinedGlobals.find(GV.getGUID());
966     if (GS == DefinedGlobals.end())
967       return;
968     auto NewLinkage = GS->second->linkage();
969     if (NewLinkage == GV.getLinkage())
970       return;
971     if (GlobalValue::isLocalLinkage(GV.getLinkage()) ||
972         // Don't internalize anything here, because the code below
973         // lacks necessary correctness checks. Leave this job to
974         // LLVM 'internalize' pass.
975         GlobalValue::isLocalLinkage(NewLinkage) ||
976         // In case it was dead and already converted to declaration.
977         GV.isDeclaration())
978       return;
979 
980     // Check for a non-prevailing def that has interposable linkage
981     // (e.g. non-odr weak or linkonce). In that case we can't simply
982     // convert to available_externally, since it would lose the
983     // interposable property and possibly get inlined. Simply drop
984     // the definition in that case.
985     if (GlobalValue::isAvailableExternallyLinkage(NewLinkage) &&
986         GlobalValue::isInterposableLinkage(GV.getLinkage())) {
987       if (!convertToDeclaration(GV))
988         // FIXME: Change this to collect replaced GVs and later erase
989         // them from the parent module once thinLTOResolvePrevailingGUID is
990         // changed to enable this for aliases.
991         llvm_unreachable("Expected GV to be converted");
992     } else {
993       // If all copies of the original symbol had global unnamed addr and
994       // linkonce_odr linkage, it should be an auto hide symbol. In that case
995       // the thin link would have marked it as CanAutoHide. Add hidden visibility
996       // to the symbol to preserve the property.
997       if (NewLinkage == GlobalValue::WeakODRLinkage &&
998           GS->second->canAutoHide()) {
999         assert(GV.hasLinkOnceODRLinkage() && GV.hasGlobalUnnamedAddr());
1000         GV.setVisibility(GlobalValue::HiddenVisibility);
1001       }
1002 
1003       LLVM_DEBUG(dbgs() << "ODR fixing up linkage for `" << GV.getName()
1004                         << "` from " << GV.getLinkage() << " to " << NewLinkage
1005                         << "\n");
1006       GV.setLinkage(NewLinkage);
1007     }
1008     // Remove declarations from comdats, including available_externally
1009     // as this is a declaration for the linker, and will be dropped eventually.
1010     // It is illegal for comdats to contain declarations.
1011     auto *GO = dyn_cast_or_null<GlobalObject>(&GV);
1012     if (GO && GO->isDeclarationForLinker() && GO->hasComdat())
1013       GO->setComdat(nullptr);
1014   };
1015 
1016   // Process functions and global now
1017   for (auto &GV : TheModule)
1018     updateLinkage(GV);
1019   for (auto &GV : TheModule.globals())
1020     updateLinkage(GV);
1021   for (auto &GV : TheModule.aliases())
1022     updateLinkage(GV);
1023 }
1024 
1025 /// Run internalization on \p TheModule based on symmary analysis.
1026 void llvm::thinLTOInternalizeModule(Module &TheModule,
1027                                     const GVSummaryMapTy &DefinedGlobals) {
1028   // Declare a callback for the internalize pass that will ask for every
1029   // candidate GlobalValue if it can be internalized or not.
1030   auto MustPreserveGV = [&](const GlobalValue &GV) -> bool {
1031     // Lookup the linkage recorded in the summaries during global analysis.
1032     auto GS = DefinedGlobals.find(GV.getGUID());
1033     if (GS == DefinedGlobals.end()) {
1034       // Must have been promoted (possibly conservatively). Find original
1035       // name so that we can access the correct summary and see if it can
1036       // be internalized again.
1037       // FIXME: Eventually we should control promotion instead of promoting
1038       // and internalizing again.
1039       StringRef OrigName =
1040           ModuleSummaryIndex::getOriginalNameBeforePromote(GV.getName());
1041       std::string OrigId = GlobalValue::getGlobalIdentifier(
1042           OrigName, GlobalValue::InternalLinkage,
1043           TheModule.getSourceFileName());
1044       GS = DefinedGlobals.find(GlobalValue::getGUID(OrigId));
1045       if (GS == DefinedGlobals.end()) {
1046         // Also check the original non-promoted non-globalized name. In some
1047         // cases a preempted weak value is linked in as a local copy because
1048         // it is referenced by an alias (IRLinker::linkGlobalValueProto).
1049         // In that case, since it was originally not a local value, it was
1050         // recorded in the index using the original name.
1051         // FIXME: This may not be needed once PR27866 is fixed.
1052         GS = DefinedGlobals.find(GlobalValue::getGUID(OrigName));
1053         assert(GS != DefinedGlobals.end());
1054       }
1055     }
1056     return !GlobalValue::isLocalLinkage(GS->second->linkage());
1057   };
1058 
1059   // FIXME: See if we can just internalize directly here via linkage changes
1060   // based on the index, rather than invoking internalizeModule.
1061   internalizeModule(TheModule, MustPreserveGV);
1062 }
1063 
1064 /// Make alias a clone of its aliasee.
1065 static Function *replaceAliasWithAliasee(Module *SrcModule, GlobalAlias *GA) {
1066   Function *Fn = cast<Function>(GA->getBaseObject());
1067 
1068   ValueToValueMapTy VMap;
1069   Function *NewFn = CloneFunction(Fn, VMap);
1070   // Clone should use the original alias's linkage, visibility and name, and we
1071   // ensure all uses of alias instead use the new clone (casted if necessary).
1072   NewFn->setLinkage(GA->getLinkage());
1073   NewFn->setVisibility(GA->getVisibility());
1074   GA->replaceAllUsesWith(ConstantExpr::getBitCast(NewFn, GA->getType()));
1075   NewFn->takeName(GA);
1076   return NewFn;
1077 }
1078 
1079 // Internalize values that we marked with specific attribute
1080 // in processGlobalForThinLTO.
1081 static void internalizeGVsAfterImport(Module &M) {
1082   for (auto &GV : M.globals())
1083     // Skip GVs which have been converted to declarations
1084     // by dropDeadSymbols.
1085     if (!GV.isDeclaration() && GV.hasAttribute("thinlto-internalize")) {
1086       GV.setLinkage(GlobalValue::InternalLinkage);
1087       GV.setVisibility(GlobalValue::DefaultVisibility);
1088     }
1089 }
1090 
1091 // Automatically import functions in Module \p DestModule based on the summaries
1092 // index.
1093 Expected<bool> FunctionImporter::importFunctions(
1094     Module &DestModule, const FunctionImporter::ImportMapTy &ImportList) {
1095   LLVM_DEBUG(dbgs() << "Starting import for Module "
1096                     << DestModule.getModuleIdentifier() << "\n");
1097   unsigned ImportedCount = 0, ImportedGVCount = 0;
1098 
1099   IRMover Mover(DestModule);
1100   // Do the actual import of functions now, one Module at a time
1101   std::set<StringRef> ModuleNameOrderedList;
1102   for (auto &FunctionsToImportPerModule : ImportList) {
1103     ModuleNameOrderedList.insert(FunctionsToImportPerModule.first());
1104   }
1105   for (auto &Name : ModuleNameOrderedList) {
1106     // Get the module for the import
1107     const auto &FunctionsToImportPerModule = ImportList.find(Name);
1108     assert(FunctionsToImportPerModule != ImportList.end());
1109     Expected<std::unique_ptr<Module>> SrcModuleOrErr = ModuleLoader(Name);
1110     if (!SrcModuleOrErr)
1111       return SrcModuleOrErr.takeError();
1112     std::unique_ptr<Module> SrcModule = std::move(*SrcModuleOrErr);
1113     assert(&DestModule.getContext() == &SrcModule->getContext() &&
1114            "Context mismatch");
1115 
1116     // If modules were created with lazy metadata loading, materialize it
1117     // now, before linking it (otherwise this will be a noop).
1118     if (Error Err = SrcModule->materializeMetadata())
1119       return std::move(Err);
1120 
1121     auto &ImportGUIDs = FunctionsToImportPerModule->second;
1122     // Find the globals to import
1123     SetVector<GlobalValue *> GlobalsToImport;
1124     for (Function &F : *SrcModule) {
1125       if (!F.hasName())
1126         continue;
1127       auto GUID = F.getGUID();
1128       auto Import = ImportGUIDs.count(GUID);
1129       LLVM_DEBUG(dbgs() << (Import ? "Is" : "Not") << " importing function "
1130                         << GUID << " " << F.getName() << " from "
1131                         << SrcModule->getSourceFileName() << "\n");
1132       if (Import) {
1133         if (Error Err = F.materialize())
1134           return std::move(Err);
1135         if (EnableImportMetadata) {
1136           // Add 'thinlto_src_module' metadata for statistics and debugging.
1137           F.setMetadata(
1138               "thinlto_src_module",
1139               MDNode::get(DestModule.getContext(),
1140                           {MDString::get(DestModule.getContext(),
1141                                          SrcModule->getSourceFileName())}));
1142         }
1143         GlobalsToImport.insert(&F);
1144       }
1145     }
1146     for (GlobalVariable &GV : SrcModule->globals()) {
1147       if (!GV.hasName())
1148         continue;
1149       auto GUID = GV.getGUID();
1150       auto Import = ImportGUIDs.count(GUID);
1151       LLVM_DEBUG(dbgs() << (Import ? "Is" : "Not") << " importing global "
1152                         << GUID << " " << GV.getName() << " from "
1153                         << SrcModule->getSourceFileName() << "\n");
1154       if (Import) {
1155         if (Error Err = GV.materialize())
1156           return std::move(Err);
1157         ImportedGVCount += GlobalsToImport.insert(&GV);
1158       }
1159     }
1160     for (GlobalAlias &GA : SrcModule->aliases()) {
1161       if (!GA.hasName())
1162         continue;
1163       auto GUID = GA.getGUID();
1164       auto Import = ImportGUIDs.count(GUID);
1165       LLVM_DEBUG(dbgs() << (Import ? "Is" : "Not") << " importing alias "
1166                         << GUID << " " << GA.getName() << " from "
1167                         << SrcModule->getSourceFileName() << "\n");
1168       if (Import) {
1169         if (Error Err = GA.materialize())
1170           return std::move(Err);
1171         // Import alias as a copy of its aliasee.
1172         GlobalObject *Base = GA.getBaseObject();
1173         if (Error Err = Base->materialize())
1174           return std::move(Err);
1175         auto *Fn = replaceAliasWithAliasee(SrcModule.get(), &GA);
1176         LLVM_DEBUG(dbgs() << "Is importing aliasee fn " << Base->getGUID()
1177                           << " " << Base->getName() << " from "
1178                           << SrcModule->getSourceFileName() << "\n");
1179         if (EnableImportMetadata) {
1180           // Add 'thinlto_src_module' metadata for statistics and debugging.
1181           Fn->setMetadata(
1182               "thinlto_src_module",
1183               MDNode::get(DestModule.getContext(),
1184                           {MDString::get(DestModule.getContext(),
1185                                          SrcModule->getSourceFileName())}));
1186         }
1187         GlobalsToImport.insert(Fn);
1188       }
1189     }
1190 
1191     // Upgrade debug info after we're done materializing all the globals and we
1192     // have loaded all the required metadata!
1193     UpgradeDebugInfo(*SrcModule);
1194 
1195     // Link in the specified functions.
1196     if (renameModuleForThinLTO(*SrcModule, Index, &GlobalsToImport))
1197       return true;
1198 
1199     if (PrintImports) {
1200       for (const auto *GV : GlobalsToImport)
1201         dbgs() << DestModule.getSourceFileName() << ": Import " << GV->getName()
1202                << " from " << SrcModule->getSourceFileName() << "\n";
1203     }
1204 
1205     if (Mover.move(std::move(SrcModule), GlobalsToImport.getArrayRef(),
1206                    [](GlobalValue &, IRMover::ValueAdder) {},
1207                    /*IsPerformingImport=*/true))
1208       report_fatal_error("Function Import: link error");
1209 
1210     ImportedCount += GlobalsToImport.size();
1211     NumImportedModules++;
1212   }
1213 
1214   internalizeGVsAfterImport(DestModule);
1215 
1216   NumImportedFunctions += (ImportedCount - ImportedGVCount);
1217   NumImportedGlobalVars += ImportedGVCount;
1218 
1219   LLVM_DEBUG(dbgs() << "Imported " << ImportedCount - ImportedGVCount
1220                     << " functions for Module "
1221                     << DestModule.getModuleIdentifier() << "\n");
1222   LLVM_DEBUG(dbgs() << "Imported " << ImportedGVCount
1223                     << " global variables for Module "
1224                     << DestModule.getModuleIdentifier() << "\n");
1225   return ImportedCount;
1226 }
1227 
1228 static bool doImportingForModule(Module &M) {
1229   if (SummaryFile.empty())
1230     report_fatal_error("error: -function-import requires -summary-file\n");
1231   Expected<std::unique_ptr<ModuleSummaryIndex>> IndexPtrOrErr =
1232       getModuleSummaryIndexForFile(SummaryFile);
1233   if (!IndexPtrOrErr) {
1234     logAllUnhandledErrors(IndexPtrOrErr.takeError(), errs(),
1235                           "Error loading file '" + SummaryFile + "': ");
1236     return false;
1237   }
1238   std::unique_ptr<ModuleSummaryIndex> Index = std::move(*IndexPtrOrErr);
1239 
1240   // First step is collecting the import list.
1241   FunctionImporter::ImportMapTy ImportList;
1242   // If requested, simply import all functions in the index. This is used
1243   // when testing distributed backend handling via the opt tool, when
1244   // we have distributed indexes containing exactly the summaries to import.
1245   if (ImportAllIndex)
1246     ComputeCrossModuleImportForModuleFromIndex(M.getModuleIdentifier(), *Index,
1247                                                ImportList);
1248   else
1249     ComputeCrossModuleImportForModule(M.getModuleIdentifier(), *Index,
1250                                       ImportList);
1251 
1252   // Conservatively mark all internal values as promoted. This interface is
1253   // only used when doing importing via the function importing pass. The pass
1254   // is only enabled when testing importing via the 'opt' tool, which does
1255   // not do the ThinLink that would normally determine what values to promote.
1256   for (auto &I : *Index) {
1257     for (auto &S : I.second.SummaryList) {
1258       if (GlobalValue::isLocalLinkage(S->linkage()))
1259         S->setLinkage(GlobalValue::ExternalLinkage);
1260     }
1261   }
1262 
1263   // Next we need to promote to global scope and rename any local values that
1264   // are potentially exported to other modules.
1265   if (renameModuleForThinLTO(M, *Index, nullptr)) {
1266     errs() << "Error renaming module\n";
1267     return false;
1268   }
1269 
1270   // Perform the import now.
1271   auto ModuleLoader = [&M](StringRef Identifier) {
1272     return loadFile(Identifier, M.getContext());
1273   };
1274   FunctionImporter Importer(*Index, ModuleLoader);
1275   Expected<bool> Result = Importer.importFunctions(M, ImportList);
1276 
1277   // FIXME: Probably need to propagate Errors through the pass manager.
1278   if (!Result) {
1279     logAllUnhandledErrors(Result.takeError(), errs(),
1280                           "Error importing module: ");
1281     return false;
1282   }
1283 
1284   return *Result;
1285 }
1286 
1287 namespace {
1288 
1289 /// Pass that performs cross-module function import provided a summary file.
1290 class FunctionImportLegacyPass : public ModulePass {
1291 public:
1292   /// Pass identification, replacement for typeid
1293   static char ID;
1294 
1295   explicit FunctionImportLegacyPass() : ModulePass(ID) {}
1296 
1297   /// Specify pass name for debug output
1298   StringRef getPassName() const override { return "Function Importing"; }
1299 
1300   bool runOnModule(Module &M) override {
1301     if (skipModule(M))
1302       return false;
1303 
1304     return doImportingForModule(M);
1305   }
1306 };
1307 
1308 } // end anonymous namespace
1309 
1310 PreservedAnalyses FunctionImportPass::run(Module &M,
1311                                           ModuleAnalysisManager &AM) {
1312   if (!doImportingForModule(M))
1313     return PreservedAnalyses::all();
1314 
1315   return PreservedAnalyses::none();
1316 }
1317 
1318 char FunctionImportLegacyPass::ID = 0;
1319 INITIALIZE_PASS(FunctionImportLegacyPass, "function-import",
1320                 "Summary Based Function Import", false, false)
1321 
1322 namespace llvm {
1323 
1324 Pass *createFunctionImportPass() {
1325   return new FunctionImportLegacyPass();
1326 }
1327 
1328 } // end namespace llvm
1329