LLVM 19.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"
23#include "llvm/IR/Module.h"
24#include "llvm/IR/PassManager.h"
26#include "llvm/Pass.h"
27#include "llvm/Support/Error.h"
31
32using namespace llvm;
33
34namespace llvm {
35
36// This struct represents the cluster information for a machine basic block,
37// which is specifed by a unique ID (`MachineBasicBlock::BBID`).
39 // Basic block ID.
41 // Cluster ID this basic block belongs to.
42 unsigned ClusterID;
43 // Position of basic block within the cluster.
45};
46
47// This represents the raw input profile for one function.
49 // BB Cluster information specified by `UniqueBBID`s.
51 // Paths to clone. A path a -> b -> c -> d implies cloning b, c, and d along
52 // the edge a -> b (a is not cloned). The index of the path in this vector
53 // determines the `UniqueBBID::CloneID` of the cloned blocks in that path.
55};
56
57// Provides DenseMapInfo for UniqueBBID.
58template <> struct DenseMapInfo<UniqueBBID> {
59 static inline UniqueBBID getEmptyKey() {
60 unsigned EmptyKey = DenseMapInfo<unsigned>::getEmptyKey();
61 return UniqueBBID{EmptyKey, EmptyKey};
62 }
63 static inline UniqueBBID getTombstoneKey() {
64 unsigned TombstoneKey = DenseMapInfo<unsigned>::getTombstoneKey();
65 return UniqueBBID{TombstoneKey, TombstoneKey};
66 }
67 static unsigned getHashValue(const UniqueBBID &Val) {
68 std::pair<unsigned, unsigned> PairVal =
69 std::make_pair(Val.BaseID, Val.CloneID);
70 return DenseMapInfo<std::pair<unsigned, unsigned>>::getHashValue(PairVal);
71 }
72 static bool isEqual(const UniqueBBID &LHS, const UniqueBBID &RHS) {
73 return DenseMapInfo<unsigned>::isEqual(LHS.BaseID, RHS.BaseID) &&
74 DenseMapInfo<unsigned>::isEqual(LHS.CloneID, RHS.CloneID);
75 }
76};
77
79public:
82 : MBuf(Buf), LineIt(*Buf, /*SkipBlanks=*/true, /*CommentMarker=*/'#'){};
83
85
86 // Returns true if basic block sections profile exist for function \p
87 // FuncName.
88 bool isFunctionHot(StringRef FuncName) const;
89
90 // Returns a pair with first element representing whether basic block sections
91 // profile exist for the function \p FuncName, and the second element
92 // representing the basic block sections profile (cluster info) for this
93 // function. If the first element is true and the second element is empty, it
94 // means unique basic block sections are desired for all basic blocks of the
95 // function.
96 std::pair<bool, SmallVector<BBClusterInfo>>
98
99 // Returns the path clonings for the given function.
102
103private:
104 StringRef getAliasName(StringRef FuncName) const {
105 auto R = FuncAliasMap.find(FuncName);
106 return R == FuncAliasMap.end() ? FuncName : R->second;
107 }
108
109 // Returns a profile parsing error for the current line.
110 Error createProfileParseError(Twine Message) const {
111 return make_error<StringError>(
112 Twine("invalid profile " + MBuf->getBufferIdentifier() + " at line " +
113 Twine(LineIt.line_number()) + ": " + Message),
115 }
116
117 // Parses a `UniqueBBID` from `S`. `S` must be in the form "<bbid>"
118 // (representing an original block) or "<bbid>.<cloneid>" (representing a
119 // cloned block) where bbid is a non-negative integer and cloneid is a
120 // positive integer.
121 Expected<UniqueBBID> parseUniqueBBID(StringRef S) const;
122
123 // Reads the basic block sections profile for functions in this module.
124 Error ReadProfile();
125
126 // Reads version 0 profile.
127 // TODO: Remove this function once version 0 is deprecated.
128 Error ReadV0Profile();
129
130 // Reads version 1 profile.
131 Error ReadV1Profile();
132
133 // This contains the basic-block-sections profile.
134 const MemoryBuffer *MBuf = nullptr;
135
136 // Iterator to the line being parsed.
137 line_iterator LineIt;
138
139 // Map from every function name in the module to its debug info filename or
140 // empty string if no debug info is available.
141 StringMap<SmallString<128>> FunctionNameToDIFilename;
142
143 // This contains the BB cluster information for the whole program.
144 //
145 // For every function name, it contains the cloning and cluster information
146 // for (all or some of) its basic blocks. The cluster information for every
147 // basic block includes its cluster ID along with the position of the basic
148 // block in that cluster.
149 StringMap<FunctionPathAndClusterInfo> ProgramPathAndClusterInfo;
150
151 // Some functions have alias names. We use this map to find the main alias
152 // name which appears in ProgramPathAndClusterInfo as a key.
153 StringMap<StringRef> FuncAliasMap;
154};
155
156// Creates a BasicBlockSectionsProfileReader pass to parse the basic block
157// sections profile. \p Buf is a memory buffer that contains the list of
158// functions and basic block ids to selectively enable basic block sections.
161
162/// Analysis pass providing the \c BasicBlockSectionsProfileReader.
163///
164/// Note that this pass's result cannot be invalidated, it is immutable for the
165/// life of the module.
167 : public AnalysisInfoMixin<BasicBlockSectionsProfileReaderAnalysis> {
168
169public:
173
175
176private:
177 const TargetMachine *TM;
178};
179
181public:
182 static char ID;
184
189 };
190
195 }
196
197 StringRef getPassName() const override {
198 return "Basic Block Sections Profile Reader";
199 }
200
201 bool isFunctionHot(StringRef FuncName) const;
202
203 std::pair<bool, SmallVector<BBClusterInfo>>
204 getClusterInfoForFunction(StringRef FuncName) const;
205
207 getClonePathsForFunction(StringRef FuncName) const;
208
209 // Initializes the FunctionNameToDIFilename map for the current module and
210 // then reads the profile for the matching functions.
211 bool doInitialization(Module &M) override;
212
214};
215
216} // namespace llvm
217#endif // LLVM_CODEGEN_BASICBLOCKSECTIONSPROFILEREADER_H
This file defines the StringMap class.
basic Basic Alias true
#define F(x, y, z)
Definition: MD5.cpp:55
Module.h This file contains the declarations for the Module class.
const char LLVMTargetMachineRef TM
This header defines various interfaces for pass management in LLVM.
This file defines the SmallString class.
This file defines the SmallVector class.
Value * RHS
Value * LHS
A container for analyses that lazily runs them and caches their results.
Definition: PassManager.h:321
Analysis pass providing the BasicBlockSectionsProfileReader.
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
StringRef getPassName() const override
getPassName - Return a nice clean name for a pass.
std::pair< bool, SmallVector< BBClusterInfo > > getClusterInfoForFunction(StringRef FuncName) const
bool isFunctionHot(StringRef FuncName) const
std::pair< bool, SmallVector< BBClusterInfo > > getClusterInfoForFunction(StringRef FuncName) const
SmallVector< SmallVector< unsigned > > getClonePathsForFunction(StringRef FuncName) const
Lightweight error class with error context and mandatory checking.
Definition: Error.h:160
Tagged union holding either a T or a Error.
Definition: Error.h:474
ImmutablePass class - This class is used to provide information that does not need to be run.
Definition: Pass.h:282
This interface provides simple read-only access to a block of memory, and provides simple methods for...
Definition: MemoryBuffer.h:51
virtual StringRef getBufferIdentifier() const
Return an identifier for this buffer, typically the filename it was read from.
Definition: MemoryBuffer.h:76
A Module instance is used to store all the information related to an LLVM module.
Definition: Module.h:65
static 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.
Definition: SmallVector.h:1209
StringMap - This is an unconventional map that is specialized for handling keys that are "strings",...
Definition: StringMap.h:127
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:50
Primary interface to the complete machine description for the target machine.
Definition: TargetMachine.h:76
Twine - A lightweight data structure for efficiently representing the concatenation of temporary valu...
Definition: Twine.h:81
A forward iterator which reads text lines from a buffer.
Definition: LineIterator.h:33
int64_t line_number() const
Return the current line number. May return any number at EOF.
Definition: LineIterator.h:66
This is an optimization pass for GlobalISel generic memory operations.
Definition: AddressRanges.h:18
std::error_code inconvertibleErrorCode()
The value returned by this function can be returned from convertToErrorCode for Error values where no...
Definition: Error.cpp:90
void initializeBasicBlockSectionsProfileReaderWrapperPassPass(PassRegistry &)
ImmutablePass * createBasicBlockSectionsProfileReaderWrapperPass(const MemoryBuffer *Buf)
A CRTP mix-in that provides informational APIs needed for analysis passes.
Definition: PassManager.h:97
A special type used by analysis passes to provide an address that identifies that particular analysis...
Definition: Analysis.h:26
static bool isEqual(const UniqueBBID &LHS, const UniqueBBID &RHS)
static unsigned getHashValue(const UniqueBBID &Val)
An information struct used to provide DenseMap with the various necessary components for a given valu...
Definition: DenseMapInfo.h:50
SmallVector< SmallVector< unsigned > > ClonePaths