Skip to content

Commit d8bab13

Browse files
committed
[Profiler] Fix counters following DoStmts
Previously we weren't compensating for label jumps and returns, i.e we assumed the exit count is the same as the entry count. Ensure we follow the same logic that other labeled statements follow such that control flow is accounted for. rdar://98881045
1 parent 92a8fbd commit d8bab13

File tree

2 files changed

+71
-2
lines changed

2 files changed

+71
-2
lines changed

lib/SIL/IR/SILProfiler.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -784,7 +784,7 @@ struct CoverageMapping : public ASTWalker {
784784
CounterExpr *JumpsToLabel = nullptr;
785785
Stmt *ParentStmt = Parent.getAsStmt();
786786
if (ParentStmt) {
787-
if (isa<DoStmt>(ParentStmt) || isa<DoCatchStmt>(ParentStmt))
787+
if (isa<DoCatchStmt>(ParentStmt))
788788
return;
789789
auto caseStmt = dyn_cast_or_null<CaseStmt>(ParentStmt);
790790
if (caseStmt && caseStmt->getParentKind() == CaseParentKind::DoCatch)
@@ -999,8 +999,8 @@ struct CoverageMapping : public ASTWalker {
999999
if (caseStmt->getParentKind() == CaseParentKind::Switch)
10001000
pushRegion(S);
10011001
} else if (auto *DS = dyn_cast<DoStmt>(S)) {
1002+
assignCounter(DS, CounterExpr::Zero());
10021003
assignCounter(DS->getBody(), CounterExpr::Ref(getCurrentCounter()));
1003-
assignCounter(DS);
10041004

10051005
} else if (auto *DCS = dyn_cast<DoCatchStmt>(S)) {
10061006
// The do-catch body is visited the same number of times as its parent.

test/Profiler/coverage_do.swift

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
// RUN: %target-swift-frontend -Xllvm -sil-full-demangle -suppress-warnings -profile-generate -profile-coverage-mapping -emit-sorted-sil -emit-sil -module-name coverage_do %s | %FileCheck %s
2+
// RUN: %target-swift-frontend -profile-generate -profile-coverage-mapping -emit-ir %s
3+
4+
// CHECK-LABEL: sil hidden @$s11coverage_do3fooyyF : $@convention(thin) () -> ()
5+
6+
// CHECK: string_literal
7+
// CHECK-NEXT: integer_literal $Builtin.Int64, 0
8+
// CHECK-NEXT: integer_literal $Builtin.Int32, 3
9+
// CHECK-NEXT: integer_literal $Builtin.Int32, 0
10+
// CHECK-NEXT: int_instrprof_increment
11+
// CHECK: function_ref @$sSb6randomSbyFZ
12+
// CHECK: cond_br {{%[0-9]+}}, [[EXITBB:bb[0-9]]], [[BB1:bb[0-9]]]
13+
14+
// CHECK: [[BB1]]
15+
// CHECK: function_ref @$sSb6randomSbyFZ
16+
// CHECK: cond_br {{%[0-9]+}}, [[BRKBB:bb[0-9]]], {{bb[0-9]}}
17+
18+
// CHECK: [[BRKBB]]
19+
// CHECK-NEXT: string_literal
20+
// CHECK-NEXT: integer_literal $Builtin.Int64, 0
21+
// CHECK-NEXT: integer_literal $Builtin.Int32, 3
22+
// CHECK-NEXT: integer_literal $Builtin.Int32, 2
23+
24+
// CHECK: [[EXITBB]]
25+
// CHECK-NEXT: string_literal
26+
// CHECK-NEXT: integer_literal $Builtin.Int64, 0
27+
// CHECK-NEXT: integer_literal $Builtin.Int32, 3
28+
// CHECK-NEXT: integer_literal $Builtin.Int32, 1
29+
30+
// CHECK-LABEL: sil_coverage_map {{.*}} "$s11coverage_do3fooyyF"
31+
// CHECK-NEXT: [[@LINE+11]]:12 -> [[@LINE+18]]:2 : 0
32+
// CHECK-NEXT: [[@LINE+11]]:9 -> [[@LINE+15]]:4 : 0
33+
// CHECK-NEXT: [[@LINE+11]]:8 -> [[@LINE+11]]:17 : 0
34+
// CHECK-NEXT: [[@LINE+10]]:18 -> [[@LINE+10]]:28 : 1
35+
// CHECK-NEXT: [[@LINE+9]]:28 -> [[@LINE+12]]:4 : (0 - 1)
36+
// CHECK-NEXT: [[@LINE+9]]:8 -> [[@LINE+9]]:17 : (0 - 1)
37+
// CHECK-NEXT: [[@LINE+8]]:18 -> [[@LINE+8]]:29 : 2
38+
// CHECK-NEXT: [[@LINE+7]]:29 -> [[@LINE+8]]:11 : ((0 - 1) - 2)
39+
// CHECK-NEXT: [[@LINE+8]]:4 -> [[@LINE+10]]:2 : 2
40+
// CHECK-NEXT: [[@LINE+8]]:6 -> [[@LINE+8]]:8 : 2
41+
// CHECK-NEXT: [[@LINE+7]]:8 -> [[@LINE+8]]:2 : 2
42+
func foo() {
43+
x: do {
44+
if .random() { return }
45+
if .random() { break x }
46+
return
47+
}
48+
do {}
49+
}
50+
// CHECK-NEXT: }
51+
52+
// CHECK-LABEL: sil_coverage_map {{.*}} "$s11coverage_do4foobyyF"
53+
func foob() {
54+
x: do {
55+
do {
56+
if .random() { return }
57+
// CHECK: [[@LINE+1]]:6 -> [[@LINE+10]]:4 : (0 - 1)
58+
}
59+
do {
60+
if .random() { break x }
61+
// CHECK: [[@LINE+1]]:6 -> [[@LINE+6]]:4 : ((0 - 1) - 2)
62+
}
63+
do {
64+
return
65+
// CHECK-NOT: zero
66+
}
67+
}
68+
do {}
69+
}

0 commit comments

Comments
 (0)