Skip to content

Commit 19e1e2f

Browse files
committed
[SILGen] Fix deinit coverage mapping
Stop profiling the deallocating deinitializer function for non-ObjC classes, and instead profile the destructor, which is where we emit the user's code written in a `deinit`. rdar://54443107
1 parent 2f38f83 commit 19e1e2f

File tree

4 files changed

+32
-4
lines changed

4 files changed

+32
-4
lines changed

lib/SILGen/SILGen.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -1079,6 +1079,7 @@ void SILGenModule::emitFunctionDefinition(SILDeclRef constant, SILFunction *f) {
10791079
auto *dd = cast<DestructorDecl>(constant.getDecl());
10801080
preEmitFunction(constant, f, dd);
10811081
PrettyStackTraceSILFunction X("silgen emitDestroyingDestructor", f);
1082+
f->createProfiler(dd, constant, ForDefinition);
10821083
SILGenFunction(*this, *f, dd).emitDestroyingDestructor(dd);
10831084
postEmitFunction(constant, f);
10841085
return;
@@ -1100,7 +1101,6 @@ void SILGenModule::emitFunctionDefinition(SILDeclRef constant, SILFunction *f) {
11001101
auto loc = RegularLocation::getAutoGeneratedLocation(dd);
11011102
preEmitFunction(constant, f, loc);
11021103
PrettyStackTraceSILFunction X("silgen emitDeallocatingDestructor", f);
1103-
f->createProfiler(dd, constant, ForDefinition);
11041104
SILGenFunction(*this, *f, dd).emitDeallocatingDestructor(dd);
11051105
postEmitFunction(constant, f);
11061106
return;

lib/SILGen/SILGenDestructor.cpp

-2
Original file line numberDiff line numberDiff line change
@@ -207,8 +207,6 @@ void SILGenFunction::emitDeallocatingDestructor(DestructorDecl *dd) {
207207
selfForDealloc = B.createUncheckedRefCast(loc, selfForDealloc, classTy);
208208
B.createDeallocRef(loc, selfForDealloc);
209209

210-
emitProfilerIncrement(dd->getTypecheckedBody());
211-
212210
// Return.
213211
B.createReturn(loc, emitEmptyTuple(loc));
214212
}

test/Profiler/coverage_class.swift

+1-1
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ class C {
1010
// CHECK-NEXT: [[@LINE+1]]:10 -> [[@LINE+1]]:12
1111
init() {}
1212

13-
// CHECK-LABEL: sil_coverage_map {{.*}}// coverage_class.C.__deallocating_deinit
13+
// CHECK-LABEL: sil_coverage_map {{.*}}// coverage_class.C.deinit
1414
// CHECK-NEXT: [[@LINE+1]]:10 -> [[@LINE+1]]:12
1515
deinit {}
1616
}

test/Profiler/coverage_deinit.swift

+30
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
// RUN: %target-swift-frontend -Xllvm -sil-full-demangle -suppress-warnings -profile-generate -profile-coverage-mapping -emit-sorted-sil -emit-sil -module-name coverage_deinit %s | %FileCheck %s
2+
// RUN: %target-swift-frontend -profile-generate -profile-coverage-mapping -emit-ir %s
3+
4+
func foo() {}
5+
6+
public class Foo {
7+
// CHECK-LABEL: sil @$s15coverage_deinit3FooCfd
8+
// CHECK: integer_literal $Builtin.Int64, 0
9+
// CHECK: integer_literal $Builtin.Int32, 2
10+
// CHECK: integer_literal $Builtin.Int32, 0
11+
// CHECK-NEXT: builtin "int_instrprof_increment"
12+
// CHECK: function_ref @$sSb6randomSbyFZ
13+
// CHECK: cond_br
14+
// CHECK: integer_literal $Builtin.Int64, 0
15+
// CHECK: integer_literal $Builtin.Int32, 2
16+
// CHECK: integer_literal $Builtin.Int32, 1
17+
// CHECK-NEXT: builtin "int_instrprof_increment"
18+
// CHECK-NEXT: // function_ref coverage_deinit.foo() -> ()
19+
deinit {
20+
if .random() {
21+
foo()
22+
}
23+
}
24+
}
25+
26+
// CHECK-LABEL: sil_coverage_map "{{.*}}coverage_deinit.swift" "$s15coverage_deinit3FooCfd"
27+
// CHECK-NEXT: [[@LINE-8]]:10 -> [[@LINE-4]]:4 : 0
28+
// CHECK-NEXT: [[@LINE-8]]:8 -> [[@LINE-8]]:17 : 0
29+
// CHECK-NEXT: [[@LINE-9]]:18 -> [[@LINE-7]]:6 : 1
30+
// CHECK-NEXT: [[@LINE-8]]:6 -> [[@LINE-7]]:4 : 0

0 commit comments

Comments
 (0)