LLVM 23.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/coverage profiles
9// from raw profile data and debug info/binary file.
10//===----------------------------------------------------------------------===//
11
12#ifndef LLVM_PROFILEDATA_INSTRPROFCORRELATOR_H
13#define LLVM_PROFILEDATA_INSTRPROFCORRELATOR_H
14
15#include "llvm/ADT/DenseMap.h"
16#include "llvm/ADT/DenseSet.h"
17#include "llvm/ADT/MapVector.h"
20#include "llvm/Object/BuildID.h"
23#include "llvm/Support/Error.h"
26#include <optional>
27#include <vector>
28
29namespace llvm {
30class DWARFDie;
31namespace object {
32class ObjectFile;
33}
34
35/// InstrProfCorrelator - A base class used to create raw instrumentation data
36/// to their functions.
38public:
39 /// Indicate if we should use the debug info or profile metadata sections to
40 /// correlate.
42
44 get(StringRef Filename, ProfCorrelatorKind FileKind,
45 const object::BuildIDFetcher *BIDFetcher = nullptr,
46 const ArrayRef<llvm::object::BuildID> BIs = {});
47
48 /// Construct a ProfileData vector used to correlate raw instrumentation data
49 /// to their functions.
50 /// \param MaxWarnings the maximum number of warnings to emit (0 = no limit)
51 virtual Error correlateProfileData(int MaxWarnings) = 0;
52
53 /// Process debug info and dump the correlation data.
54 /// \param MaxWarnings the maximum number of warnings to emit (0 = no limit)
55 virtual Error dumpYaml(int MaxWarnings, raw_ostream &OS) = 0;
56
57 /// Return the number of ProfileData elements.
58 LLVM_ABI std::optional<size_t> getDataSize() const;
59
60 /// Return a pointer to the names string that this class constructs.
61 const char *getNamesPointer() const { return Names.c_str(); }
62
63 /// Return the number of bytes in the names string.
64 size_t getNamesSize() const { return Names.size(); }
65
66 /// Return the size of the counters section in bytes.
68 return Ctx->CountersSectionEnd - Ctx->CountersSectionStart;
69 }
70
72 LLVM_ABI static const char *CFGHashAttributeName;
75
77 InstrProfCorrelatorKind getKind() const { return Kind; }
78 virtual ~InstrProfCorrelator() = default;
79
80protected:
81 struct Context {
83 get(std::unique_ptr<MemoryBuffer> Buffer, object::ObjectFile &Obj,
84 ProfCorrelatorKind FileKind);
85 std::unique_ptr<MemoryBuffer> Buffer;
86 /// The address range of the __llvm_prf_cnts section.
89 /// The address range of the __llvm_prf_bits section.
92 /// The pointer points to start/end of profile data/name sections if
93 /// FileKind is Binary.
94 const char *DataStart;
95 const char *DataEnd;
96 const char *NameStart;
97 size_t NameSize;
98 /// Resolved values for Mach-O linker fixup chains when FileKind is Binary.
99 /// The mapping is from an address relative to the start of __llvm_covdata,
100 /// to the resolved pointer value at that address.
102 /// True if target and host have different endian orders.
104 };
105 const std::unique_ptr<Context> Ctx;
106
108 : Ctx(std::move(Ctx)), Kind(K) {}
109
110 std::string Names;
111 std::vector<std::string> NamesVec;
112
113 struct Probe {
114 std::string FunctionName;
115 std::optional<std::string> LinkageName;
116 yaml::Hex64 CFGHash;
117 yaml::Hex64 CounterOffset;
118 yaml::Hex64 BitmapOffset;
121 std::optional<std::string> FilePath;
122 std::optional<int> LineNumber;
123 };
124
126 std::vector<Probe> Probes;
127 };
128
129 friend struct yaml::MappingTraits<Probe>;
130 friend struct yaml::SequenceElementTraits<Probe>;
132
133private:
135 get(std::unique_ptr<MemoryBuffer> Buffer, ProfCorrelatorKind FileKind);
136
137 const InstrProfCorrelatorKind Kind;
138};
139
140/// InstrProfCorrelatorImpl - A child of InstrProfCorrelator with a template
141/// pointer type so that the ProfileData vector can be materialized.
142template <class IntPtrT>
144public:
145 InstrProfCorrelatorImpl(std::unique_ptr<InstrProfCorrelator::Context> Ctx);
146 static bool classof(const InstrProfCorrelator *C);
147
148 /// Return a pointer to the underlying ProfileData vector that this class
149 /// constructs.
151 return Data.empty() ? nullptr : Data.data();
152 }
153
154 /// Return the number of ProfileData elements.
155 size_t getDataSize() const { return Data.size(); }
156
158 get(std::unique_ptr<InstrProfCorrelator::Context> Ctx,
159 const object::ObjectFile &Obj, ProfCorrelatorKind FileKind);
160
161protected:
162 std::vector<RawInstrProf::ProfileData<IntPtrT>> Data;
163
164 Error correlateProfileData(int MaxWarnings) override;
166 int MaxWarnings,
168
170
171 Error dumpYaml(int MaxWarnings, raw_ostream &OS) override;
172
173 void addDataProbe(uint64_t FunctionName, uint64_t CFGHash,
174 IntPtrT CounterOffset, IntPtrT BitmapOffset,
175 IntPtrT FunctionPtr, uint32_t NumCounters,
177
178 // Byte-swap the value if necessary.
179 template <class T> T maybeSwap(T Value) const {
180 return Ctx->ShouldSwapBytes ? llvm::byteswap(Value) : Value;
181 }
182
183private:
185 std::unique_ptr<InstrProfCorrelator::Context> Ctx)
186 : InstrProfCorrelator(Kind, std::move(Ctx)){};
187 llvm::DenseSet<IntPtrT> CounterOffsets;
188 llvm::DenseSet<IntPtrT> BitmapOffsets;
189};
190
191/// DwarfInstrProfCorrelator - A child of InstrProfCorrelatorImpl that takes
192/// DWARF debug info as input to correlate profiles.
193template <class IntPtrT>
195public:
196 DwarfInstrProfCorrelator(std::unique_ptr<DWARFContext> DICtx,
197 std::unique_ptr<InstrProfCorrelator::Context> Ctx)
198 : InstrProfCorrelatorImpl<IntPtrT>(std::move(Ctx)),
199 DICtx(std::move(DICtx)) {}
200
201private:
202 std::unique_ptr<DWARFContext> DICtx;
203
204 /// Return the address of the object that the provided DIE symbolizes.
205 std::optional<uint64_t> getLocation(const DWARFDie &Die) const;
206
207 /// Returns true if the provided DIE symbolizes an instrumentation probe
208 /// symbol of the necessary type.
209 static bool isDIEOfProbe(const DWARFDie &Die, StringRef Prefix);
210
211 std::optional<std::pair<InstrProfCorrelator::Probe, IntPtrT>>
212 addCountersToDataProbe(const DWARFDie &Die, const bool UnlimitedWarnings,
213 int &NumSuppressedWarnings);
214
215 std::optional<std::pair<InstrProfCorrelator::Probe, IntPtrT>>
216 addBitmapToDataProbe(const DWARFDie &Die, const bool UnlimitedWarnings,
217 int &NumSuppressedWarnings);
218
219 /// Iterate over DWARF DIEs to find those that symbolize instrumentation
220 /// probes and construct the ProfileData vector and Names string.
221 ///
222 /// Here is some example DWARF for an instrumentation probe we are looking
223 /// for:
224 /// \code
225 /// DW_TAG_subprogram
226 /// DW_AT_low_pc (0x0000000000000000)
227 /// DW_AT_high_pc (0x0000000000000014)
228 /// DW_AT_name ("foo")
229 /// DW_TAG_variable
230 /// DW_AT_name ("__profc_foo")
231 /// DW_AT_location (DW_OP_addr 0x0)
232 /// DW_TAG_LLVM_annotation
233 /// DW_AT_name ("Function Name")
234 /// DW_AT_const_value ("foo")
235 /// DW_TAG_LLVM_annotation
236 /// DW_AT_name ("CFG Hash")
237 /// DW_AT_const_value (12345678)
238 /// DW_TAG_LLVM_annotation
239 /// DW_AT_name ("Num Counters")
240 /// DW_AT_const_value (2)
241 /// NULL
242 /// NULL
243 /// \endcode
244 /// \param MaxWarnings the maximum number of warnings to emit (0 = no limit)
245 /// \param Data if provided, populate with the correlation data found
246 void correlateProfileDataImpl(
247 int MaxWarnings,
248 InstrProfCorrelator::CorrelationData *Data = nullptr) override;
249
251};
252
253/// BinaryInstrProfCorrelator - A child of InstrProfCorrelatorImpl that
254/// takes an object file as input to correlate profiles.
255template <class IntPtrT>
257public:
258 BinaryInstrProfCorrelator(std::unique_ptr<InstrProfCorrelator::Context> Ctx)
259 : InstrProfCorrelatorImpl<IntPtrT>(std::move(Ctx)) {}
260
261 /// Return a pointer to the names string that this class constructs.
262 const char *getNamesPointer() const { return this->Ctx.NameStart; }
263
264 /// Return the number of bytes in the names string.
265 size_t getNamesSize() const { return this->Ctx.NameSize; }
266
267private:
268 void correlateProfileDataImpl(
269 int MaxWarnings,
270 InstrProfCorrelator::CorrelationData *Data = nullptr) override;
271
273};
274
275} // end namespace llvm
276
277#endif // LLVM_PROFILEDATA_INSTRPROFCORRELATOR_H
This file declares a Build ID fetcher implementation for obtaining debug info from debuginfod.
This file declares a library for handling Build IDs and using them to find debug info.
#define LLVM_ABI
Definition Compiler.h:215
This file defines the DenseMap class.
This file defines the DenseSet and SmallDenseSet classes.
This file implements a map that provides insertion order iteration.
#define T
static constexpr StringLiteral Filename
static MemoryLocation getLocation(Instruction *I)
Represent a constant reference to an array (0 or more elements consecutively in memory),...
Definition ArrayRef.h:40
size_t getNamesSize() const
Return the number of bytes in the names string.
const char * getNamesPointer() const
Return a pointer to the names string that this class constructs.
BinaryInstrProfCorrelator(std::unique_ptr< InstrProfCorrelator::Context > Ctx)
Utility class that carries the DWARF compile/type unit and the debug info entry in an object.
Definition DWARFDie.h:43
Implements a dense probed hash-table based set.
Definition DenseSet.h:289
DwarfInstrProfCorrelator(std::unique_ptr< DWARFContext > DICtx, std::unique_ptr< InstrProfCorrelator::Context > Ctx)
Lightweight error class with error context and mandatory checking.
Definition Error.h:159
Tagged union holding either a T or a Error.
Definition Error.h:485
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)
void addDataProbe(uint64_t FunctionName, uint64_t CFGHash, IntPtrT CounterOffset, IntPtrT BitmapOffset, IntPtrT FunctionPtr, uint32_t NumCounters, uint32_t NumBitmapBytes)
const RawInstrProf::ProfileData< IntPtrT > * getDataPointer() const
Return a pointer to the underlying ProfileData vector that this class constructs.
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 LLVM_ABI const char * FunctionNameAttributeName
static LLVM_ABI const char * CFGHashAttributeName
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 LLVM_ABI 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 if we should use the debug info or profile metadata sections to correlate.
virtual ~InstrProfCorrelator()=default
InstrProfCorrelatorKind getKind() const
const std::unique_ptr< Context > Ctx
LLVM_ABI std::optional< size_t > getDataSize() const
Return the number of ProfileData elements.
static LLVM_ABI llvm::Expected< std::unique_ptr< InstrProfCorrelator > > get(StringRef Filename, ProfCorrelatorKind FileKind, const object::BuildIDFetcher *BIDFetcher=nullptr, const ArrayRef< llvm::object::BuildID > BIs={})
static LLVM_ABI const char * NumBitmapBitsAttributeName
size_t getNamesSize() const
Return the number of bytes in the names string.
Represent a constant reference to a string, i.e.
Definition StringRef.h:56
LLVM Value Representation.
Definition Value.h:75
BuildIDFetcher searches local cache directories for debug info.
Definition BuildID.h:40
This class is the base class for all object file types.
Definition ObjectFile.h:231
This class implements an extremely fast bulk output stream that can only output to a stream.
Definition raw_ostream.h:53
@ C
The default llvm calling convention, compatible with C.
Definition CallingConv.h:34
This is an optimization pass for GlobalISel generic memory operations.
RelativeUniformCounterPtr ValuesPtrExpr NumBitmapBytes
Definition InstrProf.h:101
constexpr T byteswap(T V) noexcept
Reverses the bytes in the given integer value V.
Definition bit.h:102
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:1917
Implement std::hash so that hash_code can be used in STL containers.
Definition BitVector.h:860
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.
llvm::DenseMap< uint64_t, uint64_t > MachOFixups
Resolved values for Mach-O linker fixup chains when FileKind is Binary.
static LLVM_ABI llvm::Expected< std::unique_ptr< Context > > get(std::unique_ptr< MemoryBuffer > Buffer, object::ObjectFile &Obj, ProfCorrelatorKind FileKind)
uint64_t BitmapSectionStart
The address range of the __llvm_prf_bits section.
const char * DataStart
The pointer points to start/end of profile data/name sections if FileKind is Binary.
std::optional< std::string > LinkageName
std::optional< std::string > FilePath
This class should be specialized by any type that needs to be converted to/from a YAML mapping.
Definition YAMLTraits.h:63
This class should be specialized by any type for which vectors of that type need to be converted to/f...
Definition YAMLTraits.h:258