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 | } |
Teresa Johnson | e1164de | 2016-02-10 21:55:02 +0000 | [diff] [blame] | 129 | // Compute the global identifier used in the function index. |
| 130 | auto CalledFunctionGlobalID = Function::getGlobalIdentifier( |
| 131 | CalledFunction->getName(), CalledFunction->getLinkage(), |
| 132 | CalledFunction->getParent()->getSourceFileName()); |
| 133 | auto It = CalledFunctions.insert(CalledFunctionGlobalID); |
Mehdi Amini | 7e88d0d | 2015-12-09 08:17:35 +0000 | [diff] [blame] | 134 | if (!It.second) { |
| 135 | // This is a call to a function we already considered, skip. |
| 136 | continue; |
| 137 | } |
| 138 | // Ignore functions already present in the destination module |
| 139 | auto *SrcGV = DestModule.getNamedValue(ImportedName); |
| 140 | if (SrcGV) { |
Teresa Johnson | 388497e | 2016-01-12 17:48:44 +0000 | [diff] [blame] | 141 | if (GlobalAlias *SGA = dyn_cast<GlobalAlias>(SrcGV)) |
| 142 | SrcGV = SGA->getBaseObject(); |
Mehdi Amini | 7e88d0d | 2015-12-09 08:17:35 +0000 | [diff] [blame] | 143 | assert(isa<Function>(SrcGV) && "Name collision during import"); |
| 144 | if (!cast<Function>(SrcGV)->isDeclaration()) { |
Teresa Johnson | 9f2ff9c | 2015-12-10 16:39:07 +0000 | [diff] [blame] | 145 | DEBUG(dbgs() << DestModule.getModuleIdentifier() << ": Ignoring " |
Mehdi Amini | 7e88d0d | 2015-12-09 08:17:35 +0000 | [diff] [blame] | 146 | << ImportedName << " already in DestinationModule\n"); |
| 147 | continue; |
| 148 | } |
| 149 | } |
| 150 | |
| 151 | Worklist.push_back(It.first->getKey()); |
| 152 | DEBUG(dbgs() << DestModule.getModuleIdentifier() |
Teresa Johnson | 9f2ff9c | 2015-12-10 16:39:07 +0000 | [diff] [blame] | 153 | << ": Adding callee for : " << ImportedName << " : " |
Mehdi Amini | 7e88d0d | 2015-12-09 08:17:35 +0000 | [diff] [blame] | 154 | << F.getName() << "\n"); |
Teresa Johnson | d450da3 | 2015-11-24 21:15:19 +0000 | [diff] [blame] | 155 | } |
| 156 | } |
| 157 | } |
| 158 | } |
| 159 | |
Mehdi Amini | c8c5517 | 2015-12-03 02:37:33 +0000 | [diff] [blame] | 160 | // 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] | 161 | // and decide what to import based on the summary information. |
| 162 | // |
| 163 | // Nothing is actually imported, functions are materialized in their source |
| 164 | // module and analyzed there. |
| 165 | // |
| 166 | // \p ModuleToFunctionsToImportMap is filled with the set of Function to import |
| 167 | // per Module. |
Rafael Espindola | 434e956 | 2015-12-16 23:16:33 +0000 | [diff] [blame] | 168 | static void GetImportList(Module &DestModule, |
| 169 | SmallVector<StringRef, 64> &Worklist, |
| 170 | StringSet<> &CalledFunctions, |
| 171 | std::map<StringRef, DenseSet<const GlobalValue *>> |
| 172 | &ModuleToFunctionsToImportMap, |
| 173 | const FunctionInfoIndex &Index, |
| 174 | ModuleLazyLoaderCache &ModuleLoaderCache) { |
Mehdi Amini | 42418ab | 2015-11-24 06:07:49 +0000 | [diff] [blame] | 175 | while (!Worklist.empty()) { |
| 176 | auto CalledFunctionName = Worklist.pop_back_val(); |
Teresa Johnson | 9f2ff9c | 2015-12-10 16:39:07 +0000 | [diff] [blame] | 177 | DEBUG(dbgs() << DestModule.getModuleIdentifier() << ": Process import for " |
Mehdi Amini | 5411d05 | 2015-12-08 23:04:19 +0000 | [diff] [blame] | 178 | << CalledFunctionName << "\n"); |
Mehdi Amini | 42418ab | 2015-11-24 06:07:49 +0000 | [diff] [blame] | 179 | |
| 180 | // Try to get a summary for this function call. |
| 181 | auto InfoList = Index.findFunctionInfoList(CalledFunctionName); |
| 182 | if (InfoList == Index.end()) { |
Teresa Johnson | 9f2ff9c | 2015-12-10 16:39:07 +0000 | [diff] [blame] | 183 | DEBUG(dbgs() << DestModule.getModuleIdentifier() << ": No summary for " |
Mehdi Amini | 5411d05 | 2015-12-08 23:04:19 +0000 | [diff] [blame] | 184 | << CalledFunctionName << " Ignoring.\n"); |
Mehdi Amini | 42418ab | 2015-11-24 06:07:49 +0000 | [diff] [blame] | 185 | continue; |
| 186 | } |
| 187 | assert(!InfoList->second.empty() && "No summary, error at import?"); |
| 188 | |
| 189 | // Comdat can have multiple entries, FIXME: what do we do with them? |
| 190 | auto &Info = InfoList->second[0]; |
| 191 | assert(Info && "Nullptr in list, error importing summaries?\n"); |
| 192 | |
| 193 | auto *Summary = Info->functionSummary(); |
| 194 | if (!Summary) { |
| 195 | // FIXME: in case we are lazyloading summaries, we can do it now. |
Mehdi Amini | 5411d05 | 2015-12-08 23:04:19 +0000 | [diff] [blame] | 196 | DEBUG(dbgs() << DestModule.getModuleIdentifier() |
Teresa Johnson | 9f2ff9c | 2015-12-10 16:39:07 +0000 | [diff] [blame] | 197 | << ": Missing summary for " << CalledFunctionName |
Teresa Johnson | 430110c | 2015-12-01 17:12:10 +0000 | [diff] [blame] | 198 | << ", error at import?\n"); |
Mehdi Amini | 42418ab | 2015-11-24 06:07:49 +0000 | [diff] [blame] | 199 | llvm_unreachable("Missing summary"); |
| 200 | } |
| 201 | |
Teresa Johnson | 3930361 | 2015-11-24 22:55:46 +0000 | [diff] [blame] | 202 | if (Summary->instCount() > ImportInstrLimit) { |
Teresa Johnson | 9f2ff9c | 2015-12-10 16:39:07 +0000 | [diff] [blame] | 203 | DEBUG(dbgs() << DestModule.getModuleIdentifier() << ": Skip import of " |
Mehdi Amini | 5411d05 | 2015-12-08 23:04:19 +0000 | [diff] [blame] | 204 | << CalledFunctionName << " with " << Summary->instCount() |
| 205 | << " instructions (limit " << ImportInstrLimit << ")\n"); |
Teresa Johnson | 3930361 | 2015-11-24 22:55:46 +0000 | [diff] [blame] | 206 | continue; |
| 207 | } |
| 208 | |
Mehdi Amini | 42418ab | 2015-11-24 06:07:49 +0000 | [diff] [blame] | 209 | // Get the module path from the summary. |
Mehdi Amini | 7e88d0d | 2015-12-09 08:17:35 +0000 | [diff] [blame] | 210 | auto ModuleIdentifier = Summary->modulePath(); |
Teresa Johnson | 9f2ff9c | 2015-12-10 16:39:07 +0000 | [diff] [blame] | 211 | DEBUG(dbgs() << DestModule.getModuleIdentifier() << ": Importing " |
Mehdi Amini | 7e88d0d | 2015-12-09 08:17:35 +0000 | [diff] [blame] | 212 | << CalledFunctionName << " from " << ModuleIdentifier << "\n"); |
Mehdi Amini | 42418ab | 2015-11-24 06:07:49 +0000 | [diff] [blame] | 213 | |
Mehdi Amini | 7e88d0d | 2015-12-09 08:17:35 +0000 | [diff] [blame] | 214 | auto &SrcModule = ModuleLoaderCache(ModuleIdentifier); |
Mehdi Amini | 42418ab | 2015-11-24 06:07:49 +0000 | [diff] [blame] | 215 | |
| 216 | // The function that we will import! |
Mehdi Amini | 7e88d0d | 2015-12-09 08:17:35 +0000 | [diff] [blame] | 217 | GlobalValue *SGV = SrcModule.getNamedValue(CalledFunctionName); |
| 218 | |
Teresa Johnson | 130de7a | 2015-11-24 19:55:04 +0000 | [diff] [blame] | 219 | if (!SGV) { |
Teresa Johnson | e1164de | 2016-02-10 21:55:02 +0000 | [diff] [blame] | 220 | // The function is referenced by a global identifier, which has the |
| 221 | // source file name prepended for functions that were originally local |
| 222 | // in the source module. Strip any prepended name to recover the original |
Mehdi Amini | 7e88d0d | 2015-12-09 08:17:35 +0000 | [diff] [blame] | 223 | // name in the source module. |
Teresa Johnson | e1164de | 2016-02-10 21:55:02 +0000 | [diff] [blame] | 224 | std::pair<StringRef, StringRef> Split = CalledFunctionName.split(":"); |
| 225 | SGV = SrcModule.getNamedValue(Split.second); |
Mehdi Amini | 7e88d0d | 2015-12-09 08:17:35 +0000 | [diff] [blame] | 226 | assert(SGV && "Can't find function to import in source module"); |
Teresa Johnson | 130de7a | 2015-11-24 19:55:04 +0000 | [diff] [blame] | 227 | } |
Mehdi Amini | 7e88d0d | 2015-12-09 08:17:35 +0000 | [diff] [blame] | 228 | if (!SGV) { |
| 229 | report_fatal_error(Twine("Can't load function '") + CalledFunctionName + |
| 230 | "' in Module '" + SrcModule.getModuleIdentifier() + |
| 231 | "', error in the summary?\n"); |
| 232 | } |
| 233 | |
Mehdi Amini | 42418ab | 2015-11-24 06:07:49 +0000 | [diff] [blame] | 234 | Function *F = dyn_cast<Function>(SGV); |
| 235 | if (!F && isa<GlobalAlias>(SGV)) { |
| 236 | auto *SGA = dyn_cast<GlobalAlias>(SGV); |
| 237 | F = dyn_cast<Function>(SGA->getBaseObject()); |
Mehdi Amini | 7e88d0d | 2015-12-09 08:17:35 +0000 | [diff] [blame] | 238 | CalledFunctionName = F->getName(); |
Mehdi Amini | 42418ab | 2015-11-24 06:07:49 +0000 | [diff] [blame] | 239 | } |
Mehdi Amini | 7e88d0d | 2015-12-09 08:17:35 +0000 | [diff] [blame] | 240 | assert(F && "Imported Function is ... not a Function"); |
Mehdi Amini | 42418ab | 2015-11-24 06:07:49 +0000 | [diff] [blame] | 241 | |
Teresa Johnson | 1762665 | 2015-11-24 16:10:43 +0000 | [diff] [blame] | 242 | // We cannot import weak_any functions/aliases without possibly affecting |
| 243 | // the order they are seen and selected by the linker, changing program |
Mehdi Amini | 42418ab | 2015-11-24 06:07:49 +0000 | [diff] [blame] | 244 | // semantics. |
Teresa Johnson | 1762665 | 2015-11-24 16:10:43 +0000 | [diff] [blame] | 245 | if (SGV->hasWeakAnyLinkage()) { |
Mehdi Amini | 5411d05 | 2015-12-08 23:04:19 +0000 | [diff] [blame] | 246 | DEBUG(dbgs() << DestModule.getModuleIdentifier() |
Teresa Johnson | 9f2ff9c | 2015-12-10 16:39:07 +0000 | [diff] [blame] | 247 | << ": Ignoring import request for weak-any " |
Teresa Johnson | 1762665 | 2015-11-24 16:10:43 +0000 | [diff] [blame] | 248 | << (isa<Function>(SGV) ? "function " : "alias ") |
Mehdi Amini | 7e88d0d | 2015-12-09 08:17:35 +0000 | [diff] [blame] | 249 | << CalledFunctionName << " from " |
| 250 | << SrcModule.getModuleIdentifier() << "\n"); |
Mehdi Amini | 42418ab | 2015-11-24 06:07:49 +0000 | [diff] [blame] | 251 | continue; |
| 252 | } |
| 253 | |
Mehdi Amini | 7e88d0d | 2015-12-09 08:17:35 +0000 | [diff] [blame] | 254 | // Add the function to the import list |
| 255 | auto &Entry = ModuleToFunctionsToImportMap[SrcModule.getModuleIdentifier()]; |
Rafael Espindola | 434e956 | 2015-12-16 23:16:33 +0000 | [diff] [blame] | 256 | Entry.insert(F); |
Mehdi Amini | 42418ab | 2015-11-24 06:07:49 +0000 | [diff] [blame] | 257 | |
Mehdi Amini | 7e88d0d | 2015-12-09 08:17:35 +0000 | [diff] [blame] | 258 | // Process the newly imported functions and add callees to the worklist. |
| 259 | F->materialize(); |
| 260 | findExternalCalls(DestModule, *F, Index, CalledFunctions, Worklist); |
Mehdi Amini | 42418ab | 2015-11-24 06:07:49 +0000 | [diff] [blame] | 261 | } |
Mehdi Amini | c8c5517 | 2015-12-03 02:37:33 +0000 | [diff] [blame] | 262 | } |
Mehdi Amini | ffe2e4a | 2015-12-02 04:34:28 +0000 | [diff] [blame] | 263 | |
Mehdi Amini | c8c5517 | 2015-12-03 02:37:33 +0000 | [diff] [blame] | 264 | // Automatically import functions in Module \p DestModule based on the summaries |
| 265 | // index. |
| 266 | // |
| 267 | // The current implementation imports every called functions that exists in the |
| 268 | // summaries index. |
| 269 | bool FunctionImporter::importFunctions(Module &DestModule) { |
Mehdi Amini | 5411d05 | 2015-12-08 23:04:19 +0000 | [diff] [blame] | 270 | DEBUG(dbgs() << "Starting import for Module " |
Mehdi Amini | 311fef6 | 2015-12-03 02:58:14 +0000 | [diff] [blame] | 271 | << DestModule.getModuleIdentifier() << "\n"); |
Mehdi Amini | c8c5517 | 2015-12-03 02:37:33 +0000 | [diff] [blame] | 272 | unsigned ImportedCount = 0; |
| 273 | |
| 274 | /// First step is collecting the called external functions. |
| 275 | StringSet<> CalledFunctions; |
| 276 | SmallVector<StringRef, 64> Worklist; |
| 277 | for (auto &F : DestModule) { |
| 278 | if (F.isDeclaration() || F.hasFnAttribute(Attribute::OptimizeNone)) |
| 279 | continue; |
Mehdi Amini | 7e88d0d | 2015-12-09 08:17:35 +0000 | [diff] [blame] | 280 | findExternalCalls(DestModule, F, Index, CalledFunctions, Worklist); |
Mehdi Amini | c8c5517 | 2015-12-03 02:37:33 +0000 | [diff] [blame] | 281 | } |
| 282 | if (Worklist.empty()) |
| 283 | return false; |
| 284 | |
| 285 | /// Second step: for every call to an external function, try to import it. |
| 286 | |
| 287 | // Linker that will be used for importing function |
Rafael Espindola | 9d2bfc4 | 2015-12-14 23:17:03 +0000 | [diff] [blame] | 288 | Linker TheLinker(DestModule); |
Mehdi Amini | c8c5517 | 2015-12-03 02:37:33 +0000 | [diff] [blame] | 289 | |
Mehdi Amini | 7e88d0d | 2015-12-09 08:17:35 +0000 | [diff] [blame] | 290 | // Map of Module -> List of Function to import from the Module |
Rafael Espindola | 434e956 | 2015-12-16 23:16:33 +0000 | [diff] [blame] | 291 | std::map<StringRef, DenseSet<const GlobalValue *>> |
Mehdi Amini | 7e88d0d | 2015-12-09 08:17:35 +0000 | [diff] [blame] | 292 | ModuleToFunctionsToImportMap; |
Mehdi Amini | c8c5517 | 2015-12-03 02:37:33 +0000 | [diff] [blame] | 293 | |
Mehdi Amini | 7e88d0d | 2015-12-09 08:17:35 +0000 | [diff] [blame] | 294 | // Analyze the summaries and get the list of functions to import by |
| 295 | // populating ModuleToFunctionsToImportMap |
| 296 | ModuleLazyLoaderCache ModuleLoaderCache(ModuleLoader); |
| 297 | GetImportList(DestModule, Worklist, CalledFunctions, |
| 298 | ModuleToFunctionsToImportMap, Index, ModuleLoaderCache); |
| 299 | assert(Worklist.empty() && "Worklist hasn't been flushed in GetImportList"); |
| 300 | |
| 301 | // Do the actual import of functions now, one Module at a time |
| 302 | for (auto &FunctionsToImportPerModule : ModuleToFunctionsToImportMap) { |
| 303 | // Get the module for the import |
Rafael Espindola | 434e956 | 2015-12-16 23:16:33 +0000 | [diff] [blame] | 304 | auto &FunctionsToImport = FunctionsToImportPerModule.second; |
| 305 | std::unique_ptr<Module> SrcModule = |
| 306 | ModuleLoaderCache.takeModule(FunctionsToImportPerModule.first); |
Mehdi Amini | 7e88d0d | 2015-12-09 08:17:35 +0000 | [diff] [blame] | 307 | assert(&DestModule.getContext() == &SrcModule->getContext() && |
| 308 | "Context mismatch"); |
| 309 | |
Teresa Johnson | 6cba37c | 2016-01-22 00:15:53 +0000 | [diff] [blame] | 310 | // If modules were created with lazy metadata loading, materialize it |
| 311 | // now, before linking it (otherwise this will be a noop). |
| 312 | SrcModule->materializeMetadata(); |
| 313 | UpgradeDebugInfo(*SrcModule); |
Teresa Johnson | e5a6191 | 2015-12-17 17:14:09 +0000 | [diff] [blame] | 314 | |
Mehdi Amini | 7e88d0d | 2015-12-09 08:17:35 +0000 | [diff] [blame] | 315 | // Link in the specified functions. |
Rafael Espindola | 434e956 | 2015-12-16 23:16:33 +0000 | [diff] [blame] | 316 | if (TheLinker.linkInModule(std::move(SrcModule), Linker::Flags::None, |
Teresa Johnson | 6cba37c | 2016-01-22 00:15:53 +0000 | [diff] [blame] | 317 | &Index, &FunctionsToImport)) |
Mehdi Amini | 7e88d0d | 2015-12-09 08:17:35 +0000 | [diff] [blame] | 318 | report_fatal_error("Function Import: link error"); |
| 319 | |
| 320 | ImportedCount += FunctionsToImport.size(); |
| 321 | } |
Teresa Johnson | e5a6191 | 2015-12-17 17:14:09 +0000 | [diff] [blame] | 322 | |
Mehdi Amini | 7e88d0d | 2015-12-09 08:17:35 +0000 | [diff] [blame] | 323 | DEBUG(dbgs() << "Imported " << ImportedCount << " functions for Module " |
Mehdi Amini | c8c5517 | 2015-12-03 02:37:33 +0000 | [diff] [blame] | 324 | << DestModule.getModuleIdentifier() << "\n"); |
| 325 | return ImportedCount; |
Mehdi Amini | 42418ab | 2015-11-24 06:07:49 +0000 | [diff] [blame] | 326 | } |
| 327 | |
| 328 | /// Summary file to use for function importing when using -function-import from |
| 329 | /// the command line. |
| 330 | static cl::opt<std::string> |
| 331 | SummaryFile("summary-file", |
| 332 | cl::desc("The summary file to use for function importing.")); |
| 333 | |
| 334 | static void diagnosticHandler(const DiagnosticInfo &DI) { |
| 335 | raw_ostream &OS = errs(); |
| 336 | DiagnosticPrinterRawOStream DP(OS); |
| 337 | DI.print(DP); |
| 338 | OS << '\n'; |
| 339 | } |
| 340 | |
| 341 | /// Parse the function index out of an IR file and return the function |
| 342 | /// index object if found, or nullptr if not. |
| 343 | static std::unique_ptr<FunctionInfoIndex> |
| 344 | getFunctionIndexForFile(StringRef Path, std::string &Error, |
| 345 | DiagnosticHandlerFunction DiagnosticHandler) { |
| 346 | std::unique_ptr<MemoryBuffer> Buffer; |
| 347 | ErrorOr<std::unique_ptr<MemoryBuffer>> BufferOrErr = |
| 348 | MemoryBuffer::getFile(Path); |
| 349 | if (std::error_code EC = BufferOrErr.getError()) { |
| 350 | Error = EC.message(); |
| 351 | return nullptr; |
| 352 | } |
| 353 | Buffer = std::move(BufferOrErr.get()); |
| 354 | ErrorOr<std::unique_ptr<object::FunctionIndexObjectFile>> ObjOrErr = |
| 355 | object::FunctionIndexObjectFile::create(Buffer->getMemBufferRef(), |
| 356 | DiagnosticHandler); |
| 357 | if (std::error_code EC = ObjOrErr.getError()) { |
| 358 | Error = EC.message(); |
| 359 | return nullptr; |
| 360 | } |
| 361 | return (*ObjOrErr)->takeIndex(); |
| 362 | } |
| 363 | |
Benjamin Kramer | fe2b541 | 2015-12-24 10:03:35 +0000 | [diff] [blame] | 364 | namespace { |
Mehdi Amini | 42418ab | 2015-11-24 06:07:49 +0000 | [diff] [blame] | 365 | /// Pass that performs cross-module function import provided a summary file. |
| 366 | class FunctionImportPass : public ModulePass { |
Teresa Johnson | 5fcbdb7 | 2015-12-07 19:21:11 +0000 | [diff] [blame] | 367 | /// Optional function summary index to use for importing, otherwise |
| 368 | /// the summary-file option must be specified. |
Teresa Johnson | 7f961e1 | 2015-12-09 19:39:47 +0000 | [diff] [blame] | 369 | const FunctionInfoIndex *Index; |
Mehdi Amini | 42418ab | 2015-11-24 06:07:49 +0000 | [diff] [blame] | 370 | |
| 371 | public: |
| 372 | /// Pass identification, replacement for typeid |
| 373 | static char ID; |
| 374 | |
Teresa Johnson | 5fcbdb7 | 2015-12-07 19:21:11 +0000 | [diff] [blame] | 375 | /// Specify pass name for debug output |
| 376 | const char *getPassName() const override { |
| 377 | return "Function Importing"; |
| 378 | } |
| 379 | |
Teresa Johnson | 7f961e1 | 2015-12-09 19:39:47 +0000 | [diff] [blame] | 380 | explicit FunctionImportPass(const FunctionInfoIndex *Index = nullptr) |
Teresa Johnson | 5fcbdb7 | 2015-12-07 19:21:11 +0000 | [diff] [blame] | 381 | : ModulePass(ID), Index(Index) {} |
Mehdi Amini | 42418ab | 2015-11-24 06:07:49 +0000 | [diff] [blame] | 382 | |
| 383 | bool runOnModule(Module &M) override { |
Teresa Johnson | 5fcbdb7 | 2015-12-07 19:21:11 +0000 | [diff] [blame] | 384 | if (SummaryFile.empty() && !Index) |
| 385 | report_fatal_error("error: -function-import requires -summary-file or " |
| 386 | "file from frontend\n"); |
| 387 | std::unique_ptr<FunctionInfoIndex> IndexPtr; |
| 388 | if (!SummaryFile.empty()) { |
| 389 | if (Index) |
| 390 | report_fatal_error("error: -summary-file and index from frontend\n"); |
| 391 | std::string Error; |
| 392 | IndexPtr = getFunctionIndexForFile(SummaryFile, Error, diagnosticHandler); |
| 393 | if (!IndexPtr) { |
| 394 | errs() << "Error loading file '" << SummaryFile << "': " << Error |
| 395 | << "\n"; |
| 396 | return false; |
| 397 | } |
| 398 | Index = IndexPtr.get(); |
Mehdi Amini | 42418ab | 2015-11-24 06:07:49 +0000 | [diff] [blame] | 399 | } |
| 400 | |
Teresa Johnson | 1b00f2d | 2016-01-08 17:06:29 +0000 | [diff] [blame] | 401 | // First we need to promote to global scope and rename any local values that |
| 402 | // are potentially exported to other modules. |
| 403 | if (renameModuleForThinLTO(M, Index)) { |
| 404 | errs() << "Error renaming module\n"; |
| 405 | return false; |
| 406 | } |
| 407 | |
Mehdi Amini | 42418ab | 2015-11-24 06:07:49 +0000 | [diff] [blame] | 408 | // Perform the import now. |
Mehdi Amini | d16c806 | 2015-12-08 22:39:40 +0000 | [diff] [blame] | 409 | auto ModuleLoader = [&M](StringRef Identifier) { |
| 410 | return loadFile(Identifier, M.getContext()); |
| 411 | }; |
Rafael Espindola | 9d2bfc4 | 2015-12-14 23:17:03 +0000 | [diff] [blame] | 412 | FunctionImporter Importer(*Index, ModuleLoader); |
Mehdi Amini | 42418ab | 2015-11-24 06:07:49 +0000 | [diff] [blame] | 413 | return Importer.importFunctions(M); |
Mehdi Amini | 42418ab | 2015-11-24 06:07:49 +0000 | [diff] [blame] | 414 | } |
| 415 | }; |
Benjamin Kramer | fe2b541 | 2015-12-24 10:03:35 +0000 | [diff] [blame] | 416 | } // anonymous namespace |
Mehdi Amini | 42418ab | 2015-11-24 06:07:49 +0000 | [diff] [blame] | 417 | |
| 418 | char FunctionImportPass::ID = 0; |
| 419 | INITIALIZE_PASS_BEGIN(FunctionImportPass, "function-import", |
| 420 | "Summary Based Function Import", false, false) |
| 421 | INITIALIZE_PASS_END(FunctionImportPass, "function-import", |
| 422 | "Summary Based Function Import", false, false) |
| 423 | |
| 424 | namespace llvm { |
Teresa Johnson | 7f961e1 | 2015-12-09 19:39:47 +0000 | [diff] [blame] | 425 | Pass *createFunctionImportPass(const FunctionInfoIndex *Index = nullptr) { |
Teresa Johnson | 5fcbdb7 | 2015-12-07 19:21:11 +0000 | [diff] [blame] | 426 | return new FunctionImportPass(Index); |
| 427 | } |
Mehdi Amini | 42418ab | 2015-11-24 06:07:49 +0000 | [diff] [blame] | 428 | } |