Skip to content

[llvm][ProfDataUtils] provide getNumBranchWeights API #90146

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

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions llvm/include/llvm/IR/ProfDataUtils.h
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,8 @@ bool hasBranchWeightOrigin(const MDNode *ProfileData);
/// Return the offset to the first branch weight data
unsigned getBranchWeightOffset(const MDNode *ProfileData);

unsigned getNumBranchWeights(const MDNode &ProfileData);

/// Extract branch weights from MD_prof metadata
///
/// \param ProfileData A pointer to an MDNode.
Expand Down
6 changes: 1 addition & 5 deletions llvm/lib/IR/Instructions.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4002,11 +4002,7 @@ void SwitchInstProfUpdateWrapper::init() {
if (!ProfileData)
return;

// FIXME: This check belongs in ProfDataUtils. Its almost equivalent to
// getValidBranchWeightMDNode(), but the need to use llvm_unreachable
// makes them slightly different.
if (ProfileData->getNumOperands() !=
SI.getNumSuccessors() + getBranchWeightOffset(ProfileData)) {
if (getNumBranchWeights(*ProfileData) != SI.getNumSuccessors()) {
llvm_unreachable("number of prof branch_weights metadata operands does "
"not correspond to number of succesors");
}
Expand Down
8 changes: 5 additions & 3 deletions llvm/lib/IR/ProfDataUtils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,10 @@ unsigned getBranchWeightOffset(const MDNode *ProfileData) {
return hasBranchWeightOrigin(ProfileData) ? 2 : 1;
}

unsigned getNumBranchWeights(const MDNode &ProfileData) {
return ProfileData.getNumOperands() - getBranchWeightOffset(&ProfileData);
}

MDNode *getBranchWeightMDNode(const Instruction &I) {
auto *ProfileData = I.getMetadata(LLVMContext::MD_prof);
if (!isBranchWeightMD(ProfileData))
Expand All @@ -151,9 +155,7 @@ MDNode *getBranchWeightMDNode(const Instruction &I) {

MDNode *getValidBranchWeightMDNode(const Instruction &I) {
auto *ProfileData = getBranchWeightMDNode(I);
auto Offset = getBranchWeightOffset(ProfileData);
if (ProfileData &&
ProfileData->getNumOperands() == Offset + I.getNumSuccessors())
if (ProfileData && getNumBranchWeights(*ProfileData) == I.getNumSuccessors())
return ProfileData;
return nullptr;
}
Expand Down
12 changes: 6 additions & 6 deletions llvm/lib/IR/Verifier.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4818,10 +4818,9 @@ void Verifier::visitProfMetadata(Instruction &I, MDNode *MD) {

// Check consistency of !prof branch_weights metadata.
if (ProfName == "branch_weights") {
unsigned int Offset = getBranchWeightOffset(MD);
unsigned NumBranchWeights = getNumBranchWeights(*MD);
if (isa<InvokeInst>(&I)) {
Check(MD->getNumOperands() == (1 + Offset) ||
MD->getNumOperands() == (2 + Offset),
Check(NumBranchWeights == 1 || NumBranchWeights == 2,
"Wrong number of InvokeInst branch_weights operands", MD);
} else {
unsigned ExpectedNumOperands = 0;
Expand All @@ -4841,10 +4840,11 @@ void Verifier::visitProfMetadata(Instruction &I, MDNode *MD) {
CheckFailed("!prof branch_weights are not allowed for this instruction",
MD);

Check(MD->getNumOperands() == Offset + ExpectedNumOperands,
"Wrong number of operands", MD);
Check(NumBranchWeights == ExpectedNumOperands, "Wrong number of operands",
MD);
}
for (unsigned i = Offset; i < MD->getNumOperands(); ++i) {
for (unsigned i = getBranchWeightOffset(MD); i < MD->getNumOperands();
++i) {
auto &MDO = MD->getOperand(i);
Check(MDO, "second operand should not be null", MD);
Check(mdconst::dyn_extract<ConstantInt>(MDO),
Expand Down