LLVM  10.0.0
MisExpect.cpp
Go to the documentation of this file.
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 
20 #include "llvm/ADT/Twine.h"
22 #include "llvm/IR/Constants.h"
23 #include "llvm/IR/DiagnosticInfo.h"
24 #include "llvm/IR/Instruction.h"
25 #include "llvm/IR/Instructions.h"
26 #include "llvm/IR/LLVMContext.h"
29 #include "llvm/Support/Debug.h"
31 #include <cstdint>
32 #include <functional>
33 #include <numeric>
34 
35 #define DEBUG_TYPE "misexpect"
36 
37 using namespace llvm;
38 using namespace misexpect;
39 
40 namespace llvm {
41 
42 // Command line option to enable/disable the warning when profile data suggests
43 // a mismatch with the use of the llvm.expect intrinsic
45  "pgo-warn-misexpect", cl::init(false), cl::Hidden,
46  cl::desc("Use this option to turn on/off "
47  "warnings about incorrect usage of llvm.expect intrinsics."));
48 
49 } // namespace llvm
50 
51 namespace {
52 
53 Instruction *getOprndOrInst(Instruction *I) {
54  assert(I != nullptr && "MisExpect target Instruction cannot be nullptr");
55  Instruction *Ret = nullptr;
56  if (auto *B = dyn_cast<BranchInst>(I)) {
57  Ret = dyn_cast<Instruction>(B->getCondition());
58  }
59  // TODO: Find a way to resolve condition location for switches
60  // Using the condition of the switch seems to often resolve to an earlier
61  // point in the program, i.e. the calculation of the switch condition, rather
62  // than the switches location in the source code. Thus, we should use the
63  // instruction to get source code locations rather than the condition to
64  // improve diagnostic output, such as the caret. If the same problem exists
65  // for branch instructions, then we should remove this function and directly
66  // use the instruction
67  //
68  // else if (auto S = dyn_cast<SwitchInst>(I)) {
69  // Ret = I;
70  //}
71  return Ret ? Ret : I;
72 }
73 
74 void emitMisexpectDiagnostic(Instruction *I, LLVMContext &Ctx,
75  uint64_t ProfCount, uint64_t TotalCount) {
76  double PercentageCorrect = (double)ProfCount / TotalCount;
77  auto PerString =
78  formatv("{0:P} ({1} / {2})", PercentageCorrect, ProfCount, TotalCount);
79  auto RemStr = formatv(
80  "Potential performance regression from use of the llvm.expect intrinsic: "
81  "Annotation was correct on {0} of profiled executions.",
82  PerString);
83  Twine Msg(PerString);
84  Instruction *Cond = getOprndOrInst(I);
85  if (PGOWarnMisExpect)
86  Ctx.diagnose(DiagnosticInfoMisExpect(Cond, Msg));
88  ORE.emit(OptimizationRemark(DEBUG_TYPE, "misexpect", Cond) << RemStr.str());
89 }
90 
91 } // namespace
92 
93 namespace llvm {
94 namespace misexpect {
95 
97  LLVMContext &Ctx) {
98  if (auto *MisExpectData = I->getMetadata(LLVMContext::MD_misexpect)) {
99  auto *MisExpectDataName = dyn_cast<MDString>(MisExpectData->getOperand(0));
100  if (MisExpectDataName &&
101  MisExpectDataName->getString().equals("misexpect")) {
102  LLVM_DEBUG(llvm::dbgs() << "------------------\n");
104  << "Function: " << I->getFunction()->getName() << "\n");
105  LLVM_DEBUG(llvm::dbgs() << "Instruction: " << *I << ":\n");
106  LLVM_DEBUG(for (int Idx = 0, Size = Weights.size(); Idx < Size; ++Idx) {
107  llvm::dbgs() << "Weights[" << Idx << "] = " << Weights[Idx] << "\n";
108  });
109 
110  // extract values from misexpect metadata
111  const auto *IndexCint =
112  mdconst::dyn_extract<ConstantInt>(MisExpectData->getOperand(1));
113  const auto *LikelyCInt =
114  mdconst::dyn_extract<ConstantInt>(MisExpectData->getOperand(2));
115  const auto *UnlikelyCInt =
116  mdconst::dyn_extract<ConstantInt>(MisExpectData->getOperand(3));
117 
118  if (!IndexCint || !LikelyCInt || !UnlikelyCInt)
119  return;
120 
121  const uint64_t Index = IndexCint->getZExtValue();
122  const uint64_t LikelyBranchWeight = LikelyCInt->getZExtValue();
123  const uint64_t UnlikelyBranchWeight = UnlikelyCInt->getZExtValue();
124  const uint64_t ProfileCount = Weights[Index];
125  const uint64_t CaseTotal = std::accumulate(
126  Weights.begin(), Weights.end(), (uint64_t)0, std::plus<uint64_t>());
127  const uint64_t NumUnlikelyTargets = Weights.size() - 1;
128 
129  const uint64_t TotalBranchWeight =
130  LikelyBranchWeight + (UnlikelyBranchWeight * NumUnlikelyTargets);
131 
132  const llvm::BranchProbability LikelyThreshold(LikelyBranchWeight,
133  TotalBranchWeight);
134  uint64_t ScaledThreshold = LikelyThreshold.scale(CaseTotal);
135 
137  << "Unlikely Targets: " << NumUnlikelyTargets << ":\n");
138  LLVM_DEBUG(llvm::dbgs() << "Profile Count: " << ProfileCount << ":\n");
140  << "Scaled Threshold: " << ScaledThreshold << ":\n");
141  LLVM_DEBUG(llvm::dbgs() << "------------------\n");
142  if (ProfileCount < ScaledThreshold)
143  emitMisexpectDiagnostic(I, Ctx, ProfileCount, CaseTotal);
144  }
145  }
146 }
147 
149  if (auto *MD = I.getMetadata(LLVMContext::MD_prof)) {
150  unsigned NOps = MD->getNumOperands();
151 
152  // Only emit misexpect diagnostics if at least 2 branch weights are present.
153  // Less than 2 branch weights means that the profiling metadata is:
154  // 1) incorrect/corrupted
155  // 2) not branch weight metadata
156  // 3) completely deterministic
157  // In these cases we should not emit any diagnostic related to misexpect.
158  if (NOps < 3)
159  return;
160 
161  // Operand 0 is a string tag "branch_weights"
162  if (MDString *Tag = cast<MDString>(MD->getOperand(0))) {
163  if (Tag->getString().equals("branch_weights")) {
164  SmallVector<uint32_t, 4> RealWeights(NOps - 1);
165  for (unsigned i = 1; i < NOps; i++) {
166  ConstantInt *Value =
167  mdconst::dyn_extract<ConstantInt>(MD->getOperand(i));
168  RealWeights[i - 1] = Value->getZExtValue();
169  }
170  verifyMisExpect(&I, RealWeights, I.getContext());
171  }
172  }
173  }
174 }
175 
176 } // namespace misexpect
177 } // namespace llvm
178 #undef DEBUG_TYPE
This class represents lattice values for constants.
Definition: AllocatorList.h:23
LLVMContext & getContext() const
All values hold a context through their type.
Definition: Value.cpp:744
auto formatv(const char *Fmt, Ts &&... Vals) -> formatv_object< decltype(std::make_tuple(detail::build_format_adapter(std::forward< Ts >(Vals))...))>
Twine - A lightweight data structure for efficiently representing the concatenation of temporary valu...
Definition: Twine.h:80
#define DEBUG_TYPE
Definition: MisExpect.cpp:35
static cl::opt< bool > PGOWarnMisExpect("pgo-warn-misexpect", cl::init(false), cl::Hidden, cl::desc("Use this option to turn on/off " "warnings about incorrect usage of llvm.expect intrinsics."))
MDNode * getMetadata(unsigned KindID) const
Get the metadata of given kind attached to this Instruction.
Definition: Instruction.h:244
void verifyMisExpect(llvm::Instruction *I, const llvm::SmallVector< uint32_t, 4 > &Weights, llvm::LLVMContext &Ctx)
verifyMisExpect - compares PGO counters to the thresholds used for llvm.expect and warns if the PGO c...
Definition: MisExpect.cpp:96
static cl::opt< uint32_t > UnlikelyBranchWeight("unlikely-branch-weight", cl::Hidden, cl::init(1), cl::desc("Weight of the branch unlikely to be taken (default = 1)"))
initializer< Ty > init(const Ty &Val)
Definition: CommandLine.h:435
uint64_t getZExtValue() const
Return the constant as a 64-bit unsigned integer value after it has been zero extended as appropriate...
Definition: Constants.h:148
static GCRegistry::Add< OcamlGC > B("ocaml", "ocaml 3.10-compatible GC")
This is an important class for using LLVM in a threaded context.
Definition: LLVMContext.h:63
This file contains the declarations for the subclasses of Constant, which represent the different fla...
Diagnostic information for applied optimization remarks.
Diagnostic information for MisExpect analysis.
const Function * getFunction() const
Return the function this instruction belongs to.
Definition: Instruction.cpp:59
static cl::opt< uint32_t > LikelyBranchWeight("likely-branch-weight", cl::Hidden, cl::init(2000), cl::desc("Weight of the branch likely to be taken (default = 2000)"))
Class to represent profile counts.
Definition: Function.h:260
size_t size() const
Definition: SmallVector.h:52
This is the shared class of boolean and integer constants.
Definition: Constants.h:83
void emit(DiagnosticInfoOptimizationBase &OptDiag)
Output the remark via the diagnostic handler and to the optimization record file. ...
raw_ostream & dbgs()
dbgs() - This returns a reference to a raw_ostream for debugging messages.
Definition: Debug.cpp:132
StringRef getName() const
Return a constant reference to the value&#39;s name.
Definition: Value.cpp:215
const Function * getParent() const
Return the enclosing method, or null if none.
Definition: BasicBlock.h:106
#define I(x, y, z)
Definition: MD5.cpp:58
LLVM_NODISCARD std::enable_if<!is_simple_type< Y >::value, typename cast_retty< X, const Y >::ret_type >::type dyn_cast(const Y &Val)
Definition: Casting.h:332
uint32_t Size
Definition: Profile.cpp:46
void diagnose(const DiagnosticInfo &DI)
Report a message to the currently installed diagnostic handler.
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
LLVM Value Representation.
Definition: Value.h:74
A single uniqued string.
Definition: Metadata.h:604
#define LLVM_DEBUG(X)
Definition: Debug.h:122
void checkFrontendInstrumentation(Instruction &I)
checkClangInstrumentation - verify if llvm.expect matches PGO profile This function checks the fronte...
Definition: MisExpect.cpp:148
The optimization diagnostic interface.
const BasicBlock * getParent() const
Definition: Instruction.h:66