blob: a99739c9ae895d9313a0daf0e7d5885bc1c82794 [file] [log] [blame]
Mehdi Amini42418ab2015-11-24 06:07:49 +00001//===- 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
Mehdi Amini01e32132016-03-26 05:40:34 +000016#include "llvm/ADT/SmallVector.h"
Teresa Johnsond29478f2016-03-27 15:27:30 +000017#include "llvm/ADT/Statistic.h"
Mehdi Amini42418ab2015-11-24 06:07:49 +000018#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"
Teresa Johnson26ab5772016-03-15 00:04:37 +000025#include "llvm/Object/ModuleSummaryIndexObjectFile.h"
Mehdi Amini42418ab2015-11-24 06:07:49 +000026#include "llvm/Support/CommandLine.h"
27#include "llvm/Support/Debug.h"
28#include "llvm/Support/SourceMgr.h"
Teresa Johnson488a8002016-02-10 18:11:31 +000029#include "llvm/Transforms/Utils/FunctionImportUtils.h"
Mehdi Amini7e88d0d2015-12-09 08:17:35 +000030
Mehdi Amini01e32132016-03-26 05:40:34 +000031#define DEBUG_TYPE "function-import"
Mehdi Amini7e88d0d2015-12-09 08:17:35 +000032
Mehdi Amini42418ab2015-11-24 06:07:49 +000033using namespace llvm;
34
Teresa Johnsond29478f2016-03-27 15:27:30 +000035STATISTIC(NumImported, "Number of functions imported");
36
Teresa Johnson39303612015-11-24 22:55:46 +000037/// Limit on instruction count of imported functions.
38static 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
Mehdi Amini40641742016-02-10 23:31:45 +000042static 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
Teresa Johnsond29478f2016-03-27 15:27:30 +000049static cl::opt<bool> PrintImports("print-imports", cl::init(false), cl::Hidden,
50 cl::desc("Print imported functions"));
51
Mehdi Amini42418ab2015-11-24 06:07:49 +000052// Load lazily a module from \p FileName in \p Context.
53static std::unique_ptr<Module> loadFile(const std::string &FileName,
54 LLVMContext &Context) {
55 SMDiagnostic Err;
56 DEBUG(dbgs() << "Loading '" << FileName << "'\n");
Teresa Johnson6cba37c2016-01-22 00:15:53 +000057 // Metadata isn't loaded until functions are imported, to minimize
58 // the memory overhead.
Teresa Johnsona1080ee2016-01-08 14:17:41 +000059 std::unique_ptr<Module> Result =
60 getLazyIRFileModule(FileName, Err, Context,
61 /* ShouldLazyLoadMetadata = */ true);
Mehdi Amini42418ab2015-11-24 06:07:49 +000062 if (!Result) {
63 Err.print("function-import", errs());
64 return nullptr;
65 }
66
Mehdi Amini42418ab2015-11-24 06:07:49 +000067 return Result;
68}
69
Mehdi Amini7e88d0d2015-12-09 08:17:35 +000070namespace {
Mehdi Amini40641742016-02-10 23:31:45 +000071
Mehdi Amini01e32132016-03-26 05:40:34 +000072/// Given a list of possible callee implementation for a call site, select one
73/// that fits the \p Threshold.
74///
75/// FIXME: select "best" instead of first that fits. But what is "best"?
76/// - The smallest: more likely to be inlined.
77/// - The one with the least outgoing edges (already well optimized).
78/// - One from a module already being imported from in order to reduce the
79/// number of source modules parsed/linked.
80/// - One that has PGO data attached.
81/// - [insert you fancy metric here]
82static const FunctionSummary *
83selectCallee(const GlobalValueInfoList &CalleeInfoList, unsigned Threshold) {
84 auto It = llvm::find_if(
85 CalleeInfoList, [&](const std::unique_ptr<GlobalValueInfo> &GlobInfo) {
86 assert(GlobInfo->summary() &&
87 "We should not have a Global Info without summary");
88 auto *Summary = cast<FunctionSummary>(GlobInfo->summary());
Mehdi Amini40641742016-02-10 23:31:45 +000089
Mehdi Amini01e32132016-03-26 05:40:34 +000090 if (GlobalValue::isWeakAnyLinkage(Summary->linkage()))
91 return false;
Mehdi Amini7e88d0d2015-12-09 08:17:35 +000092
Mehdi Amini01e32132016-03-26 05:40:34 +000093 if (Summary->instCount() > Threshold)
94 return false;
Mehdi Amini7e88d0d2015-12-09 08:17:35 +000095
Mehdi Amini01e32132016-03-26 05:40:34 +000096 return true;
97 });
98 if (It == CalleeInfoList.end())
99 return nullptr;
Mehdi Amini7e88d0d2015-12-09 08:17:35 +0000100
Mehdi Amini01e32132016-03-26 05:40:34 +0000101 return cast<FunctionSummary>((*It)->summary());
Mehdi Amini7e88d0d2015-12-09 08:17:35 +0000102}
Mehdi Amini7e88d0d2015-12-09 08:17:35 +0000103
Mehdi Amini01e32132016-03-26 05:40:34 +0000104/// Return the summary for the function \p GUID that fits the \p Threshold, or
105/// null if there's no match.
106static const FunctionSummary *selectCallee(uint64_t GUID, unsigned Threshold,
107 const ModuleSummaryIndex &Index) {
108 auto CalleeInfoList = Index.findGlobalValueInfoList(GUID);
109 if (CalleeInfoList == Index.end()) {
110 return nullptr; // This function does not have a summary
111 }
112 return selectCallee(CalleeInfoList->second, Threshold);
113}
Mehdi Amini7e88d0d2015-12-09 08:17:35 +0000114
Mehdi Amini01e32132016-03-26 05:40:34 +0000115/// Return true if the global \p GUID is exported by module \p ExportModulePath.
116static bool isGlobalExported(const ModuleSummaryIndex &Index,
117 StringRef ExportModulePath, uint64_t GUID) {
118 auto CalleeInfoList = Index.findGlobalValueInfoList(GUID);
119 if (CalleeInfoList == Index.end())
120 // This global does not have a summary, it is not part of the ThinLTO
121 // process
122 return false;
123 auto DefinedInCalleeModule = llvm::find_if(
124 CalleeInfoList->second,
125 [&](const std::unique_ptr<GlobalValueInfo> &GlobInfo) {
126 auto *Summary = GlobInfo->summary();
127 assert(Summary && "Unexpected GlobalValueInfo without summary");
128 return Summary->modulePath() == ExportModulePath;
129 });
130 return (DefinedInCalleeModule != CalleeInfoList->second.end());
131}
Mehdi Amini40641742016-02-10 23:31:45 +0000132
Mehdi Amini01e32132016-03-26 05:40:34 +0000133using EdgeInfo = std::pair<const FunctionSummary *, unsigned /* Threshold */>;
Mehdi Amini7e88d0d2015-12-09 08:17:35 +0000134
Mehdi Amini01e32132016-03-26 05:40:34 +0000135/// Compute the list of functions to import for a given caller. Mark these
136/// imported functions and the symbols they reference in their source module as
137/// exported from their source module.
138static void computeImportForFunction(
139 StringRef ModulePath, const FunctionSummary &Summary,
140 const ModuleSummaryIndex &Index, unsigned Threshold,
141 const std::map<uint64_t, FunctionSummary *> &DefinedFunctions,
142 SmallVectorImpl<EdgeInfo> &Worklist,
143 FunctionImporter::ImportMapTy &ImportsForModule,
144 StringMap<FunctionImporter::ExportSetTy> &ExportLists) {
145 for (auto &Edge : Summary.calls()) {
146 auto GUID = Edge.first;
147 DEBUG(dbgs() << " edge -> " << GUID << " Threshold:" << Threshold << "\n");
148
149 if (DefinedFunctions.count(GUID)) {
150 DEBUG(dbgs() << "ignored! Target already in destination module.\n");
151 continue;
Teresa Johnsond450da32015-11-24 21:15:19 +0000152 }
Mehdi Amini01e32132016-03-26 05:40:34 +0000153
154 auto *CalleeSummary = selectCallee(GUID, Threshold, Index);
155 if (!CalleeSummary) {
156 DEBUG(dbgs() << "ignored! No qualifying callee with summary found.\n");
157 continue;
158 }
159 assert(CalleeSummary->instCount() <= Threshold &&
160 "selectCallee() didn't honor the threshold");
161
162 auto &ProcessedThreshold =
163 ImportsForModule[CalleeSummary->modulePath()][GUID];
164 /// Since the traversal of the call graph is DFS, we can revisit a function
165 /// a second time with a higher threshold. In this case, it is added back to
166 /// the worklist with the new threshold.
167 if (ProcessedThreshold && ProcessedThreshold > Threshold) {
168 DEBUG(dbgs() << "ignored! Target was already seen with Threshold "
169 << ProcessedThreshold << "\n");
170 continue;
171 }
172 // Mark this function as imported in this module, with the current Threshold
173 ProcessedThreshold = Threshold;
174
175 // Make exports in the source module.
176 auto ExportModulePath = CalleeSummary->modulePath();
177 auto ExportList = ExportLists[ExportModulePath];
178 ExportList.insert(GUID);
179 // Mark all functions and globals referenced by this function as exported to
180 // the outside if they are defined in the same source module.
181 for (auto &Edge : CalleeSummary->calls()) {
182 auto CalleeGUID = Edge.first;
183 if (isGlobalExported(Index, ExportModulePath, CalleeGUID))
184 ExportList.insert(CalleeGUID);
185 }
186 for (auto &GUID : CalleeSummary->refs()) {
187 if (isGlobalExported(Index, ExportModulePath, GUID))
188 ExportList.insert(GUID);
189 }
190
191 // Insert the newly imported function to the worklist.
192 Worklist.push_back(std::make_pair(CalleeSummary, Threshold));
Teresa Johnsond450da32015-11-24 21:15:19 +0000193 }
194}
195
Mehdi Amini01e32132016-03-26 05:40:34 +0000196/// Given the list of globals defined in a module, compute the list of imports
197/// as well as the list of "exports", i.e. the list of symbols referenced from
198/// another module (that may require promotion).
199static void ComputeImportForModule(
200 StringRef ModulePath,
201 const std::map<uint64_t, FunctionSummary *> &DefinedFunctions,
202 const ModuleSummaryIndex &Index,
203 FunctionImporter::ImportMapTy &ImportsForModule,
204 StringMap<FunctionImporter::ExportSetTy> &ExportLists) {
205 // Worklist contains the list of function imported in this module, for which
206 // we will analyse the callees and may import further down the callgraph.
207 SmallVector<EdgeInfo, 128> Worklist;
208
209 // Populate the worklist with the import for the functions in the current
210 // module
211 for (auto &FuncInfo : DefinedFunctions) {
212 auto *Summary = FuncInfo.second;
213 DEBUG(dbgs() << "Initalize import for " << FuncInfo.first << "\n");
214 computeImportForFunction(ModulePath, *Summary, Index, ImportInstrLimit,
215 DefinedFunctions, Worklist, ImportsForModule,
216 ExportLists);
217 }
218
Mehdi Amini42418ab2015-11-24 06:07:49 +0000219 while (!Worklist.empty()) {
Mehdi Amini01e32132016-03-26 05:40:34 +0000220 auto FuncInfo = Worklist.pop_back_val();
221 auto *Summary = FuncInfo.first;
222 auto Threshold = FuncInfo.second;
Mehdi Amini42418ab2015-11-24 06:07:49 +0000223
Mehdi Amini7e88d0d2015-12-09 08:17:35 +0000224 // Process the newly imported functions and add callees to the worklist.
Mehdi Amini40641742016-02-10 23:31:45 +0000225 // Adjust the threshold
226 Threshold = Threshold * ImportInstrFactor;
Mehdi Amini01e32132016-03-26 05:40:34 +0000227
228 computeImportForFunction(ModulePath, *Summary, Index, Threshold,
229 DefinedFunctions, Worklist, ImportsForModule,
230 ExportLists);
Mehdi Amini42418ab2015-11-24 06:07:49 +0000231 }
Mehdi Aminic8c55172015-12-03 02:37:33 +0000232}
Mehdi Aminiffe2e4a2015-12-02 04:34:28 +0000233
Mehdi Amini01e32132016-03-26 05:40:34 +0000234} // anonymous namespace
235
236/// Compute all the import and export for every module in the Index.
237void llvm::ComputeCrossModuleImport(
238 const ModuleSummaryIndex &Index,
239 StringMap<FunctionImporter::ImportMapTy> &ImportLists,
240 StringMap<FunctionImporter::ExportSetTy> &ExportLists) {
241 auto ModuleCount = Index.modulePaths().size();
242
243 // Collect for each module the list of function it defines.
244 // GUID -> Summary
245 StringMap<std::map<uint64_t, FunctionSummary *>> Module2FunctionInfoMap(
246 ModuleCount);
247
248 for (auto &GlobalList : Index) {
249 auto GUID = GlobalList.first;
250 for (auto &GlobInfo : GlobalList.second) {
251 auto *Summary = dyn_cast_or_null<FunctionSummary>(GlobInfo->summary());
252 if (!Summary)
253 /// Ignore global variable, focus on functions
254 continue;
255 DEBUG(dbgs() << "Adding definition: Module '" << Summary->modulePath()
256 << "' defines '" << GUID << "'\n");
257 Module2FunctionInfoMap[Summary->modulePath()][GUID] = Summary;
258 }
259 }
260
261 // For each module that has function defined, compute the import/export lists.
262 for (auto &DefinedFunctions : Module2FunctionInfoMap) {
263 auto &ImportsForModule = ImportLists[DefinedFunctions.first()];
264 DEBUG(dbgs() << "Computing import for Module '" << DefinedFunctions.first()
265 << "'\n");
266 ComputeImportForModule(DefinedFunctions.first(), DefinedFunctions.second,
267 Index, ImportsForModule, ExportLists);
268 }
269
270#ifndef NDEBUG
271 DEBUG(dbgs() << "Import/Export lists for " << ImportLists.size()
272 << " modules:\n");
273 for (auto &ModuleImports : ImportLists) {
274 auto ModName = ModuleImports.first();
275 auto &Exports = ExportLists[ModName];
276 DEBUG(dbgs() << "* Module " << ModName << " exports " << Exports.size()
277 << " functions. Imports from " << ModuleImports.second.size()
278 << " modules.\n");
279 for (auto &Src : ModuleImports.second) {
280 auto SrcModName = Src.first();
281 DEBUG(dbgs() << " - " << Src.second.size() << " functions imported from "
282 << SrcModName << "\n");
283 }
284 }
285#endif
286}
287
Mehdi Aminic8c55172015-12-03 02:37:33 +0000288// Automatically import functions in Module \p DestModule based on the summaries
289// index.
290//
Mehdi Amini01e32132016-03-26 05:40:34 +0000291bool FunctionImporter::importFunctions(
292 Module &DestModule, const FunctionImporter::ImportMapTy &ImportList) {
Mehdi Amini5411d052015-12-08 23:04:19 +0000293 DEBUG(dbgs() << "Starting import for Module "
Mehdi Amini311fef62015-12-03 02:58:14 +0000294 << DestModule.getModuleIdentifier() << "\n");
Mehdi Aminic8c55172015-12-03 02:37:33 +0000295 unsigned ImportedCount = 0;
296
Mehdi Aminic8c55172015-12-03 02:37:33 +0000297 // Linker that will be used for importing function
Rafael Espindola9d2bfc42015-12-14 23:17:03 +0000298 Linker TheLinker(DestModule);
Mehdi Amini7e88d0d2015-12-09 08:17:35 +0000299 // Do the actual import of functions now, one Module at a time
Mehdi Amini01e32132016-03-26 05:40:34 +0000300 std::set<StringRef> ModuleNameOrderedList;
301 for (auto &FunctionsToImportPerModule : ImportList) {
302 ModuleNameOrderedList.insert(FunctionsToImportPerModule.first());
303 }
304 for (auto &Name : ModuleNameOrderedList) {
Mehdi Amini7e88d0d2015-12-09 08:17:35 +0000305 // Get the module for the import
Mehdi Amini01e32132016-03-26 05:40:34 +0000306 const auto &FunctionsToImportPerModule = ImportList.find(Name);
307 assert(FunctionsToImportPerModule != ImportList.end());
308 std::unique_ptr<Module> SrcModule = ModuleLoader(Name);
Mehdi Amini7e88d0d2015-12-09 08:17:35 +0000309 assert(&DestModule.getContext() == &SrcModule->getContext() &&
310 "Context mismatch");
311
Teresa Johnson6cba37c2016-01-22 00:15:53 +0000312 // If modules were created with lazy metadata loading, materialize it
313 // now, before linking it (otherwise this will be a noop).
314 SrcModule->materializeMetadata();
315 UpgradeDebugInfo(*SrcModule);
Teresa Johnsone5a61912015-12-17 17:14:09 +0000316
Mehdi Amini01e32132016-03-26 05:40:34 +0000317 auto &ImportGUIDs = FunctionsToImportPerModule->second;
318 // Find the globals to import
319 DenseSet<const GlobalValue *> GlobalsToImport;
320 for (auto &GV : *SrcModule) {
321 if (GV.hasName() && ImportGUIDs.count(GV.getGUID())) {
322 GV.materialize();
323 GlobalsToImport.insert(&GV);
324 }
325 }
326 for (auto &GV : SrcModule->aliases()) {
327 if (!GV.hasName())
328 continue;
329 auto GUID = GV.getGUID();
330 if (ImportGUIDs.count(GUID)) {
Mehdi Amini01e32132016-03-26 05:40:34 +0000331 // Alias can't point to "available_externally". However when we import
Teresa Johnson9aae3952016-03-27 15:01:11 +0000332 // linkOnceODR the linkage does not change. So we import the alias
333 // and aliasee only in this case.
Mehdi Amini01e32132016-03-26 05:40:34 +0000334 const GlobalObject *GO = GV.getBaseObject();
335 if (!GO->hasLinkOnceODRLinkage())
336 continue;
Teresa Johnson9aae3952016-03-27 15:01:11 +0000337 GV.materialize();
338 GlobalsToImport.insert(&GV);
Mehdi Amini01e32132016-03-26 05:40:34 +0000339 GlobalsToImport.insert(GO);
340 }
341 }
342 for (auto &GV : SrcModule->globals()) {
343 if (!GV.hasName())
344 continue;
345 auto GUID = Function::getGUID(Function::getGlobalIdentifier(
346 GV.getName(), GV.getLinkage(), SrcModule->getModuleIdentifier()));
347 if (ImportGUIDs.count(GUID)) {
348 GV.materialize();
349 GlobalsToImport.insert(&GV);
350 }
351 }
352
Mehdi Amini7e88d0d2015-12-09 08:17:35 +0000353 // Link in the specified functions.
Mehdi Amini01e32132016-03-26 05:40:34 +0000354 if (renameModuleForThinLTO(*SrcModule, Index, &GlobalsToImport))
Mehdi Amini8d051852016-03-19 00:40:31 +0000355 return true;
356
Teresa Johnsond29478f2016-03-27 15:27:30 +0000357 if (PrintImports) {
358 for (const auto *GV : GlobalsToImport)
359 dbgs() << DestModule.getSourceFileName() << ": Import " << GV->getName()
360 << " from " << SrcModule->getSourceFileName() << "\n";
361 }
362
Rafael Espindola434e9562015-12-16 23:16:33 +0000363 if (TheLinker.linkInModule(std::move(SrcModule), Linker::Flags::None,
Mehdi Amini01e32132016-03-26 05:40:34 +0000364 &GlobalsToImport))
Mehdi Amini7e88d0d2015-12-09 08:17:35 +0000365 report_fatal_error("Function Import: link error");
366
Mehdi Amini01e32132016-03-26 05:40:34 +0000367 ImportedCount += GlobalsToImport.size();
Mehdi Amini7e88d0d2015-12-09 08:17:35 +0000368 }
Teresa Johnsone5a61912015-12-17 17:14:09 +0000369
Teresa Johnsond29478f2016-03-27 15:27:30 +0000370 NumImported += ImportedCount;
371
Mehdi Amini7e88d0d2015-12-09 08:17:35 +0000372 DEBUG(dbgs() << "Imported " << ImportedCount << " functions for Module "
Mehdi Aminic8c55172015-12-03 02:37:33 +0000373 << DestModule.getModuleIdentifier() << "\n");
374 return ImportedCount;
Mehdi Amini42418ab2015-11-24 06:07:49 +0000375}
376
377/// Summary file to use for function importing when using -function-import from
378/// the command line.
379static cl::opt<std::string>
380 SummaryFile("summary-file",
381 cl::desc("The summary file to use for function importing."));
382
383static void diagnosticHandler(const DiagnosticInfo &DI) {
384 raw_ostream &OS = errs();
385 DiagnosticPrinterRawOStream DP(OS);
386 DI.print(DP);
387 OS << '\n';
388}
389
Teresa Johnson26ab5772016-03-15 00:04:37 +0000390/// Parse the summary index out of an IR file and return the summary
Mehdi Amini42418ab2015-11-24 06:07:49 +0000391/// index object if found, or nullptr if not.
Teresa Johnson26ab5772016-03-15 00:04:37 +0000392static std::unique_ptr<ModuleSummaryIndex>
393getModuleSummaryIndexForFile(StringRef Path, std::string &Error,
394 DiagnosticHandlerFunction DiagnosticHandler) {
Mehdi Amini42418ab2015-11-24 06:07:49 +0000395 std::unique_ptr<MemoryBuffer> Buffer;
396 ErrorOr<std::unique_ptr<MemoryBuffer>> BufferOrErr =
397 MemoryBuffer::getFile(Path);
398 if (std::error_code EC = BufferOrErr.getError()) {
399 Error = EC.message();
400 return nullptr;
401 }
402 Buffer = std::move(BufferOrErr.get());
Teresa Johnson26ab5772016-03-15 00:04:37 +0000403 ErrorOr<std::unique_ptr<object::ModuleSummaryIndexObjectFile>> ObjOrErr =
404 object::ModuleSummaryIndexObjectFile::create(Buffer->getMemBufferRef(),
405 DiagnosticHandler);
Mehdi Amini42418ab2015-11-24 06:07:49 +0000406 if (std::error_code EC = ObjOrErr.getError()) {
407 Error = EC.message();
408 return nullptr;
409 }
410 return (*ObjOrErr)->takeIndex();
411}
412
Benjamin Kramerfe2b5412015-12-24 10:03:35 +0000413namespace {
Mehdi Amini42418ab2015-11-24 06:07:49 +0000414/// Pass that performs cross-module function import provided a summary file.
415class FunctionImportPass : public ModulePass {
Teresa Johnson26ab5772016-03-15 00:04:37 +0000416 /// Optional module summary index to use for importing, otherwise
Teresa Johnson5fcbdb72015-12-07 19:21:11 +0000417 /// the summary-file option must be specified.
Teresa Johnson26ab5772016-03-15 00:04:37 +0000418 const ModuleSummaryIndex *Index;
Mehdi Amini42418ab2015-11-24 06:07:49 +0000419
420public:
421 /// Pass identification, replacement for typeid
422 static char ID;
423
Teresa Johnson5fcbdb72015-12-07 19:21:11 +0000424 /// Specify pass name for debug output
425 const char *getPassName() const override {
426 return "Function Importing";
427 }
428
Teresa Johnson26ab5772016-03-15 00:04:37 +0000429 explicit FunctionImportPass(const ModuleSummaryIndex *Index = nullptr)
Teresa Johnson5fcbdb72015-12-07 19:21:11 +0000430 : ModulePass(ID), Index(Index) {}
Mehdi Amini42418ab2015-11-24 06:07:49 +0000431
432 bool runOnModule(Module &M) override {
Teresa Johnson5fcbdb72015-12-07 19:21:11 +0000433 if (SummaryFile.empty() && !Index)
434 report_fatal_error("error: -function-import requires -summary-file or "
435 "file from frontend\n");
Teresa Johnson26ab5772016-03-15 00:04:37 +0000436 std::unique_ptr<ModuleSummaryIndex> IndexPtr;
Teresa Johnson5fcbdb72015-12-07 19:21:11 +0000437 if (!SummaryFile.empty()) {
438 if (Index)
439 report_fatal_error("error: -summary-file and index from frontend\n");
440 std::string Error;
Teresa Johnson26ab5772016-03-15 00:04:37 +0000441 IndexPtr =
442 getModuleSummaryIndexForFile(SummaryFile, Error, diagnosticHandler);
Teresa Johnson5fcbdb72015-12-07 19:21:11 +0000443 if (!IndexPtr) {
444 errs() << "Error loading file '" << SummaryFile << "': " << Error
445 << "\n";
446 return false;
447 }
448 Index = IndexPtr.get();
Mehdi Amini42418ab2015-11-24 06:07:49 +0000449 }
450
Mehdi Amini01e32132016-03-26 05:40:34 +0000451 // First step is collecting the import/export lists
452 // The export list is not used yet, but could limit the amount of renaming
453 // performed in renameModuleForThinLTO()
454 StringMap<FunctionImporter::ImportMapTy> ImportLists;
455 StringMap<FunctionImporter::ExportSetTy> ExportLists;
456 ComputeCrossModuleImport(*Index, ImportLists, ExportLists);
457 auto &ImportList = ImportLists[M.getModuleIdentifier()];
458
459 // Next we need to promote to global scope and rename any local values that
Teresa Johnson1b00f2d2016-01-08 17:06:29 +0000460 // are potentially exported to other modules.
Mehdi Amini01e32132016-03-26 05:40:34 +0000461 if (renameModuleForThinLTO(M, *Index, nullptr)) {
Teresa Johnson1b00f2d2016-01-08 17:06:29 +0000462 errs() << "Error renaming module\n";
463 return false;
464 }
465
Mehdi Amini42418ab2015-11-24 06:07:49 +0000466 // Perform the import now.
Mehdi Aminid16c8062015-12-08 22:39:40 +0000467 auto ModuleLoader = [&M](StringRef Identifier) {
468 return loadFile(Identifier, M.getContext());
469 };
Rafael Espindola9d2bfc42015-12-14 23:17:03 +0000470 FunctionImporter Importer(*Index, ModuleLoader);
Mehdi Amini01e32132016-03-26 05:40:34 +0000471 return Importer.importFunctions(M, ImportList);
Mehdi Amini42418ab2015-11-24 06:07:49 +0000472 }
473};
Benjamin Kramerfe2b5412015-12-24 10:03:35 +0000474} // anonymous namespace
Mehdi Amini42418ab2015-11-24 06:07:49 +0000475
476char FunctionImportPass::ID = 0;
477INITIALIZE_PASS_BEGIN(FunctionImportPass, "function-import",
478 "Summary Based Function Import", false, false)
479INITIALIZE_PASS_END(FunctionImportPass, "function-import",
480 "Summary Based Function Import", false, false)
481
482namespace llvm {
Teresa Johnson26ab5772016-03-15 00:04:37 +0000483Pass *createFunctionImportPass(const ModuleSummaryIndex *Index = nullptr) {
Teresa Johnson5fcbdb72015-12-07 19:21:11 +0000484 return new FunctionImportPass(Index);
485}
Mehdi Amini42418ab2015-11-24 06:07:49 +0000486}