LLVM 17.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:
35 get(StringRef DebugInfoFilename);
36
37 /// Construct a ProfileData vector used to correlate raw instrumentation data
38 /// to their functions.
40
41 /// Process debug info and dump the correlation data.
43
44 /// Return the number of ProfileData elements.
45 std::optional<size_t> getDataSize() const;
46
47 /// Return a pointer to the names string that this class constructs.
48 const char *getNamesPointer() const { return Names.c_str(); }
49
50 /// Return the number of bytes in the names string.
51 size_t getNamesSize() const { return Names.size(); }
52
53 /// Return the size of the counters section in bytes.
55 return Ctx->CountersSectionEnd - Ctx->CountersSectionStart;
56 }
57
58 static const char *FunctionNameAttributeName;
59 static const char *CFGHashAttributeName;
60 static const char *NumCountersAttributeName;
61
63 InstrProfCorrelatorKind getKind() const { return Kind; }
64 virtual ~InstrProfCorrelator() = default;
65
66protected:
67 struct Context {
69 get(std::unique_ptr<MemoryBuffer> Buffer, const object::ObjectFile &Obj);
70 std::unique_ptr<MemoryBuffer> Buffer;
71 /// The address range of the __llvm_prf_cnts section.
74 /// True if target and host have different endian orders.
76 };
77 const std::unique_ptr<Context> Ctx;
78
79 InstrProfCorrelator(InstrProfCorrelatorKind K, std::unique_ptr<Context> Ctx)
80 : Ctx(std::move(Ctx)), Kind(K) {}
81
82 std::string Names;
83 std::vector<std::string> NamesVec;
84
85 struct Probe {
86 std::string FunctionName;
87 std::optional<std::string> LinkageName;
88 yaml::Hex64 CFGHash;
89 yaml::Hex64 CounterOffset;
91 std::optional<std::string> FilePath;
92 std::optional<int> LineNumber;
93 };
94
96 std::vector<Probe> Probes;
97 };
98
99 friend struct yaml::MappingTraits<Probe>;
100 friend struct yaml::SequenceElementTraits<Probe>;
102
103private:
105 get(std::unique_ptr<MemoryBuffer> Buffer);
106
107 const InstrProfCorrelatorKind Kind;
108};
109
110/// InstrProfCorrelatorImpl - A child of InstrProfCorrelator with a template
111/// pointer type so that the ProfileData vector can be materialized.
112template <class IntPtrT>
114public:
115 InstrProfCorrelatorImpl(std::unique_ptr<InstrProfCorrelator::Context> Ctx);
116 static bool classof(const InstrProfCorrelator *C);
117
118 /// Return a pointer to the underlying ProfileData vector that this class
119 /// constructs.
121 return Data.empty() ? nullptr : Data.data();
122 }
123
124 /// Return the number of ProfileData elements.
125 size_t getDataSize() const { return Data.size(); }
126
128 get(std::unique_ptr<InstrProfCorrelator::Context> Ctx,
129 const object::ObjectFile &Obj);
130
131protected:
132 std::vector<RawInstrProf::ProfileData<IntPtrT>> Data;
133
134 Error correlateProfileData() override;
137
138 Error dumpYaml(raw_ostream &OS) override;
139
140 void addProbe(StringRef FunctionName, uint64_t CFGHash, IntPtrT CounterOffset,
141 IntPtrT FunctionPtr, uint32_t NumCounters);
142
143private:
145 std::unique_ptr<InstrProfCorrelator::Context> Ctx)
146 : InstrProfCorrelator(Kind, std::move(Ctx)){};
147 llvm::DenseSet<IntPtrT> CounterOffsets;
148
149 // Byte-swap the value if necessary.
150 template <class T> T maybeSwap(T Value) const {
151 return Ctx->ShouldSwapBytes ? sys::getSwappedBytes(Value) : Value;
152 }
153};
154
155/// DwarfInstrProfCorrelator - A child of InstrProfCorrelatorImpl that takes
156/// DWARF debug info as input to correlate profiles.
157template <class IntPtrT>
159public:
160 DwarfInstrProfCorrelator(std::unique_ptr<DWARFContext> DICtx,
161 std::unique_ptr<InstrProfCorrelator::Context> Ctx)
163 DICtx(std::move(DICtx)) {}
164
165private:
166 std::unique_ptr<DWARFContext> DICtx;
167
168 /// Return the address of the object that the provided DIE symbolizes.
169 std::optional<uint64_t> getLocation(const DWARFDie &Die) const;
170
171 /// Returns true if the provided DIE symbolizes an instrumentation probe
172 /// symbol.
173 static bool isDIEOfProbe(const DWARFDie &Die);
174
175 /// Iterate over DWARF DIEs to find those that symbolize instrumentation
176 /// probes and construct the ProfileData vector and Names string.
177 ///
178 /// Here is some example DWARF for an instrumentation probe we are looking
179 /// for:
180 /// \code
181 /// DW_TAG_subprogram
182 /// DW_AT_low_pc (0x0000000000000000)
183 /// DW_AT_high_pc (0x0000000000000014)
184 /// DW_AT_name ("foo")
185 /// DW_TAG_variable
186 /// DW_AT_name ("__profc_foo")
187 /// DW_AT_location (DW_OP_addr 0x0)
188 /// DW_TAG_LLVM_annotation
189 /// DW_AT_name ("Function Name")
190 /// DW_AT_const_value ("foo")
191 /// DW_TAG_LLVM_annotation
192 /// DW_AT_name ("CFG Hash")
193 /// DW_AT_const_value (12345678)
194 /// DW_TAG_LLVM_annotation
195 /// DW_AT_name ("Num Counters")
196 /// DW_AT_const_value (2)
197 /// NULL
198 /// NULL
199 /// \endcode
200 void correlateProfileDataImpl(
201 InstrProfCorrelator::CorrelationData *Data = nullptr) override;
202};
203
204} // end namespace llvm
205
206#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:156
Tagged union holding either a T or a Error.
Definition: Error.h:470
InstrProfCorrelatorImpl - A child of InstrProfCorrelator with a template pointer type so that the Pro...
virtual void correlateProfileDataImpl(InstrProfCorrelator::CorrelationData *Data=nullptr)=0
Error correlateProfileData() override
Construct a ProfileData vector used to correlate raw instrumentation data to their functions.
std::vector< RawInstrProf::ProfileData< IntPtrT > > Data
Error dumpYaml(raw_ostream &OS) override
Process debug info and dump the correlation data.
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)
static llvm::Expected< std::unique_ptr< InstrProfCorrelatorImpl< IntPtrT > > > get(std::unique_ptr< InstrProfCorrelator::Context > Ctx, const object::ObjectFile &Obj)
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()=0
Construct a ProfileData vector used to correlate raw instrumentation data to their functions.
static const char * FunctionNameAttributeName
static const char * CFGHashAttributeName
InstrProfCorrelator(InstrProfCorrelatorKind K, std::unique_ptr< Context > Ctx)
static llvm::Expected< std::unique_ptr< InstrProfCorrelator > > get(StringRef DebugInfoFilename)
uint64_t getCountersSectionSize() const
Return the size of the counters section in bytes.
std::vector< std::string > NamesVec
static const char * NumCountersAttributeName
const char * getNamesPointer() const
Return a pointer to the names string that this class constructs.
virtual Error dumpYaml(raw_ostream &OS)=0
Process debug info and dump the correlation data.
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:228
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
unsigned char getSwappedBytes(unsigned char C)
Definition: SwapByteOrder.h:72
This is an optimization pass for GlobalISel generic memory operations.
Definition: AddressRanges.h:18
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:1946
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