Skip to content

[silgen] Rename DelegateInitSelfWritebackCleanup -> OwnedValueWritebackCleanup. #31828

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions lib/SILGen/ManagedValue.h
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,13 @@ class ManagedValue {
return ManagedValue::forOwnedObjectRValue(value, cleanup);
}

static ManagedValue
forExclusivelyBorrowedOwnedObjectRValue(SILValue value,
CleanupHandle cleanup) {
assert(value->getType().isObject());
return ManagedValue::forOwnedObjectRValue(value, cleanup);
}

/// Create a managed value for a +0 borrowed non-trivial rvalue object.
static ManagedValue
forBorrowedObjectRValue(SILValue value) {
Expand Down
2 changes: 1 addition & 1 deletion lib/SILGen/SILGenApply.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1243,7 +1243,7 @@ class SILGenApply : public Lowering::ExprVisitor<SILGenApply> {
if (superMV.getValue() != SGF.InitDelegationSelf.getValue()) {
SILValue underlyingSelf = SGF.InitDelegationSelf.getValue();
SGF.InitDelegationSelf = ManagedValue::forUnmanaged(underlyingSelf);
CleanupHandle newWriteback = SGF.enterDelegateInitSelfWritebackCleanup(
CleanupHandle newWriteback = SGF.enterOwnedValueWritebackCleanup(
SGF.InitDelegationLoc.getValue(), SGF.InitDelegationSelfBox,
superMV.forward(SGF));
SGF.SuperInitDelegationSelf =
Expand Down
30 changes: 14 additions & 16 deletions lib/SILGen/SILGenExpr.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -717,20 +717,20 @@ SILValue SILGenFunction::emitEmptyTuple(SILLocation loc) {

namespace {

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

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

SILValue lvalueAddress;
SILValue value;

DelegateInitSelfWritebackCleanup(SILLocation loc, SILValue lvalueAddress,
SILValue value)
OwnedValueWritebackCleanup(SILLocation loc, SILValue lvalueAddress,
SILValue value)
: loc(loc), lvalueAddress(lvalueAddress), value(value) {}

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

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

void dump(SILGenFunction &) const override {
#ifndef NDEBUG
llvm::errs() << "SimpleWritebackCleanup "
llvm::errs() << "OwnedValueWritebackCleanup "
<< "State:" << getState() << "\n"
<< "lvalueAddress:" << lvalueAddress << "value:" << value
<< "\n";
Expand All @@ -766,10 +765,9 @@ struct DelegateInitSelfWritebackCleanup : Cleanup {

} // end anonymous namespace

CleanupHandle SILGenFunction::enterDelegateInitSelfWritebackCleanup(
CleanupHandle SILGenFunction::enterOwnedValueWritebackCleanup(
SILLocation loc, SILValue address, SILValue newValue) {
Cleanups.pushCleanup<DelegateInitSelfWritebackCleanup>(loc, address,
newValue);
Cleanups.pushCleanup<OwnedValueWritebackCleanup>(loc, address, newValue);
return Cleanups.getTopCleanup();
}

Expand Down Expand Up @@ -815,8 +813,8 @@ RValue SILGenFunction::emitRValueForSelfInDelegationInit(SILLocation loc,
// Forward our initial value for init delegation self and create a new
// cleanup that performs a writeback at the end of lexical scope if our
// value is not consumed.
InitDelegationSelf = ManagedValue(
self, enterDelegateInitSelfWritebackCleanup(*InitDelegationLoc, addr, self));
InitDelegationSelf = ManagedValue::forExclusivelyBorrowedOwnedObjectRValue(
self, enterOwnedValueWritebackCleanup(*InitDelegationLoc, addr, self));
InitDelegationSelfBox = addr;
return RValue(*this, loc, refType, InitDelegationSelf);
}
Expand Down
6 changes: 3 additions & 3 deletions lib/SILGen/SILGenFunction.h
Original file line number Diff line number Diff line change
Expand Up @@ -1188,9 +1188,9 @@ class LLVM_LIBRARY_VISIBILITY SILGenFunction
CleanupHandle enterDeallocateUninitializedArrayCleanup(SILValue array);
void emitUninitializedArrayDeallocation(SILLocation loc, SILValue array);

CleanupHandle enterDelegateInitSelfWritebackCleanup(SILLocation loc,
SILValue address,
SILValue newValue);
CleanupHandle enterOwnedValueWritebackCleanup(SILLocation loc,
SILValue address,
SILValue newValue);

SILValue emitConversionToSemanticRValue(SILLocation loc, SILValue value,
const TypeLowering &valueTL);
Expand Down