Skip to content

Commit 5d6659c

Browse files
authored
Merge pull request #38875 from meg-gupta/fixcanonicalizesilcombine
Fix borrow canonicalization bugs
2 parents c035022 + 883d145 commit 5d6659c

File tree

3 files changed

+44
-2
lines changed

3 files changed

+44
-2
lines changed

include/swift/SILOptimizer/Utils/CanonicalizeBorrowScope.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -136,7 +136,7 @@ class CanonicalizeBorrowScope {
136136

137137
protected:
138138
void initBorrow(BorrowedValue borrow) {
139-
assert(liveness.empty() && persistentCopies.empty());
139+
assert(borrow && liveness.empty() && persistentCopies.empty());
140140

141141
updatedCopies.clear();
142142
borrowedValue = borrow;

lib/SILOptimizer/Utils/CanonicalizeBorrowScope.cpp

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -778,9 +778,15 @@ bool CanonicalizeBorrowScope::consolidateBorrowScope() {
778778

779779
bool CanonicalizeBorrowScope::canonicalizeFunctionArgument(
780780
SILFunctionArgument *arg) {
781+
BorrowedValue borrow(arg);
782+
if (!borrow)
783+
return false;
784+
785+
initBorrow(borrow);
786+
781787
LLVM_DEBUG(llvm::dbgs() << "*** Canonicalize Borrow: " << borrowedValue);
782788

783-
initBorrow(BorrowedValue(arg));
789+
SWIFT_DEFER { liveness.clear(); };
784790

785791
RewriteInnerBorrowUses innerRewriter(*this);
786792
beginVisitBorrowScopeUses(); // reset the def/use worklist

test/SILOptimizer/sil_combine_ossa.sil

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
// RUN: %target-sil-opt -enable-objc-interop -enforce-exclusivity=none -enable-sil-verify-all %s -sil-combine | %FileCheck %s
22
// RUN: %target-sil-opt -enable-objc-interop -enforce-exclusivity=none -enable-sil-verify-all %s -sil-combine -generic-specializer | %FileCheck %s --check-prefix=CHECK_FORWARDING_OWNERSHIP_KIND
3+
// RUN: %target-sil-opt -enable-objc-interop -enforce-exclusivity=none -enable-sil-verify-all %s -sil-combine -enable-copy-propagation
34

45
// Declare this SIL to be canonical because some tests break raw SIL
56
// conventions. e.g. address-type block args. -enforce-exclusivity=none is also
@@ -5087,3 +5088,38 @@ bb0(%0 : @guaranteed $Function):
50875088
destroy_value %6 : $Optional<@callee_guaranteed (MyInt) -> Double>
50885089
return %13 : $Double
50895090
}
5091+
5092+
sil [reabstraction_thunk] @thunk : $@convention(thin) (@guaranteed Klass, @guaranteed Klass) -> ()
5093+
5094+
// CHECK-LABEL: sil [ossa] @test_partial_apply_apply_opt1 :
5095+
// CHECK-NOT: partial_apply
5096+
// CHECK: } // end sil function 'test_partial_apply_apply_opt1'
5097+
sil [ossa] @test_partial_apply_apply_opt1 : $@convention(thin) (@guaranteed Klass, @guaranteed Klass) -> () {
5098+
bb0(%0 : @guaranteed $Klass, %1 : @guaranteed $Klass):
5099+
%c1 = copy_value %0 : $Klass
5100+
%c2 = copy_value %1 : $Klass
5101+
%f1 = function_ref @thunk : $@convention(thin) (@guaranteed Klass, @guaranteed Klass) -> ()
5102+
%p1 = partial_apply [callee_guaranteed] %f1(%c1, %c2) : $@convention(thin) (@guaranteed Klass, @guaranteed Klass) -> ()
5103+
%r = apply %p1() : $@callee_guaranteed () -> ()
5104+
destroy_value %p1 : $@callee_guaranteed () -> ()
5105+
%7 = tuple ()
5106+
return %7 : $()
5107+
}
5108+
5109+
// CHECK-LABEL: sil [ossa] @test_partial_apply_apply_opt2 :
5110+
// CHECK-NOT: partial_apply
5111+
// CHECK: } // end sil function 'test_partial_apply_apply_opt2'
5112+
sil [ossa] @test_partial_apply_apply_opt2 : $@convention(thin) (@owned Klass, @owned Klass) -> () {
5113+
bb0(%0 : @owned $Klass, %1 : @owned $Klass):
5114+
%c1 = copy_value %0 : $Klass
5115+
%c2 = copy_value %1 : $Klass
5116+
%f1 = function_ref @thunk : $@convention(thin) (@guaranteed Klass, @guaranteed Klass) -> ()
5117+
%p1 = partial_apply [callee_guaranteed] %f1(%c1, %c2) : $@convention(thin) (@guaranteed Klass, @guaranteed Klass) -> ()
5118+
%r = apply %p1() : $@callee_guaranteed () -> ()
5119+
destroy_value %p1 : $@callee_guaranteed () -> ()
5120+
destroy_value %0 : $Klass
5121+
destroy_value %1 : $Klass
5122+
%7 = tuple ()
5123+
return %7 : $()
5124+
}
5125+

0 commit comments

Comments
 (0)