LLVM  5.0.0
FunctionImport.h
Go to the documentation of this file.
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"
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.
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.
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)>
50 
51  /// Create a Function Importer.
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:
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.
85  const ModuleSummaryIndex &Index,
86  const StringMap<GVSummaryMapTy> &ModuleToDefinedGVSummaries,
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).
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.
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
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.
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
PreservedAnalyses run(Module &M, ModuleAnalysisManager &AM)
void computeDeadSymbols(ModuleSummaryIndex &Index, const DenseSet< GlobalValue::GUID > &GUIDPreservedSymbols)
Compute all the symbols that are "dead": i.e these that can't be reached in the graph from any of t...
std::function< Expected< std::unique_ptr< Module > >StringRef Identifier)> ModuleLoaderTy
A function of this type is used to load modules referenced by the index.
A Module instance is used to store all the information related to an LLVM module. ...
Definition: Module.h:63
Implements a dense probed hash-table based set.
Definition: DenseSet.h:221
std::map< GlobalValue::GUID, unsigned > FunctionsToImportTy
Set of functions to import from a source module.
std::unordered_set< GlobalValue::GUID > ExportSetTy
The set contains an entry for every global value the module exports.
FunctionImporter(const ModuleSummaryIndex &Index, ModuleLoaderTy ModuleLoader)
Create a Function Importer.
Tagged union holding either a T or a Error.
Definition: CachePruning.h:23
static cl::opt< std::string > OutputFilename("o", cl::desc("Output filename"), cl::value_desc("filename"), cl::init("-"))
A CRTP mix-in to automatically provide informational APIs needed for passes.
Definition: PassManager.h:365
Class to hold module path string table and global value map, and encapsulate methods for operating on...
The function importing pass.
A set of analyses that are preserved following a run of a transformation pass.
Definition: PassManager.h:153
void gatherImportedSummariesForModule(StringRef ModulePath, const StringMap< GVSummaryMapTy > &ModuleToDefinedGVSummaries, const FunctionImporter::ImportMapTy &ImportList, std::map< std::string, GVSummaryMapTy > &ModuleToSummariesForIndex)
Compute the set of summaries needed for a ThinLTO backend compilation of ModulePath.
std::error_code EmitImportsFiles(StringRef ModulePath, StringRef OutputFilename, const FunctionImporter::ImportMapTy &ModuleImports)
Emit into OutputFilename the files module ModulePath will import from.
StringMap - This is an unconventional map that is specialized for handling keys that are "strings"...
Definition: StringMap.h:220
Expected< bool > importFunctions(Module &M, const ImportMapTy &ImportList)
Import functions in Module M based on the supplied import list.
void ComputeCrossModuleImport(const ModuleSummaryIndex &Index, const StringMap< GVSummaryMapTy > &ModuleToDefinedGVSummaries, StringMap< FunctionImporter::ImportMapTy > &ImportLists, StringMap< FunctionImporter::ExportSetTy > &ExportLists)
Compute all the imports and exports for every module in the Index.
void thinLTOInternalizeModule(Module &TheModule, const GVSummaryMapTy &DefinedGlobals)
Internalize TheModule based on the information recorded in the summaries during global summary-based ...
StringMap< FunctionsToImportTy > ImportMapTy
The map contains an entry for every module to import from, the key being the module identifier to pas...
void thinLTOResolveWeakForLinkerModule(Module &TheModule, const GVSummaryMapTy &DefinedGlobals)
Resolve WeakForLinker values in TheModule based on the information recorded in the summaries during g...
void ComputeCrossModuleImportForModule(StringRef ModulePath, const ModuleSummaryIndex &Index, FunctionImporter::ImportMapTy &ImportList)
Compute all the imports for the given module using the Index.
ModuleSummaryIndex.h This file contains the declarations the classes that hold the module index and s...
The function importer is automatically importing function from other modules based on the provided su...
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:49
A container for analyses that lazily runs them and caches their results.
This header defines various interfaces for pass management in LLVM.