Skip to content

Commit 98ad3ea

Browse files
committed
[ownership-value] When passing in a class to materializeForSet, use a store_borrow, rather than a store [init].
The reason why this needs to be done is that we represent class pointers as guaranteed, but we need to pass them as inout, violating the guaranteed assumption. The truth is, we should be passing them in guaranteed but due to the large required AST changes needed, we do not today. Since we model this as an inout today, SILGen rightfully tries to use a store [init] which is consuming. This commit works around that issue by using a store_guaranteed. rdar://29791263
1 parent 532e20e commit 98ad3ea

File tree

2 files changed

+9
-5
lines changed

2 files changed

+9
-5
lines changed

lib/SILGen/SILGenLValue.cpp

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -913,8 +913,12 @@ namespace {
913913
baseFormalType);
914914

915915
baseAddress = gen.emitTemporaryAllocation(loc, base.getType());
916-
gen.B.emitStoreValueOperation(loc, base.getValue(), baseAddress,
917-
StoreOwnershipQualifier::Init);
916+
if (base.getOwnershipKind() == ValueOwnershipKind::Guaranteed) {
917+
gen.B.createStoreBorrow(loc, base.getValue(), baseAddress);
918+
} else {
919+
gen.B.emitStoreValueOperation(loc, base.getValue(), baseAddress,
920+
StoreOwnershipQualifier::Init);
921+
}
918922
}
919923
baseMetatype = gen.B.createMetatype(loc, metatypeType);
920924

test/SILGen/accessors.swift

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ func test0(_ ref: A) {
6868
// CHECK-NEXT: [[CALLBACK:%.*]] = pointer_to_thin_function [[CALLBACK_ADDR]] : $Builtin.RawPointer to $@convention(method) (Builtin.RawPointer, @inout Builtin.UnsafeValueBuffer, @inout A, @thick A.Type) -> ()
6969
// CHECK-NEXT: [[TEMP2:%.*]] = alloc_stack $A
7070
// SEMANTIC SIL TODO: This is an issue caused by the callback for materializeForSet in the class case taking the value as @inout when it should really take it as @guaranteed.
71-
// CHECK-NEXT: store [[BORROWED_ARG_LHS]] to [init] [[TEMP2]] : $*A
71+
// CHECK-NEXT: store_borrow [[BORROWED_ARG_LHS]] to [[TEMP2]] : $*A
7272
// CHECK-NEXT: [[T0:%.*]] = metatype $@thick A.Type
7373
// CHECK-NEXT: [[T1:%.*]] = address_to_pointer [[ADDR]] : $*OrdinarySub to $Builtin.RawPointer
7474
// CHECK-NEXT: apply [[CALLBACK]]([[T1]], [[STORAGE]], [[TEMP2]], [[T0]])
@@ -130,7 +130,7 @@ func test1(_ ref: B) {
130130
// CHECK: [[WRITEBACK]]([[CALLBACK_ADDR:%.*]] : $Builtin.RawPointer):
131131
// CHECK-NEXT: [[CALLBACK:%.*]] = pointer_to_thin_function [[CALLBACK_ADDR]] : $Builtin.RawPointer to $@convention(method) (Builtin.RawPointer, @inout Builtin.UnsafeValueBuffer, @inout B, @thick B.Type) -> ()
132132
// CHECK-NEXT: [[TEMP2:%.*]] = alloc_stack $B
133-
// CHECK-NEXT: store [[BORROWED_ARG_RHS]] to [init] [[TEMP2]] : $*B
133+
// CHECK-NEXT: store_borrow [[BORROWED_ARG_RHS]] to [[TEMP2]] : $*B
134134
// CHECK-NEXT: [[T0:%.*]] = metatype $@thick B.Type
135135
// CHECK-NEXT: [[T1:%.*]] = address_to_pointer [[ADDR]] : $*MutatingSub to $Builtin.RawPointer
136136
// CHECK-NEXT: apply [[CALLBACK]]([[T1]], [[STORAGE]], [[TEMP2]], [[T0]])
@@ -156,7 +156,7 @@ func test1(_ ref: B) {
156156
// CHECK: [[WRITEBACK]]([[CALLBACK_ADDR:%.*]] : $Builtin.RawPointer):
157157
// CHECK-NEXT: [[CALLBACK:%.*]] = pointer_to_thin_function [[CALLBACK_ADDR]] : $Builtin.RawPointer to $@convention(method) (Builtin.RawPointer, @inout Builtin.UnsafeValueBuffer, @inout B, @thick B.Type) -> ()
158158
// CHECK-NEXT: [[TEMP2:%.*]] = alloc_stack $B
159-
// CHECK-NEXT: store [[BORROWED_ARG_LHS]] to [init] [[TEMP2]] : $*B
159+
// CHECK-NEXT: store_borrow [[BORROWED_ARG_LHS]] to [[TEMP2]] : $*B
160160
// CHECK-NEXT: [[T0:%.*]] = metatype $@thick B.Type
161161
// CHECK-NEXT: [[T1:%.*]] = address_to_pointer [[ADDR]] : $*MutatingSub to $Builtin.RawPointer
162162
// CHECK-NEXT: apply [[CALLBACK]]([[T1]], [[STORAGE2]], [[TEMP2]], [[T0]])

0 commit comments

Comments
 (0)