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       Summary->setLive(true);
888       if (auto *AS = dyn_cast<AliasSummary>(Summary.get())) {
889         // If this is an alias, visit the aliasee VI to ensure that all copies
890         // are marked live and it is added to the worklist for further
891         // processing of its references.
892         visit(AS->getAliaseeVI(), true);
893         continue;
894       }
895       for (auto Ref : Summary->refs())
896         visit(Ref, false);
897       if (auto *FS = dyn_cast<FunctionSummary>(Summary.get()))
898         for (auto Call : FS->calls())
899           visit(Call.first, false);
900     }
901   }
902   Index.setWithGlobalValueDeadStripping();
903 
904   unsigned DeadSymbols = Index.size() - LiveSymbols;
905   LLVM_DEBUG(dbgs() << LiveSymbols << " symbols Live, and " << DeadSymbols
906                     << " symbols Dead \n");
907   NumDeadSymbols += DeadSymbols;
908   NumLiveSymbols += LiveSymbols;
909 }
910 
911 // Compute dead symbols and propagate constants in combined index.
912 void llvm::computeDeadSymbolsWithConstProp(
913     ModuleSummaryIndex &Index,
914     const DenseSet<GlobalValue::GUID> &GUIDPreservedSymbols,
915     function_ref<PrevailingType(GlobalValue::GUID)> isPrevailing,
916     bool ImportEnabled) {
917   computeDeadSymbols(Index, GUIDPreservedSymbols, isPrevailing);
918   if (ImportEnabled)
919     Index.propagateAttributes(GUIDPreservedSymbols);
920 }
921 
922 /// Compute the set of summaries needed for a ThinLTO backend compilation of
923 /// \p ModulePath.
924 void llvm::gatherImportedSummariesForModule(
925     StringRef ModulePath,
926     const StringMap<GVSummaryMapTy> &ModuleToDefinedGVSummaries,
927     const FunctionImporter::ImportMapTy &ImportList,
928     std::map<std::string, GVSummaryMapTy> &ModuleToSummariesForIndex) {
929   // Include all summaries from the importing module.
930   ModuleToSummariesForIndex[std::string(ModulePath)] =
931       ModuleToDefinedGVSummaries.lookup(ModulePath);
932   // Include summaries for imports.
933   for (auto &ILI : ImportList) {
934     auto &SummariesForIndex =
935         ModuleToSummariesForIndex[std::string(ILI.first())];
936     const auto &DefinedGVSummaries =
937         ModuleToDefinedGVSummaries.lookup(ILI.first());
938     for (auto &GI : ILI.second) {
939       const auto &DS = DefinedGVSummaries.find(GI);
940       assert(DS != DefinedGVSummaries.end() &&
941              "Expected a defined summary for imported global value");
942       SummariesForIndex[GI] = DS->second;
943     }
944   }
945 }
946 
947 /// Emit the files \p ModulePath will import from into \p OutputFilename.
948 std::error_code llvm::EmitImportsFiles(
949     StringRef ModulePath, StringRef OutputFilename,
950     const std::map<std::string, GVSummaryMapTy> &ModuleToSummariesForIndex) {
951   std::error_code EC;
952   raw_fd_ostream ImportsOS(OutputFilename, EC, sys::fs::OpenFlags::OF_None);
953   if (EC)
954     return EC;
955   for (auto &ILI : ModuleToSummariesForIndex)
956     // The ModuleToSummariesForIndex map includes an entry for the current
957     // Module (needed for writing out the index files). We don't want to
958     // include it in the imports file, however, so filter it out.
959     if (ILI.first != ModulePath)
960       ImportsOS << ILI.first << "\n";
961   return std::error_code();
962 }
963 
964 bool llvm::convertToDeclaration(GlobalValue &GV) {
965   LLVM_DEBUG(dbgs() << "Converting to a declaration: `" << GV.getName()
966                     << "\n");
967   if (Function *F = dyn_cast<Function>(&GV)) {
968     F->deleteBody();
969     F->clearMetadata();
970     F->setComdat(nullptr);
971   } else if (GlobalVariable *V = dyn_cast<GlobalVariable>(&GV)) {
972     V->setInitializer(nullptr);
973     V->setLinkage(GlobalValue::ExternalLinkage);
974     V->clearMetadata();
975     V->setComdat(nullptr);
976   } else {
977     GlobalValue *NewGV;
978     if (GV.getValueType()->isFunctionTy())
979       NewGV =
980           Function::Create(cast<FunctionType>(GV.getValueType()),
981                            GlobalValue::ExternalLinkage, GV.getAddressSpace(),
982                            "", GV.getParent());
983     else
984       NewGV =
985           new GlobalVariable(*GV.getParent(), GV.getValueType(),
986                              /*isConstant*/ false, GlobalValue::ExternalLinkage,
987                              /*init*/ nullptr, "",
988                              /*insertbefore*/ nullptr, GV.getThreadLocalMode(),
989                              GV.getType()->getAddressSpace());
990     NewGV->takeName(&GV);
991     GV.replaceAllUsesWith(NewGV);
992     return false;
993   }
994   if (!GV.isImplicitDSOLocal())
995     GV.setDSOLocal(false);
996   return true;
997 }
998 
999 /// Fixup prevailing symbol linkages in \p TheModule based on summary analysis.
1000 void llvm::thinLTOResolvePrevailingInModule(
1001     Module &TheModule, const GVSummaryMapTy &DefinedGlobals) {
1002   auto updateLinkage = [&](GlobalValue &GV) {
1003     // See if the global summary analysis computed a new resolved linkage.
1004     const auto &GS = DefinedGlobals.find(GV.getGUID());
1005     if (GS == DefinedGlobals.end())
1006       return;
1007     auto NewLinkage = GS->second->linkage();
1008     if (NewLinkage == GV.getLinkage())
1009       return;
1010     if (GlobalValue::isLocalLinkage(GV.getLinkage()) ||
1011         // Don't internalize anything here, because the code below
1012         // lacks necessary correctness checks. Leave this job to
1013         // LLVM 'internalize' pass.
1014         GlobalValue::isLocalLinkage(NewLinkage) ||
1015         // In case it was dead and already converted to declaration.
1016         GV.isDeclaration())
1017       return;
1018 
1019     // Check for a non-prevailing def that has interposable linkage
1020     // (e.g. non-odr weak or linkonce). In that case we can't simply
1021     // convert to available_externally, since it would lose the
1022     // interposable property and possibly get inlined. Simply drop
1023     // the definition in that case.
1024     if (GlobalValue::isAvailableExternallyLinkage(NewLinkage) &&
1025         GlobalValue::isInterposableLinkage(GV.getLinkage())) {
1026       if (!convertToDeclaration(GV))
1027         // FIXME: Change this to collect replaced GVs and later erase
1028         // them from the parent module once thinLTOResolvePrevailingGUID is
1029         // changed to enable this for aliases.
1030         llvm_unreachable("Expected GV to be converted");
1031     } else {
1032       // If all copies of the original symbol had global unnamed addr and
1033       // linkonce_odr linkage, it should be an auto hide symbol. In that case
1034       // the thin link would have marked it as CanAutoHide. Add hidden visibility
1035       // to the symbol to preserve the property.
1036       if (NewLinkage == GlobalValue::WeakODRLinkage &&
1037           GS->second->canAutoHide()) {
1038         assert(GV.hasLinkOnceODRLinkage() && GV.hasGlobalUnnamedAddr());
1039         GV.setVisibility(GlobalValue::HiddenVisibility);
1040       }
1041 
1042       LLVM_DEBUG(dbgs() << "ODR fixing up linkage for `" << GV.getName()
1043                         << "` from " << GV.getLinkage() << " to " << NewLinkage
1044                         << "\n");
1045       GV.setLinkage(NewLinkage);
1046     }
1047     // Remove declarations from comdats, including available_externally
1048     // as this is a declaration for the linker, and will be dropped eventually.
1049     // It is illegal for comdats to contain declarations.
1050     auto *GO = dyn_cast_or_null<GlobalObject>(&GV);
1051     if (GO && GO->isDeclarationForLinker() && GO->hasComdat())
1052       GO->setComdat(nullptr);
1053   };
1054 
1055   // Process functions and global now
1056   for (auto &GV : TheModule)
1057     updateLinkage(GV);
1058   for (auto &GV : TheModule.globals())
1059     updateLinkage(GV);
1060   for (auto &GV : TheModule.aliases())
1061     updateLinkage(GV);
1062 }
1063 
1064 /// Run internalization on \p TheModule based on symmary analysis.
1065 void llvm::thinLTOInternalizeModule(Module &TheModule,
1066                                     const GVSummaryMapTy &DefinedGlobals) {
1067   // Declare a callback for the internalize pass that will ask for every
1068   // candidate GlobalValue if it can be internalized or not.
1069   auto MustPreserveGV = [&](const GlobalValue &GV) -> bool {
1070     // Lookup the linkage recorded in the summaries during global analysis.
1071     auto GS = DefinedGlobals.find(GV.getGUID());
1072     if (GS == DefinedGlobals.end()) {
1073       // Must have been promoted (possibly conservatively). Find original
1074       // name so that we can access the correct summary and see if it can
1075       // be internalized again.
1076       // FIXME: Eventually we should control promotion instead of promoting
1077       // and internalizing again.
1078       StringRef OrigName =
1079           ModuleSummaryIndex::getOriginalNameBeforePromote(GV.getName());
1080       std::string OrigId = GlobalValue::getGlobalIdentifier(
1081           OrigName, GlobalValue::InternalLinkage,
1082           TheModule.getSourceFileName());
1083       GS = DefinedGlobals.find(GlobalValue::getGUID(OrigId));
1084       if (GS == DefinedGlobals.end()) {
1085         // Also check the original non-promoted non-globalized name. In some
1086         // cases a preempted weak value is linked in as a local copy because
1087         // it is referenced by an alias (IRLinker::linkGlobalValueProto).
1088         // In that case, since it was originally not a local value, it was
1089         // recorded in the index using the original name.
1090         // FIXME: This may not be needed once PR27866 is fixed.
1091         GS = DefinedGlobals.find(GlobalValue::getGUID(OrigName));
1092         assert(GS != DefinedGlobals.end());
1093       }
1094     }
1095     return !GlobalValue::isLocalLinkage(GS->second->linkage());
1096   };
1097 
1098   // FIXME: See if we can just internalize directly here via linkage changes
1099   // based on the index, rather than invoking internalizeModule.
1100   internalizeModule(TheModule, MustPreserveGV);
1101 }
1102 
1103 /// Make alias a clone of its aliasee.
1104 static Function *replaceAliasWithAliasee(Module *SrcModule, GlobalAlias *GA) {
1105   Function *Fn = cast<Function>(GA->getBaseObject());
1106 
1107   ValueToValueMapTy VMap;
1108   Function *NewFn = CloneFunction(Fn, VMap);
1109   // Clone should use the original alias's linkage, visibility and name, and we
1110   // ensure all uses of alias instead use the new clone (casted if necessary).
1111   NewFn->setLinkage(GA->getLinkage());
1112   NewFn->setVisibility(GA->getVisibility());
1113   GA->replaceAllUsesWith(ConstantExpr::getBitCast(NewFn, GA->getType()));
1114   NewFn->takeName(GA);
1115   return NewFn;
1116 }
1117 
1118 // Internalize values that we marked with specific attribute
1119 // in processGlobalForThinLTO.
1120 static void internalizeGVsAfterImport(Module &M) {
1121   for (auto &GV : M.globals())
1122     // Skip GVs which have been converted to declarations
1123     // by dropDeadSymbols.
1124     if (!GV.isDeclaration() && GV.hasAttribute("thinlto-internalize")) {
1125       GV.setLinkage(GlobalValue::InternalLinkage);
1126       GV.setVisibility(GlobalValue::DefaultVisibility);
1127     }
1128 }
1129 
1130 // Automatically import functions in Module \p DestModule based on the summaries
1131 // index.
1132 Expected<bool> FunctionImporter::importFunctions(
1133     Module &DestModule, const FunctionImporter::ImportMapTy &ImportList) {
1134   LLVM_DEBUG(dbgs() << "Starting import for Module "
1135                     << DestModule.getModuleIdentifier() << "\n");
1136   unsigned ImportedCount = 0, ImportedGVCount = 0;
1137 
1138   IRMover Mover(DestModule);
1139   // Do the actual import of functions now, one Module at a time
1140   std::set<StringRef> ModuleNameOrderedList;
1141   for (auto &FunctionsToImportPerModule : ImportList) {
1142     ModuleNameOrderedList.insert(FunctionsToImportPerModule.first());
1143   }
1144   for (auto &Name : ModuleNameOrderedList) {
1145     // Get the module for the import
1146     const auto &FunctionsToImportPerModule = ImportList.find(Name);
1147     assert(FunctionsToImportPerModule != ImportList.end());
1148     Expected<std::unique_ptr<Module>> SrcModuleOrErr = ModuleLoader(Name);
1149     if (!SrcModuleOrErr)
1150       return SrcModuleOrErr.takeError();
1151     std::unique_ptr<Module> SrcModule = std::move(*SrcModuleOrErr);
1152     assert(&DestModule.getContext() == &SrcModule->getContext() &&
1153            "Context mismatch");
1154 
1155     // If modules were created with lazy metadata loading, materialize it
1156     // now, before linking it (otherwise this will be a noop).
1157     if (Error Err = SrcModule->materializeMetadata())
1158       return std::move(Err);
1159 
1160     auto &ImportGUIDs = FunctionsToImportPerModule->second;
1161     // Find the globals to import
1162     SetVector<GlobalValue *> GlobalsToImport;
1163     for (Function &F : *SrcModule) {
1164       if (!F.hasName())
1165         continue;
1166       auto GUID = F.getGUID();
1167       auto Import = ImportGUIDs.count(GUID);
1168       LLVM_DEBUG(dbgs() << (Import ? "Is" : "Not") << " importing function "
1169                         << GUID << " " << F.getName() << " from "
1170                         << SrcModule->getSourceFileName() << "\n");
1171       if (Import) {
1172         if (Error Err = F.materialize())
1173           return std::move(Err);
1174         if (EnableImportMetadata) {
1175           // Add 'thinlto_src_module' metadata for statistics and debugging.
1176           F.setMetadata(
1177               "thinlto_src_module",
1178               MDNode::get(DestModule.getContext(),
1179                           {MDString::get(DestModule.getContext(),
1180                                          SrcModule->getSourceFileName())}));
1181         }
1182         GlobalsToImport.insert(&F);
1183       }
1184     }
1185     for (GlobalVariable &GV : SrcModule->globals()) {
1186       if (!GV.hasName())
1187         continue;
1188       auto GUID = GV.getGUID();
1189       auto Import = ImportGUIDs.count(GUID);
1190       LLVM_DEBUG(dbgs() << (Import ? "Is" : "Not") << " importing global "
1191                         << GUID << " " << GV.getName() << " from "
1192                         << SrcModule->getSourceFileName() << "\n");
1193       if (Import) {
1194         if (Error Err = GV.materialize())
1195           return std::move(Err);
1196         ImportedGVCount += GlobalsToImport.insert(&GV);
1197       }
1198     }
1199     for (GlobalAlias &GA : SrcModule->aliases()) {
1200       if (!GA.hasName())
1201         continue;
1202       auto GUID = GA.getGUID();
1203       auto Import = ImportGUIDs.count(GUID);
1204       LLVM_DEBUG(dbgs() << (Import ? "Is" : "Not") << " importing alias "
1205                         << GUID << " " << GA.getName() << " from "
1206                         << SrcModule->getSourceFileName() << "\n");
1207       if (Import) {
1208         if (Error Err = GA.materialize())
1209           return std::move(Err);
1210         // Import alias as a copy of its aliasee.
1211         GlobalObject *Base = GA.getBaseObject();
1212         if (Error Err = Base->materialize())
1213           return std::move(Err);
1214         auto *Fn = replaceAliasWithAliasee(SrcModule.get(), &GA);
1215         LLVM_DEBUG(dbgs() << "Is importing aliasee fn " << Base->getGUID()
1216                           << " " << Base->getName() << " from "
1217                           << SrcModule->getSourceFileName() << "\n");
1218         if (EnableImportMetadata) {
1219           // Add 'thinlto_src_module' metadata for statistics and debugging.
1220           Fn->setMetadata(
1221               "thinlto_src_module",
1222               MDNode::get(DestModule.getContext(),
1223                           {MDString::get(DestModule.getContext(),
1224                                          SrcModule->getSourceFileName())}));
1225         }
1226         GlobalsToImport.insert(Fn);
1227       }
1228     }
1229 
1230     // Upgrade debug info after we're done materializing all the globals and we
1231     // have loaded all the required metadata!
1232     UpgradeDebugInfo(*SrcModule);
1233 
1234     // Set the partial sample profile ratio in the profile summary module flag
1235     // of the imported source module, if applicable, so that the profile summary
1236     // module flag will match with that of the destination module when it's
1237     // imported.
1238     SrcModule->setPartialSampleProfileRatio(Index);
1239 
1240     // Link in the specified functions.
1241     if (renameModuleForThinLTO(*SrcModule, Index, ClearDSOLocalOnDeclarations,
1242                                &GlobalsToImport))
1243       return true;
1244 
1245     if (PrintImports) {
1246       for (const auto *GV : GlobalsToImport)
1247         dbgs() << DestModule.getSourceFileName() << ": Import " << GV->getName()
1248                << " from " << SrcModule->getSourceFileName() << "\n";
1249     }
1250 
1251     if (Error Err = Mover.move(
1252             std::move(SrcModule), GlobalsToImport.getArrayRef(),
1253             [](GlobalValue &, IRMover::ValueAdder) {},
1254             /*IsPerformingImport=*/true))
1255       report_fatal_error("Function Import: link error: " +
1256                          toString(std::move(Err)));
1257 
1258     ImportedCount += GlobalsToImport.size();
1259     NumImportedModules++;
1260   }
1261 
1262   internalizeGVsAfterImport(DestModule);
1263 
1264   NumImportedFunctions += (ImportedCount - ImportedGVCount);
1265   NumImportedGlobalVars += ImportedGVCount;
1266 
1267   LLVM_DEBUG(dbgs() << "Imported " << ImportedCount - ImportedGVCount
1268                     << " functions for Module "
1269                     << DestModule.getModuleIdentifier() << "\n");
1270   LLVM_DEBUG(dbgs() << "Imported " << ImportedGVCount
1271                     << " global variables for Module "
1272                     << DestModule.getModuleIdentifier() << "\n");
1273   return ImportedCount;
1274 }
1275 
1276 static bool doImportingForModule(Module &M) {
1277   if (SummaryFile.empty())
1278     report_fatal_error("error: -function-import requires -summary-file\n");
1279   Expected<std::unique_ptr<ModuleSummaryIndex>> IndexPtrOrErr =
1280       getModuleSummaryIndexForFile(SummaryFile);
1281   if (!IndexPtrOrErr) {
1282     logAllUnhandledErrors(IndexPtrOrErr.takeError(), errs(),
1283                           "Error loading file '" + SummaryFile + "': ");
1284     return false;
1285   }
1286   std::unique_ptr<ModuleSummaryIndex> Index = std::move(*IndexPtrOrErr);
1287 
1288   // First step is collecting the import list.
1289   FunctionImporter::ImportMapTy ImportList;
1290   // If requested, simply import all functions in the index. This is used
1291   // when testing distributed backend handling via the opt tool, when
1292   // we have distributed indexes containing exactly the summaries to import.
1293   if (ImportAllIndex)
1294     ComputeCrossModuleImportForModuleFromIndex(M.getModuleIdentifier(), *Index,
1295                                                ImportList);
1296   else
1297     ComputeCrossModuleImportForModule(M.getModuleIdentifier(), *Index,
1298                                       ImportList);
1299 
1300   // Conservatively mark all internal values as promoted. This interface is
1301   // only used when doing importing via the function importing pass. The pass
1302   // is only enabled when testing importing via the 'opt' tool, which does
1303   // not do the ThinLink that would normally determine what values to promote.
1304   for (auto &I : *Index) {
1305     for (auto &S : I.second.SummaryList) {
1306       if (GlobalValue::isLocalLinkage(S->linkage()))
1307         S->setLinkage(GlobalValue::ExternalLinkage);
1308     }
1309   }
1310 
1311   // Next we need to promote to global scope and rename any local values that
1312   // are potentially exported to other modules.
1313   if (renameModuleForThinLTO(M, *Index, /*clearDSOOnDeclarations=*/false,
1314                              /*GlobalsToImport=*/nullptr)) {
1315     errs() << "Error renaming module\n";
1316     return false;
1317   }
1318 
1319   // Perform the import now.
1320   auto ModuleLoader = [&M](StringRef Identifier) {
1321     return loadFile(std::string(Identifier), M.getContext());
1322   };
1323   FunctionImporter Importer(*Index, ModuleLoader,
1324                             /*ClearDSOLocalOnDeclarations=*/false);
1325   Expected<bool> Result = Importer.importFunctions(M, ImportList);
1326 
1327   // FIXME: Probably need to propagate Errors through the pass manager.
1328   if (!Result) {
1329     logAllUnhandledErrors(Result.takeError(), errs(),
1330                           "Error importing module: ");
1331     return false;
1332   }
1333 
1334   return *Result;
1335 }
1336 
1337 namespace {
1338 
1339 /// Pass that performs cross-module function import provided a summary file.
1340 class FunctionImportLegacyPass : public ModulePass {
1341 public:
1342   /// Pass identification, replacement for typeid
1343   static char ID;
1344 
1345   explicit FunctionImportLegacyPass() : ModulePass(ID) {}
1346 
1347   /// Specify pass name for debug output
1348   StringRef getPassName() const override { return "Function Importing"; }
1349 
1350   bool runOnModule(Module &M) override {
1351     if (skipModule(M))
1352       return false;
1353 
1354     return doImportingForModule(M);
1355   }
1356 };
1357 
1358 } // end anonymous namespace
1359 
1360 PreservedAnalyses FunctionImportPass::run(Module &M,
1361                                           ModuleAnalysisManager &AM) {
1362   if (!doImportingForModule(M))
1363     return PreservedAnalyses::all();
1364 
1365   return PreservedAnalyses::none();
1366 }
1367 
1368 char FunctionImportLegacyPass::ID = 0;
1369 INITIALIZE_PASS(FunctionImportLegacyPass, "function-import",
1370                 "Summary Based Function Import", false, false)
1371 
1372 namespace llvm {
1373 
1374 Pass *createFunctionImportPass() {
1375   return new FunctionImportLegacyPass();
1376 }
1377 
1378 } // end namespace llvm
1379