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