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