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