Skip to content

Commit c217262

Browse files
committed
[silgen] Rename DelegateInitSelfWritebackCleanup -> OwnedValueWritebackCleanup.
This cleanup is meant to be used with a value that is temporarily taken from a memory location for a lexical scope. At end of scope, the value is returned back to the original memory location. It is used to implement in SILGen "move only loadable values". Note, a "move only loadable value" is not a "move only type". It is just an abstraction for working with a single copy of a loadable value as if that single copy was a move only value. This is currently only being used in initializer emission since we treat self in such a context as a "move only value" before we delegate to a super/convenience/peer initializer since we have to allow for such initializers to change the underlying class we have stored which in certain use cases require self to be guaranteed as /never/ being retained. The move only value representation makes this easy to do/enforce. My hope is that by changing the name of this cleanup it is more obvious what it is meant to do and can become (hopefully) generally useful.
1 parent 82728d3 commit c217262

File tree

4 files changed

+25
-20
lines changed

4 files changed

+25
-20
lines changed

lib/SILGen/ManagedValue.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,13 @@ class ManagedValue {
121121
return ManagedValue::forOwnedObjectRValue(value, cleanup);
122122
}
123123

124+
static ManagedValue
125+
forExclusivelyBorrowedOwnedObjectRValue(SILValue value,
126+
CleanupHandle cleanup) {
127+
assert(value->getType().isObject());
128+
return ManagedValue::forOwnedObjectRValue(value, cleanup);
129+
}
130+
124131
/// Create a managed value for a +0 borrowed non-trivial rvalue object.
125132
static ManagedValue
126133
forBorrowedObjectRValue(SILValue value) {

lib/SILGen/SILGenApply.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1243,7 +1243,7 @@ class SILGenApply : public Lowering::ExprVisitor<SILGenApply> {
12431243
if (superMV.getValue() != SGF.InitDelegationSelf.getValue()) {
12441244
SILValue underlyingSelf = SGF.InitDelegationSelf.getValue();
12451245
SGF.InitDelegationSelf = ManagedValue::forUnmanaged(underlyingSelf);
1246-
CleanupHandle newWriteback = SGF.enterDelegateInitSelfWritebackCleanup(
1246+
CleanupHandle newWriteback = SGF.enterOwnedValueWritebackCleanup(
12471247
SGF.InitDelegationLoc.getValue(), SGF.InitDelegationSelfBox,
12481248
superMV.forward(SGF));
12491249
SGF.SuperInitDelegationSelf =

lib/SILGen/SILGenExpr.cpp

Lines changed: 14 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -717,20 +717,20 @@ SILValue SILGenFunction::emitEmptyTuple(SILLocation loc) {
717717

718718
namespace {
719719

720-
/// This is a simple cleanup class that is only meant to help with delegating
721-
/// initializers. Specifically, if the delegating initializer fails to consume
722-
/// the loaded self, we want to write back self into the slot to ensure that
723-
/// ownership is preserved.
724-
struct DelegateInitSelfWritebackCleanup : Cleanup {
720+
/// This is a simple cleanup class that at the end of a lexical scope consumes
721+
/// an owned value by writing it back to memory. The user can forward this
722+
/// cleanup to take ownership of the value and thus prevent it form being
723+
/// written back.
724+
struct OwnedValueWritebackCleanup final : Cleanup {
725725

726726
/// We store our own loc so that we can ensure that DI ignores our writeback.
727727
SILLocation loc;
728728

729729
SILValue lvalueAddress;
730730
SILValue value;
731731

732-
DelegateInitSelfWritebackCleanup(SILLocation loc, SILValue lvalueAddress,
733-
SILValue value)
732+
OwnedValueWritebackCleanup(SILLocation loc, SILValue lvalueAddress,
733+
SILValue value)
734734
: loc(loc), lvalueAddress(lvalueAddress), value(value) {}
735735

736736
void emit(SILGenFunction &SGF, CleanupLocation l, ForUnwind_t forUnwind) override {
@@ -749,14 +749,13 @@ struct DelegateInitSelfWritebackCleanup : Cleanup {
749749
lvalueObjTy);
750750
}
751751

752-
auto &lowering = SGF.B.getTypeLowering(lvalueAddress->getType());
753-
lowering.emitStore(SGF.B, loc, valueToStore, lvalueAddress,
754-
StoreOwnershipQualifier::Init);
752+
SGF.B.emitStoreValueOperation(loc, valueToStore, lvalueAddress,
753+
StoreOwnershipQualifier::Init);
755754
}
756755

757756
void dump(SILGenFunction &) const override {
758757
#ifndef NDEBUG
759-
llvm::errs() << "SimpleWritebackCleanup "
758+
llvm::errs() << "OwnedValueWritebackCleanup "
760759
<< "State:" << getState() << "\n"
761760
<< "lvalueAddress:" << lvalueAddress << "value:" << value
762761
<< "\n";
@@ -766,10 +765,9 @@ struct DelegateInitSelfWritebackCleanup : Cleanup {
766765

767766
} // end anonymous namespace
768767

769-
CleanupHandle SILGenFunction::enterDelegateInitSelfWritebackCleanup(
768+
CleanupHandle SILGenFunction::enterOwnedValueWritebackCleanup(
770769
SILLocation loc, SILValue address, SILValue newValue) {
771-
Cleanups.pushCleanup<DelegateInitSelfWritebackCleanup>(loc, address,
772-
newValue);
770+
Cleanups.pushCleanup<OwnedValueWritebackCleanup>(loc, address, newValue);
773771
return Cleanups.getTopCleanup();
774772
}
775773

@@ -815,8 +813,8 @@ RValue SILGenFunction::emitRValueForSelfInDelegationInit(SILLocation loc,
815813
// Forward our initial value for init delegation self and create a new
816814
// cleanup that performs a writeback at the end of lexical scope if our
817815
// value is not consumed.
818-
InitDelegationSelf = ManagedValue(
819-
self, enterDelegateInitSelfWritebackCleanup(*InitDelegationLoc, addr, self));
816+
InitDelegationSelf = ManagedValue::forExclusivelyBorrowedOwnedObjectRValue(
817+
self, enterOwnedValueWritebackCleanup(*InitDelegationLoc, addr, self));
820818
InitDelegationSelfBox = addr;
821819
return RValue(*this, loc, refType, InitDelegationSelf);
822820
}

lib/SILGen/SILGenFunction.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1188,9 +1188,9 @@ class LLVM_LIBRARY_VISIBILITY SILGenFunction
11881188
CleanupHandle enterDeallocateUninitializedArrayCleanup(SILValue array);
11891189
void emitUninitializedArrayDeallocation(SILLocation loc, SILValue array);
11901190

1191-
CleanupHandle enterDelegateInitSelfWritebackCleanup(SILLocation loc,
1192-
SILValue address,
1193-
SILValue newValue);
1191+
CleanupHandle enterOwnedValueWritebackCleanup(SILLocation loc,
1192+
SILValue address,
1193+
SILValue newValue);
11941194

11951195
SILValue emitConversionToSemanticRValue(SILLocation loc, SILValue value,
11961196
const TypeLowering &valueTL);

0 commit comments

Comments
 (0)