Skip to content

Commit 97031a6

Browse files
authored
[NFC][LLVM] Code cleanup in MisExpect.cpp (llvm#162695)
- Make instruction arguments to the `misexpect` functions const and drop const for `ArrayRef` arguments. - Remove namespace llvm/misexpect surrounding all the code in .cpp file and instead use `using namespace`. - Change static helper functions to also use const references or pointers when possible.
1 parent ea5648b commit 97031a6

File tree

2 files changed

+32
-51
lines changed

2 files changed

+32
-51
lines changed

llvm/include/llvm/Transforms/Utils/MisExpect.h

Lines changed: 10 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,7 @@
2222
#include "llvm/IR/Instructions.h"
2323
#include "llvm/IR/LLVMContext.h"
2424

25-
namespace llvm {
26-
namespace misexpect {
25+
namespace llvm::misexpect {
2726

2827
/// checkBackendInstrumentation - compares PGO counters to the thresholds used
2928
/// for llvm.expect and warns if the PGO counters are outside of the expected
@@ -34,8 +33,8 @@ namespace misexpect {
3433
///
3534
/// \param I The Instruction being checked
3635
/// \param RealWeights A vector of profile weights for each target block
37-
void checkBackendInstrumentation(Instruction &I,
38-
const llvm::ArrayRef<uint32_t> RealWeights);
36+
void checkBackendInstrumentation(const Instruction &I,
37+
ArrayRef<uint32_t> RealWeights);
3938

4039
/// checkFrontendInstrumentation - compares PGO counters to the thresholds used
4140
/// for llvm.expect and warns if the PGO counters are outside of the expected
@@ -48,8 +47,8 @@ void checkBackendInstrumentation(Instruction &I,
4847
/// \param I The Instruction being checked
4948
/// \param ExpectedWeights A vector of the expected weights for each target
5049
/// block, this determines the threshold values used when emitting diagnostics
51-
void checkFrontendInstrumentation(Instruction &I,
52-
const ArrayRef<uint32_t> ExpectedWeights);
50+
void checkFrontendInstrumentation(const Instruction &I,
51+
ArrayRef<uint32_t> ExpectedWeights);
5352

5453
/// veryifyMisExpect - compares RealWeights to the thresholds used
5554
/// for llvm.expect and warns if the PGO counters are outside of the expected
@@ -58,8 +57,8 @@ void checkFrontendInstrumentation(Instruction &I,
5857
/// \param I The Instruction being checked
5958
/// \param RealWeights A vector of profile weights from the profile data
6059
/// \param ExpectedWeights A vector of the weights attatch by llvm.expect
61-
void verifyMisExpect(Instruction &I, ArrayRef<uint32_t> RealWeights,
62-
const ArrayRef<uint32_t> ExpectedWeights);
60+
void verifyMisExpect(const Instruction &I, ArrayRef<uint32_t> RealWeights,
61+
ArrayRef<uint32_t> ExpectedWeights);
6362

6463
/// checkExpectAnnotations - compares PGO counters to the thresholds used
6564
/// for llvm.expect and warns if the PGO counters are outside of the expected
@@ -72,11 +71,10 @@ void verifyMisExpect(Instruction &I, ArrayRef<uint32_t> RealWeights,
7271
/// \param I The Instruction being checked
7372
/// \param ExistingWeights A vector of profile weights for each target block
7473
/// \param IsFrontend A boolean describing if this is Frontend instrumentation
75-
void checkExpectAnnotations(Instruction &I,
76-
const ArrayRef<uint32_t> ExistingWeights,
74+
void checkExpectAnnotations(const Instruction &I,
75+
ArrayRef<uint32_t> ExistingWeights,
7776
bool IsFrontend);
7877

79-
} // namespace misexpect
80-
} // namespace llvm
78+
} // namespace llvm::misexpect
8179

8280
#endif

llvm/lib/Transforms/Utils/MisExpect.cpp

Lines changed: 22 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -48,8 +48,6 @@
4848
using namespace llvm;
4949
using 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
5553
static 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

Comments
 (0)