[PATCH] D61179: [WIP] Verifier: check prof branch_weights
Yevgeny Rouban via Phabricator via llvm-commits
llvm-commits at lists.llvm.org
Fri Apr 26 03:05:32 PDT 2019
yrouban created this revision.
yrouban added reviewers: asbirlea, reames, chandlerc.
Herald added a subscriber: hiraditya.
Herald added a project: LLVM.
There are constraints on !pro branch_weights metadata
https://llvm.org/docs/BranchWeightMetadata.html
This patch is to check those constraints.
A light-weight version of this kind of verification I tried to propose in D60554 <https://reviews.llvm.org/D60554>.
This patch is an in-progress-work:
- Currently, all tests pass with this verification but there are cases that fail if I artificially add more !prof branch_weights metadata to the tests. I will try fixing bugs that I found and creating new tests. I tried to fix those places at once by changing Instructions API (see D60554 <https://reviews.llvm.org/D60554>, D60604 <https://reviews.llvm.org/D60604> and D60606 <https://reviews.llvm.org/D60606>) but proposed changes were not accepted. So I'm going to fix individual passes.
- The aforementioned lang ref needs to be fixed too. As I found in the tests, two more instructions may have !prof branch_weights: Invoke and Select. I'm not sure who should fix the documentation.
https://reviews.llvm.org/D61179
Files:
llvm/lib/IR/Verifier.cpp
Index: llvm/lib/IR/Verifier.cpp
===================================================================
--- llvm/lib/IR/Verifier.cpp
+++ llvm/lib/IR/Verifier.cpp
@@ -416,6 +416,7 @@
void visitBasicBlock(BasicBlock &BB);
void visitRangeMetadata(Instruction &I, MDNode *Range, Type *Ty);
void visitDereferenceableMetadata(Instruction &I, MDNode *MD);
+ void visitProfMetadata(Instruction &I, MDNode *MD);
template <class Ty> bool isValidMetadataArray(const MDTuple &N);
#define HANDLE_SPECIALIZED_MDNODE_LEAF(CLASS) void visit##CLASS(const CLASS &N);
@@ -3965,6 +3966,45 @@
"dereferenceable_or_null metadata value must be an i64!", &I);
}
+void Verifier::visitProfMetadata(Instruction& I, MDNode* MD) {
+ Assert(MD->getNumOperands() >= 2,
+ "!prof annotations should have no less than 2 operands", MD);
+
+ // Check first operand.
+ Assert(MD->getOperand(0) != nullptr, "first operand should not be null", MD);
+ Assert(isa<MDString>(MD->getOperand(0)),
+ "expected string with name of the !prof annotation", MD);
+ MDString *MDS = cast<MDString>(MD->getOperand(0));
+ StringRef ProfName = MDS->getString();
+
+ // Check consistency of !prof branch_weights metadata.
+ if (ProfName.equals("branch_weights")) {
+ unsigned ExpectedNumOperands = 0;
+ if (BranchInst *BI = dyn_cast<BranchInst>(&I))
+ ExpectedNumOperands = BI->getNumSuccessors();
+ else if (SwitchInst *SI = dyn_cast<SwitchInst>(&I))
+ ExpectedNumOperands = SI->getNumSuccessors();
+ else if (isa<CallInst>(&I) || isa<InvokeInst>(&I))
+ ExpectedNumOperands = 1;
+ else if (IndirectBrInst *IBI = dyn_cast<IndirectBrInst>(&I))
+ ExpectedNumOperands = IBI->getNumDestinations();
+ else if (isa<SelectInst>(&I))
+ ExpectedNumOperands = 2;
+ else
+ CheckFailed(
+ "!prof branch_weights are not allowed for this instruction", MD);
+
+ Assert(MD->getNumOperands() == 1 + ExpectedNumOperands,
+ "Wrong number of operands");
+ for (unsigned i = 1; i < MD->getNumOperands(); ++i) {
+ auto &MDO = MD->getOperand(i);
+ Assert(MDO, "second operand should not be null", MD);
+ Assert(mdconst::dyn_extract<ConstantInt>(MDO),
+ "!prof brunch_weights operand is not a const int");
+ }
+ }
+}
+
/// verifyInstruction - Verify that an instruction is well formed.
///
void Verifier::visitInstruction(Instruction &I) {
@@ -4122,6 +4162,9 @@
"alignment is larger that implementation defined limit", &I);
}
+ if (MDNode *MD = I.getMetadata(LLVMContext::MD_prof))
+ visitProfMetadata(I, MD);
+
if (MDNode *N = I.getDebugLoc().getAsMDNode()) {
AssertDI(isa<DILocation>(N), "invalid !dbg metadata attachment", &I, N);
visitMDNode(*N);
-------------- next part --------------
A non-text attachment was scrubbed...
Name: D61179.196817.patch
Type: text/x-patch
Size: 2772 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20190426/9b5adf66/attachment.bin>
More information about the llvm-commits
mailing list