Skip to content

Commit adab88c

Browse files
committed
[pgo] add means to specify "unknown" MD_prof
1 parent 62f8281 commit adab88c

File tree

4 files changed

+186
-22
lines changed

4 files changed

+186
-22
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: 32 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -2526,6 +2526,12 @@ void Verifier::verifyFunctionMetadata(
25262526
for (const auto &Pair : MDs) {
25272527
if (Pair.first == LLVMContext::MD_prof) {
25282528
MDNode *MD = Pair.second;
2529+
if (isExplicitlyUnknownBranchWeightsMetadata(*MD)) {
2530+
CheckFailed("'unknown' !prof metadata should appear only on "
2531+
"instructions supporting the 'branch_weights' metadata",
2532+
MD);
2533+
continue;
2534+
}
25292535
Check(MD->getNumOperands() >= 2,
25302536
"!prof annotations should have no less than 2 operands", MD);
25312537

@@ -4982,6 +4988,30 @@ void Verifier::visitDereferenceableMetadata(Instruction& I, MDNode* MD) {
49824988
}
49834989

49844990
void Verifier::visitProfMetadata(Instruction &I, MDNode *MD) {
4991+
auto GetBranchingTerminatorNumOperands = [&]() {
4992+
unsigned ExpectedNumOperands = 0;
4993+
if (BranchInst *BI = dyn_cast<BranchInst>(&I))
4994+
ExpectedNumOperands = BI->getNumSuccessors();
4995+
else if (SwitchInst *SI = dyn_cast<SwitchInst>(&I))
4996+
ExpectedNumOperands = SI->getNumSuccessors();
4997+
else if (isa<CallInst>(&I))
4998+
ExpectedNumOperands = 1;
4999+
else if (IndirectBrInst *IBI = dyn_cast<IndirectBrInst>(&I))
5000+
ExpectedNumOperands = IBI->getNumDestinations();
5001+
else if (isa<SelectInst>(&I))
5002+
ExpectedNumOperands = 2;
5003+
else if (CallBrInst *CI = dyn_cast<CallBrInst>(&I))
5004+
ExpectedNumOperands = CI->getNumSuccessors();
5005+
return ExpectedNumOperands;
5006+
};
5007+
if (isExplicitlyUnknownBranchWeightsMetadata(*MD)) {
5008+
Check(GetBranchingTerminatorNumOperands() != 0 || isa<InvokeInst>(I),
5009+
"'unknown' !prof should only appear on instructions on which "
5010+
"'branch_weights' would",
5011+
MD);
5012+
return;
5013+
}
5014+
49855015
Check(MD->getNumOperands() >= 2,
49865016
"!prof annotations should have no less than 2 operands", MD);
49875017

@@ -4999,20 +5029,8 @@ void Verifier::visitProfMetadata(Instruction &I, MDNode *MD) {
49995029
Check(NumBranchWeights == 1 || NumBranchWeights == 2,
50005030
"Wrong number of InvokeInst branch_weights operands", MD);
50015031
} else {
5002-
unsigned ExpectedNumOperands = 0;
5003-
if (BranchInst *BI = dyn_cast<BranchInst>(&I))
5004-
ExpectedNumOperands = BI->getNumSuccessors();
5005-
else if (SwitchInst *SI = dyn_cast<SwitchInst>(&I))
5006-
ExpectedNumOperands = SI->getNumSuccessors();
5007-
else if (isa<CallInst>(&I))
5008-
ExpectedNumOperands = 1;
5009-
else if (IndirectBrInst *IBI = dyn_cast<IndirectBrInst>(&I))
5010-
ExpectedNumOperands = IBI->getNumDestinations();
5011-
else if (isa<SelectInst>(&I))
5012-
ExpectedNumOperands = 2;
5013-
else if (CallBrInst *CI = dyn_cast<CallBrInst>(&I))
5014-
ExpectedNumOperands = CI->getNumSuccessors();
5015-
else
5032+
const unsigned ExpectedNumOperands = GetBranchingTerminatorNumOperands();
5033+
if (ExpectedNumOperands == 0)
50165034
CheckFailed("!prof branch_weights are not allowed for this instruction",
50175035
MD);
50185036

llvm/test/Verifier/branch-weight.ll

Lines changed: 120 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,65 @@
11
; Test MD_prof validation
22

33
; RUN: split-file %s %t
4+
45
; RUN: opt -passes=verify %t/valid.ll --disable-output
5-
; RUN: not opt -passes=verify %t/invalid1.ll --disable-output 2>&1 | FileCheck %s
6-
; RUN: not opt -passes=verify %t/invalid2.ll --disable-output 2>&1 | FileCheck %s
6+
7+
; RUN: not opt -passes=verify %t/wrong-count.ll --disable-output 2>&1 | FileCheck %s --check-prefix=WRONG-COUNT
8+
; RUN: not opt -passes=verify %t/invalid-name1.ll --disable-output 2>&1 | FileCheck %s
9+
; RUN: not opt -passes=verify %t/invalid-name2.ll --disable-output 2>&1 | FileCheck %s
10+
11+
; RUN: opt -passes=verify %t/unknown-correct.ll --disable-output
12+
13+
; RUN: not opt -passes=verify %t/unknown-invalid.ll --disable-output 2>&1 | FileCheck %s
14+
; RUN: not opt -passes=verify %t/unknown-on-function1.ll --disable-output 2>&1 | FileCheck %s --check-prefix=ON-FUNCTION1
15+
; RUN: not opt -passes=verify %t/unknown-on-function2.ll --disable-output 2>&1 | FileCheck %s --check-prefix=ON-FUNCTION2
16+
; RUN: not opt -passes=verify %t/invalid-unknown-placement.ll --disable-output 2>&1 | FileCheck %s --check-prefix=INVALID-UNKNOWN-PLACEMENT
717

818
;--- valid.ll
9-
define void @test(i1 %0) {
10-
br i1 %0, label %2, label %3, !prof !0
11-
2:
19+
declare void @to_invoke()
20+
declare i32 @__gxx_personality_v0(...)
21+
22+
define void @invoker() personality ptr @__gxx_personality_v0 {
23+
invoke void @to_invoke() to label %exit unwind label %lpad, !prof !0
24+
lpad:
25+
%ll = landingpad {ptr, i32}
26+
cleanup
1227
ret void
13-
3:
28+
exit:
1429
ret void
1530
}
31+
32+
define i32 @test(i32 %a) {
33+
%c = icmp eq i32 %a, 0
34+
br i1 %c, label %yes, label %exit, !prof !0
35+
yes:
36+
switch i32 %a, label %exit [ i32 1, label %case_b
37+
i32 2, label %case_c], !prof !1
38+
case_b:
39+
br label %exit
40+
case_c:
41+
br label %exit
42+
exit:
43+
%r = select i1 %c, i32 1, i32 2, !prof !0
44+
ret i32 %r
45+
}
1646
!0 = !{!"branch_weights", i32 1, i32 2}
47+
!1 = !{!"branch_weights", i32 1, i32 2, i32 3}
48+
49+
;--- wrong-count.ll
50+
define void @test(i32 %a) {
51+
%c = icmp eq i32 %a, 0
52+
br i1 %c, label %yes, label %no, !prof !0
53+
yes:
54+
ret void
55+
no:
56+
ret void
57+
}
58+
!0 = !{!"branch_weights", i32 1, i32 2, i32 3}
1759

18-
;--- invalid1.ll
60+
; WRONG-COUNT: Wrong number of operands
61+
62+
;--- invalid-name1.ll
1963
define void @test(i1 %0) {
2064
br i1 %0, label %2, label %3, !prof !0
2165
2:
@@ -25,7 +69,7 @@ define void @test(i1 %0) {
2569
}
2670
!0 = !{!"invalid", i32 1, i32 2}
2771

28-
;--- invalid2.ll
72+
;--- invalid-name2.ll
2973
define void @test(i1 %0) {
3074
br i1 %0, label %2, label %3, !prof !0
3175
2:
@@ -37,3 +81,71 @@ define void @test(i1 %0) {
3781
!0 = !{!"function_entry_count", i32 1}
3882

3983
; CHECK: expected either branch_weights or VP profile name
84+
85+
;--- unknown-correct.ll
86+
declare void @to_invoke()
87+
declare i32 @__gxx_personality_v0(...)
88+
89+
define void @invoker() personality ptr @__gxx_personality_v0 {
90+
invoke void @to_invoke() to label %exit unwind label %lpad, !prof !0
91+
lpad:
92+
%ll = landingpad {ptr, i32}
93+
cleanup
94+
ret void
95+
exit:
96+
ret void
97+
}
98+
99+
define i32 @test(i32 %a) {
100+
%c = icmp eq i32 %a, 0
101+
br i1 %c, label %yes, label %exit, !prof !0
102+
yes:
103+
switch i32 %a, label %exit [ i32 1, label %case_b
104+
i32 2, label %case_c], !prof !0
105+
case_b:
106+
br label %exit
107+
case_c:
108+
br label %exit
109+
exit:
110+
%r = select i1 %c, i32 1, i32 2, !prof !0
111+
ret i32 %r
112+
}
113+
114+
!0 = !{!"unknown"}
115+
116+
;--- unknown-invalid.ll
117+
define void @test(i32 %a) {
118+
%c = icmp eq i32 %a, 0
119+
br i1 %c, label %yes, label %no, !prof !0
120+
yes:
121+
ret void
122+
no:
123+
ret void
124+
}
125+
126+
!0 = !{!"unknown", i32 12, i32 67}
127+
128+
;--- unknown-on-function1.ll
129+
define void @test() !prof !0 {
130+
ret void
131+
}
132+
133+
!0 = !{!"unknown"}
134+
; ON-FUNCTION1: 'unknown' !prof metadata should appear only on instructions supporting the 'branch_weights' metadata
135+
136+
;--- unknown-on-function2.ll
137+
define void @test() !prof !0 {
138+
ret void
139+
}
140+
141+
!0 = !{!"unknown", i64 123}
142+
; ON-FUNCTION2: first operand should be 'function_entry_count' or 'synthetic_function_entry_count'
143+
144+
;--- invalid-unknown-placement.ll
145+
define i32 @test() {
146+
%r = add i32 1, 2, !prof !0
147+
ret i32 %r
148+
}
149+
!0 = !{!"unknown"}
150+
151+
; INVALID-UNKNOWN-PLACEMENT: 'unknown' !prof should only appear on instructions on which 'branch_weigths' would

0 commit comments

Comments
 (0)