Skip to content

Commit ceea4cf

Browse files
committed
[AllocBoxToStack] See through borrows.
Now that alloc_boxes whose lifetimes are lexical are emitted with begin_borrow [lexical]/end_borrow, AllocBoxToStack needs to be able to see through those new borrow scopes in order to continue stack promoting the same boxes that it was able to before those lexical scopes were emitted.
1 parent 4bac557 commit ceea4cf

File tree

2 files changed

+21
-10
lines changed

2 files changed

+21
-10
lines changed

lib/SIL/IR/SILBuilder.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -285,6 +285,10 @@ SILBuilder::emitDestroyAddr(SILLocation Loc, SILValue Operand) {
285285
if (isa<DeallocStackInst>(Inst))
286286
continue;
287287

288+
// end_borrow insts also don't affect take-ability
289+
if (isa<EndBorrowInst>(Inst))
290+
continue;
291+
288292
// An end_access of the same address may be able to be rewritten as a
289293
// [deinit] access.
290294
if (auto endAccess = dyn_cast<EndAccessInst>(Inst)) {

lib/SILOptimizer/Transforms/AllocBoxToStack.cpp

Lines changed: 17 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -172,7 +172,8 @@ static bool getFinalReleases(SILValue Box,
172172

173173
// If we have a copy value or a mark_uninitialized, add its uses to the work
174174
// list and continue.
175-
if (isa<MarkUninitializedInst>(User) || isa<CopyValueInst>(User)) {
175+
if (isa<MarkUninitializedInst>(User) || isa<CopyValueInst>(User) ||
176+
isa<BeginBorrowInst>(User)) {
176177
llvm::copy(cast<SingleValueInstruction>(User)->getUses(),
177178
std::back_inserter(Worklist));
178179
continue;
@@ -385,12 +386,14 @@ static SILInstruction *recursivelyFindBoxOperandsPromotableToAddress(
385386
// Projections are fine as well.
386387
if (isa<StrongRetainInst>(User) || isa<StrongReleaseInst>(User) ||
387388
isa<ProjectBoxInst>(User) || isa<DestroyValueInst>(User) ||
388-
(!inAppliedFunction && isa<DeallocBoxInst>(User)))
389+
(!inAppliedFunction && isa<DeallocBoxInst>(User)) ||
390+
isa<EndBorrowInst>(User))
389391
continue;
390392

391393
// If our user instruction is a copy_value or a mark_uninitialized, visit
392394
// the users recursively.
393-
if (isa<MarkUninitializedInst>(User) || isa<CopyValueInst>(User)) {
395+
if (isa<MarkUninitializedInst>(User) || isa<CopyValueInst>(User) ||
396+
isa<BeginBorrowInst>(User)) {
394397
llvm::copy(cast<SingleValueInstruction>(User)->getUses(),
395398
std::back_inserter(Worklist));
396399
continue;
@@ -502,10 +505,12 @@ static void replaceProjectBoxUsers(SILValue HeapBox, SILValue StackBox) {
502505
continue;
503506
}
504507

505-
auto *CVI = dyn_cast<CopyValueInst>(Op->getUser());
506-
if (!CVI)
507-
continue;
508-
llvm::copy(CVI->getUses(), std::back_inserter(Worklist));
508+
auto *User = Op->getUser();
509+
if (isa<MarkUninitializedInst>(User) || isa<CopyValueInst>(User) ||
510+
isa<BeginBorrowInst>(User)) {
511+
llvm::copy(cast<SingleValueInstruction>(User)->getUses(),
512+
std::back_inserter(Worklist));
513+
}
509514
}
510515
}
511516

@@ -578,8 +583,9 @@ static bool rewriteAllocBoxAsAllocStack(AllocBoxInst *ABI) {
578583
while (!Worklist.empty()) {
579584
auto *User = Worklist.pop_back_val();
580585

581-
// Look through any mark_uninitialized, copy_values.
582-
if (isa<MarkUninitializedInst>(User) || isa<CopyValueInst>(User)) {
586+
// Look through any mark_uninitialized, copy_values, begin_borrow.
587+
if (isa<MarkUninitializedInst>(User) || isa<CopyValueInst>(User) ||
588+
isa<BeginBorrowInst>(User)) {
583589
auto Inst = cast<SingleValueInstruction>(User);
584590
llvm::transform(Inst->getUses(), std::back_inserter(Worklist),
585591
[](Operand *Op) -> SILInstruction * {
@@ -592,7 +598,7 @@ static bool rewriteAllocBoxAsAllocStack(AllocBoxInst *ABI) {
592598

593599
assert(isa<StrongReleaseInst>(User) || isa<StrongRetainInst>(User) ||
594600
isa<DeallocBoxInst>(User) || isa<ProjectBoxInst>(User) ||
595-
isa<DestroyValueInst>(User));
601+
isa<DestroyValueInst>(User) || isa<EndBorrowInst>(User));
596602

597603
User->eraseFromParent();
598604
}
@@ -934,6 +940,7 @@ specializeApplySite(SILOptFunctionBuilder &FuncBuilder, ApplySite Apply,
934940
assert((isa<SingleValueInstruction>(Box) && isa<AllocBoxInst>(Box) ||
935941
isa<CopyValueInst>(Box) ||
936942
isa<MarkUninitializedInst>(Box) ||
943+
isa<BeginBorrowInst>(Box) ||
937944
isa<SILFunctionArgument>(Box)) &&
938945
"Expected either an alloc box or a copy of an alloc box or a "
939946
"function argument");

0 commit comments

Comments
 (0)