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