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