Skip to content

Commit 095d84d

Browse files
authored
Merge pull request #64440 from meg-gupta/disablesemarcoptsedgecase
Disable owned to guaranteed phi transformation when the borrow introducers have a local scope
2 parents d6a0268 + 21e829b commit 095d84d

File tree

3 files changed

+122
-1
lines changed

3 files changed

+122
-1
lines changed

lib/SILOptimizer/SemanticARC/CopyValueOpts.cpp

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ bool SemanticARCOptVisitor::performGuaranteedCopyValueOptimization(
7676
SmallVector<BorrowedValue, 4> borrowScopeIntroducers;
7777

7878
// Find all borrow introducers for our copy operand. If we are unable to find
79-
// all of the reproducers (due to pattern matching failure), conservatively
79+
// all of the introducers (due to pattern matching failure), conservatively
8080
// return false. We can not optimize.
8181
//
8282
// NOTE: We can get multiple introducers if our copy_value's operand
@@ -216,6 +216,17 @@ bool SemanticARCOptVisitor::performGuaranteedCopyValueOptimization(
216216
return false;
217217
}
218218

219+
// Replacing owned phi operands with a local borrow introducer can
220+
// introduce reborrows. Since lifetime adjustment is not implemented for
221+
// this case, disable here.
222+
// Returning false here will make sure so this isn't populated in
223+
// joinedOwnedIntroducerToConsumedOperands which is used by
224+
// semanticarc::tryConvertOwnedPhisToGuaranteedPhis for transforming owned
225+
// phi to guaranteed.
226+
if (haveAnyLocalScopes) {
227+
return false;
228+
}
229+
219230
if (llvm::any_of(borrowScopeIntroducers, [&](BorrowedValue borrowScope) {
220231
return !borrowScope.areUsesWithinExtendedScope(
221232
phiArgLR.getAllConsumingUses(), nullptr);

lib/SILOptimizer/SemanticARC/OwnedToGuaranteedPhiOpt.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,8 @@ static bool getIncomingJoinedLiveRangeOperands(
128128
// Top Level Entrypoint
129129
//===----------------------------------------------------------------------===//
130130

131+
// This needs `SemanticARCOptVisitor::performGuaranteedCopyValueOptimization` to
132+
// run before so that joinedOwnedIntroducerToConsumedOperands is populated.
131133
bool swift::semanticarc::tryConvertOwnedPhisToGuaranteedPhis(Context &ctx) {
132134
bool madeChange = false;
133135

Lines changed: 108 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,108 @@
1+
// RUN: %target-sil-opt -semantic-arc-opts %s | %FileCheck %s
2+
3+
class Klass {}
4+
5+
// CHECK-LABEL: sil [ossa] @test_owned_to_guaranteed1 :
6+
// CHECK-NOT: copy_value
7+
// CHECK-LABEL: } // end sil function 'test_owned_to_guaranteed1'
8+
sil [ossa] @test_owned_to_guaranteed1 : $@convention(thin) (@guaranteed Klass, @guaranteed Klass) -> () {
9+
bb0(%0 : @guaranteed $Klass, %1 : @guaranteed $Klass):
10+
cond_br undef, bb1, bb2
11+
12+
bb1:
13+
%copy1 = copy_value %0 : $Klass
14+
br bb3(%copy1 : $Klass)
15+
16+
bb2:
17+
%copy2 = copy_value %1 : $Klass
18+
br bb3(%copy2 : $Klass)
19+
20+
bb3(%copy : @owned $Klass):
21+
destroy_value %copy : $Klass
22+
%t = tuple ()
23+
return %t : $()
24+
}
25+
26+
// CHECK-LABEL: sil [ossa] @test_owned_to_guaranteed2 :
27+
// CHECK: bb3([[ARG1:%.*]] : @owned $Klass, [[ARG2:%.*]] : @guaranteed $Klass)
28+
// CHECK-LABEL: } // end sil function 'test_owned_to_guaranteed2'
29+
sil [ossa] @test_owned_to_guaranteed2 : $@convention(thin) (@owned Klass, @owned Klass) -> () {
30+
bb0(%0 : @owned $Klass, %1 : @owned $Klass):
31+
cond_br undef, bb1, bb2
32+
33+
bb1:
34+
%borrow1 = begin_borrow %0 : $Klass
35+
%copy1 = copy_value %borrow1 : $Klass
36+
br bb3(%copy1 : $Klass, %borrow1 : $Klass)
37+
38+
bb2:
39+
%borrow2 = begin_borrow %1 : $Klass
40+
%copy2 = copy_value %borrow2 : $Klass
41+
br bb3(%copy2 : $Klass, %borrow2 : $Klass)
42+
43+
bb3(%copy : @owned $Klass, %borrow : @guaranteed $Klass):
44+
destroy_value %copy : $Klass
45+
end_borrow %borrow : $Klass
46+
destroy_value %0 : $Klass
47+
destroy_value %1 : $Klass
48+
%t = tuple ()
49+
return %t : $()
50+
}
51+
52+
// CHECK-LABEL: sil [ossa] @test_owned_to_guaranteed3 :
53+
// CHECK: bb3([[ARG1:%.*]] : @owned $Klass)
54+
// CHECK-LABEL: } // end sil function 'test_owned_to_guaranteed3'
55+
sil [ossa] @test_owned_to_guaranteed3 : $@convention(thin) (@owned Klass, @owned Klass) -> () {
56+
bb0(%0 : @owned $Klass, %1 : @owned $Klass):
57+
%borrow1 = begin_borrow %0 : $Klass
58+
%borrow2 = begin_borrow %1 : $Klass
59+
cond_br undef, bb1, bb2
60+
61+
bb1:
62+
%copy1 = copy_value %borrow1 : $Klass
63+
br bb3(%copy1 : $Klass)
64+
65+
bb2:
66+
%copy2 = copy_value %borrow2 : $Klass
67+
br bb3(%copy2 : $Klass)
68+
69+
bb3(%copy : @owned $Klass):
70+
destroy_value %copy : $Klass
71+
end_borrow %borrow1 : $Klass
72+
end_borrow %borrow2 : $Klass
73+
destroy_value %0 : $Klass
74+
destroy_value %1 : $Klass
75+
%t = tuple ()
76+
return %t : $()
77+
}
78+
79+
// CHECK-LABEL: sil [ossa] @test_owned_to_guaranteed4 :
80+
// CHECK: bb3([[ARG1:%.*]] : @owned $Klass)
81+
// CHECK-LABEL: } // end sil function 'test_owned_to_guaranteed4'
82+
sil [ossa] @test_owned_to_guaranteed4 : $@convention(thin) (@in Klass) -> () {
83+
bb0(%0 : $*Klass):
84+
%borrow = load_borrow %0 : $*Klass
85+
%copy = copy_value %borrow : $Klass
86+
cond_br undef, bb1, bb2
87+
88+
bb1:
89+
destroy_value %copy : $Klass
90+
br bb4
91+
92+
bb2:
93+
br bb3(%copy : $Klass)
94+
95+
bb3(%arg : @owned $Klass):
96+
destroy_value %arg : $Klass
97+
end_borrow %borrow : $Klass
98+
br bbret
99+
100+
bb4:
101+
end_borrow %borrow : $Klass
102+
br bbret
103+
104+
bbret:
105+
destroy_addr %0 : $*Klass
106+
%t = tuple ()
107+
return %t : $()
108+
}

0 commit comments

Comments
 (0)