Skip to content

Commit 2a99532

Browse files
authored
[profcheck] Allow unknown function entry count (llvm#155918)
Some passes synthesize functions, e.g. WPD, so we may need to indicate “this synthesized function’s entry count cannot be estimated at compile time” - akin to `branch_weights`​. Issue llvm#147390
1 parent 119d507 commit 2a99532

File tree

5 files changed

+28
-14
lines changed

5 files changed

+28
-14
lines changed

llvm/include/llvm/IR/ProfDataUtils.h

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -180,7 +180,11 @@ inline uint32_t scaleBranchCount(uint64_t Count, uint64_t Scale) {
180180
/// info.
181181
LLVM_ABI void setExplicitlyUnknownBranchWeights(Instruction &I);
182182

183-
LLVM_ABI bool isExplicitlyUnknownBranchWeightsMetadata(const MDNode &MD);
183+
/// Analogous to setExplicitlyUnknownBranchWeights, but for functions and their
184+
/// entry counts.
185+
LLVM_ABI void setExplicitlyUnknownFunctionEntryCount(Function &F);
186+
187+
LLVM_ABI bool isExplicitlyUnknownProfileMetadata(const MDNode &MD);
184188
LLVM_ABI bool hasExplicitlyUnknownBranchWeights(const Instruction &I);
185189

186190
/// Scaling the profile data attached to 'I' using the ratio of S/T.

llvm/lib/IR/ProfDataUtils.cpp

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -250,7 +250,15 @@ void setExplicitlyUnknownBranchWeights(Instruction &I) {
250250
MDB.createString(MDProfLabels::UnknownBranchWeightsMarker)));
251251
}
252252

253-
bool isExplicitlyUnknownBranchWeightsMetadata(const MDNode &MD) {
253+
void setExplicitlyUnknownFunctionEntryCount(Function &F) {
254+
MDBuilder MDB(F.getContext());
255+
F.setMetadata(
256+
LLVMContext::MD_prof,
257+
MDNode::get(F.getContext(),
258+
MDB.createString(MDProfLabels::UnknownBranchWeightsMarker)));
259+
}
260+
261+
bool isExplicitlyUnknownProfileMetadata(const MDNode &MD) {
254262
if (MD.getNumOperands() != 1)
255263
return false;
256264
return MD.getOperand(0).equalsStr(MDProfLabels::UnknownBranchWeightsMarker);
@@ -260,7 +268,7 @@ bool hasExplicitlyUnknownBranchWeights(const Instruction &I) {
260268
auto *MD = I.getMetadata(LLVMContext::MD_prof);
261269
if (!MD)
262270
return false;
263-
return isExplicitlyUnknownBranchWeightsMetadata(*MD);
271+
return isExplicitlyUnknownProfileMetadata(*MD);
264272
}
265273

266274
void setBranchWeights(Instruction &I, ArrayRef<uint32_t> Weights,

llvm/lib/IR/Verifier.cpp

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2529,12 +2529,11 @@ void Verifier::verifyFunctionMetadata(
25292529
for (const auto &Pair : MDs) {
25302530
if (Pair.first == LLVMContext::MD_prof) {
25312531
MDNode *MD = Pair.second;
2532-
if (isExplicitlyUnknownBranchWeightsMetadata(*MD)) {
2533-
CheckFailed("'unknown' !prof metadata should appear only on "
2534-
"instructions supporting the 'branch_weights' metadata",
2535-
MD);
2532+
// We may have functions that are synthesized by the compiler, e.g. in
2533+
// WPD, that we can't currently determine the entry count.
2534+
if (isExplicitlyUnknownProfileMetadata(*MD))
25362535
continue;
2537-
}
2536+
25382537
Check(MD->getNumOperands() >= 2,
25392538
"!prof annotations should have no less than 2 operands", MD);
25402539

llvm/lib/Transforms/Utils/ProfileVerify.cpp

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -155,12 +155,15 @@ PreservedAnalyses ProfileVerifierPass::run(Function &F,
155155
FunctionAnalysisManager &FAM) {
156156
const auto EntryCount = F.getEntryCount(/*AllowSynthetic=*/true);
157157
if (!EntryCount) {
158-
F.getContext().emitError("Profile verification failed: function entry "
159-
"count missing (set to 0 if cold)");
158+
auto *MD = F.getMetadata(LLVMContext::MD_prof);
159+
if (!MD || !isExplicitlyUnknownProfileMetadata(*MD)) {
160+
F.getContext().emitError("Profile verification failed: function entry "
161+
"count missing (set to 0 if cold)");
162+
return PreservedAnalyses::all();
163+
}
164+
} else if (EntryCount->getCount() == 0) {
160165
return PreservedAnalyses::all();
161166
}
162-
if (EntryCount->getCount() == 0)
163-
return PreservedAnalyses::all();
164167
for (const auto &BB : F) {
165168
if (AnnotateSelect) {
166169
for (const auto &I : BB)

llvm/test/Verifier/branch-weight.ll

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
; RUN: opt -passes=verify %t/unknown-correct.ll --disable-output
1212

1313
; RUN: not opt -passes=verify %t/unknown-invalid.ll --disable-output 2>&1 | FileCheck %s --check-prefix=EXTRA-ARGS
14-
; RUN: not opt -passes=verify %t/unknown-on-function1.ll --disable-output 2>&1 | FileCheck %s --check-prefix=ON-FUNCTION1
14+
; RUN: opt -passes=verify %t/unknown-on-function1.ll -S -o - | FileCheck %s --check-prefix=ON-FUNCTION1
1515
; RUN: not opt -passes=verify %t/unknown-on-function2.ll --disable-output 2>&1 | FileCheck %s --check-prefix=ON-FUNCTION2
1616
; RUN: not opt -passes=verify %t/invalid-unknown-placement.ll --disable-output 2>&1 | FileCheck %s --check-prefix=INVALID-UNKNOWN-PLACEMENT
1717

@@ -132,7 +132,7 @@ define void @test() !prof !0 {
132132
}
133133

134134
!0 = !{!"unknown"}
135-
; ON-FUNCTION1: 'unknown' !prof metadata should appear only on instructions supporting the 'branch_weights' metadata
135+
; ON-FUNCTION1: define void @test() !prof !0
136136

137137
;--- unknown-on-function2.ll
138138
define void @test() !prof !0 {

0 commit comments

Comments
 (0)