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