Skip to content

[ownership] init_existential_ref is not forwarding: it should only tr… #21093

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

Closed
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
5 changes: 2 additions & 3 deletions include/swift/SIL/SILInstruction.h
Original file line number Diff line number Diff line change
Expand Up @@ -5947,7 +5947,7 @@ class InitExistentialValueInst final
class InitExistentialRefInst final
: public UnaryInstructionWithTypeDependentOperandsBase<
SILInstructionKind::InitExistentialRefInst, InitExistentialRefInst,
OwnershipForwardingSingleValueInst> {
SingleValueInstruction> {
friend SILBuilder;

CanType ConcreteType;
Expand All @@ -5958,8 +5958,7 @@ class InitExistentialRefInst final
ArrayRef<SILValue> TypeDependentOperands,
ArrayRef<ProtocolConformanceRef> Conformances)
: UnaryInstructionWithTypeDependentOperandsBase(
DebugLoc, Instance, TypeDependentOperands, ExistentialType,
Instance.getOwnershipKind()),
DebugLoc, Instance, TypeDependentOperands, ExistentialType),
ConcreteType(FormalConcreteType), Conformances(Conformances) {}

static InitExistentialRefInst *
Expand Down
2 changes: 1 addition & 1 deletion lib/SIL/OperandOwnership.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -162,8 +162,8 @@ CONSTANT_OWNERSHIP_INST(Owned, MustBeInvalidated, DestroyValue)
CONSTANT_OWNERSHIP_INST(Owned, MustBeInvalidated, ReleaseValue)
CONSTANT_OWNERSHIP_INST(Owned, MustBeInvalidated, ReleaseValueAddr)
CONSTANT_OWNERSHIP_INST(Owned, MustBeInvalidated, StrongRelease)
CONSTANT_OWNERSHIP_INST(Owned, MustBeInvalidated, InitExistentialRef)
CONSTANT_OWNERSHIP_INST(Owned, MustBeInvalidated, EndLifetime)
CONSTANT_OWNERSHIP_INST(Owned, MustBeInvalidated, InitExistentialRef)
CONSTANT_OWNERSHIP_INST(Any, MustBeLive, AbortApply)
CONSTANT_OWNERSHIP_INST(Any, MustBeLive, AddressToPointer)
CONSTANT_OWNERSHIP_INST(Any, MustBeLive, BeginAccess)
Expand Down
8 changes: 7 additions & 1 deletion lib/SIL/ValueOwnership.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,13 @@ CONSTANT_OWNERSHIP_INST(Owned, PartialApply)
CONSTANT_OWNERSHIP_INST(Owned, InitExistentialValue)
CONSTANT_OWNERSHIP_INST(Owned, GlobalValue) // TODO: is this correct?

// NOTE: Even though init_existential_ref from a reference counting perspective
// is not considered to be "owned" since it doesn't affect reference counts,
// conceptually we want to treat it as an owned value that produces owned
// things, rather than a forwarding thing since initialization is generally a
// consuming operation.
CONSTANT_OWNERSHIP_INST(Owned, InitExistentialRef)

// One would think that these /should/ be unowned. In truth they are owned since
// objc metatypes do not go through the retain/release fast path. In their
// implementations of retain/release nothing happens, so this is safe.
Expand Down Expand Up @@ -234,7 +241,6 @@ ValueOwnershipKindClassifier::visitForwardingInst(SILInstruction *i,
}
FORWARDING_OWNERSHIP_INST(BridgeObjectToRef)
FORWARDING_OWNERSHIP_INST(ConvertFunction)
FORWARDING_OWNERSHIP_INST(InitExistentialRef)
FORWARDING_OWNERSHIP_INST(OpenExistentialRef)
FORWARDING_OWNERSHIP_INST(RefToBridgeObject)
FORWARDING_OWNERSHIP_INST(SelectValue)
Expand Down
34 changes: 33 additions & 1 deletion test/SIL/ownership-verifier/over_consume.sil
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,12 @@ class SuperKlass {
class Klass : SuperKlass {
}

typealias AnyObject = Builtin.AnyObject

class ClassProtConformingRef {}
protocol ClassProt : AnyObject {}
extension ClassProtConformingRef : ClassProt {}

///////////
// Tests //
///////////
Expand Down Expand Up @@ -452,4 +458,30 @@ bb0(%0 : @owned $Klass):
dealloc_stack %1 : $*SuperKlass
%9999 = tuple()
return %9999 : $()
}
}

// CHECK-LABEL: Function: 'init_existential_ref_not_forwarding'
// CHECK: Have operand with incompatible ownership?!
// CHECK: Value: %0 = argument of bb0 : $ClassProtConformingRef
// CHECK: User: %2 = init_existential_ref %0 : $ClassProtConformingRef : $ClassProtConformingRef, $ClassProt
// CHECK: Operand Number: 0
// CHECK: Conv: guaranteed
// CHECK: OwnershipMap:
// CHECK: -- OperandOwnershipKindMap --
// CHECK: unowned: No.
// CHECK: owned: Yes. Liveness: MustBeInvalidated
// CHECK: guaranteed: No.
// CHECK: any: Yes. Liveness: MustBeLive
// CHECK-NOT: init_existential_ref %1
// CHECK: Function: 'init_existential_ref_not_forwarding'
// CHECK: Error! Found a leaked owned value that was never consumed.
// CHECK: Value: %2 = init_existential_ref %0 : $ClassProtConformingRef : $ClassProtConformingRef, $ClassProt
// CHECK-NOT: init_existential_ref %1
sil @init_existential_ref_not_forwarding : $@convention(thin) (@guaranteed ClassProtConformingRef, @owned ClassProtConformingRef) -> @owned (ClassProt, ClassProt) {
bb0(%0 : @guaranteed $ClassProtConformingRef, %1 : @owned $ClassProtConformingRef):
%2 = init_existential_ref %0 : $ClassProtConformingRef : $ClassProtConformingRef, $ClassProt
%3 = copy_value %2 : $ClassProt
%4 = init_existential_ref %1 : $ClassProtConformingRef : $ClassProtConformingRef, $ClassProt
%5 = tuple(%3 : $ClassProt, %4 : $ClassProt)
return %5 : $(ClassProt, ClassProt)
}