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"
22#include "llvm/IR/Module.h"
24#include "llvm/Pass.h"
25#include "llvm/Support/Error.h"
28
29namespace llvm {
30
31// The cluster information for a machine basic block.
33 // Unique ID for this basic block.
34 unsigned BBID;
35 // Cluster ID this basic block belongs to.
36 unsigned ClusterID;
37 // Position of basic block within the cluster.
39};
40
42
44public:
45 static char ID;
46
48 : ImmutablePass(ID), MBuf(Buf),
49 LineIt(*Buf, /*SkipBlanks=*/true, /*CommentMarker=*/'#') {
52 };
53
57 }
58
59 StringRef getPassName() const override {
60 return "Basic Block Sections Profile Reader";
61 }
62
63 // Returns true if basic block sections profile exist for function \p
64 // FuncName.
65 bool isFunctionHot(StringRef FuncName) const;
66
67 // Returns a pair with first element representing whether basic block sections
68 // profile exist for the function \p FuncName, and the second element
69 // representing the basic block sections profile (cluster info) for this
70 // function. If the first element is true and the second element is empty, it
71 // means unique basic block sections are desired for all basic blocks of the
72 // function.
73 std::pair<bool, SmallVector<BBClusterInfo>>
75
76 // Initializes the FunctionNameToDIFilename map for the current module and
77 // then reads the profile for matching functions.
78 bool doInitialization(Module &M) override;
79
80private:
81 StringRef getAliasName(StringRef FuncName) const {
82 auto R = FuncAliasMap.find(FuncName);
83 return R == FuncAliasMap.end() ? FuncName : R->second;
84 }
85
86 // Returns a profile parsing error for the current line.
87 Error createProfileParseError(Twine Message) const {
88 return make_error<StringError>(
89 Twine("invalid profile " + MBuf->getBufferIdentifier() + " at line " +
90 Twine(LineIt.line_number()) + ": " + Message),
92 }
93
94 // Reads the basic block sections profile for functions in this module.
95 Error ReadProfile();
96
97 // Reads version 0 profile.
98 // TODO: Remove this function once version 0 is deprecated.
99 Error ReadV0Profile();
100
101 // Reads version 1 profile.
102 Error ReadV1Profile();
103
104 // This contains the basic-block-sections profile.
105 const MemoryBuffer *MBuf = nullptr;
106
107 // Iterator to the line being parsed.
108 line_iterator LineIt;
109
110 // Map from every function name in the module to its debug info filename or
111 // empty string if no debug info is available.
112 StringMap<SmallString<128>> FunctionNameToDIFilename;
113
114 // This encapsulates the BB cluster information for the whole program.
115 //
116 // For every function name, it contains the cluster information for (all or
117 // some of) its basic blocks. The cluster information for every basic block
118 // includes its cluster ID along with the position of the basic block in that
119 // cluster.
120 ProgramBBClusterInfoMapTy ProgramBBClusterInfo;
121
122 // Some functions have alias names. We use this map to find the main alias
123 // name for which we have mapping in ProgramBBClusterInfo.
124 StringMap<StringRef> FuncAliasMap;
125};
126
127// Creates a BasicBlockSectionsProfileReader pass to parse the basic block
128// sections profile. \p Buf is a memory buffer that contains the list of
129// functions and basic block ids to selectively enable basic block sections.
130ImmutablePass *
131createBasicBlockSectionsProfileReaderPass(const MemoryBuffer *Buf);
132
133} // namespace llvm
134#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.
bool isFunctionHot(StringRef FuncName) const
StringRef getPassName() const override
getPassName - Return a nice clean name for a pass.
std::pair< bool, SmallVector< BBClusterInfo > > getBBClusterInfoForFunction(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
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...
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
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
StringMap< SmallVector< BBClusterInfo > > ProgramBBClusterInfoMapTy
void initializeBasicBlockSectionsProfileReaderPass(PassRegistry &)
ImmutablePass * createBasicBlockSectionsProfileReaderPass(const MemoryBuffer *Buf)