ProfileSummary.h 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. //===- ProfileSummary.h - Profile summary data structure. -------*- C++ -*-===//
  2. //
  3. // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
  4. // See https://llvm.org/LICENSE.txt for license information.
  5. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  6. //
  7. //===----------------------------------------------------------------------===//
  8. //
  9. // This file defines the profile summary data structure.
  10. //
  11. //===----------------------------------------------------------------------===//
  12. #ifndef LLVM_IR_PROFILESUMMARY_H
  13. #define LLVM_IR_PROFILESUMMARY_H
  14. #include <algorithm>
  15. #include <cassert>
  16. #include <cstdint>
  17. #include <vector>
  18. namespace llvm {
  19. class LLVMContext;
  20. class Metadata;
  21. class raw_ostream;
  22. // The profile summary is one or more (Cutoff, MinCount, NumCounts) triplets.
  23. // The semantics of counts depend on the type of profile. For instrumentation
  24. // profile, counts are block counts and for sample profile, counts are
  25. // per-line samples. Given a target counts percentile, we compute the minimum
  26. // number of counts needed to reach this target and the minimum among these
  27. // counts.
  28. struct ProfileSummaryEntry {
  29. uint32_t Cutoff; ///< The required percentile of counts.
  30. uint64_t MinCount; ///< The minimum count for this percentile.
  31. uint64_t NumCounts; ///< Number of counts >= the minimum count.
  32. ProfileSummaryEntry(uint32_t TheCutoff, uint64_t TheMinCount,
  33. uint64_t TheNumCounts)
  34. : Cutoff(TheCutoff), MinCount(TheMinCount), NumCounts(TheNumCounts) {}
  35. };
  36. using SummaryEntryVector = std::vector<ProfileSummaryEntry>;
  37. class ProfileSummary {
  38. public:
  39. enum Kind { PSK_Instr, PSK_CSInstr, PSK_Sample };
  40. private:
  41. const Kind PSK;
  42. SummaryEntryVector DetailedSummary;
  43. uint64_t TotalCount, MaxCount, MaxInternalCount, MaxFunctionCount;
  44. uint32_t NumCounts, NumFunctions;
  45. /// If 'Partial' is false, it means the profile being used to optimize
  46. /// a target is collected from the same target.
  47. /// If 'Partial' is true, it means the profile is for common/shared
  48. /// code. The common profile is usually merged from profiles collected
  49. /// from running other targets.
  50. bool Partial = false;
  51. /// This approximately represents the ratio of the number of profile counters
  52. /// of the program being built to the number of profile counters in the
  53. /// partial sample profile. When 'Partial' is false, it is undefined. This is
  54. /// currently only available under thin LTO mode.
  55. double PartialProfileRatio = 0;
  56. /// Return detailed summary as metadata.
  57. Metadata *getDetailedSummaryMD(LLVMContext &Context);
  58. public:
  59. static const int Scale = 1000000;
  60. ProfileSummary(Kind K, SummaryEntryVector DetailedSummary,
  61. uint64_t TotalCount, uint64_t MaxCount,
  62. uint64_t MaxInternalCount, uint64_t MaxFunctionCount,
  63. uint32_t NumCounts, uint32_t NumFunctions,
  64. bool Partial = false, double PartialProfileRatio = 0)
  65. : PSK(K), DetailedSummary(std::move(DetailedSummary)),
  66. TotalCount(TotalCount), MaxCount(MaxCount),
  67. MaxInternalCount(MaxInternalCount), MaxFunctionCount(MaxFunctionCount),
  68. NumCounts(NumCounts), NumFunctions(NumFunctions), Partial(Partial),
  69. PartialProfileRatio(PartialProfileRatio) {}
  70. Kind getKind() const { return PSK; }
  71. /// Return summary information as metadata.
  72. Metadata *getMD(LLVMContext &Context, bool AddPartialField = true,
  73. bool AddPartialProfileRatioField = true);
  74. /// Construct profile summary from metdata.
  75. static ProfileSummary *getFromMD(Metadata *MD);
  76. SummaryEntryVector &getDetailedSummary() { return DetailedSummary; }
  77. uint32_t getNumFunctions() { return NumFunctions; }
  78. uint64_t getMaxFunctionCount() { return MaxFunctionCount; }
  79. uint32_t getNumCounts() { return NumCounts; }
  80. uint64_t getTotalCount() { return TotalCount; }
  81. uint64_t getMaxCount() { return MaxCount; }
  82. uint64_t getMaxInternalCount() { return MaxInternalCount; }
  83. void setPartialProfile(bool PP) { Partial = PP; }
  84. bool isPartialProfile() { return Partial; }
  85. double getPartialProfileRatio() { return PartialProfileRatio; }
  86. void setPartialProfileRatio(double R) {
  87. assert(isPartialProfile() && "Unexpected when not partial profile");
  88. PartialProfileRatio = R;
  89. }
  90. void printSummary(raw_ostream &OS);
  91. void printDetailedSummary(raw_ostream &OS);
  92. };
  93. } // end namespace llvm
  94. #endif // LLVM_IR_PROFILESUMMARY_H