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