Skip to content

Commit 32e82d0

Browse files
committed
[IR][PGO] Verify the structure of VP metadata.
1 parent c539ec0 commit 32e82d0

File tree

3 files changed

+20
-3
lines changed

3 files changed

+20
-3
lines changed

llvm/include/llvm/IR/ProfDataUtils.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,9 @@ LLVM_ABI bool hasProfMD(const Instruction &I);
3535
/// Checks if an MDNode contains Branch Weight Metadata
3636
LLVM_ABI bool isBranchWeightMD(const MDNode *ProfileData);
3737

38+
/// Checks if an MDNode contains value profiling Metadata
39+
LLVM_ABI bool isValueProfileMD(const MDNode *ProfileData);
40+
3841
/// Checks if an instructions has Branch Weight Metadata
3942
///
4043
/// \param I The instruction to check

llvm/lib/IR/ProfDataUtils.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,7 @@ bool isBranchWeightMD(const MDNode *ProfileData) {
103103
return isTargetMD(ProfileData, MDProfLabels::BranchWeights, MinBWOps);
104104
}
105105

106-
static bool isValueProfileMD(const MDNode *ProfileData) {
106+
bool isValueProfileMD(const MDNode *ProfileData) {
107107
return isTargetMD(ProfileData, MDProfLabels::ValueProfile, MinVPOps);
108108
}
109109

llvm/lib/IR/Verifier.cpp

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,7 @@
112112
#include "llvm/IR/Value.h"
113113
#include "llvm/InitializePasses.h"
114114
#include "llvm/Pass.h"
115+
#include "llvm/ProfileData/InstrProf.h"
115116
#include "llvm/Support/AMDGPUAddrSpace.h"
116117
#include "llvm/Support/AtomicOrdering.h"
117118
#include "llvm/Support/Casting.h"
@@ -5026,9 +5027,22 @@ void Verifier::visitProfMetadata(Instruction &I, MDNode *MD) {
50265027
Check(mdconst::dyn_extract<ConstantInt>(MDO),
50275028
"!prof brunch_weights operand is not a const int");
50285029
}
5030+
} else if (ProfName == MDProfLabels::ValueProfile) {
5031+
Check(isValueProfileMD(MD),"invalid value profiling metadata",MD);
5032+
ConstantInt *KindInt = mdconst::dyn_extract<ConstantInt>(MD->getOperand(1));
5033+
Check(KindInt, "VP !prof missing kind argument", MD);
5034+
5035+
auto Kind = KindInt->getZExtValue();
5036+
Check(Kind >= InstrProfValueKind::IPVK_First &&
5037+
Kind <= InstrProfValueKind::IPVK_Last,
5038+
"Invalid VP !prof kind", MD);
5039+
if (Kind == InstrProfValueKind::IPVK_IndirectCallTarget || Kind == InstrProfValueKind::IPVK_MemOPSize)
5040+
Check(isa<CallBase>(I),
5041+
"VP !prof indirect call or memop size expected to be applied to "
5042+
"CallBase instructions only",
5043+
MD);
50295044
} else {
5030-
Check(ProfName == MDProfLabels::ValueProfile,
5031-
"expected either branch_weights or VP profile name", MD);
5045+
CheckFailed("expected either branch_weights or VP profile name", MD);
50325046
}
50335047
}
50345048

0 commit comments

Comments
 (0)