1 //=-- SampleProf.h - Sampling profiling format support --------------------===//
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 // This file contains common definitions used in the reading and writing of
11 // sample profile data.
13 //===----------------------------------------------------------------------===//
15 #ifndef LLVM_PROFILEDATA_SAMPLEPROF_H_
16 #define LLVM_PROFILEDATA_SAMPLEPROF_H_
18 #include "llvm/ADT/DenseMap.h"
19 #include "llvm/ADT/SmallVector.h"
20 #include "llvm/ADT/StringMap.h"
21 #include "llvm/Support/Debug.h"
22 #include "llvm/Support/ErrorOr.h"
23 #include "llvm/Support/raw_ostream.h"
24 #include <system_error>
28 const std::error_category &sampleprof_category();
30 enum class sampleprof_error {
38 unsupported_writing_format,
43 inline std::error_code make_error_code(sampleprof_error E) {
44 return std::error_code(static_cast<int>(E), sampleprof_category());
47 } // end namespace llvm
51 struct is_error_code_enum<llvm::sampleprof_error> : std::true_type {};
56 namespace sampleprof {
58 static inline uint64_t SPMagic() {
59 return uint64_t('S') << (64 - 8) | uint64_t('P') << (64 - 16) |
60 uint64_t('R') << (64 - 24) | uint64_t('O') << (64 - 32) |
61 uint64_t('F') << (64 - 40) | uint64_t('4') << (64 - 48) |
62 uint64_t('2') << (64 - 56) | uint64_t(0xff);
65 static inline uint64_t SPVersion() { return 102; }
67 /// Represents the relative location of an instruction.
69 /// Instruction locations are specified by the line offset from the
70 /// beginning of the function (marked by the line where the function
71 /// header is) and the discriminator value within that line.
73 /// The discriminator value is useful to distinguish instructions
74 /// that are on the same line but belong to different basic blocks
75 /// (e.g., the two post-increment instructions in "if (p) x++; else y++;").
77 LineLocation(uint32_t L, uint32_t D) : LineOffset(L), Discriminator(D) {}
78 void print(raw_ostream &OS) const;
82 uint32_t Discriminator;
85 raw_ostream &operator<<(raw_ostream &OS, const LineLocation &Loc);
87 /// Represents the relative location of a callsite.
89 /// Callsite locations are specified by the line offset from the
90 /// beginning of the function (marked by the line where the function
91 /// head is), the discriminator value within that line, and the callee
93 struct CallsiteLocation : public LineLocation {
94 CallsiteLocation(uint32_t L, uint32_t D, StringRef N)
95 : LineLocation(L, D), CalleeName(N) {}
96 void print(raw_ostream &OS) const;
102 raw_ostream &operator<<(raw_ostream &OS, const CallsiteLocation &Loc);
104 } // End namespace sampleprof
106 template <> struct DenseMapInfo<sampleprof::LineLocation> {
107 typedef DenseMapInfo<uint32_t> OffsetInfo;
108 typedef DenseMapInfo<uint32_t> DiscriminatorInfo;
109 static inline sampleprof::LineLocation getEmptyKey() {
110 return sampleprof::LineLocation(OffsetInfo::getEmptyKey(),
111 DiscriminatorInfo::getEmptyKey());
113 static inline sampleprof::LineLocation getTombstoneKey() {
114 return sampleprof::LineLocation(OffsetInfo::getTombstoneKey(),
115 DiscriminatorInfo::getTombstoneKey());
117 static inline unsigned getHashValue(sampleprof::LineLocation Val) {
118 return DenseMapInfo<std::pair<uint32_t, uint32_t>>::getHashValue(
119 std::pair<uint32_t, uint32_t>(Val.LineOffset, Val.Discriminator));
121 static inline bool isEqual(sampleprof::LineLocation LHS,
122 sampleprof::LineLocation RHS) {
123 return LHS.LineOffset == RHS.LineOffset &&
124 LHS.Discriminator == RHS.Discriminator;
128 template <> struct DenseMapInfo<sampleprof::CallsiteLocation> {
129 typedef DenseMapInfo<uint32_t> OffsetInfo;
130 typedef DenseMapInfo<uint32_t> DiscriminatorInfo;
131 typedef DenseMapInfo<StringRef> CalleeNameInfo;
132 static inline sampleprof::CallsiteLocation getEmptyKey() {
133 return sampleprof::CallsiteLocation(OffsetInfo::getEmptyKey(),
134 DiscriminatorInfo::getEmptyKey(), "");
136 static inline sampleprof::CallsiteLocation getTombstoneKey() {
137 return sampleprof::CallsiteLocation(OffsetInfo::getTombstoneKey(),
138 DiscriminatorInfo::getTombstoneKey(),
141 static inline unsigned getHashValue(sampleprof::CallsiteLocation Val) {
142 return DenseMapInfo<std::pair<uint32_t, uint32_t>>::getHashValue(
143 std::pair<uint32_t, uint32_t>(Val.LineOffset, Val.Discriminator));
145 static inline bool isEqual(sampleprof::CallsiteLocation LHS,
146 sampleprof::CallsiteLocation RHS) {
147 return LHS.LineOffset == RHS.LineOffset &&
148 LHS.Discriminator == RHS.Discriminator &&
149 LHS.CalleeName.equals(RHS.CalleeName);
153 namespace sampleprof {
155 /// Representation of a single sample record.
157 /// A sample record is represented by a positive integer value, which
158 /// indicates how frequently was the associated line location executed.
160 /// Additionally, if the associated location contains a function call,
161 /// the record will hold a list of all the possible called targets. For
162 /// direct calls, this will be the exact function being invoked. For
163 /// indirect calls (function pointers, virtual table dispatch), this
164 /// will be a list of one or more functions.
167 typedef StringMap<uint64_t> CallTargetMap;
169 SampleRecord() : NumSamples(0), CallTargets() {}
171 /// Increment the number of samples for this record by \p S.
173 /// Sample counts accumulate using saturating arithmetic, to avoid wrapping
174 /// around unsigned integers.
175 void addSamples(uint64_t S) {
176 NumSamples = SaturatingAdd(NumSamples, S);
179 /// Add called function \p F with samples \p S.
181 /// Sample counts accumulate using saturating arithmetic, to avoid wrapping
182 /// around unsigned integers.
183 void addCalledTarget(StringRef F, uint64_t S) {
184 uint64_t &TargetSamples = CallTargets[F];
185 TargetSamples = SaturatingAdd(TargetSamples, S);
188 /// Return true if this sample record contains function calls.
189 bool hasCalls() const { return CallTargets.size() > 0; }
191 uint64_t getSamples() const { return NumSamples; }
192 const CallTargetMap &getCallTargets() const { return CallTargets; }
194 /// Merge the samples in \p Other into this record.
195 void merge(const SampleRecord &Other) {
196 addSamples(Other.getSamples());
197 for (const auto &I : Other.getCallTargets())
198 addCalledTarget(I.first(), I.second);
201 void print(raw_ostream &OS, unsigned Indent) const;
206 CallTargetMap CallTargets;
209 raw_ostream &operator<<(raw_ostream &OS, const SampleRecord &Sample);
211 typedef DenseMap<LineLocation, SampleRecord> BodySampleMap;
212 class FunctionSamples;
213 typedef DenseMap<CallsiteLocation, FunctionSamples> CallsiteSampleMap;
215 /// Representation of the samples collected for a function.
217 /// This data structure contains all the collected samples for the body
218 /// of a function. Each sample corresponds to a LineLocation instance
219 /// within the body of the function.
220 class FunctionSamples {
222 FunctionSamples() : TotalSamples(0), TotalHeadSamples(0) {}
223 void print(raw_ostream &OS = dbgs(), unsigned Indent = 0) const;
225 void addTotalSamples(uint64_t Num) { TotalSamples += Num; }
226 void addHeadSamples(uint64_t Num) { TotalHeadSamples += Num; }
227 void addBodySamples(uint32_t LineOffset, uint32_t Discriminator,
229 BodySamples[LineLocation(LineOffset, Discriminator)].addSamples(Num);
231 void addCalledTargetSamples(uint32_t LineOffset, uint32_t Discriminator,
232 std::string FName, uint64_t Num) {
233 BodySamples[LineLocation(LineOffset, Discriminator)].addCalledTarget(FName,
237 /// Return the number of samples collected at the given location.
238 /// Each location is specified by \p LineOffset and \p Discriminator.
239 /// If the location is not found in profile, return error.
240 ErrorOr<uint64_t> findSamplesAt(uint32_t LineOffset,
241 uint32_t Discriminator) const {
242 const auto &ret = BodySamples.find(LineLocation(LineOffset, Discriminator));
243 if (ret == BodySamples.end())
244 return std::error_code();
246 return ret->second.getSamples();
249 /// Return the function samples at the given callsite location.
250 FunctionSamples &functionSamplesAt(const CallsiteLocation &Loc) {
251 return CallsiteSamples[Loc];
254 /// Return a pointer to function samples at the given callsite location.
255 const FunctionSamples *
256 findFunctionSamplesAt(const CallsiteLocation &Loc) const {
257 auto iter = CallsiteSamples.find(Loc);
258 if (iter == CallsiteSamples.end()) {
261 return &iter->second;
265 bool empty() const { return TotalSamples == 0; }
267 /// Return the total number of samples collected inside the function.
268 uint64_t getTotalSamples() const { return TotalSamples; }
270 /// Return the total number of samples collected at the head of the
272 uint64_t getHeadSamples() const { return TotalHeadSamples; }
274 /// Return all the samples collected in the body of the function.
275 const BodySampleMap &getBodySamples() const { return BodySamples; }
277 /// Return all the callsite samples collected in the body of the function.
278 const CallsiteSampleMap &getCallsiteSamples() const {
279 return CallsiteSamples;
282 /// Merge the samples in \p Other into this one.
283 void merge(const FunctionSamples &Other) {
284 addTotalSamples(Other.getTotalSamples());
285 addHeadSamples(Other.getHeadSamples());
286 for (const auto &I : Other.getBodySamples()) {
287 const LineLocation &Loc = I.first;
288 const SampleRecord &Rec = I.second;
289 BodySamples[Loc].merge(Rec);
291 for (const auto &I : Other.getCallsiteSamples()) {
292 const CallsiteLocation &Loc = I.first;
293 const FunctionSamples &Rec = I.second;
294 functionSamplesAt(Loc).merge(Rec);
299 /// Total number of samples collected inside this function.
301 /// Samples are cumulative, they include all the samples collected
302 /// inside this function and all its inlined callees.
303 uint64_t TotalSamples;
305 /// Total number of samples collected at the head of the function.
306 /// This is an approximation of the number of calls made to this function
308 uint64_t TotalHeadSamples;
310 /// Map instruction locations to collected samples.
312 /// Each entry in this map contains the number of samples
313 /// collected at the corresponding line offset. All line locations
314 /// are an offset from the start of the function.
315 BodySampleMap BodySamples;
317 /// Map call sites to collected samples for the called function.
319 /// Each entry in this map corresponds to all the samples
320 /// collected for the inlined function call at the given
321 /// location. For example, given:
329 /// If the bar() and baz() calls were inlined inside foo(), this
330 /// map will contain two entries. One for all the samples collected
331 /// in the call to bar() at line offset 1, the other for all the samples
332 /// collected in the call to baz() at line offset 8.
333 CallsiteSampleMap CallsiteSamples;
336 raw_ostream &operator<<(raw_ostream &OS, const FunctionSamples &FS);
338 } // end namespace sampleprof
340 } // end namespace llvm
342 #endif // LLVM_PROFILEDATA_SAMPLEPROF_H_