MisExpect.cpp 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  1. //===--- MisExpect.cpp - Check the use of llvm.expect with PGO data -------===//
  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 contains code to emit warnings for potentially incorrect usage of the
  10. // llvm.expect intrinsic. This utility extracts the threshold values from
  11. // metadata associated with the instrumented Branch or Switch instruction. The
  12. // threshold values are then used to determine if a warning should be emmited.
  13. //
  14. // MisExpect metadata is generated when llvm.expect intrinsics are lowered see
  15. // LowerExpectIntrinsic.cpp
  16. //
  17. //===----------------------------------------------------------------------===//
  18. #include "llvm/Transforms/Utils/MisExpect.h"
  19. #include "llvm/ADT/Twine.h"
  20. #include "llvm/Analysis/OptimizationRemarkEmitter.h"
  21. #include "llvm/IR/Constants.h"
  22. #include "llvm/IR/DiagnosticInfo.h"
  23. #include "llvm/IR/Instruction.h"
  24. #include "llvm/IR/Instructions.h"
  25. #include "llvm/IR/LLVMContext.h"
  26. #include "llvm/Support/BranchProbability.h"
  27. #include "llvm/Support/Debug.h"
  28. #include "llvm/Support/FormatVariadic.h"
  29. #include <cstdint>
  30. #include <functional>
  31. #include <numeric>
  32. #define DEBUG_TYPE "misexpect"
  33. using namespace llvm;
  34. using namespace misexpect;
  35. namespace llvm {
  36. // Command line option to enable/disable the warning when profile data suggests
  37. // a mismatch with the use of the llvm.expect intrinsic
  38. static cl::opt<bool> PGOWarnMisExpect(
  39. "pgo-warn-misexpect", cl::init(false), cl::Hidden,
  40. cl::desc("Use this option to turn on/off "
  41. "warnings about incorrect usage of llvm.expect intrinsics."));
  42. } // namespace llvm
  43. namespace {
  44. Instruction *getOprndOrInst(Instruction *I) {
  45. assert(I != nullptr && "MisExpect target Instruction cannot be nullptr");
  46. Instruction *Ret = nullptr;
  47. if (auto *B = dyn_cast<BranchInst>(I)) {
  48. Ret = dyn_cast<Instruction>(B->getCondition());
  49. }
  50. // TODO: Find a way to resolve condition location for switches
  51. // Using the condition of the switch seems to often resolve to an earlier
  52. // point in the program, i.e. the calculation of the switch condition, rather
  53. // than the switches location in the source code. Thus, we should use the
  54. // instruction to get source code locations rather than the condition to
  55. // improve diagnostic output, such as the caret. If the same problem exists
  56. // for branch instructions, then we should remove this function and directly
  57. // use the instruction
  58. //
  59. // else if (auto S = dyn_cast<SwitchInst>(I)) {
  60. // Ret = I;
  61. //}
  62. return Ret ? Ret : I;
  63. }
  64. void emitMisexpectDiagnostic(Instruction *I, LLVMContext &Ctx,
  65. uint64_t ProfCount, uint64_t TotalCount) {
  66. double PercentageCorrect = (double)ProfCount / TotalCount;
  67. auto PerString =
  68. formatv("{0:P} ({1} / {2})", PercentageCorrect, ProfCount, TotalCount);
  69. auto RemStr = formatv(
  70. "Potential performance regression from use of the llvm.expect intrinsic: "
  71. "Annotation was correct on {0} of profiled executions.",
  72. PerString);
  73. Twine Msg(PerString);
  74. Instruction *Cond = getOprndOrInst(I);
  75. if (PGOWarnMisExpect)
  76. Ctx.diagnose(DiagnosticInfoMisExpect(Cond, Msg));
  77. OptimizationRemarkEmitter ORE(I->getParent()->getParent());
  78. ORE.emit(OptimizationRemark(DEBUG_TYPE, "misexpect", Cond) << RemStr.str());
  79. }
  80. } // namespace
  81. namespace llvm {
  82. namespace misexpect {
  83. void verifyMisExpect(Instruction *I, const SmallVector<uint32_t, 4> &Weights,
  84. LLVMContext &Ctx) {
  85. if (auto *MisExpectData = I->getMetadata(LLVMContext::MD_misexpect)) {
  86. auto *MisExpectDataName = dyn_cast<MDString>(MisExpectData->getOperand(0));
  87. if (MisExpectDataName &&
  88. MisExpectDataName->getString().equals("misexpect")) {
  89. LLVM_DEBUG(llvm::dbgs() << "------------------\n");
  90. LLVM_DEBUG(llvm::dbgs()
  91. << "Function: " << I->getFunction()->getName() << "\n");
  92. LLVM_DEBUG(llvm::dbgs() << "Instruction: " << *I << ":\n");
  93. LLVM_DEBUG(for (int Idx = 0, Size = Weights.size(); Idx < Size; ++Idx) {
  94. llvm::dbgs() << "Weights[" << Idx << "] = " << Weights[Idx] << "\n";
  95. });
  96. // extract values from misexpect metadata
  97. const auto *IndexCint =
  98. mdconst::dyn_extract<ConstantInt>(MisExpectData->getOperand(1));
  99. const auto *LikelyCInt =
  100. mdconst::dyn_extract<ConstantInt>(MisExpectData->getOperand(2));
  101. const auto *UnlikelyCInt =
  102. mdconst::dyn_extract<ConstantInt>(MisExpectData->getOperand(3));
  103. if (!IndexCint || !LikelyCInt || !UnlikelyCInt)
  104. return;
  105. const uint64_t Index = IndexCint->getZExtValue();
  106. const uint64_t LikelyBranchWeight = LikelyCInt->getZExtValue();
  107. const uint64_t UnlikelyBranchWeight = UnlikelyCInt->getZExtValue();
  108. const uint64_t ProfileCount = Weights[Index];
  109. const uint64_t CaseTotal = std::accumulate(
  110. Weights.begin(), Weights.end(), (uint64_t)0, std::plus<uint64_t>());
  111. const uint64_t NumUnlikelyTargets = Weights.size() - 1;
  112. const uint64_t TotalBranchWeight =
  113. LikelyBranchWeight + (UnlikelyBranchWeight * NumUnlikelyTargets);
  114. const llvm::BranchProbability LikelyThreshold(LikelyBranchWeight,
  115. TotalBranchWeight);
  116. uint64_t ScaledThreshold = LikelyThreshold.scale(CaseTotal);
  117. LLVM_DEBUG(llvm::dbgs()
  118. << "Unlikely Targets: " << NumUnlikelyTargets << ":\n");
  119. LLVM_DEBUG(llvm::dbgs() << "Profile Count: " << ProfileCount << ":\n");
  120. LLVM_DEBUG(llvm::dbgs()
  121. << "Scaled Threshold: " << ScaledThreshold << ":\n");
  122. LLVM_DEBUG(llvm::dbgs() << "------------------\n");
  123. if (ProfileCount < ScaledThreshold)
  124. emitMisexpectDiagnostic(I, Ctx, ProfileCount, CaseTotal);
  125. }
  126. }
  127. }
  128. void checkFrontendInstrumentation(Instruction &I) {
  129. if (auto *MD = I.getMetadata(LLVMContext::MD_prof)) {
  130. unsigned NOps = MD->getNumOperands();
  131. // Only emit misexpect diagnostics if at least 2 branch weights are present.
  132. // Less than 2 branch weights means that the profiling metadata is:
  133. // 1) incorrect/corrupted
  134. // 2) not branch weight metadata
  135. // 3) completely deterministic
  136. // In these cases we should not emit any diagnostic related to misexpect.
  137. if (NOps < 3)
  138. return;
  139. // Operand 0 is a string tag "branch_weights"
  140. if (MDString *Tag = cast<MDString>(MD->getOperand(0))) {
  141. if (Tag->getString().equals("branch_weights")) {
  142. SmallVector<uint32_t, 4> RealWeights(NOps - 1);
  143. for (unsigned i = 1; i < NOps; i++) {
  144. ConstantInt *Value =
  145. mdconst::dyn_extract<ConstantInt>(MD->getOperand(i));
  146. RealWeights[i - 1] = Value->getZExtValue();
  147. }
  148. verifyMisExpect(&I, RealWeights, I.getContext());
  149. }
  150. }
  151. }
  152. }
  153. } // namespace misexpect
  154. } // namespace llvm
  155. #undef DEBUG_TYPE