diff --git a/llvm/include/llvm/IR/ProfDataUtils.h b/llvm/include/llvm/IR/ProfDataUtils.h index 8e8d069b836f1..5c0e08b03c245 100644 --- a/llvm/include/llvm/IR/ProfDataUtils.h +++ b/llvm/include/llvm/IR/ProfDataUtils.h @@ -21,6 +21,13 @@ #include "llvm/Support/Compiler.h" namespace llvm { +struct MDProfLabels { + static const char *BranchWeights; + static const char *ValueProfile; + static const char *FunctionEntryCount; + static const char *SyntheticFunctionEntryCount; + static const char *ExpectedBranchWeights; +}; /// Checks if an Instruction has MD_prof Metadata LLVM_ABI bool hasProfMD(const Instruction &I); diff --git a/llvm/lib/Bitcode/Reader/BitcodeReader.cpp b/llvm/lib/Bitcode/Reader/BitcodeReader.cpp index fde934fbb3cf1..e91f791ab5788 100644 --- a/llvm/lib/Bitcode/Reader/BitcodeReader.cpp +++ b/llvm/lib/Bitcode/Reader/BitcodeReader.cpp @@ -7031,7 +7031,7 @@ Error BitcodeReader::materialize(GlobalValue *GV) { MDString *MDS = cast(MD->getOperand(0)); StringRef ProfName = MDS->getString(); // Check consistency of !prof branch_weights metadata. - if (ProfName != "branch_weights") + if (ProfName != MDProfLabels::BranchWeights) continue; unsigned ExpectedNumOperands = 0; if (BranchInst *BI = dyn_cast(&I)) diff --git a/llvm/lib/IR/Function.cpp b/llvm/lib/IR/Function.cpp index 28fb81055baf4..3e7fcbb983738 100644 --- a/llvm/lib/IR/Function.cpp +++ b/llvm/lib/IR/Function.cpp @@ -37,6 +37,7 @@ #include "llvm/IR/Metadata.h" #include "llvm/IR/Module.h" #include "llvm/IR/Operator.h" +#include "llvm/IR/ProfDataUtils.h" #include "llvm/IR/SymbolTableListTraits.h" #include "llvm/IR/Type.h" #include "llvm/IR/Use.h" @@ -1115,7 +1116,7 @@ std::optional Function::getEntryCount(bool AllowSynthetic) const { MDNode *MD = getMetadata(LLVMContext::MD_prof); if (MD && MD->getOperand(0)) if (MDString *MDS = dyn_cast(MD->getOperand(0))) { - if (MDS->getString() == "function_entry_count") { + if (MDS->getString() == MDProfLabels::FunctionEntryCount) { ConstantInt *CI = mdconst::extract(MD->getOperand(1)); uint64_t Count = CI->getValue().getZExtValue(); // A value of -1 is used for SamplePGO when there were no samples. @@ -1124,7 +1125,8 @@ std::optional Function::getEntryCount(bool AllowSynthetic) const { return std::nullopt; return ProfileCount(Count, PCT_Real); } else if (AllowSynthetic && - MDS->getString() == "synthetic_function_entry_count") { + MDS->getString() == + MDProfLabels::SyntheticFunctionEntryCount) { ConstantInt *CI = mdconst::extract(MD->getOperand(1)); uint64_t Count = CI->getValue().getZExtValue(); return ProfileCount(Count, PCT_Synthetic); @@ -1137,7 +1139,7 @@ DenseSet Function::getImportGUIDs() const { DenseSet R; if (MDNode *MD = getMetadata(LLVMContext::MD_prof)) if (MDString *MDS = dyn_cast(MD->getOperand(0))) - if (MDS->getString() == "function_entry_count") + if (MDS->getString() == MDProfLabels::FunctionEntryCount) for (unsigned i = 2; i < MD->getNumOperands(); i++) R.insert(mdconst::extract(MD->getOperand(i)) ->getValue() diff --git a/llvm/lib/IR/MDBuilder.cpp b/llvm/lib/IR/MDBuilder.cpp index b6aa8844a7eaf..893f99a19b936 100644 --- a/llvm/lib/IR/MDBuilder.cpp +++ b/llvm/lib/IR/MDBuilder.cpp @@ -15,6 +15,7 @@ #include "llvm/IR/Constants.h" #include "llvm/IR/Function.h" #include "llvm/IR/Metadata.h" +#include "llvm/IR/ProfDataUtils.h" using namespace llvm; MDString *MDBuilder::createString(StringRef Str) { @@ -55,9 +56,9 @@ MDNode *MDBuilder::createBranchWeights(ArrayRef Weights, unsigned int Offset = IsExpected ? 2 : 1; SmallVector Vals(Weights.size() + Offset); - Vals[0] = createString("branch_weights"); + Vals[0] = createString(MDProfLabels::BranchWeights); if (IsExpected) - Vals[1] = createString("expected"); + Vals[1] = createString(MDProfLabels::ExpectedBranchWeights); Type *Int32Ty = Type::getInt32Ty(Context); for (unsigned i = 0, e = Weights.size(); i != e; ++i) @@ -74,9 +75,9 @@ MDNode *MDBuilder::createFunctionEntryCount( Type *Int64Ty = Type::getInt64Ty(Context); SmallVector Ops; if (Synthetic) - Ops.push_back(createString("synthetic_function_entry_count")); + Ops.push_back(createString(MDProfLabels::SyntheticFunctionEntryCount)); else - Ops.push_back(createString("function_entry_count")); + Ops.push_back(createString(MDProfLabels::FunctionEntryCount)); Ops.push_back(createConstant(ConstantInt::get(Int64Ty, Count))); if (Imports) { SmallVector OrderID(Imports->begin(), Imports->end()); diff --git a/llvm/lib/IR/Metadata.cpp b/llvm/lib/IR/Metadata.cpp index 8e78cd9cc573a..86408d9643c75 100644 --- a/llvm/lib/IR/Metadata.cpp +++ b/llvm/lib/IR/Metadata.cpp @@ -1202,14 +1202,15 @@ MDNode *MDNode::mergeDirectCallProfMetadata(MDNode *A, MDNode *B, "first operand should be a non-null MDString"); StringRef AProfName = AMDS->getString(); StringRef BProfName = BMDS->getString(); - if (AProfName == "branch_weights" && BProfName == "branch_weights") { + if (AProfName == MDProfLabels::BranchWeights && + BProfName == MDProfLabels::BranchWeights) { ConstantInt *AInstrWeight = mdconst::dyn_extract( A->getOperand(getBranchWeightOffset(A))); ConstantInt *BInstrWeight = mdconst::dyn_extract( B->getOperand(getBranchWeightOffset(B))); assert(AInstrWeight && BInstrWeight && "verified by LLVM verifier"); return MDNode::get(Ctx, - {MDHelper.createString("branch_weights"), + {MDHelper.createString(MDProfLabels::BranchWeights), MDHelper.createConstant(ConstantInt::get( Type::getInt64Ty(Ctx), SaturatingAdd(AInstrWeight->getZExtValue(), diff --git a/llvm/lib/IR/ProfDataUtils.cpp b/llvm/lib/IR/ProfDataUtils.cpp index 21524eb840539..740023ca6d23b 100644 --- a/llvm/lib/IR/ProfDataUtils.cpp +++ b/llvm/lib/IR/ProfDataUtils.cpp @@ -88,16 +88,23 @@ static void extractFromBranchWeightMD(const MDNode *ProfileData, namespace llvm { +const char *MDProfLabels::BranchWeights = "branch_weights"; +const char *MDProfLabels::ExpectedBranchWeights = "expected"; +const char *MDProfLabels::ValueProfile = "VP"; +const char *MDProfLabels::FunctionEntryCount = "function_entry_count"; +const char *MDProfLabels::SyntheticFunctionEntryCount = + "synthetic_function_entry_count"; + bool hasProfMD(const Instruction &I) { return I.hasMetadata(LLVMContext::MD_prof); } bool isBranchWeightMD(const MDNode *ProfileData) { - return isTargetMD(ProfileData, "branch_weights", MinBWOps); + return isTargetMD(ProfileData, MDProfLabels::BranchWeights, MinBWOps); } static bool isValueProfileMD(const MDNode *ProfileData) { - return isTargetMD(ProfileData, "VP", MinVPOps); + return isTargetMD(ProfileData, MDProfLabels::ValueProfile, MinVPOps); } bool hasBranchWeightMD(const Instruction &I) { @@ -131,7 +138,8 @@ bool hasBranchWeightOrigin(const MDNode *ProfileData) { // NOTE: if we ever have more types of branch weight provenance, // we need to check the string value is "expected". For now, we // supply a more generic API, and avoid the spurious comparisons. - assert(ProfDataName == nullptr || ProfDataName->getString() == "expected"); + assert(ProfDataName == nullptr || + ProfDataName->getString() == MDProfLabels::ExpectedBranchWeights); return ProfDataName != nullptr; } @@ -210,7 +218,7 @@ bool extractProfTotalWeight(const MDNode *ProfileData, uint64_t &TotalVal) { if (!ProfDataName) return false; - if (ProfDataName->getString() == "branch_weights") { + if (ProfDataName->getString() == MDProfLabels::BranchWeights) { unsigned Offset = getBranchWeightOffset(ProfileData); for (unsigned Idx = Offset; Idx < ProfileData->getNumOperands(); ++Idx) { auto *V = mdconst::extract(ProfileData->getOperand(Idx)); @@ -219,7 +227,8 @@ bool extractProfTotalWeight(const MDNode *ProfileData, uint64_t &TotalVal) { return true; } - if (ProfDataName->getString() == "VP" && ProfileData->getNumOperands() > 3) { + if (ProfDataName->getString() == MDProfLabels::ValueProfile && + ProfileData->getNumOperands() > 3) { TotalVal = mdconst::dyn_extract(ProfileData->getOperand(2)) ->getValue() .getZExtValue(); @@ -246,8 +255,9 @@ void scaleProfData(Instruction &I, uint64_t S, uint64_t T) { return; auto *ProfDataName = dyn_cast(ProfileData->getOperand(0)); - if (!ProfDataName || (ProfDataName->getString() != "branch_weights" && - ProfDataName->getString() != "VP")) + if (!ProfDataName || + (ProfDataName->getString() != MDProfLabels::BranchWeights && + ProfDataName->getString() != MDProfLabels::ValueProfile)) return; if (!hasCountTypeMD(I)) @@ -259,7 +269,7 @@ void scaleProfData(Instruction &I, uint64_t S, uint64_t T) { SmallVector Vals; Vals.push_back(ProfileData->getOperand(0)); APInt APS(128, S), APT(128, T); - if (ProfDataName->getString() == "branch_weights" && + if (ProfDataName->getString() == MDProfLabels::BranchWeights && ProfileData->getNumOperands() > 0) { // Using APInt::div may be expensive, but most cases should fit 64 bits. APInt Val(128, @@ -270,7 +280,7 @@ void scaleProfData(Instruction &I, uint64_t S, uint64_t T) { Val *= APS; Vals.push_back(MDB.createConstant(ConstantInt::get( Type::getInt32Ty(C), Val.udiv(APT).getLimitedValue(UINT32_MAX)))); - } else if (ProfDataName->getString() == "VP") + } else if (ProfDataName->getString() == MDProfLabels::ValueProfile) for (unsigned Idx = 1; Idx < ProfileData->getNumOperands(); Idx += 2) { // The first value is the key of the value profile, which will not change. Vals.push_back(ProfileData->getOperand(Idx)); diff --git a/llvm/lib/IR/Verifier.cpp b/llvm/lib/IR/Verifier.cpp index e7bb6d9a3e32d..9cab88b09779a 100644 --- a/llvm/lib/IR/Verifier.cpp +++ b/llvm/lib/IR/Verifier.cpp @@ -2536,8 +2536,8 @@ void Verifier::verifyFunctionMetadata( "expected string with name of the !prof annotation", MD); MDString *MDS = cast(MD->getOperand(0)); StringRef ProfName = MDS->getString(); - Check(ProfName == "function_entry_count" || - ProfName == "synthetic_function_entry_count", + Check(ProfName == MDProfLabels::FunctionEntryCount || + ProfName == MDProfLabels::SyntheticFunctionEntryCount, "first operand should be 'function_entry_count'" " or 'synthetic_function_entry_count'", MD); @@ -4993,7 +4993,7 @@ void Verifier::visitProfMetadata(Instruction &I, MDNode *MD) { StringRef ProfName = MDS->getString(); // Check consistency of !prof branch_weights metadata. - if (ProfName == "branch_weights") { + if (ProfName == MDProfLabels::BranchWeights) { unsigned NumBranchWeights = getNumBranchWeights(*MD); if (isa(&I)) { Check(NumBranchWeights == 1 || NumBranchWeights == 2, @@ -5027,8 +5027,8 @@ void Verifier::visitProfMetadata(Instruction &I, MDNode *MD) { "!prof brunch_weights operand is not a const int"); } } else { - Check(ProfName == "VP", "expected either branch_weights or VP profile name", - MD); + Check(ProfName == MDProfLabels::ValueProfile, + "expected either branch_weights or VP profile name", MD); } } diff --git a/llvm/lib/ProfileData/InstrProf.cpp b/llvm/lib/ProfileData/InstrProf.cpp index 368e3535fe905..9a8b4cfc81f6a 100644 --- a/llvm/lib/ProfileData/InstrProf.cpp +++ b/llvm/lib/ProfileData/InstrProf.cpp @@ -27,6 +27,7 @@ #include "llvm/IR/MDBuilder.h" #include "llvm/IR/Metadata.h" #include "llvm/IR/Module.h" +#include "llvm/IR/ProfDataUtils.h" #include "llvm/IR/Type.h" #include "llvm/ProfileData/InstrProfReader.h" #include "llvm/Support/Casting.h" @@ -1358,7 +1359,7 @@ void annotateValueSite(Module &M, Instruction &Inst, MDBuilder MDHelper(Ctx); SmallVector Vals; // Tag - Vals.push_back(MDHelper.createString("VP")); + Vals.push_back(MDHelper.createString(MDProfLabels::ValueProfile)); // Value Kind Vals.push_back(MDHelper.createConstant( ConstantInt::get(Type::getInt32Ty(Ctx), ValueKind))); @@ -1389,7 +1390,7 @@ MDNode *mayHaveValueProfileOfKind(const Instruction &Inst, return nullptr; MDString *Tag = cast(MD->getOperand(0)); - if (!Tag || Tag->getString() != "VP") + if (!Tag || Tag->getString() != MDProfLabels::ValueProfile) return nullptr; // Now check kind: