Skip to content

Commit bd79eeb

Browse files
committed
Merge remote-tracking branch 'origin/main' into rebranch
2 parents 7f6db10 + 5fcac08 commit bd79eeb

File tree

78 files changed

+736
-323
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

78 files changed

+736
-323
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/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

lib/SILOptimizer/Mandatory/AccessEnforcementSelection.cpp

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -227,6 +227,11 @@ void SelectEnforcement::analyzeUsesOfBox(SingleValueInstruction *source) {
227227
for (auto use : source->getUses()) {
228228
auto user = use->getUser();
229229

230+
if (auto BBI = dyn_cast<BeginBorrowInst>(user)) {
231+
analyzeUsesOfBox(BBI);
232+
continue;
233+
}
234+
230235
if (auto MUI = dyn_cast<MarkUninitializedInst>(user)) {
231236
analyzeUsesOfBox(MUI);
232237
continue;
@@ -238,10 +243,9 @@ void SelectEnforcement::analyzeUsesOfBox(SingleValueInstruction *source) {
238243
}
239244

240245
// Ignore certain other uses that do not capture the value.
241-
if (isa<StrongRetainInst>(user) ||
242-
isa<StrongReleaseInst>(user) ||
243-
isa<DestroyValueInst>(user) ||
244-
isa<DeallocBoxInst>(user))
246+
if (isa<StrongRetainInst>(user) || isa<StrongReleaseInst>(user) ||
247+
isa<DestroyValueInst>(user) || isa<DeallocBoxInst>(user) ||
248+
isa<EndBorrowInst>(user))
245249
continue;
246250

247251
// Treat everything else as an escape.
@@ -667,6 +671,8 @@ void AccessEnforcementSelection::processFunction(SILFunction *F) {
667671
SourceAccess
668672
AccessEnforcementSelection::getAccessKindForBox(ProjectBoxInst *projection) {
669673
SILValue source = projection->getOperand();
674+
if (auto *BBI = dyn_cast<BeginBorrowInst>(source))
675+
source = BBI->getOperand();
670676
if (auto *MUI = dyn_cast<MarkUninitializedInst>(source))
671677
source = MUI->getOperand();
672678

lib/SILOptimizer/Mandatory/CapturePromotion.cpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1040,6 +1040,7 @@ class NonEscapingUserVisitor
10401040
ALWAYS_NON_ESCAPING_INST(Load)
10411041
ALWAYS_NON_ESCAPING_INST(StrongRelease)
10421042
ALWAYS_NON_ESCAPING_INST(DestroyValue)
1043+
ALWAYS_NON_ESCAPING_INST(EndBorrow)
10431044
#undef ALWAYS_NON_ESCAPING_INST
10441045

10451046
bool visitDeallocBoxInst(DeallocBoxInst *dbi) {
@@ -1213,7 +1214,8 @@ static bool findEscapeOrMutationUses(Operand *op,
12131214
// we want to be more conservative around non-top level copies (i.e. a copy
12141215
// derived from a projection like instruction). In fact such a thing may not
12151216
// even make any sense!
1216-
if (isa<CopyValueInst>(user) || isa<MarkUninitializedInst>(user)) {
1217+
if (isa<CopyValueInst>(user) || isa<MarkUninitializedInst>(user) ||
1218+
isa<BeginBorrowInst>(user)) {
12171219
bool foundSomeMutations = false;
12181220
for (auto *use : cast<SingleValueInstruction>(user)->getUses()) {
12191221
foundSomeMutations |= findEscapeOrMutationUses(use, state);

lib/SILOptimizer/Mandatory/DIMemoryUseCollector.cpp

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,11 @@ static void gatherDestroysOfContainer(const DIMemoryObjectInfo &memoryInfo,
5151
// TODO: This should really be tracked separately from other destroys so that
5252
// we distinguish the lifetime of the container from the value itself.
5353
assert(isa<ProjectBoxInst>(uninitMemory));
54-
auto *mui = cast<MarkUninitializedInst>(uninitMemory->getOperand(0));
54+
auto value = uninitMemory->getOperand(0);
55+
if (auto *bbi = dyn_cast<BeginBorrowInst>(value)) {
56+
value = bbi->getOperand();
57+
}
58+
auto *mui = cast<MarkUninitializedInst>(value);
5559
for (auto *user : mui->getUsersOfType<DestroyValueInst>()) {
5660
useInfo.trackDestroy(user);
5761
}
@@ -114,6 +118,12 @@ DIMemoryObjectInfo::DIMemoryObjectInfo(MarkUninitializedInst *MI)
114118
auto &Module = MI->getModule();
115119

116120
SILValue Address = MemoryInst;
121+
if (auto BBI = MemoryInst->getSingleUserOfType<BeginBorrowInst>()) {
122+
if (auto PBI = BBI->getSingleUserOfType<ProjectBoxInst>()) {
123+
IsBox = true;
124+
Address = PBI;
125+
}
126+
}
117127
if (auto PBI = MemoryInst->getSingleUserOfType<ProjectBoxInst>()) {
118128
IsBox = true;
119129
Address = PBI;

lib/SILOptimizer/Mandatory/DIMemoryUseCollector.h

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -102,10 +102,14 @@ class DIMemoryObjectInfo {
102102
/// with the memory info.
103103
SingleValueInstruction *getUninitializedValue() const {
104104
if (IsBox) {
105+
SILValue inst = MemoryInst;
106+
if (auto *bbi = MemoryInst->getSingleUserOfType<BeginBorrowInst>()) {
107+
inst = bbi;
108+
}
105109
// TODO: consider just storing the ProjectBoxInst in this case.
106-
auto *pbi = MemoryInst->getSingleUserOfType<ProjectBoxInst>();
107-
assert(pbi);
108-
return pbi;
110+
SingleValueInstruction *svi = inst->getSingleUserOfType<ProjectBoxInst>();
111+
assert(svi);
112+
return svi;
109113
}
110114
return MemoryInst;
111115
}

lib/SILOptimizer/Mandatory/DiagnoseInvalidEscapingCaptures.cpp

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -157,7 +157,11 @@ const ParamDecl *getParamDeclFromOperand(SILValue value) {
157157

158158
bool isUseOfSelfInInitializer(Operand *oper) {
159159
if (auto *PBI = dyn_cast<ProjectBoxInst>(oper->get())) {
160-
if (auto *MUI = dyn_cast<MarkUninitializedInst>(PBI->getOperand())) {
160+
SILValue value = PBI->getOperand();
161+
if (auto *bbi = dyn_cast<BeginBorrowInst>(value)) {
162+
value = bbi->getOperand();
163+
}
164+
if (auto *MUI = dyn_cast<MarkUninitializedInst>(value)) {
161165
switch (MUI->getMarkUninitializedKind()) {
162166
case MarkUninitializedInst::Kind::Var:
163167
return false;

0 commit comments

Comments
 (0)