Skip to content

Commit 8887217

Browse files
author
Joe Shajrawi
authored
Merge pull request #8256 from shajrawi/capture_opaque
Support for capture of a (im)mutable opaque value type
2 parents 2dc874b + 38e20af commit 8887217

File tree

2 files changed

+31
-6
lines changed

2 files changed

+31
-6
lines changed

lib/SILGen/SILGenConvert.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -665,7 +665,7 @@ ManagedValue SILGenFunction::emitExistentialErasure(
665665
}
666666
}
667667

668-
if (!C.getEmitInto() && !silConv.useLoweredAddresses()) {
668+
if (!silConv.useLoweredAddresses()) {
669669
// We should never create new buffers just for init_existential under
670670
// opaque values mode: This is a case of an opaque value that we can
671671
// "treat" as a by-value one

test/SILGen/opaque_values_silgen.swift

Lines changed: 30 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ enum AddressOnlyEnum {
8585
// CHECK: return [[RETVAL]] : $P2
8686
// CHECK-LABEL: } // end sil function '_T020opaque_values_silgen1P_pSgAA13TrivialStructVIxid_AESgAA2P2_pIxyr_TR'
8787

88-
// Test that we still use addresses when dealing with array initialization
88+
// Test array initialization - we are still (somewhat) using addresses
8989
// ---
9090
// CHECK-LABEL: sil @_T020opaque_values_silgen21s020_______callVarArgyyF : $@convention(thin) () -> () {
9191
// CHECK: %[[APY:.*]] = apply %{{.*}}<Any>(%{{.*}}) : $@convention(thin) <τ_0_0> (Builtin.Word) -> (@owned Array<τ_0_0>, Builtin.RawPointer)
@@ -94,7 +94,8 @@ enum AddressOnlyEnum {
9494
// CHECK: end_borrow %[[BRW]] from %[[APY]] : $(Array<Any>, Builtin.RawPointer), $(Array<Any>, Builtin.RawPointer)
9595
// CHECK: destroy_value %[[APY]]
9696
// CHECK: %[[PTR:.*]] = pointer_to_address %[[TPL]] : $Builtin.RawPointer to [strict] $*Any
97-
// CHECK: init_existential_addr %[[PTR]] : $*Any, $Int
97+
// CHECK: [[IOPAQUE:%.*]] = init_existential_opaque %{{.*}} : $Int, $Int, $Any
98+
// CHECK: store [[IOPAQUE]] to [init] %[[PTR]] : $*Any
9899
// CHECK: return %{{.*}} : $()
99100
// CHECK-LABEL: } // end sil function '_T020opaque_values_silgen21s020_______callVarArgyyF'
100101
public func s020_______callVarArg() {
@@ -219,15 +220,15 @@ func s090___________caller<T>(_ t: T) -> T {
219220

220221
// Test a simple opaque parameter and return value.
221222
// ---
222-
// CHECK-LABEL: sil hidden @_T020opaque_values_silgen21s100_________identityxx1t_tlF : $@convention(thin) <T> (@in T) -> @out T {
223+
// CHECK-LABEL: sil hidden @_T020opaque_values_silgen21s100_________identityxxlF : $@convention(thin) <T> (@in T) -> @out T {
223224
// CHECK: bb0([[ARG:%.*]] : $T):
224225
// CHECK: [[BORROWED_ARG:%.*]] = begin_borrow [[ARG]]
225226
// CHECK: [[COPY_ARG:%.*]] = copy_value [[BORROWED_ARG]] : $T
226227
// CHECK: end_borrow [[BORROWED_ARG]] from [[ARG]]
227228
// CHECK: destroy_value [[ARG]] : $T
228229
// CHECK: return [[COPY_ARG]] : $T
229-
// CHECK-LABEL: } // end sil function '_T020opaque_values_silgen21s100_________identityxx1t_tlF'
230-
func s100_________identity<T>(t: T) -> T {
230+
// CHECK-LABEL: } // end sil function '_T020opaque_values_silgen21s100_________identityxxlF'
231+
func s100_________identity<T>(_ t: T) -> T {
231232
return t
232233
}
233234

@@ -623,6 +624,30 @@ func s330___addrLetClosure<T>(_ x:T) -> T {
623624
return { { x }() }()
624625
}
625626

627+
// Tests support for capture of a mutable opaque value type
628+
// ---
629+
// CHECK-LABEL: sil hidden @_T020opaque_values_silgen21s340_______captureBoxyyF : $@convention(thin) () -> () {
630+
// CHECK: bb0:
631+
// CHECK: [[ALLOC_OF_BOX:%.*]] = alloc_box ${ var EmptyP }, var, name "mutableAddressOnly"
632+
// CHECK: [[PROJ_BOX:%.*]] = project_box [[ALLOC_OF_BOX]]
633+
// CHECK: [[APPLY_FOR_BOX:%.*]] = apply %{{.*}}(%{{.*}}) : $@convention(method) (@thin AddressOnlyStruct.Type) -> AddressOnlyStruct
634+
// CHECK: [[INIT_OPAQUE:%.*]] = init_existential_opaque [[APPLY_FOR_BOX]] : $AddressOnlyStruct, $AddressOnlyStruct, $EmptyP
635+
// CHECK: store [[INIT_OPAQUE]] to [init] [[PROJ_BOX]] : $*EmptyP
636+
// CHECK: [[COPY_BOX:%.*]] = copy_value [[ALLOC_OF_BOX]] : ${ var EmptyP }
637+
// CHECK: mark_function_escape [[PROJ_BOX]] : $*EmptyP
638+
// CHECK: apply %{{.*}}([[COPY_BOX]]) : $@convention(thin) (@owned { var EmptyP }) -> ()
639+
// CHECK: return %{{.*}} : $()
640+
// CHECK-LABEL: } // end sil function '_T020opaque_values_silgen21s340_______captureBoxyyF'
641+
func s340_______captureBox() {
642+
var mutableAddressOnly: EmptyP = AddressOnlyStruct()
643+
644+
func captureEverything() {
645+
s100_________identity((mutableAddressOnly))
646+
}
647+
648+
captureEverything()
649+
}
650+
626651
// Tests conditional value casts and correspondingly generated reabstraction thunk, with <T> types
627652
// ---
628653
// CHECK-LABEL: sil hidden @_T020opaque_values_silgen21s999_____condTFromAnyyyp_xtlF : $@convention(thin) <T> (@in Any, @in T) -> () {

0 commit comments

Comments
 (0)