ProfileSummary.h 4.5 KB

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