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" |
Teresa Johnson | 04c9a2d | 2016-05-25 14:03:11 +0000 | [diff] [blame] | 19 | #include "llvm/ADT/Triple.h" |
Mehdi Amini | 42418ab | 2015-11-24 06:07:49 +0000 | [diff] [blame] | 20 | #include "llvm/IR/AutoUpgrade.h" |
| 21 | #include "llvm/IR/DiagnosticPrinter.h" |
| 22 | #include "llvm/IR/IntrinsicInst.h" |
| 23 | #include "llvm/IR/Module.h" |
| 24 | #include "llvm/IRReader/IRReader.h" |
| 25 | #include "llvm/Linker/Linker.h" |
Teresa Johnson | 04c9a2d | 2016-05-25 14:03:11 +0000 | [diff] [blame] | 26 | #include "llvm/Object/IRObjectFile.h" |
Teresa Johnson | 26ab577 | 2016-03-15 00:04:37 +0000 | [diff] [blame] | 27 | #include "llvm/Object/ModuleSummaryIndexObjectFile.h" |
Mehdi Amini | 42418ab | 2015-11-24 06:07:49 +0000 | [diff] [blame] | 28 | #include "llvm/Support/CommandLine.h" |
| 29 | #include "llvm/Support/Debug.h" |
| 30 | #include "llvm/Support/SourceMgr.h" |
Teresa Johnson | 04c9a2d | 2016-05-25 14:03:11 +0000 | [diff] [blame] | 31 | #include "llvm/Transforms/IPO/Internalize.h" |
Teresa Johnson | 488a800 | 2016-02-10 18:11:31 +0000 | [diff] [blame] | 32 | #include "llvm/Transforms/Utils/FunctionImportUtils.h" |
Mehdi Amini | 7e88d0d | 2015-12-09 08:17:35 +0000 | [diff] [blame] | 33 | |
Mehdi Amini | 01e3213 | 2016-03-26 05:40:34 +0000 | [diff] [blame] | 34 | #define DEBUG_TYPE "function-import" |
Mehdi Amini | 7e88d0d | 2015-12-09 08:17:35 +0000 | [diff] [blame] | 35 | |
Mehdi Amini | 42418ab | 2015-11-24 06:07:49 +0000 | [diff] [blame] | 36 | using namespace llvm; |
| 37 | |
Teresa Johnson | d29478f | 2016-03-27 15:27:30 +0000 | [diff] [blame] | 38 | STATISTIC(NumImported, "Number of functions imported"); |
| 39 | |
Teresa Johnson | 3930361 | 2015-11-24 22:55:46 +0000 | [diff] [blame] | 40 | /// Limit on instruction count of imported functions. |
| 41 | static cl::opt<unsigned> ImportInstrLimit( |
| 42 | "import-instr-limit", cl::init(100), cl::Hidden, cl::value_desc("N"), |
| 43 | cl::desc("Only import functions with less than N instructions")); |
| 44 | |
Mehdi Amini | 4064174 | 2016-02-10 23:31:45 +0000 | [diff] [blame] | 45 | static cl::opt<float> |
| 46 | ImportInstrFactor("import-instr-evolution-factor", cl::init(0.7), |
| 47 | cl::Hidden, cl::value_desc("x"), |
| 48 | cl::desc("As we import functions, multiply the " |
| 49 | "`import-instr-limit` threshold by this factor " |
| 50 | "before processing newly imported functions")); |
| 51 | |
Teresa Johnson | d29478f | 2016-03-27 15:27:30 +0000 | [diff] [blame] | 52 | static cl::opt<bool> PrintImports("print-imports", cl::init(false), cl::Hidden, |
| 53 | cl::desc("Print imported functions")); |
| 54 | |
Mehdi Amini | bda3c97 | 2016-04-21 01:59:39 +0000 | [diff] [blame] | 55 | // Temporary allows the function import pass to disable always linking |
| 56 | // referenced discardable symbols. |
| 57 | static cl::opt<bool> |
| 58 | DontForceImportReferencedDiscardableSymbols("disable-force-link-odr", |
| 59 | cl::init(false), cl::Hidden); |
| 60 | |
Mehdi Amini | 42418ab | 2015-11-24 06:07:49 +0000 | [diff] [blame] | 61 | // Load lazily a module from \p FileName in \p Context. |
| 62 | static std::unique_ptr<Module> loadFile(const std::string &FileName, |
| 63 | LLVMContext &Context) { |
| 64 | SMDiagnostic Err; |
| 65 | DEBUG(dbgs() << "Loading '" << FileName << "'\n"); |
Teresa Johnson | 6cba37c | 2016-01-22 00:15:53 +0000 | [diff] [blame] | 66 | // Metadata isn't loaded until functions are imported, to minimize |
| 67 | // the memory overhead. |
Teresa Johnson | a1080ee | 2016-01-08 14:17:41 +0000 | [diff] [blame] | 68 | std::unique_ptr<Module> Result = |
| 69 | getLazyIRFileModule(FileName, Err, Context, |
| 70 | /* ShouldLazyLoadMetadata = */ true); |
Mehdi Amini | 42418ab | 2015-11-24 06:07:49 +0000 | [diff] [blame] | 71 | if (!Result) { |
| 72 | Err.print("function-import", errs()); |
Mehdi Amini | d7ad221 | 2016-04-01 05:33:11 +0000 | [diff] [blame] | 73 | report_fatal_error("Abort"); |
Mehdi Amini | 42418ab | 2015-11-24 06:07:49 +0000 | [diff] [blame] | 74 | } |
| 75 | |
Mehdi Amini | 42418ab | 2015-11-24 06:07:49 +0000 | [diff] [blame] | 76 | return Result; |
| 77 | } |
| 78 | |
Mehdi Amini | 7e88d0d | 2015-12-09 08:17:35 +0000 | [diff] [blame] | 79 | namespace { |
Mehdi Amini | 4064174 | 2016-02-10 23:31:45 +0000 | [diff] [blame] | 80 | |
Mehdi Amini | b4e1e82 | 2016-04-27 00:32:13 +0000 | [diff] [blame] | 81 | // Return true if the Summary describes a GlobalValue that can be externally |
| 82 | // referenced, i.e. it does not need renaming (linkage is not local) or renaming |
| 83 | // is possible (does not have a section for instance). |
| 84 | static bool canBeExternallyReferenced(const GlobalValueSummary &Summary) { |
| 85 | if (!Summary.needsRenaming()) |
| 86 | return true; |
| 87 | |
| 88 | if (Summary.hasSection()) |
| 89 | // Can't rename a global that needs renaming if has a section. |
| 90 | return false; |
| 91 | |
| 92 | return true; |
| 93 | } |
| 94 | |
| 95 | // Return true if \p GUID describes a GlobalValue that can be externally |
| 96 | // referenced, i.e. it does not need renaming (linkage is not local) or |
| 97 | // renaming is possible (does not have a section for instance). |
| 98 | static bool canBeExternallyReferenced(const ModuleSummaryIndex &Index, |
| 99 | GlobalValue::GUID GUID) { |
| 100 | auto Summaries = Index.findGlobalValueSummaryList(GUID); |
| 101 | if (Summaries == Index.end()) |
| 102 | return true; |
| 103 | if (Summaries->second.size() != 1) |
| 104 | // If there are multiple globals with this GUID, then we know it is |
| 105 | // not a local symbol, and it is necessarily externally referenced. |
| 106 | return true; |
| 107 | |
| 108 | // We don't need to check for the module path, because if it can't be |
| 109 | // externally referenced and we call it, it is necessarilly in the same |
| 110 | // module |
| 111 | return canBeExternallyReferenced(**Summaries->second.begin()); |
| 112 | } |
| 113 | |
| 114 | // Return true if the global described by \p Summary can be imported in another |
| 115 | // module. |
| 116 | static bool eligibleForImport(const ModuleSummaryIndex &Index, |
| 117 | const GlobalValueSummary &Summary) { |
| 118 | if (!canBeExternallyReferenced(Summary)) |
| 119 | // Can't import a global that needs renaming if has a section for instance. |
| 120 | // FIXME: we may be able to import it by copying it without promotion. |
| 121 | return false; |
| 122 | |
| 123 | // Check references (and potential calls) in the same module. If the current |
| 124 | // value references a global that can't be externally referenced it is not |
| 125 | // eligible for import. |
| 126 | bool AllRefsCanBeExternallyReferenced = |
| 127 | llvm::all_of(Summary.refs(), [&](const ValueInfo &VI) { |
| 128 | return canBeExternallyReferenced(Index, VI.getGUID()); |
| 129 | }); |
| 130 | if (!AllRefsCanBeExternallyReferenced) |
| 131 | return false; |
| 132 | |
| 133 | if (auto *FuncSummary = dyn_cast<FunctionSummary>(&Summary)) { |
| 134 | bool AllCallsCanBeExternallyReferenced = llvm::all_of( |
| 135 | FuncSummary->calls(), [&](const FunctionSummary::EdgeTy &Edge) { |
| 136 | return canBeExternallyReferenced(Index, Edge.first.getGUID()); |
| 137 | }); |
| 138 | if (!AllCallsCanBeExternallyReferenced) |
| 139 | return false; |
| 140 | } |
| 141 | return true; |
| 142 | } |
| 143 | |
Mehdi Amini | 01e3213 | 2016-03-26 05:40:34 +0000 | [diff] [blame] | 144 | /// Given a list of possible callee implementation for a call site, select one |
| 145 | /// that fits the \p Threshold. |
| 146 | /// |
| 147 | /// FIXME: select "best" instead of first that fits. But what is "best"? |
| 148 | /// - The smallest: more likely to be inlined. |
| 149 | /// - The one with the least outgoing edges (already well optimized). |
| 150 | /// - One from a module already being imported from in order to reduce the |
| 151 | /// number of source modules parsed/linked. |
| 152 | /// - One that has PGO data attached. |
| 153 | /// - [insert you fancy metric here] |
Mehdi Amini | 2d28f7a | 2016-04-16 06:56:44 +0000 | [diff] [blame] | 154 | static const GlobalValueSummary * |
Mehdi Amini | b4e1e82 | 2016-04-27 00:32:13 +0000 | [diff] [blame] | 155 | selectCallee(const ModuleSummaryIndex &Index, |
| 156 | const GlobalValueSummaryList &CalleeSummaryList, |
Teresa Johnson | 28e457b | 2016-04-24 14:57:11 +0000 | [diff] [blame] | 157 | unsigned Threshold) { |
Mehdi Amini | 01e3213 | 2016-03-26 05:40:34 +0000 | [diff] [blame] | 158 | auto It = llvm::find_if( |
Teresa Johnson | 28e457b | 2016-04-24 14:57:11 +0000 | [diff] [blame] | 159 | CalleeSummaryList, |
| 160 | [&](const std::unique_ptr<GlobalValueSummary> &SummaryPtr) { |
| 161 | auto *GVSummary = SummaryPtr.get(); |
Rafael Espindola | f329be8 | 2016-05-11 01:26:06 +0000 | [diff] [blame] | 162 | if (GlobalValue::isInterposableLinkage(GVSummary->linkage())) |
Mehdi Amini | 5b85d8d | 2016-05-03 00:27:28 +0000 | [diff] [blame] | 163 | // There is no point in importing these, we can't inline them |
Mehdi Amini | 01e3213 | 2016-03-26 05:40:34 +0000 | [diff] [blame] | 164 | return false; |
Mehdi Amini | 2c719cc | 2016-04-20 04:17:36 +0000 | [diff] [blame] | 165 | if (auto *AS = dyn_cast<AliasSummary>(GVSummary)) { |
| 166 | GVSummary = &AS->getAliasee(); |
| 167 | // Alias can't point to "available_externally". However when we import |
| 168 | // linkOnceODR the linkage does not change. So we import the alias |
| 169 | // and aliasee only in this case. |
| 170 | // FIXME: we should import alias as available_externally *function*, |
| 171 | // the destination module does need to know it is an alias. |
| 172 | if (!GlobalValue::isLinkOnceODRLinkage(GVSummary->linkage())) |
| 173 | return false; |
| 174 | } |
| 175 | |
| 176 | auto *Summary = cast<FunctionSummary>(GVSummary); |
Mehdi Amini | 7e88d0d | 2015-12-09 08:17:35 +0000 | [diff] [blame] | 177 | |
Mehdi Amini | 01e3213 | 2016-03-26 05:40:34 +0000 | [diff] [blame] | 178 | if (Summary->instCount() > Threshold) |
| 179 | return false; |
Mehdi Amini | 7e88d0d | 2015-12-09 08:17:35 +0000 | [diff] [blame] | 180 | |
Mehdi Amini | b4e1e82 | 2016-04-27 00:32:13 +0000 | [diff] [blame] | 181 | if (!eligibleForImport(Index, *Summary)) |
| 182 | return false; |
| 183 | |
Mehdi Amini | 01e3213 | 2016-03-26 05:40:34 +0000 | [diff] [blame] | 184 | return true; |
| 185 | }); |
Teresa Johnson | 28e457b | 2016-04-24 14:57:11 +0000 | [diff] [blame] | 186 | if (It == CalleeSummaryList.end()) |
Mehdi Amini | 01e3213 | 2016-03-26 05:40:34 +0000 | [diff] [blame] | 187 | return nullptr; |
Mehdi Amini | 7e88d0d | 2015-12-09 08:17:35 +0000 | [diff] [blame] | 188 | |
Teresa Johnson | 28e457b | 2016-04-24 14:57:11 +0000 | [diff] [blame] | 189 | return cast<GlobalValueSummary>(It->get()); |
Mehdi Amini | 7e88d0d | 2015-12-09 08:17:35 +0000 | [diff] [blame] | 190 | } |
Mehdi Amini | 7e88d0d | 2015-12-09 08:17:35 +0000 | [diff] [blame] | 191 | |
Mehdi Amini | 01e3213 | 2016-03-26 05:40:34 +0000 | [diff] [blame] | 192 | /// Return the summary for the function \p GUID that fits the \p Threshold, or |
| 193 | /// null if there's no match. |
Mehdi Amini | 2d28f7a | 2016-04-16 06:56:44 +0000 | [diff] [blame] | 194 | static const GlobalValueSummary *selectCallee(GlobalValue::GUID GUID, |
| 195 | unsigned Threshold, |
| 196 | const ModuleSummaryIndex &Index) { |
Teresa Johnson | 28e457b | 2016-04-24 14:57:11 +0000 | [diff] [blame] | 197 | auto CalleeSummaryList = Index.findGlobalValueSummaryList(GUID); |
Mehdi Amini | b4e1e82 | 2016-04-27 00:32:13 +0000 | [diff] [blame] | 198 | if (CalleeSummaryList == Index.end()) |
Mehdi Amini | 01e3213 | 2016-03-26 05:40:34 +0000 | [diff] [blame] | 199 | return nullptr; // This function does not have a summary |
Mehdi Amini | b4e1e82 | 2016-04-27 00:32:13 +0000 | [diff] [blame] | 200 | return selectCallee(Index, CalleeSummaryList->second, Threshold); |
Mehdi Amini | 01e3213 | 2016-03-26 05:40:34 +0000 | [diff] [blame] | 201 | } |
Mehdi Amini | 7e88d0d | 2015-12-09 08:17:35 +0000 | [diff] [blame] | 202 | |
Mehdi Amini | cb87494 | 2016-04-23 23:29:24 +0000 | [diff] [blame] | 203 | /// Mark the global \p GUID as export by module \p ExportModulePath if found in |
| 204 | /// this module. If it is a GlobalVariable, we also mark any referenced global |
| 205 | /// in the current module as exported. |
| 206 | static void exportGlobalInModule(const ModuleSummaryIndex &Index, |
| 207 | StringRef ExportModulePath, |
| 208 | GlobalValue::GUID GUID, |
| 209 | FunctionImporter::ExportSetTy &ExportList) { |
Teresa Johnson | 28e457b | 2016-04-24 14:57:11 +0000 | [diff] [blame] | 210 | auto FindGlobalSummaryInModule = |
| 211 | [&](GlobalValue::GUID GUID) -> GlobalValueSummary *{ |
| 212 | auto SummaryList = Index.findGlobalValueSummaryList(GUID); |
| 213 | if (SummaryList == Index.end()) |
Mehdi Amini | cb87494 | 2016-04-23 23:29:24 +0000 | [diff] [blame] | 214 | // This global does not have a summary, it is not part of the ThinLTO |
| 215 | // process |
| 216 | return nullptr; |
Teresa Johnson | 28e457b | 2016-04-24 14:57:11 +0000 | [diff] [blame] | 217 | auto SummaryIter = llvm::find_if( |
| 218 | SummaryList->second, |
| 219 | [&](const std::unique_ptr<GlobalValueSummary> &Summary) { |
Mehdi Amini | cb87494 | 2016-04-23 23:29:24 +0000 | [diff] [blame] | 220 | return Summary->modulePath() == ExportModulePath; |
| 221 | }); |
Teresa Johnson | 28e457b | 2016-04-24 14:57:11 +0000 | [diff] [blame] | 222 | if (SummaryIter == SummaryList->second.end()) |
Mehdi Amini | cb87494 | 2016-04-23 23:29:24 +0000 | [diff] [blame] | 223 | return nullptr; |
Teresa Johnson | 28e457b | 2016-04-24 14:57:11 +0000 | [diff] [blame] | 224 | return SummaryIter->get(); |
Mehdi Amini | cb87494 | 2016-04-23 23:29:24 +0000 | [diff] [blame] | 225 | }; |
| 226 | |
Teresa Johnson | 28e457b | 2016-04-24 14:57:11 +0000 | [diff] [blame] | 227 | auto *Summary = FindGlobalSummaryInModule(GUID); |
| 228 | if (!Summary) |
Mehdi Amini | cb87494 | 2016-04-23 23:29:24 +0000 | [diff] [blame] | 229 | return; |
| 230 | // We found it in the current module, mark as exported |
| 231 | ExportList.insert(GUID); |
| 232 | |
Mehdi Amini | cb87494 | 2016-04-23 23:29:24 +0000 | [diff] [blame] | 233 | auto GVS = dyn_cast<GlobalVarSummary>(Summary); |
| 234 | if (!GVS) |
| 235 | return; |
| 236 | // FunctionImportGlobalProcessing::doPromoteLocalToGlobal() will always |
| 237 | // trigger importing the initializer for `constant unnamed addr` globals that |
| 238 | // are referenced. We conservatively export all the referenced symbols for |
| 239 | // every global to workaround this, so that the ExportList is accurate. |
| 240 | // FIXME: with a "isConstant" flag in the summary we could be more targetted. |
| 241 | for (auto &Ref : GVS->refs()) { |
| 242 | auto GUID = Ref.getGUID(); |
Teresa Johnson | 28e457b | 2016-04-24 14:57:11 +0000 | [diff] [blame] | 243 | auto *RefSummary = FindGlobalSummaryInModule(GUID); |
| 244 | if (RefSummary) |
Mehdi Amini | cb87494 | 2016-04-23 23:29:24 +0000 | [diff] [blame] | 245 | // Found a ref in the current module, mark it as exported |
| 246 | ExportList.insert(GUID); |
| 247 | } |
Mehdi Amini | 01e3213 | 2016-03-26 05:40:34 +0000 | [diff] [blame] | 248 | } |
Mehdi Amini | 4064174 | 2016-02-10 23:31:45 +0000 | [diff] [blame] | 249 | |
Mehdi Amini | 01e3213 | 2016-03-26 05:40:34 +0000 | [diff] [blame] | 250 | using EdgeInfo = std::pair<const FunctionSummary *, unsigned /* Threshold */>; |
Mehdi Amini | 7e88d0d | 2015-12-09 08:17:35 +0000 | [diff] [blame] | 251 | |
Mehdi Amini | 01e3213 | 2016-03-26 05:40:34 +0000 | [diff] [blame] | 252 | /// Compute the list of functions to import for a given caller. Mark these |
| 253 | /// imported functions and the symbols they reference in their source module as |
| 254 | /// exported from their source module. |
| 255 | static void computeImportForFunction( |
Teresa Johnson | 3255eec | 2016-04-10 15:17:26 +0000 | [diff] [blame] | 256 | const FunctionSummary &Summary, const ModuleSummaryIndex &Index, |
Teresa Johnson | c851d21 | 2016-04-25 21:09:51 +0000 | [diff] [blame] | 257 | unsigned Threshold, const GVSummaryMapTy &DefinedGVSummaries, |
Mehdi Amini | 01e3213 | 2016-03-26 05:40:34 +0000 | [diff] [blame] | 258 | SmallVectorImpl<EdgeInfo> &Worklist, |
| 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 | for (auto &Edge : Summary.calls()) { |
Teresa Johnson | 2d5487c | 2016-04-11 13:58:45 +0000 | [diff] [blame] | 262 | auto GUID = Edge.first.getGUID(); |
Mehdi Amini | 01e3213 | 2016-03-26 05:40:34 +0000 | [diff] [blame] | 263 | DEBUG(dbgs() << " edge -> " << GUID << " Threshold:" << Threshold << "\n"); |
| 264 | |
Mehdi Amini | 1aafabf | 2016-04-16 07:02:16 +0000 | [diff] [blame] | 265 | if (DefinedGVSummaries.count(GUID)) { |
Mehdi Amini | 01e3213 | 2016-03-26 05:40:34 +0000 | [diff] [blame] | 266 | DEBUG(dbgs() << "ignored! Target already in destination module.\n"); |
| 267 | continue; |
Teresa Johnson | d450da3 | 2015-11-24 21:15:19 +0000 | [diff] [blame] | 268 | } |
Mehdi Amini | 01e3213 | 2016-03-26 05:40:34 +0000 | [diff] [blame] | 269 | |
| 270 | auto *CalleeSummary = selectCallee(GUID, Threshold, Index); |
| 271 | if (!CalleeSummary) { |
| 272 | DEBUG(dbgs() << "ignored! No qualifying callee with summary found.\n"); |
| 273 | continue; |
| 274 | } |
Mehdi Amini | 2d28f7a | 2016-04-16 06:56:44 +0000 | [diff] [blame] | 275 | // "Resolve" the summary, traversing alias, |
| 276 | const FunctionSummary *ResolvedCalleeSummary; |
Mehdi Amini | 6968ef7 | 2016-04-20 01:04:20 +0000 | [diff] [blame] | 277 | if (isa<AliasSummary>(CalleeSummary)) { |
Mehdi Amini | 2d28f7a | 2016-04-16 06:56:44 +0000 | [diff] [blame] | 278 | ResolvedCalleeSummary = cast<FunctionSummary>( |
| 279 | &cast<AliasSummary>(CalleeSummary)->getAliasee()); |
Mehdi Amini | 2c719cc | 2016-04-20 04:17:36 +0000 | [diff] [blame] | 280 | assert( |
| 281 | GlobalValue::isLinkOnceODRLinkage(ResolvedCalleeSummary->linkage()) && |
| 282 | "Unexpected alias to a non-linkonceODR in import list"); |
Mehdi Amini | 6968ef7 | 2016-04-20 01:04:20 +0000 | [diff] [blame] | 283 | } else |
Mehdi Amini | 2d28f7a | 2016-04-16 06:56:44 +0000 | [diff] [blame] | 284 | ResolvedCalleeSummary = cast<FunctionSummary>(CalleeSummary); |
| 285 | |
| 286 | assert(ResolvedCalleeSummary->instCount() <= Threshold && |
Mehdi Amini | 01e3213 | 2016-03-26 05:40:34 +0000 | [diff] [blame] | 287 | "selectCallee() didn't honor the threshold"); |
| 288 | |
Mehdi Amini | 2d28f7a | 2016-04-16 06:56:44 +0000 | [diff] [blame] | 289 | auto ExportModulePath = ResolvedCalleeSummary->modulePath(); |
| 290 | auto &ProcessedThreshold = ImportsForModule[ExportModulePath][GUID]; |
Mehdi Amini | 01e3213 | 2016-03-26 05:40:34 +0000 | [diff] [blame] | 291 | /// Since the traversal of the call graph is DFS, we can revisit a function |
| 292 | /// a second time with a higher threshold. In this case, it is added back to |
| 293 | /// the worklist with the new threshold. |
Teresa Johnson | 2e03094 | 2016-05-11 22:56:19 +0000 | [diff] [blame] | 294 | if (ProcessedThreshold && ProcessedThreshold >= Threshold) { |
Mehdi Amini | 01e3213 | 2016-03-26 05:40:34 +0000 | [diff] [blame] | 295 | DEBUG(dbgs() << "ignored! Target was already seen with Threshold " |
| 296 | << ProcessedThreshold << "\n"); |
| 297 | continue; |
| 298 | } |
| 299 | // Mark this function as imported in this module, with the current Threshold |
| 300 | ProcessedThreshold = Threshold; |
| 301 | |
| 302 | // Make exports in the source module. |
Teresa Johnson | c86af33 | 2016-04-12 21:13:11 +0000 | [diff] [blame] | 303 | if (ExportLists) { |
Mehdi Amini | ef7555f | 2016-04-13 01:52:32 +0000 | [diff] [blame] | 304 | auto &ExportList = (*ExportLists)[ExportModulePath]; |
Teresa Johnson | c86af33 | 2016-04-12 21:13:11 +0000 | [diff] [blame] | 305 | ExportList.insert(GUID); |
| 306 | // Mark all functions and globals referenced by this function as exported |
| 307 | // to the outside if they are defined in the same source module. |
Mehdi Amini | 2d28f7a | 2016-04-16 06:56:44 +0000 | [diff] [blame] | 308 | for (auto &Edge : ResolvedCalleeSummary->calls()) { |
Teresa Johnson | c86af33 | 2016-04-12 21:13:11 +0000 | [diff] [blame] | 309 | auto CalleeGUID = Edge.first.getGUID(); |
Mehdi Amini | cb87494 | 2016-04-23 23:29:24 +0000 | [diff] [blame] | 310 | exportGlobalInModule(Index, ExportModulePath, CalleeGUID, ExportList); |
Teresa Johnson | c86af33 | 2016-04-12 21:13:11 +0000 | [diff] [blame] | 311 | } |
Mehdi Amini | 2d28f7a | 2016-04-16 06:56:44 +0000 | [diff] [blame] | 312 | for (auto &Ref : ResolvedCalleeSummary->refs()) { |
Teresa Johnson | c86af33 | 2016-04-12 21:13:11 +0000 | [diff] [blame] | 313 | auto GUID = Ref.getGUID(); |
Mehdi Amini | cb87494 | 2016-04-23 23:29:24 +0000 | [diff] [blame] | 314 | exportGlobalInModule(Index, ExportModulePath, GUID, ExportList); |
Teresa Johnson | c86af33 | 2016-04-12 21:13:11 +0000 | [diff] [blame] | 315 | } |
Mehdi Amini | 01e3213 | 2016-03-26 05:40:34 +0000 | [diff] [blame] | 316 | } |
| 317 | |
| 318 | // Insert the newly imported function to the worklist. |
Mehdi Amini | 2d28f7a | 2016-04-16 06:56:44 +0000 | [diff] [blame] | 319 | Worklist.push_back(std::make_pair(ResolvedCalleeSummary, Threshold)); |
Teresa Johnson | d450da3 | 2015-11-24 21:15:19 +0000 | [diff] [blame] | 320 | } |
| 321 | } |
| 322 | |
Mehdi Amini | 01e3213 | 2016-03-26 05:40:34 +0000 | [diff] [blame] | 323 | /// Given the list of globals defined in a module, compute the list of imports |
| 324 | /// as well as the list of "exports", i.e. the list of symbols referenced from |
| 325 | /// another module (that may require promotion). |
| 326 | static void ComputeImportForModule( |
Teresa Johnson | c851d21 | 2016-04-25 21:09:51 +0000 | [diff] [blame] | 327 | const GVSummaryMapTy &DefinedGVSummaries, const ModuleSummaryIndex &Index, |
Mehdi Amini | 01e3213 | 2016-03-26 05:40:34 +0000 | [diff] [blame] | 328 | FunctionImporter::ImportMapTy &ImportsForModule, |
Teresa Johnson | c86af33 | 2016-04-12 21:13:11 +0000 | [diff] [blame] | 329 | StringMap<FunctionImporter::ExportSetTy> *ExportLists = nullptr) { |
Mehdi Amini | 01e3213 | 2016-03-26 05:40:34 +0000 | [diff] [blame] | 330 | // Worklist contains the list of function imported in this module, for which |
| 331 | // we will analyse the callees and may import further down the callgraph. |
| 332 | SmallVector<EdgeInfo, 128> Worklist; |
| 333 | |
| 334 | // Populate the worklist with the import for the functions in the current |
| 335 | // module |
Teresa Johnson | 28e457b | 2016-04-24 14:57:11 +0000 | [diff] [blame] | 336 | for (auto &GVSummary : DefinedGVSummaries) { |
| 337 | auto *Summary = GVSummary.second; |
Mehdi Amini | 2d28f7a | 2016-04-16 06:56:44 +0000 | [diff] [blame] | 338 | if (auto *AS = dyn_cast<AliasSummary>(Summary)) |
| 339 | Summary = &AS->getAliasee(); |
Mehdi Amini | 1aafabf | 2016-04-16 07:02:16 +0000 | [diff] [blame] | 340 | auto *FuncSummary = dyn_cast<FunctionSummary>(Summary); |
| 341 | if (!FuncSummary) |
| 342 | // Skip import for global variables |
| 343 | continue; |
Teresa Johnson | 28e457b | 2016-04-24 14:57:11 +0000 | [diff] [blame] | 344 | DEBUG(dbgs() << "Initalize import for " << GVSummary.first << "\n"); |
Mehdi Amini | 2d28f7a | 2016-04-16 06:56:44 +0000 | [diff] [blame] | 345 | computeImportForFunction(*FuncSummary, Index, ImportInstrLimit, |
Mehdi Amini | 1aafabf | 2016-04-16 07:02:16 +0000 | [diff] [blame] | 346 | DefinedGVSummaries, Worklist, ImportsForModule, |
Mehdi Amini | 01e3213 | 2016-03-26 05:40:34 +0000 | [diff] [blame] | 347 | ExportLists); |
| 348 | } |
| 349 | |
Mehdi Amini | 42418ab | 2015-11-24 06:07:49 +0000 | [diff] [blame] | 350 | while (!Worklist.empty()) { |
Mehdi Amini | 01e3213 | 2016-03-26 05:40:34 +0000 | [diff] [blame] | 351 | auto FuncInfo = Worklist.pop_back_val(); |
| 352 | auto *Summary = FuncInfo.first; |
| 353 | auto Threshold = FuncInfo.second; |
Mehdi Amini | 42418ab | 2015-11-24 06:07:49 +0000 | [diff] [blame] | 354 | |
Mehdi Amini | 7e88d0d | 2015-12-09 08:17:35 +0000 | [diff] [blame] | 355 | // Process the newly imported functions and add callees to the worklist. |
Mehdi Amini | 4064174 | 2016-02-10 23:31:45 +0000 | [diff] [blame] | 356 | // Adjust the threshold |
| 357 | Threshold = Threshold * ImportInstrFactor; |
Mehdi Amini | 01e3213 | 2016-03-26 05:40:34 +0000 | [diff] [blame] | 358 | |
Mehdi Amini | 1aafabf | 2016-04-16 07:02:16 +0000 | [diff] [blame] | 359 | computeImportForFunction(*Summary, Index, Threshold, DefinedGVSummaries, |
Teresa Johnson | 3255eec | 2016-04-10 15:17:26 +0000 | [diff] [blame] | 360 | Worklist, ImportsForModule, ExportLists); |
Mehdi Amini | 42418ab | 2015-11-24 06:07:49 +0000 | [diff] [blame] | 361 | } |
Mehdi Amini | c8c5517 | 2015-12-03 02:37:33 +0000 | [diff] [blame] | 362 | } |
Mehdi Amini | ffe2e4a | 2015-12-02 04:34:28 +0000 | [diff] [blame] | 363 | |
Mehdi Amini | 01e3213 | 2016-03-26 05:40:34 +0000 | [diff] [blame] | 364 | } // anonymous namespace |
| 365 | |
Teresa Johnson | c86af33 | 2016-04-12 21:13:11 +0000 | [diff] [blame] | 366 | /// Compute all the import and export for every module using the Index. |
Mehdi Amini | 01e3213 | 2016-03-26 05:40:34 +0000 | [diff] [blame] | 367 | void llvm::ComputeCrossModuleImport( |
| 368 | const ModuleSummaryIndex &Index, |
Teresa Johnson | c851d21 | 2016-04-25 21:09:51 +0000 | [diff] [blame] | 369 | const StringMap<GVSummaryMapTy> &ModuleToDefinedGVSummaries, |
Mehdi Amini | 01e3213 | 2016-03-26 05:40:34 +0000 | [diff] [blame] | 370 | StringMap<FunctionImporter::ImportMapTy> &ImportLists, |
| 371 | StringMap<FunctionImporter::ExportSetTy> &ExportLists) { |
Mehdi Amini | 01e3213 | 2016-03-26 05:40:34 +0000 | [diff] [blame] | 372 | // For each module that has function defined, compute the import/export lists. |
Mehdi Amini | 1aafabf | 2016-04-16 07:02:16 +0000 | [diff] [blame] | 373 | for (auto &DefinedGVSummaries : ModuleToDefinedGVSummaries) { |
| 374 | auto &ImportsForModule = ImportLists[DefinedGVSummaries.first()]; |
| 375 | DEBUG(dbgs() << "Computing import for Module '" |
| 376 | << DefinedGVSummaries.first() << "'\n"); |
| 377 | ComputeImportForModule(DefinedGVSummaries.second, Index, ImportsForModule, |
Teresa Johnson | c86af33 | 2016-04-12 21:13:11 +0000 | [diff] [blame] | 378 | &ExportLists); |
Mehdi Amini | 01e3213 | 2016-03-26 05:40:34 +0000 | [diff] [blame] | 379 | } |
| 380 | |
| 381 | #ifndef NDEBUG |
| 382 | DEBUG(dbgs() << "Import/Export lists for " << ImportLists.size() |
| 383 | << " modules:\n"); |
| 384 | for (auto &ModuleImports : ImportLists) { |
| 385 | auto ModName = ModuleImports.first(); |
| 386 | auto &Exports = ExportLists[ModName]; |
| 387 | DEBUG(dbgs() << "* Module " << ModName << " exports " << Exports.size() |
| 388 | << " functions. Imports from " << ModuleImports.second.size() |
| 389 | << " modules.\n"); |
| 390 | for (auto &Src : ModuleImports.second) { |
| 391 | auto SrcModName = Src.first(); |
| 392 | DEBUG(dbgs() << " - " << Src.second.size() << " functions imported from " |
| 393 | << SrcModName << "\n"); |
| 394 | } |
| 395 | } |
| 396 | #endif |
| 397 | } |
| 398 | |
Teresa Johnson | c86af33 | 2016-04-12 21:13:11 +0000 | [diff] [blame] | 399 | /// Compute all the imports for the given module in the Index. |
| 400 | void llvm::ComputeCrossModuleImportForModule( |
| 401 | StringRef ModulePath, const ModuleSummaryIndex &Index, |
| 402 | FunctionImporter::ImportMapTy &ImportList) { |
| 403 | |
| 404 | // Collect the list of functions this module defines. |
| 405 | // GUID -> Summary |
Teresa Johnson | c851d21 | 2016-04-25 21:09:51 +0000 | [diff] [blame] | 406 | GVSummaryMapTy FunctionSummaryMap; |
Teresa Johnson | 28e457b | 2016-04-24 14:57:11 +0000 | [diff] [blame] | 407 | Index.collectDefinedFunctionsForModule(ModulePath, FunctionSummaryMap); |
Teresa Johnson | c86af33 | 2016-04-12 21:13:11 +0000 | [diff] [blame] | 408 | |
| 409 | // Compute the import list for this module. |
| 410 | DEBUG(dbgs() << "Computing import for Module '" << ModulePath << "'\n"); |
Teresa Johnson | 28e457b | 2016-04-24 14:57:11 +0000 | [diff] [blame] | 411 | ComputeImportForModule(FunctionSummaryMap, Index, ImportList); |
Teresa Johnson | c86af33 | 2016-04-12 21:13:11 +0000 | [diff] [blame] | 412 | |
| 413 | #ifndef NDEBUG |
| 414 | DEBUG(dbgs() << "* Module " << ModulePath << " imports from " |
| 415 | << ImportList.size() << " modules.\n"); |
| 416 | for (auto &Src : ImportList) { |
| 417 | auto SrcModName = Src.first(); |
| 418 | DEBUG(dbgs() << " - " << Src.second.size() << " functions imported from " |
| 419 | << SrcModName << "\n"); |
| 420 | } |
| 421 | #endif |
| 422 | } |
| 423 | |
Teresa Johnson | 84174c3 | 2016-05-10 13:48:23 +0000 | [diff] [blame] | 424 | /// Compute the set of summaries needed for a ThinLTO backend compilation of |
| 425 | /// \p ModulePath. |
| 426 | void llvm::gatherImportedSummariesForModule( |
| 427 | StringRef ModulePath, |
| 428 | const StringMap<GVSummaryMapTy> &ModuleToDefinedGVSummaries, |
| 429 | const StringMap<FunctionImporter::ImportMapTy> &ImportLists, |
| 430 | std::map<std::string, GVSummaryMapTy> &ModuleToSummariesForIndex) { |
| 431 | // Include all summaries from the importing module. |
| 432 | ModuleToSummariesForIndex[ModulePath] = |
| 433 | ModuleToDefinedGVSummaries.lookup(ModulePath); |
| 434 | auto ModuleImports = ImportLists.find(ModulePath); |
| 435 | if (ModuleImports != ImportLists.end()) { |
| 436 | // Include summaries for imports. |
| 437 | for (auto &ILI : ModuleImports->second) { |
| 438 | auto &SummariesForIndex = ModuleToSummariesForIndex[ILI.first()]; |
| 439 | const auto &DefinedGVSummaries = |
| 440 | ModuleToDefinedGVSummaries.lookup(ILI.first()); |
| 441 | for (auto &GI : ILI.second) { |
| 442 | const auto &DS = DefinedGVSummaries.find(GI.first); |
| 443 | assert(DS != DefinedGVSummaries.end() && |
| 444 | "Expected a defined summary for imported global value"); |
| 445 | SummariesForIndex[GI.first] = DS->second; |
| 446 | } |
| 447 | } |
| 448 | } |
| 449 | } |
| 450 | |
Teresa Johnson | 8570fe4 | 2016-05-10 15:54:09 +0000 | [diff] [blame] | 451 | /// Emit the files \p ModulePath will import from into \p OutputFilename. |
| 452 | std::error_code llvm::EmitImportsFiles( |
| 453 | StringRef ModulePath, StringRef OutputFilename, |
| 454 | const StringMap<FunctionImporter::ImportMapTy> &ImportLists) { |
| 455 | auto ModuleImports = ImportLists.find(ModulePath); |
| 456 | std::error_code EC; |
| 457 | raw_fd_ostream ImportsOS(OutputFilename, EC, sys::fs::OpenFlags::F_None); |
| 458 | if (EC) |
| 459 | return EC; |
| 460 | if (ModuleImports != ImportLists.end()) |
| 461 | for (auto &ILI : ModuleImports->second) |
| 462 | ImportsOS << ILI.first() << "\n"; |
| 463 | return std::error_code(); |
| 464 | } |
| 465 | |
Teresa Johnson | 04c9a2d | 2016-05-25 14:03:11 +0000 | [diff] [blame] | 466 | /// Fixup WeakForLinker linkages in \p TheModule based on summary analysis. |
| 467 | void llvm::thinLTOResolveWeakForLinkerModule( |
| 468 | Module &TheModule, const GVSummaryMapTy &DefinedGlobals) { |
| 469 | auto updateLinkage = [&](GlobalValue &GV) { |
| 470 | if (!GlobalValue::isWeakForLinker(GV.getLinkage())) |
| 471 | return; |
| 472 | // See if the global summary analysis computed a new resolved linkage. |
| 473 | const auto &GS = DefinedGlobals.find(GV.getGUID()); |
| 474 | if (GS == DefinedGlobals.end()) |
| 475 | return; |
| 476 | auto NewLinkage = GS->second->linkage(); |
| 477 | if (NewLinkage == GV.getLinkage()) |
| 478 | return; |
| 479 | DEBUG(dbgs() << "ODR fixing up linkage for `" << GV.getName() << "` from " |
| 480 | << GV.getLinkage() << " to " << NewLinkage << "\n"); |
| 481 | GV.setLinkage(NewLinkage); |
| 482 | }; |
| 483 | |
| 484 | // Process functions and global now |
| 485 | for (auto &GV : TheModule) |
| 486 | updateLinkage(GV); |
| 487 | for (auto &GV : TheModule.globals()) |
| 488 | updateLinkage(GV); |
| 489 | for (auto &GV : TheModule.aliases()) |
| 490 | updateLinkage(GV); |
| 491 | } |
| 492 | |
| 493 | /// Run internalization on \p TheModule based on symmary analysis. |
| 494 | void llvm::thinLTOInternalizeModule(Module &TheModule, |
| 495 | const GVSummaryMapTy &DefinedGlobals) { |
| 496 | // Parse inline ASM and collect the list of symbols that are not defined in |
| 497 | // the current module. |
| 498 | StringSet<> AsmUndefinedRefs; |
| 499 | object::IRObjectFile::CollectAsmUndefinedRefs( |
| 500 | Triple(TheModule.getTargetTriple()), TheModule.getModuleInlineAsm(), |
| 501 | [&AsmUndefinedRefs](StringRef Name, object::BasicSymbolRef::Flags Flags) { |
| 502 | if (Flags & object::BasicSymbolRef::SF_Undefined) |
| 503 | AsmUndefinedRefs.insert(Name); |
| 504 | }); |
| 505 | |
| 506 | // Declare a callback for the internalize pass that will ask for every |
| 507 | // candidate GlobalValue if it can be internalized or not. |
| 508 | auto MustPreserveGV = [&](const GlobalValue &GV) -> bool { |
| 509 | // Can't be internalized if referenced in inline asm. |
| 510 | if (AsmUndefinedRefs.count(GV.getName())) |
| 511 | return true; |
| 512 | |
| 513 | // Lookup the linkage recorded in the summaries during global analysis. |
| 514 | const auto &GS = DefinedGlobals.find(GV.getGUID()); |
| 515 | GlobalValue::LinkageTypes Linkage; |
| 516 | if (GS == DefinedGlobals.end()) { |
| 517 | // Must have been promoted (possibly conservatively). Find original |
| 518 | // name so that we can access the correct summary and see if it can |
| 519 | // be internalized again. |
| 520 | // FIXME: Eventually we should control promotion instead of promoting |
| 521 | // and internalizing again. |
| 522 | StringRef OrigName = |
| 523 | ModuleSummaryIndex::getOriginalNameBeforePromote(GV.getName()); |
| 524 | std::string OrigId = GlobalValue::getGlobalIdentifier( |
| 525 | OrigName, GlobalValue::InternalLinkage, |
| 526 | TheModule.getSourceFileName()); |
| 527 | const auto &GS = DefinedGlobals.find(GlobalValue::getGUID(OrigId)); |
Teresa Johnson | 7ab1f69 | 2016-06-09 01:14:13 +0000 | [diff] [blame] | 528 | if (GS == DefinedGlobals.end()) { |
| 529 | // Also check the original non-promoted non-globalized name. In some |
| 530 | // cases a preempted weak value is linked in as a local copy because |
| 531 | // it is referenced by an alias (IRLinker::linkGlobalValueProto). |
| 532 | // In that case, since it was originally not a local value, it was |
| 533 | // recorded in the index using the original name. |
| 534 | // FIXME: This may not be needed once PR27866 is fixed. |
| 535 | const auto &GS = DefinedGlobals.find(GlobalValue::getGUID(OrigName)); |
| 536 | assert(GS != DefinedGlobals.end()); |
| 537 | Linkage = GS->second->linkage(); |
| 538 | } else { |
| 539 | Linkage = GS->second->linkage(); |
| 540 | } |
Teresa Johnson | 04c9a2d | 2016-05-25 14:03:11 +0000 | [diff] [blame] | 541 | } else |
| 542 | Linkage = GS->second->linkage(); |
| 543 | return !GlobalValue::isLocalLinkage(Linkage); |
| 544 | }; |
| 545 | |
| 546 | // FIXME: See if we can just internalize directly here via linkage changes |
| 547 | // based on the index, rather than invoking internalizeModule. |
| 548 | llvm::internalizeModule(TheModule, MustPreserveGV); |
| 549 | } |
| 550 | |
Mehdi Amini | c8c5517 | 2015-12-03 02:37:33 +0000 | [diff] [blame] | 551 | // Automatically import functions in Module \p DestModule based on the summaries |
| 552 | // index. |
| 553 | // |
Mehdi Amini | 01e3213 | 2016-03-26 05:40:34 +0000 | [diff] [blame] | 554 | bool FunctionImporter::importFunctions( |
Mehdi Amini | bda3c97 | 2016-04-21 01:59:39 +0000 | [diff] [blame] | 555 | Module &DestModule, const FunctionImporter::ImportMapTy &ImportList, |
| 556 | bool ForceImportReferencedDiscardableSymbols) { |
Mehdi Amini | 5411d05 | 2015-12-08 23:04:19 +0000 | [diff] [blame] | 557 | DEBUG(dbgs() << "Starting import for Module " |
Mehdi Amini | 311fef6 | 2015-12-03 02:58:14 +0000 | [diff] [blame] | 558 | << DestModule.getModuleIdentifier() << "\n"); |
Mehdi Amini | c8c5517 | 2015-12-03 02:37:33 +0000 | [diff] [blame] | 559 | unsigned ImportedCount = 0; |
| 560 | |
Mehdi Amini | c8c5517 | 2015-12-03 02:37:33 +0000 | [diff] [blame] | 561 | // Linker that will be used for importing function |
Rafael Espindola | 9d2bfc4 | 2015-12-14 23:17:03 +0000 | [diff] [blame] | 562 | Linker TheLinker(DestModule); |
Mehdi Amini | 7e88d0d | 2015-12-09 08:17:35 +0000 | [diff] [blame] | 563 | // Do the actual import of functions now, one Module at a time |
Mehdi Amini | 01e3213 | 2016-03-26 05:40:34 +0000 | [diff] [blame] | 564 | std::set<StringRef> ModuleNameOrderedList; |
| 565 | for (auto &FunctionsToImportPerModule : ImportList) { |
| 566 | ModuleNameOrderedList.insert(FunctionsToImportPerModule.first()); |
| 567 | } |
| 568 | for (auto &Name : ModuleNameOrderedList) { |
Mehdi Amini | 7e88d0d | 2015-12-09 08:17:35 +0000 | [diff] [blame] | 569 | // Get the module for the import |
Mehdi Amini | 01e3213 | 2016-03-26 05:40:34 +0000 | [diff] [blame] | 570 | const auto &FunctionsToImportPerModule = ImportList.find(Name); |
| 571 | assert(FunctionsToImportPerModule != ImportList.end()); |
| 572 | std::unique_ptr<Module> SrcModule = ModuleLoader(Name); |
Mehdi Amini | 7e88d0d | 2015-12-09 08:17:35 +0000 | [diff] [blame] | 573 | assert(&DestModule.getContext() == &SrcModule->getContext() && |
| 574 | "Context mismatch"); |
| 575 | |
Teresa Johnson | 6cba37c | 2016-01-22 00:15:53 +0000 | [diff] [blame] | 576 | // If modules were created with lazy metadata loading, materialize it |
| 577 | // now, before linking it (otherwise this will be a noop). |
| 578 | SrcModule->materializeMetadata(); |
| 579 | UpgradeDebugInfo(*SrcModule); |
Teresa Johnson | e5a6191 | 2015-12-17 17:14:09 +0000 | [diff] [blame] | 580 | |
Mehdi Amini | 01e3213 | 2016-03-26 05:40:34 +0000 | [diff] [blame] | 581 | auto &ImportGUIDs = FunctionsToImportPerModule->second; |
| 582 | // Find the globals to import |
| 583 | DenseSet<const GlobalValue *> GlobalsToImport; |
| 584 | for (auto &GV : *SrcModule) { |
Teresa Johnson | 0beb858 | 2016-04-04 18:52:23 +0000 | [diff] [blame] | 585 | if (!GV.hasName()) |
| 586 | continue; |
| 587 | auto GUID = GV.getGUID(); |
| 588 | auto Import = ImportGUIDs.count(GUID); |
Mehdi Amini | aeb1e59 | 2016-04-19 09:21:30 +0000 | [diff] [blame] | 589 | DEBUG(dbgs() << (Import ? "Is" : "Not") << " importing function " << GUID |
| 590 | << " " << GV.getName() << " from " |
| 591 | << SrcModule->getSourceFileName() << "\n"); |
Teresa Johnson | 0beb858 | 2016-04-04 18:52:23 +0000 | [diff] [blame] | 592 | if (Import) { |
Mehdi Amini | 01e3213 | 2016-03-26 05:40:34 +0000 | [diff] [blame] | 593 | GV.materialize(); |
| 594 | GlobalsToImport.insert(&GV); |
| 595 | } |
| 596 | } |
Mehdi Amini | 2d28f7a | 2016-04-16 06:56:44 +0000 | [diff] [blame] | 597 | for (auto &GV : SrcModule->globals()) { |
| 598 | if (!GV.hasName()) |
| 599 | continue; |
| 600 | auto GUID = GV.getGUID(); |
| 601 | auto Import = ImportGUIDs.count(GUID); |
Mehdi Amini | aeb1e59 | 2016-04-19 09:21:30 +0000 | [diff] [blame] | 602 | DEBUG(dbgs() << (Import ? "Is" : "Not") << " importing global " << GUID |
| 603 | << " " << GV.getName() << " from " |
| 604 | << SrcModule->getSourceFileName() << "\n"); |
Mehdi Amini | 2d28f7a | 2016-04-16 06:56:44 +0000 | [diff] [blame] | 605 | if (Import) { |
| 606 | GV.materialize(); |
| 607 | GlobalsToImport.insert(&GV); |
| 608 | } |
| 609 | } |
Mehdi Amini | 01e3213 | 2016-03-26 05:40:34 +0000 | [diff] [blame] | 610 | for (auto &GV : SrcModule->aliases()) { |
| 611 | if (!GV.hasName()) |
| 612 | continue; |
| 613 | auto GUID = GV.getGUID(); |
Teresa Johnson | 0beb858 | 2016-04-04 18:52:23 +0000 | [diff] [blame] | 614 | auto Import = ImportGUIDs.count(GUID); |
Mehdi Amini | aeb1e59 | 2016-04-19 09:21:30 +0000 | [diff] [blame] | 615 | DEBUG(dbgs() << (Import ? "Is" : "Not") << " importing alias " << GUID |
| 616 | << " " << GV.getName() << " from " |
| 617 | << SrcModule->getSourceFileName() << "\n"); |
Teresa Johnson | 0beb858 | 2016-04-04 18:52:23 +0000 | [diff] [blame] | 618 | if (Import) { |
Mehdi Amini | 01e3213 | 2016-03-26 05:40:34 +0000 | [diff] [blame] | 619 | // Alias can't point to "available_externally". However when we import |
Teresa Johnson | 9aae395 | 2016-03-27 15:01:11 +0000 | [diff] [blame] | 620 | // linkOnceODR the linkage does not change. So we import the alias |
Mehdi Amini | 6968ef7 | 2016-04-20 01:04:20 +0000 | [diff] [blame] | 621 | // and aliasee only in this case. This has been handled by |
| 622 | // computeImportForFunction() |
Mehdi Amini | 2d28f7a | 2016-04-16 06:56:44 +0000 | [diff] [blame] | 623 | GlobalObject *GO = GV.getBaseObject(); |
Mehdi Amini | 6968ef7 | 2016-04-20 01:04:20 +0000 | [diff] [blame] | 624 | assert(GO->hasLinkOnceODRLinkage() && |
| 625 | "Unexpected alias to a non-linkonceODR in import list"); |
Mehdi Amini | 2d28f7a | 2016-04-16 06:56:44 +0000 | [diff] [blame] | 626 | #ifndef NDEBUG |
| 627 | if (!GlobalsToImport.count(GO)) |
| 628 | DEBUG(dbgs() << " alias triggers importing aliasee " << GO->getGUID() |
| 629 | << " " << GO->getName() << " from " |
| 630 | << SrcModule->getSourceFileName() << "\n"); |
| 631 | #endif |
| 632 | GO->materialize(); |
Mehdi Amini | 01e3213 | 2016-03-26 05:40:34 +0000 | [diff] [blame] | 633 | GlobalsToImport.insert(GO); |
Mehdi Amini | 01e3213 | 2016-03-26 05:40:34 +0000 | [diff] [blame] | 634 | GV.materialize(); |
| 635 | GlobalsToImport.insert(&GV); |
| 636 | } |
| 637 | } |
| 638 | |
Mehdi Amini | 7e88d0d | 2015-12-09 08:17:35 +0000 | [diff] [blame] | 639 | // Link in the specified functions. |
Mehdi Amini | 01e3213 | 2016-03-26 05:40:34 +0000 | [diff] [blame] | 640 | if (renameModuleForThinLTO(*SrcModule, Index, &GlobalsToImport)) |
Mehdi Amini | 8d05185 | 2016-03-19 00:40:31 +0000 | [diff] [blame] | 641 | return true; |
| 642 | |
Teresa Johnson | d29478f | 2016-03-27 15:27:30 +0000 | [diff] [blame] | 643 | if (PrintImports) { |
| 644 | for (const auto *GV : GlobalsToImport) |
| 645 | dbgs() << DestModule.getSourceFileName() << ": Import " << GV->getName() |
| 646 | << " from " << SrcModule->getSourceFileName() << "\n"; |
| 647 | } |
| 648 | |
Mehdi Amini | bda3c97 | 2016-04-21 01:59:39 +0000 | [diff] [blame] | 649 | // Instruct the linker that the client will take care of linkonce resolution |
| 650 | unsigned Flags = Linker::Flags::None; |
| 651 | if (!ForceImportReferencedDiscardableSymbols) |
| 652 | Flags |= Linker::Flags::DontForceLinkLinkonceODR; |
| 653 | |
| 654 | if (TheLinker.linkInModule(std::move(SrcModule), Flags, &GlobalsToImport)) |
Mehdi Amini | 7e88d0d | 2015-12-09 08:17:35 +0000 | [diff] [blame] | 655 | report_fatal_error("Function Import: link error"); |
| 656 | |
Mehdi Amini | 01e3213 | 2016-03-26 05:40:34 +0000 | [diff] [blame] | 657 | ImportedCount += GlobalsToImport.size(); |
Mehdi Amini | 7e88d0d | 2015-12-09 08:17:35 +0000 | [diff] [blame] | 658 | } |
Teresa Johnson | e5a6191 | 2015-12-17 17:14:09 +0000 | [diff] [blame] | 659 | |
Teresa Johnson | d29478f | 2016-03-27 15:27:30 +0000 | [diff] [blame] | 660 | NumImported += ImportedCount; |
| 661 | |
Mehdi Amini | 7e88d0d | 2015-12-09 08:17:35 +0000 | [diff] [blame] | 662 | DEBUG(dbgs() << "Imported " << ImportedCount << " functions for Module " |
Mehdi Amini | c8c5517 | 2015-12-03 02:37:33 +0000 | [diff] [blame] | 663 | << DestModule.getModuleIdentifier() << "\n"); |
| 664 | return ImportedCount; |
Mehdi Amini | 42418ab | 2015-11-24 06:07:49 +0000 | [diff] [blame] | 665 | } |
| 666 | |
| 667 | /// Summary file to use for function importing when using -function-import from |
| 668 | /// the command line. |
| 669 | static cl::opt<std::string> |
| 670 | SummaryFile("summary-file", |
| 671 | cl::desc("The summary file to use for function importing.")); |
| 672 | |
| 673 | static void diagnosticHandler(const DiagnosticInfo &DI) { |
| 674 | raw_ostream &OS = errs(); |
| 675 | DiagnosticPrinterRawOStream DP(OS); |
| 676 | DI.print(DP); |
| 677 | OS << '\n'; |
| 678 | } |
| 679 | |
Teresa Johnson | 26ab577 | 2016-03-15 00:04:37 +0000 | [diff] [blame] | 680 | /// 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] | 681 | /// index object if found, or nullptr if not. |
Teresa Johnson | 26ab577 | 2016-03-15 00:04:37 +0000 | [diff] [blame] | 682 | static std::unique_ptr<ModuleSummaryIndex> |
| 683 | getModuleSummaryIndexForFile(StringRef Path, std::string &Error, |
| 684 | DiagnosticHandlerFunction DiagnosticHandler) { |
Mehdi Amini | 42418ab | 2015-11-24 06:07:49 +0000 | [diff] [blame] | 685 | std::unique_ptr<MemoryBuffer> Buffer; |
| 686 | ErrorOr<std::unique_ptr<MemoryBuffer>> BufferOrErr = |
| 687 | MemoryBuffer::getFile(Path); |
| 688 | if (std::error_code EC = BufferOrErr.getError()) { |
| 689 | Error = EC.message(); |
| 690 | return nullptr; |
| 691 | } |
| 692 | Buffer = std::move(BufferOrErr.get()); |
Teresa Johnson | 26ab577 | 2016-03-15 00:04:37 +0000 | [diff] [blame] | 693 | ErrorOr<std::unique_ptr<object::ModuleSummaryIndexObjectFile>> ObjOrErr = |
| 694 | object::ModuleSummaryIndexObjectFile::create(Buffer->getMemBufferRef(), |
| 695 | DiagnosticHandler); |
Mehdi Amini | 42418ab | 2015-11-24 06:07:49 +0000 | [diff] [blame] | 696 | if (std::error_code EC = ObjOrErr.getError()) { |
| 697 | Error = EC.message(); |
| 698 | return nullptr; |
| 699 | } |
| 700 | return (*ObjOrErr)->takeIndex(); |
| 701 | } |
| 702 | |
Benjamin Kramer | fe2b541 | 2015-12-24 10:03:35 +0000 | [diff] [blame] | 703 | namespace { |
Mehdi Amini | 42418ab | 2015-11-24 06:07:49 +0000 | [diff] [blame] | 704 | /// Pass that performs cross-module function import provided a summary file. |
| 705 | class FunctionImportPass : public ModulePass { |
Teresa Johnson | 26ab577 | 2016-03-15 00:04:37 +0000 | [diff] [blame] | 706 | /// Optional module summary index to use for importing, otherwise |
Teresa Johnson | 5fcbdb7 | 2015-12-07 19:21:11 +0000 | [diff] [blame] | 707 | /// the summary-file option must be specified. |
Teresa Johnson | 26ab577 | 2016-03-15 00:04:37 +0000 | [diff] [blame] | 708 | const ModuleSummaryIndex *Index; |
Mehdi Amini | 42418ab | 2015-11-24 06:07:49 +0000 | [diff] [blame] | 709 | |
| 710 | public: |
| 711 | /// Pass identification, replacement for typeid |
| 712 | static char ID; |
| 713 | |
Teresa Johnson | 5fcbdb7 | 2015-12-07 19:21:11 +0000 | [diff] [blame] | 714 | /// Specify pass name for debug output |
Mehdi Amini | 2d28f7a | 2016-04-16 06:56:44 +0000 | [diff] [blame] | 715 | const char *getPassName() const override { return "Function Importing"; } |
Teresa Johnson | 5fcbdb7 | 2015-12-07 19:21:11 +0000 | [diff] [blame] | 716 | |
Teresa Johnson | 26ab577 | 2016-03-15 00:04:37 +0000 | [diff] [blame] | 717 | explicit FunctionImportPass(const ModuleSummaryIndex *Index = nullptr) |
Teresa Johnson | 5fcbdb7 | 2015-12-07 19:21:11 +0000 | [diff] [blame] | 718 | : ModulePass(ID), Index(Index) {} |
Mehdi Amini | 42418ab | 2015-11-24 06:07:49 +0000 | [diff] [blame] | 719 | |
| 720 | bool runOnModule(Module &M) override { |
Andrew Kaylor | aa641a5 | 2016-04-22 22:06:11 +0000 | [diff] [blame] | 721 | if (skipModule(M)) |
| 722 | return false; |
| 723 | |
Teresa Johnson | 5fcbdb7 | 2015-12-07 19:21:11 +0000 | [diff] [blame] | 724 | if (SummaryFile.empty() && !Index) |
| 725 | report_fatal_error("error: -function-import requires -summary-file or " |
| 726 | "file from frontend\n"); |
Teresa Johnson | 26ab577 | 2016-03-15 00:04:37 +0000 | [diff] [blame] | 727 | std::unique_ptr<ModuleSummaryIndex> IndexPtr; |
Teresa Johnson | 5fcbdb7 | 2015-12-07 19:21:11 +0000 | [diff] [blame] | 728 | if (!SummaryFile.empty()) { |
| 729 | if (Index) |
| 730 | report_fatal_error("error: -summary-file and index from frontend\n"); |
| 731 | std::string Error; |
Teresa Johnson | 26ab577 | 2016-03-15 00:04:37 +0000 | [diff] [blame] | 732 | IndexPtr = |
| 733 | getModuleSummaryIndexForFile(SummaryFile, Error, diagnosticHandler); |
Teresa Johnson | 5fcbdb7 | 2015-12-07 19:21:11 +0000 | [diff] [blame] | 734 | if (!IndexPtr) { |
| 735 | errs() << "Error loading file '" << SummaryFile << "': " << Error |
| 736 | << "\n"; |
| 737 | return false; |
| 738 | } |
| 739 | Index = IndexPtr.get(); |
Mehdi Amini | 42418ab | 2015-11-24 06:07:49 +0000 | [diff] [blame] | 740 | } |
| 741 | |
Teresa Johnson | c86af33 | 2016-04-12 21:13:11 +0000 | [diff] [blame] | 742 | // First step is collecting the import list. |
| 743 | FunctionImporter::ImportMapTy ImportList; |
| 744 | ComputeCrossModuleImportForModule(M.getModuleIdentifier(), *Index, |
| 745 | ImportList); |
Mehdi Amini | 01e3213 | 2016-03-26 05:40:34 +0000 | [diff] [blame] | 746 | |
| 747 | // 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] | 748 | // are potentially exported to other modules. |
Mehdi Amini | 01e3213 | 2016-03-26 05:40:34 +0000 | [diff] [blame] | 749 | if (renameModuleForThinLTO(M, *Index, nullptr)) { |
Teresa Johnson | 1b00f2d | 2016-01-08 17:06:29 +0000 | [diff] [blame] | 750 | errs() << "Error renaming module\n"; |
| 751 | return false; |
| 752 | } |
| 753 | |
Mehdi Amini | 42418ab | 2015-11-24 06:07:49 +0000 | [diff] [blame] | 754 | // Perform the import now. |
Mehdi Amini | d16c806 | 2015-12-08 22:39:40 +0000 | [diff] [blame] | 755 | auto ModuleLoader = [&M](StringRef Identifier) { |
| 756 | return loadFile(Identifier, M.getContext()); |
| 757 | }; |
Rafael Espindola | 9d2bfc4 | 2015-12-14 23:17:03 +0000 | [diff] [blame] | 758 | FunctionImporter Importer(*Index, ModuleLoader); |
Mehdi Amini | bda3c97 | 2016-04-21 01:59:39 +0000 | [diff] [blame] | 759 | return Importer.importFunctions( |
| 760 | M, ImportList, !DontForceImportReferencedDiscardableSymbols); |
Mehdi Amini | 42418ab | 2015-11-24 06:07:49 +0000 | [diff] [blame] | 761 | } |
| 762 | }; |
Benjamin Kramer | fe2b541 | 2015-12-24 10:03:35 +0000 | [diff] [blame] | 763 | } // anonymous namespace |
Mehdi Amini | 42418ab | 2015-11-24 06:07:49 +0000 | [diff] [blame] | 764 | |
| 765 | char FunctionImportPass::ID = 0; |
| 766 | INITIALIZE_PASS_BEGIN(FunctionImportPass, "function-import", |
| 767 | "Summary Based Function Import", false, false) |
| 768 | INITIALIZE_PASS_END(FunctionImportPass, "function-import", |
| 769 | "Summary Based Function Import", false, false) |
| 770 | |
| 771 | namespace llvm { |
Teresa Johnson | 26ab577 | 2016-03-15 00:04:37 +0000 | [diff] [blame] | 772 | Pass *createFunctionImportPass(const ModuleSummaryIndex *Index = nullptr) { |
Teresa Johnson | 5fcbdb7 | 2015-12-07 19:21:11 +0000 | [diff] [blame] | 773 | return new FunctionImportPass(Index); |
| 774 | } |
Mehdi Amini | 42418ab | 2015-11-24 06:07:49 +0000 | [diff] [blame] | 775 | } |