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