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