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