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