35 #define DEBUG_TYPE "misexpect" 38 using namespace misexpect;
46 cl::desc(
"Use this option to turn on/off " 47 "warnings about incorrect usage of llvm.expect intrinsics."));
54 assert(I !=
nullptr &&
"MisExpect target Instruction cannot be nullptr");
56 if (
auto *
B = dyn_cast<BranchInst>(I)) {
75 uint64_t ProfCount, uint64_t TotalCount) {
76 double PercentageCorrect = (double)ProfCount / TotalCount;
78 formatv(
"{0:P} ({1} / {2})", PercentageCorrect, ProfCount, TotalCount);
80 "Potential performance regression from use of the llvm.expect intrinsic: " 81 "Annotation was correct on {0} of profiled executions.",
98 if (
auto *MisExpectData = I->
getMetadata(LLVMContext::MD_misexpect)) {
100 if (MisExpectDataName &&
101 MisExpectDataName->getString().equals(
"misexpect")) {
107 llvm::dbgs() <<
"Weights[" << Idx <<
"] = " << Weights[Idx] <<
"\n";
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));
118 if (!IndexCint || !LikelyCInt || !UnlikelyCInt)
121 const uint64_t
Index = IndexCint->getZExtValue();
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;
129 const uint64_t TotalBranchWeight =
130 LikelyBranchWeight + (UnlikelyBranchWeight * NumUnlikelyTargets);
134 uint64_t ScaledThreshold = LikelyThreshold.scale(CaseTotal);
137 <<
"Unlikely Targets: " << NumUnlikelyTargets <<
":\n");
140 <<
"Scaled Threshold: " << ScaledThreshold <<
":\n");
142 if (ProfileCount < ScaledThreshold)
143 emitMisexpectDiagnostic(I, Ctx, ProfileCount, CaseTotal);
149 if (
auto *MD = I.
getMetadata(LLVMContext::MD_prof)) {
150 unsigned NOps = MD->getNumOperands();
162 if (
MDString *Tag = cast<MDString>(MD->getOperand(0))) {
163 if (Tag->getString().equals(
"branch_weights")) {
165 for (
unsigned i = 1; i < NOps; i++) {
167 mdconst::dyn_extract<ConstantInt>(MD->getOperand(i));
This class represents lattice values for constants.
LLVMContext & getContext() const
All values hold a context through their type.
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...
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.
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...
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)
uint64_t getZExtValue() const
Return the constant as a 64-bit unsigned integer value after it has been zero extended as appropriate...
static GCRegistry::Add< OcamlGC > B("ocaml", "ocaml 3.10-compatible GC")
This is an important class for using LLVM in a threaded context.
This file contains the declarations for the subclasses of Constant, which represent the different fla...
Diagnostic information for MisExpect analysis.
const Function * getFunction() const
Return the function this instruction belongs to.
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.
This is the shared class of boolean and integer constants.
raw_ostream & dbgs()
dbgs() - This returns a reference to a raw_ostream for debugging messages.
StringRef getName() const
Return a constant reference to the value's name.
const Function * getParent() const
Return the enclosing method, or null if none.
LLVM_NODISCARD std::enable_if<!is_simple_type< Y >::value, typename cast_retty< X, const Y >::ret_type >::type dyn_cast(const Y &Val)
void diagnose(const DiagnosticInfo &DI)
Report a message to the currently installed diagnostic handler.
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
LLVM Value Representation.
void checkFrontendInstrumentation(Instruction &I)
checkClangInstrumentation - verify if llvm.expect matches PGO profile This function checks the fronte...
const BasicBlock * getParent() const