LLVM  16.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 <vector>
21 
22 namespace llvm {
23 class DWARFContext;
24 class DWARFDie;
25 namespace object {
26 class ObjectFile;
27 }
28 
29 /// InstrProfCorrelator - A base class used to create raw instrumentation data
30 /// to their functions.
32 public:
34  get(StringRef DebugInfoFilename);
35 
36  /// Construct a ProfileData vector used to correlate raw instrumentation data
37  /// to their functions.
38  virtual Error correlateProfileData() = 0;
39 
40  /// Process debug info and dump the correlation data.
41  virtual Error dumpYaml(raw_ostream &OS) = 0;
42 
43  /// Return the number of ProfileData elements.
45 
46  /// Return a pointer to the names string that this class constructs.
47  const char *getNamesPointer() const { return Names.c_str(); }
48 
49  /// Return the number of bytes in the names string.
50  size_t getNamesSize() const { return Names.size(); }
51 
52  /// Return the size of the counters section in bytes.
54  return Ctx->CountersSectionEnd - Ctx->CountersSectionStart;
55  }
56 
57  static const char *FunctionNameAttributeName;
58  static const char *CFGHashAttributeName;
59  static const char *NumCountersAttributeName;
60 
62  InstrProfCorrelatorKind getKind() const { return Kind; }
63  virtual ~InstrProfCorrelator() = default;
64 
65 protected:
66  struct Context {
68  get(std::unique_ptr<MemoryBuffer> Buffer, const object::ObjectFile &Obj);
69  std::unique_ptr<MemoryBuffer> Buffer;
70  /// The address range of the __llvm_prf_cnts section.
73  /// True if target and host have different endian orders.
75  };
76  const std::unique_ptr<Context> Ctx;
77 
78  InstrProfCorrelator(InstrProfCorrelatorKind K, std::unique_ptr<Context> Ctx)
79  : Ctx(std::move(Ctx)), Kind(K) {}
80 
81  std::string Names;
82  std::vector<std::string> NamesVec;
83 
84  struct Probe {
85  std::string FunctionName;
87  yaml::Hex64 CFGHash;
88  yaml::Hex64 CounterOffset;
92  };
93 
94  struct CorrelationData {
95  std::vector<Probe> Probes;
96  };
97 
98  friend struct yaml::MappingTraits<Probe>;
99  friend struct yaml::SequenceElementTraits<Probe>;
101 
102 private:
104  get(std::unique_ptr<MemoryBuffer> Buffer);
105 
107 };
108 
109 /// InstrProfCorrelatorImpl - A child of InstrProfCorrelator with a template
110 /// pointer type so that the ProfileData vector can be materialized.
111 template <class IntPtrT>
113 public:
114  InstrProfCorrelatorImpl(std::unique_ptr<InstrProfCorrelator::Context> Ctx);
115  static bool classof(const InstrProfCorrelator *C);
116 
117  /// Return a pointer to the underlying ProfileData vector that this class
118  /// constructs.
120  return Data.empty() ? nullptr : Data.data();
121  }
122 
123  /// Return the number of ProfileData elements.
124  size_t getDataSize() const { return Data.size(); }
125 
127  get(std::unique_ptr<InstrProfCorrelator::Context> Ctx,
128  const object::ObjectFile &Obj);
129 
130 protected:
131  std::vector<RawInstrProf::ProfileData<IntPtrT>> Data;
132 
133  Error correlateProfileData() override;
134  virtual void correlateProfileDataImpl(
136 
137  Error dumpYaml(raw_ostream &OS) override;
138 
139  void addProbe(StringRef FunctionName, uint64_t CFGHash, IntPtrT CounterOffset,
140  IntPtrT FunctionPtr, uint32_t NumCounters);
141 
142 private:
144  std::unique_ptr<InstrProfCorrelator::Context> Ctx)
145  : InstrProfCorrelator(Kind, std::move(Ctx)){};
146  llvm::DenseSet<IntPtrT> CounterOffsets;
147 
148  // Byte-swap the value if necessary.
149  template <class T> T maybeSwap(T Value) const {
150  return Ctx->ShouldSwapBytes ? sys::getSwappedBytes(Value) : Value;
151  }
152 };
153 
154 /// DwarfInstrProfCorrelator - A child of InstrProfCorrelatorImpl that takes
155 /// DWARF debug info as input to correlate profiles.
156 template <class IntPtrT>
158 public:
159  DwarfInstrProfCorrelator(std::unique_ptr<DWARFContext> DICtx,
160  std::unique_ptr<InstrProfCorrelator::Context> Ctx)
161  : InstrProfCorrelatorImpl<IntPtrT>(std::move(Ctx)),
162  DICtx(std::move(DICtx)) {}
163 
164 private:
165  std::unique_ptr<DWARFContext> DICtx;
166 
167  /// Return the address of the object that the provided DIE symbolizes.
168  llvm::Optional<uint64_t> getLocation(const DWARFDie &Die) const;
169 
170  /// Returns true if the provided DIE symbolizes an instrumentation probe
171  /// symbol.
172  static bool isDIEOfProbe(const DWARFDie &Die);
173 
174  /// Iterate over DWARF DIEs to find those that symbolize instrumentation
175  /// probes and construct the ProfileData vector and Names string.
176  ///
177  /// Here is some example DWARF for an instrumentation probe we are looking
178  /// for:
179  /// \code
180  /// DW_TAG_subprogram
181  /// DW_AT_low_pc (0x0000000000000000)
182  /// DW_AT_high_pc (0x0000000000000014)
183  /// DW_AT_name ("foo")
184  /// DW_TAG_variable
185  /// DW_AT_name ("__profc_foo")
186  /// DW_AT_location (DW_OP_addr 0x0)
187  /// DW_TAG_LLVM_annotation
188  /// DW_AT_name ("Function Name")
189  /// DW_AT_const_value ("foo")
190  /// DW_TAG_LLVM_annotation
191  /// DW_AT_name ("CFG Hash")
192  /// DW_AT_const_value (12345678)
193  /// DW_TAG_LLVM_annotation
194  /// DW_AT_name ("Num Counters")
195  /// DW_AT_const_value (2)
196  /// NULL
197  /// NULL
198  /// \endcode
199  void correlateProfileDataImpl(
200  InstrProfCorrelator::CorrelationData *Data = nullptr) override;
201 };
202 
203 } // end namespace llvm
204 
205 #endif // LLVM_PROFILEDATA_INSTRPROFCORRELATOR_H
MemoryBuffer.h
llvm::InstrProfCorrelator::CorrelationData::Probes
std::vector< Probe > Probes
Definition: InstrProfCorrelator.h:95
llvm
This is an optimization pass for GlobalISel generic memory operations.
Definition: AddressRanges.h:18
llvm::InstrProfCorrelator::dumpYaml
virtual Error dumpYaml(raw_ostream &OS)=0
Process debug info and dump the correlation data.
llvm::InstrProfCorrelator::NumCountersAttributeName
static const char * NumCountersAttributeName
Definition: InstrProfCorrelator.h:59
llvm::InstrProfCorrelator::Probe
Definition: InstrProfCorrelator.h:84
llvm::InstrProfCorrelator::getDataSize
llvm::Optional< size_t > getDataSize() const
Return the number of ProfileData elements.
Definition: InstrProfCorrelator.cpp:96
llvm::InstrProfCorrelator::getCountersSectionSize
uint64_t getCountersSectionSize() const
Return the size of the counters section in bytes.
Definition: InstrProfCorrelator.h:53
llvm::DwarfInstrProfCorrelator::DwarfInstrProfCorrelator
DwarfInstrProfCorrelator(std::unique_ptr< DWARFContext > DICtx, std::unique_ptr< InstrProfCorrelator::Context > Ctx)
Definition: InstrProfCorrelator.h:159
Error.h
llvm::InstrProfCorrelatorImpl::correlateProfileData
Error correlateProfileData() override
Construct a ProfileData vector used to correlate raw instrumentation data to their functions.
Definition: InstrProfCorrelator.cpp:144
llvm::InstrProfCorrelator::CorrelationData
Definition: InstrProfCorrelator.h:94
llvm::InstrProfCorrelatorImpl::getDataSize
size_t getDataSize() const
Return the number of ProfileData elements.
Definition: InstrProfCorrelator.h:124
llvm::InstrProfCorrelator::CFGHashAttributeName
static const char * CFGHashAttributeName
Definition: InstrProfCorrelator.h:58
llvm::Optional< size_t >
T
#define T
Definition: Mips16ISelLowering.cpp:341
llvm::InstrProfCorrelator::Probe::LinkageName
Optional< std::string > LinkageName
Definition: InstrProfCorrelator.h:86
llvm::Expected
Tagged union holding either a T or a Error.
Definition: APFloat.h:41
llvm::yaml::MappingTraits
Definition: ModuleSummaryIndex.h:52
llvm::InstrProfCorrelatorImpl
InstrProfCorrelatorImpl - A child of InstrProfCorrelator with a template pointer type so that the Pro...
Definition: InstrProfCorrelator.h:112
llvm::InstrProfCorrelator::Names
std::string Names
Definition: InstrProfCorrelator.h:81
llvm::DwarfInstrProfCorrelator
DwarfInstrProfCorrelator - A child of InstrProfCorrelatorImpl that takes DWARF debug info as input to...
Definition: InstrProfCorrelator.h:157
llvm::InstrProfCorrelator::getKind
InstrProfCorrelatorKind getKind() const
Definition: InstrProfCorrelator.h:62
C
(vector float) vec_cmpeq(*A, *B) C
Definition: README_ALTIVEC.txt:86
llvm::InstrProfCorrelator::CK_32Bit
@ CK_32Bit
Definition: InstrProfCorrelator.h:61
llvm::InstrProfCorrelator::FunctionNameAttributeName
static const char * FunctionNameAttributeName
Definition: InstrProfCorrelator.h:57
llvm::InstrProfCorrelator::getNamesSize
size_t getNamesSize() const
Return the number of bytes in the names string.
Definition: InstrProfCorrelator.h:50
DenseSet.h
InstrProf.h
llvm::raw_ostream
This class implements an extremely fast bulk output stream that can only output to a stream.
Definition: raw_ostream.h:52
llvm::InstrProfCorrelatorImpl::getDataPointer
const RawInstrProf::ProfileData< IntPtrT > * getDataPointer() const
Return a pointer to the underlying ProfileData vector that this class constructs.
Definition: InstrProfCorrelator.h:119
llvm::lltok::Kind
Kind
Definition: LLToken.h:18
llvm::InstrProfCorrelator::Context::ShouldSwapBytes
bool ShouldSwapBytes
True if target and host have different endian orders.
Definition: InstrProfCorrelator.h:74
llvm::InstrProfCorrelator::Context::CountersSectionStart
uint64_t CountersSectionStart
The address range of the __llvm_prf_cnts section.
Definition: InstrProfCorrelator.h:71
llvm::sys::getSwappedBytes
unsigned char getSwappedBytes(unsigned char C)
Definition: SwapByteOrder.h:103
llvm::InstrProfCorrelatorImpl::InstrProfCorrelatorImpl
InstrProfCorrelatorImpl(std::unique_ptr< InstrProfCorrelator::Context > Ctx)
llvm::DenseSet< IntPtrT >
llvm::InstrProfCorrelator::get
static llvm::Expected< std::unique_ptr< InstrProfCorrelator > > get(StringRef DebugInfoFilename)
Definition: InstrProfCorrelator.cpp:54
object
bar al al movzbl eax ret Missed when stored in a memory object
Definition: README.txt:1411
uint64_t
llvm::InstrProfCorrelator::InstrProfCorrelatorKind
InstrProfCorrelatorKind
Definition: InstrProfCorrelator.h:61
llvm::InstrProfCorrelator::getNamesPointer
const char * getNamesPointer() const
Return a pointer to the names string that this class constructs.
Definition: InstrProfCorrelator.h:47
llvm::InstrProfCorrelator::Context::CountersSectionEnd
uint64_t CountersSectionEnd
Definition: InstrProfCorrelator.h:72
llvm::InstrProfCorrelatorImpl::Data
std::vector< RawInstrProf::ProfileData< IntPtrT > > Data
Definition: InstrProfCorrelator.h:131
llvm::InstrProfCorrelator::~InstrProfCorrelator
virtual ~InstrProfCorrelator()=default
llvm::InstrProfCorrelator::Probe::FunctionName
std::string FunctionName
Definition: InstrProfCorrelator.h:85
llvm::InstrProfCorrelator::Ctx
const std::unique_ptr< Context > Ctx
Definition: InstrProfCorrelator.h:76
llvm::move
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:1836
llvm::InstrProfCorrelator::Probe::NumCounters
uint32_t NumCounters
Definition: InstrProfCorrelator.h:89
llvm::codeview::CodeViewContainer::ObjectFile
@ ObjectFile
llvm::InstrProfCorrelatorImpl::correlateProfileDataImpl
virtual void correlateProfileDataImpl(InstrProfCorrelator::CorrelationData *Data=nullptr)=0
llvm::InstrProfCorrelatorImpl::classof
static bool classof(const InstrProfCorrelator *C)
YAMLTraits.h
llvm::InstrProfCorrelator::Context::get
static llvm::Expected< std::unique_ptr< Context > > get(std::unique_ptr< MemoryBuffer > Buffer, const object::ObjectFile &Obj)
Definition: InstrProfCorrelator.cpp:40
llvm::InstrProfCorrelator::Context::Buffer
std::unique_ptr< MemoryBuffer > Buffer
Definition: InstrProfCorrelator.h:69
llvm::StringRef
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:50
llvm::object::ObjectFile
This class is the base class for all object file types.
Definition: ObjectFile.h:228
llvm::InstrProfCorrelator::Probe::FilePath
Optional< std::string > FilePath
Definition: InstrProfCorrelator.h:90
uint32_t
llvm::InstrProfCorrelator::CK_64Bit
@ CK_64Bit
Definition: InstrProfCorrelator.h:61
llvm::InstrProfCorrelatorImpl::get
static llvm::Expected< std::unique_ptr< InstrProfCorrelatorImpl< IntPtrT > > > get(std::unique_ptr< InstrProfCorrelator::Context > Ctx, const object::ObjectFile &Obj)
Definition: InstrProfCorrelator.cpp:130
llvm::InstrProfCorrelator::Probe::CFGHash
yaml::Hex64 CFGHash
Definition: InstrProfCorrelator.h:87
llvm::InstrProfCorrelator::Probe::CounterOffset
yaml::Hex64 CounterOffset
Definition: InstrProfCorrelator.h:88
std
Definition: BitVector.h:851
llvm::Error
Lightweight error class with error context and mandatory checking.
Definition: Error.h:155
llvm::InstrProfCorrelator::Probe::LineNumber
Optional< int > LineNumber
Definition: InstrProfCorrelator.h:91
llvm::InstrProfCorrelator::correlateProfileData
virtual Error correlateProfileData()=0
Construct a ProfileData vector used to correlate raw instrumentation data to their functions.
llvm::RawInstrProf::ProfileData
Definition: InstrProf.h:1205
llvm::InstrProfCorrelatorImpl::dumpYaml
Error dumpYaml(raw_ostream &OS) override
Process debug info and dump the correlation data.
Definition: InstrProfCorrelator.cpp:182
llvm::InstrProfCorrelator
InstrProfCorrelator - A base class used to create raw instrumentation data to their functions.
Definition: InstrProfCorrelator.h:31
llvm::InstrProfCorrelator::InstrProfCorrelator
InstrProfCorrelator(InstrProfCorrelatorKind K, std::unique_ptr< Context > Ctx)
Definition: InstrProfCorrelator.h:78
llvm::DWARFDie
Utility class that carries the DWARF compile/type unit and the debug info entry in an object.
Definition: DWARFDie.h:43
llvm::InstrProfCorrelator::Context
Definition: InstrProfCorrelator.h:66
llvm::Value
LLVM Value Representation.
Definition: Value.h:74
llvm::InstrProfCorrelator::NamesVec
std::vector< std::string > NamesVec
Definition: InstrProfCorrelator.h:82
llvm::InstrProfCorrelatorImpl::addProbe
void addProbe(StringRef FunctionName, uint64_t CFGHash, IntPtrT CounterOffset, IntPtrT FunctionPtr, uint32_t NumCounters)
Definition: InstrProfCorrelator.cpp:195