LLVM 18.0.0git
InstrProfCorrelator.h
Go to the documentation of this file.
1//===- InstrProfCorrelator.h ------------------------------------*- 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// This file defines InstrProfCorrelator used to generate PGO profiles from
9// raw profile data and debug info.
10//===----------------------------------------------------------------------===//
11
12#ifndef LLVM_PROFILEDATA_INSTRPROFCORRELATOR_H
13#define LLVM_PROFILEDATA_INSTRPROFCORRELATOR_H
14
15#include "llvm/ADT/DenseSet.h"
17#include "llvm/Support/Error.h"
20#include <optional>
21#include <vector>
22
23namespace llvm {
24class DWARFContext;
25class DWARFDie;
26namespace object {
27class ObjectFile;
28}
29
30/// InstrProfCorrelator - A base class used to create raw instrumentation data
31/// to their functions.
33public:
34 /// Indicate which kind correlator to use.
36
38 get(StringRef Filename, ProfCorrelatorKind FileKind);
39
40 /// Construct a ProfileData vector used to correlate raw instrumentation data
41 /// to their functions.
42 /// \param MaxWarnings the maximum number of warnings to emit (0 = no limit)
43 virtual Error correlateProfileData(int MaxWarnings) = 0;
44
45 /// Process debug info and dump the correlation data.
46 /// \param MaxWarnings the maximum number of warnings to emit (0 = no limit)
47 virtual Error dumpYaml(int MaxWarnings, raw_ostream &OS) = 0;
48
49 /// Return the number of ProfileData elements.
50 std::optional<size_t> getDataSize() const;
51
52 /// Return a pointer to the names string that this class constructs.
53 const char *getNamesPointer() const { return Names.c_str(); }
54
55 /// Return the number of bytes in the names string.
56 size_t getNamesSize() const { return Names.size(); }
57
58 /// Return the size of the counters section in bytes.
60 return Ctx->CountersSectionEnd - Ctx->CountersSectionStart;
61 }
62
63 static const char *FunctionNameAttributeName;
64 static const char *CFGHashAttributeName;
65 static const char *NumCountersAttributeName;
66
68 InstrProfCorrelatorKind getKind() const { return Kind; }
69 virtual ~InstrProfCorrelator() = default;
70
71protected:
72 struct Context {
74 get(std::unique_ptr<MemoryBuffer> Buffer, const object::ObjectFile &Obj);
75 std::unique_ptr<MemoryBuffer> Buffer;
76 /// The address range of the __llvm_prf_cnts section.
79 /// True if target and host have different endian orders.
81 };
82 const std::unique_ptr<Context> Ctx;
83
84 InstrProfCorrelator(InstrProfCorrelatorKind K, std::unique_ptr<Context> Ctx)
85 : Ctx(std::move(Ctx)), Kind(K) {}
86
87 std::string Names;
88 std::vector<std::string> NamesVec;
89
90 struct Probe {
91 std::string FunctionName;
92 std::optional<std::string> LinkageName;
93 yaml::Hex64 CFGHash;
94 yaml::Hex64 CounterOffset;
96 std::optional<std::string> FilePath;
97 std::optional<int> LineNumber;
98 };
99
101 std::vector<Probe> Probes;
102 };
103
104 friend struct yaml::MappingTraits<Probe>;
105 friend struct yaml::SequenceElementTraits<Probe>;
107
108private:
110 get(std::unique_ptr<MemoryBuffer> Buffer, ProfCorrelatorKind FileKind);
111
112 const InstrProfCorrelatorKind Kind;
113};
114
115/// InstrProfCorrelatorImpl - A child of InstrProfCorrelator with a template
116/// pointer type so that the ProfileData vector can be materialized.
117template <class IntPtrT>
119public:
120 InstrProfCorrelatorImpl(std::unique_ptr<InstrProfCorrelator::Context> Ctx);
121 static bool classof(const InstrProfCorrelator *C);
122
123 /// Return a pointer to the underlying ProfileData vector that this class
124 /// constructs.
126 return Data.empty() ? nullptr : Data.data();
127 }
128
129 /// Return the number of ProfileData elements.
130 size_t getDataSize() const { return Data.size(); }
131
133 get(std::unique_ptr<InstrProfCorrelator::Context> Ctx,
134 const object::ObjectFile &Obj, ProfCorrelatorKind FileKind);
135
136protected:
137 std::vector<RawInstrProf::ProfileData<IntPtrT>> Data;
138
139 Error correlateProfileData(int MaxWarnings) override;
141 int MaxWarnings,
143
145
146 Error dumpYaml(int MaxWarnings, raw_ostream &OS) override;
147
148 void addProbe(StringRef FunctionName, uint64_t CFGHash, IntPtrT CounterOffset,
149 IntPtrT FunctionPtr, uint32_t NumCounters);
150
151private:
153 std::unique_ptr<InstrProfCorrelator::Context> Ctx)
154 : InstrProfCorrelator(Kind, std::move(Ctx)){};
155 llvm::DenseSet<IntPtrT> CounterOffsets;
156
157 // Byte-swap the value if necessary.
158 template <class T> T maybeSwap(T Value) const {
159 return Ctx->ShouldSwapBytes ? llvm::byteswap(Value) : Value;
160 }
161};
162
163/// DwarfInstrProfCorrelator - A child of InstrProfCorrelatorImpl that takes
164/// DWARF debug info as input to correlate profiles.
165template <class IntPtrT>
167public:
168 DwarfInstrProfCorrelator(std::unique_ptr<DWARFContext> DICtx,
169 std::unique_ptr<InstrProfCorrelator::Context> Ctx)
171 DICtx(std::move(DICtx)) {}
172
173private:
174 std::unique_ptr<DWARFContext> DICtx;
175
176 /// Return the address of the object that the provided DIE symbolizes.
177 std::optional<uint64_t> getLocation(const DWARFDie &Die) const;
178
179 /// Returns true if the provided DIE symbolizes an instrumentation probe
180 /// symbol.
181 static bool isDIEOfProbe(const DWARFDie &Die);
182
183 /// Iterate over DWARF DIEs to find those that symbolize instrumentation
184 /// probes and construct the ProfileData vector and Names string.
185 ///
186 /// Here is some example DWARF for an instrumentation probe we are looking
187 /// for:
188 /// \code
189 /// DW_TAG_subprogram
190 /// DW_AT_low_pc (0x0000000000000000)
191 /// DW_AT_high_pc (0x0000000000000014)
192 /// DW_AT_name ("foo")
193 /// DW_TAG_variable
194 /// DW_AT_name ("__profc_foo")
195 /// DW_AT_location (DW_OP_addr 0x0)
196 /// DW_TAG_LLVM_annotation
197 /// DW_AT_name ("Function Name")
198 /// DW_AT_const_value ("foo")
199 /// DW_TAG_LLVM_annotation
200 /// DW_AT_name ("CFG Hash")
201 /// DW_AT_const_value (12345678)
202 /// DW_TAG_LLVM_annotation
203 /// DW_AT_name ("Num Counters")
204 /// DW_AT_const_value (2)
205 /// NULL
206 /// NULL
207 /// \endcode
208 /// \param MaxWarnings the maximum number of warnings to emit (0 = no limit)
209 /// \param Data if provided, populate with the correlation data found
210 void correlateProfileDataImpl(
211 int MaxWarnings,
212 InstrProfCorrelator::CorrelationData *Data = nullptr) override;
213
214 Error correlateProfileNameImpl() override;
215};
216
217} // end namespace llvm
218
219#endif // LLVM_PROFILEDATA_INSTRPROFCORRELATOR_H
This file defines the DenseSet and SmallDenseSet classes.
raw_pwrite_stream & OS
Utility class that carries the DWARF compile/type unit and the debug info entry in an object.
Definition: DWARFDie.h:42
Implements a dense probed hash-table based set.
Definition: DenseSet.h:271
DwarfInstrProfCorrelator - A child of InstrProfCorrelatorImpl that takes DWARF debug info as input to...
DwarfInstrProfCorrelator(std::unique_ptr< DWARFContext > DICtx, std::unique_ptr< InstrProfCorrelator::Context > Ctx)
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
InstrProfCorrelatorImpl - A child of InstrProfCorrelator with a template pointer type so that the Pro...
static llvm::Expected< std::unique_ptr< InstrProfCorrelatorImpl< IntPtrT > > > get(std::unique_ptr< InstrProfCorrelator::Context > Ctx, const object::ObjectFile &Obj, ProfCorrelatorKind FileKind)
virtual Error correlateProfileNameImpl()=0
virtual void correlateProfileDataImpl(int MaxWarnings, InstrProfCorrelator::CorrelationData *Data=nullptr)=0
std::vector< RawInstrProf::ProfileData< IntPtrT > > Data
Error correlateProfileData(int MaxWarnings) override
Construct a ProfileData vector used to correlate raw instrumentation data to their functions.
static bool classof(const InstrProfCorrelator *C)
InstrProfCorrelatorImpl(std::unique_ptr< InstrProfCorrelator::Context > Ctx)
const RawInstrProf::ProfileData< IntPtrT > * getDataPointer() const
Return a pointer to the underlying ProfileData vector that this class constructs.
void addProbe(StringRef FunctionName, uint64_t CFGHash, IntPtrT CounterOffset, IntPtrT FunctionPtr, uint32_t NumCounters)
Error dumpYaml(int MaxWarnings, raw_ostream &OS) override
Process debug info and dump the correlation data.
size_t getDataSize() const
Return the number of ProfileData elements.
InstrProfCorrelator - A base class used to create raw instrumentation data to their functions.
virtual Error correlateProfileData(int MaxWarnings)=0
Construct a ProfileData vector used to correlate raw instrumentation data to their functions.
static const char * FunctionNameAttributeName
static const char * CFGHashAttributeName
static llvm::Expected< std::unique_ptr< InstrProfCorrelator > > get(StringRef Filename, ProfCorrelatorKind FileKind)
InstrProfCorrelator(InstrProfCorrelatorKind K, std::unique_ptr< Context > Ctx)
uint64_t getCountersSectionSize() const
Return the size of the counters section in bytes.
std::vector< std::string > NamesVec
static const char * NumCountersAttributeName
virtual Error dumpYaml(int MaxWarnings, raw_ostream &OS)=0
Process debug info and dump the correlation data.
const char * getNamesPointer() const
Return a pointer to the names string that this class constructs.
ProfCorrelatorKind
Indicate which kind correlator to use.
virtual ~InstrProfCorrelator()=default
InstrProfCorrelatorKind getKind() const
const std::unique_ptr< Context > Ctx
std::optional< size_t > getDataSize() const
Return the number of ProfileData elements.
size_t getNamesSize() const
Return the number of bytes in the names string.
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:50
LLVM Value Representation.
Definition: Value.h:74
This class is the base class for all object file types.
Definition: ObjectFile.h:229
This class implements an extremely fast bulk output stream that can only output to a stream.
Definition: raw_ostream.h:52
@ C
The default llvm calling convention, compatible with C.
Definition: CallingConv.h:34
This is an optimization pass for GlobalISel generic memory operations.
Definition: AddressRanges.h:18
constexpr T byteswap(T V) noexcept
Reverses the bytes in the given integer value V.
Definition: bit.h:100
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:1853
Implement std::hash so that hash_code can be used in STL containers.
Definition: BitVector.h:858
static llvm::Expected< std::unique_ptr< Context > > get(std::unique_ptr< MemoryBuffer > Buffer, const object::ObjectFile &Obj)
bool ShouldSwapBytes
True if target and host have different endian orders.
std::unique_ptr< MemoryBuffer > Buffer
uint64_t CountersSectionStart
The address range of the __llvm_prf_cnts section.
std::optional< std::string > LinkageName
std::optional< std::string > FilePath