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