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