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