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