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