Debian Codegraph @

suite/synctools/tablegen/include/llvm/ProfileData/InstrProfCorrelator.h

raw symbols SCIP
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"
16 #include "llvm/DebugInfo/DWARF/DWARFContext.h"
17 #include "llvm/Object/Binary.h"
18 #include "llvm/Object/ObjectFile.h"
19 #include "llvm/ProfileData/InstrProf.h"
20 #include "llvm/Support/Casting.h"
21 #include "llvm/Support/Error.h"
22 #include "llvm/Support/MemoryBuffer.h"
23 #include <vector>
24
25 namespace llvm {
26
27 /// InstrProfCorrelator - A base class used to create raw instrumentation data
28 /// to their functions.
29 class InstrProfCorrelator {
30 public:
31 static llvm::Expected<std::unique_ptr<InstrProfCorrelator>>
32 get(StringRef DebugInfoFilename);
33
34 /// Construct a ProfileData vector used to correlate raw instrumentation data
35 /// to their functions.
36 virtual Error correlateProfileData() = 0;
37
38 /// Return the number of ProfileData elements.
39 llvm::Optional<size_t> getDataSize() const;
40
41 /// Return a pointer to the names string that this class constructs.
42 const char *getNamesPointer() const { return Names.c_str(); }
43
44 /// Return the number of bytes in the names string.
45 size_t getNamesSize() const { return Names.size(); }
46
47 /// Return the size of the counters section in bytes.
48 uint64_t getCountersSectionSize() const {
49 return Ctx->CountersSectionEnd - Ctx->CountersSectionStart;
50 }
51
52 static const char *FunctionNameAttributeName;
53 static const char *CFGHashAttributeName;
54 static const char *NumCountersAttributeName;
55
56 enum InstrProfCorrelatorKind { CK_32Bit, CK_64Bit };
57 InstrProfCorrelatorKind getKind() const { return Kind; }
58 virtual ~InstrProfCorrelator() = default;
59
60 protected:
61 struct Context {
62 static llvm::Expected<std::unique_ptr<Context>>
63 get(std::unique_ptr<MemoryBuffer> Buffer, const object::ObjectFile &Obj);
64 std::unique_ptr<MemoryBuffer> Buffer;
65 /// The address range of the __llvm_prf_cnts section.
66 uint64_t CountersSectionStart;
67 uint64_t CountersSectionEnd;
68 /// True if target and host have different endian orders.
69 bool ShouldSwapBytes;
70 };
71 const std::unique_ptr<InstrProfCorrelator::Context> Ctx;
72
73 InstrProfCorrelator(InstrProfCorrelatorKind K, std::unique_ptr<Context> Ctx)
74 : Ctx(std::move(Ctx)), Kind(K) {}
75
76 std::string Names;
77 std::vector<std::string> NamesVec;
78
79 private:
80 static llvm::Expected<std::unique_ptr<InstrProfCorrelator>>
81 get(std::unique_ptr<MemoryBuffer> Buffer);
82
83 const InstrProfCorrelatorKind Kind;
84 };
85
86 /// InstrProfCorrelatorImpl - A child of InstrProfCorrelator with a template
87 /// pointer type so that the ProfileData vector can be materialized.
88 template <class IntPtrT>
89 class InstrProfCorrelatorImpl : public InstrProfCorrelator {
90 public:
91 InstrProfCorrelatorImpl(std::unique_ptr<InstrProfCorrelator::Context> Ctx);
92 static bool classof(const InstrProfCorrelator *C);
93
94 /// Return a pointer to the underlying ProfileData vector that this class
95 /// constructs.
96 const RawInstrProf::ProfileData<IntPtrT> *getDataPointer() const {
97 return Data.empty() ? nullptr : Data.data();
98 }
99
100 /// Return the number of ProfileData elements.
101 size_t getDataSize() const { return Data.size(); }
102
103 static llvm::Expected<std::unique_ptr<InstrProfCorrelatorImpl<IntPtrT>>>
104 get(std::unique_ptr<InstrProfCorrelator::Context> Ctx,
105 const object::ObjectFile &Obj);
106
107 protected:
108 std::vector<RawInstrProf::ProfileData<IntPtrT>> Data;
109
110 Error correlateProfileData() override;
111 virtual void correlateProfileDataImpl() = 0;
112
113 void addProbe(StringRef FunctionName, uint64_t CFGHash, IntPtrT CounterOffset,
114 IntPtrT FunctionPtr, uint32_t NumCounters);
115
116 private:
117 InstrProfCorrelatorImpl(InstrProfCorrelatorKind Kind,
118 std::unique_ptr<InstrProfCorrelator::Context> Ctx)
119 : InstrProfCorrelator(Kind, std::move(Ctx)){};
120 llvm::DenseSet<IntPtrT> CounterOffsets;
121
122 // Byte-swap the value if necessary.
123 template <class T> T maybeSwap(T Value) const {
124 return Ctx->ShouldSwapBytes ? sys::getSwappedBytes(Value) : Value;
125 }
126 };
127
128 /// DwarfInstrProfCorrelator - A child of InstrProfCorrelatorImpl that takes
129 /// DWARF debug info as input to correlate profiles.
130 template <class IntPtrT>
131 class DwarfInstrProfCorrelator : public InstrProfCorrelatorImpl<IntPtrT> {
132 public:
133 DwarfInstrProfCorrelator(std::unique_ptr<DWARFContext> DICtx,
134 std::unique_ptr<InstrProfCorrelator::Context> Ctx)
135 : InstrProfCorrelatorImpl<IntPtrT>(std::move(Ctx)),
136 DICtx(std::move(DICtx)) {}
137
138 private:
139 std::unique_ptr<DWARFContext> DICtx;
140
141 /// Return the address of the object that the provided DIE symbolizes.
142 llvm::Optional<uint64_t> getLocation(const DWARFDie &Die) const;
143
144 /// Returns true if the provided DIE symbolizes an instrumentation probe
145 /// symbol.
146 static bool isDIEOfProbe(const DWARFDie &Die);
147
148 /// Iterate over DWARF DIEs to find those that symbolize instrumentation
149 /// probes and construct the ProfileData vector and Names string.
150 ///
151 /// Here is some example DWARF for an instrumentation probe we are looking
152 /// for:
153 /// \code
154 /// DW_TAG_subprogram
155 /// DW_AT_low_pc (0x0000000000000000)
156 /// DW_AT_high_pc (0x0000000000000014)
157 /// DW_AT_name ("foo")
158 /// DW_TAG_variable
159 /// DW_AT_name ("__profc_foo")
160 /// DW_AT_location (DW_OP_addr 0x0)
161 /// DW_TAG_LLVM_annotation
162 /// DW_AT_name ("Function Name")
163 /// DW_AT_const_value ("foo")
164 /// DW_TAG_LLVM_annotation
165 /// DW_AT_name ("CFG Hash")
166 /// DW_AT_const_value (12345678)
167 /// DW_TAG_LLVM_annotation
168 /// DW_AT_name ("Num Counters")
169 /// DW_AT_const_value (2)
170 /// NULL
171 /// NULL
172 /// \endcode
173 void correlateProfileDataImpl() override;
174 };
175
176 } // end namespace llvm
177
178 #endif // LLVM_PROFILEDATA_INSTRPROFCORRELATOR_H