Skip to content

Commit 4bac557

Browse files
committed
[SILGen] Added borrow scopes to alloc_boxes.
Because AllocBoxToStack is not able to transform every alloc_box into an alloc_stack, it's necessary to add begin_borrow [lexical] to every alloc_box in order to provide lexical scopes for those alloc_boxes which will not be transformed into alloc_stacks.
1 parent 2b5857b commit 4bac557

File tree

4 files changed

+41
-5
lines changed

4 files changed

+41
-5
lines changed

lib/SILGen/ResultPlan.cpp

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,13 @@ class IndirectOpenedSelfCleanup final : public Cleanup {
6666
void emit(SILGenFunction &SGF, CleanupLocation loc, ForUnwind_t forUnwind)
6767
override {
6868
assert(box && "buffer never emitted before activating cleanup?!");
69-
SGF.B.createDeallocBox(loc, box);
69+
auto theBox = box;
70+
if (SGF.getASTContext().SILOpts.supportsLexicalLifetimes(SGF.getModule())) {
71+
auto *bbi = cast<BeginBorrowInst>(theBox);
72+
SGF.B.createEndBorrow(loc, bbi);
73+
theBox = bbi->getOperand();
74+
}
75+
SGF.B.createDeallocBox(loc, theBox);
7076
}
7177

7278
void dump(SILGenFunction &SGF) const override {
@@ -160,7 +166,10 @@ class IndirectOpenedSelfResultPlan final : public ResultPlan {
160166
SILBoxType::get(SGF.getASTContext(),
161167
boxLayout,
162168
layoutSubs));
163-
169+
if (SGF.getASTContext().SILOpts.supportsLexicalLifetimes(SGF.getModule())) {
170+
resultBox = SGF.B.createBeginBorrow(loc, resultBox, /*isLexical=*/true);
171+
}
172+
164173
// Complete the cleanup to deallocate this buffer later, after we're
165174
// finished with the argument.
166175
static_cast<IndirectOpenedSelfCleanup&>(SGF.Cleanups.getCleanup(handle))

lib/SILGen/SILGenApply.cpp

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3505,7 +3505,13 @@ class DeallocateUninitializedBox : public Cleanup {
35053505
DeallocateUninitializedBox(SILValue box) : box(box) {}
35063506

35073507
void emit(SILGenFunction &SGF, CleanupLocation l, ForUnwind_t forUnwind) override {
3508-
SGF.B.createDeallocBox(l, box);
3508+
auto theBox = box;
3509+
if (SGF.getASTContext().SILOpts.supportsLexicalLifetimes(SGF.getModule())) {
3510+
auto *bbi = cast<BeginBorrowInst>(theBox);
3511+
SGF.B.createEndBorrow(l, bbi);
3512+
theBox = bbi->getOperand();
3513+
}
3514+
SGF.B.createDeallocBox(l, theBox);
35093515
}
35103516

35113517
void dump(SILGenFunction &SGF) const override {

lib/SILGen/SILGenDecl.cpp

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -297,7 +297,13 @@ class DeallocateUninitializedLocalVariable : public Cleanup {
297297

298298
void emit(SILGenFunction &SGF, CleanupLocation l,
299299
ForUnwind_t forUnwind) override {
300-
SGF.B.createDeallocBox(l, Box);
300+
auto box = Box;
301+
if (SGF.getASTContext().SILOpts.supportsLexicalLifetimes(SGF.getModule())) {
302+
auto *bbi = cast<BeginBorrowInst>(box);
303+
SGF.B.createEndBorrow(l, bbi);
304+
box = bbi->getOperand();
305+
}
306+
SGF.B.createDeallocBox(l, box);
301307
}
302308

303309
void dump(SILGenFunction &) const override {
@@ -360,6 +366,10 @@ class LocalVariableInitialization : public SingleBufferInitialization {
360366
if (kind)
361367
Box = SGF.B.createMarkUninitialized(decl, Box, kind.getValue());
362368

369+
if (SGF.getASTContext().SILOpts.supportsLexicalLifetimes(SGF.getModule())) {
370+
Box = SGF.B.createBeginBorrow(decl, Box, /*isLexical=*/true);
371+
}
372+
363373
Addr = SGF.B.createProjectBox(decl, Box, 0);
364374

365375
// Push a cleanup to destroy the local variable. This has to be
@@ -1751,7 +1761,15 @@ void SILGenFunction::destroyLocalVariable(SILLocation silLoc, VarDecl *vd) {
17511761
// For a heap variable, the box is responsible for the value. We just need
17521762
// to give up our retain count on it.
17531763
if (loc.box) {
1754-
B.emitDestroyValueOperation(silLoc, loc.box);
1764+
if (!getASTContext().SILOpts.supportsLexicalLifetimes(getModule())) {
1765+
B.emitDestroyValueOperation(silLoc, loc.box);
1766+
return;
1767+
}
1768+
1769+
auto *bbi = cast<BeginBorrowInst>(loc.box);
1770+
B.createEndBorrow(silLoc, bbi);
1771+
B.emitDestroyValueOperation(silLoc, bbi->getOperand());
1772+
17551773
return;
17561774
}
17571775

lib/SILGen/Scope.cpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,9 @@ static void lifetimeExtendAddressOnlyRValueSubValues(
5757
"addresses must be address only.");
5858
auto boxTy = SILBoxType::get(v->getType().getASTType());
5959
SILValue box = SGF.B.createAllocBox(loc, boxTy);
60+
if (SGF.getASTContext().SILOpts.supportsLexicalLifetimes(SGF.getModule())) {
61+
box = SGF.B.createBeginBorrow(loc, box, /*isLexical=*/true);
62+
}
6063
SILValue addr = SGF.B.createProjectBox(loc, box, 0);
6164
SGF.B.createCopyAddr(loc, v, addr, IsTake, IsInitialization);
6265

0 commit comments

Comments
 (0)