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