Skip to content

Commit a645e0c

Browse files
committed
[pgo] add means to specify "unknown" MD_prof
1 parent 1464876 commit a645e0c

File tree

4 files changed

+67
-0
lines changed

4 files changed

+67
-0
lines changed

llvm/include/llvm/IR/ProfDataUtils.h

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -133,6 +133,18 @@ LLVM_ABI bool extractProfTotalWeight(const Instruction &I,
133133
LLVM_ABI void setBranchWeights(Instruction &I, ArrayRef<uint32_t> Weights,
134134
bool IsExpected);
135135

136+
/// Specify that the branch weights for this terminator cannot be known at
137+
/// compile time. This should only be called by passes, and never as a default
138+
/// behavior in e.g. MDBuilder. The goal is to use this info to validate passes
139+
/// do not accidentally drop profile info, and this API is called in cases where
140+
/// the pass explicitly cannot provide that info. Defaulting it in would hide
141+
/// bugs where the pass forgets to transfer over or otherwise specify profile
142+
/// info.
143+
LLVM_ABI void setExplicitlyUnknownBranchWeights(Instruction &I);
144+
145+
LLVM_ABI bool isExplicitlyUnknownBranchWeightsMetadata(const MDNode &MD);
146+
LLVM_ABI bool hasExplicitlyUnknownBranchWeights(const Instruction &I);
147+
136148
/// Scaling the profile data attached to 'I' using the ratio of S/T.
137149
LLVM_ABI void scaleProfData(Instruction &I, uint64_t S, uint64_t T);
138150

llvm/lib/IR/ProfDataUtils.cpp

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,8 @@ constexpr unsigned MinBWOps = 3;
4444
// the minimum number of operands for MD_prof nodes with value profiles
4545
constexpr unsigned MinVPOps = 5;
4646

47+
const char *UnknownBranchWeightsMarker = "unknown";
48+
4749
// We may want to add support for other MD_prof types, so provide an abstraction
4850
// for checking the metadata type.
4951
bool isTargetMD(const MDNode *ProfData, const char *Name, unsigned MinOps) {
@@ -232,6 +234,26 @@ bool extractProfTotalWeight(const Instruction &I, uint64_t &TotalVal) {
232234
return extractProfTotalWeight(I.getMetadata(LLVMContext::MD_prof), TotalVal);
233235
}
234236

237+
void setExplicitlyUnknownBranchWeights(Instruction &I) {
238+
MDBuilder MDB(I.getContext());
239+
I.setMetadata(LLVMContext::MD_prof,
240+
MDNode::get(I.getContext(),
241+
MDB.createString(UnknownBranchWeightsMarker)));
242+
}
243+
244+
bool isExplicitlyUnknownBranchWeightsMetadata(const MDNode &MD) {
245+
if (MD.getNumOperands() != 1)
246+
return false;
247+
return MD.getOperand(0).equalsStr(UnknownBranchWeightsMarker);
248+
}
249+
250+
bool hasExplicitlyUnknownBranchWeights(const Instruction &I) {
251+
auto *MD = I.getMetadata(LLVMContext::MD_prof);
252+
if (!MD)
253+
return false;
254+
return isExplicitlyUnknownBranchWeightsMetadata(*MD);
255+
}
256+
235257
void setBranchWeights(Instruction &I, ArrayRef<uint32_t> Weights,
236258
bool IsExpected) {
237259
MDBuilder MDB(I.getContext());

llvm/lib/IR/Verifier.cpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4964,6 +4964,9 @@ void Verifier::visitDereferenceableMetadata(Instruction& I, MDNode* MD) {
49644964
}
49654965

49664966
void Verifier::visitProfMetadata(Instruction &I, MDNode *MD) {
4967+
if (isExplicitlyUnknownBranchWeightsMetadata(*MD))
4968+
return;
4969+
49674970
Check(MD->getNumOperands() >= 2,
49684971
"!prof annotations should have no less than 2 operands", MD);
49694972

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
; Test branch weight unknown validation
2+
3+
; RUN: split-file %s %t
4+
; RUN: opt -passes=verify %t/correct.ll --disable-output
5+
; RUN: not opt -passes=verify %t/incorrect.ll --disable-output
6+
; RUN: not opt -passes=verify %t/on_function.ll --disable-output
7+
8+
;--- correct.ll
9+
define void @correct(i32 %a) {
10+
%c = icmp eq i32 %a, 0
11+
br i1 %c, label %yes, label %no, !prof !0
12+
yes:
13+
ret void
14+
no:
15+
ret void
16+
}
17+
18+
!0 = !{!"unknown"}
19+
20+
;--- incorrect.ll
21+
define void @correct(i32 %a) {
22+
%c = icmp eq i32 %a, 0
23+
br i1 %c, label %yes, label %no, !prof !0
24+
yes:
25+
ret void
26+
no:
27+
ret void
28+
}
29+
30+
!0 = !{!"unknown", i32 12, i32 67}

0 commit comments

Comments
 (0)