Skip to content

Commit 285e023

Browse files
committed
ProfDataUtils: Add extractFromBranchWeightMD function; NFC
Expose internal helper function as new `extractFromBranchWeightMD` API. Differential revision: https://reviews.llvm.org/D157937
1 parent f8431a0 commit 285e023

File tree

2 files changed

+25
-21
lines changed

2 files changed

+25
-21
lines changed

llvm/include/llvm/IR/ProfDataUtils.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,11 @@ MDNode *getValidBranchWeightMDNode(const Instruction &I);
6464
bool extractBranchWeights(const MDNode *ProfileData,
6565
SmallVectorImpl<uint32_t> &Weights);
6666

67+
/// Faster version of extractBranchWeights() that skips checks and must only
68+
/// be called with "branch_weights" metadata nodes.
69+
void extractFromBranchWeightMD(const MDNode *ProfileData,
70+
SmallVectorImpl<uint32_t> &Weights);
71+
6772
/// Extract branch weights attatched to an Instruction
6873
///
6974
/// \param I The Instruction to extract weights from.

llvm/lib/IR/ProfDataUtils.cpp

Lines changed: 20 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -45,26 +45,6 @@ constexpr unsigned WeightsIdx = 1;
4545
// the minimum number of operands for MD_prof nodes with branch weights
4646
constexpr unsigned MinBWOps = 3;
4747

48-
bool extractWeights(const MDNode *ProfileData,
49-
SmallVectorImpl<uint32_t> &Weights) {
50-
// Assume preconditions are already met (i.e. this is valid metadata)
51-
assert(ProfileData && "ProfileData was nullptr in extractWeights");
52-
unsigned NOps = ProfileData->getNumOperands();
53-
54-
assert(WeightsIdx < NOps && "Weights Index must be less than NOps.");
55-
Weights.resize(NOps - WeightsIdx);
56-
57-
for (unsigned Idx = WeightsIdx, E = NOps; Idx != E; ++Idx) {
58-
ConstantInt *Weight =
59-
mdconst::dyn_extract<ConstantInt>(ProfileData->getOperand(Idx));
60-
assert(Weight && "Malformed branch_weight in MD_prof node");
61-
assert(Weight->getValue().getActiveBits() <= 32 &&
62-
"Too many bits for uint32_t");
63-
Weights[Idx - WeightsIdx] = Weight->getZExtValue();
64-
}
65-
return true;
66-
}
67-
6848
// We may want to add support for other MD_prof types, so provide an abstraction
6949
// for checking the metadata type.
7050
bool isTargetMD(const MDNode *ProfData, const char *Name, unsigned MinOps) {
@@ -119,11 +99,30 @@ MDNode *getValidBranchWeightMDNode(const Instruction &I) {
11999
return nullptr;
120100
}
121101

102+
void extractFromBranchWeightMD(const MDNode *ProfileData,
103+
SmallVectorImpl<uint32_t> &Weights) {
104+
assert(isBranchWeightMD(ProfileData) && "wrong metadata");
105+
106+
unsigned NOps = ProfileData->getNumOperands();
107+
assert(WeightsIdx < NOps && "Weights Index must be less than NOps.");
108+
Weights.resize(NOps - WeightsIdx);
109+
110+
for (unsigned Idx = WeightsIdx, E = NOps; Idx != E; ++Idx) {
111+
ConstantInt *Weight =
112+
mdconst::dyn_extract<ConstantInt>(ProfileData->getOperand(Idx));
113+
assert(Weight && "Malformed branch_weight in MD_prof node");
114+
assert(Weight->getValue().getActiveBits() <= 32 &&
115+
"Too many bits for uint32_t");
116+
Weights[Idx - WeightsIdx] = Weight->getZExtValue();
117+
}
118+
}
119+
122120
bool extractBranchWeights(const MDNode *ProfileData,
123121
SmallVectorImpl<uint32_t> &Weights) {
124122
if (!isBranchWeightMD(ProfileData))
125123
return false;
126-
return extractWeights(ProfileData, Weights);
124+
extractFromBranchWeightMD(ProfileData, Weights);
125+
return true;
127126
}
128127

129128
bool extractBranchWeights(const Instruction &I,

0 commit comments

Comments
 (0)