LLVM  4.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  /// \p ForceImportReferencedDiscardableSymbols will set the ModuleLinker in
57  /// a mode where referenced discarable symbols in the source modules will be
58  /// imported as well even if they are not present in the ImportList.
60  importFunctions(Module &M, const ImportMapTy &ImportList,
61  bool ForceImportReferencedDiscardableSymbols = false);
62 
63 private:
64  /// The summaries index used to trigger importing.
65  const ModuleSummaryIndex &Index;
66 
67  /// Factory function to load a Module for a given identifier
68  ModuleLoaderTy ModuleLoader;
69 };
70 
71 /// The function importing pass
72 class FunctionImportPass : public PassInfoMixin<FunctionImportPass> {
73 public:
75 };
76 
77 /// Compute all the imports and exports for every module in the Index.
78 ///
79 /// \p ModuleToDefinedGVSummaries contains for each Module a map
80 /// (GUID -> Summary) for every global defined in the module.
81 ///
82 /// \p ImportLists will be populated with an entry for every Module we are
83 /// importing into. This entry is itself a map that can be passed to
84 /// FunctionImporter::importFunctions() above (see description there).
85 ///
86 /// \p ExportLists contains for each Module the set of globals (GUID) that will
87 /// be imported by another module, or referenced by such a function. I.e. this
88 /// is the set of globals that need to be promoted/renamed appropriately.
89 ///
90 /// \p DeadSymbols (optional) contains a list of GUID that are deemed "dead" and
91 /// will be ignored for the purpose of importing.
93  const ModuleSummaryIndex &Index,
94  const StringMap<GVSummaryMapTy> &ModuleToDefinedGVSummaries,
97  const DenseSet<GlobalValue::GUID> *DeadSymbols = nullptr);
98 
99 /// Compute all the imports for the given module using the Index.
100 ///
101 /// \p ImportList will be populated with a map that can be passed to
102 /// FunctionImporter::importFunctions() above (see description there).
104  StringRef ModulePath, const ModuleSummaryIndex &Index,
105  FunctionImporter::ImportMapTy &ImportList);
106 
107 /// Compute all the symbols that are "dead": i.e these that can't be reached
108 /// in the graph from any of the given symbols listed in
109 /// \p GUIDPreservedSymbols.
112  const DenseSet<GlobalValue::GUID> &GUIDPreservedSymbols);
113 
114 /// Compute the set of summaries needed for a ThinLTO backend compilation of
115 /// \p ModulePath.
116 //
117 /// This includes summaries from that module (in case any global summary based
118 /// optimizations were recorded) and from any definitions in other modules that
119 /// should be imported.
120 //
121 /// \p ModuleToSummariesForIndex will be populated with the needed summaries
122 /// from each required module path. Use a std::map instead of StringMap to get
123 /// stable order for bitcode emission.
125  StringRef ModulePath,
126  const StringMap<GVSummaryMapTy> &ModuleToDefinedGVSummaries,
127  const FunctionImporter::ImportMapTy &ImportList,
128  std::map<std::string, GVSummaryMapTy> &ModuleToSummariesForIndex);
129 
130 /// Emit into \p OutputFilename the files module \p ModulePath will import from.
131 std::error_code
133  const FunctionImporter::ImportMapTy &ModuleImports);
134 
135 /// Resolve WeakForLinker values in \p TheModule based on the information
136 /// recorded in the summaries during global summary-based analysis.
138  const GVSummaryMapTy &DefinedGlobals);
139 
140 /// Internalize \p TheModule based on the information recorded in the summaries
141 /// during global summary-based analysis.
142 void thinLTOInternalizeModule(Module &TheModule,
143  const GVSummaryMapTy &DefinedGlobals);
144 }
145 
146 #endif // LLVM_FUNCTIONIMPORT_H
PreservedAnalyses run(Module &M, ModuleAnalysisManager &AM)
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:52
Implements a dense probed hash-table based set.
Definition: DenseSet.h:202
std::map< GlobalValue::GUID, unsigned > FunctionsToImportTy
Set of functions to import from a source module.
Expected< bool > importFunctions(Module &M, const ImportMapTy &ImportList, bool ForceImportReferencedDiscardableSymbols=false)
Import functions in Module M based on the supplied import list.
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.
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:311
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:107
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.
void ComputeCrossModuleImport(const ModuleSummaryIndex &Index, const StringMap< GVSummaryMapTy > &ModuleToDefinedGVSummaries, StringMap< FunctionImporter::ImportMapTy > &ImportLists, StringMap< FunctionImporter::ExportSetTy > &ExportLists, const DenseSet< GlobalValue::GUID > *DeadSymbols=nullptr)
Compute all the imports and exports for every module in the Index.
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:223
std::map< GlobalValue::GUID, GlobalValueSummary * > GVSummaryMapTy
Map of global value GUID to its summary, used to identify values defined in a particular module...
DenseSet< GlobalValue::GUID > computeDeadSymbols(const 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...
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:47
A container for analyses that lazily runs them and caches their results.
This header defines various interfaces for pass management in LLVM.