ProfileSummaryBuilder.cpp 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240
  1. //=-- ProfilesummaryBuilder.cpp - Profile summary computation ---------------=//
  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 contains support for computing profile summary data.
  10. //
  11. //===----------------------------------------------------------------------===//
  12. #include "llvm/IR/Attributes.h"
  13. #include "llvm/IR/Function.h"
  14. #include "llvm/IR/Metadata.h"
  15. #include "llvm/IR/Type.h"
  16. #include "llvm/ProfileData/InstrProf.h"
  17. #include "llvm/ProfileData/ProfileCommon.h"
  18. #include "llvm/ProfileData/SampleProf.h"
  19. #include "llvm/Support/Casting.h"
  20. #include "llvm/Support/CommandLine.h"
  21. using namespace llvm;
  22. cl::opt<bool> UseContextLessSummary(
  23. "profile-summary-contextless", cl::Hidden, cl::init(false), cl::ZeroOrMore,
  24. cl::desc("Merge context profiles before calculating thresholds."));
  25. // The following two parameters determine the threshold for a count to be
  26. // considered hot/cold. These two parameters are percentile values (multiplied
  27. // by 10000). If the counts are sorted in descending order, the minimum count to
  28. // reach ProfileSummaryCutoffHot gives the threshold to determine a hot count.
  29. // Similarly, the minimum count to reach ProfileSummaryCutoffCold gives the
  30. // threshold for determining cold count (everything <= this threshold is
  31. // considered cold).
  32. cl::opt<int> ProfileSummaryCutoffHot(
  33. "profile-summary-cutoff-hot", cl::Hidden, cl::init(990000), cl::ZeroOrMore,
  34. cl::desc("A count is hot if it exceeds the minimum count to"
  35. " reach this percentile of total counts."));
  36. cl::opt<int> ProfileSummaryCutoffCold(
  37. "profile-summary-cutoff-cold", cl::Hidden, cl::init(999999), cl::ZeroOrMore,
  38. cl::desc("A count is cold if it is below the minimum count"
  39. " to reach this percentile of total counts."));
  40. cl::opt<unsigned> ProfileSummaryHugeWorkingSetSizeThreshold(
  41. "profile-summary-huge-working-set-size-threshold", cl::Hidden,
  42. cl::init(15000), cl::ZeroOrMore,
  43. cl::desc("The code working set size is considered huge if the number of"
  44. " blocks required to reach the -profile-summary-cutoff-hot"
  45. " percentile exceeds this count."));
  46. cl::opt<unsigned> ProfileSummaryLargeWorkingSetSizeThreshold(
  47. "profile-summary-large-working-set-size-threshold", cl::Hidden,
  48. cl::init(12500), cl::ZeroOrMore,
  49. cl::desc("The code working set size is considered large if the number of"
  50. " blocks required to reach the -profile-summary-cutoff-hot"
  51. " percentile exceeds this count."));
  52. // The next two options override the counts derived from summary computation and
  53. // are useful for debugging purposes.
  54. cl::opt<int> ProfileSummaryHotCount(
  55. "profile-summary-hot-count", cl::ReallyHidden, cl::ZeroOrMore,
  56. cl::desc("A fixed hot count that overrides the count derived from"
  57. " profile-summary-cutoff-hot"));
  58. cl::opt<int> ProfileSummaryColdCount(
  59. "profile-summary-cold-count", cl::ReallyHidden, cl::ZeroOrMore,
  60. cl::desc("A fixed cold count that overrides the count derived from"
  61. " profile-summary-cutoff-cold"));
  62. // A set of cutoff values. Each value, when divided by ProfileSummary::Scale
  63. // (which is 1000000) is a desired percentile of total counts.
  64. static const uint32_t DefaultCutoffsData[] = {
  65. 10000, /* 1% */
  66. 100000, /* 10% */
  67. 200000, 300000, 400000, 500000, 600000, 700000, 800000,
  68. 900000, 950000, 990000, 999000, 999900, 999990, 999999};
  69. const ArrayRef<uint32_t> ProfileSummaryBuilder::DefaultCutoffs =
  70. DefaultCutoffsData;
  71. const ProfileSummaryEntry &
  72. ProfileSummaryBuilder::getEntryForPercentile(const SummaryEntryVector &DS,
  73. uint64_t Percentile) {
  74. auto It = partition_point(DS, [=](const ProfileSummaryEntry &Entry) {
  75. return Entry.Cutoff < Percentile;
  76. });
  77. // The required percentile has to be <= one of the percentiles in the
  78. // detailed summary.
  79. if (It == DS.end())
  80. report_fatal_error("Desired percentile exceeds the maximum cutoff");
  81. return *It;
  82. }
  83. void InstrProfSummaryBuilder::addRecord(const InstrProfRecord &R) {
  84. // The first counter is not necessarily an entry count for IR
  85. // instrumentation profiles.
  86. // Eventually MaxFunctionCount will become obsolete and this can be
  87. // removed.
  88. addEntryCount(R.Counts[0]);
  89. for (size_t I = 1, E = R.Counts.size(); I < E; ++I)
  90. addInternalCount(R.Counts[I]);
  91. }
  92. // To compute the detailed summary, we consider each line containing samples as
  93. // equivalent to a block with a count in the instrumented profile.
  94. void SampleProfileSummaryBuilder::addRecord(
  95. const sampleprof::FunctionSamples &FS, bool isCallsiteSample) {
  96. if (!isCallsiteSample) {
  97. NumFunctions++;
  98. if (FS.getHeadSamples() > MaxFunctionCount)
  99. MaxFunctionCount = FS.getHeadSamples();
  100. }
  101. for (const auto &I : FS.getBodySamples()) {
  102. uint64_t Count = I.second.getSamples();
  103. addCount(Count);
  104. }
  105. for (const auto &I : FS.getCallsiteSamples())
  106. for (const auto &CS : I.second)
  107. addRecord(CS.second, true);
  108. }
  109. // The argument to this method is a vector of cutoff percentages and the return
  110. // value is a vector of (Cutoff, MinCount, NumCounts) triplets.
  111. void ProfileSummaryBuilder::computeDetailedSummary() {
  112. if (DetailedSummaryCutoffs.empty())
  113. return;
  114. llvm::sort(DetailedSummaryCutoffs);
  115. auto Iter = CountFrequencies.begin();
  116. const auto End = CountFrequencies.end();
  117. uint32_t CountsSeen = 0;
  118. uint64_t CurrSum = 0, Count = 0;
  119. for (const uint32_t Cutoff : DetailedSummaryCutoffs) {
  120. assert(Cutoff <= 999999);
  121. APInt Temp(128, TotalCount);
  122. APInt N(128, Cutoff);
  123. APInt D(128, ProfileSummary::Scale);
  124. Temp *= N;
  125. Temp = Temp.sdiv(D);
  126. uint64_t DesiredCount = Temp.getZExtValue();
  127. assert(DesiredCount <= TotalCount);
  128. while (CurrSum < DesiredCount && Iter != End) {
  129. Count = Iter->first;
  130. uint32_t Freq = Iter->second;
  131. CurrSum += (Count * Freq);
  132. CountsSeen += Freq;
  133. Iter++;
  134. }
  135. assert(CurrSum >= DesiredCount);
  136. ProfileSummaryEntry PSE = {Cutoff, Count, CountsSeen};
  137. DetailedSummary.push_back(PSE);
  138. }
  139. }
  140. uint64_t
  141. ProfileSummaryBuilder::getHotCountThreshold(const SummaryEntryVector &DS) {
  142. auto &HotEntry =
  143. ProfileSummaryBuilder::getEntryForPercentile(DS, ProfileSummaryCutoffHot);
  144. uint64_t HotCountThreshold = HotEntry.MinCount;
  145. if (ProfileSummaryHotCount.getNumOccurrences() > 0)
  146. HotCountThreshold = ProfileSummaryHotCount;
  147. return HotCountThreshold;
  148. }
  149. uint64_t
  150. ProfileSummaryBuilder::getColdCountThreshold(const SummaryEntryVector &DS) {
  151. auto &ColdEntry = ProfileSummaryBuilder::getEntryForPercentile(
  152. DS, ProfileSummaryCutoffCold);
  153. uint64_t ColdCountThreshold = ColdEntry.MinCount;
  154. if (ProfileSummaryColdCount.getNumOccurrences() > 0)
  155. ColdCountThreshold = ProfileSummaryColdCount;
  156. return ColdCountThreshold;
  157. }
  158. std::unique_ptr<ProfileSummary> SampleProfileSummaryBuilder::getSummary() {
  159. computeDetailedSummary();
  160. return std::make_unique<ProfileSummary>(
  161. ProfileSummary::PSK_Sample, DetailedSummary, TotalCount, MaxCount, 0,
  162. MaxFunctionCount, NumCounts, NumFunctions);
  163. }
  164. std::unique_ptr<ProfileSummary>
  165. SampleProfileSummaryBuilder::computeSummaryForProfiles(
  166. const SampleProfileMap &Profiles) {
  167. assert(NumFunctions == 0 &&
  168. "This can only be called on an empty summary builder");
  169. sampleprof::SampleProfileMap ContextLessProfiles;
  170. const sampleprof::SampleProfileMap *ProfilesToUse = &Profiles;
  171. // For CSSPGO, context-sensitive profile effectively split a function profile
  172. // into many copies each representing the CFG profile of a particular calling
  173. // context. That makes the count distribution looks more flat as we now have
  174. // more function profiles each with lower counts, which in turn leads to lower
  175. // hot thresholds. To compensate for that, by default we merge context
  176. // profiles before computing profile summary.
  177. if (UseContextLessSummary || (sampleprof::FunctionSamples::ProfileIsCSFlat &&
  178. !UseContextLessSummary.getNumOccurrences())) {
  179. for (const auto &I : Profiles) {
  180. ContextLessProfiles[I.second.getName()].merge(I.second);
  181. }
  182. ProfilesToUse = &ContextLessProfiles;
  183. }
  184. for (const auto &I : *ProfilesToUse) {
  185. const sampleprof::FunctionSamples &Profile = I.second;
  186. addRecord(Profile);
  187. }
  188. return getSummary();
  189. }
  190. std::unique_ptr<ProfileSummary> InstrProfSummaryBuilder::getSummary() {
  191. computeDetailedSummary();
  192. return std::make_unique<ProfileSummary>(
  193. ProfileSummary::PSK_Instr, DetailedSummary, TotalCount, MaxCount,
  194. MaxInternalBlockCount, MaxFunctionCount, NumCounts, NumFunctions);
  195. }
  196. void InstrProfSummaryBuilder::addEntryCount(uint64_t Count) {
  197. NumFunctions++;
  198. // Skip invalid count.
  199. if (Count == (uint64_t)-1)
  200. return;
  201. addCount(Count);
  202. if (Count > MaxFunctionCount)
  203. MaxFunctionCount = Count;
  204. }
  205. void InstrProfSummaryBuilder::addInternalCount(uint64_t Count) {
  206. // Skip invalid count.
  207. if (Count == (uint64_t)-1)
  208. return;
  209. addCount(Count);
  210. if (Count > MaxInternalBlockCount)
  211. MaxInternalBlockCount = Count;
  212. }