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