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>
31 inline StringRef getInstrProfCountersSectionName(bool AddSegment) {
32 return AddSegment ? "__DATA,__llvm_prf_cnts" : "__llvm_prf_cnts";
35 inline StringRef getInstrProfNameSectionName(bool AddSegment) {
36 return AddSegment ? "__DATA,__llvm_prf_names" : "__llvm_prf_names";
39 inline StringRef getInstrProfDataSectionName(bool AddSegment) {
40 return AddSegment ? "__DATA,__llvm_prf_data" : "__llvm_prf_data";
43 inline StringRef getInstrProfCoverageSectionName(bool AddSegment) {
44 return AddSegment ? "__DATA,__llvm_covmap" : "__llvm_covmap";
47 inline StringRef getInstrProfNameVarPrefix() { return "__llvm_profile_name_"; }
49 inline StringRef getInstrProfDataVarPrefix() { return "__llvm_profile_data_"; }
51 inline StringRef getInstrProfCountersVarPrefix() {
52 return "__llvm_profile_counters_";
55 inline StringRef getInstrProfComdatPrefix() { return "__llvm_profile_vars_"; }
57 inline StringRef getCoverageMappingVarName() {
58 return "__llvm_coverage_mapping";
61 const std::error_category &instrprof_category();
63 enum class instrprof_error {
69 unsupported_hash_type,
77 value_site_count_mismatch
80 inline std::error_code make_error_code(instrprof_error E) {
81 return std::error_code(static_cast<int>(E), instrprof_category());
84 enum InstrProfValueKind : uint32_t {
85 IPVK_IndirectCallTarget = 0,
87 IPVK_First = IPVK_IndirectCallTarget,
88 IPVK_Last = IPVK_IndirectCallTarget
91 struct InstrProfStringTable {
92 // Set of string values in profiling data.
93 StringSet<> StringValueSet;
94 InstrProfStringTable() { StringValueSet.clear(); }
95 // Get a pointer to internal storage of a string in set
96 const char *getStringData(StringRef Str) {
97 auto Result = StringValueSet.find(Str);
98 return (Result == StringValueSet.end()) ? nullptr : Result->first().data();
100 // Insert a string to StringTable
101 const char *insertString(StringRef Str) {
102 auto Result = StringValueSet.insert(Str);
103 return Result.first->first().data();
107 struct InstrProfValueSiteRecord {
108 /// Typedef for a single TargetValue-NumTaken pair.
109 typedef std::pair<uint64_t, uint64_t> ValueDataPair;
110 /// Value profiling data pairs at a given value site.
111 std::list<ValueDataPair> ValueData;
113 InstrProfValueSiteRecord() { ValueData.clear(); }
115 /// Sort ValueData ascending by TargetValue
116 void sortByTargetValues() {
117 ValueData.sort([](const ValueDataPair &left, const ValueDataPair &right) {
118 return left.first < right.first;
122 /// Merge data from another InstrProfValueSiteRecord
123 void mergeValueData(InstrProfValueSiteRecord &Input) {
124 this->sortByTargetValues();
125 Input.sortByTargetValues();
126 auto I = ValueData.begin();
127 auto IE = ValueData.end();
128 for (auto J = Input.ValueData.begin(), JE = Input.ValueData.end(); J != JE;
130 while (I != IE && I->first < J->first)
132 if (I != IE && I->first == J->first) {
133 I->second += J->second;
137 ValueData.insert(I, *J);
142 /// Profiling information for a single function.
143 struct InstrProfRecord {
145 InstrProfRecord(StringRef Name, uint64_t Hash, std::vector<uint64_t> Counts)
146 : Name(Name), Hash(Hash), Counts(std::move(Counts)) {}
149 std::vector<uint64_t> Counts;
150 std::vector<InstrProfValueSiteRecord> IndirectCallSites;
152 const std::vector<InstrProfValueSiteRecord> &
153 getValueSitesForKind(uint32_t ValueKind) const {
155 case IPVK_IndirectCallTarget:
156 return IndirectCallSites;
158 llvm_unreachable("Unknown value kind!");
161 std::vector<InstrProfValueSiteRecord> &
162 getValueSitesForKind(uint32_t ValueKind) {
163 return const_cast<std::vector<InstrProfValueSiteRecord> &>(
164 const_cast<const InstrProfRecord *>(this)
165 ->getValueSitesForKind(ValueKind));
169 namespace IndexedInstrProf {
170 enum class HashT : uint32_t {
176 static inline uint64_t MD5Hash(StringRef Str) {
179 llvm::MD5::MD5Result Result;
181 // Return the least significant 8 bytes. Our MD5 implementation returns the
182 // result in little endian, so we may need to swap bytes.
183 using namespace llvm::support;
184 return endian::read<uint64_t, little, unaligned>(Result);
187 static inline uint64_t ComputeHash(HashT Type, StringRef K) {
190 return IndexedInstrProf::MD5Hash(K);
192 llvm_unreachable("Unhandled hash type");
195 const uint64_t Magic = 0x8169666f72706cff; // "\xfflprofi\x81"
196 const uint64_t Version = 3;
197 const HashT HashType = HashT::MD5;
202 uint64_t MaxFunctionCount;
207 } // end namespace IndexedInstrProf
209 namespace RawInstrProf {
211 const uint64_t Version = 1;
213 // Magic number to detect file format and endianness.
214 // Use 255 at one end, since no UTF-8 file can use that character. Avoid 0,
215 // so that utilities, like strings, don't grab it as a string. 129 is also
216 // invalid UTF-8, and high enough to be interesting.
217 // Use "lprofr" in the centre to stand for "LLVM Profile Raw", or "lprofR"
218 // for 32-bit platforms.
219 // The magic and version need to be kept in sync with
220 // projects/compiler-rt/lib/profile/InstrProfiling.c
222 template <class IntPtrT>
223 inline uint64_t getMagic();
225 inline uint64_t getMagic<uint64_t>() {
226 return uint64_t(255) << 56 | uint64_t('l') << 48 | uint64_t('p') << 40 |
227 uint64_t('r') << 32 | uint64_t('o') << 24 | uint64_t('f') << 16 |
228 uint64_t('r') << 8 | uint64_t(129);
232 inline uint64_t getMagic<uint32_t>() {
233 return uint64_t(255) << 56 | uint64_t('l') << 48 | uint64_t('p') << 40 |
234 uint64_t('r') << 32 | uint64_t('o') << 24 | uint64_t('f') << 16 |
235 uint64_t('R') << 8 | uint64_t(129);
238 // The definition should match the structure defined in
239 // compiler-rt/lib/profile/InstrProfiling.h.
240 // It should also match the synthesized type in
241 // Transforms/Instrumentation/InstrProfiling.cpp:getOrCreateRegionCounters.
243 template <class IntPtrT>
245 const uint32_t NameSize;
246 const uint32_t NumCounters;
247 const uint64_t FuncHash;
248 const IntPtrT NamePtr;
249 const IntPtrT CounterPtr;
252 // The definition should match the header referenced in
253 // compiler-rt/lib/profile/InstrProfilingFile.c and
254 // InstrProfilingBuffer.c.
257 const uint64_t Magic;
258 const uint64_t Version;
259 const uint64_t DataSize;
260 const uint64_t CountersSize;
261 const uint64_t NamesSize;
262 const uint64_t CountersDelta;
263 const uint64_t NamesDelta;
266 } // end namespace RawInstrProf
268 } // end namespace llvm
272 struct is_error_code_enum<llvm::instrprof_error> : std::true_type {};
275 #endif // LLVM_PROFILEDATA_INSTRPROF_H_