Skip to content

Commit 82cbd68

Browse files
authored
[NFC][PGO] Use constants rather than free strings for metadata labels (#145721)
1 parent 00f6d6a commit 82cbd68

File tree

8 files changed

+48
-26
lines changed

8 files changed

+48
-26
lines changed

llvm/include/llvm/IR/ProfDataUtils.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,13 @@
2121
#include "llvm/Support/Compiler.h"
2222

2323
namespace llvm {
24+
struct MDProfLabels {
25+
static const char *BranchWeights;
26+
static const char *ValueProfile;
27+
static const char *FunctionEntryCount;
28+
static const char *SyntheticFunctionEntryCount;
29+
static const char *ExpectedBranchWeights;
30+
};
2431

2532
/// Checks if an Instruction has MD_prof Metadata
2633
LLVM_ABI bool hasProfMD(const Instruction &I);

llvm/lib/Bitcode/Reader/BitcodeReader.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7031,7 +7031,7 @@ Error BitcodeReader::materialize(GlobalValue *GV) {
70317031
MDString *MDS = cast<MDString>(MD->getOperand(0));
70327032
StringRef ProfName = MDS->getString();
70337033
// Check consistency of !prof branch_weights metadata.
7034-
if (ProfName != "branch_weights")
7034+
if (ProfName != MDProfLabels::BranchWeights)
70357035
continue;
70367036
unsigned ExpectedNumOperands = 0;
70377037
if (BranchInst *BI = dyn_cast<BranchInst>(&I))

llvm/lib/IR/Function.cpp

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@
3737
#include "llvm/IR/Metadata.h"
3838
#include "llvm/IR/Module.h"
3939
#include "llvm/IR/Operator.h"
40+
#include "llvm/IR/ProfDataUtils.h"
4041
#include "llvm/IR/SymbolTableListTraits.h"
4142
#include "llvm/IR/Type.h"
4243
#include "llvm/IR/Use.h"
@@ -1115,7 +1116,7 @@ std::optional<ProfileCount> Function::getEntryCount(bool AllowSynthetic) const {
11151116
MDNode *MD = getMetadata(LLVMContext::MD_prof);
11161117
if (MD && MD->getOperand(0))
11171118
if (MDString *MDS = dyn_cast<MDString>(MD->getOperand(0))) {
1118-
if (MDS->getString() == "function_entry_count") {
1119+
if (MDS->getString() == MDProfLabels::FunctionEntryCount) {
11191120
ConstantInt *CI = mdconst::extract<ConstantInt>(MD->getOperand(1));
11201121
uint64_t Count = CI->getValue().getZExtValue();
11211122
// A value of -1 is used for SamplePGO when there were no samples.
@@ -1124,7 +1125,8 @@ std::optional<ProfileCount> Function::getEntryCount(bool AllowSynthetic) const {
11241125
return std::nullopt;
11251126
return ProfileCount(Count, PCT_Real);
11261127
} else if (AllowSynthetic &&
1127-
MDS->getString() == "synthetic_function_entry_count") {
1128+
MDS->getString() ==
1129+
MDProfLabels::SyntheticFunctionEntryCount) {
11281130
ConstantInt *CI = mdconst::extract<ConstantInt>(MD->getOperand(1));
11291131
uint64_t Count = CI->getValue().getZExtValue();
11301132
return ProfileCount(Count, PCT_Synthetic);
@@ -1137,7 +1139,7 @@ DenseSet<GlobalValue::GUID> Function::getImportGUIDs() const {
11371139
DenseSet<GlobalValue::GUID> R;
11381140
if (MDNode *MD = getMetadata(LLVMContext::MD_prof))
11391141
if (MDString *MDS = dyn_cast<MDString>(MD->getOperand(0)))
1140-
if (MDS->getString() == "function_entry_count")
1142+
if (MDS->getString() == MDProfLabels::FunctionEntryCount)
11411143
for (unsigned i = 2; i < MD->getNumOperands(); i++)
11421144
R.insert(mdconst::extract<ConstantInt>(MD->getOperand(i))
11431145
->getValue()

llvm/lib/IR/MDBuilder.cpp

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
#include "llvm/IR/Constants.h"
1616
#include "llvm/IR/Function.h"
1717
#include "llvm/IR/Metadata.h"
18+
#include "llvm/IR/ProfDataUtils.h"
1819
using namespace llvm;
1920

2021
MDString *MDBuilder::createString(StringRef Str) {
@@ -55,9 +56,9 @@ MDNode *MDBuilder::createBranchWeights(ArrayRef<uint32_t> Weights,
5556

5657
unsigned int Offset = IsExpected ? 2 : 1;
5758
SmallVector<Metadata *, 4> Vals(Weights.size() + Offset);
58-
Vals[0] = createString("branch_weights");
59+
Vals[0] = createString(MDProfLabels::BranchWeights);
5960
if (IsExpected)
60-
Vals[1] = createString("expected");
61+
Vals[1] = createString(MDProfLabels::ExpectedBranchWeights);
6162

6263
Type *Int32Ty = Type::getInt32Ty(Context);
6364
for (unsigned i = 0, e = Weights.size(); i != e; ++i)
@@ -74,9 +75,9 @@ MDNode *MDBuilder::createFunctionEntryCount(
7475
Type *Int64Ty = Type::getInt64Ty(Context);
7576
SmallVector<Metadata *, 8> Ops;
7677
if (Synthetic)
77-
Ops.push_back(createString("synthetic_function_entry_count"));
78+
Ops.push_back(createString(MDProfLabels::SyntheticFunctionEntryCount));
7879
else
79-
Ops.push_back(createString("function_entry_count"));
80+
Ops.push_back(createString(MDProfLabels::FunctionEntryCount));
8081
Ops.push_back(createConstant(ConstantInt::get(Int64Ty, Count)));
8182
if (Imports) {
8283
SmallVector<GlobalValue::GUID, 2> OrderID(Imports->begin(), Imports->end());

llvm/lib/IR/Metadata.cpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1202,14 +1202,15 @@ MDNode *MDNode::mergeDirectCallProfMetadata(MDNode *A, MDNode *B,
12021202
"first operand should be a non-null MDString");
12031203
StringRef AProfName = AMDS->getString();
12041204
StringRef BProfName = BMDS->getString();
1205-
if (AProfName == "branch_weights" && BProfName == "branch_weights") {
1205+
if (AProfName == MDProfLabels::BranchWeights &&
1206+
BProfName == MDProfLabels::BranchWeights) {
12061207
ConstantInt *AInstrWeight = mdconst::dyn_extract<ConstantInt>(
12071208
A->getOperand(getBranchWeightOffset(A)));
12081209
ConstantInt *BInstrWeight = mdconst::dyn_extract<ConstantInt>(
12091210
B->getOperand(getBranchWeightOffset(B)));
12101211
assert(AInstrWeight && BInstrWeight && "verified by LLVM verifier");
12111212
return MDNode::get(Ctx,
1212-
{MDHelper.createString("branch_weights"),
1213+
{MDHelper.createString(MDProfLabels::BranchWeights),
12131214
MDHelper.createConstant(ConstantInt::get(
12141215
Type::getInt64Ty(Ctx),
12151216
SaturatingAdd(AInstrWeight->getZExtValue(),

llvm/lib/IR/ProfDataUtils.cpp

Lines changed: 19 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -88,16 +88,23 @@ static void extractFromBranchWeightMD(const MDNode *ProfileData,
8888

8989
namespace llvm {
9090

91+
const char *MDProfLabels::BranchWeights = "branch_weights";
92+
const char *MDProfLabels::ExpectedBranchWeights = "expected";
93+
const char *MDProfLabels::ValueProfile = "VP";
94+
const char *MDProfLabels::FunctionEntryCount = "function_entry_count";
95+
const char *MDProfLabels::SyntheticFunctionEntryCount =
96+
"synthetic_function_entry_count";
97+
9198
bool hasProfMD(const Instruction &I) {
9299
return I.hasMetadata(LLVMContext::MD_prof);
93100
}
94101

95102
bool isBranchWeightMD(const MDNode *ProfileData) {
96-
return isTargetMD(ProfileData, "branch_weights", MinBWOps);
103+
return isTargetMD(ProfileData, MDProfLabels::BranchWeights, MinBWOps);
97104
}
98105

99106
static bool isValueProfileMD(const MDNode *ProfileData) {
100-
return isTargetMD(ProfileData, "VP", MinVPOps);
107+
return isTargetMD(ProfileData, MDProfLabels::ValueProfile, MinVPOps);
101108
}
102109

103110
bool hasBranchWeightMD(const Instruction &I) {
@@ -131,7 +138,8 @@ bool hasBranchWeightOrigin(const MDNode *ProfileData) {
131138
// NOTE: if we ever have more types of branch weight provenance,
132139
// we need to check the string value is "expected". For now, we
133140
// supply a more generic API, and avoid the spurious comparisons.
134-
assert(ProfDataName == nullptr || ProfDataName->getString() == "expected");
141+
assert(ProfDataName == nullptr ||
142+
ProfDataName->getString() == MDProfLabels::ExpectedBranchWeights);
135143
return ProfDataName != nullptr;
136144
}
137145

@@ -210,7 +218,7 @@ bool extractProfTotalWeight(const MDNode *ProfileData, uint64_t &TotalVal) {
210218
if (!ProfDataName)
211219
return false;
212220

213-
if (ProfDataName->getString() == "branch_weights") {
221+
if (ProfDataName->getString() == MDProfLabels::BranchWeights) {
214222
unsigned Offset = getBranchWeightOffset(ProfileData);
215223
for (unsigned Idx = Offset; Idx < ProfileData->getNumOperands(); ++Idx) {
216224
auto *V = mdconst::extract<ConstantInt>(ProfileData->getOperand(Idx));
@@ -219,7 +227,8 @@ bool extractProfTotalWeight(const MDNode *ProfileData, uint64_t &TotalVal) {
219227
return true;
220228
}
221229

222-
if (ProfDataName->getString() == "VP" && ProfileData->getNumOperands() > 3) {
230+
if (ProfDataName->getString() == MDProfLabels::ValueProfile &&
231+
ProfileData->getNumOperands() > 3) {
223232
TotalVal = mdconst::dyn_extract<ConstantInt>(ProfileData->getOperand(2))
224233
->getValue()
225234
.getZExtValue();
@@ -246,8 +255,9 @@ void scaleProfData(Instruction &I, uint64_t S, uint64_t T) {
246255
return;
247256

248257
auto *ProfDataName = dyn_cast<MDString>(ProfileData->getOperand(0));
249-
if (!ProfDataName || (ProfDataName->getString() != "branch_weights" &&
250-
ProfDataName->getString() != "VP"))
258+
if (!ProfDataName ||
259+
(ProfDataName->getString() != MDProfLabels::BranchWeights &&
260+
ProfDataName->getString() != MDProfLabels::ValueProfile))
251261
return;
252262

253263
if (!hasCountTypeMD(I))
@@ -259,7 +269,7 @@ void scaleProfData(Instruction &I, uint64_t S, uint64_t T) {
259269
SmallVector<Metadata *, 3> Vals;
260270
Vals.push_back(ProfileData->getOperand(0));
261271
APInt APS(128, S), APT(128, T);
262-
if (ProfDataName->getString() == "branch_weights" &&
272+
if (ProfDataName->getString() == MDProfLabels::BranchWeights &&
263273
ProfileData->getNumOperands() > 0) {
264274
// Using APInt::div may be expensive, but most cases should fit 64 bits.
265275
APInt Val(128,
@@ -270,7 +280,7 @@ void scaleProfData(Instruction &I, uint64_t S, uint64_t T) {
270280
Val *= APS;
271281
Vals.push_back(MDB.createConstant(ConstantInt::get(
272282
Type::getInt32Ty(C), Val.udiv(APT).getLimitedValue(UINT32_MAX))));
273-
} else if (ProfDataName->getString() == "VP")
283+
} else if (ProfDataName->getString() == MDProfLabels::ValueProfile)
274284
for (unsigned Idx = 1; Idx < ProfileData->getNumOperands(); Idx += 2) {
275285
// The first value is the key of the value profile, which will not change.
276286
Vals.push_back(ProfileData->getOperand(Idx));

llvm/lib/IR/Verifier.cpp

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2536,8 +2536,8 @@ void Verifier::verifyFunctionMetadata(
25362536
"expected string with name of the !prof annotation", MD);
25372537
MDString *MDS = cast<MDString>(MD->getOperand(0));
25382538
StringRef ProfName = MDS->getString();
2539-
Check(ProfName == "function_entry_count" ||
2540-
ProfName == "synthetic_function_entry_count",
2539+
Check(ProfName == MDProfLabels::FunctionEntryCount ||
2540+
ProfName == MDProfLabels::SyntheticFunctionEntryCount,
25412541
"first operand should be 'function_entry_count'"
25422542
" or 'synthetic_function_entry_count'",
25432543
MD);
@@ -4993,7 +4993,7 @@ void Verifier::visitProfMetadata(Instruction &I, MDNode *MD) {
49934993
StringRef ProfName = MDS->getString();
49944994

49954995
// Check consistency of !prof branch_weights metadata.
4996-
if (ProfName == "branch_weights") {
4996+
if (ProfName == MDProfLabels::BranchWeights) {
49974997
unsigned NumBranchWeights = getNumBranchWeights(*MD);
49984998
if (isa<InvokeInst>(&I)) {
49994999
Check(NumBranchWeights == 1 || NumBranchWeights == 2,
@@ -5027,8 +5027,8 @@ void Verifier::visitProfMetadata(Instruction &I, MDNode *MD) {
50275027
"!prof brunch_weights operand is not a const int");
50285028
}
50295029
} else {
5030-
Check(ProfName == "VP", "expected either branch_weights or VP profile name",
5031-
MD);
5030+
Check(ProfName == MDProfLabels::ValueProfile,
5031+
"expected either branch_weights or VP profile name", MD);
50325032
}
50335033
}
50345034

llvm/lib/ProfileData/InstrProf.cpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
#include "llvm/IR/MDBuilder.h"
2828
#include "llvm/IR/Metadata.h"
2929
#include "llvm/IR/Module.h"
30+
#include "llvm/IR/ProfDataUtils.h"
3031
#include "llvm/IR/Type.h"
3132
#include "llvm/ProfileData/InstrProfReader.h"
3233
#include "llvm/Support/Casting.h"
@@ -1358,7 +1359,7 @@ void annotateValueSite(Module &M, Instruction &Inst,
13581359
MDBuilder MDHelper(Ctx);
13591360
SmallVector<Metadata *, 3> Vals;
13601361
// Tag
1361-
Vals.push_back(MDHelper.createString("VP"));
1362+
Vals.push_back(MDHelper.createString(MDProfLabels::ValueProfile));
13621363
// Value Kind
13631364
Vals.push_back(MDHelper.createConstant(
13641365
ConstantInt::get(Type::getInt32Ty(Ctx), ValueKind)));
@@ -1389,7 +1390,7 @@ MDNode *mayHaveValueProfileOfKind(const Instruction &Inst,
13891390
return nullptr;
13901391

13911392
MDString *Tag = cast<MDString>(MD->getOperand(0));
1392-
if (!Tag || Tag->getString() != "VP")
1393+
if (!Tag || Tag->getString() != MDProfLabels::ValueProfile)
13931394
return nullptr;
13941395

13951396
// Now check kind:

0 commit comments

Comments
 (0)