Skip to content

Commit 7a048ea

Browse files
committed
Add support for dead partial apply elimination in mandatory combine.
1 parent 270f734 commit 7a048ea

File tree

2 files changed

+119
-0
lines changed

2 files changed

+119
-0
lines changed

lib/SILOptimizer/Mandatory/MandatoryCombine.cpp

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,7 @@ class MandatoryCombiner final
129129
/// Base visitor that does not do anything.
130130
SILInstruction *visitSILInstruction(SILInstruction *) { return nullptr; }
131131
SILInstruction *visitApplyInst(ApplyInst *instruction);
132+
SILInstruction *visitPartialApplyInst(PartialApplyInst *i);
132133
};
133134

134135
} // end anonymous namespace
@@ -287,6 +288,12 @@ SILInstruction *MandatoryCombiner::visitApplyInst(ApplyInst *instruction) {
287288
return nullptr;
288289
}
289290

291+
/// Try to remove partial applies that are no longer used
292+
SILInstruction *MandatoryCombiner::visitPartialApplyInst(PartialApplyInst *i) {
293+
tryDeleteDeadClosure(i, instModCallbacks, /*needKeepArgsAlive=*/false);
294+
return nullptr;
295+
}
296+
290297
//===----------------------------------------------------------------------===//
291298
// Top Level Entrypoint
292299
//===----------------------------------------------------------------------===//

test/SILOptimizer/mandatory_combiner.sil

Lines changed: 112 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -918,3 +918,115 @@ bb0(%0 : @guaranteed $Klass, %1 : @guaranteed $Klass, %2 : @guaranteed $Klass):
918918
%9999 = tuple ()
919919
return %9999 : $()
920920
}
921+
922+
///////////////////////////////
923+
// Partial Apply Elimination //
924+
///////////////////////////////
925+
926+
sil @use_klass : $@convention(thin) (@guaranteed Klass) -> ()
927+
928+
sil [transparent] @guaranteed_closure_func : $@convention(thin) (@guaranteed Klass) -> () {
929+
bb0(%0 : $Klass):
930+
%use = function_ref @use_klass : $@convention(thin) (@guaranteed Klass) -> ()
931+
apply %use(%0) : $@convention(thin) (@guaranteed Klass) -> ()
932+
%t = tuple ()
933+
return %t : $()
934+
}
935+
936+
// CHECK: sil [ossa] @test_no_copies_for_stack_pa : $@convention(thin) (@guaranteed Klass) -> () {
937+
// CHECK-NOT: apply
938+
// CHECK-NOT: copy_value
939+
// CHECK: [[FUNC:%.*]] = function_ref @use_klass :
940+
// CHECK-NEXT: apply [[FUNC]]
941+
// CHECK-NOT: apply
942+
// CHECK-NOT: destroy_value
943+
// CHECK: } // end sil function 'test_no_copies_for_stack_pa'
944+
sil [ossa] @test_no_copies_for_stack_pa : $@convention(thin) (@guaranteed Klass) -> () {
945+
bb0(%0 : @guaranteed $Klass):
946+
br bb1
947+
948+
bb1:
949+
%closure_fun = function_ref @guaranteed_closure_func : $@convention(thin) (@guaranteed Klass) -> ()
950+
%closure = partial_apply [on_stack] [callee_guaranteed] %closure_fun(%0) : $@convention(thin) (@guaranteed Klass) -> ()
951+
cond_br undef, bb2, bb3
952+
953+
bb2:
954+
%5 = function_ref @use_klass : $@convention(thin) (@guaranteed Klass) -> ()
955+
%6 = apply %5(%0) : $@convention(thin) (@guaranteed Klass) -> ()
956+
%7 = tuple ()
957+
br bb4
958+
959+
bb3:
960+
br bb4
961+
962+
bb4:
963+
dealloc_stack %closure : $@noescape @callee_guaranteed () -> ()
964+
cond_br undef, bb1, bb5
965+
966+
bb5:
967+
%tuple = tuple()
968+
return %tuple : $()
969+
}
970+
971+
// CHECK-LABEL: sil @test_apply_pai_in_loop : $@convention(thin) (@guaranteed Klass) -> () {
972+
// CHECK: bb0([[ARG:%.*]] : $Klass):
973+
// CHECK-NEXT: strong_retain [[ARG]]
974+
// CHECK-NEXT: br bb1
975+
//
976+
// CHECK: bb1:
977+
// CHECK-NEXT: cond_br undef, bb2, bb3
978+
//
979+
// CHECK: bb2:
980+
// CHECK-NEXT: function_ref use_klass
981+
// CHECK-NEXT: [[FUNC:%.*]] = function_ref @use_klass
982+
// CHECK-NEXT: apply [[FUNC]]([[ARG]])
983+
// CHECK-NEXT: br bb1
984+
//
985+
// CHECK: bb3:
986+
// CHECK-NEXT: strong_release [[ARG]]
987+
// CHECK-NEXT: strong_release [[ARG]]
988+
// CHECK-NEXT: tuple
989+
// CHECK-NEXT: return
990+
// CHECK-NEXT: } // end sil function 'test_apply_pai_in_loop'
991+
sil @test_apply_pai_in_loop : $@convention(thin) (@guaranteed Klass) -> () {
992+
bb0(%0: $Klass):
993+
strong_retain %0 : $Klass
994+
%closure_fun = function_ref @guaranteed_closure_func : $@convention(thin) (@guaranteed Klass) -> ()
995+
strong_retain %0 : $Klass
996+
%closure = partial_apply [callee_guaranteed] %closure_fun(%0) : $@convention(thin) (@guaranteed Klass) -> ()
997+
br bb1
998+
999+
bb1:
1000+
cond_br undef, bb2, bb3
1001+
1002+
bb2:
1003+
%7 = function_ref @use_klass : $@convention(thin) (@guaranteed Klass) -> ()
1004+
%8 = apply %7(%0) : $@convention(thin) (@guaranteed Klass) -> ()
1005+
br bb1
1006+
1007+
bb3:
1008+
strong_release %0: $Klass
1009+
strong_release %0: $Klass
1010+
%tuple = tuple()
1011+
return %tuple : $()
1012+
}
1013+
1014+
// CHECK-LABEL: sil @test_guaranteed_on_stack_closure
1015+
// CHECK: strong_retain %0 : $Klass
1016+
// CHECK: [[F:%.*]] = function_ref @use_klass : $@convention(thin) (@guaranteed Klass) -> ()
1017+
// CHECK: apply [[F]](%0) : $@convention(thin) (@guaranteed Klass) -> ()
1018+
// CHECK: strong_release %0 : $Klass
1019+
// CHECK: return
1020+
sil @test_guaranteed_on_stack_closure : $@convention(thin) (@guaranteed Klass) -> () {
1021+
bb0(%0: $Klass):
1022+
strong_retain %0 : $Klass
1023+
%closure_fun = function_ref @guaranteed_closure_func : $@convention(thin) (@guaranteed Klass) -> ()
1024+
%closure = partial_apply [on_stack] [callee_guaranteed] %closure_fun(%0) : $@convention(thin) (@guaranteed Klass) -> ()
1025+
%closure2 = function_ref @use_klass : $@convention(thin) (@guaranteed Klass) -> ()
1026+
apply %closure2(%0) : $@convention(thin) (@guaranteed Klass) -> ()
1027+
%6 = tuple ()
1028+
strong_release %0 : $Klass
1029+
dealloc_stack %closure : $@noescape @callee_guaranteed () -> ()
1030+
%tuple = tuple()
1031+
return %tuple : $()
1032+
}

0 commit comments

Comments
 (0)