Skip to content

Commit 480d3f4

Browse files
committed
SIL Optimizer: handle begin_dealloc_ref and end_init_let_ref in various optimizations
1 parent 8571ea0 commit 480d3f4

File tree

5 files changed

+33
-2
lines changed

5 files changed

+33
-2
lines changed

lib/SIL/Utils/MemAccessUtils.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -778,6 +778,7 @@ bool swift::isIdentityPreservingRefCast(SingleValueInstruction *svi) {
778778
return isa<CopyValueInst>(svi) || isa<BeginBorrowInst>(svi)
779779
|| isa<EndInitLetRefInst>(svi)
780780
|| isa<BeginDeallocRefInst>(svi)
781+
|| isa<EndCOWMutationInst>(svi)
781782
|| isIdentityAndOwnershipPreservingRefCast(svi);
782783
}
783784

lib/SILOptimizer/Analysis/ARCAnalysis.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -176,6 +176,8 @@ bool swift::canUseObject(SILInstruction *Inst) {
176176
case SILInstructionKind::RawPointerToRefInst:
177177
case SILInstructionKind::UnconditionalCheckedCastInst:
178178
case SILInstructionKind::UncheckedBitwiseCastInst:
179+
case SILInstructionKind::EndInitLetRefInst:
180+
case SILInstructionKind::BeginDeallocRefInst:
179181
return false;
180182

181183
// If we have a trivial bit cast between trivial types, it is not something

lib/SILOptimizer/Transforms/DeadObjectElimination.cpp

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -263,8 +263,9 @@ static bool canZapInstruction(SILInstruction *Inst, bool acceptRefCountInsts,
263263
isa<MoveValueInst>(Inst)) {
264264
return true;
265265
}
266-
if (isa<BeginDeallocRefInst>(Inst) || isa<FixLifetimeInst>(Inst) ||
267-
isa<EndBorrowInst>(Inst))
266+
if (isa<EndInitLetRefInst>(Inst) || isa<BeginDeallocRefInst>(Inst) ||
267+
isa<FixLifetimeInst>(Inst) || isa<EndBorrowInst>(Inst) ||
268+
isa<UpcastInst>(Inst) || isa<UncheckedRefCastInst>(Inst))
268269
return true;
269270

270271
// It is ok to eliminate various retains/releases. We are either removing
@@ -336,6 +337,14 @@ static bool onlyStoresToTailObjects(BuiltinInst *destroyArray,
336337

337338
// Check if the destroyArray destroys the tail elements of allocRef.
338339
auto destroyPath = AccessPath::compute(destroyArray->getArguments()[1]);
340+
AccessStorage storage = destroyPath.getStorage();
341+
if (auto *beginDealloc = dyn_cast<BeginDeallocRefInst>(storage.getRoot())) {
342+
destroyPath = AccessPath(
343+
storage.transformReference(beginDealloc->getAllocation()),
344+
destroyPath.getPathNode(),
345+
destroyPath.getOffset());
346+
}
347+
339348
if (destroyPath != AccessPath::forTailStorage(allocRef))
340349
return false;
341350

lib/SILOptimizer/Transforms/PerformanceInliner.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -497,6 +497,8 @@ bool SILPerformanceInliner::isProfitableToInline(
497497
}
498498
} else if (auto ri = dyn_cast<ReturnInst>(&I)) {
499499
SILValue retVal = ri->getOperand();
500+
if (auto *eir = dyn_cast<EndInitLetRefInst>(retVal))
501+
retVal = eir->getOperand();
500502
if (auto *uci = dyn_cast<UpcastInst>(retVal))
501503
retVal = uci->getOperand();
502504

test/SILOptimizer/retain_release_code_motion.sil

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1106,3 +1106,20 @@ bb0(%0 : $Y, %1 : $Act):
11061106
strong_release %3 : $C2
11071107
return %7 : $A
11081108
}
1109+
1110+
// CHECK-RELEASE-HOISTING-LABEL: sil @hoist_release_over_end_init_let_ref :
1111+
// CHECK-RELEASE-HOISTING: alloc_ref
1112+
// CHECK-RELEASE-HOISTING-NEXT: strong_release
1113+
// CHECK-RELEASE-HOISTING-NEXT: br bb1
1114+
// CHECK-RELEASE-HOISTING: } // end sil function 'hoist_release_over_end_init_let_ref'
1115+
sil @hoist_release_over_end_init_let_ref : $@convention(thin) (@guaranteed B) -> () {
1116+
bb0(%0 : $B):
1117+
%1 = alloc_ref $C
1118+
br bb1
1119+
bb1:
1120+
%3 = end_init_let_ref %0 : $B
1121+
strong_release %1 : $C
1122+
%5 = tuple()
1123+
return %5 : $()
1124+
}
1125+

0 commit comments

Comments
 (0)