Skip to content

[NFC][PGO] Use constants rather than free strings for metadata labels #145721

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Conversation

mtrofin
Copy link
Member

@mtrofin mtrofin commented Jun 25, 2025

No description provided.

Copy link
Member Author

mtrofin commented Jun 25, 2025

@mtrofin mtrofin marked this pull request as ready for review June 25, 2025 15:37
@llvmbot llvmbot added PGO Profile Guided Optimizations llvm:ir labels Jun 25, 2025
@llvmbot
Copy link
Member

llvmbot commented Jun 25, 2025

@llvm/pr-subscribers-llvm-ir

Author: Mircea Trofin (mtrofin)

Changes

Full diff: https://github.com/llvm/llvm-project/pull/145721.diff

8 Files Affected:

  • (modified) llvm/include/llvm/IR/ProfDataUtils.h (+7)
  • (modified) llvm/lib/Bitcode/Reader/BitcodeReader.cpp (+1-1)
  • (modified) llvm/lib/IR/Function.cpp (+5-3)
  • (modified) llvm/lib/IR/MDBuilder.cpp (+5-4)
  • (modified) llvm/lib/IR/Metadata.cpp (+3-2)
  • (modified) llvm/lib/IR/ProfDataUtils.cpp (+19-9)
  • (modified) llvm/lib/IR/Verifier.cpp (+5-5)
  • (modified) llvm/lib/ProfileData/InstrProf.cpp (+3-2)
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<MDString>(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<BranchInst>(&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<ProfileCount> Function::getEntryCount(bool AllowSynthetic) const {
   MDNode *MD = getMetadata(LLVMContext::MD_prof);
   if (MD && MD->getOperand(0))
     if (MDString *MDS = dyn_cast<MDString>(MD->getOperand(0))) {
-      if (MDS->getString() == "function_entry_count") {
+      if (MDS->getString() == MDProfLabels::FunctionEntryCount) {
         ConstantInt *CI = mdconst::extract<ConstantInt>(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<ProfileCount> 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<ConstantInt>(MD->getOperand(1));
         uint64_t Count = CI->getValue().getZExtValue();
         return ProfileCount(Count, PCT_Synthetic);
@@ -1137,7 +1139,7 @@ DenseSet<GlobalValue::GUID> Function::getImportGUIDs() const {
   DenseSet<GlobalValue::GUID> R;
   if (MDNode *MD = getMetadata(LLVMContext::MD_prof))
     if (MDString *MDS = dyn_cast<MDString>(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<ConstantInt>(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<uint32_t> Weights,
 
   unsigned int Offset = IsExpected ? 2 : 1;
   SmallVector<Metadata *, 4> 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<Metadata *, 8> 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<GlobalValue::GUID, 2> 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<ConstantInt>(
         A->getOperand(getBranchWeightOffset(A)));
     ConstantInt *BInstrWeight = mdconst::dyn_extract<ConstantInt>(
         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<ConstantInt>(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<ConstantInt>(ProfileData->getOperand(2))
                    ->getValue()
                    .getZExtValue();
@@ -246,8 +255,9 @@ void scaleProfData(Instruction &I, uint64_t S, uint64_t T) {
     return;
 
   auto *ProfDataName = dyn_cast<MDString>(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<Metadata *, 3> 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 ae95e3e2bff8d..2316c74fed0dc 100644
--- a/llvm/lib/IR/Verifier.cpp
+++ b/llvm/lib/IR/Verifier.cpp
@@ -2518,8 +2518,8 @@ void Verifier::verifyFunctionMetadata(
             "expected string with name of the !prof annotation", MD);
       MDString *MDS = cast<MDString>(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);
@@ -4975,7 +4975,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<InvokeInst>(&I)) {
       Check(NumBranchWeights == 1 || NumBranchWeights == 2,
@@ -5009,8 +5009,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<Metadata *, 3> 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<MDString>(MD->getOperand(0));
-  if (!Tag || Tag->getString() != "VP")
+  if (!Tag || Tag->getString() != MDProfLabels::ValueProfile)
     return nullptr;
 
   // Now check kind:

@llvmbot
Copy link
Member

llvmbot commented Jun 25, 2025

@llvm/pr-subscribers-pgo

Author: Mircea Trofin (mtrofin)

Changes

Full diff: https://github.com/llvm/llvm-project/pull/145721.diff

8 Files Affected:

  • (modified) llvm/include/llvm/IR/ProfDataUtils.h (+7)
  • (modified) llvm/lib/Bitcode/Reader/BitcodeReader.cpp (+1-1)
  • (modified) llvm/lib/IR/Function.cpp (+5-3)
  • (modified) llvm/lib/IR/MDBuilder.cpp (+5-4)
  • (modified) llvm/lib/IR/Metadata.cpp (+3-2)
  • (modified) llvm/lib/IR/ProfDataUtils.cpp (+19-9)
  • (modified) llvm/lib/IR/Verifier.cpp (+5-5)
  • (modified) llvm/lib/ProfileData/InstrProf.cpp (+3-2)
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<MDString>(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<BranchInst>(&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<ProfileCount> Function::getEntryCount(bool AllowSynthetic) const {
   MDNode *MD = getMetadata(LLVMContext::MD_prof);
   if (MD && MD->getOperand(0))
     if (MDString *MDS = dyn_cast<MDString>(MD->getOperand(0))) {
-      if (MDS->getString() == "function_entry_count") {
+      if (MDS->getString() == MDProfLabels::FunctionEntryCount) {
         ConstantInt *CI = mdconst::extract<ConstantInt>(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<ProfileCount> 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<ConstantInt>(MD->getOperand(1));
         uint64_t Count = CI->getValue().getZExtValue();
         return ProfileCount(Count, PCT_Synthetic);
@@ -1137,7 +1139,7 @@ DenseSet<GlobalValue::GUID> Function::getImportGUIDs() const {
   DenseSet<GlobalValue::GUID> R;
   if (MDNode *MD = getMetadata(LLVMContext::MD_prof))
     if (MDString *MDS = dyn_cast<MDString>(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<ConstantInt>(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<uint32_t> Weights,
 
   unsigned int Offset = IsExpected ? 2 : 1;
   SmallVector<Metadata *, 4> 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<Metadata *, 8> 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<GlobalValue::GUID, 2> 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<ConstantInt>(
         A->getOperand(getBranchWeightOffset(A)));
     ConstantInt *BInstrWeight = mdconst::dyn_extract<ConstantInt>(
         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<ConstantInt>(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<ConstantInt>(ProfileData->getOperand(2))
                    ->getValue()
                    .getZExtValue();
@@ -246,8 +255,9 @@ void scaleProfData(Instruction &I, uint64_t S, uint64_t T) {
     return;
 
   auto *ProfDataName = dyn_cast<MDString>(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<Metadata *, 3> 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 ae95e3e2bff8d..2316c74fed0dc 100644
--- a/llvm/lib/IR/Verifier.cpp
+++ b/llvm/lib/IR/Verifier.cpp
@@ -2518,8 +2518,8 @@ void Verifier::verifyFunctionMetadata(
             "expected string with name of the !prof annotation", MD);
       MDString *MDS = cast<MDString>(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);
@@ -4975,7 +4975,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<InvokeInst>(&I)) {
       Check(NumBranchWeights == 1 || NumBranchWeights == 2,
@@ -5009,8 +5009,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<Metadata *, 3> 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<MDString>(MD->getOperand(0));
-  if (!Tag || Tag->getString() != "VP")
+  if (!Tag || Tag->getString() != MDProfLabels::ValueProfile)
     return nullptr;
 
   // Now check kind:

Copy link
Contributor

@kazutakahirata kazutakahirata left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM. Thanks!

Base automatically changed from users/mtrofin/06-24-_ir_pgo_verify_invalid_md_prof_metadata_on_instructions to main June 25, 2025 20:10
@mtrofin mtrofin force-pushed the users/mtrofin/06-25-_nfc_pgo_use_constants_rather_than_free_strings_for_metadata_labels branch from db2a49f to 4f4d763 Compare June 25, 2025 20:13
Copy link
Member Author

mtrofin commented Jun 25, 2025

Merge activity

  • Jun 25, 11:18 PM UTC: A user started a stack merge that includes this pull request via Graphite.
  • Jun 25, 11:20 PM UTC: @mtrofin merged this pull request with Graphite.

@mtrofin mtrofin merged commit 82cbd68 into main Jun 25, 2025
7 checks passed
@mtrofin mtrofin deleted the users/mtrofin/06-25-_nfc_pgo_use_constants_rather_than_free_strings_for_metadata_labels branch June 25, 2025 23:20
anthonyhatran pushed a commit to anthonyhatran/llvm-project that referenced this pull request Jun 26, 2025
rlavaee pushed a commit to rlavaee/llvm-project that referenced this pull request Jul 1, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
llvm:ir PGO Profile Guided Optimizations
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants