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 | b4e1e82 | 2016-04-27 00:32:13 +0000 | [diff] [blame^] | 78 | // 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). |
| 81 | static 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). |
| 95 | static 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. |
| 113 | static 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 Amini | 01e3213 | 2016-03-26 05:40:34 +0000 | [diff] [blame] | 141 | /// 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 Amini | 2d28f7a | 2016-04-16 06:56:44 +0000 | [diff] [blame] | 151 | static const GlobalValueSummary * |
Mehdi Amini | b4e1e82 | 2016-04-27 00:32:13 +0000 | [diff] [blame^] | 152 | selectCallee(const ModuleSummaryIndex &Index, |
| 153 | const GlobalValueSummaryList &CalleeSummaryList, |
Teresa Johnson | 28e457b | 2016-04-24 14:57:11 +0000 | [diff] [blame] | 154 | unsigned Threshold) { |
Mehdi Amini | 01e3213 | 2016-03-26 05:40:34 +0000 | [diff] [blame] | 155 | auto It = llvm::find_if( |
Teresa Johnson | 28e457b | 2016-04-24 14:57:11 +0000 | [diff] [blame] | 156 | CalleeSummaryList, |
| 157 | [&](const std::unique_ptr<GlobalValueSummary> &SummaryPtr) { |
| 158 | auto *GVSummary = SummaryPtr.get(); |
Mehdi Amini | 2c719cc | 2016-04-20 04:17:36 +0000 | [diff] [blame] | 159 | if (GlobalValue::isWeakAnyLinkage(GVSummary->linkage())) |
| 160 | // 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] | 161 | return false; |
Mehdi Amini | 2c719cc | 2016-04-20 04:17:36 +0000 | [diff] [blame] | 162 | 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 Amini | 7e88d0d | 2015-12-09 08:17:35 +0000 | [diff] [blame] | 174 | |
Mehdi Amini | 01e3213 | 2016-03-26 05:40:34 +0000 | [diff] [blame] | 175 | if (Summary->instCount() > Threshold) |
| 176 | return false; |
Mehdi Amini | 7e88d0d | 2015-12-09 08:17:35 +0000 | [diff] [blame] | 177 | |
Mehdi Amini | b4e1e82 | 2016-04-27 00:32:13 +0000 | [diff] [blame^] | 178 | if (!eligibleForImport(Index, *Summary)) |
| 179 | return false; |
| 180 | |
Mehdi Amini | 01e3213 | 2016-03-26 05:40:34 +0000 | [diff] [blame] | 181 | return true; |
| 182 | }); |
Teresa Johnson | 28e457b | 2016-04-24 14:57:11 +0000 | [diff] [blame] | 183 | if (It == CalleeSummaryList.end()) |
Mehdi Amini | 01e3213 | 2016-03-26 05:40:34 +0000 | [diff] [blame] | 184 | return nullptr; |
Mehdi Amini | 7e88d0d | 2015-12-09 08:17:35 +0000 | [diff] [blame] | 185 | |
Teresa Johnson | 28e457b | 2016-04-24 14:57:11 +0000 | [diff] [blame] | 186 | return cast<GlobalValueSummary>(It->get()); |
Mehdi Amini | 7e88d0d | 2015-12-09 08:17:35 +0000 | [diff] [blame] | 187 | } |
Mehdi Amini | 7e88d0d | 2015-12-09 08:17:35 +0000 | [diff] [blame] | 188 | |
Mehdi Amini | 01e3213 | 2016-03-26 05:40:34 +0000 | [diff] [blame] | 189 | /// Return the summary for the function \p GUID that fits the \p Threshold, or |
| 190 | /// null if there's no match. |
Mehdi Amini | 2d28f7a | 2016-04-16 06:56:44 +0000 | [diff] [blame] | 191 | static const GlobalValueSummary *selectCallee(GlobalValue::GUID GUID, |
| 192 | unsigned Threshold, |
| 193 | const ModuleSummaryIndex &Index) { |
Teresa Johnson | 28e457b | 2016-04-24 14:57:11 +0000 | [diff] [blame] | 194 | auto CalleeSummaryList = Index.findGlobalValueSummaryList(GUID); |
Mehdi Amini | b4e1e82 | 2016-04-27 00:32:13 +0000 | [diff] [blame^] | 195 | if (CalleeSummaryList == Index.end()) |
Mehdi Amini | 01e3213 | 2016-03-26 05:40:34 +0000 | [diff] [blame] | 196 | return nullptr; // This function does not have a summary |
Mehdi Amini | b4e1e82 | 2016-04-27 00:32:13 +0000 | [diff] [blame^] | 197 | return selectCallee(Index, CalleeSummaryList->second, Threshold); |
Mehdi Amini | 01e3213 | 2016-03-26 05:40:34 +0000 | [diff] [blame] | 198 | } |
Mehdi Amini | 7e88d0d | 2015-12-09 08:17:35 +0000 | [diff] [blame] | 199 | |
Mehdi Amini | cb87494 | 2016-04-23 23:29:24 +0000 | [diff] [blame] | 200 | /// 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. |
| 203 | static void exportGlobalInModule(const ModuleSummaryIndex &Index, |
| 204 | StringRef ExportModulePath, |
| 205 | GlobalValue::GUID GUID, |
| 206 | FunctionImporter::ExportSetTy &ExportList) { |
Teresa Johnson | 28e457b | 2016-04-24 14:57:11 +0000 | [diff] [blame] | 207 | auto FindGlobalSummaryInModule = |
| 208 | [&](GlobalValue::GUID GUID) -> GlobalValueSummary *{ |
| 209 | auto SummaryList = Index.findGlobalValueSummaryList(GUID); |
| 210 | if (SummaryList == Index.end()) |
Mehdi Amini | cb87494 | 2016-04-23 23:29:24 +0000 | [diff] [blame] | 211 | // This global does not have a summary, it is not part of the ThinLTO |
| 212 | // process |
| 213 | return nullptr; |
Teresa Johnson | 28e457b | 2016-04-24 14:57:11 +0000 | [diff] [blame] | 214 | auto SummaryIter = llvm::find_if( |
| 215 | SummaryList->second, |
| 216 | [&](const std::unique_ptr<GlobalValueSummary> &Summary) { |
Mehdi Amini | cb87494 | 2016-04-23 23:29:24 +0000 | [diff] [blame] | 217 | return Summary->modulePath() == ExportModulePath; |
| 218 | }); |
Teresa Johnson | 28e457b | 2016-04-24 14:57:11 +0000 | [diff] [blame] | 219 | if (SummaryIter == SummaryList->second.end()) |
Mehdi Amini | cb87494 | 2016-04-23 23:29:24 +0000 | [diff] [blame] | 220 | return nullptr; |
Teresa Johnson | 28e457b | 2016-04-24 14:57:11 +0000 | [diff] [blame] | 221 | return SummaryIter->get(); |
Mehdi Amini | cb87494 | 2016-04-23 23:29:24 +0000 | [diff] [blame] | 222 | }; |
| 223 | |
Teresa Johnson | 28e457b | 2016-04-24 14:57:11 +0000 | [diff] [blame] | 224 | auto *Summary = FindGlobalSummaryInModule(GUID); |
| 225 | if (!Summary) |
Mehdi Amini | cb87494 | 2016-04-23 23:29:24 +0000 | [diff] [blame] | 226 | return; |
| 227 | // We found it in the current module, mark as exported |
| 228 | ExportList.insert(GUID); |
| 229 | |
Mehdi Amini | cb87494 | 2016-04-23 23:29:24 +0000 | [diff] [blame] | 230 | 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 Johnson | 28e457b | 2016-04-24 14:57:11 +0000 | [diff] [blame] | 240 | auto *RefSummary = FindGlobalSummaryInModule(GUID); |
| 241 | if (RefSummary) |
Mehdi Amini | cb87494 | 2016-04-23 23:29:24 +0000 | [diff] [blame] | 242 | // Found a ref in the current module, mark it as exported |
| 243 | ExportList.insert(GUID); |
| 244 | } |
Mehdi Amini | 01e3213 | 2016-03-26 05:40:34 +0000 | [diff] [blame] | 245 | } |
Mehdi Amini | 4064174 | 2016-02-10 23:31:45 +0000 | [diff] [blame] | 246 | |
Mehdi Amini | 01e3213 | 2016-03-26 05:40:34 +0000 | [diff] [blame] | 247 | using EdgeInfo = std::pair<const FunctionSummary *, unsigned /* Threshold */>; |
Mehdi Amini | 7e88d0d | 2015-12-09 08:17:35 +0000 | [diff] [blame] | 248 | |
Mehdi Amini | 01e3213 | 2016-03-26 05:40:34 +0000 | [diff] [blame] | 249 | /// 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. |
| 252 | static void computeImportForFunction( |
Teresa Johnson | 3255eec | 2016-04-10 15:17:26 +0000 | [diff] [blame] | 253 | const FunctionSummary &Summary, const ModuleSummaryIndex &Index, |
Teresa Johnson | c851d21 | 2016-04-25 21:09:51 +0000 | [diff] [blame] | 254 | unsigned Threshold, const GVSummaryMapTy &DefinedGVSummaries, |
Mehdi Amini | 01e3213 | 2016-03-26 05:40:34 +0000 | [diff] [blame] | 255 | SmallVectorImpl<EdgeInfo> &Worklist, |
| 256 | FunctionImporter::ImportMapTy &ImportsForModule, |
Teresa Johnson | c86af33 | 2016-04-12 21:13:11 +0000 | [diff] [blame] | 257 | StringMap<FunctionImporter::ExportSetTy> *ExportLists = nullptr) { |
Mehdi Amini | 01e3213 | 2016-03-26 05:40:34 +0000 | [diff] [blame] | 258 | for (auto &Edge : Summary.calls()) { |
Teresa Johnson | 2d5487c | 2016-04-11 13:58:45 +0000 | [diff] [blame] | 259 | auto GUID = Edge.first.getGUID(); |
Mehdi Amini | 01e3213 | 2016-03-26 05:40:34 +0000 | [diff] [blame] | 260 | DEBUG(dbgs() << " edge -> " << GUID << " Threshold:" << Threshold << "\n"); |
| 261 | |
Mehdi Amini | 1aafabf | 2016-04-16 07:02:16 +0000 | [diff] [blame] | 262 | if (DefinedGVSummaries.count(GUID)) { |
Mehdi Amini | 01e3213 | 2016-03-26 05:40:34 +0000 | [diff] [blame] | 263 | DEBUG(dbgs() << "ignored! Target already in destination module.\n"); |
| 264 | continue; |
Teresa Johnson | d450da3 | 2015-11-24 21:15:19 +0000 | [diff] [blame] | 265 | } |
Mehdi Amini | 01e3213 | 2016-03-26 05:40:34 +0000 | [diff] [blame] | 266 | |
| 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 Amini | 2d28f7a | 2016-04-16 06:56:44 +0000 | [diff] [blame] | 272 | // "Resolve" the summary, traversing alias, |
| 273 | const FunctionSummary *ResolvedCalleeSummary; |
Mehdi Amini | 6968ef7 | 2016-04-20 01:04:20 +0000 | [diff] [blame] | 274 | if (isa<AliasSummary>(CalleeSummary)) { |
Mehdi Amini | 2d28f7a | 2016-04-16 06:56:44 +0000 | [diff] [blame] | 275 | ResolvedCalleeSummary = cast<FunctionSummary>( |
| 276 | &cast<AliasSummary>(CalleeSummary)->getAliasee()); |
Mehdi Amini | 2c719cc | 2016-04-20 04:17:36 +0000 | [diff] [blame] | 277 | assert( |
| 278 | GlobalValue::isLinkOnceODRLinkage(ResolvedCalleeSummary->linkage()) && |
| 279 | "Unexpected alias to a non-linkonceODR in import list"); |
Mehdi Amini | 6968ef7 | 2016-04-20 01:04:20 +0000 | [diff] [blame] | 280 | } else |
Mehdi Amini | 2d28f7a | 2016-04-16 06:56:44 +0000 | [diff] [blame] | 281 | ResolvedCalleeSummary = cast<FunctionSummary>(CalleeSummary); |
| 282 | |
| 283 | assert(ResolvedCalleeSummary->instCount() <= Threshold && |
Mehdi Amini | 01e3213 | 2016-03-26 05:40:34 +0000 | [diff] [blame] | 284 | "selectCallee() didn't honor the threshold"); |
| 285 | |
Mehdi Amini | 2d28f7a | 2016-04-16 06:56:44 +0000 | [diff] [blame] | 286 | auto ExportModulePath = ResolvedCalleeSummary->modulePath(); |
| 287 | auto &ProcessedThreshold = ImportsForModule[ExportModulePath][GUID]; |
Mehdi Amini | 01e3213 | 2016-03-26 05:40:34 +0000 | [diff] [blame] | 288 | /// 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 Johnson | c86af33 | 2016-04-12 21:13:11 +0000 | [diff] [blame] | 300 | if (ExportLists) { |
Mehdi Amini | ef7555f | 2016-04-13 01:52:32 +0000 | [diff] [blame] | 301 | auto &ExportList = (*ExportLists)[ExportModulePath]; |
Teresa Johnson | c86af33 | 2016-04-12 21:13:11 +0000 | [diff] [blame] | 302 | 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 Amini | 2d28f7a | 2016-04-16 06:56:44 +0000 | [diff] [blame] | 305 | for (auto &Edge : ResolvedCalleeSummary->calls()) { |
Teresa Johnson | c86af33 | 2016-04-12 21:13:11 +0000 | [diff] [blame] | 306 | auto CalleeGUID = Edge.first.getGUID(); |
Mehdi Amini | cb87494 | 2016-04-23 23:29:24 +0000 | [diff] [blame] | 307 | exportGlobalInModule(Index, ExportModulePath, CalleeGUID, ExportList); |
Teresa Johnson | c86af33 | 2016-04-12 21:13:11 +0000 | [diff] [blame] | 308 | } |
Mehdi Amini | 2d28f7a | 2016-04-16 06:56:44 +0000 | [diff] [blame] | 309 | for (auto &Ref : ResolvedCalleeSummary->refs()) { |
Teresa Johnson | c86af33 | 2016-04-12 21:13:11 +0000 | [diff] [blame] | 310 | auto GUID = Ref.getGUID(); |
Mehdi Amini | cb87494 | 2016-04-23 23:29:24 +0000 | [diff] [blame] | 311 | exportGlobalInModule(Index, ExportModulePath, GUID, ExportList); |
Teresa Johnson | c86af33 | 2016-04-12 21:13:11 +0000 | [diff] [blame] | 312 | } |
Mehdi Amini | 01e3213 | 2016-03-26 05:40:34 +0000 | [diff] [blame] | 313 | } |
| 314 | |
| 315 | // Insert the newly imported function to the worklist. |
Mehdi Amini | 2d28f7a | 2016-04-16 06:56:44 +0000 | [diff] [blame] | 316 | Worklist.push_back(std::make_pair(ResolvedCalleeSummary, Threshold)); |
Teresa Johnson | d450da3 | 2015-11-24 21:15:19 +0000 | [diff] [blame] | 317 | } |
| 318 | } |
| 319 | |
Mehdi Amini | 01e3213 | 2016-03-26 05:40:34 +0000 | [diff] [blame] | 320 | /// 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). |
| 323 | static void ComputeImportForModule( |
Teresa Johnson | c851d21 | 2016-04-25 21:09:51 +0000 | [diff] [blame] | 324 | const GVSummaryMapTy &DefinedGVSummaries, const ModuleSummaryIndex &Index, |
Mehdi Amini | 01e3213 | 2016-03-26 05:40:34 +0000 | [diff] [blame] | 325 | FunctionImporter::ImportMapTy &ImportsForModule, |
Teresa Johnson | c86af33 | 2016-04-12 21:13:11 +0000 | [diff] [blame] | 326 | StringMap<FunctionImporter::ExportSetTy> *ExportLists = nullptr) { |
Mehdi Amini | 01e3213 | 2016-03-26 05:40:34 +0000 | [diff] [blame] | 327 | // 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 Johnson | 28e457b | 2016-04-24 14:57:11 +0000 | [diff] [blame] | 333 | for (auto &GVSummary : DefinedGVSummaries) { |
| 334 | auto *Summary = GVSummary.second; |
Mehdi Amini | 2d28f7a | 2016-04-16 06:56:44 +0000 | [diff] [blame] | 335 | if (auto *AS = dyn_cast<AliasSummary>(Summary)) |
| 336 | Summary = &AS->getAliasee(); |
Mehdi Amini | 1aafabf | 2016-04-16 07:02:16 +0000 | [diff] [blame] | 337 | auto *FuncSummary = dyn_cast<FunctionSummary>(Summary); |
| 338 | if (!FuncSummary) |
| 339 | // Skip import for global variables |
| 340 | continue; |
Teresa Johnson | 28e457b | 2016-04-24 14:57:11 +0000 | [diff] [blame] | 341 | DEBUG(dbgs() << "Initalize import for " << GVSummary.first << "\n"); |
Mehdi Amini | 2d28f7a | 2016-04-16 06:56:44 +0000 | [diff] [blame] | 342 | computeImportForFunction(*FuncSummary, Index, ImportInstrLimit, |
Mehdi Amini | 1aafabf | 2016-04-16 07:02:16 +0000 | [diff] [blame] | 343 | DefinedGVSummaries, Worklist, ImportsForModule, |
Mehdi Amini | 01e3213 | 2016-03-26 05:40:34 +0000 | [diff] [blame] | 344 | ExportLists); |
| 345 | } |
| 346 | |
Mehdi Amini | 42418ab | 2015-11-24 06:07:49 +0000 | [diff] [blame] | 347 | while (!Worklist.empty()) { |
Mehdi Amini | 01e3213 | 2016-03-26 05:40:34 +0000 | [diff] [blame] | 348 | auto FuncInfo = Worklist.pop_back_val(); |
| 349 | auto *Summary = FuncInfo.first; |
| 350 | auto Threshold = FuncInfo.second; |
Mehdi Amini | 42418ab | 2015-11-24 06:07:49 +0000 | [diff] [blame] | 351 | |
Mehdi Amini | 7e88d0d | 2015-12-09 08:17:35 +0000 | [diff] [blame] | 352 | // Process the newly imported functions and add callees to the worklist. |
Mehdi Amini | 4064174 | 2016-02-10 23:31:45 +0000 | [diff] [blame] | 353 | // Adjust the threshold |
| 354 | Threshold = Threshold * ImportInstrFactor; |
Mehdi Amini | 01e3213 | 2016-03-26 05:40:34 +0000 | [diff] [blame] | 355 | |
Mehdi Amini | 1aafabf | 2016-04-16 07:02:16 +0000 | [diff] [blame] | 356 | computeImportForFunction(*Summary, Index, Threshold, DefinedGVSummaries, |
Teresa Johnson | 3255eec | 2016-04-10 15:17:26 +0000 | [diff] [blame] | 357 | Worklist, ImportsForModule, ExportLists); |
Mehdi Amini | 42418ab | 2015-11-24 06:07:49 +0000 | [diff] [blame] | 358 | } |
Mehdi Amini | c8c5517 | 2015-12-03 02:37:33 +0000 | [diff] [blame] | 359 | } |
Mehdi Amini | ffe2e4a | 2015-12-02 04:34:28 +0000 | [diff] [blame] | 360 | |
Mehdi Amini | 01e3213 | 2016-03-26 05:40:34 +0000 | [diff] [blame] | 361 | } // anonymous namespace |
| 362 | |
Teresa Johnson | c86af33 | 2016-04-12 21:13:11 +0000 | [diff] [blame] | 363 | /// Compute all the import and export for every module using the Index. |
Mehdi Amini | 01e3213 | 2016-03-26 05:40:34 +0000 | [diff] [blame] | 364 | void llvm::ComputeCrossModuleImport( |
| 365 | const ModuleSummaryIndex &Index, |
Teresa Johnson | c851d21 | 2016-04-25 21:09:51 +0000 | [diff] [blame] | 366 | const StringMap<GVSummaryMapTy> &ModuleToDefinedGVSummaries, |
Mehdi Amini | 01e3213 | 2016-03-26 05:40:34 +0000 | [diff] [blame] | 367 | StringMap<FunctionImporter::ImportMapTy> &ImportLists, |
| 368 | StringMap<FunctionImporter::ExportSetTy> &ExportLists) { |
Mehdi Amini | 01e3213 | 2016-03-26 05:40:34 +0000 | [diff] [blame] | 369 | // For each module that has function defined, compute the import/export lists. |
Mehdi Amini | 1aafabf | 2016-04-16 07:02:16 +0000 | [diff] [blame] | 370 | 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 Johnson | c86af33 | 2016-04-12 21:13:11 +0000 | [diff] [blame] | 375 | &ExportLists); |
Mehdi Amini | 01e3213 | 2016-03-26 05:40:34 +0000 | [diff] [blame] | 376 | } |
| 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 Johnson | c86af33 | 2016-04-12 21:13:11 +0000 | [diff] [blame] | 396 | /// Compute all the imports for the given module in the Index. |
| 397 | void 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 Johnson | c851d21 | 2016-04-25 21:09:51 +0000 | [diff] [blame] | 403 | GVSummaryMapTy FunctionSummaryMap; |
Teresa Johnson | 28e457b | 2016-04-24 14:57:11 +0000 | [diff] [blame] | 404 | Index.collectDefinedFunctionsForModule(ModulePath, FunctionSummaryMap); |
Teresa Johnson | c86af33 | 2016-04-12 21:13:11 +0000 | [diff] [blame] | 405 | |
| 406 | // Compute the import list for this module. |
| 407 | DEBUG(dbgs() << "Computing import for Module '" << ModulePath << "'\n"); |
Teresa Johnson | 28e457b | 2016-04-24 14:57:11 +0000 | [diff] [blame] | 408 | ComputeImportForModule(FunctionSummaryMap, Index, ImportList); |
Teresa Johnson | c86af33 | 2016-04-12 21:13:11 +0000 | [diff] [blame] | 409 | |
| 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 | |
Mehdi Amini | c8c5517 | 2015-12-03 02:37:33 +0000 | [diff] [blame] | 421 | // Automatically import functions in Module \p DestModule based on the summaries |
| 422 | // index. |
| 423 | // |
Mehdi Amini | 01e3213 | 2016-03-26 05:40:34 +0000 | [diff] [blame] | 424 | bool FunctionImporter::importFunctions( |
Mehdi Amini | bda3c97 | 2016-04-21 01:59:39 +0000 | [diff] [blame] | 425 | Module &DestModule, const FunctionImporter::ImportMapTy &ImportList, |
| 426 | bool ForceImportReferencedDiscardableSymbols) { |
Mehdi Amini | 5411d05 | 2015-12-08 23:04:19 +0000 | [diff] [blame] | 427 | DEBUG(dbgs() << "Starting import for Module " |
Mehdi Amini | 311fef6 | 2015-12-03 02:58:14 +0000 | [diff] [blame] | 428 | << DestModule.getModuleIdentifier() << "\n"); |
Mehdi Amini | c8c5517 | 2015-12-03 02:37:33 +0000 | [diff] [blame] | 429 | unsigned ImportedCount = 0; |
| 430 | |
Mehdi Amini | c8c5517 | 2015-12-03 02:37:33 +0000 | [diff] [blame] | 431 | // Linker that will be used for importing function |
Rafael Espindola | 9d2bfc4 | 2015-12-14 23:17:03 +0000 | [diff] [blame] | 432 | Linker TheLinker(DestModule); |
Mehdi Amini | 7e88d0d | 2015-12-09 08:17:35 +0000 | [diff] [blame] | 433 | // Do the actual import of functions now, one Module at a time |
Mehdi Amini | 01e3213 | 2016-03-26 05:40:34 +0000 | [diff] [blame] | 434 | std::set<StringRef> ModuleNameOrderedList; |
| 435 | for (auto &FunctionsToImportPerModule : ImportList) { |
| 436 | ModuleNameOrderedList.insert(FunctionsToImportPerModule.first()); |
| 437 | } |
| 438 | for (auto &Name : ModuleNameOrderedList) { |
Mehdi Amini | 7e88d0d | 2015-12-09 08:17:35 +0000 | [diff] [blame] | 439 | // Get the module for the import |
Mehdi Amini | 01e3213 | 2016-03-26 05:40:34 +0000 | [diff] [blame] | 440 | const auto &FunctionsToImportPerModule = ImportList.find(Name); |
| 441 | assert(FunctionsToImportPerModule != ImportList.end()); |
| 442 | std::unique_ptr<Module> SrcModule = ModuleLoader(Name); |
Mehdi Amini | 7e88d0d | 2015-12-09 08:17:35 +0000 | [diff] [blame] | 443 | assert(&DestModule.getContext() == &SrcModule->getContext() && |
| 444 | "Context mismatch"); |
| 445 | |
Teresa Johnson | 6cba37c | 2016-01-22 00:15:53 +0000 | [diff] [blame] | 446 | // If modules were created with lazy metadata loading, materialize it |
| 447 | // now, before linking it (otherwise this will be a noop). |
| 448 | SrcModule->materializeMetadata(); |
| 449 | UpgradeDebugInfo(*SrcModule); |
Teresa Johnson | e5a6191 | 2015-12-17 17:14:09 +0000 | [diff] [blame] | 450 | |
Mehdi Amini | 01e3213 | 2016-03-26 05:40:34 +0000 | [diff] [blame] | 451 | auto &ImportGUIDs = FunctionsToImportPerModule->second; |
| 452 | // Find the globals to import |
| 453 | DenseSet<const GlobalValue *> GlobalsToImport; |
| 454 | for (auto &GV : *SrcModule) { |
Teresa Johnson | 0beb858 | 2016-04-04 18:52:23 +0000 | [diff] [blame] | 455 | if (!GV.hasName()) |
| 456 | continue; |
| 457 | auto GUID = GV.getGUID(); |
| 458 | auto Import = ImportGUIDs.count(GUID); |
Mehdi Amini | aeb1e59 | 2016-04-19 09:21:30 +0000 | [diff] [blame] | 459 | DEBUG(dbgs() << (Import ? "Is" : "Not") << " importing function " << GUID |
| 460 | << " " << GV.getName() << " from " |
| 461 | << SrcModule->getSourceFileName() << "\n"); |
Teresa Johnson | 0beb858 | 2016-04-04 18:52:23 +0000 | [diff] [blame] | 462 | if (Import) { |
Mehdi Amini | 01e3213 | 2016-03-26 05:40:34 +0000 | [diff] [blame] | 463 | GV.materialize(); |
| 464 | GlobalsToImport.insert(&GV); |
| 465 | } |
| 466 | } |
Mehdi Amini | 2d28f7a | 2016-04-16 06:56:44 +0000 | [diff] [blame] | 467 | for (auto &GV : SrcModule->globals()) { |
| 468 | if (!GV.hasName()) |
| 469 | continue; |
| 470 | auto GUID = GV.getGUID(); |
| 471 | auto Import = ImportGUIDs.count(GUID); |
Mehdi Amini | aeb1e59 | 2016-04-19 09:21:30 +0000 | [diff] [blame] | 472 | DEBUG(dbgs() << (Import ? "Is" : "Not") << " importing global " << GUID |
| 473 | << " " << GV.getName() << " from " |
| 474 | << SrcModule->getSourceFileName() << "\n"); |
Mehdi Amini | 2d28f7a | 2016-04-16 06:56:44 +0000 | [diff] [blame] | 475 | if (Import) { |
| 476 | GV.materialize(); |
| 477 | GlobalsToImport.insert(&GV); |
| 478 | } |
| 479 | } |
Mehdi Amini | 01e3213 | 2016-03-26 05:40:34 +0000 | [diff] [blame] | 480 | for (auto &GV : SrcModule->aliases()) { |
| 481 | if (!GV.hasName()) |
| 482 | continue; |
| 483 | auto GUID = GV.getGUID(); |
Teresa Johnson | 0beb858 | 2016-04-04 18:52:23 +0000 | [diff] [blame] | 484 | auto Import = ImportGUIDs.count(GUID); |
Mehdi Amini | aeb1e59 | 2016-04-19 09:21:30 +0000 | [diff] [blame] | 485 | DEBUG(dbgs() << (Import ? "Is" : "Not") << " importing alias " << GUID |
| 486 | << " " << GV.getName() << " from " |
| 487 | << SrcModule->getSourceFileName() << "\n"); |
Teresa Johnson | 0beb858 | 2016-04-04 18:52:23 +0000 | [diff] [blame] | 488 | if (Import) { |
Mehdi Amini | 01e3213 | 2016-03-26 05:40:34 +0000 | [diff] [blame] | 489 | // Alias can't point to "available_externally". However when we import |
Teresa Johnson | 9aae395 | 2016-03-27 15:01:11 +0000 | [diff] [blame] | 490 | // linkOnceODR the linkage does not change. So we import the alias |
Mehdi Amini | 6968ef7 | 2016-04-20 01:04:20 +0000 | [diff] [blame] | 491 | // and aliasee only in this case. This has been handled by |
| 492 | // computeImportForFunction() |
Mehdi Amini | 2d28f7a | 2016-04-16 06:56:44 +0000 | [diff] [blame] | 493 | GlobalObject *GO = GV.getBaseObject(); |
Mehdi Amini | 6968ef7 | 2016-04-20 01:04:20 +0000 | [diff] [blame] | 494 | assert(GO->hasLinkOnceODRLinkage() && |
| 495 | "Unexpected alias to a non-linkonceODR in import list"); |
Mehdi Amini | 2d28f7a | 2016-04-16 06:56:44 +0000 | [diff] [blame] | 496 | #ifndef NDEBUG |
| 497 | if (!GlobalsToImport.count(GO)) |
| 498 | DEBUG(dbgs() << " alias triggers importing aliasee " << GO->getGUID() |
| 499 | << " " << GO->getName() << " from " |
| 500 | << SrcModule->getSourceFileName() << "\n"); |
| 501 | #endif |
| 502 | GO->materialize(); |
Mehdi Amini | 01e3213 | 2016-03-26 05:40:34 +0000 | [diff] [blame] | 503 | GlobalsToImport.insert(GO); |
Mehdi Amini | 01e3213 | 2016-03-26 05:40:34 +0000 | [diff] [blame] | 504 | GV.materialize(); |
| 505 | GlobalsToImport.insert(&GV); |
| 506 | } |
| 507 | } |
| 508 | |
Mehdi Amini | 7e88d0d | 2015-12-09 08:17:35 +0000 | [diff] [blame] | 509 | // Link in the specified functions. |
Mehdi Amini | 01e3213 | 2016-03-26 05:40:34 +0000 | [diff] [blame] | 510 | if (renameModuleForThinLTO(*SrcModule, Index, &GlobalsToImport)) |
Mehdi Amini | 8d05185 | 2016-03-19 00:40:31 +0000 | [diff] [blame] | 511 | return true; |
| 512 | |
Teresa Johnson | d29478f | 2016-03-27 15:27:30 +0000 | [diff] [blame] | 513 | if (PrintImports) { |
| 514 | for (const auto *GV : GlobalsToImport) |
| 515 | dbgs() << DestModule.getSourceFileName() << ": Import " << GV->getName() |
| 516 | << " from " << SrcModule->getSourceFileName() << "\n"; |
| 517 | } |
| 518 | |
Mehdi Amini | bda3c97 | 2016-04-21 01:59:39 +0000 | [diff] [blame] | 519 | // Instruct the linker that the client will take care of linkonce resolution |
| 520 | unsigned Flags = Linker::Flags::None; |
| 521 | if (!ForceImportReferencedDiscardableSymbols) |
| 522 | Flags |= Linker::Flags::DontForceLinkLinkonceODR; |
| 523 | |
| 524 | if (TheLinker.linkInModule(std::move(SrcModule), Flags, &GlobalsToImport)) |
Mehdi Amini | 7e88d0d | 2015-12-09 08:17:35 +0000 | [diff] [blame] | 525 | report_fatal_error("Function Import: link error"); |
| 526 | |
Mehdi Amini | 01e3213 | 2016-03-26 05:40:34 +0000 | [diff] [blame] | 527 | ImportedCount += GlobalsToImport.size(); |
Mehdi Amini | 7e88d0d | 2015-12-09 08:17:35 +0000 | [diff] [blame] | 528 | } |
Teresa Johnson | e5a6191 | 2015-12-17 17:14:09 +0000 | [diff] [blame] | 529 | |
Teresa Johnson | d29478f | 2016-03-27 15:27:30 +0000 | [diff] [blame] | 530 | NumImported += ImportedCount; |
| 531 | |
Mehdi Amini | 7e88d0d | 2015-12-09 08:17:35 +0000 | [diff] [blame] | 532 | DEBUG(dbgs() << "Imported " << ImportedCount << " functions for Module " |
Mehdi Amini | c8c5517 | 2015-12-03 02:37:33 +0000 | [diff] [blame] | 533 | << DestModule.getModuleIdentifier() << "\n"); |
| 534 | return ImportedCount; |
Mehdi Amini | 42418ab | 2015-11-24 06:07:49 +0000 | [diff] [blame] | 535 | } |
| 536 | |
| 537 | /// Summary file to use for function importing when using -function-import from |
| 538 | /// the command line. |
| 539 | static cl::opt<std::string> |
| 540 | SummaryFile("summary-file", |
| 541 | cl::desc("The summary file to use for function importing.")); |
| 542 | |
| 543 | static void diagnosticHandler(const DiagnosticInfo &DI) { |
| 544 | raw_ostream &OS = errs(); |
| 545 | DiagnosticPrinterRawOStream DP(OS); |
| 546 | DI.print(DP); |
| 547 | OS << '\n'; |
| 548 | } |
| 549 | |
Teresa Johnson | 26ab577 | 2016-03-15 00:04:37 +0000 | [diff] [blame] | 550 | /// 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] | 551 | /// index object if found, or nullptr if not. |
Teresa Johnson | 26ab577 | 2016-03-15 00:04:37 +0000 | [diff] [blame] | 552 | static std::unique_ptr<ModuleSummaryIndex> |
| 553 | getModuleSummaryIndexForFile(StringRef Path, std::string &Error, |
| 554 | DiagnosticHandlerFunction DiagnosticHandler) { |
Mehdi Amini | 42418ab | 2015-11-24 06:07:49 +0000 | [diff] [blame] | 555 | std::unique_ptr<MemoryBuffer> Buffer; |
| 556 | ErrorOr<std::unique_ptr<MemoryBuffer>> BufferOrErr = |
| 557 | MemoryBuffer::getFile(Path); |
| 558 | if (std::error_code EC = BufferOrErr.getError()) { |
| 559 | Error = EC.message(); |
| 560 | return nullptr; |
| 561 | } |
| 562 | Buffer = std::move(BufferOrErr.get()); |
Teresa Johnson | 26ab577 | 2016-03-15 00:04:37 +0000 | [diff] [blame] | 563 | ErrorOr<std::unique_ptr<object::ModuleSummaryIndexObjectFile>> ObjOrErr = |
| 564 | object::ModuleSummaryIndexObjectFile::create(Buffer->getMemBufferRef(), |
| 565 | DiagnosticHandler); |
Mehdi Amini | 42418ab | 2015-11-24 06:07:49 +0000 | [diff] [blame] | 566 | if (std::error_code EC = ObjOrErr.getError()) { |
| 567 | Error = EC.message(); |
| 568 | return nullptr; |
| 569 | } |
| 570 | return (*ObjOrErr)->takeIndex(); |
| 571 | } |
| 572 | |
Benjamin Kramer | fe2b541 | 2015-12-24 10:03:35 +0000 | [diff] [blame] | 573 | namespace { |
Mehdi Amini | 42418ab | 2015-11-24 06:07:49 +0000 | [diff] [blame] | 574 | /// Pass that performs cross-module function import provided a summary file. |
| 575 | class FunctionImportPass : public ModulePass { |
Teresa Johnson | 26ab577 | 2016-03-15 00:04:37 +0000 | [diff] [blame] | 576 | /// Optional module summary index to use for importing, otherwise |
Teresa Johnson | 5fcbdb7 | 2015-12-07 19:21:11 +0000 | [diff] [blame] | 577 | /// the summary-file option must be specified. |
Teresa Johnson | 26ab577 | 2016-03-15 00:04:37 +0000 | [diff] [blame] | 578 | const ModuleSummaryIndex *Index; |
Mehdi Amini | 42418ab | 2015-11-24 06:07:49 +0000 | [diff] [blame] | 579 | |
| 580 | public: |
| 581 | /// Pass identification, replacement for typeid |
| 582 | static char ID; |
| 583 | |
Teresa Johnson | 5fcbdb7 | 2015-12-07 19:21:11 +0000 | [diff] [blame] | 584 | /// Specify pass name for debug output |
Mehdi Amini | 2d28f7a | 2016-04-16 06:56:44 +0000 | [diff] [blame] | 585 | const char *getPassName() const override { return "Function Importing"; } |
Teresa Johnson | 5fcbdb7 | 2015-12-07 19:21:11 +0000 | [diff] [blame] | 586 | |
Teresa Johnson | 26ab577 | 2016-03-15 00:04:37 +0000 | [diff] [blame] | 587 | explicit FunctionImportPass(const ModuleSummaryIndex *Index = nullptr) |
Teresa Johnson | 5fcbdb7 | 2015-12-07 19:21:11 +0000 | [diff] [blame] | 588 | : ModulePass(ID), Index(Index) {} |
Mehdi Amini | 42418ab | 2015-11-24 06:07:49 +0000 | [diff] [blame] | 589 | |
| 590 | bool runOnModule(Module &M) override { |
Andrew Kaylor | aa641a5 | 2016-04-22 22:06:11 +0000 | [diff] [blame] | 591 | if (skipModule(M)) |
| 592 | return false; |
| 593 | |
Teresa Johnson | 5fcbdb7 | 2015-12-07 19:21:11 +0000 | [diff] [blame] | 594 | if (SummaryFile.empty() && !Index) |
| 595 | report_fatal_error("error: -function-import requires -summary-file or " |
| 596 | "file from frontend\n"); |
Teresa Johnson | 26ab577 | 2016-03-15 00:04:37 +0000 | [diff] [blame] | 597 | std::unique_ptr<ModuleSummaryIndex> IndexPtr; |
Teresa Johnson | 5fcbdb7 | 2015-12-07 19:21:11 +0000 | [diff] [blame] | 598 | if (!SummaryFile.empty()) { |
| 599 | if (Index) |
| 600 | report_fatal_error("error: -summary-file and index from frontend\n"); |
| 601 | std::string Error; |
Teresa Johnson | 26ab577 | 2016-03-15 00:04:37 +0000 | [diff] [blame] | 602 | IndexPtr = |
| 603 | getModuleSummaryIndexForFile(SummaryFile, Error, diagnosticHandler); |
Teresa Johnson | 5fcbdb7 | 2015-12-07 19:21:11 +0000 | [diff] [blame] | 604 | if (!IndexPtr) { |
| 605 | errs() << "Error loading file '" << SummaryFile << "': " << Error |
| 606 | << "\n"; |
| 607 | return false; |
| 608 | } |
| 609 | Index = IndexPtr.get(); |
Mehdi Amini | 42418ab | 2015-11-24 06:07:49 +0000 | [diff] [blame] | 610 | } |
| 611 | |
Teresa Johnson | c86af33 | 2016-04-12 21:13:11 +0000 | [diff] [blame] | 612 | // First step is collecting the import list. |
| 613 | FunctionImporter::ImportMapTy ImportList; |
| 614 | ComputeCrossModuleImportForModule(M.getModuleIdentifier(), *Index, |
| 615 | ImportList); |
Mehdi Amini | 01e3213 | 2016-03-26 05:40:34 +0000 | [diff] [blame] | 616 | |
| 617 | // 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] | 618 | // are potentially exported to other modules. |
Mehdi Amini | 01e3213 | 2016-03-26 05:40:34 +0000 | [diff] [blame] | 619 | if (renameModuleForThinLTO(M, *Index, nullptr)) { |
Teresa Johnson | 1b00f2d | 2016-01-08 17:06:29 +0000 | [diff] [blame] | 620 | errs() << "Error renaming module\n"; |
| 621 | return false; |
| 622 | } |
| 623 | |
Mehdi Amini | 42418ab | 2015-11-24 06:07:49 +0000 | [diff] [blame] | 624 | // Perform the import now. |
Mehdi Amini | d16c806 | 2015-12-08 22:39:40 +0000 | [diff] [blame] | 625 | auto ModuleLoader = [&M](StringRef Identifier) { |
| 626 | return loadFile(Identifier, M.getContext()); |
| 627 | }; |
Rafael Espindola | 9d2bfc4 | 2015-12-14 23:17:03 +0000 | [diff] [blame] | 628 | FunctionImporter Importer(*Index, ModuleLoader); |
Mehdi Amini | bda3c97 | 2016-04-21 01:59:39 +0000 | [diff] [blame] | 629 | return Importer.importFunctions( |
| 630 | M, ImportList, !DontForceImportReferencedDiscardableSymbols); |
Mehdi Amini | 42418ab | 2015-11-24 06:07:49 +0000 | [diff] [blame] | 631 | } |
| 632 | }; |
Benjamin Kramer | fe2b541 | 2015-12-24 10:03:35 +0000 | [diff] [blame] | 633 | } // anonymous namespace |
Mehdi Amini | 42418ab | 2015-11-24 06:07:49 +0000 | [diff] [blame] | 634 | |
| 635 | char FunctionImportPass::ID = 0; |
| 636 | INITIALIZE_PASS_BEGIN(FunctionImportPass, "function-import", |
| 637 | "Summary Based Function Import", false, false) |
| 638 | INITIALIZE_PASS_END(FunctionImportPass, "function-import", |
| 639 | "Summary Based Function Import", false, false) |
| 640 | |
| 641 | namespace llvm { |
Teresa Johnson | 26ab577 | 2016-03-15 00:04:37 +0000 | [diff] [blame] | 642 | Pass *createFunctionImportPass(const ModuleSummaryIndex *Index = nullptr) { |
Teresa Johnson | 5fcbdb7 | 2015-12-07 19:21:11 +0000 | [diff] [blame] | 643 | return new FunctionImportPass(Index); |
| 644 | } |
Mehdi Amini | 42418ab | 2015-11-24 06:07:49 +0000 | [diff] [blame] | 645 | } |