4848using namespace llvm ;
4949using namespace misexpect ;
5050
51- namespace llvm {
52-
5351// Command line option to enable/disable the warning when profile data suggests
5452// a mismatch with the use of the llvm.expect intrinsic
5553static cl::opt<bool > PGOWarnMisExpect (
@@ -63,22 +61,18 @@ static cl::opt<uint32_t> MisExpectTolerance(
6361 cl::desc(" Prevents emitting diagnostics when profile counts are "
6462 " within N% of the threshold.." ));
6563
66- } // namespace llvm
67-
68- namespace {
69-
70- bool isMisExpectDiagEnabled (LLVMContext &Ctx) {
64+ static bool isMisExpectDiagEnabled (const LLVMContext &Ctx) {
7165 return PGOWarnMisExpect || Ctx.getMisExpectWarningRequested ();
7266}
7367
74- uint32_t getMisExpectTolerance (LLVMContext &Ctx) {
68+ static uint32_t getMisExpectTolerance (const LLVMContext &Ctx) {
7569 return std::max (static_cast <uint32_t >(MisExpectTolerance),
7670 Ctx.getDiagnosticsMisExpectTolerance ());
7771}
7872
79- Instruction *getInstCondition (Instruction *I) {
73+ static const Instruction *getInstCondition (const Instruction *I) {
8074 assert (I != nullptr && " MisExpect target Instruction cannot be nullptr" );
81- Instruction *Ret = nullptr ;
75+ const Instruction *Ret = nullptr ;
8276 if (auto *B = dyn_cast<BranchInst>(I)) {
8377 Ret = dyn_cast<Instruction>(B->getCondition ());
8478 }
@@ -97,29 +91,25 @@ Instruction *getInstCondition(Instruction *I) {
9791 return Ret ? Ret : I;
9892}
9993
100- void emitMisexpectDiagnostic (Instruction *I, LLVMContext &Ctx,
101- uint64_t ProfCount, uint64_t TotalCount) {
94+ static void emitMisexpectDiagnostic (const Instruction *I, LLVMContext &Ctx,
95+ uint64_t ProfCount, uint64_t TotalCount) {
10296 double PercentageCorrect = (double )ProfCount / TotalCount;
10397 auto PerString =
10498 formatv (" {0:P} ({1} / {2})" , PercentageCorrect, ProfCount, TotalCount);
10599 auto RemStr = formatv (
106100 " Potential performance regression from use of the llvm.expect intrinsic: "
107101 " Annotation was correct on {0} of profiled executions." ,
108102 PerString);
109- Instruction *Cond = getInstCondition (I);
103+ const Instruction *Cond = getInstCondition (I);
110104 if (isMisExpectDiagEnabled (Ctx))
111105 Ctx.diagnose (DiagnosticInfoMisExpect (Cond, Twine (PerString)));
112106 OptimizationRemarkEmitter ORE (I->getParent ()->getParent ());
113107 ORE.emit (OptimizationRemark (DEBUG_TYPE, " misexpect" , Cond) << RemStr.str ());
114108}
115109
116- } // namespace
117-
118- namespace llvm {
119- namespace misexpect {
120-
121- void verifyMisExpect (Instruction &I, ArrayRef<uint32_t > RealWeights,
122- ArrayRef<uint32_t > ExpectedWeights) {
110+ void misexpect::verifyMisExpect (const Instruction &I,
111+ ArrayRef<uint32_t > RealWeights,
112+ ArrayRef<uint32_t > ExpectedWeights) {
123113 // To determine if we emit a diagnostic, we need to compare the branch weights
124114 // from the profile to those added by the llvm.expect intrinsic.
125115 // So first, we extract the "likely" and "unlikely" weights from
@@ -128,15 +118,13 @@ void verifyMisExpect(Instruction &I, ArrayRef<uint32_t> RealWeights,
128118 uint64_t LikelyBranchWeight = 0 ,
129119 UnlikelyBranchWeight = std::numeric_limits<uint32_t >::max ();
130120 size_t MaxIndex = 0 ;
131- for (size_t Idx = 0 , End = ExpectedWeights.size (); Idx < End; Idx++) {
132- uint32_t V = ExpectedWeights[Idx];
121+ for (const auto &[Idx, V] : enumerate(ExpectedWeights)) {
133122 if (LikelyBranchWeight < V) {
134123 LikelyBranchWeight = V;
135124 MaxIndex = Idx;
136125 }
137- if (UnlikelyBranchWeight > V) {
126+ if (UnlikelyBranchWeight > V)
138127 UnlikelyBranchWeight = V;
139- }
140128 }
141129
142130 const uint64_t ProfiledWeight = RealWeights[MaxIndex];
@@ -161,7 +149,7 @@ void verifyMisExpect(Instruction &I, ArrayRef<uint32_t> RealWeights,
161149 uint64_t ScaledThreshold = LikelyProbablilty.scale (RealWeightsTotal);
162150
163151 // clamp tolerance range to [0, 100)
164- auto Tolerance = getMisExpectTolerance (I.getContext ());
152+ uint32_t Tolerance = getMisExpectTolerance (I.getContext ());
165153 Tolerance = std::clamp (Tolerance, 0u , 99u );
166154
167155 // Allow users to relax checking by N% i.e., if they use a 5% tolerance,
@@ -175,8 +163,8 @@ void verifyMisExpect(Instruction &I, ArrayRef<uint32_t> RealWeights,
175163 RealWeightsTotal);
176164}
177165
178- void checkBackendInstrumentation (Instruction &I,
179- const ArrayRef<uint32_t > RealWeights) {
166+ void misexpect:: checkBackendInstrumentation (const Instruction &I,
167+ ArrayRef<uint32_t > RealWeights) {
180168 // Backend checking assumes any existing weight comes from an `llvm.expect`
181169 // intrinsic. However, SampleProfiling + ThinLTO add branch weights multiple
182170 // times, leading to an invalid assumption in our checking. Backend checks
@@ -190,24 +178,19 @@ void checkBackendInstrumentation(Instruction &I,
190178 verifyMisExpect (I, RealWeights, ExpectedWeights);
191179}
192180
193- void checkFrontendInstrumentation (Instruction &I,
194- const ArrayRef<uint32_t > ExpectedWeights) {
181+ void misexpect:: checkFrontendInstrumentation (
182+ const Instruction &I, ArrayRef<uint32_t > ExpectedWeights) {
195183 SmallVector<uint32_t > RealWeights;
196184 if (!extractBranchWeights (I, RealWeights))
197185 return ;
198186 verifyMisExpect (I, RealWeights, ExpectedWeights);
199187}
200188
201- void checkExpectAnnotations (Instruction &I,
202- const ArrayRef<uint32_t > ExistingWeights,
203- bool IsFrontend) {
204- if (IsFrontend) {
189+ void misexpect:: checkExpectAnnotations (const Instruction &I,
190+ ArrayRef<uint32_t > ExistingWeights,
191+ bool IsFrontend) {
192+ if (IsFrontend)
205193 checkFrontendInstrumentation (I, ExistingWeights);
206- } else {
194+ else
207195 checkBackendInstrumentation (I, ExistingWeights);
208- }
209196}
210-
211- } // namespace misexpect
212- } // namespace llvm
213- #undef DEBUG_TYPE
0 commit comments