Skip to content

Commit b7f36e7

Browse files
authored
Merge pull request #85104 from eeckstein/fix-closure-specialization
ClosureSpecialization: don't crash for recursive functions
2 parents 3595fe4 + 6988b03 commit b7f36e7

File tree

2 files changed

+29
-1
lines changed

2 files changed

+29
-1
lines changed

SwiftCompilerSources/Sources/Optimizer/FunctionPasses/ClosureSpecialization.swift

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -242,7 +242,10 @@ private func isCalleeSpecializable(of apply: ApplySite) -> Bool {
242242
// might no longer have 'self' as the last parameter.
243243
//
244244
// TODO: Keep the self argument the last when appending arguments.
245-
!callee.mayBindDynamicSelf
245+
!callee.mayBindDynamicSelf,
246+
247+
// Don't support self-recursive functions because that would result in duplicate mapping of values when cloning.
248+
callee != apply.parentFunction
246249
{
247250
return true
248251
}

test/SILOptimizer/closure_specialization.sil

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -947,3 +947,28 @@ bb0(%nc : @owned $NC):
947947
%11 = tuple ()
948948
return %11 : $()
949949
}
950+
951+
sil @closure_with_closure : $@convention(thin) (@guaranteed @callee_guaranteed () -> ()) -> ()
952+
953+
// Just make sure the pass is not crashing.
954+
sil [ossa] @test_recursion : $@convention(thin) (@guaranteed @callee_guaranteed () -> ()) -> () {
955+
bb0(%0 : @guaranteed $@callee_guaranteed () -> ()):
956+
%2 = apply %0() : $@callee_guaranteed () -> ()
957+
cond_br undef, bb1, bb2
958+
959+
bb1:
960+
%5 = function_ref @closure_with_closure : $@convention(thin) (@guaranteed @callee_guaranteed () -> ()) -> ()
961+
%6 = copy_value %0
962+
%7 = partial_apply [callee_guaranteed] %5(%6) : $@convention(thin) (@guaranteed @callee_guaranteed () -> ()) -> ()
963+
%8 = function_ref @test_recursion : $@convention(thin) (@guaranteed @callee_guaranteed () -> ()) -> ()
964+
%9 = apply %8(%7) : $@convention(thin) (@guaranteed @callee_guaranteed () -> ()) -> ()
965+
destroy_value %7
966+
br bb3
967+
968+
bb2:
969+
br bb3
970+
971+
bb3:
972+
%13 = tuple ()
973+
return %13
974+
}

0 commit comments

Comments
 (0)