1 //===- FunctionImport.cpp - ThinLTO Summary-based Function Import ---------===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
10 // This file implements Function import based on summaries.
11 //
12 //===----------------------------------------------------------------------===//
13 
14 #include "llvm/Transforms/IPO/FunctionImport.h"
15 
16 #include "llvm/ADT/SmallVector.h"
17 #include "llvm/ADT/Statistic.h"
18 #include "llvm/ADT/StringSet.h"
19 #include "llvm/ADT/Triple.h"
20 #include "llvm/IR/AutoUpgrade.h"
21 #include "llvm/IR/DiagnosticPrinter.h"
22 #include "llvm/IR/IntrinsicInst.h"
23 #include "llvm/IR/Module.h"
24 #include "llvm/IRReader/IRReader.h"
25 #include "llvm/Linker/Linker.h"
26 #include "llvm/Object/IRObjectFile.h"
27 #include "llvm/Object/ModuleSummaryIndexObjectFile.h"
28 #include "llvm/Support/CommandLine.h"
29 #include "llvm/Support/Debug.h"
30 #include "llvm/Support/SourceMgr.h"
31 #include "llvm/Transforms/IPO/Internalize.h"
32 #include "llvm/Transforms/Utils/FunctionImportUtils.h"
33 
34 #define DEBUG_TYPE "function-import"
35 
36 using namespace llvm;
37 
38 STATISTIC(NumImported, "Number of functions imported");
39 
40 /// Limit on instruction count of imported functions.
41 static cl::opt<unsigned> ImportInstrLimit(
42     "import-instr-limit", cl::init(100), cl::Hidden, cl::value_desc("N"),
43     cl::desc("Only import functions with less than N instructions"));
44 
45 static cl::opt<float>
46     ImportInstrFactor("import-instr-evolution-factor", cl::init(0.7),
47                       cl::Hidden, cl::value_desc("x"),
48                       cl::desc("As we import functions, multiply the "
49                                "`import-instr-limit` threshold by this factor "
50                                "before processing newly imported functions"));
51 
52 static cl::opt<float> ImportHotInstrFactor(
53     "import-hot-evolution-factor", cl::init(1.0), cl::Hidden,
54     cl::value_desc("x"),
55     cl::desc("As we import functions called from hot callsite, multiply the "
56              "`import-instr-limit` threshold by this factor "
57              "before processing newly imported functions"));
58 
59 static cl::opt<float> ImportHotMultiplier(
60     "import-hot-multiplier", cl::init(3.0), cl::Hidden, cl::value_desc("x"),
61     cl::desc("Multiply the `import-instr-limit` threshold for hot callsites"));
62 
63 // FIXME: This multiplier was not really tuned up.
64 static cl::opt<float> ImportColdMultiplier(
65     "import-cold-multiplier", cl::init(0), cl::Hidden, cl::value_desc("N"),
66     cl::desc("Multiply the `import-instr-limit` threshold for cold callsites"));
67 
68 static cl::opt<bool> PrintImports("print-imports", cl::init(false), cl::Hidden,
69                                   cl::desc("Print imported functions"));
70 
71 // Temporary allows the function import pass to disable always linking
72 // referenced discardable symbols.
73 static cl::opt<bool>
74     DontForceImportReferencedDiscardableSymbols("disable-force-link-odr",
75                                                 cl::init(false), cl::Hidden);
76 
77 static cl::opt<bool> EnableImportMetadata(
78     "enable-import-metadata", cl::init(
79 #if !defined(NDEBUG)
80                                   true /*Enabled with asserts.*/
81 #else
82                                   false
83 #endif
84                                   ),
85     cl::Hidden, cl::desc("Enable import metadata like 'thinlto_src_module'"));
86 
87 // Load lazily a module from \p FileName in \p Context.
88 static std::unique_ptr<Module> loadFile(const std::string &FileName,
89                                         LLVMContext &Context) {
90   SMDiagnostic Err;
91   DEBUG(dbgs() << "Loading '" << FileName << "'\n");
92   // Metadata isn't loaded until functions are imported, to minimize
93   // the memory overhead.
94   std::unique_ptr<Module> Result =
95       getLazyIRFileModule(FileName, Err, Context,
96                           /* ShouldLazyLoadMetadata = */ true);
97   if (!Result) {
98     Err.print("function-import", errs());
99     report_fatal_error("Abort");
100   }
101 
102   return Result;
103 }
104 
105 namespace {
106 
107 // Return true if the Summary describes a GlobalValue that can be externally
108 // referenced, i.e. it does not need renaming (linkage is not local) or renaming
109 // is possible (does not have a section for instance).
110 static bool canBeExternallyReferenced(const GlobalValueSummary &Summary) {
111   if (!Summary.needsRenaming())
112     return true;
113 
114   if (Summary.hasSection())
115     // Can't rename a global that needs renaming if has a section.
116     return false;
117 
118   return true;
119 }
120 
121 // Return true if \p GUID describes a GlobalValue that can be externally
122 // referenced, i.e. it does not need renaming (linkage is not local) or
123 // renaming is possible (does not have a section for instance).
124 static bool canBeExternallyReferenced(const ModuleSummaryIndex &Index,
125                                       GlobalValue::GUID GUID) {
126   auto Summaries = Index.findGlobalValueSummaryList(GUID);
127   if (Summaries == Index.end())
128     return true;
129   if (Summaries->second.size() != 1)
130     // If there are multiple globals with this GUID, then we know it is
131     // not a local symbol, and it is necessarily externally referenced.
132     return true;
133 
134   // We don't need to check for the module path, because if it can't be
135   // externally referenced and we call it, it is necessarilly in the same
136   // module
137   return canBeExternallyReferenced(**Summaries->second.begin());
138 }
139 
140 // Return true if the global described by \p Summary can be imported in another
141 // module.
142 static bool eligibleForImport(const ModuleSummaryIndex &Index,
143                               const GlobalValueSummary &Summary) {
144   if (!canBeExternallyReferenced(Summary))
145     // Can't import a global that needs renaming if has a section for instance.
146     // FIXME: we may be able to import it by copying it without promotion.
147     return false;
148 
149   // Don't import functions that are not viable to inline.
150   if (Summary.isNotViableToInline())
151     return false;
152 
153   // Check references (and potential calls) in the same module. If the current
154   // value references a global that can't be externally referenced it is not
155   // eligible for import.
156   bool AllRefsCanBeExternallyReferenced =
157       llvm::all_of(Summary.refs(), [&](const ValueInfo &VI) {
158         return canBeExternallyReferenced(Index, VI.getGUID());
159       });
160   if (!AllRefsCanBeExternallyReferenced)
161     return false;
162 
163   if (auto *FuncSummary = dyn_cast<FunctionSummary>(&Summary)) {
164     bool AllCallsCanBeExternallyReferenced = llvm::all_of(
165         FuncSummary->calls(), [&](const FunctionSummary::EdgeTy &Edge) {
166           return canBeExternallyReferenced(Index, Edge.first.getGUID());
167         });
168     if (!AllCallsCanBeExternallyReferenced)
169       return false;
170   }
171   return true;
172 }
173 
174 /// Given a list of possible callee implementation for a call site, select one
175 /// that fits the \p Threshold.
176 ///
177 /// FIXME: select "best" instead of first that fits. But what is "best"?
178 /// - The smallest: more likely to be inlined.
179 /// - The one with the least outgoing edges (already well optimized).
180 /// - One from a module already being imported from in order to reduce the
181 ///   number of source modules parsed/linked.
182 /// - One that has PGO data attached.
183 /// - [insert you fancy metric here]
184 static const GlobalValueSummary *
185 selectCallee(const ModuleSummaryIndex &Index,
186              const GlobalValueSummaryList &CalleeSummaryList,
187              unsigned Threshold) {
188   auto It = llvm::find_if(
189       CalleeSummaryList,
190       [&](const std::unique_ptr<GlobalValueSummary> &SummaryPtr) {
191         auto *GVSummary = SummaryPtr.get();
192         if (GlobalValue::isInterposableLinkage(GVSummary->linkage()))
193           // There is no point in importing these, we can't inline them
194           return false;
195         if (auto *AS = dyn_cast<AliasSummary>(GVSummary)) {
196           GVSummary = &AS->getAliasee();
197           // Alias can't point to "available_externally". However when we import
198           // linkOnceODR the linkage does not change. So we import the alias
199           // and aliasee only in this case.
200           // FIXME: we should import alias as available_externally *function*,
201           // the destination module does need to know it is an alias.
202           if (!GlobalValue::isLinkOnceODRLinkage(GVSummary->linkage()))
203             return false;
204         }
205 
206         auto *Summary = cast<FunctionSummary>(GVSummary);
207 
208         if (Summary->instCount() > Threshold)
209           return false;
210 
211         if (!eligibleForImport(Index, *Summary))
212           return false;
213 
214         return true;
215       });
216   if (It == CalleeSummaryList.end())
217     return nullptr;
218 
219   return cast<GlobalValueSummary>(It->get());
220 }
221 
222 /// Return the summary for the function \p GUID that fits the \p Threshold, or
223 /// null if there's no match.
224 static const GlobalValueSummary *selectCallee(GlobalValue::GUID GUID,
225                                               unsigned Threshold,
226                                               const ModuleSummaryIndex &Index) {
227   auto CalleeSummaryList = Index.findGlobalValueSummaryList(GUID);
228   if (CalleeSummaryList == Index.end())
229     return nullptr; // This function does not have a summary
230   return selectCallee(Index, CalleeSummaryList->second, Threshold);
231 }
232 
233 /// Mark the global \p GUID as export by module \p ExportModulePath if found in
234 /// this module. If it is a GlobalVariable, we also mark any referenced global
235 /// in the current module as exported.
236 static void exportGlobalInModule(const ModuleSummaryIndex &Index,
237                                  StringRef ExportModulePath,
238                                  GlobalValue::GUID GUID,
239                                  FunctionImporter::ExportSetTy &ExportList) {
240   auto FindGlobalSummaryInModule =
241       [&](GlobalValue::GUID GUID) -> GlobalValueSummary *{
242         auto SummaryList = Index.findGlobalValueSummaryList(GUID);
243         if (SummaryList == Index.end())
244           // This global does not have a summary, it is not part of the ThinLTO
245           // process
246           return nullptr;
247         auto SummaryIter = llvm::find_if(
248             SummaryList->second,
249             [&](const std::unique_ptr<GlobalValueSummary> &Summary) {
250               return Summary->modulePath() == ExportModulePath;
251             });
252         if (SummaryIter == SummaryList->second.end())
253           return nullptr;
254         return SummaryIter->get();
255       };
256 
257   auto *Summary = FindGlobalSummaryInModule(GUID);
258   if (!Summary)
259     return;
260   // We found it in the current module, mark as exported
261   ExportList.insert(GUID);
262 
263   auto GVS = dyn_cast<GlobalVarSummary>(Summary);
264   if (!GVS)
265     return;
266   // FunctionImportGlobalProcessing::doPromoteLocalToGlobal() will always
267   // trigger importing  the initializer for `constant unnamed addr` globals that
268   // are referenced. We conservatively export all the referenced symbols for
269   // every global to workaround this, so that the ExportList is accurate.
270   // FIXME: with a "isConstant" flag in the summary we could be more targetted.
271   for (auto &Ref : GVS->refs()) {
272     auto GUID = Ref.getGUID();
273     auto *RefSummary = FindGlobalSummaryInModule(GUID);
274     if (RefSummary)
275       // Found a ref in the current module, mark it as exported
276       ExportList.insert(GUID);
277   }
278 }
279 
280 using EdgeInfo = std::pair<const FunctionSummary *, unsigned /* Threshold */>;
281 
282 /// Compute the list of functions to import for a given caller. Mark these
283 /// imported functions and the symbols they reference in their source module as
284 /// exported from their source module.
285 static void computeImportForFunction(
286     const FunctionSummary &Summary, const ModuleSummaryIndex &Index,
287     const unsigned Threshold, const GVSummaryMapTy &DefinedGVSummaries,
288     SmallVectorImpl<EdgeInfo> &Worklist,
289     FunctionImporter::ImportMapTy &ImportList,
290     StringMap<FunctionImporter::ExportSetTy> *ExportLists = nullptr) {
291   for (auto &Edge : Summary.calls()) {
292     auto GUID = Edge.first.getGUID();
293     DEBUG(dbgs() << " edge -> " << GUID << " Threshold:" << Threshold << "\n");
294 
295     if (DefinedGVSummaries.count(GUID)) {
296       DEBUG(dbgs() << "ignored! Target already in destination module.\n");
297       continue;
298     }
299 
300     auto GetBonusMultiplier = [](CalleeInfo::HotnessType Hotness) -> float {
301       if (Hotness == CalleeInfo::HotnessType::Hot)
302         return ImportHotMultiplier;
303       if (Hotness == CalleeInfo::HotnessType::Cold)
304         return ImportColdMultiplier;
305       return 1.0;
306     };
307 
308     const auto NewThreshold =
309         Threshold * GetBonusMultiplier(Edge.second.Hotness);
310 
311     auto *CalleeSummary = selectCallee(GUID, NewThreshold, Index);
312     if (!CalleeSummary) {
313       DEBUG(dbgs() << "ignored! No qualifying callee with summary found.\n");
314       continue;
315     }
316     // "Resolve" the summary, traversing alias,
317     const FunctionSummary *ResolvedCalleeSummary;
318     if (isa<AliasSummary>(CalleeSummary)) {
319       ResolvedCalleeSummary = cast<FunctionSummary>(
320           &cast<AliasSummary>(CalleeSummary)->getAliasee());
321       assert(
322           GlobalValue::isLinkOnceODRLinkage(ResolvedCalleeSummary->linkage()) &&
323           "Unexpected alias to a non-linkonceODR in import list");
324     } else
325       ResolvedCalleeSummary = cast<FunctionSummary>(CalleeSummary);
326 
327     assert(ResolvedCalleeSummary->instCount() <= NewThreshold &&
328            "selectCallee() didn't honor the threshold");
329 
330     auto ExportModulePath = ResolvedCalleeSummary->modulePath();
331     auto &ProcessedThreshold = ImportList[ExportModulePath][GUID];
332     /// Since the traversal of the call graph is DFS, we can revisit a function
333     /// a second time with a higher threshold. In this case, it is added back to
334     /// the worklist with the new threshold.
335     if (ProcessedThreshold && ProcessedThreshold >= Threshold) {
336       DEBUG(dbgs() << "ignored! Target was already seen with Threshold "
337                    << ProcessedThreshold << "\n");
338       continue;
339     }
340     // Mark this function as imported in this module, with the current Threshold
341     ProcessedThreshold = Threshold;
342 
343     // Make exports in the source module.
344     if (ExportLists) {
345       auto &ExportList = (*ExportLists)[ExportModulePath];
346       ExportList.insert(GUID);
347       // Mark all functions and globals referenced by this function as exported
348       // to the outside if they are defined in the same source module.
349       for (auto &Edge : ResolvedCalleeSummary->calls()) {
350         auto CalleeGUID = Edge.first.getGUID();
351         exportGlobalInModule(Index, ExportModulePath, CalleeGUID, ExportList);
352       }
353       for (auto &Ref : ResolvedCalleeSummary->refs()) {
354         auto GUID = Ref.getGUID();
355         exportGlobalInModule(Index, ExportModulePath, GUID, ExportList);
356       }
357     }
358 
359     auto GetAdjustedThreshold = [](unsigned Threshold, bool IsHotCallsite) {
360       // Adjust the threshold for next level of imported functions.
361       // The threshold is different for hot callsites because we can then
362       // inline chains of hot calls.
363       if (IsHotCallsite)
364         return Threshold * ImportHotInstrFactor;
365       return Threshold * ImportInstrFactor;
366     };
367 
368     bool IsHotCallsite = Edge.second.Hotness == CalleeInfo::HotnessType::Hot;
369 
370     // Insert the newly imported function to the worklist.
371     Worklist.emplace_back(ResolvedCalleeSummary,
372                           GetAdjustedThreshold(Threshold, IsHotCallsite));
373   }
374 }
375 
376 /// Given the list of globals defined in a module, compute the list of imports
377 /// as well as the list of "exports", i.e. the list of symbols referenced from
378 /// another module (that may require promotion).
379 static void ComputeImportForModule(
380     const GVSummaryMapTy &DefinedGVSummaries, const ModuleSummaryIndex &Index,
381     FunctionImporter::ImportMapTy &ImportList,
382     StringMap<FunctionImporter::ExportSetTy> *ExportLists = nullptr) {
383   // Worklist contains the list of function imported in this module, for which
384   // we will analyse the callees and may import further down the callgraph.
385   SmallVector<EdgeInfo, 128> Worklist;
386 
387   // Populate the worklist with the import for the functions in the current
388   // module
389   for (auto &GVSummary : DefinedGVSummaries) {
390     auto *Summary = GVSummary.second;
391     if (auto *AS = dyn_cast<AliasSummary>(Summary))
392       Summary = &AS->getAliasee();
393     auto *FuncSummary = dyn_cast<FunctionSummary>(Summary);
394     if (!FuncSummary)
395       // Skip import for global variables
396       continue;
397     DEBUG(dbgs() << "Initalize import for " << GVSummary.first << "\n");
398     computeImportForFunction(*FuncSummary, Index, ImportInstrLimit,
399                              DefinedGVSummaries, Worklist, ImportList,
400                              ExportLists);
401   }
402 
403   // Process the newly imported functions and add callees to the worklist.
404   while (!Worklist.empty()) {
405     auto FuncInfo = Worklist.pop_back_val();
406     auto *Summary = FuncInfo.first;
407     auto Threshold = FuncInfo.second;
408 
409     computeImportForFunction(*Summary, Index, Threshold, DefinedGVSummaries,
410                              Worklist, ImportList, ExportLists);
411   }
412 }
413 
414 } // anonymous namespace
415 
416 /// Compute all the import and export for every module using the Index.
417 void llvm::ComputeCrossModuleImport(
418     const ModuleSummaryIndex &Index,
419     const StringMap<GVSummaryMapTy> &ModuleToDefinedGVSummaries,
420     StringMap<FunctionImporter::ImportMapTy> &ImportLists,
421     StringMap<FunctionImporter::ExportSetTy> &ExportLists) {
422   // For each module that has function defined, compute the import/export lists.
423   for (auto &DefinedGVSummaries : ModuleToDefinedGVSummaries) {
424     auto &ImportList = ImportLists[DefinedGVSummaries.first()];
425     DEBUG(dbgs() << "Computing import for Module '"
426                  << DefinedGVSummaries.first() << "'\n");
427     ComputeImportForModule(DefinedGVSummaries.second, Index, ImportList,
428                            &ExportLists);
429   }
430 
431 #ifndef NDEBUG
432   DEBUG(dbgs() << "Import/Export lists for " << ImportLists.size()
433                << " modules:\n");
434   for (auto &ModuleImports : ImportLists) {
435     auto ModName = ModuleImports.first();
436     auto &Exports = ExportLists[ModName];
437     DEBUG(dbgs() << "* Module " << ModName << " exports " << Exports.size()
438                  << " functions. Imports from " << ModuleImports.second.size()
439                  << " modules.\n");
440     for (auto &Src : ModuleImports.second) {
441       auto SrcModName = Src.first();
442       DEBUG(dbgs() << " - " << Src.second.size() << " functions imported from "
443                    << SrcModName << "\n");
444     }
445   }
446 #endif
447 }
448 
449 /// Compute all the imports for the given module in the Index.
450 void llvm::ComputeCrossModuleImportForModule(
451     StringRef ModulePath, const ModuleSummaryIndex &Index,
452     FunctionImporter::ImportMapTy &ImportList) {
453 
454   // Collect the list of functions this module defines.
455   // GUID -> Summary
456   GVSummaryMapTy FunctionSummaryMap;
457   Index.collectDefinedFunctionsForModule(ModulePath, FunctionSummaryMap);
458 
459   // Compute the import list for this module.
460   DEBUG(dbgs() << "Computing import for Module '" << ModulePath << "'\n");
461   ComputeImportForModule(FunctionSummaryMap, Index, ImportList);
462 
463 #ifndef NDEBUG
464   DEBUG(dbgs() << "* Module " << ModulePath << " imports from "
465                << ImportList.size() << " modules.\n");
466   for (auto &Src : ImportList) {
467     auto SrcModName = Src.first();
468     DEBUG(dbgs() << " - " << Src.second.size() << " functions imported from "
469                  << SrcModName << "\n");
470   }
471 #endif
472 }
473 
474 /// Compute the set of summaries needed for a ThinLTO backend compilation of
475 /// \p ModulePath.
476 void llvm::gatherImportedSummariesForModule(
477     StringRef ModulePath,
478     const StringMap<GVSummaryMapTy> &ModuleToDefinedGVSummaries,
479     const FunctionImporter::ImportMapTy &ImportList,
480     std::map<std::string, GVSummaryMapTy> &ModuleToSummariesForIndex) {
481   // Include all summaries from the importing module.
482   ModuleToSummariesForIndex[ModulePath] =
483       ModuleToDefinedGVSummaries.lookup(ModulePath);
484   // Include summaries for imports.
485   for (auto &ILI : ImportList) {
486     auto &SummariesForIndex = ModuleToSummariesForIndex[ILI.first()];
487     const auto &DefinedGVSummaries =
488         ModuleToDefinedGVSummaries.lookup(ILI.first());
489     for (auto &GI : ILI.second) {
490       const auto &DS = DefinedGVSummaries.find(GI.first);
491       assert(DS != DefinedGVSummaries.end() &&
492              "Expected a defined summary for imported global value");
493       SummariesForIndex[GI.first] = DS->second;
494     }
495   }
496 }
497 
498 /// Emit the files \p ModulePath will import from into \p OutputFilename.
499 std::error_code
500 llvm::EmitImportsFiles(StringRef ModulePath, StringRef OutputFilename,
501                        const FunctionImporter::ImportMapTy &ModuleImports) {
502   std::error_code EC;
503   raw_fd_ostream ImportsOS(OutputFilename, EC, sys::fs::OpenFlags::F_None);
504   if (EC)
505     return EC;
506   for (auto &ILI : ModuleImports)
507     ImportsOS << ILI.first() << "\n";
508   return std::error_code();
509 }
510 
511 /// Fixup WeakForLinker linkages in \p TheModule based on summary analysis.
512 void llvm::thinLTOResolveWeakForLinkerModule(
513     Module &TheModule, const GVSummaryMapTy &DefinedGlobals) {
514   auto updateLinkage = [&](GlobalValue &GV) {
515     if (!GlobalValue::isWeakForLinker(GV.getLinkage()))
516       return;
517     // See if the global summary analysis computed a new resolved linkage.
518     const auto &GS = DefinedGlobals.find(GV.getGUID());
519     if (GS == DefinedGlobals.end())
520       return;
521     auto NewLinkage = GS->second->linkage();
522     if (NewLinkage == GV.getLinkage())
523       return;
524     DEBUG(dbgs() << "ODR fixing up linkage for `" << GV.getName() << "` from "
525                  << GV.getLinkage() << " to " << NewLinkage << "\n");
526     GV.setLinkage(NewLinkage);
527     // Remove functions converted to available_externally from comdats,
528     // as this is a declaration for the linker, and will be dropped eventually.
529     // It is illegal for comdats to contain declarations.
530     auto *GO = dyn_cast_or_null<GlobalObject>(&GV);
531     if (GO && GO->isDeclarationForLinker() && GO->hasComdat()) {
532       assert(GO->hasAvailableExternallyLinkage() &&
533              "Expected comdat on definition (possibly available external)");
534       GO->setComdat(nullptr);
535     }
536   };
537 
538   // Process functions and global now
539   for (auto &GV : TheModule)
540     updateLinkage(GV);
541   for (auto &GV : TheModule.globals())
542     updateLinkage(GV);
543   for (auto &GV : TheModule.aliases())
544     updateLinkage(GV);
545 }
546 
547 /// Run internalization on \p TheModule based on symmary analysis.
548 void llvm::thinLTOInternalizeModule(Module &TheModule,
549                                     const GVSummaryMapTy &DefinedGlobals) {
550   // Parse inline ASM and collect the list of symbols that are not defined in
551   // the current module.
552   StringSet<> AsmUndefinedRefs;
553   object::IRObjectFile::CollectAsmUndefinedRefs(
554       Triple(TheModule.getTargetTriple()), TheModule.getModuleInlineAsm(),
555       [&AsmUndefinedRefs](StringRef Name, object::BasicSymbolRef::Flags Flags) {
556         if (Flags & object::BasicSymbolRef::SF_Undefined)
557           AsmUndefinedRefs.insert(Name);
558       });
559 
560   // Declare a callback for the internalize pass that will ask for every
561   // candidate GlobalValue if it can be internalized or not.
562   auto MustPreserveGV = [&](const GlobalValue &GV) -> bool {
563     // Can't be internalized if referenced in inline asm.
564     if (AsmUndefinedRefs.count(GV.getName()))
565       return true;
566 
567     // Lookup the linkage recorded in the summaries during global analysis.
568     const auto &GS = DefinedGlobals.find(GV.getGUID());
569     GlobalValue::LinkageTypes Linkage;
570     if (GS == DefinedGlobals.end()) {
571       // Must have been promoted (possibly conservatively). Find original
572       // name so that we can access the correct summary and see if it can
573       // be internalized again.
574       // FIXME: Eventually we should control promotion instead of promoting
575       // and internalizing again.
576       StringRef OrigName =
577           ModuleSummaryIndex::getOriginalNameBeforePromote(GV.getName());
578       std::string OrigId = GlobalValue::getGlobalIdentifier(
579           OrigName, GlobalValue::InternalLinkage,
580           TheModule.getSourceFileName());
581       const auto &GS = DefinedGlobals.find(GlobalValue::getGUID(OrigId));
582       if (GS == DefinedGlobals.end()) {
583         // Also check the original non-promoted non-globalized name. In some
584         // cases a preempted weak value is linked in as a local copy because
585         // it is referenced by an alias (IRLinker::linkGlobalValueProto).
586         // In that case, since it was originally not a local value, it was
587         // recorded in the index using the original name.
588         // FIXME: This may not be needed once PR27866 is fixed.
589         const auto &GS = DefinedGlobals.find(GlobalValue::getGUID(OrigName));
590         assert(GS != DefinedGlobals.end());
591         Linkage = GS->second->linkage();
592       } else {
593         Linkage = GS->second->linkage();
594       }
595     } else
596       Linkage = GS->second->linkage();
597     return !GlobalValue::isLocalLinkage(Linkage);
598   };
599 
600   // FIXME: See if we can just internalize directly here via linkage changes
601   // based on the index, rather than invoking internalizeModule.
602   llvm::internalizeModule(TheModule, MustPreserveGV);
603 }
604 
605 // Automatically import functions in Module \p DestModule based on the summaries
606 // index.
607 //
608 bool FunctionImporter::importFunctions(
609     Module &DestModule, const FunctionImporter::ImportMapTy &ImportList,
610     bool ForceImportReferencedDiscardableSymbols) {
611   DEBUG(dbgs() << "Starting import for Module "
612                << DestModule.getModuleIdentifier() << "\n");
613   unsigned ImportedCount = 0;
614 
615   // Linker that will be used for importing function
616   Linker TheLinker(DestModule);
617   // Do the actual import of functions now, one Module at a time
618   std::set<StringRef> ModuleNameOrderedList;
619   for (auto &FunctionsToImportPerModule : ImportList) {
620     ModuleNameOrderedList.insert(FunctionsToImportPerModule.first());
621   }
622   for (auto &Name : ModuleNameOrderedList) {
623     // Get the module for the import
624     const auto &FunctionsToImportPerModule = ImportList.find(Name);
625     assert(FunctionsToImportPerModule != ImportList.end());
626     std::unique_ptr<Module> SrcModule = ModuleLoader(Name);
627     assert(&DestModule.getContext() == &SrcModule->getContext() &&
628            "Context mismatch");
629 
630     // If modules were created with lazy metadata loading, materialize it
631     // now, before linking it (otherwise this will be a noop).
632     SrcModule->materializeMetadata();
633     UpgradeDebugInfo(*SrcModule);
634 
635     auto &ImportGUIDs = FunctionsToImportPerModule->second;
636     // Find the globals to import
637     DenseSet<const GlobalValue *> GlobalsToImport;
638     for (Function &F : *SrcModule) {
639       if (!F.hasName())
640         continue;
641       auto GUID = F.getGUID();
642       auto Import = ImportGUIDs.count(GUID);
643       DEBUG(dbgs() << (Import ? "Is" : "Not") << " importing function " << GUID
644                    << " " << F.getName() << " from "
645                    << SrcModule->getSourceFileName() << "\n");
646       if (Import) {
647         F.materialize();
648         if (EnableImportMetadata) {
649           // Add 'thinlto_src_module' metadata for statistics and debugging.
650           F.setMetadata(
651               "thinlto_src_module",
652               llvm::MDNode::get(
653                   DestModule.getContext(),
654                   {llvm::MDString::get(DestModule.getContext(),
655                                        SrcModule->getSourceFileName())}));
656         }
657         GlobalsToImport.insert(&F);
658       }
659     }
660     for (GlobalVariable &GV : SrcModule->globals()) {
661       if (!GV.hasName())
662         continue;
663       auto GUID = GV.getGUID();
664       auto Import = ImportGUIDs.count(GUID);
665       DEBUG(dbgs() << (Import ? "Is" : "Not") << " importing global " << GUID
666                    << " " << GV.getName() << " from "
667                    << SrcModule->getSourceFileName() << "\n");
668       if (Import) {
669         GV.materialize();
670         GlobalsToImport.insert(&GV);
671       }
672     }
673     for (GlobalAlias &GA : SrcModule->aliases()) {
674       if (!GA.hasName())
675         continue;
676       auto GUID = GA.getGUID();
677       auto Import = ImportGUIDs.count(GUID);
678       DEBUG(dbgs() << (Import ? "Is" : "Not") << " importing alias " << GUID
679                    << " " << GA.getName() << " from "
680                    << SrcModule->getSourceFileName() << "\n");
681       if (Import) {
682         // Alias can't point to "available_externally". However when we import
683         // linkOnceODR the linkage does not change. So we import the alias
684         // and aliasee only in this case. This has been handled by
685         // computeImportForFunction()
686         GlobalObject *GO = GA.getBaseObject();
687         assert(GO->hasLinkOnceODRLinkage() &&
688                "Unexpected alias to a non-linkonceODR in import list");
689 #ifndef NDEBUG
690         if (!GlobalsToImport.count(GO))
691           DEBUG(dbgs() << " alias triggers importing aliasee " << GO->getGUID()
692                        << " " << GO->getName() << " from "
693                        << SrcModule->getSourceFileName() << "\n");
694 #endif
695         GO->materialize();
696         GlobalsToImport.insert(GO);
697         GA.materialize();
698         GlobalsToImport.insert(&GA);
699       }
700     }
701 
702     // Link in the specified functions.
703     if (renameModuleForThinLTO(*SrcModule, Index, &GlobalsToImport))
704       return true;
705 
706     if (PrintImports) {
707       for (const auto *GV : GlobalsToImport)
708         dbgs() << DestModule.getSourceFileName() << ": Import " << GV->getName()
709                << " from " << SrcModule->getSourceFileName() << "\n";
710     }
711 
712     // Instruct the linker that the client will take care of linkonce resolution
713     unsigned Flags = Linker::Flags::None;
714     if (!ForceImportReferencedDiscardableSymbols)
715       Flags |= Linker::Flags::DontForceLinkLinkonceODR;
716 
717     if (TheLinker.linkInModule(std::move(SrcModule), Flags, &GlobalsToImport))
718       report_fatal_error("Function Import: link error");
719 
720     ImportedCount += GlobalsToImport.size();
721   }
722 
723   NumImported += ImportedCount;
724 
725   DEBUG(dbgs() << "Imported " << ImportedCount << " functions for Module "
726                << DestModule.getModuleIdentifier() << "\n");
727   return ImportedCount;
728 }
729 
730 /// Summary file to use for function importing when using -function-import from
731 /// the command line.
732 static cl::opt<std::string>
733     SummaryFile("summary-file",
734                 cl::desc("The summary file to use for function importing."));
735 
736 static void diagnosticHandler(const DiagnosticInfo &DI) {
737   raw_ostream &OS = errs();
738   DiagnosticPrinterRawOStream DP(OS);
739   DI.print(DP);
740   OS << '\n';
741 }
742 
743 /// Parse the summary index out of an IR file and return the summary
744 /// index object if found, or nullptr if not.
745 static std::unique_ptr<ModuleSummaryIndex> getModuleSummaryIndexForFile(
746     StringRef Path, std::string &Error,
747     const DiagnosticHandlerFunction &DiagnosticHandler) {
748   std::unique_ptr<MemoryBuffer> Buffer;
749   ErrorOr<std::unique_ptr<MemoryBuffer>> BufferOrErr =
750       MemoryBuffer::getFile(Path);
751   if (std::error_code EC = BufferOrErr.getError()) {
752     Error = EC.message();
753     return nullptr;
754   }
755   Buffer = std::move(BufferOrErr.get());
756   ErrorOr<std::unique_ptr<object::ModuleSummaryIndexObjectFile>> ObjOrErr =
757       object::ModuleSummaryIndexObjectFile::create(Buffer->getMemBufferRef(),
758                                                    DiagnosticHandler);
759   if (std::error_code EC = ObjOrErr.getError()) {
760     Error = EC.message();
761     return nullptr;
762   }
763   return (*ObjOrErr)->takeIndex();
764 }
765 
766 static bool doImportingForModule(Module &M, const ModuleSummaryIndex *Index) {
767   if (SummaryFile.empty() && !Index)
768     report_fatal_error("error: -function-import requires -summary-file or "
769                        "file from frontend\n");
770   std::unique_ptr<ModuleSummaryIndex> IndexPtr;
771   if (!SummaryFile.empty()) {
772     if (Index)
773       report_fatal_error("error: -summary-file and index from frontend\n");
774     std::string Error;
775     IndexPtr =
776         getModuleSummaryIndexForFile(SummaryFile, Error, diagnosticHandler);
777     if (!IndexPtr) {
778       errs() << "Error loading file '" << SummaryFile << "': " << Error << "\n";
779       return false;
780     }
781     Index = IndexPtr.get();
782   }
783 
784   // First step is collecting the import list.
785   FunctionImporter::ImportMapTy ImportList;
786   ComputeCrossModuleImportForModule(M.getModuleIdentifier(), *Index,
787                                     ImportList);
788 
789   // Next we need to promote to global scope and rename any local values that
790   // are potentially exported to other modules.
791   if (renameModuleForThinLTO(M, *Index, nullptr)) {
792     errs() << "Error renaming module\n";
793     return false;
794   }
795 
796   // Perform the import now.
797   auto ModuleLoader = [&M](StringRef Identifier) {
798     return loadFile(Identifier, M.getContext());
799   };
800   FunctionImporter Importer(*Index, ModuleLoader);
801   return Importer.importFunctions(M, ImportList,
802                                   !DontForceImportReferencedDiscardableSymbols);
803 }
804 
805 namespace {
806 /// Pass that performs cross-module function import provided a summary file.
807 class FunctionImportLegacyPass : public ModulePass {
808   /// Optional module summary index to use for importing, otherwise
809   /// the summary-file option must be specified.
810   const ModuleSummaryIndex *Index;
811 
812 public:
813   /// Pass identification, replacement for typeid
814   static char ID;
815 
816   /// Specify pass name for debug output
817   StringRef getPassName() const override { return "Function Importing"; }
818 
819   explicit FunctionImportLegacyPass(const ModuleSummaryIndex *Index = nullptr)
820       : ModulePass(ID), Index(Index) {}
821 
822   bool runOnModule(Module &M) override {
823     if (skipModule(M))
824       return false;
825 
826     return doImportingForModule(M, Index);
827   }
828 };
829 } // anonymous namespace
830 
831 PreservedAnalyses FunctionImportPass::run(Module &M,
832                                           ModuleAnalysisManager &AM) {
833   if (!doImportingForModule(M, Index))
834     return PreservedAnalyses::all();
835 
836   return PreservedAnalyses::none();
837 }
838 
839 char FunctionImportLegacyPass::ID = 0;
840 INITIALIZE_PASS(FunctionImportLegacyPass, "function-import",
841                 "Summary Based Function Import", false, false)
842 
843 namespace llvm {
844 Pass *createFunctionImportPass(const ModuleSummaryIndex *Index = nullptr) {
845   return new FunctionImportLegacyPass(Index);
846 }
847 }
848