LLVM 19.0.0git
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/StringRef.h"
14#include "llvm/IR/GlobalValue.h"
16#include "llvm/IR/PassManager.h"
17#include "llvm/Support/Error.h"
18#include <functional>
19#include <map>
20#include <memory>
21#include <string>
22#include <system_error>
23#include <unordered_set>
24#include <utility>
25
26namespace llvm {
27
28class Module;
29
30/// The function importer is automatically importing function from other modules
31/// based on the provided summary informations.
33public:
34 /// Set of functions to import from a source module. Each entry is a set
35 /// containing all the GUIDs of all functions to import for a source module.
36 using FunctionsToImportTy = std::unordered_set<GlobalValue::GUID>;
37
38 /// The different reasons selectCallee will chose not to import a
39 /// candidate.
42 // We can encounter a global variable instead of a function in rare
43 // situations with SamplePGO. See comments where this failure type is
44 // set for more details.
46 // Found to be globally dead, so we don't bother importing.
48 // Instruction count over the current threshold.
50 // Don't import something with interposable linkage as we can't inline it
51 // anyway.
53 // Generally we won't end up failing due to this reason, as we expect
54 // to find at least one summary for the GUID that is global or a local
55 // in the referenced module for direct calls.
57 // This corresponds to the NotEligibleToImport being set on the summary,
58 // which can happen in a few different cases (e.g. local that can't be
59 // renamed or promoted because it is referenced on a llvm*.used variable).
61 // This corresponds to NoInline being set on the function summary,
62 // which will happen if it is known that the inliner will not be able
63 // to inline the function (e.g. it is marked with a NoInline attribute).
65 };
66
67 /// Information optionally tracked for candidates the importer decided
68 /// not to import. Used for optional stat printing.
70 // The ValueInfo corresponding to the candidate. We save an index hash
71 // table lookup for each GUID by stashing this here.
73 // The maximum call edge hotness for all failed imports of this candidate.
75 // most recent reason for failing to import (doesn't necessarily correspond
76 // to the attempt with the maximum hotness).
78 // The number of times we tried to import candidate but failed.
79 unsigned Attempts;
83 };
84
85 /// Map of callee GUID considered for import into a given module to a pair
86 /// consisting of the largest threshold applied when deciding whether to
87 /// import it and, if we decided to import, a pointer to the summary instance
88 /// imported. If we decided not to import, the summary will be nullptr.
91 std::tuple<unsigned, const GlobalValueSummary *,
92 std::unique_ptr<ImportFailureInfo>>>;
93
94 /// The map contains an entry for every module to import from, the key being
95 /// the module identifier to pass to the ModuleLoader. The value is the set of
96 /// functions to import. The module identifier strings must be owned
97 /// elsewhere, typically by the in-memory ModuleSummaryIndex the importing
98 /// decisions are made from (the module path for each summary is owned by the
99 /// index's module path string table).
101
102 /// The set contains an entry for every global value the module exports.
104
105 /// A function of this type is used to load modules referenced by the index.
107 std::function<Expected<std::unique_ptr<Module>>(StringRef Identifier)>;
108
109 /// Create a Function Importer.
111 bool ClearDSOLocalOnDeclarations)
112 : Index(Index), ModuleLoader(std::move(ModuleLoader)),
113 ClearDSOLocalOnDeclarations(ClearDSOLocalOnDeclarations) {}
114
115 /// Import functions in Module \p M based on the supplied import list.
116 Expected<bool> importFunctions(Module &M, const ImportMapTy &ImportList);
117
118private:
119 /// The summaries index used to trigger importing.
121
122 /// Factory function to load a Module for a given identifier
123 ModuleLoaderTy ModuleLoader;
124
125 /// See the comment of ClearDSOLocalOnDeclarations in
126 /// Utils/FunctionImportUtils.h.
127 bool ClearDSOLocalOnDeclarations;
128};
129
130/// The function importing pass
131class FunctionImportPass : public PassInfoMixin<FunctionImportPass> {
132public:
134};
135
136/// Compute all the imports and exports for every module in the Index.
137///
138/// \p ModuleToDefinedGVSummaries contains for each Module a map
139/// (GUID -> Summary) for every global defined in the module.
140///
141/// \p isPrevailing is a callback that will be called with a global value's GUID
142/// and summary and should return whether the module corresponding to the
143/// summary contains the linker-prevailing copy of that value.
144///
145/// \p ImportLists will be populated with an entry for every Module we are
146/// importing into. This entry is itself a map that can be passed to
147/// FunctionImporter::importFunctions() above (see description there).
148///
149/// \p ExportLists contains for each Module the set of globals (GUID) that will
150/// be imported by another module, or referenced by such a function. I.e. this
151/// is the set of globals that need to be promoted/renamed appropriately.
152///
153/// The module identifier strings that are the keys of the above two maps
154/// are owned by the in-memory ModuleSummaryIndex the importing decisions
155/// are made from (the module path for each summary is owned by the index's
156/// module path string table).
159 const DenseMap<StringRef, GVSummaryMapTy> &ModuleToDefinedGVSummaries,
161 isPrevailing,
164
165/// PrevailingType enum used as a return type of callback passed
166/// to computeDeadSymbolsAndUpdateIndirectCalls. Yes and No values used when
167/// status explicitly set by symbols resolution, otherwise status is Unknown.
168enum class PrevailingType { Yes, No, Unknown };
169
170/// Update call edges for indirect calls to local functions added from
171/// SamplePGO when needed. Normally this is done during
172/// computeDeadSymbolsAndUpdateIndirectCalls, but can be called standalone
173/// when that is not called (e.g. during testing).
174void updateIndirectCalls(ModuleSummaryIndex &Index);
175
176/// Compute all the symbols that are "dead": i.e these that can't be reached
177/// in the graph from any of the given symbols listed in
178/// \p GUIDPreservedSymbols. Non-prevailing symbols are symbols without a
179/// prevailing copy anywhere in IR and are normally dead, \p isPrevailing
180/// predicate returns status of symbol.
181/// Also update call edges for indirect calls to local functions added from
182/// SamplePGO when needed.
184 ModuleSummaryIndex &Index,
185 const DenseSet<GlobalValue::GUID> &GUIDPreservedSymbols,
186 function_ref<PrevailingType(GlobalValue::GUID)> isPrevailing);
187
188/// Compute dead symbols and run constant propagation in combined index
189/// after that.
191 ModuleSummaryIndex &Index,
192 const DenseSet<GlobalValue::GUID> &GUIDPreservedSymbols,
193 function_ref<PrevailingType(GlobalValue::GUID)> isPrevailing,
194 bool ImportEnabled);
195
196/// Converts value \p GV to declaration, or replaces with a declaration if
197/// it is an alias. Returns true if converted, false if replaced.
198bool convertToDeclaration(GlobalValue &GV);
199
200/// Compute the set of summaries needed for a ThinLTO backend compilation of
201/// \p ModulePath.
202//
203/// This includes summaries from that module (in case any global summary based
204/// optimizations were recorded) and from any definitions in other modules that
205/// should be imported.
206//
207/// \p ModuleToSummariesForIndex will be populated with the needed summaries
208/// from each required module path. Use a std::map instead of StringMap to get
209/// stable order for bitcode emission.
211 StringRef ModulePath,
212 const DenseMap<StringRef, GVSummaryMapTy> &ModuleToDefinedGVSummaries,
213 const FunctionImporter::ImportMapTy &ImportList,
214 std::map<std::string, GVSummaryMapTy> &ModuleToSummariesForIndex);
215
216/// Emit into \p OutputFilename the files module \p ModulePath will import from.
217std::error_code EmitImportsFiles(
218 StringRef ModulePath, StringRef OutputFilename,
219 const std::map<std::string, GVSummaryMapTy> &ModuleToSummariesForIndex);
220
221/// Based on the information recorded in the summaries during global
222/// summary-based analysis:
223/// 1. Resolve prevailing symbol linkages and constrain visibility (CanAutoHide
224/// and consider visibility from other definitions for ELF) in \p TheModule
225/// 2. (optional) Apply propagated function attributes to \p TheModule if
226/// PropagateAttrs is true
227void thinLTOFinalizeInModule(Module &TheModule,
228 const GVSummaryMapTy &DefinedGlobals,
229 bool PropagateAttrs);
230
231/// Internalize \p TheModule based on the information recorded in the summaries
232/// during global summary-based analysis.
233void thinLTOInternalizeModule(Module &TheModule,
234 const GVSummaryMapTy &DefinedGlobals);
235
236} // end namespace llvm
237
238#endif // LLVM_TRANSFORMS_IPO_FUNCTIONIMPORT_H
This file defines the DenseSet and SmallDenseSet classes.
Machine Check Debug Module
static cl::opt< std::string > OutputFilename("o", cl::desc("Output filename"), cl::value_desc("filename"), cl::init("-"))
static cl::opt< bool > PropagateAttrs("propagate-attrs", cl::init(true), cl::Hidden, cl::desc("Propagate attributes in index"))
ModuleSummaryIndex.h This file contains the declarations the classes that hold the module index and s...
This header defines various interfaces for pass management in LLVM.
A container for analyses that lazily runs them and caches their results.
Definition: PassManager.h:321
Implements a dense probed hash-table based set.
Definition: DenseSet.h:271
Tagged union holding either a T or a Error.
Definition: Error.h:474
The function importing pass.
PreservedAnalyses run(Module &M, ModuleAnalysisManager &AM)
The function importer is automatically importing function from other modules based on the provided su...
Expected< bool > importFunctions(Module &M, const ImportMapTy &ImportList)
Import functions in Module M based on the supplied import list.
DenseMap< StringRef, FunctionsToImportTy > ImportMapTy
The map contains an entry for every module to import from, the key being the module identifier to pas...
std::unordered_set< GlobalValue::GUID > FunctionsToImportTy
Set of functions to import from a source module.
FunctionImporter(const ModuleSummaryIndex &Index, ModuleLoaderTy ModuleLoader, bool ClearDSOLocalOnDeclarations)
Create a Function Importer.
ImportFailureReason
The different reasons selectCallee will chose not to import a candidate.
std::function< Expected< std::unique_ptr< Module > >(StringRef Identifier)> ModuleLoaderTy
A function of this type is used to load modules referenced by the index.
Function and variable summary information to aid decisions and implementation of importing.
uint64_t GUID
Declare a type to represent a global unique identifier for a global value.
Definition: GlobalValue.h:587
Class to hold module path string table and global value map, and encapsulate methods for operating on...
A Module instance is used to store all the information related to an LLVM module.
Definition: Module.h:65
A set of analyses that are preserved following a run of a transformation pass.
Definition: Analysis.h:109
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:50
An efficient, type-erasing, non-owning reference to a callable.
This is an optimization pass for GlobalISel generic memory operations.
Definition: AddressRanges.h:18
void ComputeCrossModuleImport(const ModuleSummaryIndex &Index, const DenseMap< StringRef, GVSummaryMapTy > &ModuleToDefinedGVSummaries, function_ref< bool(GlobalValue::GUID, const GlobalValueSummary *)> isPrevailing, DenseMap< StringRef, FunctionImporter::ImportMapTy > &ImportLists, DenseMap< StringRef, FunctionImporter::ExportSetTy > &ExportLists)
Compute all the imports and exports for every module in the Index.
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.
void computeDeadSymbolsAndUpdateIndirectCalls(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't be reached in the graph from any of the...
void gatherImportedSummariesForModule(StringRef ModulePath, const DenseMap< StringRef, 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 updateIndirectCalls(ModuleSummaryIndex &Index)
Update call edges for indirect calls to local functions added from SamplePGO when needed.
void thinLTOInternalizeModule(Module &TheModule, const GVSummaryMapTy &DefinedGlobals)
Internalize TheModule based on the information recorded in the summaries during global summary-based ...
OutputIt move(R &&Range, OutputIt Out)
Provide wrappers to std::move which take ranges instead of having to pass begin/end explicitly.
Definition: STLExtras.h:1849
PrevailingType
PrevailingType enum used as a return type of callback passed to computeDeadSymbolsAndUpdateIndirectCa...
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.
DenseMap< GlobalValue::GUID, GlobalValueSummary * > GVSummaryMapTy
Map of global value GUID to its summary, used to identify values defined in a particular module,...
void thinLTOFinalizeInModule(Module &TheModule, const GVSummaryMapTy &DefinedGlobals, bool PropagateAttrs)
Based on the information recorded in the summaries during global summary-based analysis:
Implement std::hash so that hash_code can be used in STL containers.
Definition: BitVector.h:858
Information optionally tracked for candidates the importer decided not to import.
ImportFailureInfo(ValueInfo VI, CalleeInfo::HotnessType MaxHotness, ImportFailureReason Reason, unsigned Attempts)
A CRTP mix-in to automatically provide informational APIs needed for passes.
Definition: PassManager.h:74
Struct that holds a reference to a particular GUID in a global value summary.