Skip to content

Commit b0154c3

Browse files
authored
[InstrProf] Add pgo use block coverage test (#72443)
Back in https://reviews.llvm.org/D124490 we added a block coverage mode that instruments a subset of basic blocks using single byte counters to get coverage for the whole function. This commit adds a test to make sure that we correctly assign branch weights based on the coverage profile. I noticed this test was missing after seeing that we had no coverage on `PGOUseFunc::populateCoverage()` https://lab.llvm.org/coverage/coverage-reports/coverage/Users/buildslave/jenkins/workspace/coverage/llvm-project/llvm/lib/Transforms/Instrumentation/PGOInstrumentation.cpp.html#L1383
1 parent 2fdf283 commit b0154c3

File tree

4 files changed

+86
-6
lines changed

4 files changed

+86
-6
lines changed

llvm/lib/ProfileData/InstrProfReader.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -271,6 +271,8 @@ Error TextInstrProfReader::readHeader() {
271271
ProfileKind |= InstrProfKind::FunctionEntryInstrumentation;
272272
else if (Str.equals_insensitive("not_entry_first"))
273273
ProfileKind &= ~InstrProfKind::FunctionEntryInstrumentation;
274+
else if (Str.equals_insensitive("single_byte_coverage"))
275+
ProfileKind |= InstrProfKind::SingleByteCoverage;
274276
else if (Str.equals_insensitive("temporal_prof_traces")) {
275277
ProfileKind |= InstrProfKind::TemporalProfile;
276278
if (auto Err = readTemporalProfTraceData())

llvm/lib/ProfileData/InstrProfWriter.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -762,6 +762,8 @@ Error InstrProfWriter::writeText(raw_fd_ostream &OS) {
762762
if (static_cast<bool>(ProfileKind &
763763
InstrProfKind::FunctionEntryInstrumentation))
764764
OS << "# Always instrument the function entry block\n:entry_first\n";
765+
if (static_cast<bool>(ProfileKind & InstrProfKind::SingleByteCoverage))
766+
OS << "# Instrument block coverage\n:single_byte_coverage\n";
765767
InstrProfSymtab Symtab;
766768

767769
using FuncPair = detail::DenseMapPair<uint64_t, InstrProfRecord>;
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
:ir
2+
:single_byte_coverage
3+
4+
foo
5+
# Func Hash:
6+
848064302753700500
7+
# Num Counters:
8+
2
9+
# Counter Values:
10+
3
11+
4
12+
13+
14+
bar
15+
# Func Hash:
16+
848064302952419074
17+
# Num Counters:
18+
2
19+
# Counter Values:
20+
2
21+
0
22+
23+
24+
goo
25+
# Func Hash:
26+
1106497858086895615
27+
# Num Counters:
28+
1
29+
# Counter Values:
30+
5
31+
32+
33+
loop
34+
# Func Hash:
35+
92940490389974880
36+
# Num Counters:
37+
2
38+
# Counter Values:
39+
1
40+
1
41+
42+
43+
hoo
44+
# Func Hash:
45+
1073332642652768409
46+
# Num Counters:
47+
6
48+
# Counter Values:
49+
1
50+
0
51+
1
52+
1
53+
0
54+
0

llvm/test/Transforms/PGOProfile/coverage.ll

Lines changed: 28 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,20 @@
1-
; RUN: opt < %s -passes=pgo-instr-gen -pgo-function-entry-coverage -S | FileCheck %s --implicit-check-not="instrprof.cover" --check-prefixes=CHECK,ENTRY
2-
; RUN: opt < %s -passes=pgo-instr-gen -pgo-block-coverage -S | FileCheck %s --implicit-check-not="instrprof.cover" --check-prefixes=CHECK,BLOCK
1+
; RUN: opt < %s -passes=pgo-instr-gen -pgo-function-entry-coverage -S | FileCheck %s --implicit-check-not="instrprof.cover" --check-prefixes=CHECK,GEN,ENTRY
2+
; RUN: opt < %s -passes=pgo-instr-gen -pgo-block-coverage -S | FileCheck %s --implicit-check-not="instrprof.cover" --check-prefixes=CHECK,GEN,BLOCK
3+
4+
; RUN: llvm-profdata merge %S/Inputs/coverage.proftext -o %t.profdata
5+
; RUN: opt < %s -passes=pgo-instr-use -pgo-test-profile-file=%t.profdata -S | FileCheck %s --check-prefixes=CHECK,USE
36
target datalayout = "e-m:e-p270:32:32-p271:32:32-p272:64:64-i64:64-f80:128-n8:16:32:64-S128"
47
target triple = "x86_64-unknown-linux-gnu"
58

9+
; CHECK-LABEL: @foo()
10+
; USE-SAME: !prof ![[HOT:[0-9]+]]
611
define void @foo() {
712
; CHECK-LABEL: entry:
813
entry:
914
; ENTRY: call void @llvm.instrprof.cover({{.*}})
1015
%c = call i1 @choice()
1116
br i1 %c, label %if.then, label %if.else
17+
; USE: br i1 %c, label %if.then, label %if.else, !prof ![[WEIGHTS0:[0-9]+]]
1218

1319
; CHECK-LABEL: if.then:
1420
if.then:
@@ -25,12 +31,15 @@ if.end:
2531
ret void
2632
}
2733

34+
; CHECK-LABEL: @bar()
35+
; USE-SAME: !prof ![[HOT:[0-9]+]]
2836
define void @bar() {
2937
; CHECK-LABEL: entry:
3038
entry:
3139
; ENTRY: call void @llvm.instrprof.cover({{.*}})
3240
%c = call i1 @choice()
3341
br i1 %c, label %if.then, label %if.end
42+
; USE: br i1 %c, label %if.then, label %if.end, !prof ![[WEIGHTS1:[0-9]+]]
3443

3544
; CHECK-LABEL: if.then:
3645
if.then:
@@ -43,24 +52,29 @@ if.end:
4352
ret void
4453
}
4554

55+
; CHECK-LABEL: @goo()
56+
; USE-SAME: !prof ![[HOT:[0-9]+]]
4657
define void @goo() {
4758
; CHECK-LABEL: entry:
4859
entry:
49-
; CHECK: call void @llvm.instrprof.cover({{.*}})
60+
; GEN: call void @llvm.instrprof.cover({{.*}})
5061
ret void
5162
}
5263

64+
; CHECK-LABEL: @loop()
65+
; USE-SAME: !prof ![[HOT:[0-9]+]]
5366
define void @loop() {
5467
; CHECK-LABEL: entry:
5568
entry:
56-
; CHECK: call void @llvm.instrprof.cover({{.*}})
69+
; GEN: call void @llvm.instrprof.cover({{.*}})
5770
br label %while
5871
while:
5972
; BLOCK: call void @llvm.instrprof.cover({{.*}})
6073
br label %while
6174
}
6275

63-
; Function Attrs: noinline nounwind ssp uwtable
76+
; CHECK-LABEL: @hoo(
77+
; USE-SAME: !prof ![[HOT:[0-9]+]]
6478
define void @hoo(i32 %a) #0 {
6579
; CHECK-LABEL: entry:
6680
entry:
@@ -72,6 +86,7 @@ entry:
7286
%rem = srem i32 %0, 2
7387
%cmp = icmp eq i32 %rem, 0
7488
br i1 %cmp, label %if.then, label %if.else
89+
; USE: br i1 %cmp, label %if.then, label %if.else, !prof ![[WEIGHTS1]]
7590

7691
; CHECK-LABEL: if.then:
7792
if.then: ; preds = %entry
@@ -94,13 +109,15 @@ for.cond: ; preds = %for.inc, %if.end
94109
%2 = load i32, i32* %a.addr, align 4
95110
%cmp1 = icmp slt i32 %1, %2
96111
br i1 %cmp1, label %for.body, label %for.end
112+
; USE: br i1 %cmp1, label %for.body, label %for.end, !prof ![[WEIGHTS1]]
97113

98114
; CHECK-LABEL: for.body:
99115
for.body: ; preds = %for.cond
100116
%3 = load i32, i32* %a.addr, align 4
101117
%rem2 = srem i32 %3, 3
102118
%cmp3 = icmp eq i32 %rem2, 0
103119
br i1 %cmp3, label %if.then4, label %if.else5
120+
; USE: br i1 %cmp3, label %if.then4, label %if.else5, !prof ![[WEIGHTS0]]
104121

105122
; CHECK-LABEL: if.then4:
106123
if.then4: ; preds = %for.body
@@ -113,6 +130,7 @@ if.else5: ; preds = %for.body
113130
%rem6 = srem i32 %4, 1001
114131
%cmp7 = icmp eq i32 %rem6, 0
115132
br i1 %cmp7, label %if.then8, label %if.end9
133+
; USE: br i1 %cmp7, label %if.then8, label %if.end9, !prof ![[WEIGHTS1]]
116134

117135
; CHECK-LABEL: if.then8:
118136
if.then8: ; preds = %if.else5
@@ -147,4 +165,8 @@ return: ; preds = %for.end, %if.then8
147165

148166
declare i1 @choice()
149167

150-
; CHECK: declare void @llvm.instrprof.cover({{.*}})
168+
; GEN: declare void @llvm.instrprof.cover({{.*}})
169+
170+
; USE-DAG: ![[HOT]] = !{!"function_entry_count", i64 10000}
171+
; USE-DAG: ![[WEIGHTS0]] = !{!"branch_weights", i32 1, i32 1}
172+
; USE-DAG: ![[WEIGHTS1]] = !{!"branch_weights", i32 1, i32 0}

0 commit comments

Comments
 (0)