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 | |
| 16 | #include "llvm/ADT/StringSet.h" |
| 17 | #include "llvm/IR/AutoUpgrade.h" |
| 18 | #include "llvm/IR/DiagnosticPrinter.h" |
| 19 | #include "llvm/IR/IntrinsicInst.h" |
| 20 | #include "llvm/IR/Module.h" |
| 21 | #include "llvm/IRReader/IRReader.h" |
| 22 | #include "llvm/Linker/Linker.h" |
| 23 | #include "llvm/Object/FunctionIndexObjectFile.h" |
| 24 | #include "llvm/Support/CommandLine.h" |
| 25 | #include "llvm/Support/Debug.h" |
| 26 | #include "llvm/Support/SourceMgr.h" |
Teresa Johnson | 488a800 | 2016-02-10 18:11:31 +0000 | [diff] [blame^] | 27 | #include "llvm/Transforms/Utils/FunctionImportUtils.h" |
Mehdi Amini | 7e88d0d | 2015-12-09 08:17:35 +0000 | [diff] [blame] | 28 | |
| 29 | #include <map> |
| 30 | |
Mehdi Amini | 42418ab | 2015-11-24 06:07:49 +0000 | [diff] [blame] | 31 | using namespace llvm; |
| 32 | |
| 33 | #define DEBUG_TYPE "function-import" |
| 34 | |
Teresa Johnson | 3930361 | 2015-11-24 22:55:46 +0000 | [diff] [blame] | 35 | /// Limit on instruction count of imported functions. |
| 36 | static cl::opt<unsigned> ImportInstrLimit( |
| 37 | "import-instr-limit", cl::init(100), cl::Hidden, cl::value_desc("N"), |
| 38 | cl::desc("Only import functions with less than N instructions")); |
| 39 | |
Mehdi Amini | 42418ab | 2015-11-24 06:07:49 +0000 | [diff] [blame] | 40 | // Load lazily a module from \p FileName in \p Context. |
| 41 | static std::unique_ptr<Module> loadFile(const std::string &FileName, |
| 42 | LLVMContext &Context) { |
| 43 | SMDiagnostic Err; |
| 44 | DEBUG(dbgs() << "Loading '" << FileName << "'\n"); |
Teresa Johnson | 6cba37c | 2016-01-22 00:15:53 +0000 | [diff] [blame] | 45 | // Metadata isn't loaded until functions are imported, to minimize |
| 46 | // the memory overhead. |
Teresa Johnson | a1080ee | 2016-01-08 14:17:41 +0000 | [diff] [blame] | 47 | std::unique_ptr<Module> Result = |
| 48 | getLazyIRFileModule(FileName, Err, Context, |
| 49 | /* ShouldLazyLoadMetadata = */ true); |
Mehdi Amini | 42418ab | 2015-11-24 06:07:49 +0000 | [diff] [blame] | 50 | if (!Result) { |
| 51 | Err.print("function-import", errs()); |
| 52 | return nullptr; |
| 53 | } |
| 54 | |
Mehdi Amini | 42418ab | 2015-11-24 06:07:49 +0000 | [diff] [blame] | 55 | return Result; |
| 56 | } |
| 57 | |
Mehdi Amini | 7e88d0d | 2015-12-09 08:17:35 +0000 | [diff] [blame] | 58 | namespace { |
| 59 | /// Helper to load on demand a Module from file and cache it for subsequent |
| 60 | /// queries. It can be used with the FunctionImporter. |
| 61 | class ModuleLazyLoaderCache { |
| 62 | /// Cache of lazily loaded module for import. |
| 63 | StringMap<std::unique_ptr<Module>> ModuleMap; |
| 64 | |
| 65 | /// Retrieve a Module from the cache or lazily load it on demand. |
| 66 | std::function<std::unique_ptr<Module>(StringRef FileName)> createLazyModule; |
| 67 | |
| 68 | public: |
| 69 | /// Create the loader, Module will be initialized in \p Context. |
| 70 | ModuleLazyLoaderCache(std::function< |
| 71 | std::unique_ptr<Module>(StringRef FileName)> createLazyModule) |
| 72 | : createLazyModule(createLazyModule) {} |
| 73 | |
| 74 | /// Retrieve a Module from the cache or lazily load it on demand. |
| 75 | Module &operator()(StringRef FileName); |
Rafael Espindola | 434e956 | 2015-12-16 23:16:33 +0000 | [diff] [blame] | 76 | |
| 77 | std::unique_ptr<Module> takeModule(StringRef FileName) { |
| 78 | auto I = ModuleMap.find(FileName); |
| 79 | assert(I != ModuleMap.end()); |
| 80 | std::unique_ptr<Module> Ret = std::move(I->second); |
| 81 | ModuleMap.erase(I); |
| 82 | return Ret; |
| 83 | } |
Mehdi Amini | 7e88d0d | 2015-12-09 08:17:35 +0000 | [diff] [blame] | 84 | }; |
| 85 | |
| 86 | // Get a Module for \p FileName from the cache, or load it lazily. |
| 87 | Module &ModuleLazyLoaderCache::operator()(StringRef Identifier) { |
| 88 | auto &Module = ModuleMap[Identifier]; |
| 89 | if (!Module) |
| 90 | Module = createLazyModule(Identifier); |
| 91 | return *Module; |
| 92 | } |
| 93 | } // anonymous namespace |
| 94 | |
Teresa Johnson | d450da3 | 2015-11-24 21:15:19 +0000 | [diff] [blame] | 95 | /// Walk through the instructions in \p F looking for external |
| 96 | /// calls not already in the \p CalledFunctions set. If any are |
| 97 | /// found they are added to the \p Worklist for importing. |
Mehdi Amini | 7e88d0d | 2015-12-09 08:17:35 +0000 | [diff] [blame] | 98 | static void findExternalCalls(const Module &DestModule, Function &F, |
| 99 | const FunctionInfoIndex &Index, |
| 100 | StringSet<> &CalledFunctions, |
Teresa Johnson | d450da3 | 2015-11-24 21:15:19 +0000 | [diff] [blame] | 101 | SmallVector<StringRef, 64> &Worklist) { |
Mehdi Amini | 7e88d0d | 2015-12-09 08:17:35 +0000 | [diff] [blame] | 102 | // We need to suffix internal function calls imported from other modules, |
| 103 | // prepare the suffix ahead of time. |
Rafael Espindola | 9edc3b8 | 2015-12-09 20:41:10 +0000 | [diff] [blame] | 104 | std::string Suffix; |
Mehdi Amini | 7e88d0d | 2015-12-09 08:17:35 +0000 | [diff] [blame] | 105 | if (F.getParent() != &DestModule) |
| 106 | Suffix = |
| 107 | (Twine(".llvm.") + |
| 108 | Twine(Index.getModuleId(F.getParent()->getModuleIdentifier()))).str(); |
| 109 | |
Teresa Johnson | d450da3 | 2015-11-24 21:15:19 +0000 | [diff] [blame] | 110 | for (auto &BB : F) { |
| 111 | for (auto &I : BB) { |
| 112 | if (isa<CallInst>(I)) { |
Teresa Johnson | d450da3 | 2015-11-24 21:15:19 +0000 | [diff] [blame] | 113 | auto CalledFunction = cast<CallInst>(I).getCalledFunction(); |
| 114 | // Insert any new external calls that have not already been |
| 115 | // added to set/worklist. |
Mehdi Amini | 7e88d0d | 2015-12-09 08:17:35 +0000 | [diff] [blame] | 116 | if (!CalledFunction || !CalledFunction->hasName()) |
| 117 | continue; |
| 118 | // Ignore intrinsics early |
| 119 | if (CalledFunction->isIntrinsic()) { |
| 120 | assert(CalledFunction->getIntrinsicID() != 0); |
| 121 | continue; |
Teresa Johnson | d450da3 | 2015-11-24 21:15:19 +0000 | [diff] [blame] | 122 | } |
Mehdi Amini | 7e88d0d | 2015-12-09 08:17:35 +0000 | [diff] [blame] | 123 | auto ImportedName = CalledFunction->getName(); |
| 124 | auto Renamed = (ImportedName + Suffix).str(); |
| 125 | // Rename internal functions |
| 126 | if (CalledFunction->hasInternalLinkage()) { |
| 127 | ImportedName = Renamed; |
| 128 | } |
| 129 | auto It = CalledFunctions.insert(ImportedName); |
| 130 | if (!It.second) { |
| 131 | // This is a call to a function we already considered, skip. |
| 132 | continue; |
| 133 | } |
| 134 | // Ignore functions already present in the destination module |
| 135 | auto *SrcGV = DestModule.getNamedValue(ImportedName); |
| 136 | if (SrcGV) { |
Teresa Johnson | 388497e | 2016-01-12 17:48:44 +0000 | [diff] [blame] | 137 | if (GlobalAlias *SGA = dyn_cast<GlobalAlias>(SrcGV)) |
| 138 | SrcGV = SGA->getBaseObject(); |
Mehdi Amini | 7e88d0d | 2015-12-09 08:17:35 +0000 | [diff] [blame] | 139 | assert(isa<Function>(SrcGV) && "Name collision during import"); |
| 140 | if (!cast<Function>(SrcGV)->isDeclaration()) { |
Teresa Johnson | 9f2ff9c | 2015-12-10 16:39:07 +0000 | [diff] [blame] | 141 | DEBUG(dbgs() << DestModule.getModuleIdentifier() << ": Ignoring " |
Mehdi Amini | 7e88d0d | 2015-12-09 08:17:35 +0000 | [diff] [blame] | 142 | << ImportedName << " already in DestinationModule\n"); |
| 143 | continue; |
| 144 | } |
| 145 | } |
| 146 | |
| 147 | Worklist.push_back(It.first->getKey()); |
| 148 | DEBUG(dbgs() << DestModule.getModuleIdentifier() |
Teresa Johnson | 9f2ff9c | 2015-12-10 16:39:07 +0000 | [diff] [blame] | 149 | << ": Adding callee for : " << ImportedName << " : " |
Mehdi Amini | 7e88d0d | 2015-12-09 08:17:35 +0000 | [diff] [blame] | 150 | << F.getName() << "\n"); |
Teresa Johnson | d450da3 | 2015-11-24 21:15:19 +0000 | [diff] [blame] | 151 | } |
| 152 | } |
| 153 | } |
| 154 | } |
| 155 | |
Mehdi Amini | c8c5517 | 2015-12-03 02:37:33 +0000 | [diff] [blame] | 156 | // Helper function: given a worklist and an index, will process all the worklist |
Mehdi Amini | 7e88d0d | 2015-12-09 08:17:35 +0000 | [diff] [blame] | 157 | // and decide what to import based on the summary information. |
| 158 | // |
| 159 | // Nothing is actually imported, functions are materialized in their source |
| 160 | // module and analyzed there. |
| 161 | // |
| 162 | // \p ModuleToFunctionsToImportMap is filled with the set of Function to import |
| 163 | // per Module. |
Rafael Espindola | 434e956 | 2015-12-16 23:16:33 +0000 | [diff] [blame] | 164 | static void GetImportList(Module &DestModule, |
| 165 | SmallVector<StringRef, 64> &Worklist, |
| 166 | StringSet<> &CalledFunctions, |
| 167 | std::map<StringRef, DenseSet<const GlobalValue *>> |
| 168 | &ModuleToFunctionsToImportMap, |
| 169 | const FunctionInfoIndex &Index, |
| 170 | ModuleLazyLoaderCache &ModuleLoaderCache) { |
Mehdi Amini | 42418ab | 2015-11-24 06:07:49 +0000 | [diff] [blame] | 171 | while (!Worklist.empty()) { |
| 172 | auto CalledFunctionName = Worklist.pop_back_val(); |
Teresa Johnson | 9f2ff9c | 2015-12-10 16:39:07 +0000 | [diff] [blame] | 173 | DEBUG(dbgs() << DestModule.getModuleIdentifier() << ": Process import for " |
Mehdi Amini | 5411d05 | 2015-12-08 23:04:19 +0000 | [diff] [blame] | 174 | << CalledFunctionName << "\n"); |
Mehdi Amini | 42418ab | 2015-11-24 06:07:49 +0000 | [diff] [blame] | 175 | |
| 176 | // Try to get a summary for this function call. |
| 177 | auto InfoList = Index.findFunctionInfoList(CalledFunctionName); |
| 178 | if (InfoList == Index.end()) { |
Teresa Johnson | 9f2ff9c | 2015-12-10 16:39:07 +0000 | [diff] [blame] | 179 | DEBUG(dbgs() << DestModule.getModuleIdentifier() << ": No summary for " |
Mehdi Amini | 5411d05 | 2015-12-08 23:04:19 +0000 | [diff] [blame] | 180 | << CalledFunctionName << " Ignoring.\n"); |
Mehdi Amini | 42418ab | 2015-11-24 06:07:49 +0000 | [diff] [blame] | 181 | continue; |
| 182 | } |
| 183 | assert(!InfoList->second.empty() && "No summary, error at import?"); |
| 184 | |
| 185 | // Comdat can have multiple entries, FIXME: what do we do with them? |
| 186 | auto &Info = InfoList->second[0]; |
| 187 | assert(Info && "Nullptr in list, error importing summaries?\n"); |
| 188 | |
| 189 | auto *Summary = Info->functionSummary(); |
| 190 | if (!Summary) { |
| 191 | // FIXME: in case we are lazyloading summaries, we can do it now. |
Mehdi Amini | 5411d05 | 2015-12-08 23:04:19 +0000 | [diff] [blame] | 192 | DEBUG(dbgs() << DestModule.getModuleIdentifier() |
Teresa Johnson | 9f2ff9c | 2015-12-10 16:39:07 +0000 | [diff] [blame] | 193 | << ": Missing summary for " << CalledFunctionName |
Teresa Johnson | 430110c | 2015-12-01 17:12:10 +0000 | [diff] [blame] | 194 | << ", error at import?\n"); |
Mehdi Amini | 42418ab | 2015-11-24 06:07:49 +0000 | [diff] [blame] | 195 | llvm_unreachable("Missing summary"); |
| 196 | } |
| 197 | |
Teresa Johnson | 3930361 | 2015-11-24 22:55:46 +0000 | [diff] [blame] | 198 | if (Summary->instCount() > ImportInstrLimit) { |
Teresa Johnson | 9f2ff9c | 2015-12-10 16:39:07 +0000 | [diff] [blame] | 199 | DEBUG(dbgs() << DestModule.getModuleIdentifier() << ": Skip import of " |
Mehdi Amini | 5411d05 | 2015-12-08 23:04:19 +0000 | [diff] [blame] | 200 | << CalledFunctionName << " with " << Summary->instCount() |
| 201 | << " instructions (limit " << ImportInstrLimit << ")\n"); |
Teresa Johnson | 3930361 | 2015-11-24 22:55:46 +0000 | [diff] [blame] | 202 | continue; |
| 203 | } |
| 204 | |
Mehdi Amini | 42418ab | 2015-11-24 06:07:49 +0000 | [diff] [blame] | 205 | // Get the module path from the summary. |
Mehdi Amini | 7e88d0d | 2015-12-09 08:17:35 +0000 | [diff] [blame] | 206 | auto ModuleIdentifier = Summary->modulePath(); |
Teresa Johnson | 9f2ff9c | 2015-12-10 16:39:07 +0000 | [diff] [blame] | 207 | DEBUG(dbgs() << DestModule.getModuleIdentifier() << ": Importing " |
Mehdi Amini | 7e88d0d | 2015-12-09 08:17:35 +0000 | [diff] [blame] | 208 | << CalledFunctionName << " from " << ModuleIdentifier << "\n"); |
Mehdi Amini | 42418ab | 2015-11-24 06:07:49 +0000 | [diff] [blame] | 209 | |
Mehdi Amini | 7e88d0d | 2015-12-09 08:17:35 +0000 | [diff] [blame] | 210 | auto &SrcModule = ModuleLoaderCache(ModuleIdentifier); |
Mehdi Amini | 42418ab | 2015-11-24 06:07:49 +0000 | [diff] [blame] | 211 | |
| 212 | // The function that we will import! |
Mehdi Amini | 7e88d0d | 2015-12-09 08:17:35 +0000 | [diff] [blame] | 213 | GlobalValue *SGV = SrcModule.getNamedValue(CalledFunctionName); |
| 214 | |
Teresa Johnson | 130de7a | 2015-11-24 19:55:04 +0000 | [diff] [blame] | 215 | if (!SGV) { |
Mehdi Amini | 7e88d0d | 2015-12-09 08:17:35 +0000 | [diff] [blame] | 216 | // The destination module is referencing function using their renamed name |
| 217 | // when importing a function that was originally local in the source |
| 218 | // module. The source module we have might not have been renamed so we try |
| 219 | // to remove the suffix added during the renaming to recover the original |
| 220 | // name in the source module. |
Teresa Johnson | 130de7a | 2015-11-24 19:55:04 +0000 | [diff] [blame] | 221 | std::pair<StringRef, StringRef> Split = |
| 222 | CalledFunctionName.split(".llvm."); |
Mehdi Amini | 7e88d0d | 2015-12-09 08:17:35 +0000 | [diff] [blame] | 223 | SGV = SrcModule.getNamedValue(Split.first); |
| 224 | assert(SGV && "Can't find function to import in source module"); |
Teresa Johnson | 130de7a | 2015-11-24 19:55:04 +0000 | [diff] [blame] | 225 | } |
Mehdi Amini | 7e88d0d | 2015-12-09 08:17:35 +0000 | [diff] [blame] | 226 | if (!SGV) { |
| 227 | report_fatal_error(Twine("Can't load function '") + CalledFunctionName + |
| 228 | "' in Module '" + SrcModule.getModuleIdentifier() + |
| 229 | "', error in the summary?\n"); |
| 230 | } |
| 231 | |
Mehdi Amini | 42418ab | 2015-11-24 06:07:49 +0000 | [diff] [blame] | 232 | Function *F = dyn_cast<Function>(SGV); |
| 233 | if (!F && isa<GlobalAlias>(SGV)) { |
| 234 | auto *SGA = dyn_cast<GlobalAlias>(SGV); |
| 235 | F = dyn_cast<Function>(SGA->getBaseObject()); |
Mehdi Amini | 7e88d0d | 2015-12-09 08:17:35 +0000 | [diff] [blame] | 236 | CalledFunctionName = F->getName(); |
Mehdi Amini | 42418ab | 2015-11-24 06:07:49 +0000 | [diff] [blame] | 237 | } |
Mehdi Amini | 7e88d0d | 2015-12-09 08:17:35 +0000 | [diff] [blame] | 238 | assert(F && "Imported Function is ... not a Function"); |
Mehdi Amini | 42418ab | 2015-11-24 06:07:49 +0000 | [diff] [blame] | 239 | |
Teresa Johnson | 1762665 | 2015-11-24 16:10:43 +0000 | [diff] [blame] | 240 | // We cannot import weak_any functions/aliases without possibly affecting |
| 241 | // the order they are seen and selected by the linker, changing program |
Mehdi Amini | 42418ab | 2015-11-24 06:07:49 +0000 | [diff] [blame] | 242 | // semantics. |
Teresa Johnson | 1762665 | 2015-11-24 16:10:43 +0000 | [diff] [blame] | 243 | if (SGV->hasWeakAnyLinkage()) { |
Mehdi Amini | 5411d05 | 2015-12-08 23:04:19 +0000 | [diff] [blame] | 244 | DEBUG(dbgs() << DestModule.getModuleIdentifier() |
Teresa Johnson | 9f2ff9c | 2015-12-10 16:39:07 +0000 | [diff] [blame] | 245 | << ": Ignoring import request for weak-any " |
Teresa Johnson | 1762665 | 2015-11-24 16:10:43 +0000 | [diff] [blame] | 246 | << (isa<Function>(SGV) ? "function " : "alias ") |
Mehdi Amini | 7e88d0d | 2015-12-09 08:17:35 +0000 | [diff] [blame] | 247 | << CalledFunctionName << " from " |
| 248 | << SrcModule.getModuleIdentifier() << "\n"); |
Mehdi Amini | 42418ab | 2015-11-24 06:07:49 +0000 | [diff] [blame] | 249 | continue; |
| 250 | } |
| 251 | |
Mehdi Amini | 7e88d0d | 2015-12-09 08:17:35 +0000 | [diff] [blame] | 252 | // Add the function to the import list |
| 253 | auto &Entry = ModuleToFunctionsToImportMap[SrcModule.getModuleIdentifier()]; |
Rafael Espindola | 434e956 | 2015-12-16 23:16:33 +0000 | [diff] [blame] | 254 | Entry.insert(F); |
Mehdi Amini | 42418ab | 2015-11-24 06:07:49 +0000 | [diff] [blame] | 255 | |
Mehdi Amini | 7e88d0d | 2015-12-09 08:17:35 +0000 | [diff] [blame] | 256 | // Process the newly imported functions and add callees to the worklist. |
| 257 | F->materialize(); |
| 258 | findExternalCalls(DestModule, *F, Index, CalledFunctions, Worklist); |
Mehdi Amini | 42418ab | 2015-11-24 06:07:49 +0000 | [diff] [blame] | 259 | } |
Mehdi Amini | c8c5517 | 2015-12-03 02:37:33 +0000 | [diff] [blame] | 260 | } |
Mehdi Amini | ffe2e4a | 2015-12-02 04:34:28 +0000 | [diff] [blame] | 261 | |
Mehdi Amini | c8c5517 | 2015-12-03 02:37:33 +0000 | [diff] [blame] | 262 | // Automatically import functions in Module \p DestModule based on the summaries |
| 263 | // index. |
| 264 | // |
| 265 | // The current implementation imports every called functions that exists in the |
| 266 | // summaries index. |
| 267 | bool FunctionImporter::importFunctions(Module &DestModule) { |
Mehdi Amini | 5411d05 | 2015-12-08 23:04:19 +0000 | [diff] [blame] | 268 | DEBUG(dbgs() << "Starting import for Module " |
Mehdi Amini | 311fef6 | 2015-12-03 02:58:14 +0000 | [diff] [blame] | 269 | << DestModule.getModuleIdentifier() << "\n"); |
Mehdi Amini | c8c5517 | 2015-12-03 02:37:33 +0000 | [diff] [blame] | 270 | unsigned ImportedCount = 0; |
| 271 | |
| 272 | /// First step is collecting the called external functions. |
| 273 | StringSet<> CalledFunctions; |
| 274 | SmallVector<StringRef, 64> Worklist; |
| 275 | for (auto &F : DestModule) { |
| 276 | if (F.isDeclaration() || F.hasFnAttribute(Attribute::OptimizeNone)) |
| 277 | continue; |
Mehdi Amini | 7e88d0d | 2015-12-09 08:17:35 +0000 | [diff] [blame] | 278 | findExternalCalls(DestModule, F, Index, CalledFunctions, Worklist); |
Mehdi Amini | c8c5517 | 2015-12-03 02:37:33 +0000 | [diff] [blame] | 279 | } |
| 280 | if (Worklist.empty()) |
| 281 | return false; |
| 282 | |
| 283 | /// Second step: for every call to an external function, try to import it. |
| 284 | |
| 285 | // Linker that will be used for importing function |
Rafael Espindola | 9d2bfc4 | 2015-12-14 23:17:03 +0000 | [diff] [blame] | 286 | Linker TheLinker(DestModule); |
Mehdi Amini | c8c5517 | 2015-12-03 02:37:33 +0000 | [diff] [blame] | 287 | |
Mehdi Amini | 7e88d0d | 2015-12-09 08:17:35 +0000 | [diff] [blame] | 288 | // Map of Module -> List of Function to import from the Module |
Rafael Espindola | 434e956 | 2015-12-16 23:16:33 +0000 | [diff] [blame] | 289 | std::map<StringRef, DenseSet<const GlobalValue *>> |
Mehdi Amini | 7e88d0d | 2015-12-09 08:17:35 +0000 | [diff] [blame] | 290 | ModuleToFunctionsToImportMap; |
Mehdi Amini | c8c5517 | 2015-12-03 02:37:33 +0000 | [diff] [blame] | 291 | |
Mehdi Amini | 7e88d0d | 2015-12-09 08:17:35 +0000 | [diff] [blame] | 292 | // Analyze the summaries and get the list of functions to import by |
| 293 | // populating ModuleToFunctionsToImportMap |
| 294 | ModuleLazyLoaderCache ModuleLoaderCache(ModuleLoader); |
| 295 | GetImportList(DestModule, Worklist, CalledFunctions, |
| 296 | ModuleToFunctionsToImportMap, Index, ModuleLoaderCache); |
| 297 | assert(Worklist.empty() && "Worklist hasn't been flushed in GetImportList"); |
| 298 | |
| 299 | // Do the actual import of functions now, one Module at a time |
| 300 | for (auto &FunctionsToImportPerModule : ModuleToFunctionsToImportMap) { |
| 301 | // Get the module for the import |
Rafael Espindola | 434e956 | 2015-12-16 23:16:33 +0000 | [diff] [blame] | 302 | auto &FunctionsToImport = FunctionsToImportPerModule.second; |
| 303 | std::unique_ptr<Module> SrcModule = |
| 304 | ModuleLoaderCache.takeModule(FunctionsToImportPerModule.first); |
Mehdi Amini | 7e88d0d | 2015-12-09 08:17:35 +0000 | [diff] [blame] | 305 | assert(&DestModule.getContext() == &SrcModule->getContext() && |
| 306 | "Context mismatch"); |
| 307 | |
Teresa Johnson | 6cba37c | 2016-01-22 00:15:53 +0000 | [diff] [blame] | 308 | // If modules were created with lazy metadata loading, materialize it |
| 309 | // now, before linking it (otherwise this will be a noop). |
| 310 | SrcModule->materializeMetadata(); |
| 311 | UpgradeDebugInfo(*SrcModule); |
Teresa Johnson | e5a6191 | 2015-12-17 17:14:09 +0000 | [diff] [blame] | 312 | |
Mehdi Amini | 7e88d0d | 2015-12-09 08:17:35 +0000 | [diff] [blame] | 313 | // Link in the specified functions. |
Rafael Espindola | 434e956 | 2015-12-16 23:16:33 +0000 | [diff] [blame] | 314 | if (TheLinker.linkInModule(std::move(SrcModule), Linker::Flags::None, |
Teresa Johnson | 6cba37c | 2016-01-22 00:15:53 +0000 | [diff] [blame] | 315 | &Index, &FunctionsToImport)) |
Mehdi Amini | 7e88d0d | 2015-12-09 08:17:35 +0000 | [diff] [blame] | 316 | report_fatal_error("Function Import: link error"); |
| 317 | |
| 318 | ImportedCount += FunctionsToImport.size(); |
| 319 | } |
Teresa Johnson | e5a6191 | 2015-12-17 17:14:09 +0000 | [diff] [blame] | 320 | |
Mehdi Amini | 7e88d0d | 2015-12-09 08:17:35 +0000 | [diff] [blame] | 321 | DEBUG(dbgs() << "Imported " << ImportedCount << " functions for Module " |
Mehdi Amini | c8c5517 | 2015-12-03 02:37:33 +0000 | [diff] [blame] | 322 | << DestModule.getModuleIdentifier() << "\n"); |
| 323 | return ImportedCount; |
Mehdi Amini | 42418ab | 2015-11-24 06:07:49 +0000 | [diff] [blame] | 324 | } |
| 325 | |
| 326 | /// Summary file to use for function importing when using -function-import from |
| 327 | /// the command line. |
| 328 | static cl::opt<std::string> |
| 329 | SummaryFile("summary-file", |
| 330 | cl::desc("The summary file to use for function importing.")); |
| 331 | |
| 332 | static void diagnosticHandler(const DiagnosticInfo &DI) { |
| 333 | raw_ostream &OS = errs(); |
| 334 | DiagnosticPrinterRawOStream DP(OS); |
| 335 | DI.print(DP); |
| 336 | OS << '\n'; |
| 337 | } |
| 338 | |
| 339 | /// Parse the function index out of an IR file and return the function |
| 340 | /// index object if found, or nullptr if not. |
| 341 | static std::unique_ptr<FunctionInfoIndex> |
| 342 | getFunctionIndexForFile(StringRef Path, std::string &Error, |
| 343 | DiagnosticHandlerFunction DiagnosticHandler) { |
| 344 | std::unique_ptr<MemoryBuffer> Buffer; |
| 345 | ErrorOr<std::unique_ptr<MemoryBuffer>> BufferOrErr = |
| 346 | MemoryBuffer::getFile(Path); |
| 347 | if (std::error_code EC = BufferOrErr.getError()) { |
| 348 | Error = EC.message(); |
| 349 | return nullptr; |
| 350 | } |
| 351 | Buffer = std::move(BufferOrErr.get()); |
| 352 | ErrorOr<std::unique_ptr<object::FunctionIndexObjectFile>> ObjOrErr = |
| 353 | object::FunctionIndexObjectFile::create(Buffer->getMemBufferRef(), |
| 354 | DiagnosticHandler); |
| 355 | if (std::error_code EC = ObjOrErr.getError()) { |
| 356 | Error = EC.message(); |
| 357 | return nullptr; |
| 358 | } |
| 359 | return (*ObjOrErr)->takeIndex(); |
| 360 | } |
| 361 | |
Benjamin Kramer | fe2b541 | 2015-12-24 10:03:35 +0000 | [diff] [blame] | 362 | namespace { |
Mehdi Amini | 42418ab | 2015-11-24 06:07:49 +0000 | [diff] [blame] | 363 | /// Pass that performs cross-module function import provided a summary file. |
| 364 | class FunctionImportPass : public ModulePass { |
Teresa Johnson | 5fcbdb7 | 2015-12-07 19:21:11 +0000 | [diff] [blame] | 365 | /// Optional function summary index to use for importing, otherwise |
| 366 | /// the summary-file option must be specified. |
Teresa Johnson | 7f961e1 | 2015-12-09 19:39:47 +0000 | [diff] [blame] | 367 | const FunctionInfoIndex *Index; |
Mehdi Amini | 42418ab | 2015-11-24 06:07:49 +0000 | [diff] [blame] | 368 | |
| 369 | public: |
| 370 | /// Pass identification, replacement for typeid |
| 371 | static char ID; |
| 372 | |
Teresa Johnson | 5fcbdb7 | 2015-12-07 19:21:11 +0000 | [diff] [blame] | 373 | /// Specify pass name for debug output |
| 374 | const char *getPassName() const override { |
| 375 | return "Function Importing"; |
| 376 | } |
| 377 | |
Teresa Johnson | 7f961e1 | 2015-12-09 19:39:47 +0000 | [diff] [blame] | 378 | explicit FunctionImportPass(const FunctionInfoIndex *Index = nullptr) |
Teresa Johnson | 5fcbdb7 | 2015-12-07 19:21:11 +0000 | [diff] [blame] | 379 | : ModulePass(ID), Index(Index) {} |
Mehdi Amini | 42418ab | 2015-11-24 06:07:49 +0000 | [diff] [blame] | 380 | |
| 381 | bool runOnModule(Module &M) override { |
Teresa Johnson | 5fcbdb7 | 2015-12-07 19:21:11 +0000 | [diff] [blame] | 382 | if (SummaryFile.empty() && !Index) |
| 383 | report_fatal_error("error: -function-import requires -summary-file or " |
| 384 | "file from frontend\n"); |
| 385 | std::unique_ptr<FunctionInfoIndex> IndexPtr; |
| 386 | if (!SummaryFile.empty()) { |
| 387 | if (Index) |
| 388 | report_fatal_error("error: -summary-file and index from frontend\n"); |
| 389 | std::string Error; |
| 390 | IndexPtr = getFunctionIndexForFile(SummaryFile, Error, diagnosticHandler); |
| 391 | if (!IndexPtr) { |
| 392 | errs() << "Error loading file '" << SummaryFile << "': " << Error |
| 393 | << "\n"; |
| 394 | return false; |
| 395 | } |
| 396 | Index = IndexPtr.get(); |
Mehdi Amini | 42418ab | 2015-11-24 06:07:49 +0000 | [diff] [blame] | 397 | } |
| 398 | |
Teresa Johnson | 1b00f2d | 2016-01-08 17:06:29 +0000 | [diff] [blame] | 399 | // First we need to promote to global scope and rename any local values that |
| 400 | // are potentially exported to other modules. |
| 401 | if (renameModuleForThinLTO(M, Index)) { |
| 402 | errs() << "Error renaming module\n"; |
| 403 | return false; |
| 404 | } |
| 405 | |
Mehdi Amini | 42418ab | 2015-11-24 06:07:49 +0000 | [diff] [blame] | 406 | // Perform the import now. |
Mehdi Amini | d16c806 | 2015-12-08 22:39:40 +0000 | [diff] [blame] | 407 | auto ModuleLoader = [&M](StringRef Identifier) { |
| 408 | return loadFile(Identifier, M.getContext()); |
| 409 | }; |
Rafael Espindola | 9d2bfc4 | 2015-12-14 23:17:03 +0000 | [diff] [blame] | 410 | FunctionImporter Importer(*Index, ModuleLoader); |
Mehdi Amini | 42418ab | 2015-11-24 06:07:49 +0000 | [diff] [blame] | 411 | return Importer.importFunctions(M); |
Mehdi Amini | 42418ab | 2015-11-24 06:07:49 +0000 | [diff] [blame] | 412 | } |
| 413 | }; |
Benjamin Kramer | fe2b541 | 2015-12-24 10:03:35 +0000 | [diff] [blame] | 414 | } // anonymous namespace |
Mehdi Amini | 42418ab | 2015-11-24 06:07:49 +0000 | [diff] [blame] | 415 | |
| 416 | char FunctionImportPass::ID = 0; |
| 417 | INITIALIZE_PASS_BEGIN(FunctionImportPass, "function-import", |
| 418 | "Summary Based Function Import", false, false) |
| 419 | INITIALIZE_PASS_END(FunctionImportPass, "function-import", |
| 420 | "Summary Based Function Import", false, false) |
| 421 | |
| 422 | namespace llvm { |
Teresa Johnson | 7f961e1 | 2015-12-09 19:39:47 +0000 | [diff] [blame] | 423 | Pass *createFunctionImportPass(const FunctionInfoIndex *Index = nullptr) { |
Teresa Johnson | 5fcbdb7 | 2015-12-07 19:21:11 +0000 | [diff] [blame] | 424 | return new FunctionImportPass(Index); |
| 425 | } |
Mehdi Amini | 42418ab | 2015-11-24 06:07:49 +0000 | [diff] [blame] | 426 | } |