Skip to content

Commit 73a9933

Browse files
authored
Merge pull request #6869 from gottesmm/end_borrow_cleanup
2 parents 0fd459f + 17b4d05 commit 73a9933

File tree

11 files changed

+66
-58
lines changed

11 files changed

+66
-58
lines changed

include/swift/SIL/SILBuilder.h

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -514,10 +514,10 @@ class SILBuilder {
514514
return lowering.emitStore(*this, Loc, Src, DestAddr, Qualifier);
515515
}
516516

517-
EndBorrowInst *createEndBorrow(SILLocation Loc, SILValue Src,
518-
SILValue DestAddr) {
519-
return insert(new (F.getModule())
520-
EndBorrowInst(getSILDebugLocation(Loc), Src, DestAddr));
517+
EndBorrowInst *createEndBorrow(SILLocation Loc, SILValue BorrowedValue,
518+
SILValue OriginalValue) {
519+
return insert(new (F.getModule()) EndBorrowInst(
520+
getSILDebugLocation(Loc), BorrowedValue, OriginalValue));
521521
}
522522

523523
AssignInst *createAssign(SILLocation Loc, SILValue Src, SILValue DestAddr) {

include/swift/SIL/SILCloner.h

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -698,10 +698,10 @@ void SILCloner<ImplClass>::visitStoreBorrowInst(StoreBorrowInst *Inst) {
698698
template <typename ImplClass>
699699
void SILCloner<ImplClass>::visitEndBorrowInst(EndBorrowInst *Inst) {
700700
getBuilder().setCurrentDebugScope(getOpScope(Inst->getDebugScope()));
701-
doPostProcess(Inst,
702-
getBuilder().createEndBorrow(getOpLocation(Inst->getLoc()),
703-
getOpValue(Inst->getSrc()),
704-
getOpValue(Inst->getDest())));
701+
doPostProcess(
702+
Inst, getBuilder().createEndBorrow(getOpLocation(Inst->getLoc()),
703+
getOpValue(Inst->getBorrowedValue()),
704+
getOpValue(Inst->getOriginalValue())));
705705
}
706706

707707
template <typename ImplClass>

include/swift/SIL/SILInstruction.h

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1811,20 +1811,21 @@ class EndBorrowInst : public SILInstruction {
18111811

18121812
public:
18131813
enum {
1814-
/// The source of the value being borrowed.
1815-
Src,
1816-
/// The destination of the borrowed value.
1817-
Dest
1814+
/// The borrowed value.
1815+
BorrowedValue,
1816+
/// The original value that was borrowed from.
1817+
OriginalValue
18181818
};
18191819

18201820
private:
18211821
FixedOperandList<2> Operands;
1822-
EndBorrowInst(SILDebugLocation DebugLoc, SILValue Src, SILValue Dest);
1822+
EndBorrowInst(SILDebugLocation DebugLoc, SILValue BorrowedValue,
1823+
SILValue OriginalValue);
18231824

18241825
public:
1825-
SILValue getSrc() const { return Operands[Src].get(); }
1826+
SILValue getBorrowedValue() const { return Operands[BorrowedValue].get(); }
18261827

1827-
SILValue getDest() const { return Operands[Dest].get(); }
1828+
SILValue getOriginalValue() const { return Operands[OriginalValue].get(); }
18281829

18291830
ArrayRef<Operand> getAllOperands() const { return Operands.asArray(); }
18301831
MutableArrayRef<Operand> getAllOperands() { return Operands.asArray(); }

lib/Parse/ParseSIL.cpp

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2420,32 +2420,32 @@ bool SILParser::parseSILInstruction(SILBasicBlock *BB, SILBuilder &B) {
24202420
}
24212421

24222422
case ValueKind::EndBorrowInst: {
2423-
UnresolvedValueName BorrowDestName, BorrowSourceName;
2423+
UnresolvedValueName BorrowedFromName, BorrowedValueName;
24242424
SourceLoc ToLoc;
24252425
Identifier ToToken;
2426-
SILType BorrowDestTy, BorrowSourceTy;
2426+
SILType BorrowedFromTy, BorrowedValueTy;
24272427

2428-
if (parseValueName(BorrowDestName) ||
2428+
if (parseValueName(BorrowedValueName) ||
24292429
parseSILIdentifier(ToToken, ToLoc, diag::expected_tok_in_sil_instr,
24302430
"from") ||
2431-
parseValueName(BorrowSourceName) ||
2431+
parseValueName(BorrowedFromName) ||
24322432
P.parseToken(tok::colon, diag::expected_sil_colon_value_ref) ||
2433-
parseSILType(BorrowDestTy) ||
2433+
parseSILType(BorrowedValueTy) ||
24342434
P.parseToken(tok::comma, diag::expected_tok_in_sil_instr, ",") ||
2435-
parseSILType(BorrowSourceTy) || parseSILDebugLocation(InstLoc, B))
2435+
parseSILType(BorrowedFromTy) || parseSILDebugLocation(InstLoc, B))
24362436
return true;
24372437

24382438
if (ToToken.str() != "from") {
24392439
P.diagnose(ToLoc, diag::expected_tok_in_sil_instr, "from");
24402440
return true;
24412441
}
24422442

2443-
SILValue BorrowDest =
2444-
getLocalValue(BorrowDestName, BorrowDestTy, InstLoc, B);
2445-
SILValue BorrowSource =
2446-
getLocalValue(BorrowSourceName, BorrowSourceTy, InstLoc, B);
2443+
SILValue BorrowedValue =
2444+
getLocalValue(BorrowedValueName, BorrowedValueTy, InstLoc, B);
2445+
SILValue BorrowedFrom =
2446+
getLocalValue(BorrowedFromName, BorrowedFromTy, InstLoc, B);
24472447

2448-
ResultVal = B.createEndBorrow(InstLoc, BorrowDest, BorrowSource);
2448+
ResultVal = B.createEndBorrow(InstLoc, BorrowedValue, BorrowedFrom);
24492449
break;
24502450
}
24512451

lib/SIL/SILOwnershipVerifier.cpp

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -393,9 +393,11 @@ OwnershipCompatibilityUseChecker::visitReturnInst(ReturnInst *RI) {
393393

394394
OwnershipUseCheckerResult
395395
OwnershipCompatibilityUseChecker::visitEndBorrowInst(EndBorrowInst *I) {
396-
// We do not consider the source to be a verified use for now.
397-
if (getOperandIndex() == EndBorrowInst::Src)
396+
// We do not consider the original value to be a verified use. But the value
397+
// does need to be alive.
398+
if (getOperandIndex() == EndBorrowInst::OriginalValue)
398399
return {true, false};
400+
// The borrowed value is a verified use though of the begin_borrow.
399401
return {compatibleWithOwnership(ValueOwnershipKind::Guaranteed), true};
400402
}
401403

lib/SIL/SILPrinter.cpp

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1069,8 +1069,10 @@ class SILPrinter : public SILVisitor<SILPrinter> {
10691069
}
10701070

10711071
void visitEndBorrowInst(EndBorrowInst *EBI) {
1072-
*this << getID(EBI->getDest()) << " from " << getID(EBI->getSrc()) << " : "
1073-
<< EBI->getDest()->getType() << ", " << EBI->getSrc()->getType();
1072+
*this << getID(EBI->getBorrowedValue()) << " from "
1073+
<< getID(EBI->getOriginalValue()) << " : "
1074+
<< EBI->getBorrowedValue()->getType() << ", "
1075+
<< EBI->getOriginalValue()->getType();
10741076
}
10751077

10761078
void visitAssignInst(AssignInst *AI) {

lib/SIL/SILVerifier.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1184,8 +1184,8 @@ class SILVerifier : public SILVerifierBase<SILVerifier> {
11841184
// We allow for end_borrow to express relationships in between addresses and
11851185
// values, but we require that the types are the same ignoring value
11861186
// category.
1187-
require(EBI->getDest()->getType().getObjectType() ==
1188-
EBI->getSrc()->getType().getObjectType(),
1187+
require(EBI->getBorrowedValue()->getType().getObjectType() ==
1188+
EBI->getOriginalValue()->getType().getObjectType(),
11891189
"end_borrow can only relate the same types ignoring value "
11901190
"category");
11911191
}

lib/SILGen/SILGenDecl.cpp

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -203,15 +203,15 @@ void TemporaryInitialization::finishInitialization(SILGenFunction &gen) {
203203

204204
namespace {
205205
class EndBorrowCleanup : public Cleanup {
206-
SILValue borrowee;
206+
SILValue original;
207207
SILValue borrowed;
208208

209209
public:
210-
EndBorrowCleanup(SILValue borrowee, SILValue borrowed)
211-
: borrowee(borrowee), borrowed(borrowed) {}
210+
EndBorrowCleanup(SILValue original, SILValue borrowed)
211+
: original(original), borrowed(borrowed) {}
212212

213213
void emit(SILGenFunction &gen, CleanupLocation l) override {
214-
gen.B.createEndBorrow(l, borrowee, borrowed);
214+
gen.B.createEndBorrow(l, borrowed, original);
215215
}
216216
};
217217
} // end anonymous namespace
@@ -1159,9 +1159,9 @@ CleanupHandle SILGenFunction::enterDestroyCleanup(SILValue valueOrAddr) {
11591159
return Cleanups.getTopCleanup();
11601160
}
11611161

1162-
CleanupHandle SILGenFunction::enterEndBorrowCleanup(SILValue borrowee,
1162+
CleanupHandle SILGenFunction::enterEndBorrowCleanup(SILValue original,
11631163
SILValue borrowed) {
1164-
Cleanups.pushCleanup<EndBorrowCleanup>(borrowee, borrowed);
1164+
Cleanups.pushCleanup<EndBorrowCleanup>(original, borrowed);
11651165
return Cleanups.getTopCleanup();
11661166
}
11671167

lib/SILGen/SILGenExpr.cpp

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -134,22 +134,24 @@ SILGenFunction::emitManagedBeginBorrow(SILLocation loc, SILValue v,
134134
}
135135

136136
ManagedValue
137-
SILGenFunction::emitManagedBorrowedRValueWithCleanup(SILValue borrowee,
137+
SILGenFunction::emitManagedBorrowedRValueWithCleanup(SILValue original,
138138
SILValue borrowed) {
139-
assert(borrowee->getType().getObjectType() ==
139+
assert(original->getType().getObjectType() ==
140140
borrowed->getType().getObjectType());
141-
auto &lowering = getTypeLowering(borrowee->getType());
142-
return emitManagedBorrowedRValueWithCleanup(borrowee, borrowed, lowering);
141+
auto &lowering = getTypeLowering(original->getType());
142+
return emitManagedBorrowedRValueWithCleanup(original, borrowed, lowering);
143143
}
144144

145145
ManagedValue SILGenFunction::emitManagedBorrowedRValueWithCleanup(
146-
SILValue borrowee, SILValue borrowed, const TypeLowering &lowering) {
146+
SILValue original, SILValue borrowed, const TypeLowering &lowering) {
147147
assert(lowering.getLoweredType().getObjectType() ==
148-
borrowee->getType().getObjectType());
148+
original->getType().getObjectType());
149149
if (lowering.isTrivial())
150150
return ManagedValue::forUnmanaged(borrowed);
151151

152-
return ManagedValue(borrowed, enterEndBorrowCleanup(borrowee, borrowed));
152+
if (borrowed->getType().isObject())
153+
enterEndBorrowCleanup(original, borrowed);
154+
return ManagedValue(borrowed, CleanupHandle::invalid());
153155
}
154156

155157
ManagedValue SILGenFunction::emitManagedRValueWithCleanup(SILValue v) {

lib/SILGen/SILGenFunction.h

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1173,11 +1173,10 @@ class LLVM_LIBRARY_VISIBILITY SILGenFunction
11731173
const TypeLowering &lowering);
11741174
ManagedValue emitManagedBeginBorrow(SILLocation loc, SILValue v);
11751175

1176-
ManagedValue emitManagedBorrowedRValueWithCleanup(SILValue borrowee,
1177-
SILValue borrower);
1178-
ManagedValue
1179-
emitManagedBorrowedRValueWithCleanup(SILValue borrowee, SILValue borrower,
1180-
const TypeLowering &lowering);
1176+
ManagedValue emitManagedBorrowedRValueWithCleanup(SILValue original,
1177+
SILValue borrowedValue);
1178+
ManagedValue emitManagedBorrowedRValueWithCleanup(
1179+
SILValue original, SILValue borrowedValue, const TypeLowering &lowering);
11811180

11821181
ManagedValue emitManagedRValueWithCleanup(SILValue v);
11831182
ManagedValue emitManagedRValueWithCleanup(SILValue v,
@@ -1624,9 +1623,9 @@ class LLVM_LIBRARY_VISIBILITY SILGenFunction
16241623
ExistentialRepresentation repr);
16251624

16261625
/// Enter a cleanup to emit an EndBorrow stating that \p borrowed (the
1627-
/// borrowed entity) is no longer borrowed from \p borrowee, the original
1626+
/// borrowed entity) is no longer borrowed from \p original, the original
16281627
/// value.
1629-
CleanupHandle enterEndBorrowCleanup(SILValue borrowee, SILValue borrowed);
1628+
CleanupHandle enterEndBorrowCleanup(SILValue original, SILValue borrowed);
16301629

16311630
/// Evaluate an Expr as an lvalue.
16321631
LValue emitLValue(Expr *E, AccessKind accessKind);

lib/Serialization/SerializeSIL.cpp

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1301,15 +1301,17 @@ void SILSerializer::writeSILInstruction(const SILInstruction &SI) {
13011301
unsigned abbrCode = SILAbbrCodes[SILTwoOperandsLayout::Code];
13021302
unsigned Attr = 0;
13031303
auto *EBI = cast<EndBorrowInst>(&SI);
1304-
SILValue Src = EBI->getSrc();
1305-
SILValue Dest = EBI->getDest();
1304+
SILValue BorrowedValue = EBI->getBorrowedValue();
1305+
SILValue OriginalValue = EBI->getOriginalValue();
13061306

13071307
SILTwoOperandsLayout::emitRecord(
13081308
Out, ScratchRecord, abbrCode, (unsigned)SI.getKind(), Attr,
1309-
S.addTypeRef(Src->getType().getSwiftRValueType()),
1310-
(unsigned)Src->getType().getCategory(), addValueRef(Src),
1311-
S.addTypeRef(Dest->getType().getSwiftRValueType()),
1312-
(unsigned)Dest->getType().getCategory(), addValueRef(Dest));
1309+
S.addTypeRef(BorrowedValue->getType().getSwiftRValueType()),
1310+
(unsigned)BorrowedValue->getType().getCategory(),
1311+
addValueRef(BorrowedValue),
1312+
S.addTypeRef(OriginalValue->getType().getSwiftRValueType()),
1313+
(unsigned)OriginalValue->getType().getCategory(),
1314+
addValueRef(OriginalValue));
13131315
break;
13141316
}
13151317

0 commit comments

Comments
 (0)