blob: 3ce3b7b841ef719bed5b4f882c87f689fe8bce07 [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 Aminibda3c972016-04-21 01:59:39 +000052// Temporary allows the function import pass to disable always linking
53// referenced discardable symbols.
54static cl::opt<bool>
55 DontForceImportReferencedDiscardableSymbols("disable-force-link-odr",
56 cl::init(false), cl::Hidden);
57
Mehdi Amini42418ab2015-11-24 06:07:49 +000058// Load lazily a module from \p FileName in \p Context.
59static std::unique_ptr<Module> loadFile(const std::string &FileName,
60 LLVMContext &Context) {
61 SMDiagnostic Err;
62 DEBUG(dbgs() << "Loading '" << FileName << "'\n");
Teresa Johnson6cba37c2016-01-22 00:15:53 +000063 // Metadata isn't loaded until functions are imported, to minimize
64 // the memory overhead.
Teresa Johnsona1080ee2016-01-08 14:17:41 +000065 std::unique_ptr<Module> Result =
66 getLazyIRFileModule(FileName, Err, Context,
67 /* ShouldLazyLoadMetadata = */ true);
Mehdi Amini42418ab2015-11-24 06:07:49 +000068 if (!Result) {
69 Err.print("function-import", errs());
Mehdi Aminid7ad2212016-04-01 05:33:11 +000070 report_fatal_error("Abort");
Mehdi Amini42418ab2015-11-24 06:07:49 +000071 }
72
Mehdi Amini42418ab2015-11-24 06:07:49 +000073 return Result;
74}
75
Mehdi Amini7e88d0d2015-12-09 08:17:35 +000076namespace {
Mehdi Amini40641742016-02-10 23:31:45 +000077
Mehdi Aminib4e1e822016-04-27 00:32:13 +000078// Return true if the Summary describes a GlobalValue that can be externally
79// referenced, i.e. it does not need renaming (linkage is not local) or renaming
80// is possible (does not have a section for instance).
81static bool canBeExternallyReferenced(const GlobalValueSummary &Summary) {
82 if (!Summary.needsRenaming())
83 return true;
84
85 if (Summary.hasSection())
86 // Can't rename a global that needs renaming if has a section.
87 return false;
88
89 return true;
90}
91
92// Return true if \p GUID describes a GlobalValue that can be externally
93// referenced, i.e. it does not need renaming (linkage is not local) or
94// renaming is possible (does not have a section for instance).
95static bool canBeExternallyReferenced(const ModuleSummaryIndex &Index,
96 GlobalValue::GUID GUID) {
97 auto Summaries = Index.findGlobalValueSummaryList(GUID);
98 if (Summaries == Index.end())
99 return true;
100 if (Summaries->second.size() != 1)
101 // If there are multiple globals with this GUID, then we know it is
102 // not a local symbol, and it is necessarily externally referenced.
103 return true;
104
105 // We don't need to check for the module path, because if it can't be
106 // externally referenced and we call it, it is necessarilly in the same
107 // module
108 return canBeExternallyReferenced(**Summaries->second.begin());
109}
110
111// Return true if the global described by \p Summary can be imported in another
112// module.
113static bool eligibleForImport(const ModuleSummaryIndex &Index,
114 const GlobalValueSummary &Summary) {
115 if (!canBeExternallyReferenced(Summary))
116 // Can't import a global that needs renaming if has a section for instance.
117 // FIXME: we may be able to import it by copying it without promotion.
118 return false;
119
120 // Check references (and potential calls) in the same module. If the current
121 // value references a global that can't be externally referenced it is not
122 // eligible for import.
123 bool AllRefsCanBeExternallyReferenced =
124 llvm::all_of(Summary.refs(), [&](const ValueInfo &VI) {
125 return canBeExternallyReferenced(Index, VI.getGUID());
126 });
127 if (!AllRefsCanBeExternallyReferenced)
128 return false;
129
130 if (auto *FuncSummary = dyn_cast<FunctionSummary>(&Summary)) {
131 bool AllCallsCanBeExternallyReferenced = llvm::all_of(
132 FuncSummary->calls(), [&](const FunctionSummary::EdgeTy &Edge) {
133 return canBeExternallyReferenced(Index, Edge.first.getGUID());
134 });
135 if (!AllCallsCanBeExternallyReferenced)
136 return false;
137 }
138 return true;
139}
140
Mehdi Amini01e32132016-03-26 05:40:34 +0000141/// Given a list of possible callee implementation for a call site, select one
142/// that fits the \p Threshold.
143///
144/// FIXME: select "best" instead of first that fits. But what is "best"?
145/// - The smallest: more likely to be inlined.
146/// - The one with the least outgoing edges (already well optimized).
147/// - One from a module already being imported from in order to reduce the
148/// number of source modules parsed/linked.
149/// - One that has PGO data attached.
150/// - [insert you fancy metric here]
Mehdi Amini2d28f7a2016-04-16 06:56:44 +0000151static const GlobalValueSummary *
Mehdi Aminib4e1e822016-04-27 00:32:13 +0000152selectCallee(const ModuleSummaryIndex &Index,
153 const GlobalValueSummaryList &CalleeSummaryList,
Teresa Johnson28e457b2016-04-24 14:57:11 +0000154 unsigned Threshold) {
Mehdi Amini01e32132016-03-26 05:40:34 +0000155 auto It = llvm::find_if(
Teresa Johnson28e457b2016-04-24 14:57:11 +0000156 CalleeSummaryList,
157 [&](const std::unique_ptr<GlobalValueSummary> &SummaryPtr) {
158 auto *GVSummary = SummaryPtr.get();
Mehdi Amini5b85d8d2016-05-03 00:27:28 +0000159 if (GlobalValue::isMayBeOverriddenLinkage(GVSummary->linkage()))
160 // There is no point in importing these, we can't inline them
Mehdi Amini01e32132016-03-26 05:40:34 +0000161 return false;
Mehdi Amini2c719cc2016-04-20 04:17:36 +0000162 if (auto *AS = dyn_cast<AliasSummary>(GVSummary)) {
163 GVSummary = &AS->getAliasee();
164 // Alias can't point to "available_externally". However when we import
165 // linkOnceODR the linkage does not change. So we import the alias
166 // and aliasee only in this case.
167 // FIXME: we should import alias as available_externally *function*,
168 // the destination module does need to know it is an alias.
169 if (!GlobalValue::isLinkOnceODRLinkage(GVSummary->linkage()))
170 return false;
171 }
172
173 auto *Summary = cast<FunctionSummary>(GVSummary);
Mehdi Amini7e88d0d2015-12-09 08:17:35 +0000174
Mehdi Amini01e32132016-03-26 05:40:34 +0000175 if (Summary->instCount() > Threshold)
176 return false;
Mehdi Amini7e88d0d2015-12-09 08:17:35 +0000177
Mehdi Aminib4e1e822016-04-27 00:32:13 +0000178 if (!eligibleForImport(Index, *Summary))
179 return false;
180
Mehdi Amini01e32132016-03-26 05:40:34 +0000181 return true;
182 });
Teresa Johnson28e457b2016-04-24 14:57:11 +0000183 if (It == CalleeSummaryList.end())
Mehdi Amini01e32132016-03-26 05:40:34 +0000184 return nullptr;
Mehdi Amini7e88d0d2015-12-09 08:17:35 +0000185
Teresa Johnson28e457b2016-04-24 14:57:11 +0000186 return cast<GlobalValueSummary>(It->get());
Mehdi Amini7e88d0d2015-12-09 08:17:35 +0000187}
Mehdi Amini7e88d0d2015-12-09 08:17:35 +0000188
Mehdi Amini01e32132016-03-26 05:40:34 +0000189/// Return the summary for the function \p GUID that fits the \p Threshold, or
190/// null if there's no match.
Mehdi Amini2d28f7a2016-04-16 06:56:44 +0000191static const GlobalValueSummary *selectCallee(GlobalValue::GUID GUID,
192 unsigned Threshold,
193 const ModuleSummaryIndex &Index) {
Teresa Johnson28e457b2016-04-24 14:57:11 +0000194 auto CalleeSummaryList = Index.findGlobalValueSummaryList(GUID);
Mehdi Aminib4e1e822016-04-27 00:32:13 +0000195 if (CalleeSummaryList == Index.end())
Mehdi Amini01e32132016-03-26 05:40:34 +0000196 return nullptr; // This function does not have a summary
Mehdi Aminib4e1e822016-04-27 00:32:13 +0000197 return selectCallee(Index, CalleeSummaryList->second, Threshold);
Mehdi Amini01e32132016-03-26 05:40:34 +0000198}
Mehdi Amini7e88d0d2015-12-09 08:17:35 +0000199
Mehdi Aminicb874942016-04-23 23:29:24 +0000200/// Mark the global \p GUID as export by module \p ExportModulePath if found in
201/// this module. If it is a GlobalVariable, we also mark any referenced global
202/// in the current module as exported.
203static void exportGlobalInModule(const ModuleSummaryIndex &Index,
204 StringRef ExportModulePath,
205 GlobalValue::GUID GUID,
206 FunctionImporter::ExportSetTy &ExportList) {
Teresa Johnson28e457b2016-04-24 14:57:11 +0000207 auto FindGlobalSummaryInModule =
208 [&](GlobalValue::GUID GUID) -> GlobalValueSummary *{
209 auto SummaryList = Index.findGlobalValueSummaryList(GUID);
210 if (SummaryList == Index.end())
Mehdi Aminicb874942016-04-23 23:29:24 +0000211 // This global does not have a summary, it is not part of the ThinLTO
212 // process
213 return nullptr;
Teresa Johnson28e457b2016-04-24 14:57:11 +0000214 auto SummaryIter = llvm::find_if(
215 SummaryList->second,
216 [&](const std::unique_ptr<GlobalValueSummary> &Summary) {
Mehdi Aminicb874942016-04-23 23:29:24 +0000217 return Summary->modulePath() == ExportModulePath;
218 });
Teresa Johnson28e457b2016-04-24 14:57:11 +0000219 if (SummaryIter == SummaryList->second.end())
Mehdi Aminicb874942016-04-23 23:29:24 +0000220 return nullptr;
Teresa Johnson28e457b2016-04-24 14:57:11 +0000221 return SummaryIter->get();
Mehdi Aminicb874942016-04-23 23:29:24 +0000222 };
223
Teresa Johnson28e457b2016-04-24 14:57:11 +0000224 auto *Summary = FindGlobalSummaryInModule(GUID);
225 if (!Summary)
Mehdi Aminicb874942016-04-23 23:29:24 +0000226 return;
227 // We found it in the current module, mark as exported
228 ExportList.insert(GUID);
229
Mehdi Aminicb874942016-04-23 23:29:24 +0000230 auto GVS = dyn_cast<GlobalVarSummary>(Summary);
231 if (!GVS)
232 return;
233 // FunctionImportGlobalProcessing::doPromoteLocalToGlobal() will always
234 // trigger importing the initializer for `constant unnamed addr` globals that
235 // are referenced. We conservatively export all the referenced symbols for
236 // every global to workaround this, so that the ExportList is accurate.
237 // FIXME: with a "isConstant" flag in the summary we could be more targetted.
238 for (auto &Ref : GVS->refs()) {
239 auto GUID = Ref.getGUID();
Teresa Johnson28e457b2016-04-24 14:57:11 +0000240 auto *RefSummary = FindGlobalSummaryInModule(GUID);
241 if (RefSummary)
Mehdi Aminicb874942016-04-23 23:29:24 +0000242 // Found a ref in the current module, mark it as exported
243 ExportList.insert(GUID);
244 }
Mehdi Amini01e32132016-03-26 05:40:34 +0000245}
Mehdi Amini40641742016-02-10 23:31:45 +0000246
Mehdi Amini01e32132016-03-26 05:40:34 +0000247using EdgeInfo = std::pair<const FunctionSummary *, unsigned /* Threshold */>;
Mehdi Amini7e88d0d2015-12-09 08:17:35 +0000248
Mehdi Amini01e32132016-03-26 05:40:34 +0000249/// Compute the list of functions to import for a given caller. Mark these
250/// imported functions and the symbols they reference in their source module as
251/// exported from their source module.
252static void computeImportForFunction(
Teresa Johnson3255eec2016-04-10 15:17:26 +0000253 const FunctionSummary &Summary, const ModuleSummaryIndex &Index,
Teresa Johnsonc851d212016-04-25 21:09:51 +0000254 unsigned Threshold, const GVSummaryMapTy &DefinedGVSummaries,
Mehdi Amini01e32132016-03-26 05:40:34 +0000255 SmallVectorImpl<EdgeInfo> &Worklist,
256 FunctionImporter::ImportMapTy &ImportsForModule,
Teresa Johnsonc86af332016-04-12 21:13:11 +0000257 StringMap<FunctionImporter::ExportSetTy> *ExportLists = nullptr) {
Mehdi Amini01e32132016-03-26 05:40:34 +0000258 for (auto &Edge : Summary.calls()) {
Teresa Johnson2d5487c2016-04-11 13:58:45 +0000259 auto GUID = Edge.first.getGUID();
Mehdi Amini01e32132016-03-26 05:40:34 +0000260 DEBUG(dbgs() << " edge -> " << GUID << " Threshold:" << Threshold << "\n");
261
Mehdi Amini1aafabf2016-04-16 07:02:16 +0000262 if (DefinedGVSummaries.count(GUID)) {
Mehdi Amini01e32132016-03-26 05:40:34 +0000263 DEBUG(dbgs() << "ignored! Target already in destination module.\n");
264 continue;
Teresa Johnsond450da32015-11-24 21:15:19 +0000265 }
Mehdi Amini01e32132016-03-26 05:40:34 +0000266
267 auto *CalleeSummary = selectCallee(GUID, Threshold, Index);
268 if (!CalleeSummary) {
269 DEBUG(dbgs() << "ignored! No qualifying callee with summary found.\n");
270 continue;
271 }
Mehdi Amini2d28f7a2016-04-16 06:56:44 +0000272 // "Resolve" the summary, traversing alias,
273 const FunctionSummary *ResolvedCalleeSummary;
Mehdi Amini6968ef72016-04-20 01:04:20 +0000274 if (isa<AliasSummary>(CalleeSummary)) {
Mehdi Amini2d28f7a2016-04-16 06:56:44 +0000275 ResolvedCalleeSummary = cast<FunctionSummary>(
276 &cast<AliasSummary>(CalleeSummary)->getAliasee());
Mehdi Amini2c719cc2016-04-20 04:17:36 +0000277 assert(
278 GlobalValue::isLinkOnceODRLinkage(ResolvedCalleeSummary->linkage()) &&
279 "Unexpected alias to a non-linkonceODR in import list");
Mehdi Amini6968ef72016-04-20 01:04:20 +0000280 } else
Mehdi Amini2d28f7a2016-04-16 06:56:44 +0000281 ResolvedCalleeSummary = cast<FunctionSummary>(CalleeSummary);
282
283 assert(ResolvedCalleeSummary->instCount() <= Threshold &&
Mehdi Amini01e32132016-03-26 05:40:34 +0000284 "selectCallee() didn't honor the threshold");
285
Mehdi Amini2d28f7a2016-04-16 06:56:44 +0000286 auto ExportModulePath = ResolvedCalleeSummary->modulePath();
287 auto &ProcessedThreshold = ImportsForModule[ExportModulePath][GUID];
Mehdi Amini01e32132016-03-26 05:40:34 +0000288 /// Since the traversal of the call graph is DFS, we can revisit a function
289 /// a second time with a higher threshold. In this case, it is added back to
290 /// the worklist with the new threshold.
291 if (ProcessedThreshold && ProcessedThreshold > Threshold) {
292 DEBUG(dbgs() << "ignored! Target was already seen with Threshold "
293 << ProcessedThreshold << "\n");
294 continue;
295 }
296 // Mark this function as imported in this module, with the current Threshold
297 ProcessedThreshold = Threshold;
298
299 // Make exports in the source module.
Teresa Johnsonc86af332016-04-12 21:13:11 +0000300 if (ExportLists) {
Mehdi Aminief7555f2016-04-13 01:52:32 +0000301 auto &ExportList = (*ExportLists)[ExportModulePath];
Teresa Johnsonc86af332016-04-12 21:13:11 +0000302 ExportList.insert(GUID);
303 // Mark all functions and globals referenced by this function as exported
304 // to the outside if they are defined in the same source module.
Mehdi Amini2d28f7a2016-04-16 06:56:44 +0000305 for (auto &Edge : ResolvedCalleeSummary->calls()) {
Teresa Johnsonc86af332016-04-12 21:13:11 +0000306 auto CalleeGUID = Edge.first.getGUID();
Mehdi Aminicb874942016-04-23 23:29:24 +0000307 exportGlobalInModule(Index, ExportModulePath, CalleeGUID, ExportList);
Teresa Johnsonc86af332016-04-12 21:13:11 +0000308 }
Mehdi Amini2d28f7a2016-04-16 06:56:44 +0000309 for (auto &Ref : ResolvedCalleeSummary->refs()) {
Teresa Johnsonc86af332016-04-12 21:13:11 +0000310 auto GUID = Ref.getGUID();
Mehdi Aminicb874942016-04-23 23:29:24 +0000311 exportGlobalInModule(Index, ExportModulePath, GUID, ExportList);
Teresa Johnsonc86af332016-04-12 21:13:11 +0000312 }
Mehdi Amini01e32132016-03-26 05:40:34 +0000313 }
314
315 // Insert the newly imported function to the worklist.
Mehdi Amini2d28f7a2016-04-16 06:56:44 +0000316 Worklist.push_back(std::make_pair(ResolvedCalleeSummary, Threshold));
Teresa Johnsond450da32015-11-24 21:15:19 +0000317 }
318}
319
Mehdi Amini01e32132016-03-26 05:40:34 +0000320/// Given the list of globals defined in a module, compute the list of imports
321/// as well as the list of "exports", i.e. the list of symbols referenced from
322/// another module (that may require promotion).
323static void ComputeImportForModule(
Teresa Johnsonc851d212016-04-25 21:09:51 +0000324 const GVSummaryMapTy &DefinedGVSummaries, const ModuleSummaryIndex &Index,
Mehdi Amini01e32132016-03-26 05:40:34 +0000325 FunctionImporter::ImportMapTy &ImportsForModule,
Teresa Johnsonc86af332016-04-12 21:13:11 +0000326 StringMap<FunctionImporter::ExportSetTy> *ExportLists = nullptr) {
Mehdi Amini01e32132016-03-26 05:40:34 +0000327 // Worklist contains the list of function imported in this module, for which
328 // we will analyse the callees and may import further down the callgraph.
329 SmallVector<EdgeInfo, 128> Worklist;
330
331 // Populate the worklist with the import for the functions in the current
332 // module
Teresa Johnson28e457b2016-04-24 14:57:11 +0000333 for (auto &GVSummary : DefinedGVSummaries) {
334 auto *Summary = GVSummary.second;
Mehdi Amini2d28f7a2016-04-16 06:56:44 +0000335 if (auto *AS = dyn_cast<AliasSummary>(Summary))
336 Summary = &AS->getAliasee();
Mehdi Amini1aafabf2016-04-16 07:02:16 +0000337 auto *FuncSummary = dyn_cast<FunctionSummary>(Summary);
338 if (!FuncSummary)
339 // Skip import for global variables
340 continue;
Teresa Johnson28e457b2016-04-24 14:57:11 +0000341 DEBUG(dbgs() << "Initalize import for " << GVSummary.first << "\n");
Mehdi Amini2d28f7a2016-04-16 06:56:44 +0000342 computeImportForFunction(*FuncSummary, Index, ImportInstrLimit,
Mehdi Amini1aafabf2016-04-16 07:02:16 +0000343 DefinedGVSummaries, Worklist, ImportsForModule,
Mehdi Amini01e32132016-03-26 05:40:34 +0000344 ExportLists);
345 }
346
Mehdi Amini42418ab2015-11-24 06:07:49 +0000347 while (!Worklist.empty()) {
Mehdi Amini01e32132016-03-26 05:40:34 +0000348 auto FuncInfo = Worklist.pop_back_val();
349 auto *Summary = FuncInfo.first;
350 auto Threshold = FuncInfo.second;
Mehdi Amini42418ab2015-11-24 06:07:49 +0000351
Mehdi Amini7e88d0d2015-12-09 08:17:35 +0000352 // Process the newly imported functions and add callees to the worklist.
Mehdi Amini40641742016-02-10 23:31:45 +0000353 // Adjust the threshold
354 Threshold = Threshold * ImportInstrFactor;
Mehdi Amini01e32132016-03-26 05:40:34 +0000355
Mehdi Amini1aafabf2016-04-16 07:02:16 +0000356 computeImportForFunction(*Summary, Index, Threshold, DefinedGVSummaries,
Teresa Johnson3255eec2016-04-10 15:17:26 +0000357 Worklist, ImportsForModule, ExportLists);
Mehdi Amini42418ab2015-11-24 06:07:49 +0000358 }
Mehdi Aminic8c55172015-12-03 02:37:33 +0000359}
Mehdi Aminiffe2e4a2015-12-02 04:34:28 +0000360
Mehdi Amini01e32132016-03-26 05:40:34 +0000361} // anonymous namespace
362
Teresa Johnsonc86af332016-04-12 21:13:11 +0000363/// Compute all the import and export for every module using the Index.
Mehdi Amini01e32132016-03-26 05:40:34 +0000364void llvm::ComputeCrossModuleImport(
365 const ModuleSummaryIndex &Index,
Teresa Johnsonc851d212016-04-25 21:09:51 +0000366 const StringMap<GVSummaryMapTy> &ModuleToDefinedGVSummaries,
Mehdi Amini01e32132016-03-26 05:40:34 +0000367 StringMap<FunctionImporter::ImportMapTy> &ImportLists,
368 StringMap<FunctionImporter::ExportSetTy> &ExportLists) {
Mehdi Amini01e32132016-03-26 05:40:34 +0000369 // For each module that has function defined, compute the import/export lists.
Mehdi Amini1aafabf2016-04-16 07:02:16 +0000370 for (auto &DefinedGVSummaries : ModuleToDefinedGVSummaries) {
371 auto &ImportsForModule = ImportLists[DefinedGVSummaries.first()];
372 DEBUG(dbgs() << "Computing import for Module '"
373 << DefinedGVSummaries.first() << "'\n");
374 ComputeImportForModule(DefinedGVSummaries.second, Index, ImportsForModule,
Teresa Johnsonc86af332016-04-12 21:13:11 +0000375 &ExportLists);
Mehdi Amini01e32132016-03-26 05:40:34 +0000376 }
377
378#ifndef NDEBUG
379 DEBUG(dbgs() << "Import/Export lists for " << ImportLists.size()
380 << " modules:\n");
381 for (auto &ModuleImports : ImportLists) {
382 auto ModName = ModuleImports.first();
383 auto &Exports = ExportLists[ModName];
384 DEBUG(dbgs() << "* Module " << ModName << " exports " << Exports.size()
385 << " functions. Imports from " << ModuleImports.second.size()
386 << " modules.\n");
387 for (auto &Src : ModuleImports.second) {
388 auto SrcModName = Src.first();
389 DEBUG(dbgs() << " - " << Src.second.size() << " functions imported from "
390 << SrcModName << "\n");
391 }
392 }
393#endif
394}
395
Teresa Johnsonc86af332016-04-12 21:13:11 +0000396/// Compute all the imports for the given module in the Index.
397void llvm::ComputeCrossModuleImportForModule(
398 StringRef ModulePath, const ModuleSummaryIndex &Index,
399 FunctionImporter::ImportMapTy &ImportList) {
400
401 // Collect the list of functions this module defines.
402 // GUID -> Summary
Teresa Johnsonc851d212016-04-25 21:09:51 +0000403 GVSummaryMapTy FunctionSummaryMap;
Teresa Johnson28e457b2016-04-24 14:57:11 +0000404 Index.collectDefinedFunctionsForModule(ModulePath, FunctionSummaryMap);
Teresa Johnsonc86af332016-04-12 21:13:11 +0000405
406 // Compute the import list for this module.
407 DEBUG(dbgs() << "Computing import for Module '" << ModulePath << "'\n");
Teresa Johnson28e457b2016-04-24 14:57:11 +0000408 ComputeImportForModule(FunctionSummaryMap, Index, ImportList);
Teresa Johnsonc86af332016-04-12 21:13:11 +0000409
410#ifndef NDEBUG
411 DEBUG(dbgs() << "* Module " << ModulePath << " imports from "
412 << ImportList.size() << " modules.\n");
413 for (auto &Src : ImportList) {
414 auto SrcModName = Src.first();
415 DEBUG(dbgs() << " - " << Src.second.size() << " functions imported from "
416 << SrcModName << "\n");
417 }
418#endif
419}
420
Teresa Johnson84174c32016-05-10 13:48:23 +0000421/// Compute the set of summaries needed for a ThinLTO backend compilation of
422/// \p ModulePath.
423void llvm::gatherImportedSummariesForModule(
424 StringRef ModulePath,
425 const StringMap<GVSummaryMapTy> &ModuleToDefinedGVSummaries,
426 const StringMap<FunctionImporter::ImportMapTy> &ImportLists,
427 std::map<std::string, GVSummaryMapTy> &ModuleToSummariesForIndex) {
428 // Include all summaries from the importing module.
429 ModuleToSummariesForIndex[ModulePath] =
430 ModuleToDefinedGVSummaries.lookup(ModulePath);
431 auto ModuleImports = ImportLists.find(ModulePath);
432 if (ModuleImports != ImportLists.end()) {
433 // Include summaries for imports.
434 for (auto &ILI : ModuleImports->second) {
435 auto &SummariesForIndex = ModuleToSummariesForIndex[ILI.first()];
436 const auto &DefinedGVSummaries =
437 ModuleToDefinedGVSummaries.lookup(ILI.first());
438 for (auto &GI : ILI.second) {
439 const auto &DS = DefinedGVSummaries.find(GI.first);
440 assert(DS != DefinedGVSummaries.end() &&
441 "Expected a defined summary for imported global value");
442 SummariesForIndex[GI.first] = DS->second;
443 }
444 }
445 }
446}
447
Teresa Johnson8570fe42016-05-10 15:54:09 +0000448/// Emit the files \p ModulePath will import from into \p OutputFilename.
449std::error_code llvm::EmitImportsFiles(
450 StringRef ModulePath, StringRef OutputFilename,
451 const StringMap<FunctionImporter::ImportMapTy> &ImportLists) {
452 auto ModuleImports = ImportLists.find(ModulePath);
453 std::error_code EC;
454 raw_fd_ostream ImportsOS(OutputFilename, EC, sys::fs::OpenFlags::F_None);
455 if (EC)
456 return EC;
457 if (ModuleImports != ImportLists.end())
458 for (auto &ILI : ModuleImports->second)
459 ImportsOS << ILI.first() << "\n";
460 return std::error_code();
461}
462
Mehdi Aminic8c55172015-12-03 02:37:33 +0000463// Automatically import functions in Module \p DestModule based on the summaries
464// index.
465//
Mehdi Amini01e32132016-03-26 05:40:34 +0000466bool FunctionImporter::importFunctions(
Mehdi Aminibda3c972016-04-21 01:59:39 +0000467 Module &DestModule, const FunctionImporter::ImportMapTy &ImportList,
468 bool ForceImportReferencedDiscardableSymbols) {
Mehdi Amini5411d052015-12-08 23:04:19 +0000469 DEBUG(dbgs() << "Starting import for Module "
Mehdi Amini311fef62015-12-03 02:58:14 +0000470 << DestModule.getModuleIdentifier() << "\n");
Mehdi Aminic8c55172015-12-03 02:37:33 +0000471 unsigned ImportedCount = 0;
472
Mehdi Aminic8c55172015-12-03 02:37:33 +0000473 // Linker that will be used for importing function
Rafael Espindola9d2bfc42015-12-14 23:17:03 +0000474 Linker TheLinker(DestModule);
Mehdi Amini7e88d0d2015-12-09 08:17:35 +0000475 // Do the actual import of functions now, one Module at a time
Mehdi Amini01e32132016-03-26 05:40:34 +0000476 std::set<StringRef> ModuleNameOrderedList;
477 for (auto &FunctionsToImportPerModule : ImportList) {
478 ModuleNameOrderedList.insert(FunctionsToImportPerModule.first());
479 }
480 for (auto &Name : ModuleNameOrderedList) {
Mehdi Amini7e88d0d2015-12-09 08:17:35 +0000481 // Get the module for the import
Mehdi Amini01e32132016-03-26 05:40:34 +0000482 const auto &FunctionsToImportPerModule = ImportList.find(Name);
483 assert(FunctionsToImportPerModule != ImportList.end());
484 std::unique_ptr<Module> SrcModule = ModuleLoader(Name);
Mehdi Amini7e88d0d2015-12-09 08:17:35 +0000485 assert(&DestModule.getContext() == &SrcModule->getContext() &&
486 "Context mismatch");
487
Teresa Johnson6cba37c2016-01-22 00:15:53 +0000488 // If modules were created with lazy metadata loading, materialize it
489 // now, before linking it (otherwise this will be a noop).
490 SrcModule->materializeMetadata();
491 UpgradeDebugInfo(*SrcModule);
Teresa Johnsone5a61912015-12-17 17:14:09 +0000492
Mehdi Amini01e32132016-03-26 05:40:34 +0000493 auto &ImportGUIDs = FunctionsToImportPerModule->second;
494 // Find the globals to import
495 DenseSet<const GlobalValue *> GlobalsToImport;
496 for (auto &GV : *SrcModule) {
Teresa Johnson0beb8582016-04-04 18:52:23 +0000497 if (!GV.hasName())
498 continue;
499 auto GUID = GV.getGUID();
500 auto Import = ImportGUIDs.count(GUID);
Mehdi Aminiaeb1e592016-04-19 09:21:30 +0000501 DEBUG(dbgs() << (Import ? "Is" : "Not") << " importing function " << GUID
502 << " " << GV.getName() << " from "
503 << SrcModule->getSourceFileName() << "\n");
Teresa Johnson0beb8582016-04-04 18:52:23 +0000504 if (Import) {
Mehdi Amini01e32132016-03-26 05:40:34 +0000505 GV.materialize();
506 GlobalsToImport.insert(&GV);
507 }
508 }
Mehdi Amini2d28f7a2016-04-16 06:56:44 +0000509 for (auto &GV : SrcModule->globals()) {
510 if (!GV.hasName())
511 continue;
512 auto GUID = GV.getGUID();
513 auto Import = ImportGUIDs.count(GUID);
Mehdi Aminiaeb1e592016-04-19 09:21:30 +0000514 DEBUG(dbgs() << (Import ? "Is" : "Not") << " importing global " << GUID
515 << " " << GV.getName() << " from "
516 << SrcModule->getSourceFileName() << "\n");
Mehdi Amini2d28f7a2016-04-16 06:56:44 +0000517 if (Import) {
518 GV.materialize();
519 GlobalsToImport.insert(&GV);
520 }
521 }
Mehdi Amini01e32132016-03-26 05:40:34 +0000522 for (auto &GV : SrcModule->aliases()) {
523 if (!GV.hasName())
524 continue;
525 auto GUID = GV.getGUID();
Teresa Johnson0beb8582016-04-04 18:52:23 +0000526 auto Import = ImportGUIDs.count(GUID);
Mehdi Aminiaeb1e592016-04-19 09:21:30 +0000527 DEBUG(dbgs() << (Import ? "Is" : "Not") << " importing alias " << GUID
528 << " " << GV.getName() << " from "
529 << SrcModule->getSourceFileName() << "\n");
Teresa Johnson0beb8582016-04-04 18:52:23 +0000530 if (Import) {
Mehdi Amini01e32132016-03-26 05:40:34 +0000531 // Alias can't point to "available_externally". However when we import
Teresa Johnson9aae3952016-03-27 15:01:11 +0000532 // linkOnceODR the linkage does not change. So we import the alias
Mehdi Amini6968ef72016-04-20 01:04:20 +0000533 // and aliasee only in this case. This has been handled by
534 // computeImportForFunction()
Mehdi Amini2d28f7a2016-04-16 06:56:44 +0000535 GlobalObject *GO = GV.getBaseObject();
Mehdi Amini6968ef72016-04-20 01:04:20 +0000536 assert(GO->hasLinkOnceODRLinkage() &&
537 "Unexpected alias to a non-linkonceODR in import list");
Mehdi Amini2d28f7a2016-04-16 06:56:44 +0000538#ifndef NDEBUG
539 if (!GlobalsToImport.count(GO))
540 DEBUG(dbgs() << " alias triggers importing aliasee " << GO->getGUID()
541 << " " << GO->getName() << " from "
542 << SrcModule->getSourceFileName() << "\n");
543#endif
544 GO->materialize();
Mehdi Amini01e32132016-03-26 05:40:34 +0000545 GlobalsToImport.insert(GO);
Mehdi Amini01e32132016-03-26 05:40:34 +0000546 GV.materialize();
547 GlobalsToImport.insert(&GV);
548 }
549 }
550
Mehdi Amini7e88d0d2015-12-09 08:17:35 +0000551 // Link in the specified functions.
Mehdi Amini01e32132016-03-26 05:40:34 +0000552 if (renameModuleForThinLTO(*SrcModule, Index, &GlobalsToImport))
Mehdi Amini8d051852016-03-19 00:40:31 +0000553 return true;
554
Teresa Johnsond29478f2016-03-27 15:27:30 +0000555 if (PrintImports) {
556 for (const auto *GV : GlobalsToImport)
557 dbgs() << DestModule.getSourceFileName() << ": Import " << GV->getName()
558 << " from " << SrcModule->getSourceFileName() << "\n";
559 }
560
Mehdi Aminibda3c972016-04-21 01:59:39 +0000561 // Instruct the linker that the client will take care of linkonce resolution
562 unsigned Flags = Linker::Flags::None;
563 if (!ForceImportReferencedDiscardableSymbols)
564 Flags |= Linker::Flags::DontForceLinkLinkonceODR;
565
566 if (TheLinker.linkInModule(std::move(SrcModule), Flags, &GlobalsToImport))
Mehdi Amini7e88d0d2015-12-09 08:17:35 +0000567 report_fatal_error("Function Import: link error");
568
Mehdi Amini01e32132016-03-26 05:40:34 +0000569 ImportedCount += GlobalsToImport.size();
Mehdi Amini7e88d0d2015-12-09 08:17:35 +0000570 }
Teresa Johnsone5a61912015-12-17 17:14:09 +0000571
Teresa Johnsond29478f2016-03-27 15:27:30 +0000572 NumImported += ImportedCount;
573
Mehdi Amini7e88d0d2015-12-09 08:17:35 +0000574 DEBUG(dbgs() << "Imported " << ImportedCount << " functions for Module "
Mehdi Aminic8c55172015-12-03 02:37:33 +0000575 << DestModule.getModuleIdentifier() << "\n");
576 return ImportedCount;
Mehdi Amini42418ab2015-11-24 06:07:49 +0000577}
578
579/// Summary file to use for function importing when using -function-import from
580/// the command line.
581static cl::opt<std::string>
582 SummaryFile("summary-file",
583 cl::desc("The summary file to use for function importing."));
584
585static void diagnosticHandler(const DiagnosticInfo &DI) {
586 raw_ostream &OS = errs();
587 DiagnosticPrinterRawOStream DP(OS);
588 DI.print(DP);
589 OS << '\n';
590}
591
Teresa Johnson26ab5772016-03-15 00:04:37 +0000592/// Parse the summary index out of an IR file and return the summary
Mehdi Amini42418ab2015-11-24 06:07:49 +0000593/// index object if found, or nullptr if not.
Teresa Johnson26ab5772016-03-15 00:04:37 +0000594static std::unique_ptr<ModuleSummaryIndex>
595getModuleSummaryIndexForFile(StringRef Path, std::string &Error,
596 DiagnosticHandlerFunction DiagnosticHandler) {
Mehdi Amini42418ab2015-11-24 06:07:49 +0000597 std::unique_ptr<MemoryBuffer> Buffer;
598 ErrorOr<std::unique_ptr<MemoryBuffer>> BufferOrErr =
599 MemoryBuffer::getFile(Path);
600 if (std::error_code EC = BufferOrErr.getError()) {
601 Error = EC.message();
602 return nullptr;
603 }
604 Buffer = std::move(BufferOrErr.get());
Teresa Johnson26ab5772016-03-15 00:04:37 +0000605 ErrorOr<std::unique_ptr<object::ModuleSummaryIndexObjectFile>> ObjOrErr =
606 object::ModuleSummaryIndexObjectFile::create(Buffer->getMemBufferRef(),
607 DiagnosticHandler);
Mehdi Amini42418ab2015-11-24 06:07:49 +0000608 if (std::error_code EC = ObjOrErr.getError()) {
609 Error = EC.message();
610 return nullptr;
611 }
612 return (*ObjOrErr)->takeIndex();
613}
614
Benjamin Kramerfe2b5412015-12-24 10:03:35 +0000615namespace {
Mehdi Amini42418ab2015-11-24 06:07:49 +0000616/// Pass that performs cross-module function import provided a summary file.
617class FunctionImportPass : public ModulePass {
Teresa Johnson26ab5772016-03-15 00:04:37 +0000618 /// Optional module summary index to use for importing, otherwise
Teresa Johnson5fcbdb72015-12-07 19:21:11 +0000619 /// the summary-file option must be specified.
Teresa Johnson26ab5772016-03-15 00:04:37 +0000620 const ModuleSummaryIndex *Index;
Mehdi Amini42418ab2015-11-24 06:07:49 +0000621
622public:
623 /// Pass identification, replacement for typeid
624 static char ID;
625
Teresa Johnson5fcbdb72015-12-07 19:21:11 +0000626 /// Specify pass name for debug output
Mehdi Amini2d28f7a2016-04-16 06:56:44 +0000627 const char *getPassName() const override { return "Function Importing"; }
Teresa Johnson5fcbdb72015-12-07 19:21:11 +0000628
Teresa Johnson26ab5772016-03-15 00:04:37 +0000629 explicit FunctionImportPass(const ModuleSummaryIndex *Index = nullptr)
Teresa Johnson5fcbdb72015-12-07 19:21:11 +0000630 : ModulePass(ID), Index(Index) {}
Mehdi Amini42418ab2015-11-24 06:07:49 +0000631
632 bool runOnModule(Module &M) override {
Andrew Kayloraa641a52016-04-22 22:06:11 +0000633 if (skipModule(M))
634 return false;
635
Teresa Johnson5fcbdb72015-12-07 19:21:11 +0000636 if (SummaryFile.empty() && !Index)
637 report_fatal_error("error: -function-import requires -summary-file or "
638 "file from frontend\n");
Teresa Johnson26ab5772016-03-15 00:04:37 +0000639 std::unique_ptr<ModuleSummaryIndex> IndexPtr;
Teresa Johnson5fcbdb72015-12-07 19:21:11 +0000640 if (!SummaryFile.empty()) {
641 if (Index)
642 report_fatal_error("error: -summary-file and index from frontend\n");
643 std::string Error;
Teresa Johnson26ab5772016-03-15 00:04:37 +0000644 IndexPtr =
645 getModuleSummaryIndexForFile(SummaryFile, Error, diagnosticHandler);
Teresa Johnson5fcbdb72015-12-07 19:21:11 +0000646 if (!IndexPtr) {
647 errs() << "Error loading file '" << SummaryFile << "': " << Error
648 << "\n";
649 return false;
650 }
651 Index = IndexPtr.get();
Mehdi Amini42418ab2015-11-24 06:07:49 +0000652 }
653
Teresa Johnsonc86af332016-04-12 21:13:11 +0000654 // First step is collecting the import list.
655 FunctionImporter::ImportMapTy ImportList;
656 ComputeCrossModuleImportForModule(M.getModuleIdentifier(), *Index,
657 ImportList);
Mehdi Amini01e32132016-03-26 05:40:34 +0000658
659 // Next we need to promote to global scope and rename any local values that
Teresa Johnson1b00f2d2016-01-08 17:06:29 +0000660 // are potentially exported to other modules.
Mehdi Amini01e32132016-03-26 05:40:34 +0000661 if (renameModuleForThinLTO(M, *Index, nullptr)) {
Teresa Johnson1b00f2d2016-01-08 17:06:29 +0000662 errs() << "Error renaming module\n";
663 return false;
664 }
665
Mehdi Amini42418ab2015-11-24 06:07:49 +0000666 // Perform the import now.
Mehdi Aminid16c8062015-12-08 22:39:40 +0000667 auto ModuleLoader = [&M](StringRef Identifier) {
668 return loadFile(Identifier, M.getContext());
669 };
Rafael Espindola9d2bfc42015-12-14 23:17:03 +0000670 FunctionImporter Importer(*Index, ModuleLoader);
Mehdi Aminibda3c972016-04-21 01:59:39 +0000671 return Importer.importFunctions(
672 M, ImportList, !DontForceImportReferencedDiscardableSymbols);
Mehdi Amini42418ab2015-11-24 06:07:49 +0000673 }
674};
Benjamin Kramerfe2b5412015-12-24 10:03:35 +0000675} // anonymous namespace
Mehdi Amini42418ab2015-11-24 06:07:49 +0000676
677char FunctionImportPass::ID = 0;
678INITIALIZE_PASS_BEGIN(FunctionImportPass, "function-import",
679 "Summary Based Function Import", false, false)
680INITIALIZE_PASS_END(FunctionImportPass, "function-import",
681 "Summary Based Function Import", false, false)
682
683namespace llvm {
Teresa Johnson26ab5772016-03-15 00:04:37 +0000684Pass *createFunctionImportPass(const ModuleSummaryIndex *Index = nullptr) {
Teresa Johnson5fcbdb72015-12-07 19:21:11 +0000685 return new FunctionImportPass(Index);
686}
Mehdi Amini42418ab2015-11-24 06:07:49 +0000687}