Skip to content

Commit 60c6bb0

Browse files
committed
[ShrinkBorrowScope] Barriers use callee analysis.
Pass a BasicCalleeAnalysis instance to isDeinitBarrier. This will allow ShrinkBorrowScope to hoist end_borrows over applies of functions which are not deinit barriers.
1 parent 4481ba8 commit 60c6bb0

File tree

4 files changed

+45
-6
lines changed

4 files changed

+45
-6
lines changed

include/swift/SILOptimizer/Utils/CanonicalizeBorrowScope.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,8 @@
3737

3838
namespace swift {
3939

40+
class BasicCalleeAnalysis;
41+
4042
//===----------------------------------------------------------------------===//
4143
// MARK: CanonicalizeBorrowScope
4244
//===----------------------------------------------------------------------===//
@@ -149,6 +151,7 @@ class CanonicalizeBorrowScope {
149151

150152
bool shrinkBorrowScope(
151153
BeginBorrowInst const &bbi, InstructionDeleter &deleter,
154+
BasicCalleeAnalysis *calleeAnalysis,
152155
SmallVectorImpl<CopyValueInst *> &modifiedCopyValueInsts);
153156

154157
MoveValueInst *foldDestroysOfCopiedLexicalBorrow(BeginBorrowInst *bbi,

lib/SILOptimizer/Transforms/CopyPropagation.cpp

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@
4343
#include "swift/SIL/BasicBlockUtils.h"
4444
#include "swift/SIL/DebugUtils.h"
4545
#include "swift/SIL/SILUndef.h"
46+
#include "swift/SILOptimizer/Analysis/BasicCalleeAnalysis.h"
4647
#include "swift/SILOptimizer/Analysis/DeadEndBlocksAnalysis.h"
4748
#include "swift/SILOptimizer/PassManager/Passes.h"
4849
#include "swift/SILOptimizer/PassManager/Transforms.h"
@@ -440,6 +441,7 @@ void CopyPropagation::run() {
440441
CanonicalizeOSSALifetime canonicalizer(
441442
pruneDebug, /*maximizeLifetime=*/!getFunction()->shouldOptimize(),
442443
accessBlockAnalysis, domTree, deleter);
444+
auto *calleeAnalysis = getAnalysis<BasicCalleeAnalysis>();
443445

444446
// NOTE: We assume that the function is in reverse post order so visiting the
445447
// blocks and pushing begin_borrows as we see them and then popping them
@@ -453,7 +455,8 @@ void CopyPropagation::run() {
453455
// at least once and then until each stops making changes.
454456
while (true) {
455457
SmallVector<CopyValueInst *, 4> modifiedCopyValueInsts;
456-
auto shrunk = shrinkBorrowScope(*bbi, deleter, modifiedCopyValueInsts);
458+
auto shrunk = shrinkBorrowScope(*bbi, deleter, calleeAnalysis,
459+
modifiedCopyValueInsts);
457460
for (auto *cvi : modifiedCopyValueInsts)
458461
defWorklist.updateForCopy(cvi);
459462
changed |= shrunk;

lib/SILOptimizer/Utils/ShrinkBorrowScope.cpp

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -62,13 +62,16 @@ struct Context final {
6262

6363
InstructionDeleter &deleter;
6464

65+
BasicCalleeAnalysis *calleeAnalysis;
66+
6567
Context(BeginBorrowInst const &introducer,
6668
SmallVectorImpl<CopyValueInst *> &modifiedCopyValueInsts,
67-
InstructionDeleter &deleter)
69+
InstructionDeleter &deleter, BasicCalleeAnalysis *calleeAnalysis)
6870
: introducer(introducer), borrowedValue(BorrowedValue(&introducer)),
6971
borrowee(introducer.getOperand()), defBlock(introducer.getParent()),
7072
function(*introducer.getFunction()),
71-
modifiedCopyValueInsts(modifiedCopyValueInsts), deleter(deleter) {}
73+
modifiedCopyValueInsts(modifiedCopyValueInsts), deleter(deleter),
74+
calleeAnalysis(calleeAnalysis) {}
7275
Context(Context const &) = delete;
7376
Context &operator=(Context const &) = delete;
7477
};
@@ -245,7 +248,7 @@ Dataflow::classifyInstruction(SILInstruction *instruction) {
245248
? Classification::Barrier
246249
: Classification::Other;
247250
}
248-
if (isDeinitBarrier(instruction, nullptr)) {
251+
if (isDeinitBarrier(instruction, context.calleeAnalysis)) {
249252
return Classification::Barrier;
250253
}
251254
return Classification::Other;
@@ -450,7 +453,9 @@ bool run(Context &context) {
450453

451454
bool swift::shrinkBorrowScope(
452455
BeginBorrowInst const &bbi, InstructionDeleter &deleter,
456+
BasicCalleeAnalysis *calleeAnalysis,
453457
SmallVectorImpl<CopyValueInst *> &modifiedCopyValueInsts) {
454-
ShrinkBorrowScope::Context context(bbi, modifiedCopyValueInsts, deleter);
458+
ShrinkBorrowScope::Context context(bbi, modifiedCopyValueInsts, deleter,
459+
calleeAnalysis);
455460
return ShrinkBorrowScope::run(context);
456461
}

test/SILOptimizer/shrink_borrow_scope.sil

Lines changed: 29 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
@@ -50,6 +52,11 @@ sil [ossa] @synchronization_point : $@convention(thin) () -> ()
5052
sil [ossa] @modify_s : $@yield_once @convention(thin) () -> @yields @inout S
5153
sil [ossa] @barrier : $@convention(thin) () -> ()
5254
sil [ossa] @failable : $@convention(thin) () -> @error Error
55+
// This function is not a synchronization point.
56+
sil [ossa] @empty : $@convention(thin) () -> () {
57+
%retval = tuple ()
58+
return %retval : $()
59+
}
5360

5461

5562
// =============================================================================
@@ -1094,6 +1101,27 @@ bb0(%0 : $*ClassWrapper):
10941101
return %23 : $()
10951102
}
10961103

1104+
// Hoist over applies of non-barrier fns.
1105+
// CHECK-LABEL: sil [ossa] @hoist_over_apply_of_non_barrier_fn : {{.*}} {
1106+
// CHECK: apply
1107+
// CHECK: end_borrow
1108+
// CHECK: apply
1109+
// CHECK-LABEL: } // end sil function 'hoist_over_apply_of_non_barrier_fn'
1110+
sil [ossa] @hoist_over_apply_of_non_barrier_fn : $@convention(thin) () -> () {
1111+
entry:
1112+
%get_owned_c = function_ref @get_owned_c : $@convention(thin) () -> (@owned C)
1113+
%c = apply %get_owned_c() : $@convention(thin) () -> (@owned C)
1114+
%borrow = begin_borrow [lexical] %c : $C
1115+
%callee_guaranteed = function_ref @callee_guaranteed : $@convention(thin) (@guaranteed C) -> ()
1116+
apply %callee_guaranteed(%borrow) : $@convention(thin) (@guaranteed C) -> ()
1117+
%empty = function_ref @empty : $@convention(thin) () -> ()
1118+
apply %empty() : $@convention(thin) () -> ()
1119+
end_borrow %borrow : $C
1120+
destroy_value %c : $C
1121+
%retval = tuple ()
1122+
return %retval : $()
1123+
}
1124+
10971125
// =============================================================================
10981126
// instruction tests }}
10991127
// =============================================================================

0 commit comments

Comments
 (0)