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