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