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