SampleProfileLoaderBaseUtil.cpp 7.5 KB

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