SampleProf.h 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249
  1. //=-- SampleProf.h - Sampling profiling format support --------------------===//
  2. //
  3. // The LLVM Compiler Infrastructure
  4. //
  5. // This file is distributed under the University of Illinois Open Source
  6. // License. See LICENSE.TXT for details.
  7. //
  8. //===----------------------------------------------------------------------===//
  9. //
  10. // This file contains common definitions used in the reading and writing of
  11. // sample profile data.
  12. //
  13. //===----------------------------------------------------------------------===//
  14. #ifndef LLVM_PROFILEDATA_SAMPLEPROF_H_
  15. #define LLVM_PROFILEDATA_SAMPLEPROF_H_
  16. #include "llvm/ADT/DenseMap.h"
  17. #include "llvm/ADT/SmallVector.h"
  18. #include "llvm/ADT/StringMap.h"
  19. #include "llvm/Support/Debug.h"
  20. #include "llvm/Support/raw_ostream.h"
  21. #include <system_error>
  22. namespace llvm {
  23. const std::error_category &sampleprof_category();
  24. enum class sampleprof_error {
  25. success = 0,
  26. bad_magic,
  27. unsupported_version,
  28. too_large,
  29. truncated,
  30. malformed,
  31. unrecognized_format
  32. };
  33. inline std::error_code make_error_code(sampleprof_error E) {
  34. return std::error_code(static_cast<int>(E), sampleprof_category());
  35. }
  36. } // end namespace llvm
  37. namespace std {
  38. template <>
  39. struct is_error_code_enum<llvm::sampleprof_error> : std::true_type {};
  40. }
  41. namespace llvm {
  42. namespace sampleprof {
  43. static inline uint64_t SPMagic() {
  44. return uint64_t('S') << (64 - 8) | uint64_t('P') << (64 - 16) |
  45. uint64_t('R') << (64 - 24) | uint64_t('O') << (64 - 32) |
  46. uint64_t('F') << (64 - 40) | uint64_t('4') << (64 - 48) |
  47. uint64_t('2') << (64 - 56) | uint64_t(0xff);
  48. }
  49. static inline uint64_t SPVersion() { return 100; }
  50. /// Represents the relative location of an instruction.
  51. ///
  52. /// Instruction locations are specified by the line offset from the
  53. /// beginning of the function (marked by the line where the function
  54. /// header is) and the discriminator value within that line.
  55. ///
  56. /// The discriminator value is useful to distinguish instructions
  57. /// that are on the same line but belong to different basic blocks
  58. /// (e.g., the two post-increment instructions in "if (p) x++; else y++;").
  59. struct LineLocation {
  60. LineLocation(int L, unsigned D) : LineOffset(L), Discriminator(D) {}
  61. int LineOffset;
  62. unsigned Discriminator;
  63. };
  64. } // End namespace sampleprof
  65. template <> struct DenseMapInfo<sampleprof::LineLocation> {
  66. typedef DenseMapInfo<int> OffsetInfo;
  67. typedef DenseMapInfo<unsigned> DiscriminatorInfo;
  68. static inline sampleprof::LineLocation getEmptyKey() {
  69. return sampleprof::LineLocation(OffsetInfo::getEmptyKey(),
  70. DiscriminatorInfo::getEmptyKey());
  71. }
  72. static inline sampleprof::LineLocation getTombstoneKey() {
  73. return sampleprof::LineLocation(OffsetInfo::getTombstoneKey(),
  74. DiscriminatorInfo::getTombstoneKey());
  75. }
  76. static inline unsigned getHashValue(sampleprof::LineLocation Val) {
  77. return DenseMapInfo<std::pair<int, unsigned>>::getHashValue(
  78. std::pair<int, unsigned>(Val.LineOffset, Val.Discriminator));
  79. }
  80. static inline bool isEqual(sampleprof::LineLocation LHS,
  81. sampleprof::LineLocation RHS) {
  82. return LHS.LineOffset == RHS.LineOffset &&
  83. LHS.Discriminator == RHS.Discriminator;
  84. }
  85. };
  86. namespace sampleprof {
  87. /// Representation of a single sample record.
  88. ///
  89. /// A sample record is represented by a positive integer value, which
  90. /// indicates how frequently was the associated line location executed.
  91. ///
  92. /// Additionally, if the associated location contains a function call,
  93. /// the record will hold a list of all the possible called targets. For
  94. /// direct calls, this will be the exact function being invoked. For
  95. /// indirect calls (function pointers, virtual table dispatch), this
  96. /// will be a list of one or more functions.
  97. class SampleRecord {
  98. public:
  99. typedef StringMap<unsigned> CallTargetMap;
  100. SampleRecord() : NumSamples(0), CallTargets() {}
  101. /// Increment the number of samples for this record by \p S.
  102. ///
  103. /// Sample counts accumulate using saturating arithmetic, to avoid wrapping
  104. /// around unsigned integers.
  105. void addSamples(unsigned S) {
  106. if (NumSamples <= std::numeric_limits<unsigned>::max() - S)
  107. NumSamples += S;
  108. else
  109. NumSamples = std::numeric_limits<unsigned>::max();
  110. }
  111. /// Add called function \p F with samples \p S.
  112. ///
  113. /// Sample counts accumulate using saturating arithmetic, to avoid wrapping
  114. /// around unsigned integers.
  115. void addCalledTarget(StringRef F, unsigned S) {
  116. unsigned &TargetSamples = CallTargets[F];
  117. if (TargetSamples <= std::numeric_limits<unsigned>::max() - S)
  118. TargetSamples += S;
  119. else
  120. TargetSamples = std::numeric_limits<unsigned>::max();
  121. }
  122. /// Return true if this sample record contains function calls.
  123. bool hasCalls() const { return CallTargets.size() > 0; }
  124. unsigned getSamples() const { return NumSamples; }
  125. const CallTargetMap &getCallTargets() const { return CallTargets; }
  126. /// Merge the samples in \p Other into this record.
  127. void merge(const SampleRecord &Other) {
  128. addSamples(Other.getSamples());
  129. for (const auto &I : Other.getCallTargets())
  130. addCalledTarget(I.first(), I.second);
  131. }
  132. private:
  133. unsigned NumSamples;
  134. CallTargetMap CallTargets;
  135. };
  136. typedef DenseMap<LineLocation, SampleRecord> BodySampleMap;
  137. /// Representation of the samples collected for a function.
  138. ///
  139. /// This data structure contains all the collected samples for the body
  140. /// of a function. Each sample corresponds to a LineLocation instance
  141. /// within the body of the function.
  142. class FunctionSamples {
  143. public:
  144. FunctionSamples() : TotalSamples(0), TotalHeadSamples(0) {}
  145. void print(raw_ostream &OS = dbgs());
  146. void addTotalSamples(unsigned Num) { TotalSamples += Num; }
  147. void addHeadSamples(unsigned Num) { TotalHeadSamples += Num; }
  148. void addBodySamples(int LineOffset, unsigned Discriminator, unsigned Num) {
  149. assert(LineOffset >= 0);
  150. // When dealing with instruction weights, we use the value
  151. // zero to indicate the absence of a sample. If we read an
  152. // actual zero from the profile file, use the value 1 to
  153. // avoid the confusion later on.
  154. if (Num == 0)
  155. Num = 1;
  156. BodySamples[LineLocation(LineOffset, Discriminator)].addSamples(Num);
  157. }
  158. void addCalledTargetSamples(int LineOffset, unsigned Discriminator,
  159. std::string FName, unsigned Num) {
  160. assert(LineOffset >= 0);
  161. BodySamples[LineLocation(LineOffset, Discriminator)].addCalledTarget(FName,
  162. Num);
  163. }
  164. /// Return the sample record at the given location.
  165. /// Each location is specified by \p LineOffset and \p Discriminator.
  166. SampleRecord &sampleRecordAt(const LineLocation &Loc) {
  167. return BodySamples[Loc];
  168. }
  169. /// Return the number of samples collected at the given location.
  170. /// Each location is specified by \p LineOffset and \p Discriminator.
  171. unsigned samplesAt(int LineOffset, unsigned Discriminator) {
  172. return sampleRecordAt(LineLocation(LineOffset, Discriminator)).getSamples();
  173. }
  174. bool empty() const { return BodySamples.empty(); }
  175. /// Return the total number of samples collected inside the function.
  176. unsigned getTotalSamples() const { return TotalSamples; }
  177. /// Return the total number of samples collected at the head of the
  178. /// function.
  179. unsigned getHeadSamples() const { return TotalHeadSamples; }
  180. /// Return all the samples collected in the body of the function.
  181. const BodySampleMap &getBodySamples() const { return BodySamples; }
  182. /// Merge the samples in \p Other into this one.
  183. void merge(const FunctionSamples &Other) {
  184. addTotalSamples(Other.getTotalSamples());
  185. addHeadSamples(Other.getHeadSamples());
  186. for (const auto &I : Other.getBodySamples()) {
  187. const LineLocation &Loc = I.first;
  188. const SampleRecord &Rec = I.second;
  189. sampleRecordAt(Loc).merge(Rec);
  190. }
  191. }
  192. private:
  193. /// Total number of samples collected inside this function.
  194. ///
  195. /// Samples are cumulative, they include all the samples collected
  196. /// inside this function and all its inlined callees.
  197. unsigned TotalSamples;
  198. /// Total number of samples collected at the head of the function.
  199. /// This is an approximation of the number of calls made to this function
  200. /// at runtime.
  201. unsigned TotalHeadSamples;
  202. /// Map instruction locations to collected samples.
  203. ///
  204. /// Each entry in this map contains the number of samples
  205. /// collected at the corresponding line offset. All line locations
  206. /// are an offset from the start of the function.
  207. BodySampleMap BodySamples;
  208. };
  209. } // End namespace sampleprof
  210. } // End namespace llvm
  211. #endif // LLVM_PROFILEDATA_SAMPLEPROF_H_