SampleProfileLoaderBaseUtil.cpp 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  1. //===- SampleProfileLoaderBaseUtil.cpp - Profile loader Util func ---------===//
  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 implements the SampleProfileLoader base utility functions.
  10. //
  11. //===----------------------------------------------------------------------===//
  12. #include "llvm/Transforms/Utils/SampleProfileLoaderBaseUtil.h"
  13. namespace llvm {
  14. cl::opt<unsigned> SampleProfileMaxPropagateIterations(
  15. "sample-profile-max-propagate-iterations", cl::init(100),
  16. cl::desc("Maximum number of iterations to go through when propagating "
  17. "sample block/edge weights through the CFG."));
  18. cl::opt<unsigned> SampleProfileRecordCoverage(
  19. "sample-profile-check-record-coverage", cl::init(0), cl::value_desc("N"),
  20. cl::desc("Emit a warning if less than N% of records in the input profile "
  21. "are matched to the IR."));
  22. cl::opt<unsigned> SampleProfileSampleCoverage(
  23. "sample-profile-check-sample-coverage", cl::init(0), cl::value_desc("N"),
  24. cl::desc("Emit a warning if less than N% of samples in the input profile "
  25. "are matched to the IR."));
  26. cl::opt<bool> NoWarnSampleUnused(
  27. "no-warn-sample-unused", cl::init(false), cl::Hidden,
  28. cl::desc("Use this option to turn off/on warnings about function with "
  29. "samples but without debug information to use those samples. "));
  30. cl::opt<bool> SampleProfileUseProfi(
  31. "sample-profile-use-profi", cl::init(false), cl::Hidden, cl::ZeroOrMore,
  32. cl::desc("Use profi to infer block and edge counts."));
  33. namespace sampleprofutil {
  34. /// Return true if the given callsite is hot wrt to hot cutoff threshold.
  35. ///
  36. /// Functions that were inlined in the original binary will be represented
  37. /// in the inline stack in the sample profile. If the profile shows that
  38. /// the original inline decision was "good" (i.e., the callsite is executed
  39. /// frequently), then we will recreate the inline decision and apply the
  40. /// profile from the inlined callsite.
  41. ///
  42. /// To decide whether an inlined callsite is hot, we compare the callsite
  43. /// sample count with the hot cutoff computed by ProfileSummaryInfo, it is
  44. /// regarded as hot if the count is above the cutoff value.
  45. ///
  46. /// When ProfileAccurateForSymsInList is enabled and profile symbol list
  47. /// is present, functions in the profile symbol list but without profile will
  48. /// be regarded as cold and much less inlining will happen in CGSCC inlining
  49. /// pass, so we tend to lower the hot criteria here to allow more early
  50. /// inlining to happen for warm callsites and it is helpful for performance.
  51. bool callsiteIsHot(const FunctionSamples *CallsiteFS, ProfileSummaryInfo *PSI,
  52. bool ProfAccForSymsInList) {
  53. if (!CallsiteFS)
  54. return false; // The callsite was not inlined in the original binary.
  55. assert(PSI && "PSI is expected to be non null");
  56. uint64_t CallsiteTotalSamples = CallsiteFS->getTotalSamples();
  57. if (ProfAccForSymsInList)
  58. return !PSI->isColdCount(CallsiteTotalSamples);
  59. else
  60. return PSI->isHotCount(CallsiteTotalSamples);
  61. }
  62. /// Mark as used the sample record for the given function samples at
  63. /// (LineOffset, Discriminator).
  64. ///
  65. /// \returns true if this is the first time we mark the given record.
  66. bool SampleCoverageTracker::markSamplesUsed(const FunctionSamples *FS,
  67. uint32_t LineOffset,
  68. uint32_t Discriminator,
  69. uint64_t Samples) {
  70. LineLocation Loc(LineOffset, Discriminator);
  71. unsigned &Count = SampleCoverage[FS][Loc];
  72. bool FirstTime = (++Count == 1);
  73. if (FirstTime)
  74. TotalUsedSamples += Samples;
  75. return FirstTime;
  76. }
  77. /// Return the number of sample records that were applied from this profile.
  78. ///
  79. /// This count does not include records from cold inlined callsites.
  80. unsigned
  81. SampleCoverageTracker::countUsedRecords(const FunctionSamples *FS,
  82. ProfileSummaryInfo *PSI) const {
  83. auto I = SampleCoverage.find(FS);
  84. // The size of the coverage map for FS represents the number of records
  85. // that were marked used at least once.
  86. unsigned Count = (I != SampleCoverage.end()) ? I->second.size() : 0;
  87. // If there are inlined callsites in this function, count the samples found
  88. // in the respective bodies. However, do not bother counting callees with 0
  89. // total samples, these are callees that were never invoked at runtime.
  90. for (const auto &I : FS->getCallsiteSamples())
  91. for (const auto &J : I.second) {
  92. const FunctionSamples *CalleeSamples = &J.second;
  93. if (callsiteIsHot(CalleeSamples, PSI, ProfAccForSymsInList))
  94. Count += countUsedRecords(CalleeSamples, PSI);
  95. }
  96. return Count;
  97. }
  98. /// Return the number of sample records in the body of this profile.
  99. ///
  100. /// This count does not include records from cold inlined callsites.
  101. unsigned
  102. SampleCoverageTracker::countBodyRecords(const FunctionSamples *FS,
  103. ProfileSummaryInfo *PSI) const {
  104. unsigned Count = FS->getBodySamples().size();
  105. // Only count records in hot callsites.
  106. for (const auto &I : FS->getCallsiteSamples())
  107. for (const auto &J : I.second) {
  108. const FunctionSamples *CalleeSamples = &J.second;
  109. if (callsiteIsHot(CalleeSamples, PSI, ProfAccForSymsInList))
  110. Count += countBodyRecords(CalleeSamples, PSI);
  111. }
  112. return Count;
  113. }
  114. /// Return the number of samples collected in the body of this profile.
  115. ///
  116. /// This count does not include samples from cold inlined callsites.
  117. uint64_t
  118. SampleCoverageTracker::countBodySamples(const FunctionSamples *FS,
  119. ProfileSummaryInfo *PSI) const {
  120. uint64_t Total = 0;
  121. for (const auto &I : FS->getBodySamples())
  122. Total += I.second.getSamples();
  123. // Only count samples in hot callsites.
  124. for (const auto &I : FS->getCallsiteSamples())
  125. for (const auto &J : I.second) {
  126. const FunctionSamples *CalleeSamples = &J.second;
  127. if (callsiteIsHot(CalleeSamples, PSI, ProfAccForSymsInList))
  128. Total += countBodySamples(CalleeSamples, PSI);
  129. }
  130. return Total;
  131. }
  132. /// Return the fraction of sample records used in this profile.
  133. ///
  134. /// The returned value is an unsigned integer in the range 0-100 indicating
  135. /// the percentage of sample records that were used while applying this
  136. /// profile to the associated function.
  137. unsigned SampleCoverageTracker::computeCoverage(unsigned Used,
  138. unsigned Total) const {
  139. assert(Used <= Total &&
  140. "number of used records cannot exceed the total number of records");
  141. return Total > 0 ? Used * 100 / Total : 100;
  142. }
  143. /// Create a global variable to flag FSDiscriminators are used.
  144. void createFSDiscriminatorVariable(Module *M) {
  145. const char *FSDiscriminatorVar = "__llvm_fs_discriminator__";
  146. if (M->getGlobalVariable(FSDiscriminatorVar))
  147. return;
  148. auto &Context = M->getContext();
  149. // Place this variable to llvm.used so it won't be GC'ed.
  150. appendToUsed(*M, {new GlobalVariable(*M, Type::getInt1Ty(Context), true,
  151. GlobalValue::WeakODRLinkage,
  152. ConstantInt::getTrue(Context),
  153. FSDiscriminatorVar)});
  154. }
  155. } // end of namespace sampleprofutil
  156. } // end of namespace llvm