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