1 //===- FunctionImport.cpp - ThinLTO Summary-based Function Import ---------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file implements Function import based on summaries.
12 //===----------------------------------------------------------------------===//
14 #include "llvm/Transforms/IPO/FunctionImport.h"
16 #include "llvm/ADT/StringSet.h"
17 #include "llvm/IR/AutoUpgrade.h"
18 #include "llvm/IR/DiagnosticPrinter.h"
19 #include "llvm/IR/IntrinsicInst.h"
20 #include "llvm/IR/Module.h"
21 #include "llvm/IRReader/IRReader.h"
22 #include "llvm/Linker/Linker.h"
23 #include "llvm/Object/FunctionIndexObjectFile.h"
24 #include "llvm/Support/CommandLine.h"
25 #include "llvm/Support/Debug.h"
26 #include "llvm/Support/SourceMgr.h"
29 #define DEBUG_TYPE "function-import"
31 /// Limit on instruction count of imported functions.
32 static cl::opt<unsigned> ImportInstrLimit(
33 "import-instr-limit", cl::init(100), cl::Hidden, cl::value_desc("N"),
34 cl::desc("Only import functions with less than N instructions"));
36 // Load lazily a module from \p FileName in \p Context.
37 static std::unique_ptr<Module> loadFile(const std::string &FileName,
38 LLVMContext &Context) {
40 DEBUG(dbgs() << "Loading '" << FileName << "'\n");
41 std::unique_ptr<Module> Result = getLazyIRFileModule(FileName, Err, Context);
43 Err.print("function-import", errs());
47 Result->materializeMetadata();
48 UpgradeDebugInfo(*Result);
53 /// Walk through the instructions in \p F looking for external
54 /// calls not already in the \p CalledFunctions set. If any are
55 /// found they are added to the \p Worklist for importing.
56 static void findExternalCalls(const Function &F, StringSet<> &CalledFunctions,
57 SmallVector<StringRef, 64> &Worklist) {
60 if (isa<CallInst>(I)) {
61 auto CalledFunction = cast<CallInst>(I).getCalledFunction();
62 // Insert any new external calls that have not already been
63 // added to set/worklist.
64 if (CalledFunction && CalledFunction->hasName() &&
65 CalledFunction->isDeclaration() &&
66 !CalledFunctions.count(CalledFunction->getName())) {
67 CalledFunctions.insert(CalledFunction->getName());
68 Worklist.push_back(CalledFunction->getName());
75 // Helper function: given a worklist and an index, will process all the worklist
76 // and import them based on the summary information
77 static unsigned ProcessImportWorklist(
78 Module &DestModule, SmallVector<StringRef, 64> &Worklist,
79 StringSet<> &CalledFunctions, Linker &TheLinker,
80 const FunctionInfoIndex &Index,
81 std::function<std::unique_ptr<Module>(StringRef FileName)> &
83 unsigned ImportCount = 0;
84 while (!Worklist.empty()) {
85 auto CalledFunctionName = Worklist.pop_back_val();
86 DEBUG(dbgs() << DestModule.getModuleIdentifier() << "Process import for "
87 << CalledFunctionName << "\n");
89 // Try to get a summary for this function call.
90 auto InfoList = Index.findFunctionInfoList(CalledFunctionName);
91 if (InfoList == Index.end()) {
92 DEBUG(dbgs() << DestModule.getModuleIdentifier() << "No summary for "
93 << CalledFunctionName << " Ignoring.\n");
96 assert(!InfoList->second.empty() && "No summary, error at import?");
98 // Comdat can have multiple entries, FIXME: what do we do with them?
99 auto &Info = InfoList->second[0];
100 assert(Info && "Nullptr in list, error importing summaries?\n");
102 auto *Summary = Info->functionSummary();
104 // FIXME: in case we are lazyloading summaries, we can do it now.
105 DEBUG(dbgs() << DestModule.getModuleIdentifier()
106 << " Missing summary for " << CalledFunctionName
107 << ", error at import?\n");
108 llvm_unreachable("Missing summary");
111 if (Summary->instCount() > ImportInstrLimit) {
112 DEBUG(dbgs() << DestModule.getModuleIdentifier() << " Skip import of "
113 << CalledFunctionName << " with " << Summary->instCount()
114 << " instructions (limit " << ImportInstrLimit << ")\n");
118 // Get the module path from the summary.
119 auto FileName = Summary->modulePath();
120 DEBUG(dbgs() << "Importing " << CalledFunctionName << " from " << FileName
123 // Get the module for the import
124 auto SrcModule = LazyModuleLoader(FileName);
125 assert(&SrcModule->getContext() == &DestModule.getContext());
127 // The function that we will import!
128 GlobalValue *SGV = SrcModule->getNamedValue(CalledFunctionName);
129 StringRef ImportFunctionName = CalledFunctionName;
131 // Might be local in source Module, promoted/renamed in DestModule.
132 std::pair<StringRef, StringRef> Split =
133 CalledFunctionName.split(".llvm.");
134 SGV = SrcModule->getNamedValue(Split.first);
136 // Assert that Split.second is module id
138 assert(!Split.second.getAsInteger(10, ModuleId));
139 assert(ModuleId == Index.getModuleId(FileName));
142 Function *F = dyn_cast<Function>(SGV);
143 if (!F && isa<GlobalAlias>(SGV)) {
144 auto *SGA = dyn_cast<GlobalAlias>(SGV);
145 F = dyn_cast<Function>(SGA->getBaseObject());
146 ImportFunctionName = F->getName();
149 errs() << "Can't load function '" << CalledFunctionName << "' in Module '"
150 << FileName << "', error in the summary?\n";
151 llvm_unreachable("Can't load function in Module");
154 // We cannot import weak_any functions/aliases without possibly affecting
155 // the order they are seen and selected by the linker, changing program
157 if (SGV->hasWeakAnyLinkage()) {
158 DEBUG(dbgs() << DestModule.getModuleIdentifier()
159 << " Ignoring import request for weak-any "
160 << (isa<Function>(SGV) ? "function " : "alias ")
161 << CalledFunctionName << " from " << FileName << "\n");
165 // Link in the specified function.
166 DenseSet<const GlobalValue *> FunctionsToImport;
167 FunctionsToImport.insert(F);
168 if (TheLinker.linkInModule(*SrcModule, Linker::Flags::None, &Index,
170 report_fatal_error("Function Import: link error");
172 // Process the newly imported function and add callees to the worklist.
173 GlobalValue *NewGV = DestModule.getNamedValue(ImportFunctionName);
175 Function *NewF = dyn_cast<Function>(NewGV);
177 findExternalCalls(*NewF, CalledFunctions, Worklist);
183 // Automatically import functions in Module \p DestModule based on the summaries
186 // The current implementation imports every called functions that exists in the
188 bool FunctionImporter::importFunctions(Module &DestModule) {
189 DEBUG(dbgs() << "Starting import for Module "
190 << DestModule.getModuleIdentifier() << "\n");
191 unsigned ImportedCount = 0;
193 /// First step is collecting the called external functions.
194 StringSet<> CalledFunctions;
195 SmallVector<StringRef, 64> Worklist;
196 for (auto &F : DestModule) {
197 if (F.isDeclaration() || F.hasFnAttribute(Attribute::OptimizeNone))
199 findExternalCalls(F, CalledFunctions, Worklist);
201 if (Worklist.empty())
204 /// Second step: for every call to an external function, try to import it.
206 // Linker that will be used for importing function
207 Linker TheLinker(DestModule, DiagnosticHandler);
209 ImportedCount += ProcessImportWorklist(DestModule, Worklist, CalledFunctions,
210 TheLinker, Index, ModuleLoader);
212 DEBUG(errs() << "Imported " << ImportedCount << " functions for Module "
213 << DestModule.getModuleIdentifier() << "\n");
214 return ImportedCount;
217 /// Summary file to use for function importing when using -function-import from
218 /// the command line.
219 static cl::opt<std::string>
220 SummaryFile("summary-file",
221 cl::desc("The summary file to use for function importing."));
223 static void diagnosticHandler(const DiagnosticInfo &DI) {
224 raw_ostream &OS = errs();
225 DiagnosticPrinterRawOStream DP(OS);
230 /// Parse the function index out of an IR file and return the function
231 /// index object if found, or nullptr if not.
232 static std::unique_ptr<FunctionInfoIndex>
233 getFunctionIndexForFile(StringRef Path, std::string &Error,
234 DiagnosticHandlerFunction DiagnosticHandler) {
235 std::unique_ptr<MemoryBuffer> Buffer;
236 ErrorOr<std::unique_ptr<MemoryBuffer>> BufferOrErr =
237 MemoryBuffer::getFile(Path);
238 if (std::error_code EC = BufferOrErr.getError()) {
239 Error = EC.message();
242 Buffer = std::move(BufferOrErr.get());
243 ErrorOr<std::unique_ptr<object::FunctionIndexObjectFile>> ObjOrErr =
244 object::FunctionIndexObjectFile::create(Buffer->getMemBufferRef(),
246 if (std::error_code EC = ObjOrErr.getError()) {
247 Error = EC.message();
250 return (*ObjOrErr)->takeIndex();
253 /// Pass that performs cross-module function import provided a summary file.
254 class FunctionImportPass : public ModulePass {
255 /// Optional function summary index to use for importing, otherwise
256 /// the summary-file option must be specified.
257 FunctionInfoIndex *Index;
260 /// Pass identification, replacement for typeid
263 /// Specify pass name for debug output
264 const char *getPassName() const override {
265 return "Function Importing";
268 explicit FunctionImportPass(FunctionInfoIndex *Index = nullptr)
269 : ModulePass(ID), Index(Index) {}
271 bool runOnModule(Module &M) override {
272 if (SummaryFile.empty() && !Index)
273 report_fatal_error("error: -function-import requires -summary-file or "
274 "file from frontend\n");
275 std::unique_ptr<FunctionInfoIndex> IndexPtr;
276 if (!SummaryFile.empty()) {
278 report_fatal_error("error: -summary-file and index from frontend\n");
280 IndexPtr = getFunctionIndexForFile(SummaryFile, Error, diagnosticHandler);
282 errs() << "Error loading file '" << SummaryFile << "': " << Error
286 Index = IndexPtr.get();
289 // Perform the import now.
290 auto ModuleLoader = [&M](StringRef Identifier) {
291 return loadFile(Identifier, M.getContext());
293 FunctionImporter Importer(*Index, diagnosticHandler, ModuleLoader);
294 return Importer.importFunctions(M);
300 char FunctionImportPass::ID = 0;
301 INITIALIZE_PASS_BEGIN(FunctionImportPass, "function-import",
302 "Summary Based Function Import", false, false)
303 INITIALIZE_PASS_END(FunctionImportPass, "function-import",
304 "Summary Based Function Import", false, false)
307 Pass *createFunctionImportPass(FunctionInfoIndex *Index = nullptr) {
308 return new FunctionImportPass(Index);