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" |
| 15 | |
Mehdi Amini | 01e3213 | 2016-03-26 05:40:34 +0000 | [diff] [blame] | 16 | #include "llvm/ADT/SmallVector.h" |
Teresa Johnson | d29478f | 2016-03-27 15:27:30 +0000 | [diff] [blame] | 17 | #include "llvm/ADT/Statistic.h" |
Mehdi Amini | 42418ab | 2015-11-24 06:07:49 +0000 | [diff] [blame] | 18 | #include "llvm/ADT/StringSet.h" |
| 19 | #include "llvm/IR/AutoUpgrade.h" |
| 20 | #include "llvm/IR/DiagnosticPrinter.h" |
| 21 | #include "llvm/IR/IntrinsicInst.h" |
| 22 | #include "llvm/IR/Module.h" |
| 23 | #include "llvm/IRReader/IRReader.h" |
| 24 | #include "llvm/Linker/Linker.h" |
Teresa Johnson | 26ab577 | 2016-03-15 00:04:37 +0000 | [diff] [blame] | 25 | #include "llvm/Object/ModuleSummaryIndexObjectFile.h" |
Mehdi Amini | 42418ab | 2015-11-24 06:07:49 +0000 | [diff] [blame] | 26 | #include "llvm/Support/CommandLine.h" |
| 27 | #include "llvm/Support/Debug.h" |
| 28 | #include "llvm/Support/SourceMgr.h" |
Teresa Johnson | 488a800 | 2016-02-10 18:11:31 +0000 | [diff] [blame] | 29 | #include "llvm/Transforms/Utils/FunctionImportUtils.h" |
Mehdi Amini | 7e88d0d | 2015-12-09 08:17:35 +0000 | [diff] [blame] | 30 | |
Mehdi Amini | 01e3213 | 2016-03-26 05:40:34 +0000 | [diff] [blame] | 31 | #define DEBUG_TYPE "function-import" |
Mehdi Amini | 7e88d0d | 2015-12-09 08:17:35 +0000 | [diff] [blame] | 32 | |
Mehdi Amini | 42418ab | 2015-11-24 06:07:49 +0000 | [diff] [blame] | 33 | using namespace llvm; |
| 34 | |
Teresa Johnson | d29478f | 2016-03-27 15:27:30 +0000 | [diff] [blame] | 35 | STATISTIC(NumImported, "Number of functions imported"); |
| 36 | |
Teresa Johnson | 3930361 | 2015-11-24 22:55:46 +0000 | [diff] [blame] | 37 | /// Limit on instruction count of imported functions. |
| 38 | static cl::opt<unsigned> ImportInstrLimit( |
| 39 | "import-instr-limit", cl::init(100), cl::Hidden, cl::value_desc("N"), |
| 40 | cl::desc("Only import functions with less than N instructions")); |
| 41 | |
Mehdi Amini | 4064174 | 2016-02-10 23:31:45 +0000 | [diff] [blame] | 42 | static cl::opt<float> |
| 43 | ImportInstrFactor("import-instr-evolution-factor", cl::init(0.7), |
| 44 | cl::Hidden, cl::value_desc("x"), |
| 45 | cl::desc("As we import functions, multiply the " |
| 46 | "`import-instr-limit` threshold by this factor " |
| 47 | "before processing newly imported functions")); |
| 48 | |
Teresa Johnson | d29478f | 2016-03-27 15:27:30 +0000 | [diff] [blame] | 49 | static cl::opt<bool> PrintImports("print-imports", cl::init(false), cl::Hidden, |
| 50 | cl::desc("Print imported functions")); |
| 51 | |
Mehdi Amini | bda3c97 | 2016-04-21 01:59:39 +0000 | [diff] [blame] | 52 | // Temporary allows the function import pass to disable always linking |
| 53 | // referenced discardable symbols. |
| 54 | static cl::opt<bool> |
| 55 | DontForceImportReferencedDiscardableSymbols("disable-force-link-odr", |
| 56 | cl::init(false), cl::Hidden); |
| 57 | |
Mehdi Amini | 42418ab | 2015-11-24 06:07:49 +0000 | [diff] [blame] | 58 | // Load lazily a module from \p FileName in \p Context. |
| 59 | static std::unique_ptr<Module> loadFile(const std::string &FileName, |
| 60 | LLVMContext &Context) { |
| 61 | SMDiagnostic Err; |
| 62 | DEBUG(dbgs() << "Loading '" << FileName << "'\n"); |
Teresa Johnson | 6cba37c | 2016-01-22 00:15:53 +0000 | [diff] [blame] | 63 | // Metadata isn't loaded until functions are imported, to minimize |
| 64 | // the memory overhead. |
Teresa Johnson | a1080ee | 2016-01-08 14:17:41 +0000 | [diff] [blame] | 65 | std::unique_ptr<Module> Result = |
| 66 | getLazyIRFileModule(FileName, Err, Context, |
| 67 | /* ShouldLazyLoadMetadata = */ true); |
Mehdi Amini | 42418ab | 2015-11-24 06:07:49 +0000 | [diff] [blame] | 68 | if (!Result) { |
| 69 | Err.print("function-import", errs()); |
Mehdi Amini | d7ad221 | 2016-04-01 05:33:11 +0000 | [diff] [blame] | 70 | report_fatal_error("Abort"); |
Mehdi Amini | 42418ab | 2015-11-24 06:07:49 +0000 | [diff] [blame] | 71 | } |
| 72 | |
Mehdi Amini | 42418ab | 2015-11-24 06:07:49 +0000 | [diff] [blame] | 73 | return Result; |
| 74 | } |
| 75 | |
Mehdi Amini | 7e88d0d | 2015-12-09 08:17:35 +0000 | [diff] [blame] | 76 | namespace { |
Mehdi Amini | 4064174 | 2016-02-10 23:31:45 +0000 | [diff] [blame] | 77 | |
Mehdi Amini | 01e3213 | 2016-03-26 05:40:34 +0000 | [diff] [blame] | 78 | /// Given a list of possible callee implementation for a call site, select one |
| 79 | /// that fits the \p Threshold. |
| 80 | /// |
| 81 | /// FIXME: select "best" instead of first that fits. But what is "best"? |
| 82 | /// - The smallest: more likely to be inlined. |
| 83 | /// - The one with the least outgoing edges (already well optimized). |
| 84 | /// - One from a module already being imported from in order to reduce the |
| 85 | /// number of source modules parsed/linked. |
| 86 | /// - One that has PGO data attached. |
| 87 | /// - [insert you fancy metric here] |
Mehdi Amini | 2d28f7a | 2016-04-16 06:56:44 +0000 | [diff] [blame] | 88 | static const GlobalValueSummary * |
Mehdi Amini | 01e3213 | 2016-03-26 05:40:34 +0000 | [diff] [blame] | 89 | selectCallee(const GlobalValueInfoList &CalleeInfoList, unsigned Threshold) { |
| 90 | auto It = llvm::find_if( |
| 91 | CalleeInfoList, [&](const std::unique_ptr<GlobalValueInfo> &GlobInfo) { |
| 92 | assert(GlobInfo->summary() && |
| 93 | "We should not have a Global Info without summary"); |
Mehdi Amini | 2d28f7a | 2016-04-16 06:56:44 +0000 | [diff] [blame] | 94 | auto *GVSummary = GlobInfo->summary(); |
Mehdi Amini | 2c719cc | 2016-04-20 04:17:36 +0000 | [diff] [blame] | 95 | if (GlobalValue::isWeakAnyLinkage(GVSummary->linkage())) |
| 96 | // There is no point in importing weak symbols, we can't inline them |
Mehdi Amini | 01e3213 | 2016-03-26 05:40:34 +0000 | [diff] [blame] | 97 | return false; |
Mehdi Amini | 2c719cc | 2016-04-20 04:17:36 +0000 | [diff] [blame] | 98 | if (auto *AS = dyn_cast<AliasSummary>(GVSummary)) { |
| 99 | GVSummary = &AS->getAliasee(); |
| 100 | // Alias can't point to "available_externally". However when we import |
| 101 | // linkOnceODR the linkage does not change. So we import the alias |
| 102 | // and aliasee only in this case. |
| 103 | // FIXME: we should import alias as available_externally *function*, |
| 104 | // the destination module does need to know it is an alias. |
| 105 | if (!GlobalValue::isLinkOnceODRLinkage(GVSummary->linkage())) |
| 106 | return false; |
| 107 | } |
| 108 | |
| 109 | auto *Summary = cast<FunctionSummary>(GVSummary); |
Mehdi Amini | 7e88d0d | 2015-12-09 08:17:35 +0000 | [diff] [blame] | 110 | |
Mehdi Amini | 01e3213 | 2016-03-26 05:40:34 +0000 | [diff] [blame] | 111 | if (Summary->instCount() > Threshold) |
| 112 | return false; |
Mehdi Amini | 7e88d0d | 2015-12-09 08:17:35 +0000 | [diff] [blame] | 113 | |
Mehdi Amini | 01e3213 | 2016-03-26 05:40:34 +0000 | [diff] [blame] | 114 | return true; |
| 115 | }); |
| 116 | if (It == CalleeInfoList.end()) |
| 117 | return nullptr; |
Mehdi Amini | 7e88d0d | 2015-12-09 08:17:35 +0000 | [diff] [blame] | 118 | |
Mehdi Amini | 2d28f7a | 2016-04-16 06:56:44 +0000 | [diff] [blame] | 119 | return cast<GlobalValueSummary>((*It)->summary()); |
Mehdi Amini | 7e88d0d | 2015-12-09 08:17:35 +0000 | [diff] [blame] | 120 | } |
Mehdi Amini | 7e88d0d | 2015-12-09 08:17:35 +0000 | [diff] [blame] | 121 | |
Mehdi Amini | 01e3213 | 2016-03-26 05:40:34 +0000 | [diff] [blame] | 122 | /// Return the summary for the function \p GUID that fits the \p Threshold, or |
| 123 | /// null if there's no match. |
Mehdi Amini | 2d28f7a | 2016-04-16 06:56:44 +0000 | [diff] [blame] | 124 | static const GlobalValueSummary *selectCallee(GlobalValue::GUID GUID, |
| 125 | unsigned Threshold, |
| 126 | const ModuleSummaryIndex &Index) { |
Mehdi Amini | 01e3213 | 2016-03-26 05:40:34 +0000 | [diff] [blame] | 127 | auto CalleeInfoList = Index.findGlobalValueInfoList(GUID); |
| 128 | if (CalleeInfoList == Index.end()) { |
| 129 | return nullptr; // This function does not have a summary |
| 130 | } |
| 131 | return selectCallee(CalleeInfoList->second, Threshold); |
| 132 | } |
Mehdi Amini | 7e88d0d | 2015-12-09 08:17:35 +0000 | [diff] [blame] | 133 | |
Mehdi Amini | cb87494 | 2016-04-23 23:29:24 +0000 | [diff] [blame] | 134 | /// Mark the global \p GUID as export by module \p ExportModulePath if found in |
| 135 | /// this module. If it is a GlobalVariable, we also mark any referenced global |
| 136 | /// in the current module as exported. |
| 137 | static void exportGlobalInModule(const ModuleSummaryIndex &Index, |
| 138 | StringRef ExportModulePath, |
| 139 | GlobalValue::GUID GUID, |
| 140 | FunctionImporter::ExportSetTy &ExportList) { |
| 141 | auto FindGlobalInfoInModule = |
| 142 | [&](GlobalValue::GUID GUID) -> GlobalValueInfo *{ |
| 143 | auto InfoList = Index.findGlobalValueInfoList(GUID); |
| 144 | if (InfoList == Index.end()) |
| 145 | // This global does not have a summary, it is not part of the ThinLTO |
| 146 | // process |
| 147 | return nullptr; |
| 148 | auto Info = llvm::find_if( |
| 149 | InfoList->second, |
| 150 | [&](const std::unique_ptr<GlobalValueInfo> &GlobInfo) { |
| 151 | auto *Summary = GlobInfo->summary(); |
| 152 | assert(Summary && "Unexpected GlobalValueInfo without summary"); |
| 153 | return Summary->modulePath() == ExportModulePath; |
| 154 | }); |
| 155 | if (Info == InfoList->second.end()) |
| 156 | return nullptr; |
| 157 | return Info->get(); |
| 158 | }; |
| 159 | |
| 160 | auto *GVInfo = FindGlobalInfoInModule(GUID); |
| 161 | if (!GVInfo) |
| 162 | return; |
| 163 | // We found it in the current module, mark as exported |
| 164 | ExportList.insert(GUID); |
| 165 | |
| 166 | auto *Summary = GVInfo->summary(); |
| 167 | auto GVS = dyn_cast<GlobalVarSummary>(Summary); |
| 168 | if (!GVS) |
| 169 | return; |
| 170 | // FunctionImportGlobalProcessing::doPromoteLocalToGlobal() will always |
| 171 | // trigger importing the initializer for `constant unnamed addr` globals that |
| 172 | // are referenced. We conservatively export all the referenced symbols for |
| 173 | // every global to workaround this, so that the ExportList is accurate. |
| 174 | // FIXME: with a "isConstant" flag in the summary we could be more targetted. |
| 175 | for (auto &Ref : GVS->refs()) { |
| 176 | auto GUID = Ref.getGUID(); |
| 177 | auto *RefInfo = FindGlobalInfoInModule(GUID); |
| 178 | if (RefInfo) |
| 179 | // Found a ref in the current module, mark it as exported |
| 180 | ExportList.insert(GUID); |
| 181 | } |
Mehdi Amini | 01e3213 | 2016-03-26 05:40:34 +0000 | [diff] [blame] | 182 | } |
Mehdi Amini | 4064174 | 2016-02-10 23:31:45 +0000 | [diff] [blame] | 183 | |
Mehdi Amini | 01e3213 | 2016-03-26 05:40:34 +0000 | [diff] [blame] | 184 | using EdgeInfo = std::pair<const FunctionSummary *, unsigned /* Threshold */>; |
Mehdi Amini | 7e88d0d | 2015-12-09 08:17:35 +0000 | [diff] [blame] | 185 | |
Mehdi Amini | 01e3213 | 2016-03-26 05:40:34 +0000 | [diff] [blame] | 186 | /// Compute the list of functions to import for a given caller. Mark these |
| 187 | /// imported functions and the symbols they reference in their source module as |
| 188 | /// exported from their source module. |
| 189 | static void computeImportForFunction( |
Teresa Johnson | 3255eec | 2016-04-10 15:17:26 +0000 | [diff] [blame] | 190 | const FunctionSummary &Summary, const ModuleSummaryIndex &Index, |
| 191 | unsigned Threshold, |
Mehdi Amini | 1aafabf | 2016-04-16 07:02:16 +0000 | [diff] [blame] | 192 | const std::map<GlobalValue::GUID, GlobalValueSummary *> &DefinedGVSummaries, |
Mehdi Amini | 01e3213 | 2016-03-26 05:40:34 +0000 | [diff] [blame] | 193 | SmallVectorImpl<EdgeInfo> &Worklist, |
| 194 | FunctionImporter::ImportMapTy &ImportsForModule, |
Teresa Johnson | c86af33 | 2016-04-12 21:13:11 +0000 | [diff] [blame] | 195 | StringMap<FunctionImporter::ExportSetTy> *ExportLists = nullptr) { |
Mehdi Amini | 01e3213 | 2016-03-26 05:40:34 +0000 | [diff] [blame] | 196 | for (auto &Edge : Summary.calls()) { |
Teresa Johnson | 2d5487c | 2016-04-11 13:58:45 +0000 | [diff] [blame] | 197 | auto GUID = Edge.first.getGUID(); |
Mehdi Amini | 01e3213 | 2016-03-26 05:40:34 +0000 | [diff] [blame] | 198 | DEBUG(dbgs() << " edge -> " << GUID << " Threshold:" << Threshold << "\n"); |
| 199 | |
Mehdi Amini | 1aafabf | 2016-04-16 07:02:16 +0000 | [diff] [blame] | 200 | if (DefinedGVSummaries.count(GUID)) { |
Mehdi Amini | 01e3213 | 2016-03-26 05:40:34 +0000 | [diff] [blame] | 201 | DEBUG(dbgs() << "ignored! Target already in destination module.\n"); |
| 202 | continue; |
Teresa Johnson | d450da3 | 2015-11-24 21:15:19 +0000 | [diff] [blame] | 203 | } |
Mehdi Amini | 01e3213 | 2016-03-26 05:40:34 +0000 | [diff] [blame] | 204 | |
| 205 | auto *CalleeSummary = selectCallee(GUID, Threshold, Index); |
| 206 | if (!CalleeSummary) { |
| 207 | DEBUG(dbgs() << "ignored! No qualifying callee with summary found.\n"); |
| 208 | continue; |
| 209 | } |
Mehdi Amini | 2d28f7a | 2016-04-16 06:56:44 +0000 | [diff] [blame] | 210 | // "Resolve" the summary, traversing alias, |
| 211 | const FunctionSummary *ResolvedCalleeSummary; |
Mehdi Amini | 6968ef7 | 2016-04-20 01:04:20 +0000 | [diff] [blame] | 212 | if (isa<AliasSummary>(CalleeSummary)) { |
Mehdi Amini | 2d28f7a | 2016-04-16 06:56:44 +0000 | [diff] [blame] | 213 | ResolvedCalleeSummary = cast<FunctionSummary>( |
| 214 | &cast<AliasSummary>(CalleeSummary)->getAliasee()); |
Mehdi Amini | 2c719cc | 2016-04-20 04:17:36 +0000 | [diff] [blame] | 215 | assert( |
| 216 | GlobalValue::isLinkOnceODRLinkage(ResolvedCalleeSummary->linkage()) && |
| 217 | "Unexpected alias to a non-linkonceODR in import list"); |
Mehdi Amini | 6968ef7 | 2016-04-20 01:04:20 +0000 | [diff] [blame] | 218 | } else |
Mehdi Amini | 2d28f7a | 2016-04-16 06:56:44 +0000 | [diff] [blame] | 219 | ResolvedCalleeSummary = cast<FunctionSummary>(CalleeSummary); |
| 220 | |
| 221 | assert(ResolvedCalleeSummary->instCount() <= Threshold && |
Mehdi Amini | 01e3213 | 2016-03-26 05:40:34 +0000 | [diff] [blame] | 222 | "selectCallee() didn't honor the threshold"); |
| 223 | |
Mehdi Amini | 2d28f7a | 2016-04-16 06:56:44 +0000 | [diff] [blame] | 224 | auto ExportModulePath = ResolvedCalleeSummary->modulePath(); |
| 225 | auto &ProcessedThreshold = ImportsForModule[ExportModulePath][GUID]; |
Mehdi Amini | 01e3213 | 2016-03-26 05:40:34 +0000 | [diff] [blame] | 226 | /// Since the traversal of the call graph is DFS, we can revisit a function |
| 227 | /// a second time with a higher threshold. In this case, it is added back to |
| 228 | /// the worklist with the new threshold. |
| 229 | if (ProcessedThreshold && ProcessedThreshold > Threshold) { |
| 230 | DEBUG(dbgs() << "ignored! Target was already seen with Threshold " |
| 231 | << ProcessedThreshold << "\n"); |
| 232 | continue; |
| 233 | } |
| 234 | // Mark this function as imported in this module, with the current Threshold |
| 235 | ProcessedThreshold = Threshold; |
| 236 | |
| 237 | // Make exports in the source module. |
Teresa Johnson | c86af33 | 2016-04-12 21:13:11 +0000 | [diff] [blame] | 238 | if (ExportLists) { |
Mehdi Amini | ef7555f | 2016-04-13 01:52:32 +0000 | [diff] [blame] | 239 | auto &ExportList = (*ExportLists)[ExportModulePath]; |
Teresa Johnson | c86af33 | 2016-04-12 21:13:11 +0000 | [diff] [blame] | 240 | ExportList.insert(GUID); |
| 241 | // Mark all functions and globals referenced by this function as exported |
| 242 | // to the outside if they are defined in the same source module. |
Mehdi Amini | 2d28f7a | 2016-04-16 06:56:44 +0000 | [diff] [blame] | 243 | for (auto &Edge : ResolvedCalleeSummary->calls()) { |
Teresa Johnson | c86af33 | 2016-04-12 21:13:11 +0000 | [diff] [blame] | 244 | auto CalleeGUID = Edge.first.getGUID(); |
Mehdi Amini | cb87494 | 2016-04-23 23:29:24 +0000 | [diff] [blame] | 245 | exportGlobalInModule(Index, ExportModulePath, CalleeGUID, ExportList); |
Teresa Johnson | c86af33 | 2016-04-12 21:13:11 +0000 | [diff] [blame] | 246 | } |
Mehdi Amini | 2d28f7a | 2016-04-16 06:56:44 +0000 | [diff] [blame] | 247 | for (auto &Ref : ResolvedCalleeSummary->refs()) { |
Teresa Johnson | c86af33 | 2016-04-12 21:13:11 +0000 | [diff] [blame] | 248 | auto GUID = Ref.getGUID(); |
Mehdi Amini | cb87494 | 2016-04-23 23:29:24 +0000 | [diff] [blame] | 249 | exportGlobalInModule(Index, ExportModulePath, GUID, ExportList); |
Teresa Johnson | c86af33 | 2016-04-12 21:13:11 +0000 | [diff] [blame] | 250 | } |
Mehdi Amini | 01e3213 | 2016-03-26 05:40:34 +0000 | [diff] [blame] | 251 | } |
| 252 | |
| 253 | // Insert the newly imported function to the worklist. |
Mehdi Amini | 2d28f7a | 2016-04-16 06:56:44 +0000 | [diff] [blame] | 254 | Worklist.push_back(std::make_pair(ResolvedCalleeSummary, Threshold)); |
Teresa Johnson | d450da3 | 2015-11-24 21:15:19 +0000 | [diff] [blame] | 255 | } |
| 256 | } |
| 257 | |
Mehdi Amini | 01e3213 | 2016-03-26 05:40:34 +0000 | [diff] [blame] | 258 | /// Given the list of globals defined in a module, compute the list of imports |
| 259 | /// as well as the list of "exports", i.e. the list of symbols referenced from |
| 260 | /// another module (that may require promotion). |
| 261 | static void ComputeImportForModule( |
Mehdi Amini | 1aafabf | 2016-04-16 07:02:16 +0000 | [diff] [blame] | 262 | const std::map<GlobalValue::GUID, GlobalValueSummary *> &DefinedGVSummaries, |
Mehdi Amini | 01e3213 | 2016-03-26 05:40:34 +0000 | [diff] [blame] | 263 | const ModuleSummaryIndex &Index, |
| 264 | FunctionImporter::ImportMapTy &ImportsForModule, |
Teresa Johnson | c86af33 | 2016-04-12 21:13:11 +0000 | [diff] [blame] | 265 | StringMap<FunctionImporter::ExportSetTy> *ExportLists = nullptr) { |
Mehdi Amini | 01e3213 | 2016-03-26 05:40:34 +0000 | [diff] [blame] | 266 | // Worklist contains the list of function imported in this module, for which |
| 267 | // we will analyse the callees and may import further down the callgraph. |
| 268 | SmallVector<EdgeInfo, 128> Worklist; |
| 269 | |
| 270 | // Populate the worklist with the import for the functions in the current |
| 271 | // module |
Mehdi Amini | 1aafabf | 2016-04-16 07:02:16 +0000 | [diff] [blame] | 272 | for (auto &GVInfo : DefinedGVSummaries) { |
| 273 | auto *Summary = GVInfo.second; |
Mehdi Amini | 2d28f7a | 2016-04-16 06:56:44 +0000 | [diff] [blame] | 274 | if (auto *AS = dyn_cast<AliasSummary>(Summary)) |
| 275 | Summary = &AS->getAliasee(); |
Mehdi Amini | 1aafabf | 2016-04-16 07:02:16 +0000 | [diff] [blame] | 276 | auto *FuncSummary = dyn_cast<FunctionSummary>(Summary); |
| 277 | if (!FuncSummary) |
| 278 | // Skip import for global variables |
| 279 | continue; |
| 280 | DEBUG(dbgs() << "Initalize import for " << GVInfo.first << "\n"); |
Mehdi Amini | 2d28f7a | 2016-04-16 06:56:44 +0000 | [diff] [blame] | 281 | computeImportForFunction(*FuncSummary, Index, ImportInstrLimit, |
Mehdi Amini | 1aafabf | 2016-04-16 07:02:16 +0000 | [diff] [blame] | 282 | DefinedGVSummaries, Worklist, ImportsForModule, |
Mehdi Amini | 01e3213 | 2016-03-26 05:40:34 +0000 | [diff] [blame] | 283 | ExportLists); |
| 284 | } |
| 285 | |
Mehdi Amini | 42418ab | 2015-11-24 06:07:49 +0000 | [diff] [blame] | 286 | while (!Worklist.empty()) { |
Mehdi Amini | 01e3213 | 2016-03-26 05:40:34 +0000 | [diff] [blame] | 287 | auto FuncInfo = Worklist.pop_back_val(); |
| 288 | auto *Summary = FuncInfo.first; |
| 289 | auto Threshold = FuncInfo.second; |
Mehdi Amini | 42418ab | 2015-11-24 06:07:49 +0000 | [diff] [blame] | 290 | |
Mehdi Amini | 7e88d0d | 2015-12-09 08:17:35 +0000 | [diff] [blame] | 291 | // Process the newly imported functions and add callees to the worklist. |
Mehdi Amini | 4064174 | 2016-02-10 23:31:45 +0000 | [diff] [blame] | 292 | // Adjust the threshold |
| 293 | Threshold = Threshold * ImportInstrFactor; |
Mehdi Amini | 01e3213 | 2016-03-26 05:40:34 +0000 | [diff] [blame] | 294 | |
Mehdi Amini | 1aafabf | 2016-04-16 07:02:16 +0000 | [diff] [blame] | 295 | computeImportForFunction(*Summary, Index, Threshold, DefinedGVSummaries, |
Teresa Johnson | 3255eec | 2016-04-10 15:17:26 +0000 | [diff] [blame] | 296 | Worklist, ImportsForModule, ExportLists); |
Mehdi Amini | 42418ab | 2015-11-24 06:07:49 +0000 | [diff] [blame] | 297 | } |
Mehdi Amini | c8c5517 | 2015-12-03 02:37:33 +0000 | [diff] [blame] | 298 | } |
Mehdi Amini | ffe2e4a | 2015-12-02 04:34:28 +0000 | [diff] [blame] | 299 | |
Mehdi Amini | 01e3213 | 2016-03-26 05:40:34 +0000 | [diff] [blame] | 300 | } // anonymous namespace |
| 301 | |
Teresa Johnson | c86af33 | 2016-04-12 21:13:11 +0000 | [diff] [blame] | 302 | /// Compute all the import and export for every module using the Index. |
Mehdi Amini | 01e3213 | 2016-03-26 05:40:34 +0000 | [diff] [blame] | 303 | void llvm::ComputeCrossModuleImport( |
| 304 | const ModuleSummaryIndex &Index, |
Mehdi Amini | 1aafabf | 2016-04-16 07:02:16 +0000 | [diff] [blame] | 305 | const StringMap<std::map<GlobalValue::GUID, GlobalValueSummary *>> & |
| 306 | ModuleToDefinedGVSummaries, |
Mehdi Amini | 01e3213 | 2016-03-26 05:40:34 +0000 | [diff] [blame] | 307 | StringMap<FunctionImporter::ImportMapTy> &ImportLists, |
| 308 | StringMap<FunctionImporter::ExportSetTy> &ExportLists) { |
Mehdi Amini | 01e3213 | 2016-03-26 05:40:34 +0000 | [diff] [blame] | 309 | // For each module that has function defined, compute the import/export lists. |
Mehdi Amini | 1aafabf | 2016-04-16 07:02:16 +0000 | [diff] [blame] | 310 | for (auto &DefinedGVSummaries : ModuleToDefinedGVSummaries) { |
| 311 | auto &ImportsForModule = ImportLists[DefinedGVSummaries.first()]; |
| 312 | DEBUG(dbgs() << "Computing import for Module '" |
| 313 | << DefinedGVSummaries.first() << "'\n"); |
| 314 | ComputeImportForModule(DefinedGVSummaries.second, Index, ImportsForModule, |
Teresa Johnson | c86af33 | 2016-04-12 21:13:11 +0000 | [diff] [blame] | 315 | &ExportLists); |
Mehdi Amini | 01e3213 | 2016-03-26 05:40:34 +0000 | [diff] [blame] | 316 | } |
| 317 | |
| 318 | #ifndef NDEBUG |
| 319 | DEBUG(dbgs() << "Import/Export lists for " << ImportLists.size() |
| 320 | << " modules:\n"); |
| 321 | for (auto &ModuleImports : ImportLists) { |
| 322 | auto ModName = ModuleImports.first(); |
| 323 | auto &Exports = ExportLists[ModName]; |
| 324 | DEBUG(dbgs() << "* Module " << ModName << " exports " << Exports.size() |
| 325 | << " functions. Imports from " << ModuleImports.second.size() |
| 326 | << " modules.\n"); |
| 327 | for (auto &Src : ModuleImports.second) { |
| 328 | auto SrcModName = Src.first(); |
| 329 | DEBUG(dbgs() << " - " << Src.second.size() << " functions imported from " |
| 330 | << SrcModName << "\n"); |
| 331 | } |
| 332 | } |
| 333 | #endif |
| 334 | } |
| 335 | |
Teresa Johnson | c86af33 | 2016-04-12 21:13:11 +0000 | [diff] [blame] | 336 | /// Compute all the imports for the given module in the Index. |
| 337 | void llvm::ComputeCrossModuleImportForModule( |
| 338 | StringRef ModulePath, const ModuleSummaryIndex &Index, |
| 339 | FunctionImporter::ImportMapTy &ImportList) { |
| 340 | |
| 341 | // Collect the list of functions this module defines. |
| 342 | // GUID -> Summary |
Mehdi Amini | 2d28f7a | 2016-04-16 06:56:44 +0000 | [diff] [blame] | 343 | std::map<GlobalValue::GUID, GlobalValueSummary *> FunctionInfoMap; |
Teresa Johnson | c86af33 | 2016-04-12 21:13:11 +0000 | [diff] [blame] | 344 | Index.collectDefinedFunctionsForModule(ModulePath, FunctionInfoMap); |
| 345 | |
| 346 | // Compute the import list for this module. |
| 347 | DEBUG(dbgs() << "Computing import for Module '" << ModulePath << "'\n"); |
| 348 | ComputeImportForModule(FunctionInfoMap, Index, ImportList); |
| 349 | |
| 350 | #ifndef NDEBUG |
| 351 | DEBUG(dbgs() << "* Module " << ModulePath << " imports from " |
| 352 | << ImportList.size() << " modules.\n"); |
| 353 | for (auto &Src : ImportList) { |
| 354 | auto SrcModName = Src.first(); |
| 355 | DEBUG(dbgs() << " - " << Src.second.size() << " functions imported from " |
| 356 | << SrcModName << "\n"); |
| 357 | } |
| 358 | #endif |
| 359 | } |
| 360 | |
Mehdi Amini | c8c5517 | 2015-12-03 02:37:33 +0000 | [diff] [blame] | 361 | // Automatically import functions in Module \p DestModule based on the summaries |
| 362 | // index. |
| 363 | // |
Mehdi Amini | 01e3213 | 2016-03-26 05:40:34 +0000 | [diff] [blame] | 364 | bool FunctionImporter::importFunctions( |
Mehdi Amini | bda3c97 | 2016-04-21 01:59:39 +0000 | [diff] [blame] | 365 | Module &DestModule, const FunctionImporter::ImportMapTy &ImportList, |
| 366 | bool ForceImportReferencedDiscardableSymbols) { |
Mehdi Amini | 5411d05 | 2015-12-08 23:04:19 +0000 | [diff] [blame] | 367 | DEBUG(dbgs() << "Starting import for Module " |
Mehdi Amini | 311fef6 | 2015-12-03 02:58:14 +0000 | [diff] [blame] | 368 | << DestModule.getModuleIdentifier() << "\n"); |
Mehdi Amini | c8c5517 | 2015-12-03 02:37:33 +0000 | [diff] [blame] | 369 | unsigned ImportedCount = 0; |
| 370 | |
Mehdi Amini | c8c5517 | 2015-12-03 02:37:33 +0000 | [diff] [blame] | 371 | // Linker that will be used for importing function |
Rafael Espindola | 9d2bfc4 | 2015-12-14 23:17:03 +0000 | [diff] [blame] | 372 | Linker TheLinker(DestModule); |
Mehdi Amini | 7e88d0d | 2015-12-09 08:17:35 +0000 | [diff] [blame] | 373 | // Do the actual import of functions now, one Module at a time |
Mehdi Amini | 01e3213 | 2016-03-26 05:40:34 +0000 | [diff] [blame] | 374 | std::set<StringRef> ModuleNameOrderedList; |
| 375 | for (auto &FunctionsToImportPerModule : ImportList) { |
| 376 | ModuleNameOrderedList.insert(FunctionsToImportPerModule.first()); |
| 377 | } |
| 378 | for (auto &Name : ModuleNameOrderedList) { |
Mehdi Amini | 7e88d0d | 2015-12-09 08:17:35 +0000 | [diff] [blame] | 379 | // Get the module for the import |
Mehdi Amini | 01e3213 | 2016-03-26 05:40:34 +0000 | [diff] [blame] | 380 | const auto &FunctionsToImportPerModule = ImportList.find(Name); |
| 381 | assert(FunctionsToImportPerModule != ImportList.end()); |
| 382 | std::unique_ptr<Module> SrcModule = ModuleLoader(Name); |
Mehdi Amini | 7e88d0d | 2015-12-09 08:17:35 +0000 | [diff] [blame] | 383 | assert(&DestModule.getContext() == &SrcModule->getContext() && |
| 384 | "Context mismatch"); |
| 385 | |
Teresa Johnson | 6cba37c | 2016-01-22 00:15:53 +0000 | [diff] [blame] | 386 | // If modules were created with lazy metadata loading, materialize it |
| 387 | // now, before linking it (otherwise this will be a noop). |
| 388 | SrcModule->materializeMetadata(); |
| 389 | UpgradeDebugInfo(*SrcModule); |
Teresa Johnson | e5a6191 | 2015-12-17 17:14:09 +0000 | [diff] [blame] | 390 | |
Mehdi Amini | 01e3213 | 2016-03-26 05:40:34 +0000 | [diff] [blame] | 391 | auto &ImportGUIDs = FunctionsToImportPerModule->second; |
| 392 | // Find the globals to import |
| 393 | DenseSet<const GlobalValue *> GlobalsToImport; |
| 394 | for (auto &GV : *SrcModule) { |
Teresa Johnson | 0beb858 | 2016-04-04 18:52:23 +0000 | [diff] [blame] | 395 | if (!GV.hasName()) |
| 396 | continue; |
| 397 | auto GUID = GV.getGUID(); |
| 398 | auto Import = ImportGUIDs.count(GUID); |
Mehdi Amini | aeb1e59 | 2016-04-19 09:21:30 +0000 | [diff] [blame] | 399 | DEBUG(dbgs() << (Import ? "Is" : "Not") << " importing function " << GUID |
| 400 | << " " << GV.getName() << " from " |
| 401 | << SrcModule->getSourceFileName() << "\n"); |
Teresa Johnson | 0beb858 | 2016-04-04 18:52:23 +0000 | [diff] [blame] | 402 | if (Import) { |
Mehdi Amini | 01e3213 | 2016-03-26 05:40:34 +0000 | [diff] [blame] | 403 | GV.materialize(); |
| 404 | GlobalsToImport.insert(&GV); |
| 405 | } |
| 406 | } |
Mehdi Amini | 2d28f7a | 2016-04-16 06:56:44 +0000 | [diff] [blame] | 407 | for (auto &GV : SrcModule->globals()) { |
| 408 | if (!GV.hasName()) |
| 409 | continue; |
| 410 | auto GUID = GV.getGUID(); |
| 411 | auto Import = ImportGUIDs.count(GUID); |
Mehdi Amini | aeb1e59 | 2016-04-19 09:21:30 +0000 | [diff] [blame] | 412 | DEBUG(dbgs() << (Import ? "Is" : "Not") << " importing global " << GUID |
| 413 | << " " << GV.getName() << " from " |
| 414 | << SrcModule->getSourceFileName() << "\n"); |
Mehdi Amini | 2d28f7a | 2016-04-16 06:56:44 +0000 | [diff] [blame] | 415 | if (Import) { |
| 416 | GV.materialize(); |
| 417 | GlobalsToImport.insert(&GV); |
| 418 | } |
| 419 | } |
Mehdi Amini | 01e3213 | 2016-03-26 05:40:34 +0000 | [diff] [blame] | 420 | for (auto &GV : SrcModule->aliases()) { |
| 421 | if (!GV.hasName()) |
| 422 | continue; |
| 423 | auto GUID = GV.getGUID(); |
Teresa Johnson | 0beb858 | 2016-04-04 18:52:23 +0000 | [diff] [blame] | 424 | auto Import = ImportGUIDs.count(GUID); |
Mehdi Amini | aeb1e59 | 2016-04-19 09:21:30 +0000 | [diff] [blame] | 425 | DEBUG(dbgs() << (Import ? "Is" : "Not") << " importing alias " << GUID |
| 426 | << " " << GV.getName() << " from " |
| 427 | << SrcModule->getSourceFileName() << "\n"); |
Teresa Johnson | 0beb858 | 2016-04-04 18:52:23 +0000 | [diff] [blame] | 428 | if (Import) { |
Mehdi Amini | 01e3213 | 2016-03-26 05:40:34 +0000 | [diff] [blame] | 429 | // Alias can't point to "available_externally". However when we import |
Teresa Johnson | 9aae395 | 2016-03-27 15:01:11 +0000 | [diff] [blame] | 430 | // linkOnceODR the linkage does not change. So we import the alias |
Mehdi Amini | 6968ef7 | 2016-04-20 01:04:20 +0000 | [diff] [blame] | 431 | // and aliasee only in this case. This has been handled by |
| 432 | // computeImportForFunction() |
Mehdi Amini | 2d28f7a | 2016-04-16 06:56:44 +0000 | [diff] [blame] | 433 | GlobalObject *GO = GV.getBaseObject(); |
Mehdi Amini | 6968ef7 | 2016-04-20 01:04:20 +0000 | [diff] [blame] | 434 | assert(GO->hasLinkOnceODRLinkage() && |
| 435 | "Unexpected alias to a non-linkonceODR in import list"); |
Mehdi Amini | 2d28f7a | 2016-04-16 06:56:44 +0000 | [diff] [blame] | 436 | #ifndef NDEBUG |
| 437 | if (!GlobalsToImport.count(GO)) |
| 438 | DEBUG(dbgs() << " alias triggers importing aliasee " << GO->getGUID() |
| 439 | << " " << GO->getName() << " from " |
| 440 | << SrcModule->getSourceFileName() << "\n"); |
| 441 | #endif |
| 442 | GO->materialize(); |
Mehdi Amini | 01e3213 | 2016-03-26 05:40:34 +0000 | [diff] [blame] | 443 | GlobalsToImport.insert(GO); |
Mehdi Amini | 01e3213 | 2016-03-26 05:40:34 +0000 | [diff] [blame] | 444 | GV.materialize(); |
| 445 | GlobalsToImport.insert(&GV); |
| 446 | } |
| 447 | } |
| 448 | |
Mehdi Amini | 7e88d0d | 2015-12-09 08:17:35 +0000 | [diff] [blame] | 449 | // Link in the specified functions. |
Mehdi Amini | 01e3213 | 2016-03-26 05:40:34 +0000 | [diff] [blame] | 450 | if (renameModuleForThinLTO(*SrcModule, Index, &GlobalsToImport)) |
Mehdi Amini | 8d05185 | 2016-03-19 00:40:31 +0000 | [diff] [blame] | 451 | return true; |
| 452 | |
Teresa Johnson | d29478f | 2016-03-27 15:27:30 +0000 | [diff] [blame] | 453 | if (PrintImports) { |
| 454 | for (const auto *GV : GlobalsToImport) |
| 455 | dbgs() << DestModule.getSourceFileName() << ": Import " << GV->getName() |
| 456 | << " from " << SrcModule->getSourceFileName() << "\n"; |
| 457 | } |
| 458 | |
Mehdi Amini | bda3c97 | 2016-04-21 01:59:39 +0000 | [diff] [blame] | 459 | // Instruct the linker that the client will take care of linkonce resolution |
| 460 | unsigned Flags = Linker::Flags::None; |
| 461 | if (!ForceImportReferencedDiscardableSymbols) |
| 462 | Flags |= Linker::Flags::DontForceLinkLinkonceODR; |
| 463 | |
| 464 | if (TheLinker.linkInModule(std::move(SrcModule), Flags, &GlobalsToImport)) |
Mehdi Amini | 7e88d0d | 2015-12-09 08:17:35 +0000 | [diff] [blame] | 465 | report_fatal_error("Function Import: link error"); |
| 466 | |
Mehdi Amini | 01e3213 | 2016-03-26 05:40:34 +0000 | [diff] [blame] | 467 | ImportedCount += GlobalsToImport.size(); |
Mehdi Amini | 7e88d0d | 2015-12-09 08:17:35 +0000 | [diff] [blame] | 468 | } |
Teresa Johnson | e5a6191 | 2015-12-17 17:14:09 +0000 | [diff] [blame] | 469 | |
Teresa Johnson | d29478f | 2016-03-27 15:27:30 +0000 | [diff] [blame] | 470 | NumImported += ImportedCount; |
| 471 | |
Mehdi Amini | 7e88d0d | 2015-12-09 08:17:35 +0000 | [diff] [blame] | 472 | DEBUG(dbgs() << "Imported " << ImportedCount << " functions for Module " |
Mehdi Amini | c8c5517 | 2015-12-03 02:37:33 +0000 | [diff] [blame] | 473 | << DestModule.getModuleIdentifier() << "\n"); |
| 474 | return ImportedCount; |
Mehdi Amini | 42418ab | 2015-11-24 06:07:49 +0000 | [diff] [blame] | 475 | } |
| 476 | |
| 477 | /// Summary file to use for function importing when using -function-import from |
| 478 | /// the command line. |
| 479 | static cl::opt<std::string> |
| 480 | SummaryFile("summary-file", |
| 481 | cl::desc("The summary file to use for function importing.")); |
| 482 | |
| 483 | static void diagnosticHandler(const DiagnosticInfo &DI) { |
| 484 | raw_ostream &OS = errs(); |
| 485 | DiagnosticPrinterRawOStream DP(OS); |
| 486 | DI.print(DP); |
| 487 | OS << '\n'; |
| 488 | } |
| 489 | |
Teresa Johnson | 26ab577 | 2016-03-15 00:04:37 +0000 | [diff] [blame] | 490 | /// Parse the summary index out of an IR file and return the summary |
Mehdi Amini | 42418ab | 2015-11-24 06:07:49 +0000 | [diff] [blame] | 491 | /// index object if found, or nullptr if not. |
Teresa Johnson | 26ab577 | 2016-03-15 00:04:37 +0000 | [diff] [blame] | 492 | static std::unique_ptr<ModuleSummaryIndex> |
| 493 | getModuleSummaryIndexForFile(StringRef Path, std::string &Error, |
| 494 | DiagnosticHandlerFunction DiagnosticHandler) { |
Mehdi Amini | 42418ab | 2015-11-24 06:07:49 +0000 | [diff] [blame] | 495 | std::unique_ptr<MemoryBuffer> Buffer; |
| 496 | ErrorOr<std::unique_ptr<MemoryBuffer>> BufferOrErr = |
| 497 | MemoryBuffer::getFile(Path); |
| 498 | if (std::error_code EC = BufferOrErr.getError()) { |
| 499 | Error = EC.message(); |
| 500 | return nullptr; |
| 501 | } |
| 502 | Buffer = std::move(BufferOrErr.get()); |
Teresa Johnson | 26ab577 | 2016-03-15 00:04:37 +0000 | [diff] [blame] | 503 | ErrorOr<std::unique_ptr<object::ModuleSummaryIndexObjectFile>> ObjOrErr = |
| 504 | object::ModuleSummaryIndexObjectFile::create(Buffer->getMemBufferRef(), |
| 505 | DiagnosticHandler); |
Mehdi Amini | 42418ab | 2015-11-24 06:07:49 +0000 | [diff] [blame] | 506 | if (std::error_code EC = ObjOrErr.getError()) { |
| 507 | Error = EC.message(); |
| 508 | return nullptr; |
| 509 | } |
| 510 | return (*ObjOrErr)->takeIndex(); |
| 511 | } |
| 512 | |
Benjamin Kramer | fe2b541 | 2015-12-24 10:03:35 +0000 | [diff] [blame] | 513 | namespace { |
Mehdi Amini | 42418ab | 2015-11-24 06:07:49 +0000 | [diff] [blame] | 514 | /// Pass that performs cross-module function import provided a summary file. |
| 515 | class FunctionImportPass : public ModulePass { |
Teresa Johnson | 26ab577 | 2016-03-15 00:04:37 +0000 | [diff] [blame] | 516 | /// Optional module summary index to use for importing, otherwise |
Teresa Johnson | 5fcbdb7 | 2015-12-07 19:21:11 +0000 | [diff] [blame] | 517 | /// the summary-file option must be specified. |
Teresa Johnson | 26ab577 | 2016-03-15 00:04:37 +0000 | [diff] [blame] | 518 | const ModuleSummaryIndex *Index; |
Mehdi Amini | 42418ab | 2015-11-24 06:07:49 +0000 | [diff] [blame] | 519 | |
| 520 | public: |
| 521 | /// Pass identification, replacement for typeid |
| 522 | static char ID; |
| 523 | |
Teresa Johnson | 5fcbdb7 | 2015-12-07 19:21:11 +0000 | [diff] [blame] | 524 | /// Specify pass name for debug output |
Mehdi Amini | 2d28f7a | 2016-04-16 06:56:44 +0000 | [diff] [blame] | 525 | const char *getPassName() const override { return "Function Importing"; } |
Teresa Johnson | 5fcbdb7 | 2015-12-07 19:21:11 +0000 | [diff] [blame] | 526 | |
Teresa Johnson | 26ab577 | 2016-03-15 00:04:37 +0000 | [diff] [blame] | 527 | explicit FunctionImportPass(const ModuleSummaryIndex *Index = nullptr) |
Teresa Johnson | 5fcbdb7 | 2015-12-07 19:21:11 +0000 | [diff] [blame] | 528 | : ModulePass(ID), Index(Index) {} |
Mehdi Amini | 42418ab | 2015-11-24 06:07:49 +0000 | [diff] [blame] | 529 | |
| 530 | bool runOnModule(Module &M) override { |
Andrew Kaylor | aa641a5 | 2016-04-22 22:06:11 +0000 | [diff] [blame] | 531 | if (skipModule(M)) |
| 532 | return false; |
| 533 | |
Teresa Johnson | 5fcbdb7 | 2015-12-07 19:21:11 +0000 | [diff] [blame] | 534 | if (SummaryFile.empty() && !Index) |
| 535 | report_fatal_error("error: -function-import requires -summary-file or " |
| 536 | "file from frontend\n"); |
Teresa Johnson | 26ab577 | 2016-03-15 00:04:37 +0000 | [diff] [blame] | 537 | std::unique_ptr<ModuleSummaryIndex> IndexPtr; |
Teresa Johnson | 5fcbdb7 | 2015-12-07 19:21:11 +0000 | [diff] [blame] | 538 | if (!SummaryFile.empty()) { |
| 539 | if (Index) |
| 540 | report_fatal_error("error: -summary-file and index from frontend\n"); |
| 541 | std::string Error; |
Teresa Johnson | 26ab577 | 2016-03-15 00:04:37 +0000 | [diff] [blame] | 542 | IndexPtr = |
| 543 | getModuleSummaryIndexForFile(SummaryFile, Error, diagnosticHandler); |
Teresa Johnson | 5fcbdb7 | 2015-12-07 19:21:11 +0000 | [diff] [blame] | 544 | if (!IndexPtr) { |
| 545 | errs() << "Error loading file '" << SummaryFile << "': " << Error |
| 546 | << "\n"; |
| 547 | return false; |
| 548 | } |
| 549 | Index = IndexPtr.get(); |
Mehdi Amini | 42418ab | 2015-11-24 06:07:49 +0000 | [diff] [blame] | 550 | } |
| 551 | |
Teresa Johnson | c86af33 | 2016-04-12 21:13:11 +0000 | [diff] [blame] | 552 | // First step is collecting the import list. |
| 553 | FunctionImporter::ImportMapTy ImportList; |
| 554 | ComputeCrossModuleImportForModule(M.getModuleIdentifier(), *Index, |
| 555 | ImportList); |
Mehdi Amini | 01e3213 | 2016-03-26 05:40:34 +0000 | [diff] [blame] | 556 | |
| 557 | // Next we need to promote to global scope and rename any local values that |
Teresa Johnson | 1b00f2d | 2016-01-08 17:06:29 +0000 | [diff] [blame] | 558 | // are potentially exported to other modules. |
Mehdi Amini | 01e3213 | 2016-03-26 05:40:34 +0000 | [diff] [blame] | 559 | if (renameModuleForThinLTO(M, *Index, nullptr)) { |
Teresa Johnson | 1b00f2d | 2016-01-08 17:06:29 +0000 | [diff] [blame] | 560 | errs() << "Error renaming module\n"; |
| 561 | return false; |
| 562 | } |
| 563 | |
Mehdi Amini | 42418ab | 2015-11-24 06:07:49 +0000 | [diff] [blame] | 564 | // Perform the import now. |
Mehdi Amini | d16c806 | 2015-12-08 22:39:40 +0000 | [diff] [blame] | 565 | auto ModuleLoader = [&M](StringRef Identifier) { |
| 566 | return loadFile(Identifier, M.getContext()); |
| 567 | }; |
Rafael Espindola | 9d2bfc4 | 2015-12-14 23:17:03 +0000 | [diff] [blame] | 568 | FunctionImporter Importer(*Index, ModuleLoader); |
Mehdi Amini | bda3c97 | 2016-04-21 01:59:39 +0000 | [diff] [blame] | 569 | return Importer.importFunctions( |
| 570 | M, ImportList, !DontForceImportReferencedDiscardableSymbols); |
Mehdi Amini | 42418ab | 2015-11-24 06:07:49 +0000 | [diff] [blame] | 571 | } |
| 572 | }; |
Benjamin Kramer | fe2b541 | 2015-12-24 10:03:35 +0000 | [diff] [blame] | 573 | } // anonymous namespace |
Mehdi Amini | 42418ab | 2015-11-24 06:07:49 +0000 | [diff] [blame] | 574 | |
| 575 | char FunctionImportPass::ID = 0; |
| 576 | INITIALIZE_PASS_BEGIN(FunctionImportPass, "function-import", |
| 577 | "Summary Based Function Import", false, false) |
| 578 | INITIALIZE_PASS_END(FunctionImportPass, "function-import", |
| 579 | "Summary Based Function Import", false, false) |
| 580 | |
| 581 | namespace llvm { |
Teresa Johnson | 26ab577 | 2016-03-15 00:04:37 +0000 | [diff] [blame] | 582 | Pass *createFunctionImportPass(const ModuleSummaryIndex *Index = nullptr) { |
Teresa Johnson | 5fcbdb7 | 2015-12-07 19:21:11 +0000 | [diff] [blame] | 583 | return new FunctionImportPass(Index); |
| 584 | } |
Mehdi Amini | 42418ab | 2015-11-24 06:07:49 +0000 | [diff] [blame] | 585 | } |