Skip to content

Commit caf5972

Browse files
authored
Merge pull request #60645 from hamishknight/simon-says
2 parents 5065c8f + d8bab13 commit caf5972

File tree

2 files changed

+74
-4
lines changed

2 files changed

+74
-4
lines changed

lib/SIL/IR/SILProfiler.cpp

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -402,8 +402,9 @@ class CounterExpr {
402402
expand(llvm::coverage::CounterExpressionBuilder &Builder,
403403
const llvm::DenseMap<ASTNode, unsigned> &Counters) const {
404404
return expand(Builder, [&](auto Node) {
405-
// FIXME: We ought to assert that the node is present.
406-
return Counters.lookup(Node);
405+
auto Result = Counters.find(Node);
406+
assert(Result != Counters.end() && "Counter not found");
407+
return Result->second;
407408
});
408409
}
409410

@@ -799,7 +800,7 @@ struct CoverageMapping : public ASTWalker {
799800
CounterExpr *JumpsToLabel = nullptr;
800801
Stmt *ParentStmt = Parent.getAsStmt();
801802
if (ParentStmt) {
802-
if (isa<DoStmt>(ParentStmt) || isa<DoCatchStmt>(ParentStmt))
803+
if (isa<DoCatchStmt>(ParentStmt))
803804
return;
804805
auto caseStmt = dyn_cast_or_null<CaseStmt>(ParentStmt);
805806
if (caseStmt && caseStmt->getParentKind() == CaseParentKind::DoCatch)
@@ -1020,8 +1021,8 @@ struct CoverageMapping : public ASTWalker {
10201021
if (caseStmt->getParentKind() == CaseParentKind::Switch)
10211022
pushRegion(S);
10221023
} else if (auto *DS = dyn_cast<DoStmt>(S)) {
1024+
assignCounter(DS, CounterExpr::Zero());
10231025
assignCounter(DS->getBody(), CounterExpr::Ref(getCurrentCounter()));
1024-
assignCounter(DS);
10251026

10261027
} else if (auto *DCS = dyn_cast<DoCatchStmt>(S)) {
10271028
// 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)