xref: /llvm-project/llvm/lib/Transforms/IPO/FunctionImport.cpp (revision 4e30cf7b2a94b502abb10c400255547e50f79648)
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/StringRef.h"
20 #include "llvm/Bitcode/BitcodeReader.h"
21 #include "llvm/IR/AutoUpgrade.h"
22 #include "llvm/IR/Function.h"
23 #include "llvm/IR/GlobalAlias.h"
24 #include "llvm/IR/GlobalObject.h"
25 #include "llvm/IR/GlobalValue.h"
26 #include "llvm/IR/GlobalVariable.h"
27 #include "llvm/IR/Metadata.h"
28 #include "llvm/IR/Module.h"
29 #include "llvm/IR/ModuleSummaryIndex.h"
30 #include "llvm/IRReader/IRReader.h"
31 #include "llvm/Linker/IRMover.h"
32 #include "llvm/ProfileData/PGOCtxProfReader.h"
33 #include "llvm/Support/Casting.h"
34 #include "llvm/Support/CommandLine.h"
35 #include "llvm/Support/Debug.h"
36 #include "llvm/Support/Errc.h"
37 #include "llvm/Support/Error.h"
38 #include "llvm/Support/ErrorHandling.h"
39 #include "llvm/Support/FileSystem.h"
40 #include "llvm/Support/JSON.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' and "
129              "'thinlto_src_file'"));
130 
131 /// Summary file to use for function importing when using -function-import from
132 /// the command line.
133 static cl::opt<std::string>
134     SummaryFile("summary-file",
135                 cl::desc("The summary file to use for function importing."));
136 
137 /// Used when testing importing from distributed indexes via opt
138 // -function-import.
139 static cl::opt<bool>
140     ImportAllIndex("import-all-index",
141                    cl::desc("Import all external functions in index."));
142 
143 /// This is a test-only option.
144 /// If this option is enabled, the ThinLTO indexing step will import each
145 /// function declaration as a fallback. In a real build this may increase ram
146 /// usage of the indexing step unnecessarily.
147 /// TODO: Implement selective import (based on combined summary analysis) to
148 /// ensure the imported function has a use case in the postlink pipeline.
149 static cl::opt<bool> ImportDeclaration(
150     "import-declaration", cl::init(false), cl::Hidden,
151     cl::desc("If true, import function declaration as fallback if the function "
152              "definition is not imported."));
153 
154 /// Pass a workload description file - an example of workload would be the
155 /// functions executed to satisfy a RPC request. A workload is defined by a root
156 /// function and the list of functions that are (frequently) needed to satisfy
157 /// it. The module that defines the root will have all those functions imported.
158 /// The file contains a JSON dictionary. The keys are root functions, the values
159 /// are lists of functions to import in the module defining the root. It is
160 /// assumed -funique-internal-linkage-names was used, thus ensuring function
161 /// names are unique even for local linkage ones.
162 static cl::opt<std::string> WorkloadDefinitions(
163     "thinlto-workload-def",
164     cl::desc("Pass a workload definition. This is a file containing a JSON "
165              "dictionary. The keys are root functions, the values are lists of "
166              "functions to import in the module defining the root. It is "
167              "assumed -funique-internal-linkage-names was used, to ensure "
168              "local linkage functions have unique names. For example: \n"
169              "{\n"
170              "  \"rootFunction_1\": [\"function_to_import_1\", "
171              "\"function_to_import_2\"], \n"
172              "  \"rootFunction_2\": [\"function_to_import_3\", "
173              "\"function_to_import_4\"] \n"
174              "}"),
175     cl::Hidden);
176 
177 extern cl::opt<std::string> UseCtxProfile;
178 
179 namespace llvm {
180 extern cl::opt<bool> EnableMemProfContextDisambiguation;
181 }
182 
183 // Load lazily a module from \p FileName in \p Context.
184 static std::unique_ptr<Module> loadFile(const std::string &FileName,
185                                         LLVMContext &Context) {
186   SMDiagnostic Err;
187   LLVM_DEBUG(dbgs() << "Loading '" << FileName << "'\n");
188   // Metadata isn't loaded until functions are imported, to minimize
189   // the memory overhead.
190   std::unique_ptr<Module> Result =
191       getLazyIRFileModule(FileName, Err, Context,
192                           /* ShouldLazyLoadMetadata = */ true);
193   if (!Result) {
194     Err.print("function-import", errs());
195     report_fatal_error("Abort");
196   }
197 
198   return Result;
199 }
200 
201 static bool shouldSkipLocalInAnotherModule(const GlobalValueSummary *RefSummary,
202                                            size_t NumDefs,
203                                            StringRef ImporterModule) {
204   // We can import a local when there is one definition.
205   if (NumDefs == 1)
206     return false;
207   // In other cases, make sure we import the copy in the caller's module if the
208   // referenced value has local linkage. The only time a local variable can
209   // share an entry in the index is if there is a local with the same name in
210   // another module that had the same source file name (in a different
211   // directory), where each was compiled in their own directory so there was not
212   // distinguishing path.
213   return GlobalValue::isLocalLinkage(RefSummary->linkage()) &&
214          RefSummary->modulePath() != ImporterModule;
215 }
216 
217 /// Given a list of possible callee implementation for a call site, qualify the
218 /// legality of importing each. The return is a range of pairs. Each pair
219 /// corresponds to a candidate. The first value is the ImportFailureReason for
220 /// that candidate, the second is the candidate.
221 static auto qualifyCalleeCandidates(
222     const ModuleSummaryIndex &Index,
223     ArrayRef<std::unique_ptr<GlobalValueSummary>> CalleeSummaryList,
224     StringRef CallerModulePath) {
225   return llvm::map_range(
226       CalleeSummaryList,
227       [&Index, CalleeSummaryList,
228        CallerModulePath](const std::unique_ptr<GlobalValueSummary> &SummaryPtr)
229           -> std::pair<FunctionImporter::ImportFailureReason,
230                        const GlobalValueSummary *> {
231         auto *GVSummary = SummaryPtr.get();
232         if (!Index.isGlobalValueLive(GVSummary))
233           return {FunctionImporter::ImportFailureReason::NotLive, GVSummary};
234 
235         if (GlobalValue::isInterposableLinkage(GVSummary->linkage()))
236           return {FunctionImporter::ImportFailureReason::InterposableLinkage,
237                   GVSummary};
238 
239         auto *Summary = dyn_cast<FunctionSummary>(GVSummary->getBaseObject());
240 
241         // Ignore any callees that aren't actually functions. This could happen
242         // in the case of GUID hash collisions. It could also happen in theory
243         // for SamplePGO profiles collected on old versions of the code after
244         // renaming, since we synthesize edges to any inlined callees appearing
245         // in the profile.
246         if (!Summary)
247           return {FunctionImporter::ImportFailureReason::GlobalVar, GVSummary};
248 
249         // If this is a local function, make sure we import the copy in the
250         // caller's module. The only time a local function can share an entry in
251         // the index is if there is a local with the same name in another module
252         // that had the same source file name (in a different directory), where
253         // each was compiled in their own directory so there was not
254         // distinguishing path.
255         // If the local function is from another module, it must be a reference
256         // due to indirect call profile data since a function pointer can point
257         // to a local in another module. Do the import from another module if
258         // there is only one entry in the list or when all files in the program
259         // are compiled with full path - in both cases the local function has
260         // unique PGO name and GUID.
261         if (shouldSkipLocalInAnotherModule(Summary, CalleeSummaryList.size(),
262                                            CallerModulePath))
263           return {
264               FunctionImporter::ImportFailureReason::LocalLinkageNotInModule,
265               GVSummary};
266 
267         // Skip if it isn't legal to import (e.g. may reference unpromotable
268         // locals).
269         if (Summary->notEligibleToImport())
270           return {FunctionImporter::ImportFailureReason::NotEligible,
271                   GVSummary};
272 
273         return {FunctionImporter::ImportFailureReason::None, GVSummary};
274       });
275 }
276 
277 /// Given a list of possible callee implementation for a call site, select one
278 /// that fits the \p Threshold for function definition import. If none are
279 /// found, the Reason will give the last reason for the failure (last, in the
280 /// order of CalleeSummaryList entries). While looking for a callee definition,
281 /// sets \p TooLargeOrNoInlineSummary to the last seen too-large or noinline
282 /// candidate; other modules may want to know the function summary or
283 /// declaration even if a definition is not needed.
284 ///
285 /// FIXME: select "best" instead of first that fits. But what is "best"?
286 /// - The smallest: more likely to be inlined.
287 /// - The one with the least outgoing edges (already well optimized).
288 /// - One from a module already being imported from in order to reduce the
289 ///   number of source modules parsed/linked.
290 /// - One that has PGO data attached.
291 /// - [insert you fancy metric here]
292 static const GlobalValueSummary *
293 selectCallee(const ModuleSummaryIndex &Index,
294              ArrayRef<std::unique_ptr<GlobalValueSummary>> CalleeSummaryList,
295              unsigned Threshold, StringRef CallerModulePath,
296              const GlobalValueSummary *&TooLargeOrNoInlineSummary,
297              FunctionImporter::ImportFailureReason &Reason) {
298   // Records the last summary with reason noinline or too-large.
299   TooLargeOrNoInlineSummary = nullptr;
300   auto QualifiedCandidates =
301       qualifyCalleeCandidates(Index, CalleeSummaryList, CallerModulePath);
302   for (auto QualifiedValue : QualifiedCandidates) {
303     Reason = QualifiedValue.first;
304     // Skip a summary if its import is not (proved to be) legal.
305     if (Reason != FunctionImporter::ImportFailureReason::None)
306       continue;
307     auto *Summary =
308         cast<FunctionSummary>(QualifiedValue.second->getBaseObject());
309 
310     // Don't bother importing the definition if the chance of inlining it is
311     // not high enough (except under `--force-import-all`).
312     if ((Summary->instCount() > Threshold) && !Summary->fflags().AlwaysInline &&
313         !ForceImportAll) {
314       TooLargeOrNoInlineSummary = Summary;
315       Reason = FunctionImporter::ImportFailureReason::TooLarge;
316       continue;
317     }
318 
319     // Don't bother importing the definition if we can't inline it anyway.
320     if (Summary->fflags().NoInline && !ForceImportAll) {
321       TooLargeOrNoInlineSummary = Summary;
322       Reason = FunctionImporter::ImportFailureReason::NoInline;
323       continue;
324     }
325 
326     return Summary;
327   }
328   return nullptr;
329 }
330 
331 namespace {
332 
333 using EdgeInfo = std::tuple<const FunctionSummary *, unsigned /* Threshold */>;
334 
335 } // anonymous namespace
336 
337 FunctionImporter::ImportMapTy::AddDefinitionStatus
338 FunctionImporter::ImportMapTy::addDefinition(StringRef FromModule,
339                                              GlobalValue::GUID GUID) {
340   auto [It, Inserted] =
341       ImportMap[FromModule].try_emplace(GUID, GlobalValueSummary::Definition);
342   if (Inserted)
343     return AddDefinitionStatus::Inserted;
344   if (It->second == GlobalValueSummary::Definition)
345     return AddDefinitionStatus::NoChange;
346   It->second = GlobalValueSummary::Definition;
347   return AddDefinitionStatus::ChangedToDefinition;
348 }
349 
350 void FunctionImporter::ImportMapTy::maybeAddDeclaration(
351     StringRef FromModule, GlobalValue::GUID GUID) {
352   ImportMap[FromModule].try_emplace(GUID, GlobalValueSummary::Declaration);
353 }
354 
355 SmallVector<StringRef, 0>
356 FunctionImporter::ImportMapTy::getSourceModules() const {
357   SmallVector<StringRef, 0> Modules(make_first_range(ImportMap));
358   llvm::sort(Modules);
359   return Modules;
360 }
361 
362 /// Import globals referenced by a function or other globals that are being
363 /// imported, if importing such global is possible.
364 class GlobalsImporter final {
365   const ModuleSummaryIndex &Index;
366   const GVSummaryMapTy &DefinedGVSummaries;
367   function_ref<bool(GlobalValue::GUID, const GlobalValueSummary *)>
368       IsPrevailing;
369   FunctionImporter::ImportMapTy &ImportList;
370   DenseMap<StringRef, FunctionImporter::ExportSetTy> *const ExportLists;
371 
372   bool shouldImportGlobal(const ValueInfo &VI) {
373     const auto &GVS = DefinedGVSummaries.find(VI.getGUID());
374     if (GVS == DefinedGVSummaries.end())
375       return true;
376     // We should not skip import if the module contains a non-prevailing
377     // definition with interposable linkage type. This is required for
378     // correctness in the situation where there is a prevailing def available
379     // for import and marked read-only. In this case, the non-prevailing def
380     // will be converted to a declaration, while the prevailing one becomes
381     // internal, thus no definitions will be available for linking. In order to
382     // prevent undefined symbol link error, the prevailing definition must be
383     // imported.
384     // FIXME: Consider adding a check that the suitable prevailing definition
385     // exists and marked read-only.
386     if (VI.getSummaryList().size() > 1 &&
387         GlobalValue::isInterposableLinkage(GVS->second->linkage()) &&
388         !IsPrevailing(VI.getGUID(), GVS->second))
389       return true;
390 
391     return false;
392   }
393 
394   void
395   onImportingSummaryImpl(const GlobalValueSummary &Summary,
396                          SmallVectorImpl<const GlobalVarSummary *> &Worklist) {
397     for (const auto &VI : Summary.refs()) {
398       if (!shouldImportGlobal(VI)) {
399         LLVM_DEBUG(
400             dbgs() << "Ref ignored! Target already in destination module.\n");
401         continue;
402       }
403 
404       LLVM_DEBUG(dbgs() << " ref -> " << VI << "\n");
405 
406       for (const auto &RefSummary : VI.getSummaryList()) {
407         const auto *GVS = dyn_cast<GlobalVarSummary>(RefSummary.get());
408         // Functions could be referenced by global vars - e.g. a vtable; but we
409         // don't currently imagine a reason those would be imported here, rather
410         // than as part of the logic deciding which functions to import (i.e.
411         // based on profile information). Should we decide to handle them here,
412         // we can refactor accordingly at that time.
413         if (!GVS || !Index.canImportGlobalVar(GVS, /* AnalyzeRefs */ true) ||
414             shouldSkipLocalInAnotherModule(GVS, VI.getSummaryList().size(),
415                                            Summary.modulePath()))
416           continue;
417 
418         // If there isn't an entry for GUID, insert <GUID, Definition> pair.
419         // Otherwise, definition should take precedence over declaration.
420         if (ImportList.addDefinition(RefSummary->modulePath(), VI.getGUID()) !=
421             FunctionImporter::ImportMapTy::AddDefinitionStatus::Inserted)
422           break;
423 
424         // Only update stat and exports if we haven't already imported this
425         // variable.
426         NumImportedGlobalVarsThinLink++;
427         // Any references made by this variable will be marked exported
428         // later, in ComputeCrossModuleImport, after import decisions are
429         // complete, which is more efficient than adding them here.
430         if (ExportLists)
431           (*ExportLists)[RefSummary->modulePath()].insert(VI);
432 
433         // If variable is not writeonly we attempt to recursively analyze
434         // its references in order to import referenced constants.
435         if (!Index.isWriteOnly(GVS))
436           Worklist.emplace_back(GVS);
437         break;
438       }
439     }
440   }
441 
442 public:
443   GlobalsImporter(
444       const ModuleSummaryIndex &Index, const GVSummaryMapTy &DefinedGVSummaries,
445       function_ref<bool(GlobalValue::GUID, const GlobalValueSummary *)>
446           IsPrevailing,
447       FunctionImporter::ImportMapTy &ImportList,
448       DenseMap<StringRef, FunctionImporter::ExportSetTy> *ExportLists)
449       : Index(Index), DefinedGVSummaries(DefinedGVSummaries),
450         IsPrevailing(IsPrevailing), ImportList(ImportList),
451         ExportLists(ExportLists) {}
452 
453   void onImportingSummary(const GlobalValueSummary &Summary) {
454     SmallVector<const GlobalVarSummary *, 128> Worklist;
455     onImportingSummaryImpl(Summary, Worklist);
456     while (!Worklist.empty())
457       onImportingSummaryImpl(*Worklist.pop_back_val(), Worklist);
458   }
459 };
460 
461 static const char *getFailureName(FunctionImporter::ImportFailureReason Reason);
462 
463 /// Determine the list of imports and exports for each module.
464 class ModuleImportsManager {
465 protected:
466   function_ref<bool(GlobalValue::GUID, const GlobalValueSummary *)>
467       IsPrevailing;
468   const ModuleSummaryIndex &Index;
469   DenseMap<StringRef, FunctionImporter::ExportSetTy> *const ExportLists;
470 
471   ModuleImportsManager(
472       function_ref<bool(GlobalValue::GUID, const GlobalValueSummary *)>
473           IsPrevailing,
474       const ModuleSummaryIndex &Index,
475       DenseMap<StringRef, FunctionImporter::ExportSetTy> *ExportLists = nullptr)
476       : IsPrevailing(IsPrevailing), Index(Index), ExportLists(ExportLists) {}
477 
478 public:
479   virtual ~ModuleImportsManager() = default;
480 
481   /// Given the list of globals defined in a module, compute the list of imports
482   /// as well as the list of "exports", i.e. the list of symbols referenced from
483   /// another module (that may require promotion).
484   virtual void
485   computeImportForModule(const GVSummaryMapTy &DefinedGVSummaries,
486                          StringRef ModName,
487                          FunctionImporter::ImportMapTy &ImportList);
488 
489   static std::unique_ptr<ModuleImportsManager>
490   create(function_ref<bool(GlobalValue::GUID, const GlobalValueSummary *)>
491              IsPrevailing,
492          const ModuleSummaryIndex &Index,
493          DenseMap<StringRef, FunctionImporter::ExportSetTy> *ExportLists =
494              nullptr);
495 };
496 
497 /// A ModuleImportsManager that operates based on a workload definition (see
498 /// -thinlto-workload-def). For modules that do not define workload roots, it
499 /// applies the base ModuleImportsManager import policy.
500 class WorkloadImportsManager : public ModuleImportsManager {
501   // Keep a module name -> value infos to import association. We use it to
502   // determine if a module's import list should be done by the base
503   // ModuleImportsManager or by us.
504   StringMap<DenseSet<ValueInfo>> Workloads;
505 
506   void
507   computeImportForModule(const GVSummaryMapTy &DefinedGVSummaries,
508                          StringRef ModName,
509                          FunctionImporter::ImportMapTy &ImportList) override {
510     auto SetIter = Workloads.find(ModName);
511     if (SetIter == Workloads.end()) {
512       LLVM_DEBUG(dbgs() << "[Workload] " << ModName
513                         << " does not contain the root of any context.\n");
514       return ModuleImportsManager::computeImportForModule(DefinedGVSummaries,
515                                                           ModName, ImportList);
516     }
517     LLVM_DEBUG(dbgs() << "[Workload] " << ModName
518                       << " contains the root(s) of context(s).\n");
519 
520     GlobalsImporter GVI(Index, DefinedGVSummaries, IsPrevailing, ImportList,
521                         ExportLists);
522     auto &ValueInfos = SetIter->second;
523     SmallVector<EdgeInfo, 128> GlobWorklist;
524     for (auto &VI : llvm::make_early_inc_range(ValueInfos)) {
525       auto It = DefinedGVSummaries.find(VI.getGUID());
526       if (It != DefinedGVSummaries.end() &&
527           IsPrevailing(VI.getGUID(), It->second)) {
528         LLVM_DEBUG(
529             dbgs() << "[Workload] " << VI.name()
530                    << " has the prevailing variant already in the module "
531                    << ModName << ". No need to import\n");
532         continue;
533       }
534       auto Candidates =
535           qualifyCalleeCandidates(Index, VI.getSummaryList(), ModName);
536 
537       const GlobalValueSummary *GVS = nullptr;
538       auto PotentialCandidates = llvm::map_range(
539           llvm::make_filter_range(
540               Candidates,
541               [&](const auto &Candidate) {
542                 LLVM_DEBUG(dbgs() << "[Workflow] Candidate for " << VI.name()
543                                   << " from " << Candidate.second->modulePath()
544                                   << " ImportFailureReason: "
545                                   << getFailureName(Candidate.first) << "\n");
546                 return Candidate.first ==
547                         FunctionImporter::ImportFailureReason::None;
548               }),
549           [](const auto &Candidate) { return Candidate.second; });
550       if (PotentialCandidates.empty()) {
551         LLVM_DEBUG(dbgs() << "[Workload] Not importing " << VI.name()
552                           << " because can't find eligible Callee. Guid is: "
553                           << Function::getGUID(VI.name()) << "\n");
554         continue;
555       }
556       /// We will prefer importing the prevailing candidate, if not, we'll
557       /// still pick the first available candidate. The reason we want to make
558       /// sure we do import the prevailing candidate is because the goal of
559       /// workload-awareness is to enable optimizations specializing the call
560       /// graph of that workload. Suppose a function is already defined in the
561       /// module, but it's not the prevailing variant. Suppose also we do not
562       /// inline it (in fact, if it were interposable, we can't inline it),
563       /// but we could specialize it to the workload in other ways. However,
564       /// the linker would drop it in the favor of the prevailing copy.
565       /// Instead, by importing the prevailing variant (assuming also the use
566       /// of `-avail-extern-to-local`), we keep the specialization. We could
567       /// alteranatively make the non-prevailing variant local, but the
568       /// prevailing one is also the one for which we would have previously
569       /// collected profiles, making it preferrable.
570       auto PrevailingCandidates = llvm::make_filter_range(
571           PotentialCandidates, [&](const auto *Candidate) {
572             return IsPrevailing(VI.getGUID(), Candidate);
573           });
574       if (PrevailingCandidates.empty()) {
575         GVS = *PotentialCandidates.begin();
576         if (!llvm::hasSingleElement(PotentialCandidates) &&
577             GlobalValue::isLocalLinkage(GVS->linkage()))
578           LLVM_DEBUG(
579               dbgs()
580               << "[Workload] Found multiple non-prevailing candidates for "
581               << VI.name()
582               << ". This is unexpected. Are module paths passed to the "
583                  "compiler unique for the modules passed to the linker?");
584         // We could in theory have multiple (interposable) copies of a symbol
585         // when there is no prevailing candidate, if say the prevailing copy was
586         // in a native object being linked in. However, we should in theory be
587         // marking all of these non-prevailing IR copies dead in that case, in
588         // which case they won't be candidates.
589         assert(GVS->isLive());
590       } else {
591         assert(llvm::hasSingleElement(PrevailingCandidates));
592         GVS = *PrevailingCandidates.begin();
593       }
594 
595       auto ExportingModule = GVS->modulePath();
596       // We checked that for the prevailing case, but if we happen to have for
597       // example an internal that's defined in this module, it'd have no
598       // PrevailingCandidates.
599       if (ExportingModule == ModName) {
600         LLVM_DEBUG(dbgs() << "[Workload] Not importing " << VI.name()
601                           << " because its defining module is the same as the "
602                              "current module\n");
603         continue;
604       }
605       LLVM_DEBUG(dbgs() << "[Workload][Including]" << VI.name() << " from "
606                         << ExportingModule << " : "
607                         << Function::getGUID(VI.name()) << "\n");
608       ImportList.addDefinition(ExportingModule, VI.getGUID());
609       GVI.onImportingSummary(*GVS);
610       if (ExportLists)
611         (*ExportLists)[ExportingModule].insert(VI);
612     }
613     LLVM_DEBUG(dbgs() << "[Workload] Done\n");
614   }
615 
616   void loadFromJson() {
617     // Since the workload def uses names, we need a quick lookup
618     // name->ValueInfo.
619     StringMap<ValueInfo> NameToValueInfo;
620     StringSet<> AmbiguousNames;
621     for (auto &I : Index) {
622       ValueInfo VI = Index.getValueInfo(I);
623       if (!NameToValueInfo.insert(std::make_pair(VI.name(), VI)).second)
624         LLVM_DEBUG(AmbiguousNames.insert(VI.name()));
625     }
626     auto DbgReportIfAmbiguous = [&](StringRef Name) {
627       LLVM_DEBUG(if (AmbiguousNames.count(Name) > 0) {
628         dbgs() << "[Workload] Function name " << Name
629                << " present in the workload definition is ambiguous. Consider "
630                   "compiling with -funique-internal-linkage-names.";
631       });
632     };
633     std::error_code EC;
634     auto BufferOrErr = MemoryBuffer::getFileOrSTDIN(WorkloadDefinitions);
635     if (std::error_code EC = BufferOrErr.getError()) {
636       report_fatal_error("Failed to open context file");
637       return;
638     }
639     auto Buffer = std::move(BufferOrErr.get());
640     std::map<std::string, std::vector<std::string>> WorkloadDefs;
641     json::Path::Root NullRoot;
642     // The JSON is supposed to contain a dictionary matching the type of
643     // WorkloadDefs. For example:
644     // {
645     //   "rootFunction_1": ["function_to_import_1", "function_to_import_2"],
646     //   "rootFunction_2": ["function_to_import_3", "function_to_import_4"]
647     // }
648     auto Parsed = json::parse(Buffer->getBuffer());
649     if (!Parsed)
650       report_fatal_error(Parsed.takeError());
651     if (!json::fromJSON(*Parsed, WorkloadDefs, NullRoot))
652       report_fatal_error("Invalid thinlto contextual profile format.");
653     for (const auto &Workload : WorkloadDefs) {
654       const auto &Root = Workload.first;
655       DbgReportIfAmbiguous(Root);
656       LLVM_DEBUG(dbgs() << "[Workload] Root: " << Root << "\n");
657       const auto &AllCallees = Workload.second;
658       auto RootIt = NameToValueInfo.find(Root);
659       if (RootIt == NameToValueInfo.end()) {
660         LLVM_DEBUG(dbgs() << "[Workload] Root " << Root
661                           << " not found in this linkage unit.\n");
662         continue;
663       }
664       auto RootVI = RootIt->second;
665       if (RootVI.getSummaryList().size() != 1) {
666         LLVM_DEBUG(dbgs() << "[Workload] Root " << Root
667                           << " should have exactly one summary, but has "
668                           << RootVI.getSummaryList().size() << ". Skipping.\n");
669         continue;
670       }
671       StringRef RootDefiningModule =
672           RootVI.getSummaryList().front()->modulePath();
673       LLVM_DEBUG(dbgs() << "[Workload] Root defining module for " << Root
674                         << " is : " << RootDefiningModule << "\n");
675       auto &Set = Workloads[RootDefiningModule];
676       for (const auto &Callee : AllCallees) {
677         LLVM_DEBUG(dbgs() << "[Workload] " << Callee << "\n");
678         DbgReportIfAmbiguous(Callee);
679         auto ElemIt = NameToValueInfo.find(Callee);
680         if (ElemIt == NameToValueInfo.end()) {
681           LLVM_DEBUG(dbgs() << "[Workload] " << Callee << " not found\n");
682           continue;
683         }
684         Set.insert(ElemIt->second);
685       }
686     }
687   }
688 
689   void loadFromCtxProf() {
690     std::error_code EC;
691     auto BufferOrErr = MemoryBuffer::getFileOrSTDIN(UseCtxProfile);
692     if (std::error_code EC = BufferOrErr.getError()) {
693       report_fatal_error("Failed to open contextual profile file");
694       return;
695     }
696     auto Buffer = std::move(BufferOrErr.get());
697 
698     PGOCtxProfileReader Reader(Buffer->getBuffer());
699     auto Ctx = Reader.loadContexts();
700     if (!Ctx) {
701       report_fatal_error("Failed to parse contextual profiles");
702       return;
703     }
704     const auto &CtxMap = *Ctx;
705     DenseSet<GlobalValue::GUID> ContainedGUIDs;
706     for (const auto &[RootGuid, Root] : CtxMap) {
707       // Avoid ContainedGUIDs to get in/out of scope. Reuse its memory for
708       // subsequent roots, but clear its contents.
709       ContainedGUIDs.clear();
710 
711       auto RootVI = Index.getValueInfo(RootGuid);
712       if (!RootVI) {
713         LLVM_DEBUG(dbgs() << "[Workload] Root " << RootGuid
714                           << " not found in this linkage unit.\n");
715         continue;
716       }
717       if (RootVI.getSummaryList().size() != 1) {
718         LLVM_DEBUG(dbgs() << "[Workload] Root " << RootGuid
719                           << " should have exactly one summary, but has "
720                           << RootVI.getSummaryList().size() << ". Skipping.\n");
721         continue;
722       }
723       StringRef RootDefiningModule =
724           RootVI.getSummaryList().front()->modulePath();
725       LLVM_DEBUG(dbgs() << "[Workload] Root defining module for " << RootGuid
726                         << " is : " << RootDefiningModule << "\n");
727       auto &Set = Workloads[RootDefiningModule];
728       Root.getContainedGuids(ContainedGUIDs);
729       for (auto Guid : ContainedGUIDs)
730         if (auto VI = Index.getValueInfo(Guid))
731           Set.insert(VI);
732     }
733   }
734 
735 public:
736   WorkloadImportsManager(
737       function_ref<bool(GlobalValue::GUID, const GlobalValueSummary *)>
738           IsPrevailing,
739       const ModuleSummaryIndex &Index,
740       DenseMap<StringRef, FunctionImporter::ExportSetTy> *ExportLists)
741       : ModuleImportsManager(IsPrevailing, Index, ExportLists) {
742     if (UseCtxProfile.empty() == WorkloadDefinitions.empty()) {
743       report_fatal_error(
744           "Pass only one of: -thinlto-pgo-ctx-prof or -thinlto-workload-def");
745       return;
746     }
747     if (!UseCtxProfile.empty())
748       loadFromCtxProf();
749     else
750       loadFromJson();
751     LLVM_DEBUG({
752       for (const auto &[Root, Set] : Workloads) {
753         dbgs() << "[Workload] Root: " << Root << " we have " << Set.size()
754                << " distinct callees.\n";
755         for (const auto &VI : Set) {
756           dbgs() << "[Workload] Root: " << Root
757                  << " Would include: " << VI.getGUID() << "\n";
758         }
759       }
760     });
761   }
762 };
763 
764 std::unique_ptr<ModuleImportsManager> ModuleImportsManager::create(
765     function_ref<bool(GlobalValue::GUID, const GlobalValueSummary *)>
766         IsPrevailing,
767     const ModuleSummaryIndex &Index,
768     DenseMap<StringRef, FunctionImporter::ExportSetTy> *ExportLists) {
769   if (WorkloadDefinitions.empty() && UseCtxProfile.empty()) {
770     LLVM_DEBUG(dbgs() << "[Workload] Using the regular imports manager.\n");
771     return std::unique_ptr<ModuleImportsManager>(
772         new ModuleImportsManager(IsPrevailing, Index, ExportLists));
773   }
774   LLVM_DEBUG(dbgs() << "[Workload] Using the contextual imports manager.\n");
775   return std::make_unique<WorkloadImportsManager>(IsPrevailing, Index,
776                                                   ExportLists);
777 }
778 
779 static const char *
780 getFailureName(FunctionImporter::ImportFailureReason Reason) {
781   switch (Reason) {
782   case FunctionImporter::ImportFailureReason::None:
783     return "None";
784   case FunctionImporter::ImportFailureReason::GlobalVar:
785     return "GlobalVar";
786   case FunctionImporter::ImportFailureReason::NotLive:
787     return "NotLive";
788   case FunctionImporter::ImportFailureReason::TooLarge:
789     return "TooLarge";
790   case FunctionImporter::ImportFailureReason::InterposableLinkage:
791     return "InterposableLinkage";
792   case FunctionImporter::ImportFailureReason::LocalLinkageNotInModule:
793     return "LocalLinkageNotInModule";
794   case FunctionImporter::ImportFailureReason::NotEligible:
795     return "NotEligible";
796   case FunctionImporter::ImportFailureReason::NoInline:
797     return "NoInline";
798   }
799   llvm_unreachable("invalid reason");
800 }
801 
802 /// Compute the list of functions to import for a given caller. Mark these
803 /// imported functions and the symbols they reference in their source module as
804 /// exported from their source module.
805 static void computeImportForFunction(
806     const FunctionSummary &Summary, const ModuleSummaryIndex &Index,
807     const unsigned Threshold, const GVSummaryMapTy &DefinedGVSummaries,
808     function_ref<bool(GlobalValue::GUID, const GlobalValueSummary *)>
809         isPrevailing,
810     SmallVectorImpl<EdgeInfo> &Worklist, GlobalsImporter &GVImporter,
811     FunctionImporter::ImportMapTy &ImportList,
812     DenseMap<StringRef, FunctionImporter::ExportSetTy> *ExportLists,
813     FunctionImporter::ImportThresholdsTy &ImportThresholds) {
814   GVImporter.onImportingSummary(Summary);
815   static int ImportCount = 0;
816   for (const auto &Edge : Summary.calls()) {
817     ValueInfo VI = Edge.first;
818     LLVM_DEBUG(dbgs() << " edge -> " << VI << " Threshold:" << Threshold
819                       << "\n");
820 
821     if (ImportCutoff >= 0 && ImportCount >= ImportCutoff) {
822       LLVM_DEBUG(dbgs() << "ignored! import-cutoff value of " << ImportCutoff
823                         << " reached.\n");
824       continue;
825     }
826 
827     if (DefinedGVSummaries.count(VI.getGUID())) {
828       // FIXME: Consider not skipping import if the module contains
829       // a non-prevailing def with interposable linkage. The prevailing copy
830       // can safely be imported (see shouldImportGlobal()).
831       LLVM_DEBUG(dbgs() << "ignored! Target already in destination module.\n");
832       continue;
833     }
834 
835     auto GetBonusMultiplier = [](CalleeInfo::HotnessType Hotness) -> float {
836       if (Hotness == CalleeInfo::HotnessType::Hot)
837         return ImportHotMultiplier;
838       if (Hotness == CalleeInfo::HotnessType::Cold)
839         return ImportColdMultiplier;
840       if (Hotness == CalleeInfo::HotnessType::Critical)
841         return ImportCriticalMultiplier;
842       return 1.0;
843     };
844 
845     const auto NewThreshold =
846         Threshold * GetBonusMultiplier(Edge.second.getHotness());
847 
848     auto IT = ImportThresholds.insert(std::make_pair(
849         VI.getGUID(), std::make_tuple(NewThreshold, nullptr, nullptr)));
850     bool PreviouslyVisited = !IT.second;
851     auto &ProcessedThreshold = std::get<0>(IT.first->second);
852     auto &CalleeSummary = std::get<1>(IT.first->second);
853     auto &FailureInfo = std::get<2>(IT.first->second);
854 
855     bool IsHotCallsite =
856         Edge.second.getHotness() == CalleeInfo::HotnessType::Hot;
857     bool IsCriticalCallsite =
858         Edge.second.getHotness() == CalleeInfo::HotnessType::Critical;
859 
860     const FunctionSummary *ResolvedCalleeSummary = nullptr;
861     if (CalleeSummary) {
862       assert(PreviouslyVisited);
863       // Since the traversal of the call graph is DFS, we can revisit a function
864       // a second time with a higher threshold. In this case, it is added back
865       // to the worklist with the new threshold (so that its own callee chains
866       // can be considered with the higher threshold).
867       if (NewThreshold <= ProcessedThreshold) {
868         LLVM_DEBUG(
869             dbgs() << "ignored! Target was already imported with Threshold "
870                    << ProcessedThreshold << "\n");
871         continue;
872       }
873       // Update with new larger threshold.
874       ProcessedThreshold = NewThreshold;
875       ResolvedCalleeSummary = cast<FunctionSummary>(CalleeSummary);
876     } else {
877       // If we already rejected importing a callee at the same or higher
878       // threshold, don't waste time calling selectCallee.
879       if (PreviouslyVisited && NewThreshold <= ProcessedThreshold) {
880         LLVM_DEBUG(
881             dbgs() << "ignored! Target was already rejected with Threshold "
882             << ProcessedThreshold << "\n");
883         if (PrintImportFailures) {
884           assert(FailureInfo &&
885                  "Expected FailureInfo for previously rejected candidate");
886           FailureInfo->Attempts++;
887         }
888         continue;
889       }
890 
891       FunctionImporter::ImportFailureReason Reason{};
892 
893       // `SummaryForDeclImport` is an summary eligible for declaration import.
894       const GlobalValueSummary *SummaryForDeclImport = nullptr;
895       CalleeSummary =
896           selectCallee(Index, VI.getSummaryList(), NewThreshold,
897                        Summary.modulePath(), SummaryForDeclImport, Reason);
898       if (!CalleeSummary) {
899         // There isn't a callee for definition import but one for declaration
900         // import.
901         if (ImportDeclaration && SummaryForDeclImport) {
902           StringRef DeclSourceModule = SummaryForDeclImport->modulePath();
903 
904           // Note `ExportLists` only keeps track of exports due to imported
905           // definitions.
906           ImportList.maybeAddDeclaration(DeclSourceModule, VI.getGUID());
907         }
908         // Update with new larger threshold if this was a retry (otherwise
909         // we would have already inserted with NewThreshold above). Also
910         // update failure info if requested.
911         if (PreviouslyVisited) {
912           ProcessedThreshold = NewThreshold;
913           if (PrintImportFailures) {
914             assert(FailureInfo &&
915                    "Expected FailureInfo for previously rejected candidate");
916             FailureInfo->Reason = Reason;
917             FailureInfo->Attempts++;
918             FailureInfo->MaxHotness =
919                 std::max(FailureInfo->MaxHotness, Edge.second.getHotness());
920           }
921         } else if (PrintImportFailures) {
922           assert(!FailureInfo &&
923                  "Expected no FailureInfo for newly rejected candidate");
924           FailureInfo = std::make_unique<FunctionImporter::ImportFailureInfo>(
925               VI, Edge.second.getHotness(), Reason, 1);
926         }
927         if (ForceImportAll) {
928           std::string Msg = std::string("Failed to import function ") +
929                             VI.name().str() + " due to " +
930                             getFailureName(Reason);
931           auto Error = make_error<StringError>(
932               Msg, make_error_code(errc::not_supported));
933           logAllUnhandledErrors(std::move(Error), errs(),
934                                 "Error importing module: ");
935           break;
936         } else {
937           LLVM_DEBUG(dbgs()
938                      << "ignored! No qualifying callee with summary found.\n");
939           continue;
940         }
941       }
942 
943       // "Resolve" the summary
944       CalleeSummary = CalleeSummary->getBaseObject();
945       ResolvedCalleeSummary = cast<FunctionSummary>(CalleeSummary);
946 
947       assert((ResolvedCalleeSummary->fflags().AlwaysInline || ForceImportAll ||
948               (ResolvedCalleeSummary->instCount() <= NewThreshold)) &&
949              "selectCallee() didn't honor the threshold");
950 
951       auto ExportModulePath = ResolvedCalleeSummary->modulePath();
952 
953       // Try emplace the definition entry, and update stats based on insertion
954       // status.
955       if (ImportList.addDefinition(ExportModulePath, VI.getGUID()) !=
956           FunctionImporter::ImportMapTy::AddDefinitionStatus::NoChange) {
957         NumImportedFunctionsThinLink++;
958         if (IsHotCallsite)
959           NumImportedHotFunctionsThinLink++;
960         if (IsCriticalCallsite)
961           NumImportedCriticalFunctionsThinLink++;
962       }
963 
964       // Any calls/references made by this function will be marked exported
965       // later, in ComputeCrossModuleImport, after import decisions are
966       // complete, which is more efficient than adding them here.
967       if (ExportLists)
968         (*ExportLists)[ExportModulePath].insert(VI);
969     }
970 
971     auto GetAdjustedThreshold = [](unsigned Threshold, bool IsHotCallsite) {
972       // Adjust the threshold for next level of imported functions.
973       // The threshold is different for hot callsites because we can then
974       // inline chains of hot calls.
975       if (IsHotCallsite)
976         return Threshold * ImportHotInstrFactor;
977       return Threshold * ImportInstrFactor;
978     };
979 
980     const auto AdjThreshold = GetAdjustedThreshold(Threshold, IsHotCallsite);
981 
982     ImportCount++;
983 
984     // Insert the newly imported function to the worklist.
985     Worklist.emplace_back(ResolvedCalleeSummary, AdjThreshold);
986   }
987 }
988 
989 void ModuleImportsManager::computeImportForModule(
990     const GVSummaryMapTy &DefinedGVSummaries, StringRef ModName,
991     FunctionImporter::ImportMapTy &ImportList) {
992   // Worklist contains the list of function imported in this module, for which
993   // we will analyse the callees and may import further down the callgraph.
994   SmallVector<EdgeInfo, 128> Worklist;
995   GlobalsImporter GVI(Index, DefinedGVSummaries, IsPrevailing, ImportList,
996                       ExportLists);
997   FunctionImporter::ImportThresholdsTy ImportThresholds;
998 
999   // Populate the worklist with the import for the functions in the current
1000   // module
1001   for (const auto &GVSummary : DefinedGVSummaries) {
1002 #ifndef NDEBUG
1003     // FIXME: Change the GVSummaryMapTy to hold ValueInfo instead of GUID
1004     // so this map look up (and possibly others) can be avoided.
1005     auto VI = Index.getValueInfo(GVSummary.first);
1006 #endif
1007     if (!Index.isGlobalValueLive(GVSummary.second)) {
1008       LLVM_DEBUG(dbgs() << "Ignores Dead GUID: " << VI << "\n");
1009       continue;
1010     }
1011     auto *FuncSummary =
1012         dyn_cast<FunctionSummary>(GVSummary.second->getBaseObject());
1013     if (!FuncSummary)
1014       // Skip import for global variables
1015       continue;
1016     LLVM_DEBUG(dbgs() << "Initialize import for " << VI << "\n");
1017     computeImportForFunction(*FuncSummary, Index, ImportInstrLimit,
1018                              DefinedGVSummaries, IsPrevailing, Worklist, GVI,
1019                              ImportList, ExportLists, ImportThresholds);
1020   }
1021 
1022   // Process the newly imported functions and add callees to the worklist.
1023   while (!Worklist.empty()) {
1024     auto GVInfo = Worklist.pop_back_val();
1025     auto *Summary = std::get<0>(GVInfo);
1026     auto Threshold = std::get<1>(GVInfo);
1027 
1028     if (auto *FS = dyn_cast<FunctionSummary>(Summary))
1029       computeImportForFunction(*FS, Index, Threshold, DefinedGVSummaries,
1030                                IsPrevailing, Worklist, GVI, ImportList,
1031                                ExportLists, ImportThresholds);
1032   }
1033 
1034   // Print stats about functions considered but rejected for importing
1035   // when requested.
1036   if (PrintImportFailures) {
1037     dbgs() << "Missed imports into module " << ModName << "\n";
1038     for (auto &I : ImportThresholds) {
1039       auto &ProcessedThreshold = std::get<0>(I.second);
1040       auto &CalleeSummary = std::get<1>(I.second);
1041       auto &FailureInfo = std::get<2>(I.second);
1042       if (CalleeSummary)
1043         continue; // We are going to import.
1044       assert(FailureInfo);
1045       FunctionSummary *FS = nullptr;
1046       if (!FailureInfo->VI.getSummaryList().empty())
1047         FS = dyn_cast<FunctionSummary>(
1048             FailureInfo->VI.getSummaryList()[0]->getBaseObject());
1049       dbgs() << FailureInfo->VI
1050              << ": Reason = " << getFailureName(FailureInfo->Reason)
1051              << ", Threshold = " << ProcessedThreshold
1052              << ", Size = " << (FS ? (int)FS->instCount() : -1)
1053              << ", MaxHotness = " << getHotnessName(FailureInfo->MaxHotness)
1054              << ", Attempts = " << FailureInfo->Attempts << "\n";
1055     }
1056   }
1057 }
1058 
1059 #ifndef NDEBUG
1060 static bool isGlobalVarSummary(const ModuleSummaryIndex &Index, ValueInfo VI) {
1061   auto SL = VI.getSummaryList();
1062   return SL.empty()
1063              ? false
1064              : SL[0]->getSummaryKind() == GlobalValueSummary::GlobalVarKind;
1065 }
1066 
1067 static bool isGlobalVarSummary(const ModuleSummaryIndex &Index,
1068                                GlobalValue::GUID G) {
1069   if (const auto &VI = Index.getValueInfo(G))
1070     return isGlobalVarSummary(Index, VI);
1071   return false;
1072 }
1073 
1074 // Return the number of global variable summaries in ExportSet.
1075 static unsigned
1076 numGlobalVarSummaries(const ModuleSummaryIndex &Index,
1077                       FunctionImporter::ExportSetTy &ExportSet) {
1078   unsigned NumGVS = 0;
1079   for (auto &VI : ExportSet)
1080     if (isGlobalVarSummary(Index, VI.getGUID()))
1081       ++NumGVS;
1082   return NumGVS;
1083 }
1084 
1085 // Given ImportMap, return the number of global variable summaries and record
1086 // the number of defined function summaries as output parameter.
1087 static unsigned
1088 numGlobalVarSummaries(const ModuleSummaryIndex &Index,
1089                       const FunctionImporter::FunctionsToImportTy &ImportMap,
1090                       unsigned &DefinedFS) {
1091   unsigned NumGVS = 0;
1092   DefinedFS = 0;
1093   for (auto &[GUID, Type] : ImportMap) {
1094     if (isGlobalVarSummary(Index, GUID))
1095       ++NumGVS;
1096     else if (Type == GlobalValueSummary::Definition)
1097       ++DefinedFS;
1098   }
1099   return NumGVS;
1100 }
1101 #endif
1102 
1103 #ifndef NDEBUG
1104 static bool checkVariableImport(
1105     const ModuleSummaryIndex &Index,
1106     DenseMap<StringRef, FunctionImporter::ImportMapTy> &ImportLists,
1107     DenseMap<StringRef, FunctionImporter::ExportSetTy> &ExportLists) {
1108   DenseSet<GlobalValue::GUID> FlattenedImports;
1109 
1110   for (const auto &ImportPerModule : ImportLists)
1111     for (const auto &ExportPerModule : ImportPerModule.second.getImportMap())
1112       for (const auto &[GUID, Type] : ExportPerModule.second)
1113         FlattenedImports.insert(GUID);
1114 
1115   // Checks that all GUIDs of read/writeonly vars we see in export lists
1116   // are also in the import lists. Otherwise we my face linker undefs,
1117   // because readonly and writeonly vars are internalized in their
1118   // source modules. The exception would be if it has a linkage type indicating
1119   // that there may have been a copy existing in the importing module (e.g.
1120   // linkonce_odr). In that case we cannot accurately do this checking.
1121   auto IsReadOrWriteOnlyVarNeedingImporting = [&](StringRef ModulePath,
1122                                                   const ValueInfo &VI) {
1123     auto *GVS = dyn_cast_or_null<GlobalVarSummary>(
1124         Index.findSummaryInModule(VI, ModulePath));
1125     return GVS && (Index.isReadOnly(GVS) || Index.isWriteOnly(GVS)) &&
1126            !(GVS->linkage() == GlobalValue::AvailableExternallyLinkage ||
1127              GVS->linkage() == GlobalValue::WeakODRLinkage ||
1128              GVS->linkage() == GlobalValue::LinkOnceODRLinkage);
1129   };
1130 
1131   for (auto &ExportPerModule : ExportLists)
1132     for (auto &VI : ExportPerModule.second)
1133       if (!FlattenedImports.count(VI.getGUID()) &&
1134           IsReadOrWriteOnlyVarNeedingImporting(ExportPerModule.first, VI))
1135         return false;
1136 
1137   return true;
1138 }
1139 #endif
1140 
1141 /// Compute all the import and export for every module using the Index.
1142 void llvm::ComputeCrossModuleImport(
1143     const ModuleSummaryIndex &Index,
1144     const DenseMap<StringRef, GVSummaryMapTy> &ModuleToDefinedGVSummaries,
1145     function_ref<bool(GlobalValue::GUID, const GlobalValueSummary *)>
1146         isPrevailing,
1147     DenseMap<StringRef, FunctionImporter::ImportMapTy> &ImportLists,
1148     DenseMap<StringRef, FunctionImporter::ExportSetTy> &ExportLists) {
1149   auto MIS = ModuleImportsManager::create(isPrevailing, Index, &ExportLists);
1150   // For each module that has function defined, compute the import/export lists.
1151   for (const auto &DefinedGVSummaries : ModuleToDefinedGVSummaries) {
1152     auto &ImportList = ImportLists[DefinedGVSummaries.first];
1153     LLVM_DEBUG(dbgs() << "Computing import for Module '"
1154                       << DefinedGVSummaries.first << "'\n");
1155     MIS->computeImportForModule(DefinedGVSummaries.second,
1156                                 DefinedGVSummaries.first, ImportList);
1157   }
1158 
1159   // When computing imports we only added the variables and functions being
1160   // imported to the export list. We also need to mark any references and calls
1161   // they make as exported as well. We do this here, as it is more efficient
1162   // since we may import the same values multiple times into different modules
1163   // during the import computation.
1164   for (auto &ELI : ExportLists) {
1165     // `NewExports` tracks the VI that gets exported because the full definition
1166     // of its user/referencer gets exported.
1167     FunctionImporter::ExportSetTy NewExports;
1168     const auto &DefinedGVSummaries =
1169         ModuleToDefinedGVSummaries.lookup(ELI.first);
1170     for (auto &EI : ELI.second) {
1171       // Find the copy defined in the exporting module so that we can mark the
1172       // values it references in that specific definition as exported.
1173       // Below we will add all references and called values, without regard to
1174       // whether they are also defined in this module. We subsequently prune the
1175       // list to only include those defined in the exporting module, see comment
1176       // there as to why.
1177       auto DS = DefinedGVSummaries.find(EI.getGUID());
1178       // Anything marked exported during the import computation must have been
1179       // defined in the exporting module.
1180       assert(DS != DefinedGVSummaries.end());
1181       auto *S = DS->getSecond();
1182       S = S->getBaseObject();
1183       if (auto *GVS = dyn_cast<GlobalVarSummary>(S)) {
1184         // Export referenced functions and variables. We don't export/promote
1185         // objects referenced by writeonly variable initializer, because
1186         // we convert such variables initializers to "zeroinitializer".
1187         // See processGlobalForThinLTO.
1188         if (!Index.isWriteOnly(GVS))
1189           for (const auto &VI : GVS->refs())
1190             NewExports.insert(VI);
1191       } else {
1192         auto *FS = cast<FunctionSummary>(S);
1193         for (const auto &Edge : FS->calls())
1194           NewExports.insert(Edge.first);
1195         for (const auto &Ref : FS->refs())
1196           NewExports.insert(Ref);
1197       }
1198     }
1199     // Prune list computed above to only include values defined in the
1200     // exporting module. We do this after the above insertion since we may hit
1201     // the same ref/call target multiple times in above loop, and it is more
1202     // efficient to avoid a set lookup each time.
1203     for (auto EI = NewExports.begin(); EI != NewExports.end();) {
1204       if (!DefinedGVSummaries.count(EI->getGUID()))
1205         NewExports.erase(EI++);
1206       else
1207         ++EI;
1208     }
1209     ELI.second.insert(NewExports.begin(), NewExports.end());
1210   }
1211 
1212   assert(checkVariableImport(Index, ImportLists, ExportLists));
1213 #ifndef NDEBUG
1214   LLVM_DEBUG(dbgs() << "Import/Export lists for " << ImportLists.size()
1215                     << " modules:\n");
1216   for (auto &ModuleImports : ImportLists) {
1217     auto ModName = ModuleImports.first;
1218     auto &Exports = ExportLists[ModName];
1219     unsigned NumGVS = numGlobalVarSummaries(Index, Exports);
1220     LLVM_DEBUG(dbgs() << "* Module " << ModName << " exports "
1221                       << Exports.size() - NumGVS << " functions and " << NumGVS
1222                       << " vars. Imports from "
1223                       << ModuleImports.second.getImportMap().size()
1224                       << " modules.\n");
1225     for (const auto &Src : ModuleImports.second.getImportMap()) {
1226       auto SrcModName = Src.first;
1227       unsigned DefinedFS = 0;
1228       unsigned NumGVSPerMod =
1229           numGlobalVarSummaries(Index, Src.second, DefinedFS);
1230       LLVM_DEBUG(dbgs() << " - " << DefinedFS << " function definitions and "
1231                         << Src.second.size() - NumGVSPerMod - DefinedFS
1232                         << " function declarations imported from " << SrcModName
1233                         << "\n");
1234       LLVM_DEBUG(dbgs() << " - " << NumGVSPerMod
1235                         << " global vars imported from " << SrcModName << "\n");
1236     }
1237   }
1238 #endif
1239 }
1240 
1241 #ifndef NDEBUG
1242 static void dumpImportListForModule(const ModuleSummaryIndex &Index,
1243                                     StringRef ModulePath,
1244                                     FunctionImporter::ImportMapTy &ImportList) {
1245   LLVM_DEBUG(dbgs() << "* Module " << ModulePath << " imports from "
1246                     << ImportList.getImportMap().size() << " modules.\n");
1247   for (const auto &Src : ImportList.getImportMap()) {
1248     auto SrcModName = Src.first;
1249     unsigned DefinedFS = 0;
1250     unsigned NumGVSPerMod = numGlobalVarSummaries(Index, Src.second, DefinedFS);
1251     LLVM_DEBUG(dbgs() << " - " << DefinedFS << " function definitions and "
1252                       << Src.second.size() - DefinedFS - NumGVSPerMod
1253                       << " function declarations imported from " << SrcModName
1254                       << "\n");
1255     LLVM_DEBUG(dbgs() << " - " << NumGVSPerMod << " vars imported from "
1256                       << SrcModName << "\n");
1257   }
1258 }
1259 #endif
1260 
1261 /// Compute all the imports for the given module using the Index.
1262 ///
1263 /// \p isPrevailing is a callback that will be called with a global value's GUID
1264 /// and summary and should return whether the module corresponding to the
1265 /// summary contains the linker-prevailing copy of that value.
1266 ///
1267 /// \p ImportList will be populated with a map that can be passed to
1268 /// FunctionImporter::importFunctions() above (see description there).
1269 static void ComputeCrossModuleImportForModuleForTest(
1270     StringRef ModulePath,
1271     function_ref<bool(GlobalValue::GUID, const GlobalValueSummary *)>
1272         isPrevailing,
1273     const ModuleSummaryIndex &Index,
1274     FunctionImporter::ImportMapTy &ImportList) {
1275   // Collect the list of functions this module defines.
1276   // GUID -> Summary
1277   GVSummaryMapTy FunctionSummaryMap;
1278   Index.collectDefinedFunctionsForModule(ModulePath, FunctionSummaryMap);
1279 
1280   // Compute the import list for this module.
1281   LLVM_DEBUG(dbgs() << "Computing import for Module '" << ModulePath << "'\n");
1282   auto MIS = ModuleImportsManager::create(isPrevailing, Index);
1283   MIS->computeImportForModule(FunctionSummaryMap, ModulePath, ImportList);
1284 
1285 #ifndef NDEBUG
1286   dumpImportListForModule(Index, ModulePath, ImportList);
1287 #endif
1288 }
1289 
1290 /// Mark all external summaries in \p Index for import into the given module.
1291 /// Used for testing the case of distributed builds using a distributed index.
1292 ///
1293 /// \p ImportList will be populated with a map that can be passed to
1294 /// FunctionImporter::importFunctions() above (see description there).
1295 static void ComputeCrossModuleImportForModuleFromIndexForTest(
1296     StringRef ModulePath, const ModuleSummaryIndex &Index,
1297     FunctionImporter::ImportMapTy &ImportList) {
1298   for (const auto &GlobalList : Index) {
1299     // Ignore entries for undefined references.
1300     if (GlobalList.second.SummaryList.empty())
1301       continue;
1302 
1303     auto GUID = GlobalList.first;
1304     assert(GlobalList.second.SummaryList.size() == 1 &&
1305            "Expected individual combined index to have one summary per GUID");
1306     auto &Summary = GlobalList.second.SummaryList[0];
1307     // Skip the summaries for the importing module. These are included to
1308     // e.g. record required linkage changes.
1309     if (Summary->modulePath() == ModulePath)
1310       continue;
1311     // Add an entry to provoke importing by thinBackend.
1312     ImportList.addGUID(Summary->modulePath(), GUID, Summary->importType());
1313   }
1314 #ifndef NDEBUG
1315   dumpImportListForModule(Index, ModulePath, ImportList);
1316 #endif
1317 }
1318 
1319 // For SamplePGO, the indirect call targets for local functions will
1320 // have its original name annotated in profile. We try to find the
1321 // corresponding PGOFuncName as the GUID, and fix up the edges
1322 // accordingly.
1323 void updateValueInfoForIndirectCalls(ModuleSummaryIndex &Index,
1324                                      FunctionSummary *FS) {
1325   for (auto &EI : FS->mutableCalls()) {
1326     if (!EI.first.getSummaryList().empty())
1327       continue;
1328     auto GUID = Index.getGUIDFromOriginalID(EI.first.getGUID());
1329     if (GUID == 0)
1330       continue;
1331     // Update the edge to point directly to the correct GUID.
1332     auto VI = Index.getValueInfo(GUID);
1333     if (llvm::any_of(
1334             VI.getSummaryList(),
1335             [&](const std::unique_ptr<GlobalValueSummary> &SummaryPtr) {
1336               // The mapping from OriginalId to GUID may return a GUID
1337               // that corresponds to a static variable. Filter it out here.
1338               // This can happen when
1339               // 1) There is a call to a library function which is not defined
1340               // in the index.
1341               // 2) There is a static variable with the  OriginalGUID identical
1342               // to the GUID of the library function in 1);
1343               // When this happens the static variable in 2) will be found,
1344               // which needs to be filtered out.
1345               return SummaryPtr->getSummaryKind() ==
1346                      GlobalValueSummary::GlobalVarKind;
1347             }))
1348       continue;
1349     EI.first = VI;
1350   }
1351 }
1352 
1353 void llvm::updateIndirectCalls(ModuleSummaryIndex &Index) {
1354   for (const auto &Entry : Index) {
1355     for (const auto &S : Entry.second.SummaryList) {
1356       if (auto *FS = dyn_cast<FunctionSummary>(S.get()))
1357         updateValueInfoForIndirectCalls(Index, FS);
1358     }
1359   }
1360 }
1361 
1362 void llvm::computeDeadSymbolsAndUpdateIndirectCalls(
1363     ModuleSummaryIndex &Index,
1364     const DenseSet<GlobalValue::GUID> &GUIDPreservedSymbols,
1365     function_ref<PrevailingType(GlobalValue::GUID)> isPrevailing) {
1366   assert(!Index.withGlobalValueDeadStripping());
1367   if (!ComputeDead ||
1368       // Don't do anything when nothing is live, this is friendly with tests.
1369       GUIDPreservedSymbols.empty()) {
1370     // Still need to update indirect calls.
1371     updateIndirectCalls(Index);
1372     return;
1373   }
1374   unsigned LiveSymbols = 0;
1375   SmallVector<ValueInfo, 128> Worklist;
1376   Worklist.reserve(GUIDPreservedSymbols.size() * 2);
1377   for (auto GUID : GUIDPreservedSymbols) {
1378     ValueInfo VI = Index.getValueInfo(GUID);
1379     if (!VI)
1380       continue;
1381     for (const auto &S : VI.getSummaryList())
1382       S->setLive(true);
1383   }
1384 
1385   // Add values flagged in the index as live roots to the worklist.
1386   for (const auto &Entry : Index) {
1387     auto VI = Index.getValueInfo(Entry);
1388     for (const auto &S : Entry.second.SummaryList) {
1389       if (auto *FS = dyn_cast<FunctionSummary>(S.get()))
1390         updateValueInfoForIndirectCalls(Index, FS);
1391       if (S->isLive()) {
1392         LLVM_DEBUG(dbgs() << "Live root: " << VI << "\n");
1393         Worklist.push_back(VI);
1394         ++LiveSymbols;
1395         break;
1396       }
1397     }
1398   }
1399 
1400   // Make value live and add it to the worklist if it was not live before.
1401   auto visit = [&](ValueInfo VI, bool IsAliasee) {
1402     // FIXME: If we knew which edges were created for indirect call profiles,
1403     // we could skip them here. Any that are live should be reached via
1404     // other edges, e.g. reference edges. Otherwise, using a profile collected
1405     // on a slightly different binary might provoke preserving, importing
1406     // and ultimately promoting calls to functions not linked into this
1407     // binary, which increases the binary size unnecessarily. Note that
1408     // if this code changes, the importer needs to change so that edges
1409     // to functions marked dead are skipped.
1410 
1411     if (llvm::any_of(VI.getSummaryList(),
1412                      [](const std::unique_ptr<llvm::GlobalValueSummary> &S) {
1413                        return S->isLive();
1414                      }))
1415       return;
1416 
1417     // We only keep live symbols that are known to be non-prevailing if any are
1418     // available_externally, linkonceodr, weakodr. Those symbols are discarded
1419     // later in the EliminateAvailableExternally pass and setting them to
1420     // not-live could break downstreams users of liveness information (PR36483)
1421     // or limit optimization opportunities.
1422     if (isPrevailing(VI.getGUID()) == PrevailingType::No) {
1423       bool KeepAliveLinkage = false;
1424       bool Interposable = false;
1425       for (const auto &S : VI.getSummaryList()) {
1426         if (S->linkage() == GlobalValue::AvailableExternallyLinkage ||
1427             S->linkage() == GlobalValue::WeakODRLinkage ||
1428             S->linkage() == GlobalValue::LinkOnceODRLinkage)
1429           KeepAliveLinkage = true;
1430         else if (GlobalValue::isInterposableLinkage(S->linkage()))
1431           Interposable = true;
1432       }
1433 
1434       if (!IsAliasee) {
1435         if (!KeepAliveLinkage)
1436           return;
1437 
1438         if (Interposable)
1439           report_fatal_error(
1440               "Interposable and available_externally/linkonce_odr/weak_odr "
1441               "symbol");
1442       }
1443     }
1444 
1445     for (const auto &S : VI.getSummaryList())
1446       S->setLive(true);
1447     ++LiveSymbols;
1448     Worklist.push_back(VI);
1449   };
1450 
1451   while (!Worklist.empty()) {
1452     auto VI = Worklist.pop_back_val();
1453     for (const auto &Summary : VI.getSummaryList()) {
1454       if (auto *AS = dyn_cast<AliasSummary>(Summary.get())) {
1455         // If this is an alias, visit the aliasee VI to ensure that all copies
1456         // are marked live and it is added to the worklist for further
1457         // processing of its references.
1458         visit(AS->getAliaseeVI(), true);
1459         continue;
1460       }
1461       for (auto Ref : Summary->refs())
1462         visit(Ref, false);
1463       if (auto *FS = dyn_cast<FunctionSummary>(Summary.get()))
1464         for (auto Call : FS->calls())
1465           visit(Call.first, false);
1466     }
1467   }
1468   Index.setWithGlobalValueDeadStripping();
1469 
1470   unsigned DeadSymbols = Index.size() - LiveSymbols;
1471   LLVM_DEBUG(dbgs() << LiveSymbols << " symbols Live, and " << DeadSymbols
1472                     << " symbols Dead \n");
1473   NumDeadSymbols += DeadSymbols;
1474   NumLiveSymbols += LiveSymbols;
1475 }
1476 
1477 // Compute dead symbols and propagate constants in combined index.
1478 void llvm::computeDeadSymbolsWithConstProp(
1479     ModuleSummaryIndex &Index,
1480     const DenseSet<GlobalValue::GUID> &GUIDPreservedSymbols,
1481     function_ref<PrevailingType(GlobalValue::GUID)> isPrevailing,
1482     bool ImportEnabled) {
1483   computeDeadSymbolsAndUpdateIndirectCalls(Index, GUIDPreservedSymbols,
1484                                            isPrevailing);
1485   if (ImportEnabled)
1486     Index.propagateAttributes(GUIDPreservedSymbols);
1487 }
1488 
1489 /// Compute the set of summaries needed for a ThinLTO backend compilation of
1490 /// \p ModulePath.
1491 void llvm::gatherImportedSummariesForModule(
1492     StringRef ModulePath,
1493     const DenseMap<StringRef, GVSummaryMapTy> &ModuleToDefinedGVSummaries,
1494     const FunctionImporter::ImportMapTy &ImportList,
1495     ModuleToSummariesForIndexTy &ModuleToSummariesForIndex,
1496     GVSummaryPtrSet &DecSummaries) {
1497   // Include all summaries from the importing module.
1498   ModuleToSummariesForIndex[std::string(ModulePath)] =
1499       ModuleToDefinedGVSummaries.lookup(ModulePath);
1500   // Include summaries for imports.
1501   for (const auto &ILI : ImportList.getImportMap()) {
1502     auto &SummariesForIndex = ModuleToSummariesForIndex[std::string(ILI.first)];
1503 
1504     const auto &DefinedGVSummaries =
1505         ModuleToDefinedGVSummaries.lookup(ILI.first);
1506     for (const auto &[GUID, Type] : ILI.second) {
1507       const auto &DS = DefinedGVSummaries.find(GUID);
1508       assert(DS != DefinedGVSummaries.end() &&
1509              "Expected a defined summary for imported global value");
1510       if (Type == GlobalValueSummary::Declaration)
1511         DecSummaries.insert(DS->second);
1512 
1513       SummariesForIndex[GUID] = DS->second;
1514     }
1515   }
1516 }
1517 
1518 /// Emit the files \p ModulePath will import from into \p OutputFilename.
1519 std::error_code llvm::EmitImportsFiles(
1520     StringRef ModulePath, StringRef OutputFilename,
1521     const ModuleToSummariesForIndexTy &ModuleToSummariesForIndex) {
1522   std::error_code EC;
1523   raw_fd_ostream ImportsOS(OutputFilename, EC, sys::fs::OpenFlags::OF_Text);
1524   if (EC)
1525     return EC;
1526   for (const auto &ILI : ModuleToSummariesForIndex)
1527     // The ModuleToSummariesForIndex map includes an entry for the current
1528     // Module (needed for writing out the index files). We don't want to
1529     // include it in the imports file, however, so filter it out.
1530     if (ILI.first != ModulePath)
1531       ImportsOS << ILI.first << "\n";
1532   return std::error_code();
1533 }
1534 
1535 bool llvm::convertToDeclaration(GlobalValue &GV) {
1536   LLVM_DEBUG(dbgs() << "Converting to a declaration: `" << GV.getName()
1537                     << "\n");
1538   if (Function *F = dyn_cast<Function>(&GV)) {
1539     F->deleteBody();
1540     F->clearMetadata();
1541     F->setComdat(nullptr);
1542   } else if (GlobalVariable *V = dyn_cast<GlobalVariable>(&GV)) {
1543     V->setInitializer(nullptr);
1544     V->setLinkage(GlobalValue::ExternalLinkage);
1545     V->clearMetadata();
1546     V->setComdat(nullptr);
1547   } else {
1548     GlobalValue *NewGV;
1549     if (GV.getValueType()->isFunctionTy())
1550       NewGV =
1551           Function::Create(cast<FunctionType>(GV.getValueType()),
1552                            GlobalValue::ExternalLinkage, GV.getAddressSpace(),
1553                            "", GV.getParent());
1554     else
1555       NewGV =
1556           new GlobalVariable(*GV.getParent(), GV.getValueType(),
1557                              /*isConstant*/ false, GlobalValue::ExternalLinkage,
1558                              /*init*/ nullptr, "",
1559                              /*insertbefore*/ nullptr, GV.getThreadLocalMode(),
1560                              GV.getType()->getAddressSpace());
1561     NewGV->takeName(&GV);
1562     GV.replaceAllUsesWith(NewGV);
1563     return false;
1564   }
1565   if (!GV.isImplicitDSOLocal())
1566     GV.setDSOLocal(false);
1567   return true;
1568 }
1569 
1570 void llvm::thinLTOFinalizeInModule(Module &TheModule,
1571                                    const GVSummaryMapTy &DefinedGlobals,
1572                                    bool PropagateAttrs) {
1573   DenseSet<Comdat *> NonPrevailingComdats;
1574   auto FinalizeInModule = [&](GlobalValue &GV, bool Propagate = false) {
1575     // See if the global summary analysis computed a new resolved linkage.
1576     const auto &GS = DefinedGlobals.find(GV.getGUID());
1577     if (GS == DefinedGlobals.end())
1578       return;
1579 
1580     if (Propagate)
1581       if (FunctionSummary *FS = dyn_cast<FunctionSummary>(GS->second)) {
1582         if (Function *F = dyn_cast<Function>(&GV)) {
1583           // TODO: propagate ReadNone and ReadOnly.
1584           if (FS->fflags().ReadNone && !F->doesNotAccessMemory())
1585             F->setDoesNotAccessMemory();
1586 
1587           if (FS->fflags().ReadOnly && !F->onlyReadsMemory())
1588             F->setOnlyReadsMemory();
1589 
1590           if (FS->fflags().NoRecurse && !F->doesNotRecurse())
1591             F->setDoesNotRecurse();
1592 
1593           if (FS->fflags().NoUnwind && !F->doesNotThrow())
1594             F->setDoesNotThrow();
1595         }
1596       }
1597 
1598     auto NewLinkage = GS->second->linkage();
1599     if (GlobalValue::isLocalLinkage(GV.getLinkage()) ||
1600         // Don't internalize anything here, because the code below
1601         // lacks necessary correctness checks. Leave this job to
1602         // LLVM 'internalize' pass.
1603         GlobalValue::isLocalLinkage(NewLinkage) ||
1604         // In case it was dead and already converted to declaration.
1605         GV.isDeclaration())
1606       return;
1607 
1608     // Set the potentially more constraining visibility computed from summaries.
1609     // The DefaultVisibility condition is because older GlobalValueSummary does
1610     // not record DefaultVisibility and we don't want to change protected/hidden
1611     // to default.
1612     if (GS->second->getVisibility() != GlobalValue::DefaultVisibility)
1613       GV.setVisibility(GS->second->getVisibility());
1614 
1615     if (NewLinkage == GV.getLinkage())
1616       return;
1617 
1618     // Check for a non-prevailing def that has interposable linkage
1619     // (e.g. non-odr weak or linkonce). In that case we can't simply
1620     // convert to available_externally, since it would lose the
1621     // interposable property and possibly get inlined. Simply drop
1622     // the definition in that case.
1623     if (GlobalValue::isAvailableExternallyLinkage(NewLinkage) &&
1624         GlobalValue::isInterposableLinkage(GV.getLinkage())) {
1625       if (!convertToDeclaration(GV))
1626         // FIXME: Change this to collect replaced GVs and later erase
1627         // them from the parent module once thinLTOResolvePrevailingGUID is
1628         // changed to enable this for aliases.
1629         llvm_unreachable("Expected GV to be converted");
1630     } else {
1631       // If all copies of the original symbol had global unnamed addr and
1632       // linkonce_odr linkage, or if all of them had local unnamed addr linkage
1633       // and are constants, then it should be an auto hide symbol. In that case
1634       // the thin link would have marked it as CanAutoHide. Add hidden
1635       // visibility to the symbol to preserve the property.
1636       if (NewLinkage == GlobalValue::WeakODRLinkage &&
1637           GS->second->canAutoHide()) {
1638         assert(GV.canBeOmittedFromSymbolTable());
1639         GV.setVisibility(GlobalValue::HiddenVisibility);
1640       }
1641 
1642       LLVM_DEBUG(dbgs() << "ODR fixing up linkage for `" << GV.getName()
1643                         << "` from " << GV.getLinkage() << " to " << NewLinkage
1644                         << "\n");
1645       GV.setLinkage(NewLinkage);
1646     }
1647     // Remove declarations from comdats, including available_externally
1648     // as this is a declaration for the linker, and will be dropped eventually.
1649     // It is illegal for comdats to contain declarations.
1650     auto *GO = dyn_cast_or_null<GlobalObject>(&GV);
1651     if (GO && GO->isDeclarationForLinker() && GO->hasComdat()) {
1652       if (GO->getComdat()->getName() == GO->getName())
1653         NonPrevailingComdats.insert(GO->getComdat());
1654       GO->setComdat(nullptr);
1655     }
1656   };
1657 
1658   // Process functions and global now
1659   for (auto &GV : TheModule)
1660     FinalizeInModule(GV, PropagateAttrs);
1661   for (auto &GV : TheModule.globals())
1662     FinalizeInModule(GV);
1663   for (auto &GV : TheModule.aliases())
1664     FinalizeInModule(GV);
1665 
1666   // For a non-prevailing comdat, all its members must be available_externally.
1667   // FinalizeInModule has handled non-local-linkage GlobalValues. Here we handle
1668   // local linkage GlobalValues.
1669   if (NonPrevailingComdats.empty())
1670     return;
1671   for (auto &GO : TheModule.global_objects()) {
1672     if (auto *C = GO.getComdat(); C && NonPrevailingComdats.count(C)) {
1673       GO.setComdat(nullptr);
1674       GO.setLinkage(GlobalValue::AvailableExternallyLinkage);
1675     }
1676   }
1677   bool Changed;
1678   do {
1679     Changed = false;
1680     // If an alias references a GlobalValue in a non-prevailing comdat, change
1681     // it to available_externally. For simplicity we only handle GlobalValue and
1682     // ConstantExpr with a base object. ConstantExpr without a base object is
1683     // unlikely used in a COMDAT.
1684     for (auto &GA : TheModule.aliases()) {
1685       if (GA.hasAvailableExternallyLinkage())
1686         continue;
1687       GlobalObject *Obj = GA.getAliaseeObject();
1688       assert(Obj && "aliasee without an base object is unimplemented");
1689       if (Obj->hasAvailableExternallyLinkage()) {
1690         GA.setLinkage(GlobalValue::AvailableExternallyLinkage);
1691         Changed = true;
1692       }
1693     }
1694   } while (Changed);
1695 }
1696 
1697 /// Run internalization on \p TheModule based on symmary analysis.
1698 void llvm::thinLTOInternalizeModule(Module &TheModule,
1699                                     const GVSummaryMapTy &DefinedGlobals) {
1700   // Declare a callback for the internalize pass that will ask for every
1701   // candidate GlobalValue if it can be internalized or not.
1702   auto MustPreserveGV = [&](const GlobalValue &GV) -> bool {
1703     // It may be the case that GV is on a chain of an ifunc, its alias and
1704     // subsequent aliases. In this case, the summary for the value is not
1705     // available.
1706     if (isa<GlobalIFunc>(&GV) ||
1707         (isa<GlobalAlias>(&GV) &&
1708          isa<GlobalIFunc>(cast<GlobalAlias>(&GV)->getAliaseeObject())))
1709       return true;
1710 
1711     // Lookup the linkage recorded in the summaries during global analysis.
1712     auto GS = DefinedGlobals.find(GV.getGUID());
1713     if (GS == DefinedGlobals.end()) {
1714       // Must have been promoted (possibly conservatively). Find original
1715       // name so that we can access the correct summary and see if it can
1716       // be internalized again.
1717       // FIXME: Eventually we should control promotion instead of promoting
1718       // and internalizing again.
1719       StringRef OrigName =
1720           ModuleSummaryIndex::getOriginalNameBeforePromote(GV.getName());
1721       std::string OrigId = GlobalValue::getGlobalIdentifier(
1722           OrigName, GlobalValue::InternalLinkage,
1723           TheModule.getSourceFileName());
1724       GS = DefinedGlobals.find(GlobalValue::getGUID(OrigId));
1725       if (GS == DefinedGlobals.end()) {
1726         // Also check the original non-promoted non-globalized name. In some
1727         // cases a preempted weak value is linked in as a local copy because
1728         // it is referenced by an alias (IRLinker::linkGlobalValueProto).
1729         // In that case, since it was originally not a local value, it was
1730         // recorded in the index using the original name.
1731         // FIXME: This may not be needed once PR27866 is fixed.
1732         GS = DefinedGlobals.find(GlobalValue::getGUID(OrigName));
1733         assert(GS != DefinedGlobals.end());
1734       }
1735     }
1736     return !GlobalValue::isLocalLinkage(GS->second->linkage());
1737   };
1738 
1739   // FIXME: See if we can just internalize directly here via linkage changes
1740   // based on the index, rather than invoking internalizeModule.
1741   internalizeModule(TheModule, MustPreserveGV);
1742 }
1743 
1744 /// Make alias a clone of its aliasee.
1745 static Function *replaceAliasWithAliasee(Module *SrcModule, GlobalAlias *GA) {
1746   Function *Fn = cast<Function>(GA->getAliaseeObject());
1747 
1748   ValueToValueMapTy VMap;
1749   Function *NewFn = CloneFunction(Fn, VMap);
1750   // Clone should use the original alias's linkage, visibility and name, and we
1751   // ensure all uses of alias instead use the new clone (casted if necessary).
1752   NewFn->setLinkage(GA->getLinkage());
1753   NewFn->setVisibility(GA->getVisibility());
1754   GA->replaceAllUsesWith(NewFn);
1755   NewFn->takeName(GA);
1756   return NewFn;
1757 }
1758 
1759 // Internalize values that we marked with specific attribute
1760 // in processGlobalForThinLTO.
1761 static void internalizeGVsAfterImport(Module &M) {
1762   for (auto &GV : M.globals())
1763     // Skip GVs which have been converted to declarations
1764     // by dropDeadSymbols.
1765     if (!GV.isDeclaration() && GV.hasAttribute("thinlto-internalize")) {
1766       GV.setLinkage(GlobalValue::InternalLinkage);
1767       GV.setVisibility(GlobalValue::DefaultVisibility);
1768     }
1769 }
1770 
1771 // Automatically import functions in Module \p DestModule based on the summaries
1772 // index.
1773 Expected<bool> FunctionImporter::importFunctions(
1774     Module &DestModule, const FunctionImporter::ImportMapTy &ImportList) {
1775   LLVM_DEBUG(dbgs() << "Starting import for Module "
1776                     << DestModule.getModuleIdentifier() << "\n");
1777   unsigned ImportedCount = 0, ImportedGVCount = 0;
1778 
1779   IRMover Mover(DestModule);
1780 
1781   auto getImportType = [&](const FunctionsToImportTy &GUIDToImportType,
1782                            GlobalValue::GUID GUID)
1783       -> std::optional<GlobalValueSummary::ImportKind> {
1784     auto Iter = GUIDToImportType.find(GUID);
1785     if (Iter == GUIDToImportType.end())
1786       return std::nullopt;
1787     return Iter->second;
1788   };
1789 
1790   // Do the actual import of functions now, one Module at a time
1791   for (const auto &Name : ImportList.getSourceModules()) {
1792     // Get the module for the import
1793     const auto &FunctionsToImportPerModule =
1794         ImportList.getImportMap().find(Name);
1795     assert(FunctionsToImportPerModule != ImportList.getImportMap().end());
1796     Expected<std::unique_ptr<Module>> SrcModuleOrErr = ModuleLoader(Name);
1797     if (!SrcModuleOrErr)
1798       return SrcModuleOrErr.takeError();
1799     std::unique_ptr<Module> SrcModule = std::move(*SrcModuleOrErr);
1800     assert(&DestModule.getContext() == &SrcModule->getContext() &&
1801            "Context mismatch");
1802 
1803     // If modules were created with lazy metadata loading, materialize it
1804     // now, before linking it (otherwise this will be a noop).
1805     if (Error Err = SrcModule->materializeMetadata())
1806       return std::move(Err);
1807 
1808     auto &ImportGUIDs = FunctionsToImportPerModule->second;
1809 
1810     // Find the globals to import
1811     SetVector<GlobalValue *> GlobalsToImport;
1812     for (Function &F : *SrcModule) {
1813       if (!F.hasName())
1814         continue;
1815       auto GUID = F.getGUID();
1816       auto MaybeImportType = getImportType(ImportGUIDs, GUID);
1817       bool ImportDefinition = MaybeImportType == GlobalValueSummary::Definition;
1818 
1819       LLVM_DEBUG(dbgs() << (MaybeImportType ? "Is" : "Not")
1820                         << " importing function"
1821                         << (ImportDefinition
1822                                 ? " definition "
1823                                 : (MaybeImportType ? " declaration " : " "))
1824                         << GUID << " " << F.getName() << " from "
1825                         << SrcModule->getSourceFileName() << "\n");
1826       if (ImportDefinition) {
1827         if (Error Err = F.materialize())
1828           return std::move(Err);
1829         // MemProf should match function's definition and summary,
1830         // 'thinlto_src_module' is needed.
1831         if (EnableImportMetadata || EnableMemProfContextDisambiguation) {
1832           // Add 'thinlto_src_module' and 'thinlto_src_file' metadata for
1833           // statistics and debugging.
1834           F.setMetadata(
1835               "thinlto_src_module",
1836               MDNode::get(DestModule.getContext(),
1837                           {MDString::get(DestModule.getContext(),
1838                                          SrcModule->getModuleIdentifier())}));
1839           F.setMetadata(
1840               "thinlto_src_file",
1841               MDNode::get(DestModule.getContext(),
1842                           {MDString::get(DestModule.getContext(),
1843                                          SrcModule->getSourceFileName())}));
1844         }
1845         GlobalsToImport.insert(&F);
1846       }
1847     }
1848     for (GlobalVariable &GV : SrcModule->globals()) {
1849       if (!GV.hasName())
1850         continue;
1851       auto GUID = GV.getGUID();
1852       auto MaybeImportType = getImportType(ImportGUIDs, GUID);
1853       bool ImportDefinition = MaybeImportType == GlobalValueSummary::Definition;
1854 
1855       LLVM_DEBUG(dbgs() << (MaybeImportType ? "Is" : "Not")
1856                         << " importing global"
1857                         << (ImportDefinition
1858                                 ? " definition "
1859                                 : (MaybeImportType ? " declaration " : " "))
1860                         << GUID << " " << GV.getName() << " from "
1861                         << SrcModule->getSourceFileName() << "\n");
1862       if (ImportDefinition) {
1863         if (Error Err = GV.materialize())
1864           return std::move(Err);
1865         ImportedGVCount += GlobalsToImport.insert(&GV);
1866       }
1867     }
1868     for (GlobalAlias &GA : SrcModule->aliases()) {
1869       if (!GA.hasName() || isa<GlobalIFunc>(GA.getAliaseeObject()))
1870         continue;
1871       auto GUID = GA.getGUID();
1872       auto MaybeImportType = getImportType(ImportGUIDs, GUID);
1873       bool ImportDefinition = MaybeImportType == GlobalValueSummary::Definition;
1874 
1875       LLVM_DEBUG(dbgs() << (MaybeImportType ? "Is" : "Not")
1876                         << " importing alias"
1877                         << (ImportDefinition
1878                                 ? " definition "
1879                                 : (MaybeImportType ? " declaration " : " "))
1880                         << GUID << " " << GA.getName() << " from "
1881                         << SrcModule->getSourceFileName() << "\n");
1882       if (ImportDefinition) {
1883         if (Error Err = GA.materialize())
1884           return std::move(Err);
1885         // Import alias as a copy of its aliasee.
1886         GlobalObject *GO = GA.getAliaseeObject();
1887         if (Error Err = GO->materialize())
1888           return std::move(Err);
1889         auto *Fn = replaceAliasWithAliasee(SrcModule.get(), &GA);
1890         LLVM_DEBUG(dbgs() << "Is importing aliasee fn " << GO->getGUID() << " "
1891                           << GO->getName() << " from "
1892                           << SrcModule->getSourceFileName() << "\n");
1893         if (EnableImportMetadata || EnableMemProfContextDisambiguation) {
1894           // Add 'thinlto_src_module' and 'thinlto_src_file' metadata for
1895           // statistics and debugging.
1896           Fn->setMetadata(
1897               "thinlto_src_module",
1898               MDNode::get(DestModule.getContext(),
1899                           {MDString::get(DestModule.getContext(),
1900                                          SrcModule->getModuleIdentifier())}));
1901           Fn->setMetadata(
1902               "thinlto_src_file",
1903               MDNode::get(DestModule.getContext(),
1904                           {MDString::get(DestModule.getContext(),
1905                                          SrcModule->getSourceFileName())}));
1906         }
1907         GlobalsToImport.insert(Fn);
1908       }
1909     }
1910 
1911     // Upgrade debug info after we're done materializing all the globals and we
1912     // have loaded all the required metadata!
1913     UpgradeDebugInfo(*SrcModule);
1914 
1915     // Set the partial sample profile ratio in the profile summary module flag
1916     // of the imported source module, if applicable, so that the profile summary
1917     // module flag will match with that of the destination module when it's
1918     // imported.
1919     SrcModule->setPartialSampleProfileRatio(Index);
1920 
1921     // Link in the specified functions.
1922     if (renameModuleForThinLTO(*SrcModule, Index, ClearDSOLocalOnDeclarations,
1923                                &GlobalsToImport))
1924       return true;
1925 
1926     if (PrintImports) {
1927       for (const auto *GV : GlobalsToImport)
1928         dbgs() << DestModule.getSourceFileName() << ": Import " << GV->getName()
1929                << " from " << SrcModule->getSourceFileName() << "\n";
1930     }
1931 
1932     if (Error Err = Mover.move(std::move(SrcModule),
1933                                GlobalsToImport.getArrayRef(), nullptr,
1934                                /*IsPerformingImport=*/true))
1935       return createStringError(errc::invalid_argument,
1936                                Twine("Function Import: link error: ") +
1937                                    toString(std::move(Err)));
1938 
1939     ImportedCount += GlobalsToImport.size();
1940     NumImportedModules++;
1941   }
1942 
1943   internalizeGVsAfterImport(DestModule);
1944 
1945   NumImportedFunctions += (ImportedCount - ImportedGVCount);
1946   NumImportedGlobalVars += ImportedGVCount;
1947 
1948   // TODO: Print counters for definitions and declarations in the debugging log.
1949   LLVM_DEBUG(dbgs() << "Imported " << ImportedCount - ImportedGVCount
1950                     << " functions for Module "
1951                     << DestModule.getModuleIdentifier() << "\n");
1952   LLVM_DEBUG(dbgs() << "Imported " << ImportedGVCount
1953                     << " global variables for Module "
1954                     << DestModule.getModuleIdentifier() << "\n");
1955   return ImportedCount;
1956 }
1957 
1958 static bool doImportingForModuleForTest(
1959     Module &M, function_ref<bool(GlobalValue::GUID, const GlobalValueSummary *)>
1960                    isPrevailing) {
1961   if (SummaryFile.empty())
1962     report_fatal_error("error: -function-import requires -summary-file\n");
1963   Expected<std::unique_ptr<ModuleSummaryIndex>> IndexPtrOrErr =
1964       getModuleSummaryIndexForFile(SummaryFile);
1965   if (!IndexPtrOrErr) {
1966     logAllUnhandledErrors(IndexPtrOrErr.takeError(), errs(),
1967                           "Error loading file '" + SummaryFile + "': ");
1968     return false;
1969   }
1970   std::unique_ptr<ModuleSummaryIndex> Index = std::move(*IndexPtrOrErr);
1971 
1972   // First step is collecting the import list.
1973   FunctionImporter::ImportMapTy ImportList;
1974   // If requested, simply import all functions in the index. This is used
1975   // when testing distributed backend handling via the opt tool, when
1976   // we have distributed indexes containing exactly the summaries to import.
1977   if (ImportAllIndex)
1978     ComputeCrossModuleImportForModuleFromIndexForTest(M.getModuleIdentifier(),
1979                                                       *Index, ImportList);
1980   else
1981     ComputeCrossModuleImportForModuleForTest(M.getModuleIdentifier(),
1982                                              isPrevailing, *Index, ImportList);
1983 
1984   // Conservatively mark all internal values as promoted. This interface is
1985   // only used when doing importing via the function importing pass. The pass
1986   // is only enabled when testing importing via the 'opt' tool, which does
1987   // not do the ThinLink that would normally determine what values to promote.
1988   for (auto &I : *Index) {
1989     for (auto &S : I.second.SummaryList) {
1990       if (GlobalValue::isLocalLinkage(S->linkage()))
1991         S->setLinkage(GlobalValue::ExternalLinkage);
1992     }
1993   }
1994 
1995   // Next we need to promote to global scope and rename any local values that
1996   // are potentially exported to other modules.
1997   if (renameModuleForThinLTO(M, *Index, /*ClearDSOLocalOnDeclarations=*/false,
1998                              /*GlobalsToImport=*/nullptr)) {
1999     errs() << "Error renaming module\n";
2000     return true;
2001   }
2002 
2003   // Perform the import now.
2004   auto ModuleLoader = [&M](StringRef Identifier) {
2005     return loadFile(std::string(Identifier), M.getContext());
2006   };
2007   FunctionImporter Importer(*Index, ModuleLoader,
2008                             /*ClearDSOLocalOnDeclarations=*/false);
2009   Expected<bool> Result = Importer.importFunctions(M, ImportList);
2010 
2011   // FIXME: Probably need to propagate Errors through the pass manager.
2012   if (!Result) {
2013     logAllUnhandledErrors(Result.takeError(), errs(),
2014                           "Error importing module: ");
2015     return true;
2016   }
2017 
2018   return true;
2019 }
2020 
2021 PreservedAnalyses FunctionImportPass::run(Module &M,
2022                                           ModuleAnalysisManager &AM) {
2023   // This is only used for testing the function import pass via opt, where we
2024   // don't have prevailing information from the LTO context available, so just
2025   // conservatively assume everything is prevailing (which is fine for the very
2026   // limited use of prevailing checking in this pass).
2027   auto isPrevailing = [](GlobalValue::GUID, const GlobalValueSummary *) {
2028     return true;
2029   };
2030   if (!doImportingForModuleForTest(M, isPrevailing))
2031     return PreservedAnalyses::all();
2032 
2033   return PreservedAnalyses::none();
2034 }
2035