Skip to content

Commit 2dbe751

Browse files
committed
SimplifyDestructure: convert destructure_tuple/destructure_struct with guaranteed ownership to to tuple_extract/struct_extract
1 parent 64a36ce commit 2dbe751

File tree

4 files changed

+42
-14
lines changed

4 files changed

+42
-14
lines changed

SwiftCompilerSources/Sources/Optimizer/InstructionSimplification/SimplifyDestructure.swift

Lines changed: 15 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -50,19 +50,6 @@ private extension DestructureInstruction {
5050

5151
func foldWithAggregateConstruction(_ context: SimplifyContext) {
5252

53-
if aggregate.type.isTrivial(in: parentFunction) {
54-
// ```
55-
// (%1, %2) = destructure_tuple %t
56-
// ```
57-
// ->
58-
// ```
59-
// %1 = tuple_extract %t, 0
60-
// %2 = tuple_extract %t, 1
61-
// ```
62-
replaceWithAggregateExtract(context)
63-
return
64-
}
65-
6653
switch aggregate {
6754
case let constructInst as ConstructureInstruction:
6855
// Eliminate the redundant instruction pair
@@ -99,6 +86,21 @@ private extension DestructureInstruction {
9986
default:
10087
break
10188
}
89+
90+
if !isDeleted,
91+
aggregate.type.isTrivial(in: parentFunction) || aggregate.ownership == .guaranteed
92+
{
93+
// ```
94+
// (%1, %2) = destructure_tuple %t
95+
// ```
96+
// ->
97+
// ```
98+
// %1 = tuple_extract %t, 0
99+
// %2 = tuple_extract %t, 1
100+
// ```
101+
replaceWithAggregateExtract(context)
102+
return
103+
}
102104
}
103105

104106
private func replaceWithAggregateExtract(_ context: SimplifyContext) {

test/SILOptimizer/sil_combine_misc_opts.sil

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,7 @@ bb0(%0 : $*Builtin.NativeObject):
102102
}
103103

104104
// CHECK-LABEL: sil [ossa] @test_updating_borrowed_from :
105-
// CHECK: borrowed {{.*}} from (%0 : $S)
105+
// this is not optimized anymore in SILCombine
106106
// CHECK-LABEL: } // end sil function 'test_updating_borrowed_from'
107107
sil [ossa] @test_updating_borrowed_from : $@convention(thin) (@guaranteed S) -> () {
108108
bb0(%0 : @guaranteed $S):

test/SILOptimizer/simplify_destructure_struct.sil

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -158,3 +158,16 @@ bb0(%0 : $TrivialS):
158158
return %3
159159
}
160160

161+
// CHECK-LABEL: sil [ossa] @guaranteed_to_struct_extract :
162+
// CHECK: %1 = struct_extract %0 : $S, #S.a
163+
// CHECK: %2 = struct_extract %0 : $S, #S.b
164+
// CHECK: } // end sil function 'guaranteed_to_struct_extract'
165+
sil [ossa] @guaranteed_to_struct_extract : $@convention(thin) (@guaranteed S) -> () {
166+
bb0(%0 : @guaranteed $S):
167+
(%1, %2) = destructure_struct %0
168+
fix_lifetime %1
169+
fix_lifetime %2
170+
%r = tuple ()
171+
return %r
172+
}
173+

test/SILOptimizer/simplify_destructure_tuple.sil

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -153,3 +153,16 @@ bb0(%0 : $(Int, Int)):
153153
return %3
154154
}
155155

156+
// CHECK-LABEL: sil [ossa] @guaranteed_to_tuple_extract :
157+
// CHECK: %1 = tuple_extract %0 : $(String, Int), 0
158+
// CHECK: %2 = tuple_extract %0 : $(String, Int), 1
159+
// CHECK: } // end sil function 'guaranteed_to_tuple_extract'
160+
sil [ossa] @guaranteed_to_tuple_extract : $@convention(thin) (@guaranteed (String, Int)) -> () {
161+
bb0(%0 : @guaranteed $(String, Int)):
162+
(%1, %2) = destructure_tuple %0
163+
fix_lifetime %1
164+
fix_lifetime %2
165+
%r = tuple ()
166+
return %r
167+
}
168+

0 commit comments

Comments
 (0)