blob: 11418edbf7bc1634f4d42717e17843612cd55225 [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
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"
Mehdi Amini7e88d0d2015-12-09 08:17:35 +000027
28#include <map>
29
Mehdi Amini42418ab2015-11-24 06:07:49 +000030using namespace llvm;
31
32#define DEBUG_TYPE "function-import"
33
Teresa Johnson39303612015-11-24 22:55:46 +000034/// Limit on instruction count of imported functions.
35static cl::opt<unsigned> ImportInstrLimit(
36 "import-instr-limit", cl::init(100), cl::Hidden, cl::value_desc("N"),
37 cl::desc("Only import functions with less than N instructions"));
38
Mehdi Amini42418ab2015-11-24 06:07:49 +000039// Load lazily a module from \p FileName in \p Context.
40static std::unique_ptr<Module> loadFile(const std::string &FileName,
41 LLVMContext &Context) {
42 SMDiagnostic Err;
43 DEBUG(dbgs() << "Loading '" << FileName << "'\n");
Teresa Johnsona1080ee2016-01-08 14:17:41 +000044 // Metadata isn't loaded or linked until after all functions are
45 // imported, after which it will be materialized and linked.
46 std::unique_ptr<Module> Result =
47 getLazyIRFileModule(FileName, Err, Context,
48 /* ShouldLazyLoadMetadata = */ true);
Mehdi Amini42418ab2015-11-24 06:07:49 +000049 if (!Result) {
50 Err.print("function-import", errs());
51 return nullptr;
52 }
53
Mehdi Amini42418ab2015-11-24 06:07:49 +000054 return Result;
55}
56
Mehdi Amini7e88d0d2015-12-09 08:17:35 +000057namespace {
58/// Helper to load on demand a Module from file and cache it for subsequent
59/// queries. It can be used with the FunctionImporter.
60class ModuleLazyLoaderCache {
61 /// Cache of lazily loaded module for import.
62 StringMap<std::unique_ptr<Module>> ModuleMap;
63
64 /// Retrieve a Module from the cache or lazily load it on demand.
65 std::function<std::unique_ptr<Module>(StringRef FileName)> createLazyModule;
66
67public:
68 /// Create the loader, Module will be initialized in \p Context.
69 ModuleLazyLoaderCache(std::function<
70 std::unique_ptr<Module>(StringRef FileName)> createLazyModule)
71 : createLazyModule(createLazyModule) {}
72
73 /// Retrieve a Module from the cache or lazily load it on demand.
74 Module &operator()(StringRef FileName);
Rafael Espindola434e9562015-12-16 23:16:33 +000075
76 std::unique_ptr<Module> takeModule(StringRef FileName) {
77 auto I = ModuleMap.find(FileName);
78 assert(I != ModuleMap.end());
79 std::unique_ptr<Module> Ret = std::move(I->second);
80 ModuleMap.erase(I);
81 return Ret;
82 }
Mehdi Amini7e88d0d2015-12-09 08:17:35 +000083};
84
85// Get a Module for \p FileName from the cache, or load it lazily.
86Module &ModuleLazyLoaderCache::operator()(StringRef Identifier) {
87 auto &Module = ModuleMap[Identifier];
88 if (!Module)
89 Module = createLazyModule(Identifier);
90 return *Module;
91}
92} // anonymous namespace
93
Teresa Johnsond450da32015-11-24 21:15:19 +000094/// Walk through the instructions in \p F looking for external
95/// calls not already in the \p CalledFunctions set. If any are
96/// found they are added to the \p Worklist for importing.
Mehdi Amini7e88d0d2015-12-09 08:17:35 +000097static void findExternalCalls(const Module &DestModule, Function &F,
98 const FunctionInfoIndex &Index,
99 StringSet<> &CalledFunctions,
Teresa Johnsond450da32015-11-24 21:15:19 +0000100 SmallVector<StringRef, 64> &Worklist) {
Mehdi Amini7e88d0d2015-12-09 08:17:35 +0000101 // We need to suffix internal function calls imported from other modules,
102 // prepare the suffix ahead of time.
Rafael Espindola9edc3b82015-12-09 20:41:10 +0000103 std::string Suffix;
Mehdi Amini7e88d0d2015-12-09 08:17:35 +0000104 if (F.getParent() != &DestModule)
105 Suffix =
106 (Twine(".llvm.") +
107 Twine(Index.getModuleId(F.getParent()->getModuleIdentifier()))).str();
108
Teresa Johnsond450da32015-11-24 21:15:19 +0000109 for (auto &BB : F) {
110 for (auto &I : BB) {
111 if (isa<CallInst>(I)) {
Teresa Johnsond450da32015-11-24 21:15:19 +0000112 auto CalledFunction = cast<CallInst>(I).getCalledFunction();
113 // Insert any new external calls that have not already been
114 // added to set/worklist.
Mehdi Amini7e88d0d2015-12-09 08:17:35 +0000115 if (!CalledFunction || !CalledFunction->hasName())
116 continue;
117 // Ignore intrinsics early
118 if (CalledFunction->isIntrinsic()) {
119 assert(CalledFunction->getIntrinsicID() != 0);
120 continue;
Teresa Johnsond450da32015-11-24 21:15:19 +0000121 }
Mehdi Amini7e88d0d2015-12-09 08:17:35 +0000122 auto ImportedName = CalledFunction->getName();
123 auto Renamed = (ImportedName + Suffix).str();
124 // Rename internal functions
125 if (CalledFunction->hasInternalLinkage()) {
126 ImportedName = Renamed;
127 }
128 auto It = CalledFunctions.insert(ImportedName);
129 if (!It.second) {
130 // This is a call to a function we already considered, skip.
131 continue;
132 }
133 // Ignore functions already present in the destination module
134 auto *SrcGV = DestModule.getNamedValue(ImportedName);
135 if (SrcGV) {
136 assert(isa<Function>(SrcGV) && "Name collision during import");
137 if (!cast<Function>(SrcGV)->isDeclaration()) {
Teresa Johnson9f2ff9c2015-12-10 16:39:07 +0000138 DEBUG(dbgs() << DestModule.getModuleIdentifier() << ": Ignoring "
Mehdi Amini7e88d0d2015-12-09 08:17:35 +0000139 << ImportedName << " already in DestinationModule\n");
140 continue;
141 }
142 }
143
144 Worklist.push_back(It.first->getKey());
145 DEBUG(dbgs() << DestModule.getModuleIdentifier()
Teresa Johnson9f2ff9c2015-12-10 16:39:07 +0000146 << ": Adding callee for : " << ImportedName << " : "
Mehdi Amini7e88d0d2015-12-09 08:17:35 +0000147 << F.getName() << "\n");
Teresa Johnsond450da32015-11-24 21:15:19 +0000148 }
149 }
150 }
151}
152
Mehdi Aminic8c55172015-12-03 02:37:33 +0000153// Helper function: given a worklist and an index, will process all the worklist
Mehdi Amini7e88d0d2015-12-09 08:17:35 +0000154// and decide what to import based on the summary information.
155//
156// Nothing is actually imported, functions are materialized in their source
157// module and analyzed there.
158//
159// \p ModuleToFunctionsToImportMap is filled with the set of Function to import
160// per Module.
Rafael Espindola434e9562015-12-16 23:16:33 +0000161static void GetImportList(Module &DestModule,
162 SmallVector<StringRef, 64> &Worklist,
163 StringSet<> &CalledFunctions,
164 std::map<StringRef, DenseSet<const GlobalValue *>>
165 &ModuleToFunctionsToImportMap,
166 const FunctionInfoIndex &Index,
167 ModuleLazyLoaderCache &ModuleLoaderCache) {
Mehdi Amini42418ab2015-11-24 06:07:49 +0000168 while (!Worklist.empty()) {
169 auto CalledFunctionName = Worklist.pop_back_val();
Teresa Johnson9f2ff9c2015-12-10 16:39:07 +0000170 DEBUG(dbgs() << DestModule.getModuleIdentifier() << ": Process import for "
Mehdi Amini5411d052015-12-08 23:04:19 +0000171 << CalledFunctionName << "\n");
Mehdi Amini42418ab2015-11-24 06:07:49 +0000172
173 // Try to get a summary for this function call.
174 auto InfoList = Index.findFunctionInfoList(CalledFunctionName);
175 if (InfoList == Index.end()) {
Teresa Johnson9f2ff9c2015-12-10 16:39:07 +0000176 DEBUG(dbgs() << DestModule.getModuleIdentifier() << ": No summary for "
Mehdi Amini5411d052015-12-08 23:04:19 +0000177 << CalledFunctionName << " Ignoring.\n");
Mehdi Amini42418ab2015-11-24 06:07:49 +0000178 continue;
179 }
180 assert(!InfoList->second.empty() && "No summary, error at import?");
181
182 // Comdat can have multiple entries, FIXME: what do we do with them?
183 auto &Info = InfoList->second[0];
184 assert(Info && "Nullptr in list, error importing summaries?\n");
185
186 auto *Summary = Info->functionSummary();
187 if (!Summary) {
188 // FIXME: in case we are lazyloading summaries, we can do it now.
Mehdi Amini5411d052015-12-08 23:04:19 +0000189 DEBUG(dbgs() << DestModule.getModuleIdentifier()
Teresa Johnson9f2ff9c2015-12-10 16:39:07 +0000190 << ": Missing summary for " << CalledFunctionName
Teresa Johnson430110c2015-12-01 17:12:10 +0000191 << ", error at import?\n");
Mehdi Amini42418ab2015-11-24 06:07:49 +0000192 llvm_unreachable("Missing summary");
193 }
194
Teresa Johnson39303612015-11-24 22:55:46 +0000195 if (Summary->instCount() > ImportInstrLimit) {
Teresa Johnson9f2ff9c2015-12-10 16:39:07 +0000196 DEBUG(dbgs() << DestModule.getModuleIdentifier() << ": Skip import of "
Mehdi Amini5411d052015-12-08 23:04:19 +0000197 << CalledFunctionName << " with " << Summary->instCount()
198 << " instructions (limit " << ImportInstrLimit << ")\n");
Teresa Johnson39303612015-11-24 22:55:46 +0000199 continue;
200 }
201
Mehdi Amini42418ab2015-11-24 06:07:49 +0000202 // Get the module path from the summary.
Mehdi Amini7e88d0d2015-12-09 08:17:35 +0000203 auto ModuleIdentifier = Summary->modulePath();
Teresa Johnson9f2ff9c2015-12-10 16:39:07 +0000204 DEBUG(dbgs() << DestModule.getModuleIdentifier() << ": Importing "
Mehdi Amini7e88d0d2015-12-09 08:17:35 +0000205 << CalledFunctionName << " from " << ModuleIdentifier << "\n");
Mehdi Amini42418ab2015-11-24 06:07:49 +0000206
Mehdi Amini7e88d0d2015-12-09 08:17:35 +0000207 auto &SrcModule = ModuleLoaderCache(ModuleIdentifier);
Mehdi Amini42418ab2015-11-24 06:07:49 +0000208
209 // The function that we will import!
Mehdi Amini7e88d0d2015-12-09 08:17:35 +0000210 GlobalValue *SGV = SrcModule.getNamedValue(CalledFunctionName);
211
Teresa Johnson130de7a2015-11-24 19:55:04 +0000212 if (!SGV) {
Mehdi Amini7e88d0d2015-12-09 08:17:35 +0000213 // The destination module is referencing function using their renamed name
214 // when importing a function that was originally local in the source
215 // module. The source module we have might not have been renamed so we try
216 // to remove the suffix added during the renaming to recover the original
217 // name in the source module.
Teresa Johnson130de7a2015-11-24 19:55:04 +0000218 std::pair<StringRef, StringRef> Split =
219 CalledFunctionName.split(".llvm.");
Mehdi Amini7e88d0d2015-12-09 08:17:35 +0000220 SGV = SrcModule.getNamedValue(Split.first);
221 assert(SGV && "Can't find function to import in source module");
Teresa Johnson130de7a2015-11-24 19:55:04 +0000222 }
Mehdi Amini7e88d0d2015-12-09 08:17:35 +0000223 if (!SGV) {
224 report_fatal_error(Twine("Can't load function '") + CalledFunctionName +
225 "' in Module '" + SrcModule.getModuleIdentifier() +
226 "', error in the summary?\n");
227 }
228
Mehdi Amini42418ab2015-11-24 06:07:49 +0000229 Function *F = dyn_cast<Function>(SGV);
230 if (!F && isa<GlobalAlias>(SGV)) {
231 auto *SGA = dyn_cast<GlobalAlias>(SGV);
232 F = dyn_cast<Function>(SGA->getBaseObject());
Mehdi Amini7e88d0d2015-12-09 08:17:35 +0000233 CalledFunctionName = F->getName();
Mehdi Amini42418ab2015-11-24 06:07:49 +0000234 }
Mehdi Amini7e88d0d2015-12-09 08:17:35 +0000235 assert(F && "Imported Function is ... not a Function");
Mehdi Amini42418ab2015-11-24 06:07:49 +0000236
Teresa Johnson17626652015-11-24 16:10:43 +0000237 // We cannot import weak_any functions/aliases without possibly affecting
238 // the order they are seen and selected by the linker, changing program
Mehdi Amini42418ab2015-11-24 06:07:49 +0000239 // semantics.
Teresa Johnson17626652015-11-24 16:10:43 +0000240 if (SGV->hasWeakAnyLinkage()) {
Mehdi Amini5411d052015-12-08 23:04:19 +0000241 DEBUG(dbgs() << DestModule.getModuleIdentifier()
Teresa Johnson9f2ff9c2015-12-10 16:39:07 +0000242 << ": Ignoring import request for weak-any "
Teresa Johnson17626652015-11-24 16:10:43 +0000243 << (isa<Function>(SGV) ? "function " : "alias ")
Mehdi Amini7e88d0d2015-12-09 08:17:35 +0000244 << CalledFunctionName << " from "
245 << SrcModule.getModuleIdentifier() << "\n");
Mehdi Amini42418ab2015-11-24 06:07:49 +0000246 continue;
247 }
248
Mehdi Amini7e88d0d2015-12-09 08:17:35 +0000249 // Add the function to the import list
250 auto &Entry = ModuleToFunctionsToImportMap[SrcModule.getModuleIdentifier()];
Rafael Espindola434e9562015-12-16 23:16:33 +0000251 Entry.insert(F);
Mehdi Amini42418ab2015-11-24 06:07:49 +0000252
Mehdi Amini7e88d0d2015-12-09 08:17:35 +0000253 // Process the newly imported functions and add callees to the worklist.
254 F->materialize();
255 findExternalCalls(DestModule, *F, Index, CalledFunctions, Worklist);
Mehdi Amini42418ab2015-11-24 06:07:49 +0000256 }
Mehdi Aminic8c55172015-12-03 02:37:33 +0000257}
Mehdi Aminiffe2e4a2015-12-02 04:34:28 +0000258
Mehdi Aminic8c55172015-12-03 02:37:33 +0000259// Automatically import functions in Module \p DestModule based on the summaries
260// index.
261//
262// The current implementation imports every called functions that exists in the
263// summaries index.
264bool FunctionImporter::importFunctions(Module &DestModule) {
Mehdi Amini5411d052015-12-08 23:04:19 +0000265 DEBUG(dbgs() << "Starting import for Module "
Mehdi Amini311fef62015-12-03 02:58:14 +0000266 << DestModule.getModuleIdentifier() << "\n");
Mehdi Aminic8c55172015-12-03 02:37:33 +0000267 unsigned ImportedCount = 0;
268
269 /// First step is collecting the called external functions.
270 StringSet<> CalledFunctions;
271 SmallVector<StringRef, 64> Worklist;
272 for (auto &F : DestModule) {
273 if (F.isDeclaration() || F.hasFnAttribute(Attribute::OptimizeNone))
274 continue;
Mehdi Amini7e88d0d2015-12-09 08:17:35 +0000275 findExternalCalls(DestModule, F, Index, CalledFunctions, Worklist);
Mehdi Aminic8c55172015-12-03 02:37:33 +0000276 }
277 if (Worklist.empty())
278 return false;
279
280 /// Second step: for every call to an external function, try to import it.
281
282 // Linker that will be used for importing function
Rafael Espindola9d2bfc42015-12-14 23:17:03 +0000283 Linker TheLinker(DestModule);
Mehdi Aminic8c55172015-12-03 02:37:33 +0000284
Mehdi Amini7e88d0d2015-12-09 08:17:35 +0000285 // Map of Module -> List of Function to import from the Module
Rafael Espindola434e9562015-12-16 23:16:33 +0000286 std::map<StringRef, DenseSet<const GlobalValue *>>
Mehdi Amini7e88d0d2015-12-09 08:17:35 +0000287 ModuleToFunctionsToImportMap;
Mehdi Aminic8c55172015-12-03 02:37:33 +0000288
Mehdi Amini7e88d0d2015-12-09 08:17:35 +0000289 // Analyze the summaries and get the list of functions to import by
290 // populating ModuleToFunctionsToImportMap
291 ModuleLazyLoaderCache ModuleLoaderCache(ModuleLoader);
292 GetImportList(DestModule, Worklist, CalledFunctions,
293 ModuleToFunctionsToImportMap, Index, ModuleLoaderCache);
294 assert(Worklist.empty() && "Worklist hasn't been flushed in GetImportList");
295
Teresa Johnsone5a61912015-12-17 17:14:09 +0000296 StringMap<std::unique_ptr<DenseMap<unsigned, MDNode *>>>
297 ModuleToTempMDValsMap;
298
Mehdi Amini7e88d0d2015-12-09 08:17:35 +0000299 // Do the actual import of functions now, one Module at a time
300 for (auto &FunctionsToImportPerModule : ModuleToFunctionsToImportMap) {
301 // Get the module for the import
Rafael Espindola434e9562015-12-16 23:16:33 +0000302 auto &FunctionsToImport = FunctionsToImportPerModule.second;
303 std::unique_ptr<Module> SrcModule =
304 ModuleLoaderCache.takeModule(FunctionsToImportPerModule.first);
Mehdi Amini7e88d0d2015-12-09 08:17:35 +0000305 assert(&DestModule.getContext() == &SrcModule->getContext() &&
306 "Context mismatch");
307
Teresa Johnsone5a61912015-12-17 17:14:09 +0000308 // Save the mapping of value ids to temporary metadata created when
309 // importing this function. If we have already imported from this module,
310 // add new temporary metadata to the existing mapping.
311 auto &TempMDVals = ModuleToTempMDValsMap[SrcModule->getModuleIdentifier()];
312 if (!TempMDVals)
313 TempMDVals = llvm::make_unique<DenseMap<unsigned, MDNode *>>();
314
Mehdi Amini7e88d0d2015-12-09 08:17:35 +0000315 // Link in the specified functions.
Rafael Espindola434e9562015-12-16 23:16:33 +0000316 if (TheLinker.linkInModule(std::move(SrcModule), Linker::Flags::None,
Teresa Johnsone5a61912015-12-17 17:14:09 +0000317 &Index, &FunctionsToImport, TempMDVals.get()))
Mehdi Amini7e88d0d2015-12-09 08:17:35 +0000318 report_fatal_error("Function Import: link error");
319
320 ImportedCount += FunctionsToImport.size();
321 }
Teresa Johnsone5a61912015-12-17 17:14:09 +0000322
323 // Now link in metadata for all modules from which we imported functions.
324 for (StringMapEntry<std::unique_ptr<DenseMap<unsigned, MDNode *>>> &SME :
325 ModuleToTempMDValsMap) {
326 // Load the specified source module.
327 auto &SrcModule = ModuleLoaderCache(SME.getKey());
Teresa Johnsona1080ee2016-01-08 14:17:41 +0000328 // The modules were created with lazy metadata loading. Materialize it
329 // now, before linking it.
330 SrcModule.materializeMetadata();
331 UpgradeDebugInfo(SrcModule);
Teresa Johnsone5a61912015-12-17 17:14:09 +0000332
333 // Link in all necessary metadata from this module.
334 if (TheLinker.linkInMetadata(SrcModule, SME.getValue().get()))
335 return false;
336 }
337
Mehdi Amini7e88d0d2015-12-09 08:17:35 +0000338 DEBUG(dbgs() << "Imported " << ImportedCount << " functions for Module "
Mehdi Aminic8c55172015-12-03 02:37:33 +0000339 << DestModule.getModuleIdentifier() << "\n");
340 return ImportedCount;
Mehdi Amini42418ab2015-11-24 06:07:49 +0000341}
342
343/// Summary file to use for function importing when using -function-import from
344/// the command line.
345static cl::opt<std::string>
346 SummaryFile("summary-file",
347 cl::desc("The summary file to use for function importing."));
348
349static void diagnosticHandler(const DiagnosticInfo &DI) {
350 raw_ostream &OS = errs();
351 DiagnosticPrinterRawOStream DP(OS);
352 DI.print(DP);
353 OS << '\n';
354}
355
356/// Parse the function index out of an IR file and return the function
357/// index object if found, or nullptr if not.
358static std::unique_ptr<FunctionInfoIndex>
359getFunctionIndexForFile(StringRef Path, std::string &Error,
360 DiagnosticHandlerFunction DiagnosticHandler) {
361 std::unique_ptr<MemoryBuffer> Buffer;
362 ErrorOr<std::unique_ptr<MemoryBuffer>> BufferOrErr =
363 MemoryBuffer::getFile(Path);
364 if (std::error_code EC = BufferOrErr.getError()) {
365 Error = EC.message();
366 return nullptr;
367 }
368 Buffer = std::move(BufferOrErr.get());
369 ErrorOr<std::unique_ptr<object::FunctionIndexObjectFile>> ObjOrErr =
370 object::FunctionIndexObjectFile::create(Buffer->getMemBufferRef(),
371 DiagnosticHandler);
372 if (std::error_code EC = ObjOrErr.getError()) {
373 Error = EC.message();
374 return nullptr;
375 }
376 return (*ObjOrErr)->takeIndex();
377}
378
Benjamin Kramerfe2b5412015-12-24 10:03:35 +0000379namespace {
Mehdi Amini42418ab2015-11-24 06:07:49 +0000380/// Pass that performs cross-module function import provided a summary file.
381class FunctionImportPass : public ModulePass {
Teresa Johnson5fcbdb72015-12-07 19:21:11 +0000382 /// Optional function summary index to use for importing, otherwise
383 /// the summary-file option must be specified.
Teresa Johnson7f961e12015-12-09 19:39:47 +0000384 const FunctionInfoIndex *Index;
Mehdi Amini42418ab2015-11-24 06:07:49 +0000385
386public:
387 /// Pass identification, replacement for typeid
388 static char ID;
389
Teresa Johnson5fcbdb72015-12-07 19:21:11 +0000390 /// Specify pass name for debug output
391 const char *getPassName() const override {
392 return "Function Importing";
393 }
394
Teresa Johnson7f961e12015-12-09 19:39:47 +0000395 explicit FunctionImportPass(const FunctionInfoIndex *Index = nullptr)
Teresa Johnson5fcbdb72015-12-07 19:21:11 +0000396 : ModulePass(ID), Index(Index) {}
Mehdi Amini42418ab2015-11-24 06:07:49 +0000397
398 bool runOnModule(Module &M) override {
Teresa Johnson5fcbdb72015-12-07 19:21:11 +0000399 if (SummaryFile.empty() && !Index)
400 report_fatal_error("error: -function-import requires -summary-file or "
401 "file from frontend\n");
402 std::unique_ptr<FunctionInfoIndex> IndexPtr;
403 if (!SummaryFile.empty()) {
404 if (Index)
405 report_fatal_error("error: -summary-file and index from frontend\n");
406 std::string Error;
407 IndexPtr = getFunctionIndexForFile(SummaryFile, Error, diagnosticHandler);
408 if (!IndexPtr) {
409 errs() << "Error loading file '" << SummaryFile << "': " << Error
410 << "\n";
411 return false;
412 }
413 Index = IndexPtr.get();
Mehdi Amini42418ab2015-11-24 06:07:49 +0000414 }
415
Teresa Johnson1b00f2d2016-01-08 17:06:29 +0000416 // First we need to promote to global scope and rename any local values that
417 // are potentially exported to other modules.
418 if (renameModuleForThinLTO(M, Index)) {
419 errs() << "Error renaming module\n";
420 return false;
421 }
422
Mehdi Amini42418ab2015-11-24 06:07:49 +0000423 // Perform the import now.
Mehdi Aminid16c8062015-12-08 22:39:40 +0000424 auto ModuleLoader = [&M](StringRef Identifier) {
425 return loadFile(Identifier, M.getContext());
426 };
Rafael Espindola9d2bfc42015-12-14 23:17:03 +0000427 FunctionImporter Importer(*Index, ModuleLoader);
Mehdi Amini42418ab2015-11-24 06:07:49 +0000428 return Importer.importFunctions(M);
Mehdi Amini42418ab2015-11-24 06:07:49 +0000429 }
430};
Benjamin Kramerfe2b5412015-12-24 10:03:35 +0000431} // anonymous namespace
Mehdi Amini42418ab2015-11-24 06:07:49 +0000432
433char FunctionImportPass::ID = 0;
434INITIALIZE_PASS_BEGIN(FunctionImportPass, "function-import",
435 "Summary Based Function Import", false, false)
436INITIALIZE_PASS_END(FunctionImportPass, "function-import",
437 "Summary Based Function Import", false, false)
438
439namespace llvm {
Teresa Johnson7f961e12015-12-09 19:39:47 +0000440Pass *createFunctionImportPass(const FunctionInfoIndex *Index = nullptr) {
Teresa Johnson5fcbdb72015-12-07 19:21:11 +0000441 return new FunctionImportPass(Index);
442}
Mehdi Amini42418ab2015-11-24 06:07:49 +0000443}