LLVM 24.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"
24#include "llvm/Support/Error.h"
27#include <optional>
28#include <vector>
29
30namespace llvm {
31class DWARFDie;
32namespace object {
33class ObjectFile;
34}
35
36/// InstrProfCorrelator - A base class used to create raw instrumentation data
37/// to their functions.
39public:
42 const object::BuildIDFetcher *BIDFetcher = nullptr,
43 const ArrayRef<llvm::object::BuildID> BIs = {});
44
45 /// Construct a ProfileData vector used to correlate raw instrumentation data
46 /// to their functions.
47 /// \param MaxWarnings the maximum number of warnings to emit (0 = no limit)
48 virtual Error correlateProfileData(int MaxWarnings) = 0;
49
50 /// Process debug info and dump the correlation data.
51 /// \param MaxWarnings the maximum number of warnings to emit (0 = no limit)
52 virtual Error dumpYaml(int MaxWarnings, raw_ostream &OS) = 0;
53
54 /// Return the number of ProfileData elements.
55 LLVM_ABI std::optional<size_t> getDataSize() const;
56
57 /// Return a pointer to the names string that this class constructs.
58 const char *getNamesPointer() const { return Names.c_str(); }
59
60 /// Return the number of bytes in the names string.
61 size_t getNamesSize() const { return Names.size(); }
62
63 /// Return the size of the counters section in bytes.
65 return Ctx->CountersSectionEnd - Ctx->CountersSectionStart;
66 }
67
69 LLVM_ABI static const char *CFGHashAttributeName;
72
74 InstrProfCorrelatorKind getKind() const { return Kind; }
75 virtual ~InstrProfCorrelator() = default;
76
77protected:
78 struct Context {
80 get(std::unique_ptr<MemoryBuffer> Buffer, object::ObjectFile &Obj,
81 ProfCorrelatorKind FileKind);
82 std::unique_ptr<MemoryBuffer> Buffer;
83 /// The address range of the __llvm_prf_cnts section.
86 /// The address range of the __llvm_prf_bits section.
89 /// The pointer points to start/end of profile data/name sections if
90 /// FileKind is Binary.
91 const char *DataStart;
92 const char *DataEnd;
93 const char *NameStart;
94 size_t NameSize;
95 /// Resolved values for Mach-O linker fixup chains when FileKind is Binary.
96 /// The mapping is from an address relative to the start of __llvm_covdata,
97 /// to the resolved pointer value at that address.
99 /// True if target and host have different endian orders.
101 };
102 const std::unique_ptr<Context> Ctx;
103
105 : Ctx(std::move(Ctx)), Kind(K) {}
106
107 std::string Names;
108 std::vector<std::string> NamesVec;
109
110 struct Probe {
111 std::string FunctionName;
112 std::optional<std::string> LinkageName;
113 yaml::Hex64 CFGHash;
114 yaml::Hex64 CounterOffset;
115 yaml::Hex64 BitmapOffset;
118 std::optional<std::string> FilePath;
119 std::optional<int> LineNumber;
120 };
121
123 std::vector<Probe> Probes;
124 };
125
126 friend struct yaml::MappingTraits<Probe>;
127 friend struct yaml::SequenceElementTraits<Probe>;
129
130private:
132 get(std::unique_ptr<MemoryBuffer> Buffer, ProfCorrelatorKind FileKind);
133
134 const InstrProfCorrelatorKind Kind;
135};
136
137/// InstrProfCorrelatorImpl - A child of InstrProfCorrelator with a template
138/// pointer type so that the ProfileData vector can be materialized.
139template <class IntPtrT>
141public:
142 InstrProfCorrelatorImpl(std::unique_ptr<InstrProfCorrelator::Context> Ctx);
143 static bool classof(const InstrProfCorrelator *C);
144
145 /// Return a pointer to the underlying ProfileData vector that this class
146 /// constructs.
148 return Data.empty() ? nullptr : Data.data();
149 }
150
151 /// Return the number of ProfileData elements.
152 size_t getDataSize() const { return Data.size(); }
153
155 get(std::unique_ptr<InstrProfCorrelator::Context> Ctx,
156 const object::ObjectFile &Obj, ProfCorrelatorKind FileKind);
157
158protected:
159 std::vector<RawInstrProf::ProfileData<IntPtrT>> Data;
160
161 Error correlateProfileData(int MaxWarnings) override;
163 int MaxWarnings,
165
167
168 Error dumpYaml(int MaxWarnings, raw_ostream &OS) override;
169
170 void addDataProbe(uint64_t FunctionName, uint64_t CFGHash,
171 IntPtrT CounterOffset, IntPtrT BitmapOffset,
172 IntPtrT FunctionPtr, uint32_t NumCounters,
174
175 // Byte-swap the value if necessary.
176 template <class T> T maybeSwap(T Value) const {
177 return Ctx->ShouldSwapBytes ? llvm::byteswap(Value) : Value;
178 }
179
180private:
182 std::unique_ptr<InstrProfCorrelator::Context> Ctx)
183 : InstrProfCorrelator(Kind, std::move(Ctx)){};
184 llvm::DenseSet<IntPtrT> CounterOffsets;
185 llvm::DenseSet<IntPtrT> BitmapOffsets;
186};
187
188/// DwarfInstrProfCorrelator - A child of InstrProfCorrelatorImpl that takes
189/// DWARF debug info as input to correlate profiles.
190template <class IntPtrT>
192public:
193 DwarfInstrProfCorrelator(std::unique_ptr<DWARFContext> DICtx,
194 std::unique_ptr<InstrProfCorrelator::Context> Ctx)
195 : InstrProfCorrelatorImpl<IntPtrT>(std::move(Ctx)),
196 DICtx(std::move(DICtx)) {}
197
198private:
199 std::unique_ptr<DWARFContext> DICtx;
200
201 /// Return the address of the object that the provided DIE symbolizes.
202 std::optional<uint64_t> getLocation(const DWARFDie &Die) const;
203
204 /// Returns true if the provided DIE symbolizes an instrumentation probe
205 /// symbol of the necessary type.
206 static bool isDIEOfProbe(const DWARFDie &Die, StringRef Prefix);
207
208 std::optional<std::pair<InstrProfCorrelator::Probe, IntPtrT>>
209 addCountersToDataProbe(const DWARFDie &Die, const bool UnlimitedWarnings,
210 int &NumSuppressedWarnings);
211
212 std::optional<std::pair<InstrProfCorrelator::Probe, IntPtrT>>
213 addBitmapToDataProbe(const DWARFDie &Die, const bool UnlimitedWarnings,
214 int &NumSuppressedWarnings);
215
216 /// Iterate over DWARF DIEs to find those that symbolize instrumentation
217 /// probes and construct the ProfileData vector and Names string.
218 ///
219 /// Here is some example DWARF for an instrumentation probe we are looking
220 /// for:
221 /// \code
222 /// DW_TAG_subprogram
223 /// DW_AT_low_pc (0x0000000000000000)
224 /// DW_AT_high_pc (0x0000000000000014)
225 /// DW_AT_name ("foo")
226 /// DW_TAG_variable
227 /// DW_AT_name ("__profc_foo")
228 /// DW_AT_location (DW_OP_addr 0x0)
229 /// DW_TAG_LLVM_annotation
230 /// DW_AT_name ("Function Name")
231 /// DW_AT_const_value ("foo")
232 /// DW_TAG_LLVM_annotation
233 /// DW_AT_name ("CFG Hash")
234 /// DW_AT_const_value (12345678)
235 /// DW_TAG_LLVM_annotation
236 /// DW_AT_name ("Num Counters")
237 /// DW_AT_const_value (2)
238 /// NULL
239 /// NULL
240 /// \endcode
241 /// \param MaxWarnings the maximum number of warnings to emit (0 = no limit)
242 /// \param Data if provided, populate with the correlation data found
243 void correlateProfileDataImpl(
244 int MaxWarnings,
245 InstrProfCorrelator::CorrelationData *Data = nullptr) override;
246
248};
249
250/// BinaryInstrProfCorrelator - A child of InstrProfCorrelatorImpl that
251/// takes an object file as input to correlate profiles.
252template <class IntPtrT>
254public:
255 BinaryInstrProfCorrelator(std::unique_ptr<InstrProfCorrelator::Context> Ctx)
256 : InstrProfCorrelatorImpl<IntPtrT>(std::move(Ctx)) {}
257
258 /// Return a pointer to the names string that this class constructs.
259 const char *getNamesPointer() const { return this->Ctx.NameStart; }
260
261 /// Return the number of bytes in the names string.
262 size_t getNamesSize() const { return this->Ctx.NameSize; }
263
264private:
265 void correlateProfileDataImpl(
266 int MaxWarnings,
267 InstrProfCorrelator::CorrelationData *Data = nullptr) override;
268
270};
271
272} // end namespace llvm
273
274#endif // LLVM_PROFILEDATA_INSTRPROFCORRELATOR_H
unsigned uint64_t
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.
static GCRegistry::Add< ShadowStackGC > C("shadow-stack", "Very portable GC for uncooperative code generators")
#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
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:281
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.
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:41
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
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
ProfCorrelatorKind
Whether the debug info or the profile metadata sections correlate raw profile data to functions.
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:1933
Implement std::hash so that hash_code can be used in STL containers.
Definition BitVector.h:878
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