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