SampleProfReader.h 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. //===- SampleProfReader.h - Read LLVM sample profile data -----------------===//
  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 definitions needed for reading sample profiles.
  11. //
  12. //===----------------------------------------------------------------------===//
  13. #ifndef LLVM_PROFILEDATA_SAMPLEPROFREADER_H
  14. #define LLVM_PROFILEDATA_SAMPLEPROFREADER_H
  15. #include "llvm/ADT/DenseMap.h"
  16. #include "llvm/ADT/StringMap.h"
  17. #include "llvm/ADT/StringRef.h"
  18. #include "llvm/ADT/Twine.h"
  19. #include "llvm/IR/DiagnosticInfo.h"
  20. #include "llvm/IR/Function.h"
  21. #include "llvm/IR/LLVMContext.h"
  22. #include "llvm/ProfileData/SampleProf.h"
  23. #include "llvm/Support/Debug.h"
  24. #include "llvm/Support/ErrorHandling.h"
  25. #include "llvm/Support/ErrorOr.h"
  26. #include "llvm/Support/MemoryBuffer.h"
  27. #include "llvm/Support/raw_ostream.h"
  28. namespace llvm {
  29. namespace sampleprof {
  30. /// \brief Sample-based profile reader.
  31. ///
  32. /// Each profile contains sample counts for all the functions
  33. /// executed. Inside each function, statements are annotated with the
  34. /// collected samples on all the instructions associated with that
  35. /// statement.
  36. ///
  37. /// For this to produce meaningful data, the program needs to be
  38. /// compiled with some debug information (at minimum, line numbers:
  39. /// -gline-tables-only). Otherwise, it will be impossible to match IR
  40. /// instructions to the line numbers collected by the profiler.
  41. ///
  42. /// From the profile file, we are interested in collecting the
  43. /// following information:
  44. ///
  45. /// * A list of functions included in the profile (mangled names).
  46. ///
  47. /// * For each function F:
  48. /// 1. The total number of samples collected in F.
  49. ///
  50. /// 2. The samples collected at each line in F. To provide some
  51. /// protection against source code shuffling, line numbers should
  52. /// be relative to the start of the function.
  53. ///
  54. /// The reader supports two file formats: text and binary. The text format
  55. /// is useful for debugging and testing, while the binary format is more
  56. /// compact. They can both be used interchangeably.
  57. class SampleProfileReader {
  58. public:
  59. SampleProfileReader(std::unique_ptr<MemoryBuffer> B, LLVMContext &C)
  60. : Profiles(0), Ctx(C), Buffer(std::move(B)) {}
  61. virtual ~SampleProfileReader() {}
  62. /// \brief Read and validate the file header.
  63. virtual std::error_code readHeader() = 0;
  64. /// \brief Read sample profiles from the associated file.
  65. virtual std::error_code read() = 0;
  66. /// \brief Print the profile for \p FName on stream \p OS.
  67. void dumpFunctionProfile(StringRef FName, raw_ostream &OS = dbgs());
  68. /// \brief Print all the profiles on stream \p OS.
  69. void dump(raw_ostream &OS = dbgs());
  70. /// \brief Return the samples collected for function \p F.
  71. FunctionSamples *getSamplesFor(const Function &F) {
  72. return &Profiles[F.getName()];
  73. }
  74. /// \brief Return all the profiles.
  75. StringMap<FunctionSamples> &getProfiles() { return Profiles; }
  76. /// \brief Report a parse error message.
  77. void reportParseError(int64_t LineNumber, Twine Msg) const {
  78. Ctx.diagnose(DiagnosticInfoSampleProfile(Buffer->getBufferIdentifier(),
  79. LineNumber, Msg));
  80. }
  81. /// \brief Create a sample profile reader appropriate to the file format.
  82. static ErrorOr<std::unique_ptr<SampleProfileReader>>
  83. create(StringRef Filename, LLVMContext &C);
  84. protected:
  85. /// \brief Map every function to its associated profile.
  86. ///
  87. /// The profile of every function executed at runtime is collected
  88. /// in the structure FunctionSamples. This maps function objects
  89. /// to their corresponding profiles.
  90. StringMap<FunctionSamples> Profiles;
  91. /// \brief LLVM context used to emit diagnostics.
  92. LLVMContext &Ctx;
  93. /// \brief Memory buffer holding the profile file.
  94. std::unique_ptr<MemoryBuffer> Buffer;
  95. };
  96. class SampleProfileReaderText : public SampleProfileReader {
  97. public:
  98. SampleProfileReaderText(std::unique_ptr<MemoryBuffer> B, LLVMContext &C)
  99. : SampleProfileReader(std::move(B), C) {}
  100. /// \brief Read and validate the file header.
  101. std::error_code readHeader() override { return sampleprof_error::success; }
  102. /// \brief Read sample profiles from the associated file.
  103. std::error_code read() override;
  104. };
  105. class SampleProfileReaderBinary : public SampleProfileReader {
  106. public:
  107. SampleProfileReaderBinary(std::unique_ptr<MemoryBuffer> B, LLVMContext &C)
  108. : SampleProfileReader(std::move(B), C), Data(nullptr), End(nullptr) {}
  109. /// \brief Read and validate the file header.
  110. std::error_code readHeader() override;
  111. /// \brief Read sample profiles from the associated file.
  112. std::error_code read() override;
  113. /// \brief Return true if \p Buffer is in the format supported by this class.
  114. static bool hasFormat(const MemoryBuffer &Buffer);
  115. protected:
  116. /// \brief Read a numeric value of type T from the profile.
  117. ///
  118. /// If an error occurs during decoding, a diagnostic message is emitted and
  119. /// EC is set.
  120. ///
  121. /// \returns the read value.
  122. template <typename T> ErrorOr<T> readNumber();
  123. /// \brief Read a string from the profile.
  124. ///
  125. /// If an error occurs during decoding, a diagnostic message is emitted and
  126. /// EC is set.
  127. ///
  128. /// \returns the read value.
  129. ErrorOr<StringRef> readString();
  130. /// \brief Return true if we've reached the end of file.
  131. bool at_eof() const { return Data >= End; }
  132. /// \brief Points to the current location in the buffer.
  133. const uint8_t *Data;
  134. /// \brief Points to the end of the buffer.
  135. const uint8_t *End;
  136. };
  137. } // End namespace sampleprof
  138. } // End namespace llvm
  139. #endif // LLVM_PROFILEDATA_SAMPLEPROFREADER_H