Skip to content

Commit 1b048ff

Browse files
committed
[pgo] add means to specify "unknown" MD_prof
1 parent 3c77cbc commit 1b048ff

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
@@ -2508,6 +2508,12 @@ void Verifier::verifyFunctionMetadata(
25082508
for (const auto &Pair : MDs) {
25092509
if (Pair.first == LLVMContext::MD_prof) {
25102510
MDNode *MD = Pair.second;
2511+
if (isExplicitlyUnknownBranchWeightsMetadata(*MD)) {
2512+
CheckFailed("'unknown' !prof metadata should appear only on "
2513+
"instructions supporting the 'branch_weights' metadata",
2514+
MD);
2515+
continue;
2516+
}
25112517
Check(MD->getNumOperands() >= 2,
25122518
"!prof annotations should have no less than 2 operands", MD);
25132519

@@ -4964,6 +4970,30 @@ void Verifier::visitDereferenceableMetadata(Instruction& I, MDNode* MD) {
49644970
}
49654971

49664972
void Verifier::visitProfMetadata(Instruction &I, MDNode *MD) {
4973+
auto GetBranchingTerminatorNumOperands = [&]() {
4974+
unsigned ExpectedNumOperands = 0;
4975+
if (BranchInst *BI = dyn_cast<BranchInst>(&I))
4976+
ExpectedNumOperands = BI->getNumSuccessors();
4977+
else if (SwitchInst *SI = dyn_cast<SwitchInst>(&I))
4978+
ExpectedNumOperands = SI->getNumSuccessors();
4979+
else if (isa<CallInst>(&I))
4980+
ExpectedNumOperands = 1;
4981+
else if (IndirectBrInst *IBI = dyn_cast<IndirectBrInst>(&I))
4982+
ExpectedNumOperands = IBI->getNumDestinations();
4983+
else if (isa<SelectInst>(&I))
4984+
ExpectedNumOperands = 2;
4985+
else if (CallBrInst *CI = dyn_cast<CallBrInst>(&I))
4986+
ExpectedNumOperands = CI->getNumSuccessors();
4987+
return ExpectedNumOperands;
4988+
};
4989+
if (isExplicitlyUnknownBranchWeightsMetadata(*MD)) {
4990+
Check(GetBranchingTerminatorNumOperands() != 0 || isa<InvokeInst>(I),
4991+
"'unknown' !prof should only appear on instructions on which "
4992+
"'branch_weights' would",
4993+
MD);
4994+
return;
4995+
}
4996+
49674997
Check(MD->getNumOperands() >= 2,
49684998
"!prof annotations should have no less than 2 operands", MD);
49694999

@@ -4981,20 +5011,8 @@ void Verifier::visitProfMetadata(Instruction &I, MDNode *MD) {
49815011
Check(NumBranchWeights == 1 || NumBranchWeights == 2,
49825012
"Wrong number of InvokeInst branch_weights operands", MD);
49835013
} else {
4984-
unsigned ExpectedNumOperands = 0;
4985-
if (BranchInst *BI = dyn_cast<BranchInst>(&I))
4986-
ExpectedNumOperands = BI->getNumSuccessors();
4987-
else if (SwitchInst *SI = dyn_cast<SwitchInst>(&I))
4988-
ExpectedNumOperands = SI->getNumSuccessors();
4989-
else if (isa<CallInst>(&I))
4990-
ExpectedNumOperands = 1;
4991-
else if (IndirectBrInst *IBI = dyn_cast<IndirectBrInst>(&I))
4992-
ExpectedNumOperands = IBI->getNumDestinations();
4993-
else if (isa<SelectInst>(&I))
4994-
ExpectedNumOperands = 2;
4995-
else if (CallBrInst *CI = dyn_cast<CallBrInst>(&I))
4996-
ExpectedNumOperands = CI->getNumSuccessors();
4997-
else
5014+
const unsigned ExpectedNumOperands = GetBranchingTerminatorNumOperands();
5015+
if (ExpectedNumOperands == 0)
49985016
CheckFailed("!prof branch_weights are not allowed for this instruction",
49995017
MD);
50005018

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)