Skip to content

Commit 0983e84

Browse files
committed
SILCombine: fix wrong placement of a strong_release of a dead closure argument which caused a memory leak
The code to find the release-points didn't handle release_value instructions. https://bugs.swift.org/browse/SR-9518 rdar://problem/46794455
1 parent 9af6623 commit 0983e84

File tree

2 files changed

+25
-1
lines changed

2 files changed

+25
-1
lines changed

lib/SILOptimizer/Analysis/ARCAnalysis.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1055,7 +1055,7 @@ bool swift::getFinalReleasesForValue(SILValue V, ReleaseTracker &Tracker) {
10551055

10561056
// Try to speed up the trivial case of single release/dealloc.
10571057
if (isa<StrongReleaseInst>(User) || isa<DeallocBoxInst>(User) ||
1058-
isa<DestroyValueInst>(User)) {
1058+
isa<DestroyValueInst>(User) || isa<ReleaseValueInst>(User)) {
10591059
if (!seenRelease)
10601060
OneRelease = User;
10611061
else

test/SILOptimizer/sil_combine.sil

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -614,6 +614,30 @@ bb0(%0 : $Builtin.RawPointer):
614614
return %2 : $Builtin.RawPointer
615615
}
616616

617+
sil @test_closure : $@convention(thin) (@guaranteed C) -> ()
618+
619+
// CHECK-LABEL: sil @dead_closure_elimination
620+
// CHECK: bb0(%0 : $C):
621+
// CHECK-NEXT: cond_br
622+
// CHECK-NOT: strong_release
623+
// CHECK: bb2:
624+
// CHECK-NEXT: strong_release %0
625+
// CHECK return
626+
sil @dead_closure_elimination : $@convention(thin) (@owned C) -> () {
627+
bb0(%0 : $C):
628+
%1 = function_ref @test_closure : $@convention(thin) (@guaranteed C) -> ()
629+
%2 = partial_apply [callee_guaranteed] %1(%0) : $@convention(thin) (@guaranteed C) -> ()
630+
cond_br undef, bb1, bb2
631+
bb1:
632+
strong_retain %2 : $@callee_guaranteed () -> ()
633+
strong_release %2 : $@callee_guaranteed () -> ()
634+
br bb2
635+
bb2:
636+
release_value %2 : $@callee_guaranteed () -> ()
637+
%5 = tuple()
638+
return %5 : $()
639+
}
640+
617641
////////////////////////////////////////////
618642
// Load Proj To GEP Load Canonicalization //
619643
////////////////////////////////////////////

0 commit comments

Comments
 (0)