Skip to content

Commit b46b859

Browse files
committed
[PGO][profcheck] ignore explicitly cold functions
1 parent c5459a0 commit b46b859

File tree

5 files changed

+65
-12
lines changed

5 files changed

+65
-12
lines changed

llvm/lib/Transforms/Utils/ProfileVerify.cpp

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,12 @@
2020
#include "llvm/IR/MDBuilder.h"
2121
#include "llvm/IR/ProfDataUtils.h"
2222
#include "llvm/Support/BranchProbability.h"
23+
#include "llvm/Support/CommandLine.h"
2324

2425
using namespace llvm;
26+
static cl::opt<int64_t>
27+
DefaultFunctionEntryCount("profcheck-default-fct-entry-count",
28+
cl::init(1000));
2529
namespace {
2630
class ProfileInjector {
2731
Function &F;
@@ -63,6 +67,17 @@ bool ProfileInjector::inject() {
6367
// will get the same BPI it does if the injector wasn't running.
6468
auto &BPI = FAM.getResult<BranchProbabilityAnalysis>(F);
6569

70+
// Inject a function count if there's none. It's reasonable for a pass to
71+
// want to clear the MD_prof of a function with zero entry count. From a
72+
// metadata economy perspective, if a profile comes empty for a function, it's
73+
// cheaper to assign it the 0-entry count explicitly than to mark every branch
74+
// as cold.
75+
if (!F.getEntryCount(true))
76+
F.setEntryCount(DefaultFunctionEntryCount);
77+
// If there is an entry count that's 0, then don't bother injecting. We won't
78+
// verify these either.
79+
if (F.getEntryCount(true)->getCount() == 0)
80+
return false;
6681
bool Changed = false;
6782
for (auto &BB : F) {
6883
auto *Term = getTerminatorBenefitingFromMDProf(BB);
@@ -119,11 +134,13 @@ PreservedAnalyses ProfileInjectorPass::run(Function &F,
119134

120135
PreservedAnalyses ProfileVerifierPass::run(Function &F,
121136
FunctionAnalysisManager &FAM) {
137+
if (!F.getEntryCount(true) || F.getEntryCount(true)->getCount() == 0)
138+
return PreservedAnalyses::all();
122139
for (const auto &BB : F)
123140
if (const auto *Term =
124141
ProfileInjector::getTerminatorBenefitingFromMDProf(BB))
125142
if (!Term->getMetadata(LLVMContext::MD_prof))
126143
F.getContext().emitError("Profile verification failed");
127144

128-
return PreservedAnalyses::none();
145+
return PreservedAnalyses::all();
129146
}
Lines changed: 23 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
; Test that prof-inject only injects missing metadata
22

3-
; RUN: opt -passes=prof-inject %s -S -o - | FileCheck %s
3+
; RUN: opt -passes=prof-inject -profcheck-default-fct-entry-count=10 %s -S -o - | FileCheck %s
44

55
define void @foo(i32 %i) {
66
%c = icmp eq i32 %i, 0
@@ -13,8 +13,26 @@ no:
1313
ret void
1414
}
1515

16+
define void @cold(i32 %i) !prof !1 {
17+
%c = icmp eq i32 %i, 0
18+
br i1 %c, label %yes, label %no
19+
yes:
20+
br i1 %c, label %yes2, label %no
21+
yes2:
22+
ret void
23+
no:
24+
ret void
25+
}
1626
!0 = !{!"branch_weights", i32 1, i32 2}
17-
; CHECK: br i1 %c, label %yes, label %no, !prof !0
18-
; CHECK: br i1 %c, label %yes2, label %no, !prof !1
19-
; CHECK: !0 = !{!"branch_weights", i32 1, i32 2}
20-
; CHECK: !1 = !{!"branch_weights", i32 3, i32 5}
27+
!1 = !{!"function_entry_count", i32 0}
28+
29+
; CHECK-LABEL: @foo
30+
; CHECK: br i1 %c, label %yes, label %no, !prof !1
31+
; CHECK: br i1 %c, label %yes2, label %no, !prof !2
32+
; CHECK-LABEL: @cold
33+
; CHECK: br i1 %c, label %yes, label %no{{$}}
34+
; CHECK: br i1 %c, label %yes2, label %no{{$}}
35+
; CHECK: !0 = !{!"function_entry_count", i64 10}
36+
; CHECK: !1 = !{!"branch_weights", i32 1, i32 2}
37+
; CHECK: !2 = !{!"branch_weights", i32 3, i32 5}
38+
; CHECK: !3 = !{!"function_entry_count", i32 0}

llvm/test/Transforms/PGOProfile/prof-verify-existing.ll

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@ no:
1616

1717
!0 = !{!"branch_weights", i32 1, i32 2}
1818
!1 = !{!"unknown"}
19-
; CHECK: br i1 %c, label %yes, label %no, !prof !0
20-
; CHECK: !0 = !{!"branch_weights", i32 1, i32 2}
21-
; CHECK: !1 = !{!"unknown"}
19+
; CHECK: define void @foo(i32 %i) !prof !0
20+
; CHECK: br i1 %c, label %yes, label %no, !prof !1
21+
; CHECK: !0 = !{!"function_entry_count", i64 1000}
22+
; CHECK: !1 = !{!"branch_weights", i32 1, i32 2}
23+
; CHECK: !2 = !{!"unknown"}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
; Test prof-verify for functions explicitly marked as cold
2+
3+
; RUN: opt -passes=prof-inject,prof-verify %s -o - 2>&1 | FileCheck %s
4+
5+
define void @foo(i32 %i) !prof !0 {
6+
%c = icmp eq i32 %i, 0
7+
br i1 %c, label %yes, label %no
8+
yes:
9+
ret void
10+
no:
11+
ret void
12+
}
13+
!0 = !{!"function_entry_count", i32 0}
14+
15+
; CHECK-NOT: Profile verification failed

llvm/test/Transforms/PGOProfile/prof-verify.ll

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,16 +5,17 @@
55
; RUN: opt -passes=prof-inject,prof-verify %s --disable-output
66
; RUN: opt -enable-profcheck %s -S -o - | FileCheck %s --check-prefix=INJECT
77

8-
define void @foo(i32 %i) {
8+
define void @foo(i32 %i) !prof !0 {
99
%c = icmp eq i32 %i, 0
1010
br i1 %c, label %yes, label %no
1111
yes:
1212
ret void
1313
no:
1414
ret void
1515
}
16+
!0 = !{!"function_entry_count", i32 1}
1617

17-
; INJECT: br i1 %c, label %yes, label %no, !prof !0
18-
; INJECT: !0 = !{!"branch_weights", i32 3, i32 5}
18+
; INJECT: br i1 %c, label %yes, label %no, !prof !1
19+
; INJECT: !1 = !{!"branch_weights", i32 3, i32 5}
1920

2021
; VERIFY: Profile verification failed

0 commit comments

Comments
 (0)