Skip to content

Commit 4218917

Browse files
committed
[sil-combine] Improve dead closures elimination
Cover more corner cases, e.g. unbalanced retain/release pairs due to an unreachable at the end of the block. This significantly improves the performance of some closure-heavy test-cases. rdar://23691514
1 parent 13591c5 commit 4218917

File tree

2 files changed

+20
-21
lines changed

2 files changed

+20
-21
lines changed

lib/SILAnalysis/ARCAnalysis.cpp

Lines changed: 11 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -777,21 +777,19 @@ static bool successorHasLiveIn(SILBasicBlock *BB,
777777
return false;
778778
}
779779

780-
// Walk backwards in BB looking for strong_release or dealloc_box of
781-
// the given value, and add it to Releases.
782-
static bool addLastRelease(SILValue V, SILBasicBlock *BB,
783-
ReleaseTracker &Tracker) {
780+
// Walk backwards in BB looking for the last use of a given
781+
// value, and add it to the set of release points.
782+
static bool addLastUse(SILValue V, SILBasicBlock *BB,
783+
ReleaseTracker &Tracker) {
784784
for (auto I = BB->rbegin(); I != BB->rend(); ++I) {
785-
if (isa<StrongReleaseInst>(*I) || isa<DeallocBoxInst>(*I) ||
786-
isa<ReleaseValueInst>(*I)) {
787-
if (I->getOperand(0) != V)
788-
continue;
789-
790-
Tracker.trackLastRelease(&*I);
791-
return true;
792-
}
785+
for (auto &Op : I->getAllOperands())
786+
if (Op.get().getDef() == V.getDef()) {
787+
Tracker.trackLastRelease(&*I);
788+
return true;
789+
}
793790
}
794791

792+
llvm_unreachable("BB is expected to have a use of a closure");
795793
return false;
796794
}
797795

@@ -850,7 +848,7 @@ bool swift::getFinalReleasesForValue(SILValue V, ReleaseTracker &Tracker) {
850848
// release/dealloc.
851849
for (auto *BB : UseBlocks)
852850
if (!successorHasLiveIn(BB, LiveIn))
853-
if (!addLastRelease(V, BB, Tracker))
851+
if (!addLastUse(V, BB, Tracker))
854852
return false;
855853

856854
return true;

test/SILPasses/sil_combine.sil

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -736,18 +736,19 @@ bb1:
736736
return %5 : $()
737737
}
738738

739-
sil @unbalanced_closure : $@convention(thin) (Builtin.Int32) -> ()
739+
sil @unbalanced_closure : $@convention(thin) (@guaranteed B) -> ()
740740

741741
// CHECK-LABEL: sil @partial_apply_unbalanced_retain_release
742-
sil @partial_apply_unbalanced_retain_release : $@convention(thin) (Builtin.Int32) -> () {
742+
sil @partial_apply_unbalanced_retain_release : $@convention(thin) (@owned B) -> () {
743743
// CHECK: bb0
744-
bb0(%0 : $Builtin.Int32):
745-
%1 = function_ref @unbalanced_closure : $@convention(thin) (Builtin.Int32) -> ()
746-
// CHECK: partial_apply
747-
%2 = partial_apply %1(%0) : $@convention(thin) (Builtin.Int32) -> ()
744+
bb0(%0 : $B):
745+
%1 = function_ref @unbalanced_closure : $@convention(thin) (@guaranteed B) -> ()
746+
// CHECK-NOT: partial_apply
747+
%2 = partial_apply %1(%0) : $@convention(thin) (@guaranteed B) -> ()
748+
// Check that the arguments of the closure are released after its last use
749+
// CHECK-NEXT: strong_release %0 : $B
748750
strong_retain %2 : $@callee_owned () -> ()
749-
%4 = builtin "int_trap"() : $()
750-
// CHECK: unreachable
751+
// CHECK-NEXT: unreachable
751752
unreachable
752753
}
753754

0 commit comments

Comments
 (0)