Mehdi Amini | 42418ab | 2015-11-24 06:07:49 +0000 | [diff] [blame] | 1 | //===- 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 Amini | 01e3213 | 2016-03-26 05:40:34 +0000 | [diff] [blame] | 16 | #include "llvm/ADT/SmallVector.h" |
Teresa Johnson | d29478f | 2016-03-27 15:27:30 +0000 | [diff] [blame] | 17 | #include "llvm/ADT/Statistic.h" |
Mehdi Amini | 42418ab | 2015-11-24 06:07:49 +0000 | [diff] [blame] | 18 | #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 Johnson | 26ab577 | 2016-03-15 00:04:37 +0000 | [diff] [blame] | 25 | #include "llvm/Object/ModuleSummaryIndexObjectFile.h" |
Mehdi Amini | 42418ab | 2015-11-24 06:07:49 +0000 | [diff] [blame] | 26 | #include "llvm/Support/CommandLine.h" |
| 27 | #include "llvm/Support/Debug.h" |
| 28 | #include "llvm/Support/SourceMgr.h" |
Teresa Johnson | 488a800 | 2016-02-10 18:11:31 +0000 | [diff] [blame] | 29 | #include "llvm/Transforms/Utils/FunctionImportUtils.h" |
Mehdi Amini | 7e88d0d | 2015-12-09 08:17:35 +0000 | [diff] [blame] | 30 | |
Mehdi Amini | 01e3213 | 2016-03-26 05:40:34 +0000 | [diff] [blame] | 31 | #define DEBUG_TYPE "function-import" |
Mehdi Amini | 7e88d0d | 2015-12-09 08:17:35 +0000 | [diff] [blame] | 32 | |
Mehdi Amini | 42418ab | 2015-11-24 06:07:49 +0000 | [diff] [blame] | 33 | using namespace llvm; |
| 34 | |
Teresa Johnson | d29478f | 2016-03-27 15:27:30 +0000 | [diff] [blame] | 35 | STATISTIC(NumImported, "Number of functions imported"); |
| 36 | |
Teresa Johnson | 3930361 | 2015-11-24 22:55:46 +0000 | [diff] [blame] | 37 | /// Limit on instruction count of imported functions. |
| 38 | static 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 Amini | 4064174 | 2016-02-10 23:31:45 +0000 | [diff] [blame] | 42 | static 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 Johnson | d29478f | 2016-03-27 15:27:30 +0000 | [diff] [blame] | 49 | static cl::opt<bool> PrintImports("print-imports", cl::init(false), cl::Hidden, |
| 50 | cl::desc("Print imported functions")); |
| 51 | |
Mehdi Amini | bda3c97 | 2016-04-21 01:59:39 +0000 | [diff] [blame] | 52 | // Temporary allows the function import pass to disable always linking |
| 53 | // referenced discardable symbols. |
| 54 | static cl::opt<bool> |
| 55 | DontForceImportReferencedDiscardableSymbols("disable-force-link-odr", |
| 56 | cl::init(false), cl::Hidden); |
| 57 | |
Mehdi Amini | 42418ab | 2015-11-24 06:07:49 +0000 | [diff] [blame] | 58 | // Load lazily a module from \p FileName in \p Context. |
| 59 | static std::unique_ptr<Module> loadFile(const std::string &FileName, |
| 60 | LLVMContext &Context) { |
| 61 | SMDiagnostic Err; |
| 62 | DEBUG(dbgs() << "Loading '" << FileName << "'\n"); |
Teresa Johnson | 6cba37c | 2016-01-22 00:15:53 +0000 | [diff] [blame] | 63 | // Metadata isn't loaded until functions are imported, to minimize |
| 64 | // the memory overhead. |
Teresa Johnson | a1080ee | 2016-01-08 14:17:41 +0000 | [diff] [blame] | 65 | std::unique_ptr<Module> Result = |
| 66 | getLazyIRFileModule(FileName, Err, Context, |
| 67 | /* ShouldLazyLoadMetadata = */ true); |
Mehdi Amini | 42418ab | 2015-11-24 06:07:49 +0000 | [diff] [blame] | 68 | if (!Result) { |
| 69 | Err.print("function-import", errs()); |
Mehdi Amini | d7ad221 | 2016-04-01 05:33:11 +0000 | [diff] [blame] | 70 | report_fatal_error("Abort"); |
Mehdi Amini | 42418ab | 2015-11-24 06:07:49 +0000 | [diff] [blame] | 71 | } |
| 72 | |
Mehdi Amini | 42418ab | 2015-11-24 06:07:49 +0000 | [diff] [blame] | 73 | return Result; |
| 74 | } |
| 75 | |
Mehdi Amini | 7e88d0d | 2015-12-09 08:17:35 +0000 | [diff] [blame] | 76 | namespace { |
Mehdi Amini | 4064174 | 2016-02-10 23:31:45 +0000 | [diff] [blame] | 77 | |
Mehdi Amini | 01e3213 | 2016-03-26 05:40:34 +0000 | [diff] [blame] | 78 | /// Given a list of possible callee implementation for a call site, select one |
| 79 | /// that fits the \p Threshold. |
| 80 | /// |
| 81 | /// FIXME: select "best" instead of first that fits. But what is "best"? |
| 82 | /// - The smallest: more likely to be inlined. |
| 83 | /// - The one with the least outgoing edges (already well optimized). |
| 84 | /// - One from a module already being imported from in order to reduce the |
| 85 | /// number of source modules parsed/linked. |
| 86 | /// - One that has PGO data attached. |
| 87 | /// - [insert you fancy metric here] |
Mehdi Amini | 2d28f7a | 2016-04-16 06:56:44 +0000 | [diff] [blame] | 88 | static const GlobalValueSummary * |
Teresa Johnson | 28e457b | 2016-04-24 14:57:11 +0000 | [diff] [blame] | 89 | selectCallee(const GlobalValueSummaryList &CalleeSummaryList, |
| 90 | unsigned Threshold) { |
Mehdi Amini | 01e3213 | 2016-03-26 05:40:34 +0000 | [diff] [blame] | 91 | auto It = llvm::find_if( |
Teresa Johnson | 28e457b | 2016-04-24 14:57:11 +0000 | [diff] [blame] | 92 | CalleeSummaryList, |
| 93 | [&](const std::unique_ptr<GlobalValueSummary> &SummaryPtr) { |
| 94 | auto *GVSummary = SummaryPtr.get(); |
Mehdi Amini | 2c719cc | 2016-04-20 04:17:36 +0000 | [diff] [blame] | 95 | if (GlobalValue::isWeakAnyLinkage(GVSummary->linkage())) |
| 96 | // There is no point in importing weak symbols, we can't inline them |
Mehdi Amini | 01e3213 | 2016-03-26 05:40:34 +0000 | [diff] [blame] | 97 | return false; |
Mehdi Amini | 2c719cc | 2016-04-20 04:17:36 +0000 | [diff] [blame] | 98 | if (auto *AS = dyn_cast<AliasSummary>(GVSummary)) { |
| 99 | GVSummary = &AS->getAliasee(); |
| 100 | // Alias can't point to "available_externally". However when we import |
| 101 | // linkOnceODR the linkage does not change. So we import the alias |
| 102 | // and aliasee only in this case. |
| 103 | // FIXME: we should import alias as available_externally *function*, |
| 104 | // the destination module does need to know it is an alias. |
| 105 | if (!GlobalValue::isLinkOnceODRLinkage(GVSummary->linkage())) |
| 106 | return false; |
| 107 | } |
| 108 | |
| 109 | auto *Summary = cast<FunctionSummary>(GVSummary); |
Mehdi Amini | 7e88d0d | 2015-12-09 08:17:35 +0000 | [diff] [blame] | 110 | |
Mehdi Amini | 01e3213 | 2016-03-26 05:40:34 +0000 | [diff] [blame] | 111 | if (Summary->instCount() > Threshold) |
| 112 | return false; |
Mehdi Amini | 7e88d0d | 2015-12-09 08:17:35 +0000 | [diff] [blame] | 113 | |
Mehdi Amini | 01e3213 | 2016-03-26 05:40:34 +0000 | [diff] [blame] | 114 | return true; |
| 115 | }); |
Teresa Johnson | 28e457b | 2016-04-24 14:57:11 +0000 | [diff] [blame] | 116 | if (It == CalleeSummaryList.end()) |
Mehdi Amini | 01e3213 | 2016-03-26 05:40:34 +0000 | [diff] [blame] | 117 | return nullptr; |
Mehdi Amini | 7e88d0d | 2015-12-09 08:17:35 +0000 | [diff] [blame] | 118 | |
Teresa Johnson | 28e457b | 2016-04-24 14:57:11 +0000 | [diff] [blame] | 119 | return cast<GlobalValueSummary>(It->get()); |
Mehdi Amini | 7e88d0d | 2015-12-09 08:17:35 +0000 | [diff] [blame] | 120 | } |
Mehdi Amini | 7e88d0d | 2015-12-09 08:17:35 +0000 | [diff] [blame] | 121 | |
Mehdi Amini | 01e3213 | 2016-03-26 05:40:34 +0000 | [diff] [blame] | 122 | /// Return the summary for the function \p GUID that fits the \p Threshold, or |
| 123 | /// null if there's no match. |
Mehdi Amini | 2d28f7a | 2016-04-16 06:56:44 +0000 | [diff] [blame] | 124 | static const GlobalValueSummary *selectCallee(GlobalValue::GUID GUID, |
| 125 | unsigned Threshold, |
| 126 | const ModuleSummaryIndex &Index) { |
Teresa Johnson | 28e457b | 2016-04-24 14:57:11 +0000 | [diff] [blame] | 127 | auto CalleeSummaryList = Index.findGlobalValueSummaryList(GUID); |
| 128 | if (CalleeSummaryList == Index.end()) { |
Mehdi Amini | 01e3213 | 2016-03-26 05:40:34 +0000 | [diff] [blame] | 129 | return nullptr; // This function does not have a summary |
| 130 | } |
Teresa Johnson | 28e457b | 2016-04-24 14:57:11 +0000 | [diff] [blame] | 131 | return selectCallee(CalleeSummaryList->second, Threshold); |
Mehdi Amini | 01e3213 | 2016-03-26 05:40:34 +0000 | [diff] [blame] | 132 | } |
Mehdi Amini | 7e88d0d | 2015-12-09 08:17:35 +0000 | [diff] [blame] | 133 | |
Mehdi Amini | cb87494 | 2016-04-23 23:29:24 +0000 | [diff] [blame] | 134 | /// Mark the global \p GUID as export by module \p ExportModulePath if found in |
| 135 | /// this module. If it is a GlobalVariable, we also mark any referenced global |
| 136 | /// in the current module as exported. |
| 137 | static void exportGlobalInModule(const ModuleSummaryIndex &Index, |
| 138 | StringRef ExportModulePath, |
| 139 | GlobalValue::GUID GUID, |
| 140 | FunctionImporter::ExportSetTy &ExportList) { |
Teresa Johnson | 28e457b | 2016-04-24 14:57:11 +0000 | [diff] [blame] | 141 | auto FindGlobalSummaryInModule = |
| 142 | [&](GlobalValue::GUID GUID) -> GlobalValueSummary *{ |
| 143 | auto SummaryList = Index.findGlobalValueSummaryList(GUID); |
| 144 | if (SummaryList == Index.end()) |
Mehdi Amini | cb87494 | 2016-04-23 23:29:24 +0000 | [diff] [blame] | 145 | // This global does not have a summary, it is not part of the ThinLTO |
| 146 | // process |
| 147 | return nullptr; |
Teresa Johnson | 28e457b | 2016-04-24 14:57:11 +0000 | [diff] [blame] | 148 | auto SummaryIter = llvm::find_if( |
| 149 | SummaryList->second, |
| 150 | [&](const std::unique_ptr<GlobalValueSummary> &Summary) { |
Mehdi Amini | cb87494 | 2016-04-23 23:29:24 +0000 | [diff] [blame] | 151 | return Summary->modulePath() == ExportModulePath; |
| 152 | }); |
Teresa Johnson | 28e457b | 2016-04-24 14:57:11 +0000 | [diff] [blame] | 153 | if (SummaryIter == SummaryList->second.end()) |
Mehdi Amini | cb87494 | 2016-04-23 23:29:24 +0000 | [diff] [blame] | 154 | return nullptr; |
Teresa Johnson | 28e457b | 2016-04-24 14:57:11 +0000 | [diff] [blame] | 155 | return SummaryIter->get(); |
Mehdi Amini | cb87494 | 2016-04-23 23:29:24 +0000 | [diff] [blame] | 156 | }; |
| 157 | |
Teresa Johnson | 28e457b | 2016-04-24 14:57:11 +0000 | [diff] [blame] | 158 | auto *Summary = FindGlobalSummaryInModule(GUID); |
| 159 | if (!Summary) |
Mehdi Amini | cb87494 | 2016-04-23 23:29:24 +0000 | [diff] [blame] | 160 | return; |
| 161 | // We found it in the current module, mark as exported |
| 162 | ExportList.insert(GUID); |
| 163 | |
Mehdi Amini | cb87494 | 2016-04-23 23:29:24 +0000 | [diff] [blame] | 164 | auto GVS = dyn_cast<GlobalVarSummary>(Summary); |
| 165 | if (!GVS) |
| 166 | return; |
| 167 | // FunctionImportGlobalProcessing::doPromoteLocalToGlobal() will always |
| 168 | // trigger importing the initializer for `constant unnamed addr` globals that |
| 169 | // are referenced. We conservatively export all the referenced symbols for |
| 170 | // every global to workaround this, so that the ExportList is accurate. |
| 171 | // FIXME: with a "isConstant" flag in the summary we could be more targetted. |
| 172 | for (auto &Ref : GVS->refs()) { |
| 173 | auto GUID = Ref.getGUID(); |
Teresa Johnson | 28e457b | 2016-04-24 14:57:11 +0000 | [diff] [blame] | 174 | auto *RefSummary = FindGlobalSummaryInModule(GUID); |
| 175 | if (RefSummary) |
Mehdi Amini | cb87494 | 2016-04-23 23:29:24 +0000 | [diff] [blame] | 176 | // Found a ref in the current module, mark it as exported |
| 177 | ExportList.insert(GUID); |
| 178 | } |
Mehdi Amini | 01e3213 | 2016-03-26 05:40:34 +0000 | [diff] [blame] | 179 | } |
Mehdi Amini | 4064174 | 2016-02-10 23:31:45 +0000 | [diff] [blame] | 180 | |
Mehdi Amini | 01e3213 | 2016-03-26 05:40:34 +0000 | [diff] [blame] | 181 | using EdgeInfo = std::pair<const FunctionSummary *, unsigned /* Threshold */>; |
Mehdi Amini | 7e88d0d | 2015-12-09 08:17:35 +0000 | [diff] [blame] | 182 | |
Mehdi Amini | 01e3213 | 2016-03-26 05:40:34 +0000 | [diff] [blame] | 183 | /// Compute the list of functions to import for a given caller. Mark these |
| 184 | /// imported functions and the symbols they reference in their source module as |
| 185 | /// exported from their source module. |
| 186 | static void computeImportForFunction( |
Teresa Johnson | 3255eec | 2016-04-10 15:17:26 +0000 | [diff] [blame] | 187 | const FunctionSummary &Summary, const ModuleSummaryIndex &Index, |
Teresa Johnson | c851d21 | 2016-04-25 21:09:51 +0000 | [diff] [blame^] | 188 | unsigned Threshold, const GVSummaryMapTy &DefinedGVSummaries, |
Mehdi Amini | 01e3213 | 2016-03-26 05:40:34 +0000 | [diff] [blame] | 189 | SmallVectorImpl<EdgeInfo> &Worklist, |
| 190 | FunctionImporter::ImportMapTy &ImportsForModule, |
Teresa Johnson | c86af33 | 2016-04-12 21:13:11 +0000 | [diff] [blame] | 191 | StringMap<FunctionImporter::ExportSetTy> *ExportLists = nullptr) { |
Mehdi Amini | 01e3213 | 2016-03-26 05:40:34 +0000 | [diff] [blame] | 192 | for (auto &Edge : Summary.calls()) { |
Teresa Johnson | 2d5487c | 2016-04-11 13:58:45 +0000 | [diff] [blame] | 193 | auto GUID = Edge.first.getGUID(); |
Mehdi Amini | 01e3213 | 2016-03-26 05:40:34 +0000 | [diff] [blame] | 194 | DEBUG(dbgs() << " edge -> " << GUID << " Threshold:" << Threshold << "\n"); |
| 195 | |
Mehdi Amini | 1aafabf | 2016-04-16 07:02:16 +0000 | [diff] [blame] | 196 | if (DefinedGVSummaries.count(GUID)) { |
Mehdi Amini | 01e3213 | 2016-03-26 05:40:34 +0000 | [diff] [blame] | 197 | DEBUG(dbgs() << "ignored! Target already in destination module.\n"); |
| 198 | continue; |
Teresa Johnson | d450da3 | 2015-11-24 21:15:19 +0000 | [diff] [blame] | 199 | } |
Mehdi Amini | 01e3213 | 2016-03-26 05:40:34 +0000 | [diff] [blame] | 200 | |
| 201 | auto *CalleeSummary = selectCallee(GUID, Threshold, Index); |
| 202 | if (!CalleeSummary) { |
| 203 | DEBUG(dbgs() << "ignored! No qualifying callee with summary found.\n"); |
| 204 | continue; |
| 205 | } |
Mehdi Amini | 2d28f7a | 2016-04-16 06:56:44 +0000 | [diff] [blame] | 206 | // "Resolve" the summary, traversing alias, |
| 207 | const FunctionSummary *ResolvedCalleeSummary; |
Mehdi Amini | 6968ef7 | 2016-04-20 01:04:20 +0000 | [diff] [blame] | 208 | if (isa<AliasSummary>(CalleeSummary)) { |
Mehdi Amini | 2d28f7a | 2016-04-16 06:56:44 +0000 | [diff] [blame] | 209 | ResolvedCalleeSummary = cast<FunctionSummary>( |
| 210 | &cast<AliasSummary>(CalleeSummary)->getAliasee()); |
Mehdi Amini | 2c719cc | 2016-04-20 04:17:36 +0000 | [diff] [blame] | 211 | assert( |
| 212 | GlobalValue::isLinkOnceODRLinkage(ResolvedCalleeSummary->linkage()) && |
| 213 | "Unexpected alias to a non-linkonceODR in import list"); |
Mehdi Amini | 6968ef7 | 2016-04-20 01:04:20 +0000 | [diff] [blame] | 214 | } else |
Mehdi Amini | 2d28f7a | 2016-04-16 06:56:44 +0000 | [diff] [blame] | 215 | ResolvedCalleeSummary = cast<FunctionSummary>(CalleeSummary); |
| 216 | |
| 217 | assert(ResolvedCalleeSummary->instCount() <= Threshold && |
Mehdi Amini | 01e3213 | 2016-03-26 05:40:34 +0000 | [diff] [blame] | 218 | "selectCallee() didn't honor the threshold"); |
| 219 | |
Mehdi Amini | 2d28f7a | 2016-04-16 06:56:44 +0000 | [diff] [blame] | 220 | auto ExportModulePath = ResolvedCalleeSummary->modulePath(); |
| 221 | auto &ProcessedThreshold = ImportsForModule[ExportModulePath][GUID]; |
Mehdi Amini | 01e3213 | 2016-03-26 05:40:34 +0000 | [diff] [blame] | 222 | /// Since the traversal of the call graph is DFS, we can revisit a function |
| 223 | /// a second time with a higher threshold. In this case, it is added back to |
| 224 | /// the worklist with the new threshold. |
| 225 | if (ProcessedThreshold && ProcessedThreshold > Threshold) { |
| 226 | DEBUG(dbgs() << "ignored! Target was already seen with Threshold " |
| 227 | << ProcessedThreshold << "\n"); |
| 228 | continue; |
| 229 | } |
| 230 | // Mark this function as imported in this module, with the current Threshold |
| 231 | ProcessedThreshold = Threshold; |
| 232 | |
| 233 | // Make exports in the source module. |
Teresa Johnson | c86af33 | 2016-04-12 21:13:11 +0000 | [diff] [blame] | 234 | if (ExportLists) { |
Mehdi Amini | ef7555f | 2016-04-13 01:52:32 +0000 | [diff] [blame] | 235 | auto &ExportList = (*ExportLists)[ExportModulePath]; |
Teresa Johnson | c86af33 | 2016-04-12 21:13:11 +0000 | [diff] [blame] | 236 | ExportList.insert(GUID); |
| 237 | // Mark all functions and globals referenced by this function as exported |
| 238 | // to the outside if they are defined in the same source module. |
Mehdi Amini | 2d28f7a | 2016-04-16 06:56:44 +0000 | [diff] [blame] | 239 | for (auto &Edge : ResolvedCalleeSummary->calls()) { |
Teresa Johnson | c86af33 | 2016-04-12 21:13:11 +0000 | [diff] [blame] | 240 | auto CalleeGUID = Edge.first.getGUID(); |
Mehdi Amini | cb87494 | 2016-04-23 23:29:24 +0000 | [diff] [blame] | 241 | exportGlobalInModule(Index, ExportModulePath, CalleeGUID, ExportList); |
Teresa Johnson | c86af33 | 2016-04-12 21:13:11 +0000 | [diff] [blame] | 242 | } |
Mehdi Amini | 2d28f7a | 2016-04-16 06:56:44 +0000 | [diff] [blame] | 243 | for (auto &Ref : ResolvedCalleeSummary->refs()) { |
Teresa Johnson | c86af33 | 2016-04-12 21:13:11 +0000 | [diff] [blame] | 244 | auto GUID = Ref.getGUID(); |
Mehdi Amini | cb87494 | 2016-04-23 23:29:24 +0000 | [diff] [blame] | 245 | exportGlobalInModule(Index, ExportModulePath, GUID, ExportList); |
Teresa Johnson | c86af33 | 2016-04-12 21:13:11 +0000 | [diff] [blame] | 246 | } |
Mehdi Amini | 01e3213 | 2016-03-26 05:40:34 +0000 | [diff] [blame] | 247 | } |
| 248 | |
| 249 | // Insert the newly imported function to the worklist. |
Mehdi Amini | 2d28f7a | 2016-04-16 06:56:44 +0000 | [diff] [blame] | 250 | Worklist.push_back(std::make_pair(ResolvedCalleeSummary, Threshold)); |
Teresa Johnson | d450da3 | 2015-11-24 21:15:19 +0000 | [diff] [blame] | 251 | } |
| 252 | } |
| 253 | |
Mehdi Amini | 01e3213 | 2016-03-26 05:40:34 +0000 | [diff] [blame] | 254 | /// Given the list of globals defined in a module, compute the list of imports |
| 255 | /// as well as the list of "exports", i.e. the list of symbols referenced from |
| 256 | /// another module (that may require promotion). |
| 257 | static void ComputeImportForModule( |
Teresa Johnson | c851d21 | 2016-04-25 21:09:51 +0000 | [diff] [blame^] | 258 | const GVSummaryMapTy &DefinedGVSummaries, const ModuleSummaryIndex &Index, |
Mehdi Amini | 01e3213 | 2016-03-26 05:40:34 +0000 | [diff] [blame] | 259 | FunctionImporter::ImportMapTy &ImportsForModule, |
Teresa Johnson | c86af33 | 2016-04-12 21:13:11 +0000 | [diff] [blame] | 260 | StringMap<FunctionImporter::ExportSetTy> *ExportLists = nullptr) { |
Mehdi Amini | 01e3213 | 2016-03-26 05:40:34 +0000 | [diff] [blame] | 261 | // Worklist contains the list of function imported in this module, for which |
| 262 | // we will analyse the callees and may import further down the callgraph. |
| 263 | SmallVector<EdgeInfo, 128> Worklist; |
| 264 | |
| 265 | // Populate the worklist with the import for the functions in the current |
| 266 | // module |
Teresa Johnson | 28e457b | 2016-04-24 14:57:11 +0000 | [diff] [blame] | 267 | for (auto &GVSummary : DefinedGVSummaries) { |
| 268 | auto *Summary = GVSummary.second; |
Mehdi Amini | 2d28f7a | 2016-04-16 06:56:44 +0000 | [diff] [blame] | 269 | if (auto *AS = dyn_cast<AliasSummary>(Summary)) |
| 270 | Summary = &AS->getAliasee(); |
Mehdi Amini | 1aafabf | 2016-04-16 07:02:16 +0000 | [diff] [blame] | 271 | auto *FuncSummary = dyn_cast<FunctionSummary>(Summary); |
| 272 | if (!FuncSummary) |
| 273 | // Skip import for global variables |
| 274 | continue; |
Teresa Johnson | 28e457b | 2016-04-24 14:57:11 +0000 | [diff] [blame] | 275 | DEBUG(dbgs() << "Initalize import for " << GVSummary.first << "\n"); |
Mehdi Amini | 2d28f7a | 2016-04-16 06:56:44 +0000 | [diff] [blame] | 276 | computeImportForFunction(*FuncSummary, Index, ImportInstrLimit, |
Mehdi Amini | 1aafabf | 2016-04-16 07:02:16 +0000 | [diff] [blame] | 277 | DefinedGVSummaries, Worklist, ImportsForModule, |
Mehdi Amini | 01e3213 | 2016-03-26 05:40:34 +0000 | [diff] [blame] | 278 | ExportLists); |
| 279 | } |
| 280 | |
Mehdi Amini | 42418ab | 2015-11-24 06:07:49 +0000 | [diff] [blame] | 281 | while (!Worklist.empty()) { |
Mehdi Amini | 01e3213 | 2016-03-26 05:40:34 +0000 | [diff] [blame] | 282 | auto FuncInfo = Worklist.pop_back_val(); |
| 283 | auto *Summary = FuncInfo.first; |
| 284 | auto Threshold = FuncInfo.second; |
Mehdi Amini | 42418ab | 2015-11-24 06:07:49 +0000 | [diff] [blame] | 285 | |
Mehdi Amini | 7e88d0d | 2015-12-09 08:17:35 +0000 | [diff] [blame] | 286 | // Process the newly imported functions and add callees to the worklist. |
Mehdi Amini | 4064174 | 2016-02-10 23:31:45 +0000 | [diff] [blame] | 287 | // Adjust the threshold |
| 288 | Threshold = Threshold * ImportInstrFactor; |
Mehdi Amini | 01e3213 | 2016-03-26 05:40:34 +0000 | [diff] [blame] | 289 | |
Mehdi Amini | 1aafabf | 2016-04-16 07:02:16 +0000 | [diff] [blame] | 290 | computeImportForFunction(*Summary, Index, Threshold, DefinedGVSummaries, |
Teresa Johnson | 3255eec | 2016-04-10 15:17:26 +0000 | [diff] [blame] | 291 | Worklist, ImportsForModule, ExportLists); |
Mehdi Amini | 42418ab | 2015-11-24 06:07:49 +0000 | [diff] [blame] | 292 | } |
Mehdi Amini | c8c5517 | 2015-12-03 02:37:33 +0000 | [diff] [blame] | 293 | } |
Mehdi Amini | ffe2e4a | 2015-12-02 04:34:28 +0000 | [diff] [blame] | 294 | |
Mehdi Amini | 01e3213 | 2016-03-26 05:40:34 +0000 | [diff] [blame] | 295 | } // anonymous namespace |
| 296 | |
Teresa Johnson | c86af33 | 2016-04-12 21:13:11 +0000 | [diff] [blame] | 297 | /// Compute all the import and export for every module using the Index. |
Mehdi Amini | 01e3213 | 2016-03-26 05:40:34 +0000 | [diff] [blame] | 298 | void llvm::ComputeCrossModuleImport( |
| 299 | const ModuleSummaryIndex &Index, |
Teresa Johnson | c851d21 | 2016-04-25 21:09:51 +0000 | [diff] [blame^] | 300 | const StringMap<GVSummaryMapTy> &ModuleToDefinedGVSummaries, |
Mehdi Amini | 01e3213 | 2016-03-26 05:40:34 +0000 | [diff] [blame] | 301 | StringMap<FunctionImporter::ImportMapTy> &ImportLists, |
| 302 | StringMap<FunctionImporter::ExportSetTy> &ExportLists) { |
Mehdi Amini | 01e3213 | 2016-03-26 05:40:34 +0000 | [diff] [blame] | 303 | // For each module that has function defined, compute the import/export lists. |
Mehdi Amini | 1aafabf | 2016-04-16 07:02:16 +0000 | [diff] [blame] | 304 | for (auto &DefinedGVSummaries : ModuleToDefinedGVSummaries) { |
| 305 | auto &ImportsForModule = ImportLists[DefinedGVSummaries.first()]; |
| 306 | DEBUG(dbgs() << "Computing import for Module '" |
| 307 | << DefinedGVSummaries.first() << "'\n"); |
| 308 | ComputeImportForModule(DefinedGVSummaries.second, Index, ImportsForModule, |
Teresa Johnson | c86af33 | 2016-04-12 21:13:11 +0000 | [diff] [blame] | 309 | &ExportLists); |
Mehdi Amini | 01e3213 | 2016-03-26 05:40:34 +0000 | [diff] [blame] | 310 | } |
| 311 | |
| 312 | #ifndef NDEBUG |
| 313 | DEBUG(dbgs() << "Import/Export lists for " << ImportLists.size() |
| 314 | << " modules:\n"); |
| 315 | for (auto &ModuleImports : ImportLists) { |
| 316 | auto ModName = ModuleImports.first(); |
| 317 | auto &Exports = ExportLists[ModName]; |
| 318 | DEBUG(dbgs() << "* Module " << ModName << " exports " << Exports.size() |
| 319 | << " functions. Imports from " << ModuleImports.second.size() |
| 320 | << " modules.\n"); |
| 321 | for (auto &Src : ModuleImports.second) { |
| 322 | auto SrcModName = Src.first(); |
| 323 | DEBUG(dbgs() << " - " << Src.second.size() << " functions imported from " |
| 324 | << SrcModName << "\n"); |
| 325 | } |
| 326 | } |
| 327 | #endif |
| 328 | } |
| 329 | |
Teresa Johnson | c86af33 | 2016-04-12 21:13:11 +0000 | [diff] [blame] | 330 | /// Compute all the imports for the given module in the Index. |
| 331 | void llvm::ComputeCrossModuleImportForModule( |
| 332 | StringRef ModulePath, const ModuleSummaryIndex &Index, |
| 333 | FunctionImporter::ImportMapTy &ImportList) { |
| 334 | |
| 335 | // Collect the list of functions this module defines. |
| 336 | // GUID -> Summary |
Teresa Johnson | c851d21 | 2016-04-25 21:09:51 +0000 | [diff] [blame^] | 337 | GVSummaryMapTy FunctionSummaryMap; |
Teresa Johnson | 28e457b | 2016-04-24 14:57:11 +0000 | [diff] [blame] | 338 | Index.collectDefinedFunctionsForModule(ModulePath, FunctionSummaryMap); |
Teresa Johnson | c86af33 | 2016-04-12 21:13:11 +0000 | [diff] [blame] | 339 | |
| 340 | // Compute the import list for this module. |
| 341 | DEBUG(dbgs() << "Computing import for Module '" << ModulePath << "'\n"); |
Teresa Johnson | 28e457b | 2016-04-24 14:57:11 +0000 | [diff] [blame] | 342 | ComputeImportForModule(FunctionSummaryMap, Index, ImportList); |
Teresa Johnson | c86af33 | 2016-04-12 21:13:11 +0000 | [diff] [blame] | 343 | |
| 344 | #ifndef NDEBUG |
| 345 | DEBUG(dbgs() << "* Module " << ModulePath << " imports from " |
| 346 | << ImportList.size() << " modules.\n"); |
| 347 | for (auto &Src : ImportList) { |
| 348 | auto SrcModName = Src.first(); |
| 349 | DEBUG(dbgs() << " - " << Src.second.size() << " functions imported from " |
| 350 | << SrcModName << "\n"); |
| 351 | } |
| 352 | #endif |
| 353 | } |
| 354 | |
Mehdi Amini | c8c5517 | 2015-12-03 02:37:33 +0000 | [diff] [blame] | 355 | // Automatically import functions in Module \p DestModule based on the summaries |
| 356 | // index. |
| 357 | // |
Mehdi Amini | 01e3213 | 2016-03-26 05:40:34 +0000 | [diff] [blame] | 358 | bool FunctionImporter::importFunctions( |
Mehdi Amini | bda3c97 | 2016-04-21 01:59:39 +0000 | [diff] [blame] | 359 | Module &DestModule, const FunctionImporter::ImportMapTy &ImportList, |
| 360 | bool ForceImportReferencedDiscardableSymbols) { |
Mehdi Amini | 5411d05 | 2015-12-08 23:04:19 +0000 | [diff] [blame] | 361 | DEBUG(dbgs() << "Starting import for Module " |
Mehdi Amini | 311fef6 | 2015-12-03 02:58:14 +0000 | [diff] [blame] | 362 | << DestModule.getModuleIdentifier() << "\n"); |
Mehdi Amini | c8c5517 | 2015-12-03 02:37:33 +0000 | [diff] [blame] | 363 | unsigned ImportedCount = 0; |
| 364 | |
Mehdi Amini | c8c5517 | 2015-12-03 02:37:33 +0000 | [diff] [blame] | 365 | // Linker that will be used for importing function |
Rafael Espindola | 9d2bfc4 | 2015-12-14 23:17:03 +0000 | [diff] [blame] | 366 | Linker TheLinker(DestModule); |
Mehdi Amini | 7e88d0d | 2015-12-09 08:17:35 +0000 | [diff] [blame] | 367 | // Do the actual import of functions now, one Module at a time |
Mehdi Amini | 01e3213 | 2016-03-26 05:40:34 +0000 | [diff] [blame] | 368 | std::set<StringRef> ModuleNameOrderedList; |
| 369 | for (auto &FunctionsToImportPerModule : ImportList) { |
| 370 | ModuleNameOrderedList.insert(FunctionsToImportPerModule.first()); |
| 371 | } |
| 372 | for (auto &Name : ModuleNameOrderedList) { |
Mehdi Amini | 7e88d0d | 2015-12-09 08:17:35 +0000 | [diff] [blame] | 373 | // Get the module for the import |
Mehdi Amini | 01e3213 | 2016-03-26 05:40:34 +0000 | [diff] [blame] | 374 | const auto &FunctionsToImportPerModule = ImportList.find(Name); |
| 375 | assert(FunctionsToImportPerModule != ImportList.end()); |
| 376 | std::unique_ptr<Module> SrcModule = ModuleLoader(Name); |
Mehdi Amini | 7e88d0d | 2015-12-09 08:17:35 +0000 | [diff] [blame] | 377 | assert(&DestModule.getContext() == &SrcModule->getContext() && |
| 378 | "Context mismatch"); |
| 379 | |
Teresa Johnson | 6cba37c | 2016-01-22 00:15:53 +0000 | [diff] [blame] | 380 | // If modules were created with lazy metadata loading, materialize it |
| 381 | // now, before linking it (otherwise this will be a noop). |
| 382 | SrcModule->materializeMetadata(); |
| 383 | UpgradeDebugInfo(*SrcModule); |
Teresa Johnson | e5a6191 | 2015-12-17 17:14:09 +0000 | [diff] [blame] | 384 | |
Mehdi Amini | 01e3213 | 2016-03-26 05:40:34 +0000 | [diff] [blame] | 385 | auto &ImportGUIDs = FunctionsToImportPerModule->second; |
| 386 | // Find the globals to import |
| 387 | DenseSet<const GlobalValue *> GlobalsToImport; |
| 388 | for (auto &GV : *SrcModule) { |
Teresa Johnson | 0beb858 | 2016-04-04 18:52:23 +0000 | [diff] [blame] | 389 | if (!GV.hasName()) |
| 390 | continue; |
| 391 | auto GUID = GV.getGUID(); |
| 392 | auto Import = ImportGUIDs.count(GUID); |
Mehdi Amini | aeb1e59 | 2016-04-19 09:21:30 +0000 | [diff] [blame] | 393 | DEBUG(dbgs() << (Import ? "Is" : "Not") << " importing function " << GUID |
| 394 | << " " << GV.getName() << " from " |
| 395 | << SrcModule->getSourceFileName() << "\n"); |
Teresa Johnson | 0beb858 | 2016-04-04 18:52:23 +0000 | [diff] [blame] | 396 | if (Import) { |
Mehdi Amini | 01e3213 | 2016-03-26 05:40:34 +0000 | [diff] [blame] | 397 | GV.materialize(); |
| 398 | GlobalsToImport.insert(&GV); |
| 399 | } |
| 400 | } |
Mehdi Amini | 2d28f7a | 2016-04-16 06:56:44 +0000 | [diff] [blame] | 401 | for (auto &GV : SrcModule->globals()) { |
| 402 | if (!GV.hasName()) |
| 403 | continue; |
| 404 | auto GUID = GV.getGUID(); |
| 405 | auto Import = ImportGUIDs.count(GUID); |
Mehdi Amini | aeb1e59 | 2016-04-19 09:21:30 +0000 | [diff] [blame] | 406 | DEBUG(dbgs() << (Import ? "Is" : "Not") << " importing global " << GUID |
| 407 | << " " << GV.getName() << " from " |
| 408 | << SrcModule->getSourceFileName() << "\n"); |
Mehdi Amini | 2d28f7a | 2016-04-16 06:56:44 +0000 | [diff] [blame] | 409 | if (Import) { |
| 410 | GV.materialize(); |
| 411 | GlobalsToImport.insert(&GV); |
| 412 | } |
| 413 | } |
Mehdi Amini | 01e3213 | 2016-03-26 05:40:34 +0000 | [diff] [blame] | 414 | for (auto &GV : SrcModule->aliases()) { |
| 415 | if (!GV.hasName()) |
| 416 | continue; |
| 417 | auto GUID = GV.getGUID(); |
Teresa Johnson | 0beb858 | 2016-04-04 18:52:23 +0000 | [diff] [blame] | 418 | auto Import = ImportGUIDs.count(GUID); |
Mehdi Amini | aeb1e59 | 2016-04-19 09:21:30 +0000 | [diff] [blame] | 419 | DEBUG(dbgs() << (Import ? "Is" : "Not") << " importing alias " << GUID |
| 420 | << " " << GV.getName() << " from " |
| 421 | << SrcModule->getSourceFileName() << "\n"); |
Teresa Johnson | 0beb858 | 2016-04-04 18:52:23 +0000 | [diff] [blame] | 422 | if (Import) { |
Mehdi Amini | 01e3213 | 2016-03-26 05:40:34 +0000 | [diff] [blame] | 423 | // Alias can't point to "available_externally". However when we import |
Teresa Johnson | 9aae395 | 2016-03-27 15:01:11 +0000 | [diff] [blame] | 424 | // linkOnceODR the linkage does not change. So we import the alias |
Mehdi Amini | 6968ef7 | 2016-04-20 01:04:20 +0000 | [diff] [blame] | 425 | // and aliasee only in this case. This has been handled by |
| 426 | // computeImportForFunction() |
Mehdi Amini | 2d28f7a | 2016-04-16 06:56:44 +0000 | [diff] [blame] | 427 | GlobalObject *GO = GV.getBaseObject(); |
Mehdi Amini | 6968ef7 | 2016-04-20 01:04:20 +0000 | [diff] [blame] | 428 | assert(GO->hasLinkOnceODRLinkage() && |
| 429 | "Unexpected alias to a non-linkonceODR in import list"); |
Mehdi Amini | 2d28f7a | 2016-04-16 06:56:44 +0000 | [diff] [blame] | 430 | #ifndef NDEBUG |
| 431 | if (!GlobalsToImport.count(GO)) |
| 432 | DEBUG(dbgs() << " alias triggers importing aliasee " << GO->getGUID() |
| 433 | << " " << GO->getName() << " from " |
| 434 | << SrcModule->getSourceFileName() << "\n"); |
| 435 | #endif |
| 436 | GO->materialize(); |
Mehdi Amini | 01e3213 | 2016-03-26 05:40:34 +0000 | [diff] [blame] | 437 | GlobalsToImport.insert(GO); |
Mehdi Amini | 01e3213 | 2016-03-26 05:40:34 +0000 | [diff] [blame] | 438 | GV.materialize(); |
| 439 | GlobalsToImport.insert(&GV); |
| 440 | } |
| 441 | } |
| 442 | |
Mehdi Amini | 7e88d0d | 2015-12-09 08:17:35 +0000 | [diff] [blame] | 443 | // Link in the specified functions. |
Mehdi Amini | 01e3213 | 2016-03-26 05:40:34 +0000 | [diff] [blame] | 444 | if (renameModuleForThinLTO(*SrcModule, Index, &GlobalsToImport)) |
Mehdi Amini | 8d05185 | 2016-03-19 00:40:31 +0000 | [diff] [blame] | 445 | return true; |
| 446 | |
Teresa Johnson | d29478f | 2016-03-27 15:27:30 +0000 | [diff] [blame] | 447 | if (PrintImports) { |
| 448 | for (const auto *GV : GlobalsToImport) |
| 449 | dbgs() << DestModule.getSourceFileName() << ": Import " << GV->getName() |
| 450 | << " from " << SrcModule->getSourceFileName() << "\n"; |
| 451 | } |
| 452 | |
Mehdi Amini | bda3c97 | 2016-04-21 01:59:39 +0000 | [diff] [blame] | 453 | // Instruct the linker that the client will take care of linkonce resolution |
| 454 | unsigned Flags = Linker::Flags::None; |
| 455 | if (!ForceImportReferencedDiscardableSymbols) |
| 456 | Flags |= Linker::Flags::DontForceLinkLinkonceODR; |
| 457 | |
| 458 | if (TheLinker.linkInModule(std::move(SrcModule), Flags, &GlobalsToImport)) |
Mehdi Amini | 7e88d0d | 2015-12-09 08:17:35 +0000 | [diff] [blame] | 459 | report_fatal_error("Function Import: link error"); |
| 460 | |
Mehdi Amini | 01e3213 | 2016-03-26 05:40:34 +0000 | [diff] [blame] | 461 | ImportedCount += GlobalsToImport.size(); |
Mehdi Amini | 7e88d0d | 2015-12-09 08:17:35 +0000 | [diff] [blame] | 462 | } |
Teresa Johnson | e5a6191 | 2015-12-17 17:14:09 +0000 | [diff] [blame] | 463 | |
Teresa Johnson | d29478f | 2016-03-27 15:27:30 +0000 | [diff] [blame] | 464 | NumImported += ImportedCount; |
| 465 | |
Mehdi Amini | 7e88d0d | 2015-12-09 08:17:35 +0000 | [diff] [blame] | 466 | DEBUG(dbgs() << "Imported " << ImportedCount << " functions for Module " |
Mehdi Amini | c8c5517 | 2015-12-03 02:37:33 +0000 | [diff] [blame] | 467 | << DestModule.getModuleIdentifier() << "\n"); |
| 468 | return ImportedCount; |
Mehdi Amini | 42418ab | 2015-11-24 06:07:49 +0000 | [diff] [blame] | 469 | } |
| 470 | |
| 471 | /// Summary file to use for function importing when using -function-import from |
| 472 | /// the command line. |
| 473 | static cl::opt<std::string> |
| 474 | SummaryFile("summary-file", |
| 475 | cl::desc("The summary file to use for function importing.")); |
| 476 | |
| 477 | static void diagnosticHandler(const DiagnosticInfo &DI) { |
| 478 | raw_ostream &OS = errs(); |
| 479 | DiagnosticPrinterRawOStream DP(OS); |
| 480 | DI.print(DP); |
| 481 | OS << '\n'; |
| 482 | } |
| 483 | |
Teresa Johnson | 26ab577 | 2016-03-15 00:04:37 +0000 | [diff] [blame] | 484 | /// Parse the summary index out of an IR file and return the summary |
Mehdi Amini | 42418ab | 2015-11-24 06:07:49 +0000 | [diff] [blame] | 485 | /// index object if found, or nullptr if not. |
Teresa Johnson | 26ab577 | 2016-03-15 00:04:37 +0000 | [diff] [blame] | 486 | static std::unique_ptr<ModuleSummaryIndex> |
| 487 | getModuleSummaryIndexForFile(StringRef Path, std::string &Error, |
| 488 | DiagnosticHandlerFunction DiagnosticHandler) { |
Mehdi Amini | 42418ab | 2015-11-24 06:07:49 +0000 | [diff] [blame] | 489 | std::unique_ptr<MemoryBuffer> Buffer; |
| 490 | ErrorOr<std::unique_ptr<MemoryBuffer>> BufferOrErr = |
| 491 | MemoryBuffer::getFile(Path); |
| 492 | if (std::error_code EC = BufferOrErr.getError()) { |
| 493 | Error = EC.message(); |
| 494 | return nullptr; |
| 495 | } |
| 496 | Buffer = std::move(BufferOrErr.get()); |
Teresa Johnson | 26ab577 | 2016-03-15 00:04:37 +0000 | [diff] [blame] | 497 | ErrorOr<std::unique_ptr<object::ModuleSummaryIndexObjectFile>> ObjOrErr = |
| 498 | object::ModuleSummaryIndexObjectFile::create(Buffer->getMemBufferRef(), |
| 499 | DiagnosticHandler); |
Mehdi Amini | 42418ab | 2015-11-24 06:07:49 +0000 | [diff] [blame] | 500 | if (std::error_code EC = ObjOrErr.getError()) { |
| 501 | Error = EC.message(); |
| 502 | return nullptr; |
| 503 | } |
| 504 | return (*ObjOrErr)->takeIndex(); |
| 505 | } |
| 506 | |
Benjamin Kramer | fe2b541 | 2015-12-24 10:03:35 +0000 | [diff] [blame] | 507 | namespace { |
Mehdi Amini | 42418ab | 2015-11-24 06:07:49 +0000 | [diff] [blame] | 508 | /// Pass that performs cross-module function import provided a summary file. |
| 509 | class FunctionImportPass : public ModulePass { |
Teresa Johnson | 26ab577 | 2016-03-15 00:04:37 +0000 | [diff] [blame] | 510 | /// Optional module summary index to use for importing, otherwise |
Teresa Johnson | 5fcbdb7 | 2015-12-07 19:21:11 +0000 | [diff] [blame] | 511 | /// the summary-file option must be specified. |
Teresa Johnson | 26ab577 | 2016-03-15 00:04:37 +0000 | [diff] [blame] | 512 | const ModuleSummaryIndex *Index; |
Mehdi Amini | 42418ab | 2015-11-24 06:07:49 +0000 | [diff] [blame] | 513 | |
| 514 | public: |
| 515 | /// Pass identification, replacement for typeid |
| 516 | static char ID; |
| 517 | |
Teresa Johnson | 5fcbdb7 | 2015-12-07 19:21:11 +0000 | [diff] [blame] | 518 | /// Specify pass name for debug output |
Mehdi Amini | 2d28f7a | 2016-04-16 06:56:44 +0000 | [diff] [blame] | 519 | const char *getPassName() const override { return "Function Importing"; } |
Teresa Johnson | 5fcbdb7 | 2015-12-07 19:21:11 +0000 | [diff] [blame] | 520 | |
Teresa Johnson | 26ab577 | 2016-03-15 00:04:37 +0000 | [diff] [blame] | 521 | explicit FunctionImportPass(const ModuleSummaryIndex *Index = nullptr) |
Teresa Johnson | 5fcbdb7 | 2015-12-07 19:21:11 +0000 | [diff] [blame] | 522 | : ModulePass(ID), Index(Index) {} |
Mehdi Amini | 42418ab | 2015-11-24 06:07:49 +0000 | [diff] [blame] | 523 | |
| 524 | bool runOnModule(Module &M) override { |
Andrew Kaylor | aa641a5 | 2016-04-22 22:06:11 +0000 | [diff] [blame] | 525 | if (skipModule(M)) |
| 526 | return false; |
| 527 | |
Teresa Johnson | 5fcbdb7 | 2015-12-07 19:21:11 +0000 | [diff] [blame] | 528 | if (SummaryFile.empty() && !Index) |
| 529 | report_fatal_error("error: -function-import requires -summary-file or " |
| 530 | "file from frontend\n"); |
Teresa Johnson | 26ab577 | 2016-03-15 00:04:37 +0000 | [diff] [blame] | 531 | std::unique_ptr<ModuleSummaryIndex> IndexPtr; |
Teresa Johnson | 5fcbdb7 | 2015-12-07 19:21:11 +0000 | [diff] [blame] | 532 | if (!SummaryFile.empty()) { |
| 533 | if (Index) |
| 534 | report_fatal_error("error: -summary-file and index from frontend\n"); |
| 535 | std::string Error; |
Teresa Johnson | 26ab577 | 2016-03-15 00:04:37 +0000 | [diff] [blame] | 536 | IndexPtr = |
| 537 | getModuleSummaryIndexForFile(SummaryFile, Error, diagnosticHandler); |
Teresa Johnson | 5fcbdb7 | 2015-12-07 19:21:11 +0000 | [diff] [blame] | 538 | if (!IndexPtr) { |
| 539 | errs() << "Error loading file '" << SummaryFile << "': " << Error |
| 540 | << "\n"; |
| 541 | return false; |
| 542 | } |
| 543 | Index = IndexPtr.get(); |
Mehdi Amini | 42418ab | 2015-11-24 06:07:49 +0000 | [diff] [blame] | 544 | } |
| 545 | |
Teresa Johnson | c86af33 | 2016-04-12 21:13:11 +0000 | [diff] [blame] | 546 | // First step is collecting the import list. |
| 547 | FunctionImporter::ImportMapTy ImportList; |
| 548 | ComputeCrossModuleImportForModule(M.getModuleIdentifier(), *Index, |
| 549 | ImportList); |
Mehdi Amini | 01e3213 | 2016-03-26 05:40:34 +0000 | [diff] [blame] | 550 | |
| 551 | // Next we need to promote to global scope and rename any local values that |
Teresa Johnson | 1b00f2d | 2016-01-08 17:06:29 +0000 | [diff] [blame] | 552 | // are potentially exported to other modules. |
Mehdi Amini | 01e3213 | 2016-03-26 05:40:34 +0000 | [diff] [blame] | 553 | if (renameModuleForThinLTO(M, *Index, nullptr)) { |
Teresa Johnson | 1b00f2d | 2016-01-08 17:06:29 +0000 | [diff] [blame] | 554 | errs() << "Error renaming module\n"; |
| 555 | return false; |
| 556 | } |
| 557 | |
Mehdi Amini | 42418ab | 2015-11-24 06:07:49 +0000 | [diff] [blame] | 558 | // Perform the import now. |
Mehdi Amini | d16c806 | 2015-12-08 22:39:40 +0000 | [diff] [blame] | 559 | auto ModuleLoader = [&M](StringRef Identifier) { |
| 560 | return loadFile(Identifier, M.getContext()); |
| 561 | }; |
Rafael Espindola | 9d2bfc4 | 2015-12-14 23:17:03 +0000 | [diff] [blame] | 562 | FunctionImporter Importer(*Index, ModuleLoader); |
Mehdi Amini | bda3c97 | 2016-04-21 01:59:39 +0000 | [diff] [blame] | 563 | return Importer.importFunctions( |
| 564 | M, ImportList, !DontForceImportReferencedDiscardableSymbols); |
Mehdi Amini | 42418ab | 2015-11-24 06:07:49 +0000 | [diff] [blame] | 565 | } |
| 566 | }; |
Benjamin Kramer | fe2b541 | 2015-12-24 10:03:35 +0000 | [diff] [blame] | 567 | } // anonymous namespace |
Mehdi Amini | 42418ab | 2015-11-24 06:07:49 +0000 | [diff] [blame] | 568 | |
| 569 | char FunctionImportPass::ID = 0; |
| 570 | INITIALIZE_PASS_BEGIN(FunctionImportPass, "function-import", |
| 571 | "Summary Based Function Import", false, false) |
| 572 | INITIALIZE_PASS_END(FunctionImportPass, "function-import", |
| 573 | "Summary Based Function Import", false, false) |
| 574 | |
| 575 | namespace llvm { |
Teresa Johnson | 26ab577 | 2016-03-15 00:04:37 +0000 | [diff] [blame] | 576 | Pass *createFunctionImportPass(const ModuleSummaryIndex *Index = nullptr) { |
Teresa Johnson | 5fcbdb7 | 2015-12-07 19:21:11 +0000 | [diff] [blame] | 577 | return new FunctionImportPass(Index); |
| 578 | } |
Mehdi Amini | 42418ab | 2015-11-24 06:07:49 +0000 | [diff] [blame] | 579 | } |