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