blob: a3c743f6356b4658fe66688eddd00ac27cc3c2aa [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"
Teresa Johnson04c9a2d2016-05-25 14:03:11 +000019#include "llvm/ADT/Triple.h"
Mehdi Amini42418ab2015-11-24 06:07:49 +000020#include "llvm/IR/AutoUpgrade.h"
21#include "llvm/IR/DiagnosticPrinter.h"
22#include "llvm/IR/IntrinsicInst.h"
23#include "llvm/IR/Module.h"
Mehdi Aminifc06b832016-12-23 18:04:51 +000024#include "llvm/IR/Verifier.h"
Mehdi Amini42418ab2015-11-24 06:07:49 +000025#include "llvm/IRReader/IRReader.h"
26#include "llvm/Linker/Linker.h"
Teresa Johnson04c9a2d2016-05-25 14:03:11 +000027#include "llvm/Object/IRObjectFile.h"
Teresa Johnson26ab5772016-03-15 00:04:37 +000028#include "llvm/Object/ModuleSummaryIndexObjectFile.h"
Mehdi Amini42418ab2015-11-24 06:07:49 +000029#include "llvm/Support/CommandLine.h"
30#include "llvm/Support/Debug.h"
31#include "llvm/Support/SourceMgr.h"
Teresa Johnson04c9a2d2016-05-25 14:03:11 +000032#include "llvm/Transforms/IPO/Internalize.h"
Teresa Johnson488a8002016-02-10 18:11:31 +000033#include "llvm/Transforms/Utils/FunctionImportUtils.h"
Mehdi Amini7e88d0d2015-12-09 08:17:35 +000034
Mehdi Amini01e32132016-03-26 05:40:34 +000035#define DEBUG_TYPE "function-import"
Mehdi Amini7e88d0d2015-12-09 08:17:35 +000036
Mehdi Amini42418ab2015-11-24 06:07:49 +000037using namespace llvm;
38
Teresa Johnsond29478f2016-03-27 15:27:30 +000039STATISTIC(NumImported, "Number of functions imported");
40
Teresa Johnson39303612015-11-24 22:55:46 +000041/// Limit on instruction count of imported functions.
42static cl::opt<unsigned> ImportInstrLimit(
43 "import-instr-limit", cl::init(100), cl::Hidden, cl::value_desc("N"),
44 cl::desc("Only import functions with less than N instructions"));
45
Mehdi Amini40641742016-02-10 23:31:45 +000046static cl::opt<float>
47 ImportInstrFactor("import-instr-evolution-factor", cl::init(0.7),
48 cl::Hidden, cl::value_desc("x"),
49 cl::desc("As we import functions, multiply the "
50 "`import-instr-limit` threshold by this factor "
51 "before processing newly imported functions"));
Piotr Padlewskiba72b952016-09-29 17:32:07 +000052
Piotr Padlewskid2869472016-09-30 03:01:17 +000053static cl::opt<float> ImportHotInstrFactor(
54 "import-hot-evolution-factor", cl::init(1.0), cl::Hidden,
55 cl::value_desc("x"),
56 cl::desc("As we import functions called from hot callsite, multiply the "
57 "`import-instr-limit` threshold by this factor "
58 "before processing newly imported functions"));
59
Piotr Padlewskid9830eb2016-09-26 20:37:32 +000060static cl::opt<float> ImportHotMultiplier(
61 "import-hot-multiplier", cl::init(3.0), cl::Hidden, cl::value_desc("x"),
Piotr Padlewskiba72b952016-09-29 17:32:07 +000062 cl::desc("Multiply the `import-instr-limit` threshold for hot callsites"));
63
64// FIXME: This multiplier was not really tuned up.
65static cl::opt<float> ImportColdMultiplier(
66 "import-cold-multiplier", cl::init(0), cl::Hidden, cl::value_desc("N"),
67 cl::desc("Multiply the `import-instr-limit` threshold for cold callsites"));
Mehdi Amini40641742016-02-10 23:31:45 +000068
Teresa Johnsond29478f2016-03-27 15:27:30 +000069static cl::opt<bool> PrintImports("print-imports", cl::init(false), cl::Hidden,
70 cl::desc("Print imported functions"));
71
Mehdi Aminibda3c972016-04-21 01:59:39 +000072// Temporary allows the function import pass to disable always linking
73// referenced discardable symbols.
74static cl::opt<bool>
75 DontForceImportReferencedDiscardableSymbols("disable-force-link-odr",
76 cl::init(false), cl::Hidden);
77
Piotr Padlewski3b776122016-07-08 23:01:49 +000078static cl::opt<bool> EnableImportMetadata(
79 "enable-import-metadata", cl::init(
80#if !defined(NDEBUG)
81 true /*Enabled with asserts.*/
82#else
83 false
84#endif
85 ),
86 cl::Hidden, cl::desc("Enable import metadata like 'thinlto_src_module'"));
87
Mehdi Amini42418ab2015-11-24 06:07:49 +000088// Load lazily a module from \p FileName in \p Context.
89static std::unique_ptr<Module> loadFile(const std::string &FileName,
90 LLVMContext &Context) {
91 SMDiagnostic Err;
92 DEBUG(dbgs() << "Loading '" << FileName << "'\n");
Teresa Johnson6cba37c2016-01-22 00:15:53 +000093 // Metadata isn't loaded until functions are imported, to minimize
94 // the memory overhead.
Teresa Johnsona1080ee2016-01-08 14:17:41 +000095 std::unique_ptr<Module> Result =
96 getLazyIRFileModule(FileName, Err, Context,
97 /* ShouldLazyLoadMetadata = */ true);
Mehdi Amini42418ab2015-11-24 06:07:49 +000098 if (!Result) {
99 Err.print("function-import", errs());
Mehdi Aminid7ad2212016-04-01 05:33:11 +0000100 report_fatal_error("Abort");
Mehdi Amini42418ab2015-11-24 06:07:49 +0000101 }
102
Mehdi Amini42418ab2015-11-24 06:07:49 +0000103 return Result;
104}
105
Mehdi Amini7e88d0d2015-12-09 08:17:35 +0000106namespace {
Mehdi Amini40641742016-02-10 23:31:45 +0000107
Mehdi Amini01e32132016-03-26 05:40:34 +0000108/// Given a list of possible callee implementation for a call site, select one
109/// that fits the \p Threshold.
110///
111/// FIXME: select "best" instead of first that fits. But what is "best"?
112/// - The smallest: more likely to be inlined.
113/// - The one with the least outgoing edges (already well optimized).
114/// - One from a module already being imported from in order to reduce the
115/// number of source modules parsed/linked.
116/// - One that has PGO data attached.
117/// - [insert you fancy metric here]
Mehdi Amini2d28f7a2016-04-16 06:56:44 +0000118static const GlobalValueSummary *
Mehdi Aminib4e1e822016-04-27 00:32:13 +0000119selectCallee(const ModuleSummaryIndex &Index,
120 const GlobalValueSummaryList &CalleeSummaryList,
Teresa Johnson28e457b2016-04-24 14:57:11 +0000121 unsigned Threshold) {
Mehdi Amini01e32132016-03-26 05:40:34 +0000122 auto It = llvm::find_if(
Teresa Johnson28e457b2016-04-24 14:57:11 +0000123 CalleeSummaryList,
124 [&](const std::unique_ptr<GlobalValueSummary> &SummaryPtr) {
125 auto *GVSummary = SummaryPtr.get();
Rafael Espindolaf329be82016-05-11 01:26:06 +0000126 if (GlobalValue::isInterposableLinkage(GVSummary->linkage()))
Mehdi Amini5b85d8d2016-05-03 00:27:28 +0000127 // There is no point in importing these, we can't inline them
Mehdi Amini01e32132016-03-26 05:40:34 +0000128 return false;
Mehdi Amini2c719cc2016-04-20 04:17:36 +0000129 if (auto *AS = dyn_cast<AliasSummary>(GVSummary)) {
130 GVSummary = &AS->getAliasee();
131 // Alias can't point to "available_externally". However when we import
132 // linkOnceODR the linkage does not change. So we import the alias
133 // and aliasee only in this case.
134 // FIXME: we should import alias as available_externally *function*,
135 // the destination module does need to know it is an alias.
136 if (!GlobalValue::isLinkOnceODRLinkage(GVSummary->linkage()))
137 return false;
138 }
139
140 auto *Summary = cast<FunctionSummary>(GVSummary);
Mehdi Amini7e88d0d2015-12-09 08:17:35 +0000141
Mehdi Amini01e32132016-03-26 05:40:34 +0000142 if (Summary->instCount() > Threshold)
143 return false;
Mehdi Amini7e88d0d2015-12-09 08:17:35 +0000144
Teresa Johnson519465b2017-01-05 14:32:16 +0000145 if (Summary->notEligibleToImport())
Mehdi Aminib4e1e822016-04-27 00:32:13 +0000146 return false;
147
Mehdi Amini01e32132016-03-26 05:40:34 +0000148 return true;
149 });
Teresa Johnson28e457b2016-04-24 14:57:11 +0000150 if (It == CalleeSummaryList.end())
Mehdi Amini01e32132016-03-26 05:40:34 +0000151 return nullptr;
Mehdi Amini7e88d0d2015-12-09 08:17:35 +0000152
Teresa Johnson28e457b2016-04-24 14:57:11 +0000153 return cast<GlobalValueSummary>(It->get());
Mehdi Amini7e88d0d2015-12-09 08:17:35 +0000154}
Mehdi Amini7e88d0d2015-12-09 08:17:35 +0000155
Mehdi Amini01e32132016-03-26 05:40:34 +0000156/// Return the summary for the function \p GUID that fits the \p Threshold, or
157/// null if there's no match.
Mehdi Amini2d28f7a2016-04-16 06:56:44 +0000158static const GlobalValueSummary *selectCallee(GlobalValue::GUID GUID,
159 unsigned Threshold,
160 const ModuleSummaryIndex &Index) {
Teresa Johnson28e457b2016-04-24 14:57:11 +0000161 auto CalleeSummaryList = Index.findGlobalValueSummaryList(GUID);
Mehdi Aminib4e1e822016-04-27 00:32:13 +0000162 if (CalleeSummaryList == Index.end())
Mehdi Amini01e32132016-03-26 05:40:34 +0000163 return nullptr; // This function does not have a summary
Mehdi Aminib4e1e822016-04-27 00:32:13 +0000164 return selectCallee(Index, CalleeSummaryList->second, Threshold);
Mehdi Amini01e32132016-03-26 05:40:34 +0000165}
Mehdi Amini7e88d0d2015-12-09 08:17:35 +0000166
Teresa Johnson475b51a2016-12-15 20:48:19 +0000167using EdgeInfo = std::tuple<const FunctionSummary *, unsigned /* Threshold */,
168 GlobalValue::GUID>;
Mehdi Amini7e88d0d2015-12-09 08:17:35 +0000169
Mehdi Amini01e32132016-03-26 05:40:34 +0000170/// Compute the list of functions to import for a given caller. Mark these
171/// imported functions and the symbols they reference in their source module as
172/// exported from their source module.
173static void computeImportForFunction(
Teresa Johnson3255eec2016-04-10 15:17:26 +0000174 const FunctionSummary &Summary, const ModuleSummaryIndex &Index,
Piotr Padlewskid9830eb2016-09-26 20:37:32 +0000175 const unsigned Threshold, const GVSummaryMapTy &DefinedGVSummaries,
Mehdi Amini01e32132016-03-26 05:40:34 +0000176 SmallVectorImpl<EdgeInfo> &Worklist,
Mehdi Amini9b490f12016-08-16 05:47:12 +0000177 FunctionImporter::ImportMapTy &ImportList,
Teresa Johnsonc86af332016-04-12 21:13:11 +0000178 StringMap<FunctionImporter::ExportSetTy> *ExportLists = nullptr) {
Mehdi Amini01e32132016-03-26 05:40:34 +0000179 for (auto &Edge : Summary.calls()) {
Teresa Johnson2d5487c2016-04-11 13:58:45 +0000180 auto GUID = Edge.first.getGUID();
Mehdi Amini01e32132016-03-26 05:40:34 +0000181 DEBUG(dbgs() << " edge -> " << GUID << " Threshold:" << Threshold << "\n");
182
Mehdi Amini1aafabf2016-04-16 07:02:16 +0000183 if (DefinedGVSummaries.count(GUID)) {
Mehdi Amini01e32132016-03-26 05:40:34 +0000184 DEBUG(dbgs() << "ignored! Target already in destination module.\n");
185 continue;
Teresa Johnsond450da32015-11-24 21:15:19 +0000186 }
Mehdi Amini01e32132016-03-26 05:40:34 +0000187
Piotr Padlewskiba72b952016-09-29 17:32:07 +0000188 auto GetBonusMultiplier = [](CalleeInfo::HotnessType Hotness) -> float {
189 if (Hotness == CalleeInfo::HotnessType::Hot)
190 return ImportHotMultiplier;
191 if (Hotness == CalleeInfo::HotnessType::Cold)
192 return ImportColdMultiplier;
193 return 1.0;
194 };
195
Piotr Padlewskid9830eb2016-09-26 20:37:32 +0000196 const auto NewThreshold =
Piotr Padlewskiba72b952016-09-29 17:32:07 +0000197 Threshold * GetBonusMultiplier(Edge.second.Hotness);
Piotr Padlewskid2869472016-09-30 03:01:17 +0000198
Piotr Padlewskid9830eb2016-09-26 20:37:32 +0000199 auto *CalleeSummary = selectCallee(GUID, NewThreshold, Index);
Mehdi Amini01e32132016-03-26 05:40:34 +0000200 if (!CalleeSummary) {
201 DEBUG(dbgs() << "ignored! No qualifying callee with summary found.\n");
202 continue;
203 }
Mehdi Amini2d28f7a2016-04-16 06:56:44 +0000204 // "Resolve" the summary, traversing alias,
205 const FunctionSummary *ResolvedCalleeSummary;
Mehdi Amini6968ef72016-04-20 01:04:20 +0000206 if (isa<AliasSummary>(CalleeSummary)) {
Mehdi Amini2d28f7a2016-04-16 06:56:44 +0000207 ResolvedCalleeSummary = cast<FunctionSummary>(
208 &cast<AliasSummary>(CalleeSummary)->getAliasee());
Mehdi Amini2c719cc2016-04-20 04:17:36 +0000209 assert(
210 GlobalValue::isLinkOnceODRLinkage(ResolvedCalleeSummary->linkage()) &&
211 "Unexpected alias to a non-linkonceODR in import list");
Mehdi Amini6968ef72016-04-20 01:04:20 +0000212 } else
Mehdi Amini2d28f7a2016-04-16 06:56:44 +0000213 ResolvedCalleeSummary = cast<FunctionSummary>(CalleeSummary);
214
Piotr Padlewskid9830eb2016-09-26 20:37:32 +0000215 assert(ResolvedCalleeSummary->instCount() <= NewThreshold &&
Mehdi Amini01e32132016-03-26 05:40:34 +0000216 "selectCallee() didn't honor the threshold");
217
Piotr Padlewskid2869472016-09-30 03:01:17 +0000218 auto GetAdjustedThreshold = [](unsigned Threshold, bool IsHotCallsite) {
219 // Adjust the threshold for next level of imported functions.
220 // The threshold is different for hot callsites because we can then
221 // inline chains of hot calls.
222 if (IsHotCallsite)
223 return Threshold * ImportHotInstrFactor;
224 return Threshold * ImportInstrFactor;
225 };
226
227 bool IsHotCallsite = Edge.second.Hotness == CalleeInfo::HotnessType::Hot;
Teresa Johnson1b859a22016-12-15 18:21:01 +0000228 const auto AdjThreshold = GetAdjustedThreshold(Threshold, IsHotCallsite);
229
230 auto ExportModulePath = ResolvedCalleeSummary->modulePath();
231 auto &ProcessedThreshold = ImportList[ExportModulePath][GUID];
232 /// Since the traversal of the call graph is DFS, we can revisit a function
233 /// a second time with a higher threshold. In this case, it is added back to
234 /// the worklist with the new threshold.
235 if (ProcessedThreshold && ProcessedThreshold >= AdjThreshold) {
236 DEBUG(dbgs() << "ignored! Target was already seen with Threshold "
237 << ProcessedThreshold << "\n");
238 continue;
239 }
Teresa Johnson19f2aa72016-12-15 23:50:06 +0000240 bool PreviouslyImported = ProcessedThreshold != 0;
Teresa Johnson1b859a22016-12-15 18:21:01 +0000241 // Mark this function as imported in this module, with the current Threshold
242 ProcessedThreshold = AdjThreshold;
243
244 // Make exports in the source module.
245 if (ExportLists) {
246 auto &ExportList = (*ExportLists)[ExportModulePath];
247 ExportList.insert(GUID);
Teresa Johnson19f2aa72016-12-15 23:50:06 +0000248 if (!PreviouslyImported) {
249 // This is the first time this function was exported from its source
250 // module, so mark all functions and globals it references as exported
251 // to the outside if they are defined in the same source module.
Teresa Johnsonedddca22016-12-16 04:11:51 +0000252 // For efficiency, we unconditionally add all the referenced GUIDs
253 // to the ExportList for this module, and will prune out any not
254 // defined in the module later in a single pass.
Teresa Johnson19f2aa72016-12-15 23:50:06 +0000255 for (auto &Edge : ResolvedCalleeSummary->calls()) {
256 auto CalleeGUID = Edge.first.getGUID();
Teresa Johnsonedddca22016-12-16 04:11:51 +0000257 ExportList.insert(CalleeGUID);
Teresa Johnson19f2aa72016-12-15 23:50:06 +0000258 }
259 for (auto &Ref : ResolvedCalleeSummary->refs()) {
260 auto GUID = Ref.getGUID();
Teresa Johnsonedddca22016-12-16 04:11:51 +0000261 ExportList.insert(GUID);
Teresa Johnson19f2aa72016-12-15 23:50:06 +0000262 }
Teresa Johnson1b859a22016-12-15 18:21:01 +0000263 }
264 }
Piotr Padlewskid2869472016-09-30 03:01:17 +0000265
Mehdi Amini01e32132016-03-26 05:40:34 +0000266 // Insert the newly imported function to the worklist.
Teresa Johnson475b51a2016-12-15 20:48:19 +0000267 Worklist.emplace_back(ResolvedCalleeSummary, AdjThreshold, GUID);
Teresa Johnsond450da32015-11-24 21:15:19 +0000268 }
269}
270
Mehdi Amini01e32132016-03-26 05:40:34 +0000271/// Given the list of globals defined in a module, compute the list of imports
272/// as well as the list of "exports", i.e. the list of symbols referenced from
273/// another module (that may require promotion).
274static void ComputeImportForModule(
Teresa Johnsonc851d212016-04-25 21:09:51 +0000275 const GVSummaryMapTy &DefinedGVSummaries, const ModuleSummaryIndex &Index,
Mehdi Amini9b490f12016-08-16 05:47:12 +0000276 FunctionImporter::ImportMapTy &ImportList,
Teresa Johnsonc86af332016-04-12 21:13:11 +0000277 StringMap<FunctionImporter::ExportSetTy> *ExportLists = nullptr) {
Mehdi Amini01e32132016-03-26 05:40:34 +0000278 // Worklist contains the list of function imported in this module, for which
279 // we will analyse the callees and may import further down the callgraph.
280 SmallVector<EdgeInfo, 128> Worklist;
281
282 // Populate the worklist with the import for the functions in the current
283 // module
Teresa Johnson28e457b2016-04-24 14:57:11 +0000284 for (auto &GVSummary : DefinedGVSummaries) {
285 auto *Summary = GVSummary.second;
Mehdi Amini2d28f7a2016-04-16 06:56:44 +0000286 if (auto *AS = dyn_cast<AliasSummary>(Summary))
287 Summary = &AS->getAliasee();
Mehdi Amini1aafabf2016-04-16 07:02:16 +0000288 auto *FuncSummary = dyn_cast<FunctionSummary>(Summary);
289 if (!FuncSummary)
290 // Skip import for global variables
291 continue;
Teresa Johnson28e457b2016-04-24 14:57:11 +0000292 DEBUG(dbgs() << "Initalize import for " << GVSummary.first << "\n");
Mehdi Amini2d28f7a2016-04-16 06:56:44 +0000293 computeImportForFunction(*FuncSummary, Index, ImportInstrLimit,
Mehdi Amini9b490f12016-08-16 05:47:12 +0000294 DefinedGVSummaries, Worklist, ImportList,
Mehdi Amini01e32132016-03-26 05:40:34 +0000295 ExportLists);
296 }
297
Piotr Padlewskid2869472016-09-30 03:01:17 +0000298 // Process the newly imported functions and add callees to the worklist.
Mehdi Amini42418ab2015-11-24 06:07:49 +0000299 while (!Worklist.empty()) {
Mehdi Amini01e32132016-03-26 05:40:34 +0000300 auto FuncInfo = Worklist.pop_back_val();
Teresa Johnson475b51a2016-12-15 20:48:19 +0000301 auto *Summary = std::get<0>(FuncInfo);
302 auto Threshold = std::get<1>(FuncInfo);
303 auto GUID = std::get<2>(FuncInfo);
304
305 // Check if we later added this summary with a higher threshold.
306 // If so, skip this entry.
307 auto ExportModulePath = Summary->modulePath();
308 auto &LatestProcessedThreshold = ImportList[ExportModulePath][GUID];
309 if (LatestProcessedThreshold > Threshold)
310 continue;
Mehdi Amini42418ab2015-11-24 06:07:49 +0000311
Mehdi Amini1aafabf2016-04-16 07:02:16 +0000312 computeImportForFunction(*Summary, Index, Threshold, DefinedGVSummaries,
Mehdi Amini9b490f12016-08-16 05:47:12 +0000313 Worklist, ImportList, ExportLists);
Mehdi Amini42418ab2015-11-24 06:07:49 +0000314 }
Mehdi Aminic8c55172015-12-03 02:37:33 +0000315}
Mehdi Aminiffe2e4a2015-12-02 04:34:28 +0000316
Mehdi Amini01e32132016-03-26 05:40:34 +0000317} // anonymous namespace
318
Teresa Johnsonc86af332016-04-12 21:13:11 +0000319/// Compute all the import and export for every module using the Index.
Mehdi Amini01e32132016-03-26 05:40:34 +0000320void llvm::ComputeCrossModuleImport(
321 const ModuleSummaryIndex &Index,
Teresa Johnsonc851d212016-04-25 21:09:51 +0000322 const StringMap<GVSummaryMapTy> &ModuleToDefinedGVSummaries,
Mehdi Amini01e32132016-03-26 05:40:34 +0000323 StringMap<FunctionImporter::ImportMapTy> &ImportLists,
324 StringMap<FunctionImporter::ExportSetTy> &ExportLists) {
Mehdi Amini01e32132016-03-26 05:40:34 +0000325 // For each module that has function defined, compute the import/export lists.
Mehdi Amini1aafabf2016-04-16 07:02:16 +0000326 for (auto &DefinedGVSummaries : ModuleToDefinedGVSummaries) {
Mehdi Amini9b490f12016-08-16 05:47:12 +0000327 auto &ImportList = ImportLists[DefinedGVSummaries.first()];
Mehdi Amini1aafabf2016-04-16 07:02:16 +0000328 DEBUG(dbgs() << "Computing import for Module '"
329 << DefinedGVSummaries.first() << "'\n");
Mehdi Amini9b490f12016-08-16 05:47:12 +0000330 ComputeImportForModule(DefinedGVSummaries.second, Index, ImportList,
Teresa Johnsonc86af332016-04-12 21:13:11 +0000331 &ExportLists);
Mehdi Amini01e32132016-03-26 05:40:34 +0000332 }
333
Teresa Johnsonedddca22016-12-16 04:11:51 +0000334 // When computing imports we added all GUIDs referenced by anything
335 // imported from the module to its ExportList. Now we prune each ExportList
336 // of any not defined in that module. This is more efficient than checking
337 // while computing imports because some of the summary lists may be long
338 // due to linkonce (comdat) copies.
339 for (auto &ELI : ExportLists) {
340 const auto &DefinedGVSummaries =
341 ModuleToDefinedGVSummaries.lookup(ELI.first());
342 for (auto EI = ELI.second.begin(); EI != ELI.second.end();) {
343 if (!DefinedGVSummaries.count(*EI))
344 EI = ELI.second.erase(EI);
345 else
346 ++EI;
347 }
348 }
349
Mehdi Amini01e32132016-03-26 05:40:34 +0000350#ifndef NDEBUG
351 DEBUG(dbgs() << "Import/Export lists for " << ImportLists.size()
352 << " modules:\n");
353 for (auto &ModuleImports : ImportLists) {
354 auto ModName = ModuleImports.first();
355 auto &Exports = ExportLists[ModName];
356 DEBUG(dbgs() << "* Module " << ModName << " exports " << Exports.size()
357 << " functions. Imports from " << ModuleImports.second.size()
358 << " modules.\n");
359 for (auto &Src : ModuleImports.second) {
360 auto SrcModName = Src.first();
361 DEBUG(dbgs() << " - " << Src.second.size() << " functions imported from "
362 << SrcModName << "\n");
363 }
364 }
365#endif
366}
367
Teresa Johnsonc86af332016-04-12 21:13:11 +0000368/// Compute all the imports for the given module in the Index.
369void llvm::ComputeCrossModuleImportForModule(
370 StringRef ModulePath, const ModuleSummaryIndex &Index,
371 FunctionImporter::ImportMapTy &ImportList) {
372
373 // Collect the list of functions this module defines.
374 // GUID -> Summary
Teresa Johnsonc851d212016-04-25 21:09:51 +0000375 GVSummaryMapTy FunctionSummaryMap;
Teresa Johnson28e457b2016-04-24 14:57:11 +0000376 Index.collectDefinedFunctionsForModule(ModulePath, FunctionSummaryMap);
Teresa Johnsonc86af332016-04-12 21:13:11 +0000377
378 // Compute the import list for this module.
379 DEBUG(dbgs() << "Computing import for Module '" << ModulePath << "'\n");
Teresa Johnson28e457b2016-04-24 14:57:11 +0000380 ComputeImportForModule(FunctionSummaryMap, Index, ImportList);
Teresa Johnsonc86af332016-04-12 21:13:11 +0000381
382#ifndef NDEBUG
383 DEBUG(dbgs() << "* Module " << ModulePath << " imports from "
384 << ImportList.size() << " modules.\n");
385 for (auto &Src : ImportList) {
386 auto SrcModName = Src.first();
387 DEBUG(dbgs() << " - " << Src.second.size() << " functions imported from "
388 << SrcModName << "\n");
389 }
390#endif
391}
392
Teresa Johnson84174c32016-05-10 13:48:23 +0000393/// Compute the set of summaries needed for a ThinLTO backend compilation of
394/// \p ModulePath.
395void llvm::gatherImportedSummariesForModule(
396 StringRef ModulePath,
397 const StringMap<GVSummaryMapTy> &ModuleToDefinedGVSummaries,
Mehdi Aminicdbcbf72016-08-16 05:46:05 +0000398 const FunctionImporter::ImportMapTy &ImportList,
Teresa Johnson84174c32016-05-10 13:48:23 +0000399 std::map<std::string, GVSummaryMapTy> &ModuleToSummariesForIndex) {
400 // Include all summaries from the importing module.
401 ModuleToSummariesForIndex[ModulePath] =
402 ModuleToDefinedGVSummaries.lookup(ModulePath);
Mehdi Aminicdbcbf72016-08-16 05:46:05 +0000403 // Include summaries for imports.
Mehdi Amini88c491d2016-08-16 05:49:12 +0000404 for (auto &ILI : ImportList) {
Mehdi Aminicdbcbf72016-08-16 05:46:05 +0000405 auto &SummariesForIndex = ModuleToSummariesForIndex[ILI.first()];
406 const auto &DefinedGVSummaries =
407 ModuleToDefinedGVSummaries.lookup(ILI.first());
408 for (auto &GI : ILI.second) {
409 const auto &DS = DefinedGVSummaries.find(GI.first);
410 assert(DS != DefinedGVSummaries.end() &&
411 "Expected a defined summary for imported global value");
412 SummariesForIndex[GI.first] = DS->second;
Teresa Johnson84174c32016-05-10 13:48:23 +0000413 }
414 }
415}
416
Teresa Johnson8570fe42016-05-10 15:54:09 +0000417/// Emit the files \p ModulePath will import from into \p OutputFilename.
Mehdi Aminicdbcbf72016-08-16 05:46:05 +0000418std::error_code
419llvm::EmitImportsFiles(StringRef ModulePath, StringRef OutputFilename,
420 const FunctionImporter::ImportMapTy &ModuleImports) {
Teresa Johnson8570fe42016-05-10 15:54:09 +0000421 std::error_code EC;
422 raw_fd_ostream ImportsOS(OutputFilename, EC, sys::fs::OpenFlags::F_None);
423 if (EC)
424 return EC;
Mehdi Aminicdbcbf72016-08-16 05:46:05 +0000425 for (auto &ILI : ModuleImports)
426 ImportsOS << ILI.first() << "\n";
Teresa Johnson8570fe42016-05-10 15:54:09 +0000427 return std::error_code();
428}
429
Teresa Johnson04c9a2d2016-05-25 14:03:11 +0000430/// Fixup WeakForLinker linkages in \p TheModule based on summary analysis.
431void llvm::thinLTOResolveWeakForLinkerModule(
432 Module &TheModule, const GVSummaryMapTy &DefinedGlobals) {
433 auto updateLinkage = [&](GlobalValue &GV) {
434 if (!GlobalValue::isWeakForLinker(GV.getLinkage()))
435 return;
436 // See if the global summary analysis computed a new resolved linkage.
437 const auto &GS = DefinedGlobals.find(GV.getGUID());
438 if (GS == DefinedGlobals.end())
439 return;
440 auto NewLinkage = GS->second->linkage();
441 if (NewLinkage == GV.getLinkage())
442 return;
443 DEBUG(dbgs() << "ODR fixing up linkage for `" << GV.getName() << "` from "
444 << GV.getLinkage() << " to " << NewLinkage << "\n");
445 GV.setLinkage(NewLinkage);
Teresa Johnson6107a412016-08-15 21:00:04 +0000446 // Remove functions converted to available_externally from comdats,
447 // as this is a declaration for the linker, and will be dropped eventually.
448 // It is illegal for comdats to contain declarations.
449 auto *GO = dyn_cast_or_null<GlobalObject>(&GV);
450 if (GO && GO->isDeclarationForLinker() && GO->hasComdat()) {
451 assert(GO->hasAvailableExternallyLinkage() &&
452 "Expected comdat on definition (possibly available external)");
453 GO->setComdat(nullptr);
454 }
Teresa Johnson04c9a2d2016-05-25 14:03:11 +0000455 };
456
457 // Process functions and global now
458 for (auto &GV : TheModule)
459 updateLinkage(GV);
460 for (auto &GV : TheModule.globals())
461 updateLinkage(GV);
462 for (auto &GV : TheModule.aliases())
463 updateLinkage(GV);
464}
465
466/// Run internalization on \p TheModule based on symmary analysis.
467void llvm::thinLTOInternalizeModule(Module &TheModule,
468 const GVSummaryMapTy &DefinedGlobals) {
469 // Parse inline ASM and collect the list of symbols that are not defined in
470 // the current module.
471 StringSet<> AsmUndefinedRefs;
Peter Collingbourne863cbfb2016-12-01 06:51:47 +0000472 ModuleSymbolTable::CollectAsmSymbols(
Teresa Johnson04c9a2d2016-05-25 14:03:11 +0000473 Triple(TheModule.getTargetTriple()), TheModule.getModuleInlineAsm(),
474 [&AsmUndefinedRefs](StringRef Name, object::BasicSymbolRef::Flags Flags) {
475 if (Flags & object::BasicSymbolRef::SF_Undefined)
476 AsmUndefinedRefs.insert(Name);
477 });
478
479 // Declare a callback for the internalize pass that will ask for every
480 // candidate GlobalValue if it can be internalized or not.
481 auto MustPreserveGV = [&](const GlobalValue &GV) -> bool {
482 // Can't be internalized if referenced in inline asm.
483 if (AsmUndefinedRefs.count(GV.getName()))
484 return true;
485
486 // Lookup the linkage recorded in the summaries during global analysis.
487 const auto &GS = DefinedGlobals.find(GV.getGUID());
488 GlobalValue::LinkageTypes Linkage;
489 if (GS == DefinedGlobals.end()) {
490 // Must have been promoted (possibly conservatively). Find original
491 // name so that we can access the correct summary and see if it can
492 // be internalized again.
493 // FIXME: Eventually we should control promotion instead of promoting
494 // and internalizing again.
495 StringRef OrigName =
496 ModuleSummaryIndex::getOriginalNameBeforePromote(GV.getName());
497 std::string OrigId = GlobalValue::getGlobalIdentifier(
498 OrigName, GlobalValue::InternalLinkage,
499 TheModule.getSourceFileName());
500 const auto &GS = DefinedGlobals.find(GlobalValue::getGUID(OrigId));
Teresa Johnson7ab1f692016-06-09 01:14:13 +0000501 if (GS == DefinedGlobals.end()) {
502 // Also check the original non-promoted non-globalized name. In some
503 // cases a preempted weak value is linked in as a local copy because
504 // it is referenced by an alias (IRLinker::linkGlobalValueProto).
505 // In that case, since it was originally not a local value, it was
506 // recorded in the index using the original name.
507 // FIXME: This may not be needed once PR27866 is fixed.
508 const auto &GS = DefinedGlobals.find(GlobalValue::getGUID(OrigName));
509 assert(GS != DefinedGlobals.end());
510 Linkage = GS->second->linkage();
511 } else {
512 Linkage = GS->second->linkage();
513 }
Teresa Johnson04c9a2d2016-05-25 14:03:11 +0000514 } else
515 Linkage = GS->second->linkage();
516 return !GlobalValue::isLocalLinkage(Linkage);
517 };
518
519 // FIXME: See if we can just internalize directly here via linkage changes
520 // based on the index, rather than invoking internalizeModule.
521 llvm::internalizeModule(TheModule, MustPreserveGV);
522}
523
Mehdi Aminic8c55172015-12-03 02:37:33 +0000524// Automatically import functions in Module \p DestModule based on the summaries
525// index.
526//
Peter Collingbourne7f00d0a2016-11-09 17:49:19 +0000527Expected<bool> FunctionImporter::importFunctions(
Mehdi Aminibda3c972016-04-21 01:59:39 +0000528 Module &DestModule, const FunctionImporter::ImportMapTy &ImportList,
529 bool ForceImportReferencedDiscardableSymbols) {
Mehdi Amini5411d052015-12-08 23:04:19 +0000530 DEBUG(dbgs() << "Starting import for Module "
Mehdi Amini311fef62015-12-03 02:58:14 +0000531 << DestModule.getModuleIdentifier() << "\n");
Mehdi Aminic8c55172015-12-03 02:37:33 +0000532 unsigned ImportedCount = 0;
533
Mehdi Aminic8c55172015-12-03 02:37:33 +0000534 // Linker that will be used for importing function
Rafael Espindola9d2bfc42015-12-14 23:17:03 +0000535 Linker TheLinker(DestModule);
Mehdi Amini7e88d0d2015-12-09 08:17:35 +0000536 // Do the actual import of functions now, one Module at a time
Mehdi Amini01e32132016-03-26 05:40:34 +0000537 std::set<StringRef> ModuleNameOrderedList;
538 for (auto &FunctionsToImportPerModule : ImportList) {
539 ModuleNameOrderedList.insert(FunctionsToImportPerModule.first());
540 }
541 for (auto &Name : ModuleNameOrderedList) {
Mehdi Amini7e88d0d2015-12-09 08:17:35 +0000542 // Get the module for the import
Mehdi Amini01e32132016-03-26 05:40:34 +0000543 const auto &FunctionsToImportPerModule = ImportList.find(Name);
544 assert(FunctionsToImportPerModule != ImportList.end());
Peter Collingbourned9445c42016-11-13 07:00:17 +0000545 Expected<std::unique_ptr<Module>> SrcModuleOrErr = ModuleLoader(Name);
546 if (!SrcModuleOrErr)
547 return SrcModuleOrErr.takeError();
548 std::unique_ptr<Module> SrcModule = std::move(*SrcModuleOrErr);
Mehdi Amini7e88d0d2015-12-09 08:17:35 +0000549 assert(&DestModule.getContext() == &SrcModule->getContext() &&
550 "Context mismatch");
551
Teresa Johnson6cba37c2016-01-22 00:15:53 +0000552 // If modules were created with lazy metadata loading, materialize it
553 // now, before linking it (otherwise this will be a noop).
Peter Collingbourne7f00d0a2016-11-09 17:49:19 +0000554 if (Error Err = SrcModule->materializeMetadata())
555 return std::move(Err);
Teresa Johnsone5a61912015-12-17 17:14:09 +0000556
Mehdi Amini01e32132016-03-26 05:40:34 +0000557 auto &ImportGUIDs = FunctionsToImportPerModule->second;
558 // Find the globals to import
559 DenseSet<const GlobalValue *> GlobalsToImport;
Piotr Padlewski1f685e02016-07-06 18:12:23 +0000560 for (Function &F : *SrcModule) {
561 if (!F.hasName())
Teresa Johnson0beb8582016-04-04 18:52:23 +0000562 continue;
Piotr Padlewski1f685e02016-07-06 18:12:23 +0000563 auto GUID = F.getGUID();
Teresa Johnson0beb8582016-04-04 18:52:23 +0000564 auto Import = ImportGUIDs.count(GUID);
Mehdi Aminiaeb1e592016-04-19 09:21:30 +0000565 DEBUG(dbgs() << (Import ? "Is" : "Not") << " importing function " << GUID
Piotr Padlewski1f685e02016-07-06 18:12:23 +0000566 << " " << F.getName() << " from "
Mehdi Aminiaeb1e592016-04-19 09:21:30 +0000567 << SrcModule->getSourceFileName() << "\n");
Teresa Johnson0beb8582016-04-04 18:52:23 +0000568 if (Import) {
Peter Collingbourne7f00d0a2016-11-09 17:49:19 +0000569 if (Error Err = F.materialize())
570 return std::move(Err);
Piotr Padlewski3b776122016-07-08 23:01:49 +0000571 if (EnableImportMetadata) {
572 // Add 'thinlto_src_module' metadata for statistics and debugging.
573 F.setMetadata(
574 "thinlto_src_module",
575 llvm::MDNode::get(
576 DestModule.getContext(),
577 {llvm::MDString::get(DestModule.getContext(),
578 SrcModule->getSourceFileName())}));
579 }
Piotr Padlewski1f685e02016-07-06 18:12:23 +0000580 GlobalsToImport.insert(&F);
Mehdi Amini01e32132016-03-26 05:40:34 +0000581 }
582 }
Piotr Padlewski1f685e02016-07-06 18:12:23 +0000583 for (GlobalVariable &GV : SrcModule->globals()) {
Mehdi Amini2d28f7a2016-04-16 06:56:44 +0000584 if (!GV.hasName())
585 continue;
586 auto GUID = GV.getGUID();
587 auto Import = ImportGUIDs.count(GUID);
Mehdi Aminiaeb1e592016-04-19 09:21:30 +0000588 DEBUG(dbgs() << (Import ? "Is" : "Not") << " importing global " << GUID
589 << " " << GV.getName() << " from "
590 << SrcModule->getSourceFileName() << "\n");
Mehdi Amini2d28f7a2016-04-16 06:56:44 +0000591 if (Import) {
Peter Collingbourne7f00d0a2016-11-09 17:49:19 +0000592 if (Error Err = GV.materialize())
593 return std::move(Err);
Mehdi Amini2d28f7a2016-04-16 06:56:44 +0000594 GlobalsToImport.insert(&GV);
595 }
596 }
Piotr Padlewski1f685e02016-07-06 18:12:23 +0000597 for (GlobalAlias &GA : SrcModule->aliases()) {
598 if (!GA.hasName())
Mehdi Amini01e32132016-03-26 05:40:34 +0000599 continue;
Piotr Padlewski1f685e02016-07-06 18:12:23 +0000600 auto GUID = GA.getGUID();
Teresa Johnson0beb8582016-04-04 18:52:23 +0000601 auto Import = ImportGUIDs.count(GUID);
Mehdi Aminiaeb1e592016-04-19 09:21:30 +0000602 DEBUG(dbgs() << (Import ? "Is" : "Not") << " importing alias " << GUID
Piotr Padlewski1f685e02016-07-06 18:12:23 +0000603 << " " << GA.getName() << " from "
Mehdi Aminiaeb1e592016-04-19 09:21:30 +0000604 << SrcModule->getSourceFileName() << "\n");
Teresa Johnson0beb8582016-04-04 18:52:23 +0000605 if (Import) {
Mehdi Amini01e32132016-03-26 05:40:34 +0000606 // Alias can't point to "available_externally". However when we import
Teresa Johnson9aae3952016-03-27 15:01:11 +0000607 // linkOnceODR the linkage does not change. So we import the alias
Mehdi Amini6968ef72016-04-20 01:04:20 +0000608 // and aliasee only in this case. This has been handled by
609 // computeImportForFunction()
Piotr Padlewski1f685e02016-07-06 18:12:23 +0000610 GlobalObject *GO = GA.getBaseObject();
Mehdi Amini6968ef72016-04-20 01:04:20 +0000611 assert(GO->hasLinkOnceODRLinkage() &&
612 "Unexpected alias to a non-linkonceODR in import list");
Mehdi Amini2d28f7a2016-04-16 06:56:44 +0000613#ifndef NDEBUG
614 if (!GlobalsToImport.count(GO))
615 DEBUG(dbgs() << " alias triggers importing aliasee " << GO->getGUID()
616 << " " << GO->getName() << " from "
617 << SrcModule->getSourceFileName() << "\n");
618#endif
Peter Collingbourne7f00d0a2016-11-09 17:49:19 +0000619 if (Error Err = GO->materialize())
620 return std::move(Err);
Mehdi Amini01e32132016-03-26 05:40:34 +0000621 GlobalsToImport.insert(GO);
Peter Collingbourne7f00d0a2016-11-09 17:49:19 +0000622 if (Error Err = GA.materialize())
623 return std::move(Err);
Piotr Padlewski1f685e02016-07-06 18:12:23 +0000624 GlobalsToImport.insert(&GA);
Mehdi Amini01e32132016-03-26 05:40:34 +0000625 }
626 }
627
Mehdi Amini19ef4fa2017-01-04 22:54:33 +0000628 // Upgrade debug info after we're done materializing all the globals and we
629 // have loaded all the required metadata!
630 UpgradeDebugInfo(*SrcModule);
631
Mehdi Amini7e88d0d2015-12-09 08:17:35 +0000632 // Link in the specified functions.
Mehdi Amini01e32132016-03-26 05:40:34 +0000633 if (renameModuleForThinLTO(*SrcModule, Index, &GlobalsToImport))
Mehdi Amini8d051852016-03-19 00:40:31 +0000634 return true;
635
Teresa Johnsond29478f2016-03-27 15:27:30 +0000636 if (PrintImports) {
637 for (const auto *GV : GlobalsToImport)
638 dbgs() << DestModule.getSourceFileName() << ": Import " << GV->getName()
639 << " from " << SrcModule->getSourceFileName() << "\n";
640 }
641
Mehdi Aminibda3c972016-04-21 01:59:39 +0000642 // Instruct the linker that the client will take care of linkonce resolution
643 unsigned Flags = Linker::Flags::None;
644 if (!ForceImportReferencedDiscardableSymbols)
645 Flags |= Linker::Flags::DontForceLinkLinkonceODR;
646
647 if (TheLinker.linkInModule(std::move(SrcModule), Flags, &GlobalsToImport))
Mehdi Amini7e88d0d2015-12-09 08:17:35 +0000648 report_fatal_error("Function Import: link error");
649
Mehdi Amini01e32132016-03-26 05:40:34 +0000650 ImportedCount += GlobalsToImport.size();
Mehdi Amini7e88d0d2015-12-09 08:17:35 +0000651 }
Teresa Johnsone5a61912015-12-17 17:14:09 +0000652
Teresa Johnsond29478f2016-03-27 15:27:30 +0000653 NumImported += ImportedCount;
654
Mehdi Amini7e88d0d2015-12-09 08:17:35 +0000655 DEBUG(dbgs() << "Imported " << ImportedCount << " functions for Module "
Mehdi Aminic8c55172015-12-03 02:37:33 +0000656 << DestModule.getModuleIdentifier() << "\n");
657 return ImportedCount;
Mehdi Amini42418ab2015-11-24 06:07:49 +0000658}
659
660/// Summary file to use for function importing when using -function-import from
661/// the command line.
662static cl::opt<std::string>
663 SummaryFile("summary-file",
664 cl::desc("The summary file to use for function importing."));
665
Peter Collingbourne598bd2a2016-12-21 00:50:12 +0000666static bool doImportingForModule(Module &M) {
667 if (SummaryFile.empty())
668 report_fatal_error("error: -function-import requires -summary-file\n");
669 Expected<std::unique_ptr<ModuleSummaryIndex>> IndexPtrOrErr =
670 getModuleSummaryIndexForFile(SummaryFile);
671 if (!IndexPtrOrErr) {
672 logAllUnhandledErrors(IndexPtrOrErr.takeError(), errs(),
673 "Error loading file '" + SummaryFile + "': ");
674 return false;
Teresa Johnson21241572016-07-18 21:22:24 +0000675 }
Peter Collingbourne598bd2a2016-12-21 00:50:12 +0000676 std::unique_ptr<ModuleSummaryIndex> Index = std::move(*IndexPtrOrErr);
Teresa Johnson21241572016-07-18 21:22:24 +0000677
678 // First step is collecting the import list.
679 FunctionImporter::ImportMapTy ImportList;
680 ComputeCrossModuleImportForModule(M.getModuleIdentifier(), *Index,
681 ImportList);
682
Teresa Johnson4fef68c2016-11-14 19:21:41 +0000683 // Conservatively mark all internal values as promoted. This interface is
684 // only used when doing importing via the function importing pass. The pass
685 // is only enabled when testing importing via the 'opt' tool, which does
686 // not do the ThinLink that would normally determine what values to promote.
687 for (auto &I : *Index) {
688 for (auto &S : I.second) {
689 if (GlobalValue::isLocalLinkage(S->linkage()))
690 S->setLinkage(GlobalValue::ExternalLinkage);
691 }
692 }
693
Teresa Johnson21241572016-07-18 21:22:24 +0000694 // Next we need to promote to global scope and rename any local values that
695 // are potentially exported to other modules.
696 if (renameModuleForThinLTO(M, *Index, nullptr)) {
697 errs() << "Error renaming module\n";
698 return false;
699 }
700
701 // Perform the import now.
702 auto ModuleLoader = [&M](StringRef Identifier) {
703 return loadFile(Identifier, M.getContext());
704 };
705 FunctionImporter Importer(*Index, ModuleLoader);
Peter Collingbourne7f00d0a2016-11-09 17:49:19 +0000706 Expected<bool> Result = Importer.importFunctions(
707 M, ImportList, !DontForceImportReferencedDiscardableSymbols);
708
709 // FIXME: Probably need to propagate Errors through the pass manager.
710 if (!Result) {
711 logAllUnhandledErrors(Result.takeError(), errs(),
712 "Error importing module: ");
713 return false;
714 }
715
716 return *Result;
Teresa Johnson21241572016-07-18 21:22:24 +0000717}
718
Benjamin Kramerfe2b5412015-12-24 10:03:35 +0000719namespace {
Mehdi Amini42418ab2015-11-24 06:07:49 +0000720/// Pass that performs cross-module function import provided a summary file.
Teresa Johnson21241572016-07-18 21:22:24 +0000721class FunctionImportLegacyPass : public ModulePass {
Mehdi Amini42418ab2015-11-24 06:07:49 +0000722public:
723 /// Pass identification, replacement for typeid
724 static char ID;
725
Teresa Johnson5fcbdb72015-12-07 19:21:11 +0000726 /// Specify pass name for debug output
Mehdi Amini117296c2016-10-01 02:56:57 +0000727 StringRef getPassName() const override { return "Function Importing"; }
Teresa Johnson5fcbdb72015-12-07 19:21:11 +0000728
Peter Collingbourne598bd2a2016-12-21 00:50:12 +0000729 explicit FunctionImportLegacyPass() : ModulePass(ID) {}
Mehdi Amini42418ab2015-11-24 06:07:49 +0000730
731 bool runOnModule(Module &M) override {
Andrew Kayloraa641a52016-04-22 22:06:11 +0000732 if (skipModule(M))
733 return false;
734
Peter Collingbourne598bd2a2016-12-21 00:50:12 +0000735 return doImportingForModule(M);
Mehdi Amini42418ab2015-11-24 06:07:49 +0000736 }
737};
Benjamin Kramerfe2b5412015-12-24 10:03:35 +0000738} // anonymous namespace
Mehdi Amini42418ab2015-11-24 06:07:49 +0000739
Teresa Johnson21241572016-07-18 21:22:24 +0000740PreservedAnalyses FunctionImportPass::run(Module &M,
Sean Silvafd03ac62016-08-09 00:28:38 +0000741 ModuleAnalysisManager &AM) {
Peter Collingbourne598bd2a2016-12-21 00:50:12 +0000742 if (!doImportingForModule(M))
Teresa Johnson21241572016-07-18 21:22:24 +0000743 return PreservedAnalyses::all();
744
745 return PreservedAnalyses::none();
746}
747
748char FunctionImportLegacyPass::ID = 0;
749INITIALIZE_PASS(FunctionImportLegacyPass, "function-import",
750 "Summary Based Function Import", false, false)
Mehdi Amini42418ab2015-11-24 06:07:49 +0000751
752namespace llvm {
Peter Collingbourne598bd2a2016-12-21 00:50:12 +0000753Pass *createFunctionImportPass() {
754 return new FunctionImportLegacyPass();
Teresa Johnson5fcbdb72015-12-07 19:21:11 +0000755}
Mehdi Amini42418ab2015-11-24 06:07:49 +0000756}