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