48#define DEBUG_TYPE "misexpect"
51using namespace misexpect;
59 cl::desc(
"Use this option to turn on/off "
60 "warnings about incorrect usage of llvm.expect intrinsics."));
65 cl::desc(
"Prevents emitting diagnostics when profile counts are "
66 "within N% of the threshold.."));
82 assert(
I !=
nullptr &&
"MisExpect target Instruction cannot be nullptr");
84 if (
auto *
B = dyn_cast<BranchInst>(
I)) {
85 Ret = dyn_cast<Instruction>(
B->getCondition());
96 else if (
auto *S = dyn_cast<SwitchInst>(
I)) {
97 Ret = dyn_cast<Instruction>(S->getCondition());
104 double PercentageCorrect = (double)ProfCount / TotalCount;
106 formatv(
"{0:P} ({1} / {2})", PercentageCorrect, ProfCount, TotalCount);
108 "Potential performance regression from use of the llvm.expect intrinsic: "
109 "Annotation was correct on {0} of profiled executions.",
111 Twine Msg(PerString);
113 if (isMisExpectDiagEnabled(Ctx))
145 const uint64_t ProfiledWeight = RealWeights[MaxIndex];
148 std::plus<uint64_t>());
149 const uint64_t NumUnlikelyTargets = RealWeights.
size() - 1;
156 "TotalBranchWeight is less than the Likely branch weight");
164 uint64_t ScaledThreshold = LikelyProbablilty.scale(RealWeightsTotal);
167 auto Tolerance = getMisExpectTolerance(
I.getContext());
168 Tolerance = std::clamp(Tolerance, 0u, 99u);
173 ScaledThreshold *= (1.0 - Tolerance / 100.0);
176 if (ProfiledWeight < ScaledThreshold)
177 emitMisexpectDiagnostic(&
I,
I.getContext(), ProfiledWeight,
static GCRegistry::Add< OcamlGC > B("ocaml", "ocaml 3.10-compatible GC")
This file contains the declarations for the subclasses of Constant, which represent the different fla...
Returns the sub type a function will return at a given Idx Should correspond to the result type of an ExtractValue instruction executed with just that one unsigned Idx
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)"))
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)"))
This file contains the declarations for profiling metadata utility functions.
const SmallVectorImpl< MachineOperand > & Cond
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
ArrayRef - Represent a constant reference to an array (0 or more elements consecutively in memory),...
size_t size() const
size - Get the array size.
static BranchProbability getBranchProbability(uint64_t Numerator, uint64_t Denominator)
Diagnostic information for MisExpect analysis.
This is an important class for using LLVM in a threaded context.
bool getMisExpectWarningRequested() const
void diagnose(const DiagnosticInfo &DI)
Report a message to the currently installed diagnostic handler.
uint32_t getDiagnosticsMisExpectTolerance() const
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small.
Twine - A lightweight data structure for efficiently representing the concatenation of temporary valu...
initializer< Ty > init(const Ty &Val)
void checkFrontendInstrumentation(Instruction &I, const ArrayRef< uint32_t > ExpectedWeights)
checkFrontendInstrumentation - compares PGO counters to the thresholds used for llvm....
void checkExpectAnnotations(Instruction &I, const ArrayRef< uint32_t > ExistingWeights, bool IsFrontend)
checkExpectAnnotations - compares PGO counters to the thresholds used for llvm.expect and warns if th...
void checkBackendInstrumentation(Instruction &I, const llvm::ArrayRef< uint32_t > RealWeights)
checkBackendInstrumentation - compares PGO counters to the thresholds used for llvm....
void verifyMisExpect(Instruction &I, ArrayRef< uint32_t > RealWeights, const ArrayRef< uint32_t > ExpectedWeights)
veryifyMisExpect - compares RealWeights to the thresholds used for llvm.expect and warns if the PGO c...
This is an optimization pass for GlobalISel generic memory operations.
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."))
auto formatv(const char *Fmt, Ts &&...Vals) -> formatv_object< decltype(std::make_tuple(support::detail::build_format_adapter(std::forward< Ts >(Vals))...))>
bool hasBranchWeightOrigin(const Instruction &I)
Check if Branch Weight Metadata has an "expected" field from an llvm.expect* intrinsic.
static cl::opt< uint32_t > MisExpectTolerance("misexpect-tolerance", cl::init(0), cl::desc("Prevents emitting diagnostics when profile counts are " "within N% of the threshold.."))
bool extractBranchWeights(const MDNode *ProfileData, SmallVectorImpl< uint32_t > &Weights)
Extract branch weights from MD_prof metadata.