Skip to content

Commit 15da203

Browse files
authored
Merge pull request #16817 from eeckstein/fix-release-hoisting
2 parents da20780 + a471bcc commit 15da203

File tree

2 files changed

+50
-2
lines changed

2 files changed

+50
-2
lines changed

lib/SILOptimizer/Analysis/ARCAnalysis.cpp

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,12 +36,14 @@ using BasicBlockRetainValue = std::pair<SILBasicBlock *, SILValue>;
3636
//===----------------------------------------------------------------------===//
3737

3838
bool swift::isRetainInstruction(SILInstruction *I) {
39-
return isa<StrongRetainInst>(I) || isa<RetainValueInst>(I);
39+
return isa<StrongRetainInst>(I) || isa<RetainValueInst>(I) ||
40+
isa<UnownedRetainInst>(I);
4041
}
4142

4243

4344
bool swift::isReleaseInstruction(SILInstruction *I) {
44-
return isa<StrongReleaseInst>(I) || isa<ReleaseValueInst>(I);
45+
return isa<StrongReleaseInst>(I) || isa<ReleaseValueInst>(I) ||
46+
isa<UnownedReleaseInst>(I);
4547
}
4648

4749
//===----------------------------------------------------------------------===//

test/SILOptimizer/retain_release_code_motion.sil

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
// RUN: %target-sil-opt -assume-parsing-unqualified-ownership-sil -enable-sil-verify-all -retain-sinking -late-release-hoisting %s | %FileCheck %s
2+
// RUN: %target-sil-opt -assume-parsing-unqualified-ownership-sil -enable-sil-verify-all -release-hoisting %s | %FileCheck --check-prefix=CHECK-RELEASE-HOISTING %s
23
// RUN: %target-sil-opt -assume-parsing-unqualified-ownership-sil -enable-sil-verify-all -retain-sinking -retain-sinking -late-release-hoisting %s | %FileCheck --check-prefix=CHECK-MULTIPLE-RS-ROUNDS %s
34

45
import Builtin
@@ -60,6 +61,10 @@ enum Optional<T> {
6061
case some(T)
6162
}
6263

64+
struct Unowned {
65+
@sil_stored unowned let x: @sil_unowned Builtin.NativeObject
66+
}
67+
6368
sil @createS : $@convention(thin) () -> @owned S
6469

6570
sil @use_C2 : $@convention(thin) (C2) -> ()
@@ -620,3 +625,44 @@ bb3(%p1: $C2, %p2: $C2):
620625
return %10 : $()
621626
}
622627

628+
// CHECK-LABEL: sil @test_unowned
629+
// CHECK: bb0(%0 : $Builtin.NativeObject):
630+
// CHECK-NEXT: br bb1
631+
// CHECK: bb1:
632+
// CHECK-NEXT: tuple
633+
// CHECK-NEXT: return
634+
sil @test_unowned : $@convention(thin) (@guaranteed Builtin.NativeObject) -> () {
635+
bb0(%0 : $Builtin.NativeObject):
636+
%1 = ref_to_unowned %0 : $Builtin.NativeObject to $@sil_unowned Builtin.NativeObject
637+
%2 = struct $Unowned (%1 : $@sil_unowned Builtin.NativeObject)
638+
retain_value %2: $Unowned
639+
br bb1
640+
641+
bb1:
642+
release_value %2: $Unowned
643+
%5 = tuple()
644+
return %5 : $()
645+
}
646+
647+
// CHECK-RELEASE-HOISTING-LABEL: sil @dont_eliminate_strong_retain_and_unowned_release_pair
648+
// CHECK-RELEASE-HOISTING: = function_ref @blocker
649+
// CHECK-RELEASE-HOISTING-NEXT: apply
650+
// CHECK-RELEASE-HOISTING-NEXT: strong_retain %0 : $Builtin.NativeObject
651+
// CHECK-RELEASE-HOISTING-NEXT: unowned_release %1 : $@sil_unowned Builtin.NativeObject
652+
sil @dont_eliminate_strong_retain_and_unowned_release_pair : $@convention(thin) (@guaranteed Builtin.NativeObject) -> () {
653+
bb0(%0 : $Builtin.NativeObject):
654+
%1 = ref_to_unowned %0 : $Builtin.NativeObject to $@sil_unowned Builtin.NativeObject
655+
%2 = struct $Unowned (%1 : $@sil_unowned Builtin.NativeObject)
656+
retain_value %2: $Unowned
657+
%b = function_ref @blocker : $@convention(thin) () -> ()
658+
apply %b() : $@convention(thin) () -> ()
659+
strong_retain %0: $Builtin.NativeObject
660+
br bb1
661+
662+
bb1:
663+
release_value %2: $Unowned
664+
apply %b() : $@convention(thin) () -> ()
665+
strong_release %0: $Builtin.NativeObject
666+
%5 = tuple()
667+
return %5 : $()
668+
}

0 commit comments

Comments
 (0)