LLVM  8.0.0svn
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  /// The different reasons selectCallee will chose not to import a
41  /// candidate.
44  // We can encounter a global variable instead of a function in rare
45  // situations with SamplePGO. See comments where this failure type is
46  // set for more details.
48  // Found to be globally dead, so we don't bother importing.
50  // Instruction count over the current threshold.
52  // Don't import something with interposable linkage as we can't inline it
53  // anyway.
55  // Generally we won't end up failing due to this reason, as we expect
56  // to find at least one summary for the GUID that is global or a local
57  // in the referenced module for direct calls.
59  // This corresponse to the NotEligibleToImport being set on the summary,
60  // which can happen in a few different cases (e.g. local that can't be
61  // renamed or promoted because it is referenced on a llvm*.used variable).
63  };
64 
65  /// Information optionally tracked for candidates the importer decided
66  /// not to import. Used for optional stat printing.
68  // The ValueInfo corresponding to the candidate. We save an index hash
69  // table lookup for each GUID by stashing this here.
71  // The maximum call edge hotness for all failed imports of this candidate.
73  // most recent reason for failing to import (doesn't necessarily correspond
74  // to the attempt with the maximum hotness).
76  // The number of times we tried to import candidate but failed.
77  unsigned Attempts;
79  ImportFailureReason Reason, unsigned Attempts)
80  : VI(VI), MaxHotness(MaxHotness), Reason(Reason), Attempts(Attempts) {}
81  };
82 
83  /// Map of callee GUID considered for import into a given module to a pair
84  /// consisting of the largest threshold applied when deciding whether to
85  /// import it and, if we decided to import, a pointer to the summary instance
86  /// imported. If we decided not to import, the summary will be nullptr.
87  using ImportThresholdsTy =
89  std::tuple<unsigned, const GlobalValueSummary *,
90  std::unique_ptr<ImportFailureInfo>>>;
91 
92  /// The map contains an entry for every module to import from, the key being
93  /// the module identifier to pass to the ModuleLoader. The value is the set of
94  /// functions to import.
96 
97  /// The set contains an entry for every global value the module exports.
98  using ExportSetTy = std::unordered_set<GlobalValue::GUID>;
99 
100  /// A function of this type is used to load modules referenced by the index.
101  using ModuleLoaderTy =
102  std::function<Expected<std::unique_ptr<Module>>(StringRef Identifier)>;
103 
104  /// Create a Function Importer.
106  : Index(Index), ModuleLoader(std::move(ModuleLoader)) {}
107 
108  /// Import functions in Module \p M based on the supplied import list.
109  Expected<bool> importFunctions(Module &M, const ImportMapTy &ImportList);
110 
111 private:
112  /// The summaries index used to trigger importing.
113  const ModuleSummaryIndex &Index;
114 
115  /// Factory function to load a Module for a given identifier
116  ModuleLoaderTy ModuleLoader;
117 };
118 
119 /// The function importing pass
120 class FunctionImportPass : public PassInfoMixin<FunctionImportPass> {
121 public:
123 };
124 
125 /// Compute all the imports and exports for every module in the Index.
126 ///
127 /// \p ModuleToDefinedGVSummaries contains for each Module a map
128 /// (GUID -> Summary) for every global defined in the module.
129 ///
130 /// \p ImportLists will be populated with an entry for every Module we are
131 /// importing into. This entry is itself a map that can be passed to
132 /// FunctionImporter::importFunctions() above (see description there).
133 ///
134 /// \p ExportLists contains for each Module the set of globals (GUID) that will
135 /// be imported by another module, or referenced by such a function. I.e. this
136 /// is the set of globals that need to be promoted/renamed appropriately.
138  const ModuleSummaryIndex &Index,
139  const StringMap<GVSummaryMapTy> &ModuleToDefinedGVSummaries,
142 
143 /// Compute all the imports for the given module using the Index.
144 ///
145 /// \p ImportList will be populated with a map that can be passed to
146 /// FunctionImporter::importFunctions() above (see description there).
148  StringRef ModulePath, const ModuleSummaryIndex &Index,
149  FunctionImporter::ImportMapTy &ImportList);
150 
151 /// Mark all external summaries in \p Index for import into the given module.
152 /// Used for distributed builds using a distributed index.
153 ///
154 /// \p ImportList will be populated with a map that can be passed to
155 /// FunctionImporter::importFunctions() above (see description there).
157  StringRef ModulePath, const ModuleSummaryIndex &Index,
158  FunctionImporter::ImportMapTy &ImportList);
159 
160 /// PrevailingType enum used as a return type of callback passed
161 /// to computeDeadSymbols. Yes and No values used when status explicitly
162 /// set by symbols resolution, otherwise status is Unknown.
163 enum class PrevailingType { Yes, No, Unknown };
164 
165 /// Compute all the symbols that are "dead": i.e these that can't be reached
166 /// in the graph from any of the given symbols listed in
167 /// \p GUIDPreservedSymbols. Non-prevailing symbols are symbols without a
168 /// prevailing copy anywhere in IR and are normally dead, \p isPrevailing
169 /// predicate returns status of symbol.
170 void computeDeadSymbols(
171  ModuleSummaryIndex &Index,
172  const DenseSet<GlobalValue::GUID> &GUIDPreservedSymbols,
174 
175 /// Converts value \p GV to declaration, or replaces with a declaration if
176 /// it is an alias. Returns true if converted, false if replaced.
178 
179 /// Compute the set of summaries needed for a ThinLTO backend compilation of
180 /// \p ModulePath.
181 //
182 /// This includes summaries from that module (in case any global summary based
183 /// optimizations were recorded) and from any definitions in other modules that
184 /// should be imported.
185 //
186 /// \p ModuleToSummariesForIndex will be populated with the needed summaries
187 /// from each required module path. Use a std::map instead of StringMap to get
188 /// stable order for bitcode emission.
190  StringRef ModulePath,
191  const StringMap<GVSummaryMapTy> &ModuleToDefinedGVSummaries,
192  const FunctionImporter::ImportMapTy &ImportList,
193  std::map<std::string, GVSummaryMapTy> &ModuleToSummariesForIndex);
194 
195 /// Emit into \p OutputFilename the files module \p ModulePath will import from.
196 std::error_code EmitImportsFiles(
197  StringRef ModulePath, StringRef OutputFilename,
198  const std::map<std::string, GVSummaryMapTy> &ModuleToSummariesForIndex);
199 
200 /// Resolve WeakForLinker values in \p TheModule based on the information
201 /// recorded in the summaries during global summary-based analysis.
203  const GVSummaryMapTy &DefinedGlobals);
204 
205 /// Internalize \p TheModule based on the information recorded in the summaries
206 /// during global summary-based analysis.
207 void thinLTOInternalizeModule(Module &TheModule,
208  const GVSummaryMapTy &DefinedGlobals);
209 
210 } // end namespace llvm
211 
212 #endif // LLVM_TRANSFORMS_IPO_FUNCTIONIMPORT_H
ImportFailureInfo(ValueInfo VI, CalleeInfo::HotnessType MaxHotness, ImportFailureReason Reason, unsigned Attempts)
uint64_t GUID
Declare a type to represent a global unique identifier for a global value.
Definition: GlobalValue.h:493
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:64
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:107
Definition: BitVector.h:938
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:366
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:154
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.
ImportFailureReason
The different reasons selectCallee will chose not to import a candidate.
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.
Function and variable summary information to aid decisions and implementation of importing.
Struct that holds a reference to a particular GUID in a global value summary.
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 ...
Information optionally tracked for candidates the importer decided not to import. ...
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.