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"
19#include "llvm/Object/BuildID.h"
22#include "llvm/Support/Error.h"
25#include <optional>
26#include <vector>
27
28namespace llvm {
29class DWARFDie;
30namespace object {
31class ObjectFile;
32}
33
34/// InstrProfCorrelator - A base class used to create raw instrumentation data
35/// to their functions.
37public:
38 /// Indicate if we should use the debug info or profile metadata sections to
39 /// correlate.
41
43 get(StringRef Filename, ProfCorrelatorKind FileKind,
44 const object::BuildIDFetcher *BIDFetcher = nullptr,
45 const ArrayRef<llvm::object::BuildID> BIs = {});
46
47 /// Construct a ProfileData vector used to correlate raw instrumentation data
48 /// to their functions.
49 /// \param MaxWarnings the maximum number of warnings to emit (0 = no limit)
50 virtual Error correlateProfileData(int MaxWarnings) = 0;
51
52 /// Process debug info and dump the correlation data.
53 /// \param MaxWarnings the maximum number of warnings to emit (0 = no limit)
54 virtual Error dumpYaml(int MaxWarnings, raw_ostream &OS) = 0;
55
56 /// Return the number of ProfileData elements.
57 LLVM_ABI std::optional<size_t> getDataSize() const;
58
59 /// Return a pointer to the names string that this class constructs.
60 const char *getNamesPointer() const { return Names.c_str(); }
61
62 /// Return the number of bytes in the names string.
63 size_t getNamesSize() const { return Names.size(); }
64
65 /// Return the size of the counters section in bytes.
67 return Ctx->CountersSectionEnd - Ctx->CountersSectionStart;
68 }
69
71 LLVM_ABI static const char *CFGHashAttributeName;
73
75 InstrProfCorrelatorKind getKind() const { return Kind; }
76 virtual ~InstrProfCorrelator() = default;
77
78protected:
79 struct Context {
81 get(std::unique_ptr<MemoryBuffer> Buffer, object::ObjectFile &Obj,
82 ProfCorrelatorKind FileKind);
83 std::unique_ptr<MemoryBuffer> Buffer;
84 /// The address range of the __llvm_prf_cnts section.
87 /// The pointer points to start/end of profile data/name sections if
88 /// FileKind is Binary.
89 const char *DataStart;
90 const char *DataEnd;
91 const char *NameStart;
92 size_t NameSize;
93 /// Resolved values for Mach-O linker fixup chains when FileKind is Binary.
94 /// The mapping is from an address relative to the start of __llvm_covdata,
95 /// to the resolved pointer value at that address.
97 /// True if target and host have different endian orders.
99 };
100 const std::unique_ptr<Context> Ctx;
101
103 : Ctx(std::move(Ctx)), Kind(K) {}
104
105 std::string Names;
106 std::vector<std::string> NamesVec;
107
108 struct Probe {
109 std::string FunctionName;
110 std::optional<std::string> LinkageName;
111 yaml::Hex64 CFGHash;
112 yaml::Hex64 CounterOffset;
114 std::optional<std::string> FilePath;
115 std::optional<int> LineNumber;
116 };
117
119 std::vector<Probe> Probes;
120 };
121
122 friend struct yaml::MappingTraits<Probe>;
123 friend struct yaml::SequenceElementTraits<Probe>;
125
126private:
128 get(std::unique_ptr<MemoryBuffer> Buffer, ProfCorrelatorKind FileKind);
129
130 const InstrProfCorrelatorKind Kind;
131};
132
133/// InstrProfCorrelatorImpl - A child of InstrProfCorrelator with a template
134/// pointer type so that the ProfileData vector can be materialized.
135template <class IntPtrT>
137public:
138 InstrProfCorrelatorImpl(std::unique_ptr<InstrProfCorrelator::Context> Ctx);
139 static bool classof(const InstrProfCorrelator *C);
140
141 /// Return a pointer to the underlying ProfileData vector that this class
142 /// constructs.
144 return Data.empty() ? nullptr : Data.data();
145 }
146
147 /// Return the number of ProfileData elements.
148 size_t getDataSize() const { return Data.size(); }
149
151 get(std::unique_ptr<InstrProfCorrelator::Context> Ctx,
152 const object::ObjectFile &Obj, ProfCorrelatorKind FileKind);
153
154protected:
155 std::vector<RawInstrProf::ProfileData<IntPtrT>> Data;
156
157 Error correlateProfileData(int MaxWarnings) override;
159 int MaxWarnings,
161
163
164 Error dumpYaml(int MaxWarnings, raw_ostream &OS) override;
165
166 void addDataProbe(uint64_t FunctionName, uint64_t CFGHash,
167 IntPtrT CounterOffset, IntPtrT FunctionPtr,
169
170 // Byte-swap the value if necessary.
171 template <class T> T maybeSwap(T Value) const {
172 return Ctx->ShouldSwapBytes ? llvm::byteswap(Value) : Value;
173 }
174
175private:
177 std::unique_ptr<InstrProfCorrelator::Context> Ctx)
178 : InstrProfCorrelator(Kind, std::move(Ctx)){};
179 llvm::DenseSet<IntPtrT> CounterOffsets;
180};
181
182/// DwarfInstrProfCorrelator - A child of InstrProfCorrelatorImpl that takes
183/// DWARF debug info as input to correlate profiles.
184template <class IntPtrT>
186public:
187 DwarfInstrProfCorrelator(std::unique_ptr<DWARFContext> DICtx,
188 std::unique_ptr<InstrProfCorrelator::Context> Ctx)
189 : InstrProfCorrelatorImpl<IntPtrT>(std::move(Ctx)),
190 DICtx(std::move(DICtx)) {}
191
192private:
193 std::unique_ptr<DWARFContext> DICtx;
194
195 /// Return the address of the object that the provided DIE symbolizes.
196 std::optional<uint64_t> getLocation(const DWARFDie &Die) const;
197
198 /// Returns true if the provided DIE symbolizes an instrumentation probe
199 /// symbol.
200 static bool isDIEOfProbe(const DWARFDie &Die);
201
202 /// Iterate over DWARF DIEs to find those that symbolize instrumentation
203 /// probes and construct the ProfileData vector and Names string.
204 ///
205 /// Here is some example DWARF for an instrumentation probe we are looking
206 /// for:
207 /// \code
208 /// DW_TAG_subprogram
209 /// DW_AT_low_pc (0x0000000000000000)
210 /// DW_AT_high_pc (0x0000000000000014)
211 /// DW_AT_name ("foo")
212 /// DW_TAG_variable
213 /// DW_AT_name ("__profc_foo")
214 /// DW_AT_location (DW_OP_addr 0x0)
215 /// DW_TAG_LLVM_annotation
216 /// DW_AT_name ("Function Name")
217 /// DW_AT_const_value ("foo")
218 /// DW_TAG_LLVM_annotation
219 /// DW_AT_name ("CFG Hash")
220 /// DW_AT_const_value (12345678)
221 /// DW_TAG_LLVM_annotation
222 /// DW_AT_name ("Num Counters")
223 /// DW_AT_const_value (2)
224 /// NULL
225 /// NULL
226 /// \endcode
227 /// \param MaxWarnings the maximum number of warnings to emit (0 = no limit)
228 /// \param Data if provided, populate with the correlation data found
229 void correlateProfileDataImpl(
230 int MaxWarnings,
231 InstrProfCorrelator::CorrelationData *Data = nullptr) override;
232
234};
235
236/// BinaryInstrProfCorrelator - A child of InstrProfCorrelatorImpl that
237/// takes an object file as input to correlate profiles.
238template <class IntPtrT>
240public:
241 BinaryInstrProfCorrelator(std::unique_ptr<InstrProfCorrelator::Context> Ctx)
242 : InstrProfCorrelatorImpl<IntPtrT>(std::move(Ctx)) {}
243
244 /// Return a pointer to the names string that this class constructs.
245 const char *getNamesPointer() const { return this->Ctx.NameStart; }
246
247 /// Return the number of bytes in the names string.
248 size_t getNamesSize() const { return this->Ctx.NameSize; }
249
250private:
251 void correlateProfileDataImpl(
252 int MaxWarnings,
253 InstrProfCorrelator::CorrelationData *Data = nullptr) override;
254
256};
257
258} // end namespace llvm
259
260#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:213
This file defines the DenseMap class.
This file defines the DenseSet and SmallDenseSet classes.
#define T
static constexpr StringLiteral Filename
static MemoryLocation getLocation(Instruction *I)
ArrayRef - 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:279
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 FunctionPtr, uint32_t NumCounters)
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={})
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:55
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.
Definition Types.h:26
constexpr T byteswap(T V) noexcept
Reverses the bytes in the given integer value V.
Definition bit.h:102
FunctionAddr NumCounters
Definition InstrProf.h:91
FunctionAddr VTableAddr uintptr_t uintptr_t Data
Definition InstrProf.h:189
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:870
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)
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:62
This class should be specialized by any type for which vectors of that type need to be converted to/f...
Definition YAMLTraits.h:257