LLVM  7.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_TRANSFORMS_IPO_FUNCTIONIMPORT_H
11 #define LLVM_TRANSFORMS_IPO_FUNCTIONIMPORT_H
12 
13 #include "llvm/ADT/DenseSet.h"
14 #include "llvm/ADT/StringMap.h"
15 #include "llvm/ADT/StringRef.h"
16 #include "llvm/IR/GlobalValue.h"
18 #include "llvm/IR/PassManager.h"
19 #include "llvm/Support/Error.h"
20 #include <functional>
21 #include <map>
22 #include <memory>
23 #include <string>
24 #include <system_error>
25 #include <unordered_set>
26 #include <utility>
27 
28 namespace llvm {
29 
30 class Module;
31 
32 /// The function importer is automatically importing function from other modules
33 /// based on the provided summary informations.
35 public:
36  /// Set of functions to import from a source module. Each entry is a set
37  /// containing all the GUIDs of all functions to import for a source module.
38  using FunctionsToImportTy = std::unordered_set<GlobalValue::GUID>;
39 
40  /// Map of callee GUID considered for import into a given module to a pair
41  /// consisting of the largest threshold applied when deciding whether to
42  /// import it and, if we decided to import, a pointer to the summary instance
43  /// imported. If we decided not to import, the summary will be nullptr.
44  using ImportThresholdsTy =
46  std::pair<unsigned, const GlobalValueSummary *>>;
47 
48  /// The map contains an entry for every module to import from, the key being
49  /// the module identifier to pass to the ModuleLoader. The value is the set of
50  /// functions to import.
52 
53  /// The set contains an entry for every global value the module exports.
54  using ExportSetTy = std::unordered_set<GlobalValue::GUID>;
55 
56  /// A function of this type is used to load modules referenced by the index.
57  using ModuleLoaderTy =
58  std::function<Expected<std::unique_ptr<Module>>(StringRef Identifier)>;
59 
60  /// Create a Function Importer.
62  : Index(Index), ModuleLoader(std::move(ModuleLoader)) {}
63 
64  /// Import functions in Module \p M based on the supplied import list.
65  Expected<bool> importFunctions(Module &M, const ImportMapTy &ImportList);
66 
67 private:
68  /// The summaries index used to trigger importing.
69  const ModuleSummaryIndex &Index;
70 
71  /// Factory function to load a Module for a given identifier
72  ModuleLoaderTy ModuleLoader;
73 };
74 
75 /// The function importing pass
76 class FunctionImportPass : public PassInfoMixin<FunctionImportPass> {
77 public:
79 };
80 
81 /// Compute all the imports and exports for every module in the Index.
82 ///
83 /// \p ModuleToDefinedGVSummaries contains for each Module a map
84 /// (GUID -> Summary) for every global defined in the module.
85 ///
86 /// \p ImportLists will be populated with an entry for every Module we are
87 /// importing into. This entry is itself a map that can be passed to
88 /// FunctionImporter::importFunctions() above (see description there).
89 ///
90 /// \p ExportLists contains for each Module the set of globals (GUID) that will
91 /// be imported by another module, or referenced by such a function. I.e. this
92 /// is the set of globals that need to be promoted/renamed appropriately.
94  const ModuleSummaryIndex &Index,
95  const StringMap<GVSummaryMapTy> &ModuleToDefinedGVSummaries,
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 /// Mark all external summaries in \p Index for import into the given module.
108 /// Used for distributed builds using a distributed index.
109 ///
110 /// \p ImportList will be populated with a map that can be passed to
111 /// FunctionImporter::importFunctions() above (see description there).
113  StringRef ModulePath, const ModuleSummaryIndex &Index,
114  FunctionImporter::ImportMapTy &ImportList);
115 
116 /// PrevailingType enum used as a return type of callback passed
117 /// to computeDeadSymbols. Yes and No values used when status explicitly
118 /// set by symbols resolution, otherwise status is Unknown.
119 enum class PrevailingType { Yes, No, Unknown };
120 
121 /// Compute all the symbols that are "dead": i.e these that can't be reached
122 /// in the graph from any of the given symbols listed in
123 /// \p GUIDPreservedSymbols. Non-prevailing symbols are symbols without a
124 /// prevailing copy anywhere in IR and are normally dead, \p isPrevailing
125 /// predicate returns status of symbol.
126 void computeDeadSymbols(
127  ModuleSummaryIndex &Index,
128  const DenseSet<GlobalValue::GUID> &GUIDPreservedSymbols,
130 
131 /// Converts value \p GV to declaration, or replaces with a declaration if
132 /// it is an alias. Returns true if converted, false if replaced.
134 
135 /// Compute the set of summaries needed for a ThinLTO backend compilation of
136 /// \p ModulePath.
137 //
138 /// This includes summaries from that module (in case any global summary based
139 /// optimizations were recorded) and from any definitions in other modules that
140 /// should be imported.
141 //
142 /// \p ModuleToSummariesForIndex will be populated with the needed summaries
143 /// from each required module path. Use a std::map instead of StringMap to get
144 /// stable order for bitcode emission.
146  StringRef ModulePath,
147  const StringMap<GVSummaryMapTy> &ModuleToDefinedGVSummaries,
148  const FunctionImporter::ImportMapTy &ImportList,
149  std::map<std::string, GVSummaryMapTy> &ModuleToSummariesForIndex);
150 
151 /// Emit into \p OutputFilename the files module \p ModulePath will import from.
152 std::error_code EmitImportsFiles(
153  StringRef ModulePath, StringRef OutputFilename,
154  const std::map<std::string, GVSummaryMapTy> &ModuleToSummariesForIndex);
155 
156 /// Resolve WeakForLinker values in \p TheModule based on the information
157 /// recorded in the summaries during global summary-based analysis.
159  const GVSummaryMapTy &DefinedGlobals);
160 
161 /// Internalize \p TheModule based on the information recorded in the summaries
162 /// during global summary-based analysis.
163 void thinLTOInternalizeModule(Module &TheModule,
164  const GVSummaryMapTy &DefinedGlobals);
165 
166 } // end namespace llvm
167 
168 #endif // LLVM_TRANSFORMS_IPO_FUNCTIONIMPORT_H
uint64_t GUID
Declare a type to represent a global unique identifier for a global value.
Definition: GlobalValue.h:492
Compute iterated dominance frontiers using a linear time algorithm.
Definition: AllocatorList.h:24
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::error_code EmitImportsFiles(StringRef ModulePath, StringRef OutputFilename, const std::map< std::string, GVSummaryMapTy > &ModuleToSummariesForIndex)
Emit into OutputFilename the files module ModulePath will import from.
bool convertToDeclaration(GlobalValue &GV)
Converts value GV to declaration, or replaces with a declaration if it is an alias.
An efficient, type-erasing, non-owning reference to a callable.
Definition: STLExtras.h:106
Definition: BitVector.h:921
FunctionImporter(const ModuleSummaryIndex &Index, ModuleLoaderTy ModuleLoader)
Create a Function Importer.
std::unordered_set< GlobalValue::GUID > ExportSetTy
The set contains an entry for every global value the module exports.
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.
PrevailingType
PrevailingType enum used as a return type of callback passed to computeDeadSymbols.
void ComputeCrossModuleImportForModuleFromIndex(StringRef ModulePath, const ModuleSummaryIndex &Index, FunctionImporter::ImportMapTy &ImportList)
Mark all external summaries in Index for import into the given module.
std::function< Expected< std::unique_ptr< Module > >(StringRef Identifier)> ModuleLoaderTy
A function of this type is used to load modules referenced by the index.
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 ...
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...
std::unordered_set< GlobalValue::GUID > FunctionsToImportTy
Set of functions to import from a source module.
The function importer is automatically importing function from other modules based on the provided su...
void computeDeadSymbols(ModuleSummaryIndex &Index, const DenseSet< GlobalValue::GUID > &GUIDPreservedSymbols, function_ref< PrevailingType(GlobalValue::GUID)> isPrevailing)
Compute all the symbols that are "dead": i.e these that can&#39;t be reached in the graph from any of the...
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.