Skip to content

Commit 4b85d0a

Browse files
committed
[DestroyHoisting] Barriers use callee analysis.
Pass a BasicCalleeAnalysis instance to isDeinitBarrier. This enables LexicalDestroyHoisting to hoist destroys over applies of functions which are not deinit barriers.
1 parent f85074d commit 4b85d0a

File tree

4 files changed

+39
-12
lines changed

4 files changed

+39
-12
lines changed

include/swift/SILOptimizer/Utils/CanonicalizeBorrowScope.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -160,7 +160,8 @@ MoveValueInst *foldDestroysOfCopiedLexicalBorrow(BeginBorrowInst *bbi,
160160

161161
bool hoistDestroysOfOwnedLexicalValue(SILValue const value,
162162
SILFunction &function,
163-
InstructionDeleter &deleter);
163+
InstructionDeleter &deleter,
164+
BasicCalleeAnalysis *calleeAnalysis);
164165

165166
} // namespace swift
166167

lib/SILOptimizer/Transforms/CopyPropagation.cpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -476,15 +476,16 @@ void CopyPropagation::run() {
476476
auto folded = foldDestroysOfCopiedLexicalBorrow(bbi, *domTree, deleter);
477477
if (!folded)
478478
break;
479-
auto hoisted = hoistDestroysOfOwnedLexicalValue(folded, *f, deleter);
479+
auto hoisted =
480+
hoistDestroysOfOwnedLexicalValue(folded, *f, deleter, calleeAnalysis);
480481
// Keep running even if the new move's destroys can't be hoisted.
481482
(void)hoisted;
482483
firstRun = false;
483484
}
484485
}
485486
for (auto *argument : f->getArguments()) {
486487
if (argument->getOwnershipKind() == OwnershipKind::Owned) {
487-
hoistDestroysOfOwnedLexicalValue(argument, *f, deleter);
488+
hoistDestroysOfOwnedLexicalValue(argument, *f, deleter, calleeAnalysis);
488489
}
489490
}
490491
deleter.cleanupDeadInstructions();

lib/SILOptimizer/Utils/LexicalDestroyHoisting.cpp

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -51,11 +51,13 @@ struct Context final {
5151

5252
InstructionDeleter &deleter;
5353

54+
BasicCalleeAnalysis *calleeAnalysis;
55+
5456
Context(SILValue const &value, SILFunction &function,
55-
InstructionDeleter &deleter)
57+
InstructionDeleter &deleter, BasicCalleeAnalysis *calleeAnalysis)
5658
: value(value), definition(value->getDefiningInstruction()),
57-
defBlock(value->getParentBlock()), function(function),
58-
deleter(deleter) {
59+
defBlock(value->getParentBlock()), function(function), deleter(deleter),
60+
calleeAnalysis(calleeAnalysis) {
5961
assert(value->isLexical());
6062
assert(value->getOwnershipKind() == OwnershipKind::Owned);
6163
}
@@ -195,7 +197,7 @@ Dataflow::classifyInstruction(SILInstruction *instruction) {
195197
? Classification::Barrier
196198
: Classification::Other;
197199
}
198-
if (isDeinitBarrier(instruction, nullptr)) {
200+
if (isDeinitBarrier(instruction, context.calleeAnalysis)) {
199201
return Classification::Barrier;
200202
}
201203
return Classification::Other;
@@ -386,13 +388,14 @@ bool run(Context &context) {
386388
}
387389
} // end namespace LexicalDestroyHoisting
388390

389-
bool swift::hoistDestroysOfOwnedLexicalValue(SILValue const value,
390-
SILFunction &function,
391-
InstructionDeleter &deleter) {
391+
bool swift::hoistDestroysOfOwnedLexicalValue(
392+
SILValue const value, SILFunction &function, InstructionDeleter &deleter,
393+
BasicCalleeAnalysis *calleeAnalysis) {
392394
if (!value->isLexical())
393395
return false;
394396
if (value->getOwnershipKind() != OwnershipKind::Owned)
395397
return false;
396-
LexicalDestroyHoisting::Context context(value, function, deleter);
398+
LexicalDestroyHoisting::Context context(value, function, deleter,
399+
calleeAnalysis);
397400
return LexicalDestroyHoisting::run(context);
398401
}

test/SILOptimizer/lexical_destroy_hoisting.sil

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1-
// RUN: %target-sil-opt -copy-propagation -enable-sil-verify-all %s | %FileCheck %s
1+
// RUN: %target-sil-opt -compute-side-effects -copy-propagation -enable-sil-verify-all %s | %FileCheck %s
2+
3+
// REQUIRES: swift_in_compiler
24

35
import Builtin
46
import Swift
@@ -7,6 +9,7 @@ import Swift
79
// = DECLARATIONS {{
810
// =============================================================================
911

12+
class X {}
1013
class C {
1114
weak var d: D?
1215
}
@@ -36,6 +39,11 @@ sil [ossa] @callee_optional_d_guaranteed: $@convention(thin) (@guaranteed Option
3639
sil [ossa] @synchronization_point : $@convention(thin) () -> ()
3740
sil [ossa] @modify_s : $@yield_once @convention(thin) () -> @yields @inout S
3841
sil [ossa] @barrier : $@convention(thin) () -> ()
42+
// This function is not a synchronization point.
43+
sil [ossa] @empty : $@convention(thin) () -> () {
44+
%retval = tuple ()
45+
return %retval : $()
46+
}
3947

4048

4149
// =============================================================================
@@ -471,6 +479,20 @@ bb0(%0 : @owned $Optional<@Sendable @callee_guaranteed () -> ()>):
471479
return %copy : $Optional<@callee_guaranteed () -> ()>
472480
}
473481

482+
// Hoist over applies of non-barrier fns.
483+
// CHECK-LABEL: sil [ossa] @hoist_over_apply_of_non_barrier_fn : {{.*}} {
484+
// CHECK: destroy_value
485+
// CHECK: apply
486+
// CHECK-LABEL: } // end sil function 'hoist_over_apply_of_non_barrier_fn'
487+
sil [ossa] @hoist_over_apply_of_non_barrier_fn : $@convention(thin) (@owned X) -> () {
488+
entry(%instance : @owned $X):
489+
%empty = function_ref @empty : $@convention(thin) () -> ()
490+
apply %empty() : $@convention(thin) () -> ()
491+
destroy_value %instance : $X
492+
%retval = tuple ()
493+
return %retval : $()
494+
}
495+
474496
// =============================================================================
475497
// instruction tests }}
476498
// =============================================================================

0 commit comments

Comments
 (0)