1 //=-- InstrProf.h - Instrumented profiling format support ---------*- C++ -*-=//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // Instrumentation-based profiling data is generated by instrumented
11 // binaries through library functions in compiler-rt, and read by the clang
12 // frontend to feed PGO.
14 //===----------------------------------------------------------------------===//
16 #ifndef LLVM_PROFILEDATA_INSTRPROF_H_
17 #define LLVM_PROFILEDATA_INSTRPROF_H_
19 #include "llvm/ADT/StringRef.h"
20 #include "llvm/ADT/StringSet.h"
21 #include "llvm/Support/Endian.h"
22 #include "llvm/Support/ErrorHandling.h"
23 #include "llvm/Support/MD5.h"
26 #include <system_error>
30 const std::error_category &instrprof_category();
32 enum class instrprof_error {
38 unsupported_hash_type,
46 value_site_count_mismatch
49 inline std::error_code make_error_code(instrprof_error E) {
50 return std::error_code(static_cast<int>(E), instrprof_category());
53 enum InstrProfValueKind : uint32_t {
54 IPVK_IndirectCallTarget = 0,
56 IPVK_First = IPVK_IndirectCallTarget,
57 IPVK_Last = IPVK_IndirectCallTarget
60 struct InstrProfStringTable {
61 // Set of string values in profiling data.
62 StringSet<> StringValueSet;
63 InstrProfStringTable() { StringValueSet.clear(); }
64 // Get a pointer to internal storage of a string in set
65 const char *getStringData(StringRef Str) {
66 auto Result = StringValueSet.find(Str);
67 return (Result == StringValueSet.end()) ? nullptr : Result->first().data();
69 // Insert a string to StringTable
70 const char *insertString(StringRef Str) {
71 auto Result = StringValueSet.insert(Str);
72 return Result.first->first().data();
76 struct InstrProfValueSiteRecord {
77 /// Typedef for a single TargetValue-NumTaken pair.
78 typedef std::pair<uint64_t, uint64_t> ValueDataPair;
79 /// Value profiling data pairs at a given value site.
80 std::list<ValueDataPair> ValueData;
82 InstrProfValueSiteRecord() { ValueData.clear(); }
84 /// Sort ValueData ascending by TargetValue
85 void sortByTargetValues() {
86 ValueData.sort([](const ValueDataPair &left, const ValueDataPair &right) {
87 return left.first < right.first;
91 /// Merge data from another InstrProfValueSiteRecord
92 void mergeValueData(InstrProfValueSiteRecord &Input) {
93 this->sortByTargetValues();
94 Input.sortByTargetValues();
95 auto I = ValueData.begin();
96 auto IE = ValueData.end();
97 for (auto J = Input.ValueData.begin(), JE = Input.ValueData.end(); J != JE;
99 while (I != IE && I->first < J->first)
101 if (I != IE && I->first == J->first) {
102 I->second += J->second;
106 ValueData.insert(I, *J);
111 /// Profiling information for a single function.
112 struct InstrProfRecord {
114 InstrProfRecord(StringRef Name, uint64_t Hash, std::vector<uint64_t> Counts)
115 : Name(Name), Hash(Hash), Counts(std::move(Counts)) {}
118 std::vector<uint64_t> Counts;
119 std::vector<InstrProfValueSiteRecord> IndirectCallSites;
121 const std::vector<InstrProfValueSiteRecord> &
122 getValueSitesForKind(uint32_t ValueKind) const {
124 case IPVK_IndirectCallTarget:
125 return IndirectCallSites;
127 llvm_unreachable("Unknown value kind!");
130 std::vector<InstrProfValueSiteRecord> &
131 getValueSitesForKind(uint32_t ValueKind) {
132 return const_cast<std::vector<InstrProfValueSiteRecord> &>(
133 const_cast<const InstrProfRecord *>(this)
134 ->getValueSitesForKind(ValueKind));
138 namespace IndexedInstrProf {
139 enum class HashT : uint32_t {
145 static inline uint64_t MD5Hash(StringRef Str) {
148 llvm::MD5::MD5Result Result;
150 // Return the least significant 8 bytes. Our MD5 implementation returns the
151 // result in little endian, so we may need to swap bytes.
152 using namespace llvm::support;
153 return endian::read<uint64_t, little, unaligned>(Result);
156 static inline uint64_t ComputeHash(HashT Type, StringRef K) {
159 return IndexedInstrProf::MD5Hash(K);
161 llvm_unreachable("Unhandled hash type");
164 const uint64_t Magic = 0x8169666f72706cff; // "\xfflprofi\x81"
165 const uint64_t Version = 3;
166 const HashT HashType = HashT::MD5;
171 uint64_t MaxFunctionCount;
176 } // end namespace IndexedInstrProf
178 namespace RawInstrProf {
180 const uint64_t Version = 1;
182 // Magic number to detect file format and endianness.
183 // Use 255 at one end, since no UTF-8 file can use that character. Avoid 0,
184 // so that utilities, like strings, don't grab it as a string. 129 is also
185 // invalid UTF-8, and high enough to be interesting.
186 // Use "lprofr" in the centre to stand for "LLVM Profile Raw", or "lprofR"
187 // for 32-bit platforms.
188 // The magic and version need to be kept in sync with
189 // projects/compiler-rt/lib/profile/InstrProfiling.c
191 template <class IntPtrT>
192 inline uint64_t getMagic();
194 inline uint64_t getMagic<uint64_t>() {
195 return uint64_t(255) << 56 | uint64_t('l') << 48 | uint64_t('p') << 40 |
196 uint64_t('r') << 32 | uint64_t('o') << 24 | uint64_t('f') << 16 |
197 uint64_t('r') << 8 | uint64_t(129);
201 inline uint64_t getMagic<uint32_t>() {
202 return uint64_t(255) << 56 | uint64_t('l') << 48 | uint64_t('p') << 40 |
203 uint64_t('r') << 32 | uint64_t('o') << 24 | uint64_t('f') << 16 |
204 uint64_t('R') << 8 | uint64_t(129);
207 // The definition should match the structure defined in
208 // compiler-rt/lib/profile/InstrProfiling.h.
209 // It should also match the synthesized type in
210 // Transforms/Instrumentation/InstrProfiling.cpp:getOrCreateRegionCounters.
212 template <class IntPtrT>
214 const uint32_t NameSize;
215 const uint32_t NumCounters;
216 const uint64_t FuncHash;
217 const IntPtrT NamePtr;
218 const IntPtrT CounterPtr;
221 // The definition should match the header referenced in
222 // compiler-rt/lib/profile/InstrProfilingFile.c and
223 // InstrProfilingBuffer.c.
226 const uint64_t Magic;
227 const uint64_t Version;
228 const uint64_t DataSize;
229 const uint64_t CountersSize;
230 const uint64_t NamesSize;
231 const uint64_t CountersDelta;
232 const uint64_t NamesDelta;
235 } // end namespace RawInstrProf
237 } // end namespace llvm
241 struct is_error_code_enum<llvm::instrprof_error> : std::true_type {};
244 #endif // LLVM_PROFILEDATA_INSTRPROF_H_