Skip to content

[4.2] ARC code motion: handle unowned retains/releases correctly. #16819

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions lib/SILOptimizer/Analysis/ARCAnalysis.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -36,12 +36,14 @@ using BasicBlockRetainValue = std::pair<SILBasicBlock *, SILValue>;
//===----------------------------------------------------------------------===//

bool swift::isRetainInstruction(SILInstruction *I) {
return isa<StrongRetainInst>(I) || isa<RetainValueInst>(I);
return isa<StrongRetainInst>(I) || isa<RetainValueInst>(I) ||
isa<UnownedRetainInst>(I);
}


bool swift::isReleaseInstruction(SILInstruction *I) {
return isa<StrongReleaseInst>(I) || isa<ReleaseValueInst>(I);
return isa<StrongReleaseInst>(I) || isa<ReleaseValueInst>(I) ||
isa<UnownedReleaseInst>(I);
}

//===----------------------------------------------------------------------===//
Expand Down
46 changes: 46 additions & 0 deletions test/SILOptimizer/retain_release_code_motion.sil
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
// RUN: %target-sil-opt -assume-parsing-unqualified-ownership-sil -enable-sil-verify-all -retain-sinking -late-release-hoisting %s | %FileCheck %s
// RUN: %target-sil-opt -assume-parsing-unqualified-ownership-sil -enable-sil-verify-all -release-hoisting %s | %FileCheck --check-prefix=CHECK-RELEASE-HOISTING %s
// 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

import Builtin
Expand Down Expand Up @@ -60,6 +61,10 @@ enum Optional<T> {
case some(T)
}

struct Unowned {
@sil_stored unowned let x: @sil_unowned Builtin.NativeObject
}

sil @createS : $@convention(thin) () -> @owned S

sil @use_C2 : $@convention(thin) (C2) -> ()
Expand Down Expand Up @@ -620,3 +625,44 @@ bb3(%p1: $C2, %p2: $C2):
return %10 : $()
}

// CHECK-LABEL: sil @test_unowned
// CHECK: bb0(%0 : $Builtin.NativeObject):
// CHECK-NEXT: br bb1
// CHECK: bb1:
// CHECK-NEXT: tuple
// CHECK-NEXT: return
sil @test_unowned : $@convention(thin) (@guaranteed Builtin.NativeObject) -> () {
bb0(%0 : $Builtin.NativeObject):
%1 = ref_to_unowned %0 : $Builtin.NativeObject to $@sil_unowned Builtin.NativeObject
%2 = struct $Unowned (%1 : $@sil_unowned Builtin.NativeObject)
retain_value %2: $Unowned
br bb1

bb1:
release_value %2: $Unowned
%5 = tuple()
return %5 : $()
}

// CHECK-RELEASE-HOISTING-LABEL: sil @dont_eliminate_strong_retain_and_unowned_release_pair
// CHECK-RELEASE-HOISTING: = function_ref @blocker
// CHECK-RELEASE-HOISTING-NEXT: apply
// CHECK-RELEASE-HOISTING-NEXT: strong_retain %0 : $Builtin.NativeObject
// CHECK-RELEASE-HOISTING-NEXT: unowned_release %1 : $@sil_unowned Builtin.NativeObject
sil @dont_eliminate_strong_retain_and_unowned_release_pair : $@convention(thin) (@guaranteed Builtin.NativeObject) -> () {
bb0(%0 : $Builtin.NativeObject):
%1 = ref_to_unowned %0 : $Builtin.NativeObject to $@sil_unowned Builtin.NativeObject
%2 = struct $Unowned (%1 : $@sil_unowned Builtin.NativeObject)
retain_value %2: $Unowned
%b = function_ref @blocker : $@convention(thin) () -> ()
apply %b() : $@convention(thin) () -> ()
strong_retain %0: $Builtin.NativeObject
br bb1

bb1:
release_value %2: $Unowned
apply %b() : $@convention(thin) () -> ()
strong_release %0: $Builtin.NativeObject
%5 = tuple()
return %5 : $()
}