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