LLVM 18.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"
25#include "llvm/Pass.h"
26#include "llvm/Support/Error.h"
29using namespace llvm;
30
31namespace llvm {
32
33// This struct represents the cluster information for a machine basic block,
34// which is specifed by a unique ID (`MachineBasicBlock::BBID`).
36 // Basic block ID.
38 // Cluster ID this basic block belongs to.
39 unsigned ClusterID;
40 // Position of basic block within the cluster.
42};
43
44// This represents the raw input profile for one function.
46 // BB Cluster information specified by `UniqueBBID`s.
48 // Paths to clone. A path a -> b -> c -> d implies cloning b, c, and d along
49 // the edge a -> b (a is not cloned). The index of the path in this vector
50 // determines the `UniqueBBID::CloneID` of the cloned blocks in that path.
52};
53
54// Provides DenseMapInfo for UniqueBBID.
55template <> struct DenseMapInfo<UniqueBBID> {
56 static inline UniqueBBID getEmptyKey() {
57 unsigned EmptyKey = DenseMapInfo<unsigned>::getEmptyKey();
58 return UniqueBBID{EmptyKey, EmptyKey};
59 }
60 static inline UniqueBBID getTombstoneKey() {
61 unsigned TombstoneKey = DenseMapInfo<unsigned>::getTombstoneKey();
62 return UniqueBBID{TombstoneKey, TombstoneKey};
63 }
64 static unsigned getHashValue(const UniqueBBID &Val) {
65 std::pair<unsigned, unsigned> PairVal =
66 std::make_pair(Val.BaseID, Val.CloneID);
67 return DenseMapInfo<std::pair<unsigned, unsigned>>::getHashValue(PairVal);
68 }
69 static bool isEqual(const UniqueBBID &LHS, const UniqueBBID &RHS) {
70 return DenseMapInfo<unsigned>::isEqual(LHS.BaseID, RHS.BaseID) &&
71 DenseMapInfo<unsigned>::isEqual(LHS.CloneID, RHS.CloneID);
72 }
73};
74
76public:
77 static char ID;
78
80 : ImmutablePass(ID), MBuf(Buf),
81 LineIt(*Buf, /*SkipBlanks=*/true, /*CommentMarker=*/'#') {
84 };
85
89 }
90
91 StringRef getPassName() const override {
92 return "Basic Block Sections Profile Reader";
93 }
94
95 // Returns true if basic block sections profile exist for function \p
96 // FuncName.
97 bool isFunctionHot(StringRef FuncName) const;
98
99 // Returns a pair with first element representing whether basic block sections
100 // profile exist for the function \p FuncName, and the second element
101 // representing the basic block sections profile (cluster info) for this
102 // function. If the first element is true and the second element is empty, it
103 // means unique basic block sections are desired for all basic blocks of the
104 // function.
105 std::pair<bool, SmallVector<BBClusterInfo>>
107
108 // Returns the path clonings for the given function.
111
112 // Initializes the FunctionNameToDIFilename map for the current module and
113 // then reads the profile for the matching functions.
114 bool doInitialization(Module &M) override;
115
116private:
117 StringRef getAliasName(StringRef FuncName) const {
118 auto R = FuncAliasMap.find(FuncName);
119 return R == FuncAliasMap.end() ? FuncName : R->second;
120 }
121
122 // Returns a profile parsing error for the current line.
123 Error createProfileParseError(Twine Message) const {
124 return make_error<StringError>(
125 Twine("invalid profile " + MBuf->getBufferIdentifier() + " at line " +
126 Twine(LineIt.line_number()) + ": " + Message),
128 }
129
130 // Parses a `UniqueBBID` from `S`. `S` must be in the form "<bbid>"
131 // (representing an original block) or "<bbid>.<cloneid>" (representing a
132 // cloned block) where bbid is a non-negative integer and cloneid is a
133 // positive integer.
134 Expected<UniqueBBID> parseUniqueBBID(StringRef S) const;
135
136 // Reads the basic block sections profile for functions in this module.
137 Error ReadProfile();
138
139 // Reads version 0 profile.
140 // TODO: Remove this function once version 0 is deprecated.
141 Error ReadV0Profile();
142
143 // Reads version 1 profile.
144 Error ReadV1Profile();
145
146 // This contains the basic-block-sections profile.
147 const MemoryBuffer *MBuf = nullptr;
148
149 // Iterator to the line being parsed.
150 line_iterator LineIt;
151
152 // Map from every function name in the module to its debug info filename or
153 // empty string if no debug info is available.
154 StringMap<SmallString<128>> FunctionNameToDIFilename;
155
156 // This contains the BB cluster information for the whole program.
157 //
158 // For every function name, it contains the cloning and cluster information
159 // for (all or some of) its basic blocks. The cluster information for every
160 // basic block includes its cluster ID along with the position of the basic
161 // block in that cluster.
162 StringMap<FunctionPathAndClusterInfo> ProgramPathAndClusterInfo;
163
164 // Some functions have alias names. We use this map to find the main alias
165 // name which appears in ProgramPathAndClusterInfo as a key.
166 StringMap<StringRef> FuncAliasMap;
167};
168
169// Creates a BasicBlockSectionsProfileReader pass to parse the basic block
170// sections profile. \p Buf is a memory buffer that contains the list of
171// functions and basic block ids to selectively enable basic block sections.
174
175} // namespace llvm
176#endif // LLVM_CODEGEN_BASICBLOCKSECTIONSPROFILEREADER_H
This file defines the StringMap class.
basic Basic Alias true
Module.h This file contains the declarations for the Module class.
This file defines the SmallString class.
This file defines the SmallVector class.
Value * RHS
Value * LHS
bool isFunctionHot(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
SmallVector< SmallVector< unsigned > > getClonePathsForFunction(StringRef FuncName) const
bool doInitialization(Module &M) override
doInitialization - Virtual method overridden by subclasses to do any necessary initialization before ...
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:1200
StringMap - This is an unconventional map that is specialized for handling keys that are "strings",...
Definition: StringMap.h:112
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:50
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 initializeBasicBlockSectionsProfileReaderPass(PassRegistry &)
ImmutablePass * createBasicBlockSectionsProfileReaderPass(const MemoryBuffer *Buf)
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