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 17 #include <functional> 18 #include <map> 19 #include <unordered_set> 20 #include <utility> 21 22 namespace llvm { 23 class LLVMContext; 24 class GlobalValueSummary; 25 class Module; 26 27 /// The function importer is automatically importing function from other modules 28 /// based on the provided summary informations. 29 class FunctionImporter { 30 public: 31 /// Set of functions to import from a source module. Each entry is a map 32 /// containing all the functions to import for a source module. 33 /// The keys is the GUID identifying a function to import, and the value 34 /// is the threshold applied when deciding to import it. 35 typedef std::map<GlobalValue::GUID, unsigned> FunctionsToImportTy; 36 37 /// The map contains an entry for every module to import from, the key being 38 /// the module identifier to pass to the ModuleLoader. The value is the set of 39 /// functions to import. 40 typedef StringMap<FunctionsToImportTy> ImportMapTy; 41 42 /// The set contains an entry for every global value the module exports. 43 typedef std::unordered_set<GlobalValue::GUID> ExportSetTy; 44 45 /// Create a Function Importer. 46 FunctionImporter( 47 const ModuleSummaryIndex &Index, 48 std::function<std::unique_ptr<Module>(StringRef Identifier)> ModuleLoader) 49 : Index(Index), ModuleLoader(std::move(ModuleLoader)) {} 50 51 /// Import functions in Module \p M based on the supplied import list. 52 /// \p ForceImportReferencedDiscardableSymbols will set the ModuleLinker in 53 /// a mode where referenced discarable symbols in the source modules will be 54 /// imported as well even if they are not present in the ImportList. 55 bool importFunctions(Module &M, const ImportMapTy &ImportList, 56 bool ForceImportReferencedDiscardableSymbols = false); 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 std::function<std::unique_ptr<Module>(StringRef Identifier)> ModuleLoader; 64 }; 65 66 /// Compute all the imports and exports for every module in the Index. 67 /// 68 /// \p ModuleToDefinedGVSummaries contains for each Module a map 69 /// (GUID -> Summary) for every global defined in the module. 70 /// 71 /// \p ImportLists will be populated with an entry for every Module we are 72 /// importing into. This entry is itself a map that can be passed to 73 /// FunctionImporter::importFunctions() above (see description there). 74 /// 75 /// \p ExportLists contains for each Module the set of globals (GUID) that will 76 /// be imported by another module, or referenced by such a function. I.e. this 77 /// is the set of globals that need to be promoted/renamed appropriately. 78 void ComputeCrossModuleImport( 79 const ModuleSummaryIndex &Index, 80 const StringMap<GVSummaryMapTy> &ModuleToDefinedGVSummaries, 81 StringMap<FunctionImporter::ImportMapTy> &ImportLists, 82 StringMap<FunctionImporter::ExportSetTy> &ExportLists); 83 84 /// Compute all the imports for the given module using the Index. 85 /// 86 /// \p ImportList will be populated with a map that can be passed to 87 /// FunctionImporter::importFunctions() above (see description there). 88 void ComputeCrossModuleImportForModule( 89 StringRef ModulePath, const ModuleSummaryIndex &Index, 90 FunctionImporter::ImportMapTy &ImportList); 91 92 /// Compute the set of summaries needed for a ThinLTO backend compilation of 93 /// \p ModulePath. 94 // 95 /// This includes summaries from that module (in case any global summary based 96 /// optimizations were recorded) and from any definitions in other modules that 97 /// should be imported. 98 // 99 /// \p ModuleToSummariesForIndex will be populated with the needed summaries 100 /// from each required module path. Use a std::map instead of StringMap to get 101 /// stable order for bitcode emission. 102 void gatherImportedSummariesForModule( 103 StringRef ModulePath, 104 const StringMap<GVSummaryMapTy> &ModuleToDefinedGVSummaries, 105 const StringMap<FunctionImporter::ImportMapTy> &ImportLists, 106 std::map<std::string, GVSummaryMapTy> &ModuleToSummariesForIndex); 107 108 std::error_code 109 EmitImportsFiles(StringRef ModulePath, StringRef OutputFilename, 110 const StringMap<FunctionImporter::ImportMapTy> &ImportLists); 111 112 /// Resolve WeakForLinker values in \p TheModule based on the information 113 /// recorded in the summaries during global summary-based analysis. 114 void thinLTOResolveWeakForLinkerModule(Module &TheModule, 115 const GVSummaryMapTy &DefinedGlobals); 116 117 /// Internalize \p TheModule based on the information recorded in the summaries 118 /// during global summary-based analysis. 119 void thinLTOInternalizeModule(Module &TheModule, 120 const GVSummaryMapTy &DefinedGlobals); 121 } 122 123 #endif // LLVM_FUNCTIONIMPORT_H 124