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/IR/AutoUpgrade.h"
20 #include "llvm/IR/DiagnosticPrinter.h"
21 #include "llvm/IR/IntrinsicInst.h"
22 #include "llvm/IR/Module.h"
23 #include "llvm/IRReader/IRReader.h"
24 #include "llvm/Linker/Linker.h"
25 #include "llvm/Object/ModuleSummaryIndexObjectFile.h"
26 #include "llvm/Support/CommandLine.h"
27 #include "llvm/Support/Debug.h"
28 #include "llvm/Support/SourceMgr.h"
29 #include "llvm/Transforms/Utils/FunctionImportUtils.h"
30 
31 #define DEBUG_TYPE "function-import"
32 
33 using namespace llvm;
34 
35 STATISTIC(NumImported, "Number of functions imported");
36 
37 /// Limit on instruction count of imported functions.
38 static cl::opt<unsigned> ImportInstrLimit(
39     "import-instr-limit", cl::init(100), cl::Hidden, cl::value_desc("N"),
40     cl::desc("Only import functions with less than N instructions"));
41 
42 static cl::opt<float>
43     ImportInstrFactor("import-instr-evolution-factor", cl::init(0.7),
44                       cl::Hidden, cl::value_desc("x"),
45                       cl::desc("As we import functions, multiply the "
46                                "`import-instr-limit` threshold by this factor "
47                                "before processing newly imported functions"));
48 
49 static cl::opt<bool> PrintImports("print-imports", cl::init(false), cl::Hidden,
50                                   cl::desc("Print imported functions"));
51 
52 // Temporary allows the function import pass to disable always linking
53 // referenced discardable symbols.
54 static cl::opt<bool>
55     DontForceImportReferencedDiscardableSymbols("disable-force-link-odr",
56                                                 cl::init(false), cl::Hidden);
57 
58 // Load lazily a module from \p FileName in \p Context.
59 static std::unique_ptr<Module> loadFile(const std::string &FileName,
60                                         LLVMContext &Context) {
61   SMDiagnostic Err;
62   DEBUG(dbgs() << "Loading '" << FileName << "'\n");
63   // Metadata isn't loaded until functions are imported, to minimize
64   // the memory overhead.
65   std::unique_ptr<Module> Result =
66       getLazyIRFileModule(FileName, Err, Context,
67                           /* ShouldLazyLoadMetadata = */ true);
68   if (!Result) {
69     Err.print("function-import", errs());
70     report_fatal_error("Abort");
71   }
72 
73   return Result;
74 }
75 
76 namespace {
77 
78 /// Given a list of possible callee implementation for a call site, select one
79 /// that fits the \p Threshold.
80 ///
81 /// FIXME: select "best" instead of first that fits. But what is "best"?
82 /// - The smallest: more likely to be inlined.
83 /// - The one with the least outgoing edges (already well optimized).
84 /// - One from a module already being imported from in order to reduce the
85 ///   number of source modules parsed/linked.
86 /// - One that has PGO data attached.
87 /// - [insert you fancy metric here]
88 static const GlobalValueSummary *
89 selectCallee(const GlobalValueInfoList &CalleeInfoList, unsigned Threshold) {
90   auto It = llvm::find_if(
91       CalleeInfoList, [&](const std::unique_ptr<GlobalValueInfo> &GlobInfo) {
92         assert(GlobInfo->summary() &&
93                "We should not have a Global Info without summary");
94         auto *GVSummary = GlobInfo->summary();
95         if (GlobalValue::isWeakAnyLinkage(GVSummary->linkage()))
96           // There is no point in importing weak symbols, we can't inline them
97           return false;
98         if (auto *AS = dyn_cast<AliasSummary>(GVSummary)) {
99           GVSummary = &AS->getAliasee();
100           // Alias can't point to "available_externally". However when we import
101           // linkOnceODR the linkage does not change. So we import the alias
102           // and aliasee only in this case.
103           // FIXME: we should import alias as available_externally *function*,
104           // the destination module does need to know it is an alias.
105           if (!GlobalValue::isLinkOnceODRLinkage(GVSummary->linkage()))
106             return false;
107         }
108 
109         auto *Summary = cast<FunctionSummary>(GVSummary);
110 
111         if (Summary->instCount() > Threshold)
112           return false;
113 
114         return true;
115       });
116   if (It == CalleeInfoList.end())
117     return nullptr;
118 
119   return cast<GlobalValueSummary>((*It)->summary());
120 }
121 
122 /// Return the summary for the function \p GUID that fits the \p Threshold, or
123 /// null if there's no match.
124 static const GlobalValueSummary *selectCallee(GlobalValue::GUID GUID,
125                                               unsigned Threshold,
126                                               const ModuleSummaryIndex &Index) {
127   auto CalleeInfoList = Index.findGlobalValueInfoList(GUID);
128   if (CalleeInfoList == Index.end()) {
129     return nullptr; // This function does not have a summary
130   }
131   return selectCallee(CalleeInfoList->second, Threshold);
132 }
133 
134 /// Return true if the global \p GUID is exported by module \p ExportModulePath.
135 static bool isGlobalExported(const ModuleSummaryIndex &Index,
136                              StringRef ExportModulePath,
137                              GlobalValue::GUID GUID) {
138   auto CalleeInfoList = Index.findGlobalValueInfoList(GUID);
139   if (CalleeInfoList == Index.end())
140     // This global does not have a summary, it is not part of the ThinLTO
141     // process
142     return false;
143   auto DefinedInCalleeModule = llvm::find_if(
144       CalleeInfoList->second,
145       [&](const std::unique_ptr<GlobalValueInfo> &GlobInfo) {
146         auto *Summary = GlobInfo->summary();
147         assert(Summary && "Unexpected GlobalValueInfo without summary");
148         return Summary->modulePath() == ExportModulePath;
149       });
150   return (DefinedInCalleeModule != CalleeInfoList->second.end());
151 }
152 
153 using EdgeInfo = std::pair<const FunctionSummary *, unsigned /* Threshold */>;
154 
155 /// Compute the list of functions to import for a given caller. Mark these
156 /// imported functions and the symbols they reference in their source module as
157 /// exported from their source module.
158 static void computeImportForFunction(
159     const FunctionSummary &Summary, const ModuleSummaryIndex &Index,
160     unsigned Threshold,
161     const std::map<GlobalValue::GUID, GlobalValueSummary *> &DefinedGVSummaries,
162     SmallVectorImpl<EdgeInfo> &Worklist,
163     FunctionImporter::ImportMapTy &ImportsForModule,
164     StringMap<FunctionImporter::ExportSetTy> *ExportLists = nullptr) {
165   for (auto &Edge : Summary.calls()) {
166     auto GUID = Edge.first.getGUID();
167     DEBUG(dbgs() << " edge -> " << GUID << " Threshold:" << Threshold << "\n");
168 
169     if (DefinedGVSummaries.count(GUID)) {
170       DEBUG(dbgs() << "ignored! Target already in destination module.\n");
171       continue;
172     }
173 
174     auto *CalleeSummary = selectCallee(GUID, Threshold, Index);
175     if (!CalleeSummary) {
176       DEBUG(dbgs() << "ignored! No qualifying callee with summary found.\n");
177       continue;
178     }
179     // "Resolve" the summary, traversing alias,
180     const FunctionSummary *ResolvedCalleeSummary;
181     if (isa<AliasSummary>(CalleeSummary)) {
182       ResolvedCalleeSummary = cast<FunctionSummary>(
183           &cast<AliasSummary>(CalleeSummary)->getAliasee());
184       assert(
185           GlobalValue::isLinkOnceODRLinkage(ResolvedCalleeSummary->linkage()) &&
186           "Unexpected alias to a non-linkonceODR in import list");
187     } else
188       ResolvedCalleeSummary = cast<FunctionSummary>(CalleeSummary);
189 
190     assert(ResolvedCalleeSummary->instCount() <= Threshold &&
191            "selectCallee() didn't honor the threshold");
192 
193     auto ExportModulePath = ResolvedCalleeSummary->modulePath();
194     auto &ProcessedThreshold = ImportsForModule[ExportModulePath][GUID];
195     /// Since the traversal of the call graph is DFS, we can revisit a function
196     /// a second time with a higher threshold. In this case, it is added back to
197     /// the worklist with the new threshold.
198     if (ProcessedThreshold && ProcessedThreshold > Threshold) {
199       DEBUG(dbgs() << "ignored! Target was already seen with Threshold "
200                    << ProcessedThreshold << "\n");
201       continue;
202     }
203     // Mark this function as imported in this module, with the current Threshold
204     ProcessedThreshold = Threshold;
205 
206     // Make exports in the source module.
207     if (ExportLists) {
208       auto &ExportList = (*ExportLists)[ExportModulePath];
209       ExportList.insert(GUID);
210       // Mark all functions and globals referenced by this function as exported
211       // to the outside if they are defined in the same source module.
212       for (auto &Edge : ResolvedCalleeSummary->calls()) {
213         auto CalleeGUID = Edge.first.getGUID();
214         if (isGlobalExported(Index, ExportModulePath, CalleeGUID))
215           ExportList.insert(CalleeGUID);
216       }
217       for (auto &Ref : ResolvedCalleeSummary->refs()) {
218         auto GUID = Ref.getGUID();
219         if (isGlobalExported(Index, ExportModulePath, GUID))
220           ExportList.insert(GUID);
221       }
222     }
223 
224     // Insert the newly imported function to the worklist.
225     Worklist.push_back(std::make_pair(ResolvedCalleeSummary, Threshold));
226   }
227 }
228 
229 /// Given the list of globals defined in a module, compute the list of imports
230 /// as well as the list of "exports", i.e. the list of symbols referenced from
231 /// another module (that may require promotion).
232 static void ComputeImportForModule(
233     const std::map<GlobalValue::GUID, GlobalValueSummary *> &DefinedGVSummaries,
234     const ModuleSummaryIndex &Index,
235     FunctionImporter::ImportMapTy &ImportsForModule,
236     StringMap<FunctionImporter::ExportSetTy> *ExportLists = nullptr) {
237   // Worklist contains the list of function imported in this module, for which
238   // we will analyse the callees and may import further down the callgraph.
239   SmallVector<EdgeInfo, 128> Worklist;
240 
241   // Populate the worklist with the import for the functions in the current
242   // module
243   for (auto &GVInfo : DefinedGVSummaries) {
244     auto *Summary = GVInfo.second;
245     if (auto *AS = dyn_cast<AliasSummary>(Summary))
246       Summary = &AS->getAliasee();
247     auto *FuncSummary = dyn_cast<FunctionSummary>(Summary);
248     if (!FuncSummary)
249       // Skip import for global variables
250       continue;
251     DEBUG(dbgs() << "Initalize import for " << GVInfo.first << "\n");
252     computeImportForFunction(*FuncSummary, Index, ImportInstrLimit,
253                              DefinedGVSummaries, Worklist, ImportsForModule,
254                              ExportLists);
255   }
256 
257   while (!Worklist.empty()) {
258     auto FuncInfo = Worklist.pop_back_val();
259     auto *Summary = FuncInfo.first;
260     auto Threshold = FuncInfo.second;
261 
262     // Process the newly imported functions and add callees to the worklist.
263     // Adjust the threshold
264     Threshold = Threshold * ImportInstrFactor;
265 
266     computeImportForFunction(*Summary, Index, Threshold, DefinedGVSummaries,
267                              Worklist, ImportsForModule, ExportLists);
268   }
269 }
270 
271 } // anonymous namespace
272 
273 /// Compute all the import and export for every module using the Index.
274 void llvm::ComputeCrossModuleImport(
275     const ModuleSummaryIndex &Index,
276     const StringMap<std::map<GlobalValue::GUID, GlobalValueSummary *>> &
277         ModuleToDefinedGVSummaries,
278     StringMap<FunctionImporter::ImportMapTy> &ImportLists,
279     StringMap<FunctionImporter::ExportSetTy> &ExportLists) {
280   // For each module that has function defined, compute the import/export lists.
281   for (auto &DefinedGVSummaries : ModuleToDefinedGVSummaries) {
282     auto &ImportsForModule = ImportLists[DefinedGVSummaries.first()];
283     DEBUG(dbgs() << "Computing import for Module '"
284                  << DefinedGVSummaries.first() << "'\n");
285     ComputeImportForModule(DefinedGVSummaries.second, Index, ImportsForModule,
286                            &ExportLists);
287   }
288 
289 #ifndef NDEBUG
290   DEBUG(dbgs() << "Import/Export lists for " << ImportLists.size()
291                << " modules:\n");
292   for (auto &ModuleImports : ImportLists) {
293     auto ModName = ModuleImports.first();
294     auto &Exports = ExportLists[ModName];
295     DEBUG(dbgs() << "* Module " << ModName << " exports " << Exports.size()
296                  << " functions. Imports from " << ModuleImports.second.size()
297                  << " modules.\n");
298     for (auto &Src : ModuleImports.second) {
299       auto SrcModName = Src.first();
300       DEBUG(dbgs() << " - " << Src.second.size() << " functions imported from "
301                    << SrcModName << "\n");
302     }
303   }
304 #endif
305 }
306 
307 /// Compute all the imports for the given module in the Index.
308 void llvm::ComputeCrossModuleImportForModule(
309     StringRef ModulePath, const ModuleSummaryIndex &Index,
310     FunctionImporter::ImportMapTy &ImportList) {
311 
312   // Collect the list of functions this module defines.
313   // GUID -> Summary
314   std::map<GlobalValue::GUID, GlobalValueSummary *> FunctionInfoMap;
315   Index.collectDefinedFunctionsForModule(ModulePath, FunctionInfoMap);
316 
317   // Compute the import list for this module.
318   DEBUG(dbgs() << "Computing import for Module '" << ModulePath << "'\n");
319   ComputeImportForModule(FunctionInfoMap, Index, ImportList);
320 
321 #ifndef NDEBUG
322   DEBUG(dbgs() << "* Module " << ModulePath << " imports from "
323                << ImportList.size() << " modules.\n");
324   for (auto &Src : ImportList) {
325     auto SrcModName = Src.first();
326     DEBUG(dbgs() << " - " << Src.second.size() << " functions imported from "
327                  << SrcModName << "\n");
328   }
329 #endif
330 }
331 
332 // Automatically import functions in Module \p DestModule based on the summaries
333 // index.
334 //
335 bool FunctionImporter::importFunctions(
336     Module &DestModule, const FunctionImporter::ImportMapTy &ImportList,
337     bool ForceImportReferencedDiscardableSymbols) {
338   DEBUG(dbgs() << "Starting import for Module "
339                << DestModule.getModuleIdentifier() << "\n");
340   unsigned ImportedCount = 0;
341 
342   // Linker that will be used for importing function
343   Linker TheLinker(DestModule);
344   // Do the actual import of functions now, one Module at a time
345   std::set<StringRef> ModuleNameOrderedList;
346   for (auto &FunctionsToImportPerModule : ImportList) {
347     ModuleNameOrderedList.insert(FunctionsToImportPerModule.first());
348   }
349   for (auto &Name : ModuleNameOrderedList) {
350     // Get the module for the import
351     const auto &FunctionsToImportPerModule = ImportList.find(Name);
352     assert(FunctionsToImportPerModule != ImportList.end());
353     std::unique_ptr<Module> SrcModule = ModuleLoader(Name);
354     assert(&DestModule.getContext() == &SrcModule->getContext() &&
355            "Context mismatch");
356 
357     // If modules were created with lazy metadata loading, materialize it
358     // now, before linking it (otherwise this will be a noop).
359     SrcModule->materializeMetadata();
360     UpgradeDebugInfo(*SrcModule);
361 
362     auto &ImportGUIDs = FunctionsToImportPerModule->second;
363     // Find the globals to import
364     DenseSet<const GlobalValue *> GlobalsToImport;
365     for (auto &GV : *SrcModule) {
366       if (!GV.hasName())
367         continue;
368       auto GUID = GV.getGUID();
369       auto Import = ImportGUIDs.count(GUID);
370       DEBUG(dbgs() << (Import ? "Is" : "Not") << " importing function " << GUID
371                    << " " << GV.getName() << " from "
372                    << SrcModule->getSourceFileName() << "\n");
373       if (Import) {
374         GV.materialize();
375         GlobalsToImport.insert(&GV);
376       }
377     }
378     for (auto &GV : SrcModule->globals()) {
379       if (!GV.hasName())
380         continue;
381       auto GUID = GV.getGUID();
382       auto Import = ImportGUIDs.count(GUID);
383       DEBUG(dbgs() << (Import ? "Is" : "Not") << " importing global " << GUID
384                    << " " << GV.getName() << " from "
385                    << SrcModule->getSourceFileName() << "\n");
386       if (Import) {
387         GV.materialize();
388         GlobalsToImport.insert(&GV);
389       }
390     }
391     for (auto &GV : SrcModule->aliases()) {
392       if (!GV.hasName())
393         continue;
394       auto GUID = GV.getGUID();
395       auto Import = ImportGUIDs.count(GUID);
396       DEBUG(dbgs() << (Import ? "Is" : "Not") << " importing alias " << GUID
397                    << " " << GV.getName() << " from "
398                    << SrcModule->getSourceFileName() << "\n");
399       if (Import) {
400         // Alias can't point to "available_externally". However when we import
401         // linkOnceODR the linkage does not change. So we import the alias
402         // and aliasee only in this case. This has been handled by
403         // computeImportForFunction()
404         GlobalObject *GO = GV.getBaseObject();
405         assert(GO->hasLinkOnceODRLinkage() &&
406                "Unexpected alias to a non-linkonceODR in import list");
407 #ifndef NDEBUG
408         if (!GlobalsToImport.count(GO))
409           DEBUG(dbgs() << " alias triggers importing aliasee " << GO->getGUID()
410                        << " " << GO->getName() << " from "
411                        << SrcModule->getSourceFileName() << "\n");
412 #endif
413         GO->materialize();
414         GlobalsToImport.insert(GO);
415         GV.materialize();
416         GlobalsToImport.insert(&GV);
417       }
418     }
419 
420     // Link in the specified functions.
421     if (renameModuleForThinLTO(*SrcModule, Index, &GlobalsToImport))
422       return true;
423 
424     if (PrintImports) {
425       for (const auto *GV : GlobalsToImport)
426         dbgs() << DestModule.getSourceFileName() << ": Import " << GV->getName()
427                << " from " << SrcModule->getSourceFileName() << "\n";
428     }
429 
430     // Instruct the linker that the client will take care of linkonce resolution
431     unsigned Flags = Linker::Flags::None;
432     if (!ForceImportReferencedDiscardableSymbols)
433       Flags |= Linker::Flags::DontForceLinkLinkonceODR;
434 
435     if (TheLinker.linkInModule(std::move(SrcModule), Flags, &GlobalsToImport))
436       report_fatal_error("Function Import: link error");
437 
438     ImportedCount += GlobalsToImport.size();
439   }
440 
441   NumImported += ImportedCount;
442 
443   DEBUG(dbgs() << "Imported " << ImportedCount << " functions for Module "
444                << DestModule.getModuleIdentifier() << "\n");
445   return ImportedCount;
446 }
447 
448 /// Summary file to use for function importing when using -function-import from
449 /// the command line.
450 static cl::opt<std::string>
451     SummaryFile("summary-file",
452                 cl::desc("The summary file to use for function importing."));
453 
454 static void diagnosticHandler(const DiagnosticInfo &DI) {
455   raw_ostream &OS = errs();
456   DiagnosticPrinterRawOStream DP(OS);
457   DI.print(DP);
458   OS << '\n';
459 }
460 
461 /// Parse the summary index out of an IR file and return the summary
462 /// index object if found, or nullptr if not.
463 static std::unique_ptr<ModuleSummaryIndex>
464 getModuleSummaryIndexForFile(StringRef Path, std::string &Error,
465                              DiagnosticHandlerFunction DiagnosticHandler) {
466   std::unique_ptr<MemoryBuffer> Buffer;
467   ErrorOr<std::unique_ptr<MemoryBuffer>> BufferOrErr =
468       MemoryBuffer::getFile(Path);
469   if (std::error_code EC = BufferOrErr.getError()) {
470     Error = EC.message();
471     return nullptr;
472   }
473   Buffer = std::move(BufferOrErr.get());
474   ErrorOr<std::unique_ptr<object::ModuleSummaryIndexObjectFile>> ObjOrErr =
475       object::ModuleSummaryIndexObjectFile::create(Buffer->getMemBufferRef(),
476                                                    DiagnosticHandler);
477   if (std::error_code EC = ObjOrErr.getError()) {
478     Error = EC.message();
479     return nullptr;
480   }
481   return (*ObjOrErr)->takeIndex();
482 }
483 
484 namespace {
485 /// Pass that performs cross-module function import provided a summary file.
486 class FunctionImportPass : public ModulePass {
487   /// Optional module summary index to use for importing, otherwise
488   /// the summary-file option must be specified.
489   const ModuleSummaryIndex *Index;
490 
491 public:
492   /// Pass identification, replacement for typeid
493   static char ID;
494 
495   /// Specify pass name for debug output
496   const char *getPassName() const override { return "Function Importing"; }
497 
498   explicit FunctionImportPass(const ModuleSummaryIndex *Index = nullptr)
499       : ModulePass(ID), Index(Index) {}
500 
501   bool runOnModule(Module &M) override {
502     if (skipModule(M))
503       return false;
504 
505     if (SummaryFile.empty() && !Index)
506       report_fatal_error("error: -function-import requires -summary-file or "
507                          "file from frontend\n");
508     std::unique_ptr<ModuleSummaryIndex> IndexPtr;
509     if (!SummaryFile.empty()) {
510       if (Index)
511         report_fatal_error("error: -summary-file and index from frontend\n");
512       std::string Error;
513       IndexPtr =
514           getModuleSummaryIndexForFile(SummaryFile, Error, diagnosticHandler);
515       if (!IndexPtr) {
516         errs() << "Error loading file '" << SummaryFile << "': " << Error
517                << "\n";
518         return false;
519       }
520       Index = IndexPtr.get();
521     }
522 
523     // First step is collecting the import list.
524     FunctionImporter::ImportMapTy ImportList;
525     ComputeCrossModuleImportForModule(M.getModuleIdentifier(), *Index,
526                                       ImportList);
527 
528     // Next we need to promote to global scope and rename any local values that
529     // are potentially exported to other modules.
530     if (renameModuleForThinLTO(M, *Index, nullptr)) {
531       errs() << "Error renaming module\n";
532       return false;
533     }
534 
535     // Perform the import now.
536     auto ModuleLoader = [&M](StringRef Identifier) {
537       return loadFile(Identifier, M.getContext());
538     };
539     FunctionImporter Importer(*Index, ModuleLoader);
540     return Importer.importFunctions(
541         M, ImportList, !DontForceImportReferencedDiscardableSymbols);
542   }
543 };
544 } // anonymous namespace
545 
546 char FunctionImportPass::ID = 0;
547 INITIALIZE_PASS_BEGIN(FunctionImportPass, "function-import",
548                       "Summary Based Function Import", false, false)
549 INITIALIZE_PASS_END(FunctionImportPass, "function-import",
550                     "Summary Based Function Import", false, false)
551 
552 namespace llvm {
553 Pass *createFunctionImportPass(const ModuleSummaryIndex *Index = nullptr) {
554   return new FunctionImportPass(Index);
555 }
556 }
557