LLVM  9.0.0svn
FunctionImport.h
Go to the documentation of this file.
1 //===- llvm/Transforms/IPO/FunctionImport.h - ThinLTO importing -*- C++ -*-===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 
9 #ifndef LLVM_TRANSFORMS_IPO_FUNCTIONIMPORT_H
10 #define LLVM_TRANSFORMS_IPO_FUNCTIONIMPORT_H
11 
12 #include "llvm/ADT/DenseSet.h"
13 #include "llvm/ADT/StringMap.h"
14 #include "llvm/ADT/StringRef.h"
15 #include "llvm/IR/GlobalValue.h"
17 #include "llvm/IR/PassManager.h"
18 #include "llvm/Support/Error.h"
19 #include <functional>
20 #include <map>
21 #include <memory>
22 #include <string>
23 #include <system_error>
24 #include <unordered_set>
25 #include <utility>
26 
27 namespace llvm {
28 
29 class Module;
30 
31 /// The function importer is automatically importing function from other modules
32 /// based on the provided summary informations.
34 public:
35  /// Set of functions to import from a source module. Each entry is a set
36  /// containing all the GUIDs of all functions to import for a source module.
37  using FunctionsToImportTy = std::unordered_set<GlobalValue::GUID>;
38 
39  /// The different reasons selectCallee will chose not to import a
40  /// candidate.
43  // We can encounter a global variable instead of a function in rare
44  // situations with SamplePGO. See comments where this failure type is
45  // set for more details.
47  // Found to be globally dead, so we don't bother importing.
49  // Instruction count over the current threshold.
51  // Don't import something with interposable linkage as we can't inline it
52  // anyway.
54  // Generally we won't end up failing due to this reason, as we expect
55  // to find at least one summary for the GUID that is global or a local
56  // in the referenced module for direct calls.
58  // This corresponds to the NotEligibleToImport being set on the summary,
59  // which can happen in a few different cases (e.g. local that can't be
60  // renamed or promoted because it is referenced on a llvm*.used variable).
62  // This corresponds to NoInline being set on the function summary,
63  // which will happen if it is known that the inliner will not be able
64  // to inline the function (e.g. it is marked with a NoInline attribute).
66  };
67 
68  /// Information optionally tracked for candidates the importer decided
69  /// not to import. Used for optional stat printing.
71  // The ValueInfo corresponding to the candidate. We save an index hash
72  // table lookup for each GUID by stashing this here.
74  // The maximum call edge hotness for all failed imports of this candidate.
76  // most recent reason for failing to import (doesn't necessarily correspond
77  // to the attempt with the maximum hotness).
79  // The number of times we tried to import candidate but failed.
80  unsigned Attempts;
82  ImportFailureReason Reason, unsigned Attempts)
83  : VI(VI), MaxHotness(MaxHotness), Reason(Reason), Attempts(Attempts) {}
84  };
85 
86  /// Map of callee GUID considered for import into a given module to a pair
87  /// consisting of the largest threshold applied when deciding whether to
88  /// import it and, if we decided to import, a pointer to the summary instance
89  /// imported. If we decided not to import, the summary will be nullptr.
90  using ImportThresholdsTy =
92  std::tuple<unsigned, const GlobalValueSummary *,
93  std::unique_ptr<ImportFailureInfo>>>;
94 
95  /// The map contains an entry for every module to import from, the key being
96  /// the module identifier to pass to the ModuleLoader. The value is the set of
97  /// functions to import.
99 
100  /// The set contains an entry for every global value the module exports.
101  using ExportSetTy = std::unordered_set<GlobalValue::GUID>;
102 
103  /// A function of this type is used to load modules referenced by the index.
104  using ModuleLoaderTy =
105  std::function<Expected<std::unique_ptr<Module>>(StringRef Identifier)>;
106 
107  /// Create a Function Importer.
109  : Index(Index), ModuleLoader(std::move(ModuleLoader)) {}
110 
111  /// Import functions in Module \p M based on the supplied import list.
112  Expected<bool> importFunctions(Module &M, const ImportMapTy &ImportList);
113 
114 private:
115  /// The summaries index used to trigger importing.
116  const ModuleSummaryIndex &Index;
117 
118  /// Factory function to load a Module for a given identifier
119  ModuleLoaderTy ModuleLoader;
120 };
121 
122 /// The function importing pass
123 class FunctionImportPass : public PassInfoMixin<FunctionImportPass> {
124 public:
126 };
127 
128 /// Compute all the imports and exports for every module in the Index.
129 ///
130 /// \p ModuleToDefinedGVSummaries contains for each Module a map
131 /// (GUID -> Summary) for every global defined in the module.
132 ///
133 /// \p ImportLists will be populated with an entry for every Module we are
134 /// importing into. This entry is itself a map that can be passed to
135 /// FunctionImporter::importFunctions() above (see description there).
136 ///
137 /// \p ExportLists contains for each Module the set of globals (GUID) that will
138 /// be imported by another module, or referenced by such a function. I.e. this
139 /// is the set of globals that need to be promoted/renamed appropriately.
141  const ModuleSummaryIndex &Index,
142  const StringMap<GVSummaryMapTy> &ModuleToDefinedGVSummaries,
145 
146 /// Compute all the imports for the given module using the Index.
147 ///
148 /// \p ImportList will be populated with a map that can be passed to
149 /// FunctionImporter::importFunctions() above (see description there).
151  StringRef ModulePath, const ModuleSummaryIndex &Index,
152  FunctionImporter::ImportMapTy &ImportList);
153 
154 /// Mark all external summaries in \p Index for import into the given module.
155 /// Used for distributed builds using a distributed index.
156 ///
157 /// \p ImportList will be populated with a map that can be passed to
158 /// FunctionImporter::importFunctions() above (see description there).
160  StringRef ModulePath, const ModuleSummaryIndex &Index,
161  FunctionImporter::ImportMapTy &ImportList);
162 
163 /// PrevailingType enum used as a return type of callback passed
164 /// to computeDeadSymbols. Yes and No values used when status explicitly
165 /// set by symbols resolution, otherwise status is Unknown.
166 enum class PrevailingType { Yes, No, Unknown };
167 
168 /// Compute all the symbols that are "dead": i.e these that can't be reached
169 /// in the graph from any of the given symbols listed in
170 /// \p GUIDPreservedSymbols. Non-prevailing symbols are symbols without a
171 /// prevailing copy anywhere in IR and are normally dead, \p isPrevailing
172 /// predicate returns status of symbol.
173 void computeDeadSymbols(
174  ModuleSummaryIndex &Index,
175  const DenseSet<GlobalValue::GUID> &GUIDPreservedSymbols,
177 
178 /// Compute dead symbols and run constant propagation in combined index
179 /// after that.
181  ModuleSummaryIndex &Index,
182  const DenseSet<GlobalValue::GUID> &GUIDPreservedSymbols,
184  bool ImportEnabled);
185 
186 /// Converts value \p GV to declaration, or replaces with a declaration if
187 /// it is an alias. Returns true if converted, false if replaced.
189 
190 /// Compute the set of summaries needed for a ThinLTO backend compilation of
191 /// \p ModulePath.
192 //
193 /// This includes summaries from that module (in case any global summary based
194 /// optimizations were recorded) and from any definitions in other modules that
195 /// should be imported.
196 //
197 /// \p ModuleToSummariesForIndex will be populated with the needed summaries
198 /// from each required module path. Use a std::map instead of StringMap to get
199 /// stable order for bitcode emission.
201  StringRef ModulePath,
202  const StringMap<GVSummaryMapTy> &ModuleToDefinedGVSummaries,
203  const FunctionImporter::ImportMapTy &ImportList,
204  std::map<std::string, GVSummaryMapTy> &ModuleToSummariesForIndex);
205 
206 /// Emit into \p OutputFilename the files module \p ModulePath will import from.
207 std::error_code EmitImportsFiles(
208  StringRef ModulePath, StringRef OutputFilename,
209  const std::map<std::string, GVSummaryMapTy> &ModuleToSummariesForIndex);
210 
211 /// Resolve prevailing symbol linkages in \p TheModule based on the information
212 /// recorded in the summaries during global summary-based analysis.
214  const GVSummaryMapTy &DefinedGlobals);
215 
216 /// Internalize \p TheModule based on the information recorded in the summaries
217 /// during global summary-based analysis.
218 void thinLTOInternalizeModule(Module &TheModule,
219  const GVSummaryMapTy &DefinedGlobals);
220 
221 } // end namespace llvm
222 
223 #endif // LLVM_TRANSFORMS_IPO_FUNCTIONIMPORT_H
ImportFailureInfo(ValueInfo VI, CalleeInfo::HotnessType MaxHotness, ImportFailureReason Reason, unsigned Attempts)
void thinLTOResolvePrevailingInModule(Module &TheModule, const GVSummaryMapTy &DefinedGlobals)
Resolve prevailing symbol linkages in TheModule based on the information recorded in the summaries du...
uint64_t GUID
Declare a type to represent a global unique identifier for a global value.
Definition: GlobalValue.h:492
This class represents lattice values for constants.
Definition: AllocatorList.h:23
A Module instance is used to store all the information related to an LLVM module. ...
Definition: Module.h:65
Implements a dense probed hash-table based set.
Definition: DenseSet.h:249
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:116
Definition: BitVector.h:937
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:22
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:372
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.
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:219
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 ComputeCrossModuleImportForModule(StringRef ModulePath, const ModuleSummaryIndex &Index, FunctionImporter::ImportMapTy &ImportList)
Compute all the imports for the given module using the Index.
void computeDeadSymbolsWithConstProp(ModuleSummaryIndex &Index, const DenseSet< GlobalValue::GUID > &GUIDPreservedSymbols, function_ref< PrevailingType(GlobalValue::GUID)> isPrevailing, bool ImportEnabled)
Compute dead symbols and run constant propagation in combined index after that.
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:48
A container for analyses that lazily runs them and caches their results.
This header defines various interfaces for pass management in LLVM.