1 //===- llvm/Transforms/IPO/FunctionImport.h - ThinLTO importing -*- C++ -*-===// 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 #ifndef LLVM_FUNCTIONIMPORT_H 11 #define LLVM_FUNCTIONIMPORT_H 12 13 #include "llvm/ADT/StringMap.h" 14 #include "llvm/IR/GlobalValue.h" 15 #include "llvm/IR/ModuleSummaryIndex.h" 16 #include "llvm/IR/PassManager.h" 17 #include "llvm/Support/Error.h" 18 19 #include <functional> 20 #include <map> 21 #include <unordered_set> 22 #include <utility> 23 24 namespace llvm { 25 class LLVMContext; 26 class GlobalValueSummary; 27 class Module; 28 29 /// The function importer is automatically importing function from other modules 30 /// based on the provided summary informations. 31 class FunctionImporter { 32 public: 33 /// Set of functions to import from a source module. Each entry is a map 34 /// containing all the functions to import for a source module. 35 /// The keys is the GUID identifying a function to import, and the value 36 /// is the threshold applied when deciding to import it. 37 typedef std::map<GlobalValue::GUID, unsigned> FunctionsToImportTy; 38 39 /// The map contains an entry for every module to import from, the key being 40 /// the module identifier to pass to the ModuleLoader. The value is the set of 41 /// functions to import. 42 typedef StringMap<FunctionsToImportTy> ImportMapTy; 43 44 /// The set contains an entry for every global value the module exports. 45 typedef std::unordered_set<GlobalValue::GUID> ExportSetTy; 46 47 /// A function of this type is used to load modules referenced by the index. 48 typedef std::function<Expected<std::unique_ptr<Module>>(StringRef Identifier)> 49 ModuleLoaderTy; 50 51 /// Create a Function Importer. 52 FunctionImporter(const ModuleSummaryIndex &Index, ModuleLoaderTy ModuleLoader) 53 : Index(Index), ModuleLoader(std::move(ModuleLoader)) {} 54 55 /// Import functions in Module \p M based on the supplied import list. 56 Expected<bool> importFunctions(Module &M, const ImportMapTy &ImportList); 57 58 private: 59 /// The summaries index used to trigger importing. 60 const ModuleSummaryIndex &Index; 61 62 /// Factory function to load a Module for a given identifier 63 ModuleLoaderTy ModuleLoader; 64 }; 65 66 /// The function importing pass 67 class FunctionImportPass : public PassInfoMixin<FunctionImportPass> { 68 public: 69 PreservedAnalyses run(Module &M, ModuleAnalysisManager &AM); 70 }; 71 72 /// Compute all the imports and exports for every module in the Index. 73 /// 74 /// \p ModuleToDefinedGVSummaries contains for each Module a map 75 /// (GUID -> Summary) for every global defined in the module. 76 /// 77 /// \p ImportLists will be populated with an entry for every Module we are 78 /// importing into. This entry is itself a map that can be passed to 79 /// FunctionImporter::importFunctions() above (see description there). 80 /// 81 /// \p ExportLists contains for each Module the set of globals (GUID) that will 82 /// be imported by another module, or referenced by such a function. I.e. this 83 /// is the set of globals that need to be promoted/renamed appropriately. 84 void ComputeCrossModuleImport( 85 const ModuleSummaryIndex &Index, 86 const StringMap<GVSummaryMapTy> &ModuleToDefinedGVSummaries, 87 StringMap<FunctionImporter::ImportMapTy> &ImportLists, 88 StringMap<FunctionImporter::ExportSetTy> &ExportLists); 89 90 /// Compute all the imports for the given module using the Index. 91 /// 92 /// \p ImportList will be populated with a map that can be passed to 93 /// FunctionImporter::importFunctions() above (see description there). 94 void ComputeCrossModuleImportForModule( 95 StringRef ModulePath, const ModuleSummaryIndex &Index, 96 FunctionImporter::ImportMapTy &ImportList); 97 98 /// Compute all the symbols that are "dead": i.e these that can't be reached 99 /// in the graph from any of the given symbols listed in 100 /// \p GUIDPreservedSymbols. 101 void computeDeadSymbols( 102 ModuleSummaryIndex &Index, 103 const DenseSet<GlobalValue::GUID> &GUIDPreservedSymbols); 104 105 /// Compute the set of summaries needed for a ThinLTO backend compilation of 106 /// \p ModulePath. 107 // 108 /// This includes summaries from that module (in case any global summary based 109 /// optimizations were recorded) and from any definitions in other modules that 110 /// should be imported. 111 // 112 /// \p ModuleToSummariesForIndex will be populated with the needed summaries 113 /// from each required module path. Use a std::map instead of StringMap to get 114 /// stable order for bitcode emission. 115 void gatherImportedSummariesForModule( 116 StringRef ModulePath, 117 const StringMap<GVSummaryMapTy> &ModuleToDefinedGVSummaries, 118 const FunctionImporter::ImportMapTy &ImportList, 119 std::map<std::string, GVSummaryMapTy> &ModuleToSummariesForIndex); 120 121 /// Emit into \p OutputFilename the files module \p ModulePath will import from. 122 std::error_code 123 EmitImportsFiles(StringRef ModulePath, StringRef OutputFilename, 124 const FunctionImporter::ImportMapTy &ModuleImports); 125 126 /// Resolve WeakForLinker values in \p TheModule based on the information 127 /// recorded in the summaries during global summary-based analysis. 128 void thinLTOResolveWeakForLinkerModule(Module &TheModule, 129 const GVSummaryMapTy &DefinedGlobals); 130 131 /// Internalize \p TheModule based on the information recorded in the summaries 132 /// during global summary-based analysis. 133 void thinLTOInternalizeModule(Module &TheModule, 134 const GVSummaryMapTy &DefinedGlobals); 135 } 136 137 #endif // LLVM_FUNCTIONIMPORT_H 138