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;
80 bool operator<(const LineLocation &O) const {
81 return LineOffset < O.LineOffset ||
82 (LineOffset == O.LineOffset && Discriminator < O.Discriminator);
86 uint32_t Discriminator;
89 raw_ostream &operator<<(raw_ostream &OS, const LineLocation &Loc);
91 /// Represents the relative location of a callsite.
93 /// Callsite locations are specified by the line offset from the
94 /// beginning of the function (marked by the line where the function
95 /// head is), the discriminator value within that line, and the callee
97 struct CallsiteLocation : public LineLocation {
98 CallsiteLocation(uint32_t L, uint32_t D, StringRef N)
99 : LineLocation(L, D), CalleeName(N) {}
100 void print(raw_ostream &OS) const;
103 StringRef CalleeName;
106 raw_ostream &operator<<(raw_ostream &OS, const CallsiteLocation &Loc);
108 } // End namespace sampleprof
110 template <> struct DenseMapInfo<sampleprof::LineLocation> {
111 typedef DenseMapInfo<uint32_t> OffsetInfo;
112 typedef DenseMapInfo<uint32_t> DiscriminatorInfo;
113 static inline sampleprof::LineLocation getEmptyKey() {
114 return sampleprof::LineLocation(OffsetInfo::getEmptyKey(),
115 DiscriminatorInfo::getEmptyKey());
117 static inline sampleprof::LineLocation getTombstoneKey() {
118 return sampleprof::LineLocation(OffsetInfo::getTombstoneKey(),
119 DiscriminatorInfo::getTombstoneKey());
121 static inline unsigned getHashValue(sampleprof::LineLocation Val) {
122 return DenseMapInfo<std::pair<uint32_t, uint32_t>>::getHashValue(
123 std::pair<uint32_t, uint32_t>(Val.LineOffset, Val.Discriminator));
125 static inline bool isEqual(sampleprof::LineLocation LHS,
126 sampleprof::LineLocation RHS) {
127 return LHS.LineOffset == RHS.LineOffset &&
128 LHS.Discriminator == RHS.Discriminator;
132 template <> struct DenseMapInfo<sampleprof::CallsiteLocation> {
133 typedef DenseMapInfo<uint32_t> OffsetInfo;
134 typedef DenseMapInfo<uint32_t> DiscriminatorInfo;
135 typedef DenseMapInfo<StringRef> CalleeNameInfo;
136 static inline sampleprof::CallsiteLocation getEmptyKey() {
137 return sampleprof::CallsiteLocation(OffsetInfo::getEmptyKey(),
138 DiscriminatorInfo::getEmptyKey(), "");
140 static inline sampleprof::CallsiteLocation getTombstoneKey() {
141 return sampleprof::CallsiteLocation(OffsetInfo::getTombstoneKey(),
142 DiscriminatorInfo::getTombstoneKey(),
145 static inline unsigned getHashValue(sampleprof::CallsiteLocation Val) {
146 return DenseMapInfo<std::pair<uint32_t, uint32_t>>::getHashValue(
147 std::pair<uint32_t, uint32_t>(Val.LineOffset, Val.Discriminator));
149 static inline bool isEqual(sampleprof::CallsiteLocation LHS,
150 sampleprof::CallsiteLocation RHS) {
151 return LHS.LineOffset == RHS.LineOffset &&
152 LHS.Discriminator == RHS.Discriminator &&
153 LHS.CalleeName.equals(RHS.CalleeName);
157 namespace sampleprof {
159 /// Representation of a single sample record.
161 /// A sample record is represented by a positive integer value, which
162 /// indicates how frequently was the associated line location executed.
164 /// Additionally, if the associated location contains a function call,
165 /// the record will hold a list of all the possible called targets. For
166 /// direct calls, this will be the exact function being invoked. For
167 /// indirect calls (function pointers, virtual table dispatch), this
168 /// will be a list of one or more functions.
171 typedef StringMap<uint64_t> CallTargetMap;
173 SampleRecord() : NumSamples(0), CallTargets() {}
175 /// Increment the number of samples for this record by \p S.
177 /// Sample counts accumulate using saturating arithmetic, to avoid wrapping
178 /// around unsigned integers.
179 void addSamples(uint64_t S) {
180 NumSamples = SaturatingAdd(NumSamples, S);
183 /// Add called function \p F with samples \p S.
185 /// Sample counts accumulate using saturating arithmetic, to avoid wrapping
186 /// around unsigned integers.
187 void addCalledTarget(StringRef F, uint64_t S) {
188 uint64_t &TargetSamples = CallTargets[F];
189 TargetSamples = SaturatingAdd(TargetSamples, S);
192 /// Return true if this sample record contains function calls.
193 bool hasCalls() const { return CallTargets.size() > 0; }
195 uint64_t getSamples() const { return NumSamples; }
196 const CallTargetMap &getCallTargets() const { return CallTargets; }
198 /// Merge the samples in \p Other into this record.
199 void merge(const SampleRecord &Other) {
200 addSamples(Other.getSamples());
201 for (const auto &I : Other.getCallTargets())
202 addCalledTarget(I.first(), I.second);
205 void print(raw_ostream &OS, unsigned Indent) const;
210 CallTargetMap CallTargets;
213 raw_ostream &operator<<(raw_ostream &OS, const SampleRecord &Sample);
215 typedef DenseMap<LineLocation, SampleRecord> BodySampleMap;
216 class FunctionSamples;
217 typedef DenseMap<CallsiteLocation, FunctionSamples> CallsiteSampleMap;
219 /// Representation of the samples collected for a function.
221 /// This data structure contains all the collected samples for the body
222 /// of a function. Each sample corresponds to a LineLocation instance
223 /// within the body of the function.
224 class FunctionSamples {
226 FunctionSamples() : TotalSamples(0), TotalHeadSamples(0) {}
227 void print(raw_ostream &OS = dbgs(), unsigned Indent = 0) const;
229 void addTotalSamples(uint64_t Num) { TotalSamples += Num; }
230 void addHeadSamples(uint64_t Num) { TotalHeadSamples += Num; }
231 void addBodySamples(uint32_t LineOffset, uint32_t Discriminator,
233 BodySamples[LineLocation(LineOffset, Discriminator)].addSamples(Num);
235 void addCalledTargetSamples(uint32_t LineOffset, uint32_t Discriminator,
236 std::string FName, uint64_t Num) {
237 BodySamples[LineLocation(LineOffset, Discriminator)].addCalledTarget(FName,
241 /// Return the number of samples collected at the given location.
242 /// Each location is specified by \p LineOffset and \p Discriminator.
243 /// If the location is not found in profile, return error.
244 ErrorOr<uint64_t> findSamplesAt(uint32_t LineOffset,
245 uint32_t Discriminator) const {
246 const auto &ret = BodySamples.find(LineLocation(LineOffset, Discriminator));
247 if (ret == BodySamples.end())
248 return std::error_code();
250 return ret->second.getSamples();
253 /// Return the function samples at the given callsite location.
254 FunctionSamples &functionSamplesAt(const CallsiteLocation &Loc) {
255 return CallsiteSamples[Loc];
258 /// Return a pointer to function samples at the given callsite location.
259 const FunctionSamples *
260 findFunctionSamplesAt(const CallsiteLocation &Loc) const {
261 auto iter = CallsiteSamples.find(Loc);
262 if (iter == CallsiteSamples.end()) {
265 return &iter->second;
269 bool empty() const { return TotalSamples == 0; }
271 /// Return the total number of samples collected inside the function.
272 uint64_t getTotalSamples() const { return TotalSamples; }
274 /// Return the total number of samples collected at the head of the
276 uint64_t getHeadSamples() const { return TotalHeadSamples; }
278 /// Return all the samples collected in the body of the function.
279 const BodySampleMap &getBodySamples() const { return BodySamples; }
281 /// Return all the callsite samples collected in the body of the function.
282 const CallsiteSampleMap &getCallsiteSamples() const {
283 return CallsiteSamples;
286 /// Merge the samples in \p Other into this one.
287 void merge(const FunctionSamples &Other) {
288 addTotalSamples(Other.getTotalSamples());
289 addHeadSamples(Other.getHeadSamples());
290 for (const auto &I : Other.getBodySamples()) {
291 const LineLocation &Loc = I.first;
292 const SampleRecord &Rec = I.second;
293 BodySamples[Loc].merge(Rec);
295 for (const auto &I : Other.getCallsiteSamples()) {
296 const CallsiteLocation &Loc = I.first;
297 const FunctionSamples &Rec = I.second;
298 functionSamplesAt(Loc).merge(Rec);
303 /// Total number of samples collected inside this function.
305 /// Samples are cumulative, they include all the samples collected
306 /// inside this function and all its inlined callees.
307 uint64_t TotalSamples;
309 /// Total number of samples collected at the head of the function.
310 /// This is an approximation of the number of calls made to this function
312 uint64_t TotalHeadSamples;
314 /// Map instruction locations to collected samples.
316 /// Each entry in this map contains the number of samples
317 /// collected at the corresponding line offset. All line locations
318 /// are an offset from the start of the function.
319 BodySampleMap BodySamples;
321 /// Map call sites to collected samples for the called function.
323 /// Each entry in this map corresponds to all the samples
324 /// collected for the inlined function call at the given
325 /// location. For example, given:
333 /// If the bar() and baz() calls were inlined inside foo(), this
334 /// map will contain two entries. One for all the samples collected
335 /// in the call to bar() at line offset 1, the other for all the samples
336 /// collected in the call to baz() at line offset 8.
337 CallsiteSampleMap CallsiteSamples;
340 raw_ostream &operator<<(raw_ostream &OS, const FunctionSamples &FS);
342 /// Sort a LocationT->SampleT map by LocationT.
344 /// It produces a sorted list of <LocationT, SampleT> records by ascending
345 /// order of LocationT.
346 template <class LocationT, class SampleT> class SampleSorter {
348 typedef detail::DenseMapPair<LocationT, SampleT> SamplesWithLoc;
349 typedef SmallVector<const SamplesWithLoc *, 20> SamplesWithLocList;
351 SampleSorter(const DenseMap<LocationT, SampleT> &Samples) {
352 for (const auto &I : Samples)
354 std::stable_sort(V.begin(), V.end(),
355 [](const SamplesWithLoc *A, const SamplesWithLoc *B) {
356 return A->first < B->first;
359 const SamplesWithLocList &get() const { return V; }
362 SamplesWithLocList V;
365 } // end namespace sampleprof
367 } // end namespace llvm
369 #endif // LLVM_PROFILEDATA_SAMPLEPROF_H_