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