Skip to content

Commit 62dc05f

Browse files
committed
[CopyPropagation] Hoist owned lexical destroys.
1 parent dd168d8 commit 62dc05f

File tree

7 files changed

+21
-10
lines changed

7 files changed

+21
-10
lines changed

lib/SILOptimizer/Transforms/CopyPropagation.cpp

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -471,11 +471,17 @@ void CopyPropagation::run() {
471471
auto folded = foldDestroysOfCopiedLexicalBorrow(bbi, *domTree, deleter);
472472
if (!folded)
473473
break;
474-
// We don't add the produced move_value instruction to the worklist
475-
// because it can't handle propagation.
474+
auto hoisted = hoistDestroysOfOwnedLexicalValue(folded, *f, deleter);
475+
// Keep running even if the new move's destroys can't be hoisted.
476+
(void)hoisted;
476477
firstRun = false;
477478
}
478479
}
480+
for (auto *argument : f->getArguments()) {
481+
if (argument->getOwnershipKind() == OwnershipKind::Owned) {
482+
hoistDestroysOfOwnedLexicalValue(argument, *f, deleter);
483+
}
484+
}
479485
deleter.cleanupDeadInstructions();
480486

481487
// For now, only modify forwarding instructions

test/Distributed/SIL/distributed_actor_default_init_sil_5.swift

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,6 @@ distributed actor MyDistActor {
6464
// CHECK: [[CONTINUE]]:
6565
// CHECK: hop_to_executor [[SELF]] : $MyDistActor
6666
// CHECK-NEXT: retain_value [[SYSTEM]] : $FakeActorSystem
67-
// CHECK-NEXT: retain_value [[SYSTEM]] : $FakeActorSystem
6867
// CHECK-NEXT: // function_ref FakeActorSystem.actorReady<A>(_:)
6968
// CHECK-NEXT: [[READY_FN:%[0-9]+]] = function_ref @$s27FakeDistributedActorSystems0aC6SystemV10actorReadyyyx01_B00bC0RzAA0C7AddressV2IDRtzlF : $@convention(method) <τ_0_0 where τ_0_0 : DistributedActor, τ_0_0.ID == ActorAddress> (@guaranteed τ_0_0, @guaranteed FakeActorSystem) -> ()
7069
// CHECK-NEXT: = apply [[READY_FN]]

test/SILGen/moveonly_builtin.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,10 +31,10 @@ class Klass {}
3131
// CHECK-SIL-NEXT: debug_value
3232
// CHECK-SIL-NEXT: strong_retain
3333
// CHECK-SIL-NEXT: move_value
34+
// CHECK-SIL-NEXT: strong_release
3435
// CHECK-SIL-NEXT: debug_value [moved] undef
3536
// CHECK-SIL-NEXT: tuple
3637
// CHECK-SIL-NEXT: tuple
37-
// CHECK-SIL-NEXT: strong_release
3838
// CHECK-SIL-NEXT: return
3939
// CHECK-SIL: } // end sil function '$s8moveonly7useMoveyAA5KlassCADnF'
4040
func useMove(_ k: __owned Klass) -> Klass {
@@ -65,10 +65,10 @@ func useMove(_ k: __owned Klass) -> Klass {
6565
// CHECK-SIL-NEXT: debug_value
6666
// CHECK-SIL-NEXT: strong_retain
6767
// CHECK-SIL-NEXT: move_value
68+
// CHECK-SIL-NEXT: strong_release
6869
// CHECK-SIL-NEXT: debug_value [moved] undef
6970
// CHECK-SIL-NEXT: tuple
7071
// CHECK-SIL-NEXT: tuple
71-
// CHECK-SIL-NEXT: strong_release
7272
// CHECK-SIL-NEXT: return
7373
// CHECK-SIL: } // end sil function '$s8moveonly7useMoveyxxnRlzClF'
7474
func useMove<T : AnyObject>(_ k: __owned T) -> T {

test/SILOptimizer/copy_propagation_borrow.sil

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -233,12 +233,13 @@ bb3:
233233
// CHECK: bb1:
234234
// CHECK-NEXT: [[COPY:%.*]] = copy_value [[OUTERCOPY]] : $C
235235
// CHECK-NEXT: apply %{{.*}}([[COPY]]) : $@convention(thin) (@owned C) -> ()
236+
// CHECK-NEXT: destroy_value %0 : $C
236237
// CHECK-NEXT: br bb3
237238
// CHECK: bb2:
239+
// CHECK-NEXT: destroy_value %0 : $C
238240
// CHECK-NEXT: br bb3
239241
// CHECK: bb3:
240242
// CHECK-NEXT: destroy_value [[OUTERCOPY]] : $C
241-
// CHECK-NEXT: destroy_value %0 : $C
242243
// CHECK-LABEL: } // end sil function 'testLocalBorrowPostDomDestroy'
243244
sil [ossa] @testLocalBorrowPostDomDestroy : $@convention(thin) (@owned C) -> () {
244245
bb0(%0 : @owned $C):
@@ -274,13 +275,14 @@ bb3:
274275
// CHECK: bb1:
275276
// CHECK-NEXT: apply %{{.*}}([[OUTERCOPY]]) : $@convention(thin) (@guaranteed C) -> ()
276277
// CHECK-NEXT: apply %{{.*}}([[OUTERCOPY]]) : $@convention(thin) (@owned C) -> ()
278+
// CHECK-NEXT: destroy_value %0 : $C
277279
// CHECK-NEXT: br bb3
278280
// CHECK: bb2:
279281
// CHECK-NEXT: apply %{{.*}}([[OUTERCOPY]]) : $@convention(thin) (@guaranteed C) -> ()
282+
// CHECK-NEXT: destroy_value %0 : $C
280283
// CHECK-NEXT: destroy_value [[OUTERCOPY]] : $C
281284
// CHECK-NEXT: br bb3
282285
// CHECK: bb3:
283-
// CHECK-NEXT: destroy_value %0 : $C
284286
// CHECK-LABEL: } // end sil function 'testLocalBorrowNoPostDomDestroy'
285287
sil [ossa] @testLocalBorrowNoPostDomDestroy : $@convention(thin) (@owned C) -> () {
286288
bb0(%0 : @owned $C):
@@ -316,17 +318,18 @@ bb3:
316318
// CHECK-NEXT: apply %{{.*}}([[OUTERCOPY]]) : $@convention(thin) (@guaranteed C) -> ()
317319
// CHECK-NEXT: [[ARGCOPY:%.*]] = copy_value [[OUTERCOPY]] : $C
318320
// CHECK-NEXT: apply %2([[OUTERCOPY]], [[ARGCOPY:%.*]]) : $@convention(thin) (@owned C, @owned C) -> ()
321+
// CHECK-NEXT: destroy_value %0 : $C
319322
// CHECK-NEXT: br bb3
320323
// CHECK: bb2:
321324
// CHECK-NEXT: apply %{{.*}}([[OUTERCOPY]]) : $@convention(thin) (@guaranteed C) -> ()
322325
//
323326
// This copy would be eliminated if the outer lifetime were also canonicalized (no unchecked_ownership_conversion)
327+
// CHECK-NEXT: destroy_value %0 : $C
324328
// CHECK-NEXT: [[COPY2:%.*]] = copy_value [[OUTERCOPY]] : $C
325329
// CHECK-NEXT: destroy_value [[COPY2]] : $C
326330
// CHECK-NEXT: destroy_value %4 : $C
327331
// CHECK-NEXT: br bb3
328332
// CHECK: bb3:
329-
// CHECK-NEXT: destroy_value %0 : $C
330333
// CHECK-LABEL: } // end sil function 'testLocalBorrowDoubleConsume'
331334
sil [ossa] @testLocalBorrowDoubleConsume : $@convention(thin) (@owned C) -> () {
332335
bb0(%0 : @owned $C):

test/SILOptimizer/copy_propagation_opaque.sil

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -440,7 +440,9 @@ bb0:
440440
//
441441
// CHECK-LABEL: sil [ossa] @testCopyBorrow : $@convention(thin) <T> (@in T) -> () {
442442
// CHECK: bb0(%0 : @owned $T):
443+
// CHECK: [[COPY:%[^,]+]] = copy_value %0
443444
// CHECK: destroy_value %0 : $T
445+
// CHECK: destroy_value [[COPY]]
444446
// CHECK-NEXT: tuple
445447
// CHECK-NEXT: return
446448
// CHECK-LABEL: } // end sil function 'testCopyBorrow'

test/SILOptimizer/shrink_borrow_scope.sil

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -856,13 +856,14 @@ bad(%15 : @owned $Error):
856856
// CHECK: [[LEFT]]:
857857
// CHECK: apply undef()
858858
// CHECK: end_borrow [[LIFETIME]]
859+
// CHECK: destroy_value [[INSTANCE]]
859860
// CHECK: br [[EXIT:bb[0-9]+]]
860861
// CHECK: [[RIGHT]]:
861862
// CHECK: apply undef()
862863
// CHECK: end_borrow [[LIFETIME]]
864+
// CHECK: destroy_value [[INSTANCE]]
863865
// CHECK: br [[EXIT]]
864866
// CHECK: [[EXIT]]:
865-
// CHECK: destroy_value [[INSTANCE]]
866867
// CHECK-LABEL: } // end sil function 'hoist_up_to_two_barrier_applies'
867868
sil [ossa] @hoist_up_to_two_barrier_applies : $@convention(thin) (@owned C) -> () {
868869
entry(%instance : @owned $C):

test/SILOptimizer/shrink_borrow_scope.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,8 @@ public func eliminate_copy_of_returned_then_consumed_owned_value(arg: __owned An
3131
// CHECK: end_borrow [[ARG_LIFETIME]]
3232
// release result
3333
// release arg
34-
// CHECK: destroy_value [[RESULT]]
3534
// CHECK: destroy_value [[ARG]]
35+
// CHECK: destroy_value [[RESULT]]
3636
// CHECK-LABEL: } // end sil function 'eliminate_copy_of_returned_then_consumed_owned_value'
3737
}
3838

0 commit comments

Comments
 (0)