LLVM 22.0.0git
BasicBlockSectionsProfileReader.h
Go to the documentation of this file.
1//===-- BasicBlockSectionsProfileReader.h - BB sections profile reader pass ==//
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// This pass creates the basic block cluster info by reading the basic block
10// sections profile. The cluster info will be used by the basic-block-sections
11// pass to arrange basic blocks in their sections.
12//
13//===----------------------------------------------------------------------===//
14
15#ifndef LLVM_CODEGEN_BASICBLOCKSECTIONSPROFILEREADER_H
16#define LLVM_CODEGEN_BASICBLOCKSECTIONSPROFILEREADER_H
17
20#include "llvm/ADT/StringMap.h"
21#include "llvm/ADT/StringRef.h"
22#include "llvm/IR/Module.h"
23#include "llvm/IR/PassManager.h"
25#include "llvm/Pass.h"
26#include "llvm/Support/Error.h"
31
32namespace llvm {
33
34// This struct represents the cluster information for a machine basic block,
35// which is specifed by a unique basic block ID.
37 // Basic block ID.
39 // Cluster ID this basic block belongs to.
40 unsigned ClusterID;
41 // Position of basic block within the cluster.
43};
44
45// This represents the CFG profile data for a function.
46struct CFGProfile {
47 // Node counts for each basic block.
49 // Edge counts for each edge, stored as a nested map.
51
52 // Hash for each basic block. The Hashes are stored for every original block
53 // (not cloned blocks), hence the map key being unsigned instead of
54 // UniqueBBID.
56
57 // Returns the profile count for the given basic block or zero if it does not
58 // exist.
59 uint64_t getBlockCount(const UniqueBBID &BBID) const {
60 return NodeCounts.lookup(BBID);
61 }
62
63 // Returns the profile count for the edge from `SrcBBID` to `SinkBBID` or
64 // zero if it does not exist.
66 const UniqueBBID &SinkBBID) const {
67 auto It = EdgeCounts.find(SrcBBID);
68 if (It == EdgeCounts.end())
69 return 0;
70 return It->second.lookup(SinkBBID);
71 }
72};
73
74// The prefetch symbol is emitted immediately after the call of the given index,
75// in block `BBID` (First call has an index of 1). Zero callsite index means the
76// start of the block.
81
82// This struct represents the raw optimization profile for a function,
83// including CFG data (block and edge counts) and layout directives (clustering
84// and cloning paths).
86 // BB Cluster information specified by `UniqueBBID`s.
88 // Paths to clone. A path a -> b -> c -> d implies cloning b, c, and d along
89 // the edge a -> b (a is not cloned). The index of the path in this vector
90 // determines the `UniqueBBID::CloneID` of the cloned blocks in that path.
92 // Cfg profile data (block and edge frequencies).
94 // Code prefetch targets, specified by the callsite ID. The target is the code
95 // immediately following this callsite.
97 // Node counts for each basic block.
99 // Edge counts for each edge.
101 // Hash for each basic block. The Hashes are stored for every original block
102 // (not cloned blocks), hence the map key being unsigned instead of
103 // UniqueBBID.
105};
106
108public:
111 : MBuf(Buf), LineIt(*Buf, /*SkipBlanks=*/true, /*CommentMarker=*/'#'){};
112
114
115 // Returns true if function \p FuncName is hot based on the basic block
116 // section profile.
117 bool isFunctionHot(StringRef FuncName) const;
118
119 // Returns the cluster info for the function \p FuncName. Returns an empty
120 // vector if function has no cluster info.
123
124 // Returns the path clonings for the given function.
127
128 uint64_t getEdgeCount(StringRef FuncName, const UniqueBBID &SrcBBID,
129 const UniqueBBID &DestBBID) const;
130
131 // Returns a pointer to the CFGProfile for the function \p FuncName.
132 // Returns nullptr if no profile data is available for the function.
134 auto It = ProgramOptimizationProfile.find(getAliasName(FuncName));
135 if (It == ProgramOptimizationProfile.end())
136 return nullptr;
137 return &It->second.CFG;
138 }
139
140 // Returns the prefetch targets (identified by their containing callsite IDs)
141 // for function `FuncName`.
144
145private:
146 StringRef getAliasName(StringRef FuncName) const {
147 auto R = FuncAliasMap.find(FuncName);
148 return R == FuncAliasMap.end() ? FuncName : R->second;
149 }
150
151 // Returns a profile parsing error for the current line.
152 Error createProfileParseError(Twine Message) const {
154 Twine("invalid profile " + MBuf->getBufferIdentifier() + " at line " +
155 Twine(LineIt.line_number()) + ": " + Message),
157 }
158
159 // Parses a `UniqueBBID` from `S`. `S` must be in the form "<bbid>"
160 // (representing an original block) or "<bbid>.<cloneid>" (representing a
161 // cloned block) where bbid is a non-negative integer and cloneid is a
162 // positive integer.
163 Expected<UniqueBBID> parseUniqueBBID(StringRef S) const;
164
165 // Reads the basic block sections profile for functions in this module.
166 Error ReadProfile();
167
168 // Reads version 0 profile.
169 // TODO: Remove this function once version 0 is deprecated.
170 Error ReadV0Profile();
171
172 // Reads version 1 profile.
173 Error ReadV1Profile();
174
175 // This contains the basic-block-sections profile.
176 const MemoryBuffer *MBuf = nullptr;
177
178 // Iterator to the line being parsed.
179 line_iterator LineIt;
180
181 // Map from every function name in the module to its debug info filename or
182 // empty string if no debug info is available.
183 StringMap<SmallString<128>> FunctionNameToDIFilename;
184
185 // This map contains the optimization profile for each function in the
186 // program. A function's optimization profile consists of CFG data (node and
187 // edge counts) and layout directives such as basic block clustering and
188 // cloning paths.
189 StringMap<FunctionOptimizationProfile> ProgramOptimizationProfile;
190
191 // Some functions have alias names. We use this map to find the main alias
192 // name which appears in ProgramOptimizationProfile as a key.
193 StringMap<StringRef> FuncAliasMap;
194};
195
196// Creates a BasicBlockSectionsProfileReader pass to parse the basic block
197// sections profile. \p Buf is a memory buffer that contains the list of
198// functions and basic block ids to selectively enable basic block sections.
201
202/// Analysis pass providing the \c BasicBlockSectionsProfileReader.
203///
204/// Note that this pass's result cannot be invalidated, it is immutable for the
205/// life of the module.
207 : public AnalysisInfoMixin<BasicBlockSectionsProfileReaderAnalysis> {
208
209public:
213
215
216private:
217 const TargetMachine *TM;
218};
219
221public:
222 static char ID;
224
230
236
237 StringRef getPassName() const override {
238 return "Basic Block Sections Profile Reader";
239 }
240
241 bool isFunctionHot(StringRef FuncName) const;
242
244 getClusterInfoForFunction(StringRef FuncName) const;
245
247 getClonePathsForFunction(StringRef FuncName) const;
248
249 const CFGProfile *getFunctionCFGProfile(StringRef FuncName) const;
250
251 uint64_t getEdgeCount(StringRef FuncName, const UniqueBBID &SrcBBID,
252 const UniqueBBID &DestBBID) const;
253
256
257 // Initializes the FunctionNameToDIFilename map for the current module and
258 // then reads the profile for the matching functions.
259 bool doInitialization(Module &M) override;
260
262};
263
264} // namespace llvm
265#endif // LLVM_CODEGEN_BASICBLOCKSECTIONSPROFILEREADER_H
This file defines the StringMap class.
Module.h This file contains the declarations for the Module class.
This header defines various interfaces for pass management in LLVM.
#define F(x, y, z)
Definition MD5.cpp:54
This file defines the SmallString class.
This file defines the SmallVector class.
Result run(Function &F, FunctionAnalysisManager &AM)
bool doInitialization(Module &M) override
doInitialization - Virtual method overridden by subclasses to do any necessary initialization before ...
SmallVector< SmallVector< unsigned > > getClonePathsForFunction(StringRef FuncName) const
SmallVector< BBClusterInfo > getClusterInfoForFunction(StringRef FuncName) const
SmallVector< CallsiteID > getPrefetchTargetsForFunction(StringRef FuncName) const
const CFGProfile * getFunctionCFGProfile(StringRef FuncName) const
uint64_t getEdgeCount(StringRef FuncName, const UniqueBBID &SrcBBID, const UniqueBBID &DestBBID) const
StringRef getPassName() const override
getPassName - Return a nice clean name for a pass.
bool isFunctionHot(StringRef FuncName) const
SmallVector< SmallVector< unsigned > > getClonePathsForFunction(StringRef FuncName) const
const CFGProfile * getFunctionCFGProfile(StringRef FuncName) const
SmallVector< CallsiteID > getPrefetchTargetsForFunction(StringRef FuncName) const
uint64_t getEdgeCount(StringRef FuncName, const UniqueBBID &SrcBBID, const UniqueBBID &DestBBID) const
SmallVector< BBClusterInfo > getClusterInfoForFunction(StringRef FuncName) const
Lightweight error class with error context and mandatory checking.
Definition Error.h:159
ImmutablePass class - This class is used to provide information that does not need to be run.
Definition Pass.h:285
ImmutablePass(char &pid)
Definition Pass.h:287
This interface provides simple read-only access to a block of memory, and provides simple methods for...
virtual StringRef getBufferIdentifier() const
Return an identifier for this buffer, typically the filename it was read from.
A Module instance is used to store all the information related to an LLVM module.
Definition Module.h:67
static LLVM_ABI PassRegistry * getPassRegistry()
getPassRegistry - Access the global registry object, which is automatically initialized at applicatio...
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small.
StringRef - Represent a constant reference to a string, i.e.
Definition StringRef.h:55
Primary interface to the complete machine description for the target machine.
Twine - A lightweight data structure for efficiently representing the concatenation of temporary valu...
Definition Twine.h:82
int64_t line_number() const
Return the current line number. May return any number at EOF.
This is an optimization pass for GlobalISel generic memory operations.
Definition Types.h:26
LLVM_ABI void initializeBasicBlockSectionsProfileReaderWrapperPassPass(PassRegistry &)
LLVM_ABI std::error_code inconvertibleErrorCode()
The value returned by this function can be returned from convertToErrorCode for Error values where no...
Definition Error.cpp:98
ImmutablePass * createBasicBlockSectionsProfileReaderWrapperPass(const MemoryBuffer *Buf)
Error make_error(ArgTs &&... Args)
Make a Error instance representing failure using the given error info type.
Definition Error.h:340
AnalysisManager< Function > FunctionAnalysisManager
Convenience typedef for the Function analysis manager.
A CRTP mix-in that provides informational APIs needed for analysis passes.
Definition PassManager.h:93
A special type used by analysis passes to provide an address that identifies that particular analysis...
Definition Analysis.h:29
DenseMap< unsigned, uint64_t > BBHashes
DenseMap< UniqueBBID, uint64_t > NodeCounts
DenseMap< UniqueBBID, DenseMap< UniqueBBID, uint64_t > > EdgeCounts
uint64_t getEdgeCount(const UniqueBBID &SrcBBID, const UniqueBBID &SinkBBID) const
uint64_t getBlockCount(const UniqueBBID &BBID) const
DenseMap< UniqueBBID, uint64_t > NodeCounts
DenseMap< UniqueBBID, DenseMap< UniqueBBID, uint64_t > > EdgeCounts
SmallVector< SmallVector< unsigned > > ClonePaths