Skip to content

Commit 093254e

Browse files
committed
[pgo] add means to specify "unknown" MD_prof
1 parent 3cda0f3 commit 093254e

File tree

4 files changed

+73
-1
lines changed

4 files changed

+73
-1
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

llvm/test/Verifier/branch-weight.ll

Lines changed: 36 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,9 @@
44
; RUN: opt -passes=verify %t/valid.ll --disable-output
55
; RUN: not opt -passes=verify %t/invalid1.ll --disable-output 2>&1 | FileCheck %s
66
; RUN: not opt -passes=verify %t/invalid2.ll --disable-output 2>&1 | FileCheck %s
7+
; RUN: opt -passes=verify %t/unknown-correct.ll --disable-output
8+
; RUN: not opt -passes=verify %t/unknown-invalid.ll --disable-output 2>&1 | FileCheck %s
9+
; RUN: not opt -passes=verify %t/unknown-on-function.ll --disable-output 2>&1 | FileCheck %s --check-prefix=ON-FUNCTION
710

811
;--- valid.ll
912
define void @test(i1 %0) {
@@ -36,4 +39,36 @@ define void @test(i1 %0) {
3639

3740
!0 = !{!"function_entry_count", i32 1}
3841

39-
; CHECK: expected either branch_weights or VP profile name
42+
; CHECK: expected either branch_weights or VP profile name
43+
44+
;--- unknown-correct.ll
45+
define void @test(i32 %a) {
46+
%c = icmp eq i32 %a, 0
47+
br i1 %c, label %yes, label %no, !prof !0
48+
yes:
49+
ret void
50+
no:
51+
ret void
52+
}
53+
54+
!0 = !{!"unknown"}
55+
56+
;--- unknown-invalid.ll
57+
define void @test(i32 %a) {
58+
%c = icmp eq i32 %a, 0
59+
br i1 %c, label %yes, label %no, !prof !0
60+
yes:
61+
ret void
62+
no:
63+
ret void
64+
}
65+
66+
!0 = !{!"unknown", i32 12, i32 67}
67+
68+
;--- unknown-on-function.ll
69+
define void @test() !prof !0 {
70+
ret void
71+
}
72+
73+
!0 = !{!"unknown"}
74+
; ON-FUNCTION: first operand should be 'function_entry_count' or 'synthetic_function_entry_count'

0 commit comments

Comments
 (0)