Files
swift-mirror/test/IRGen/property_descriptor.sil
Yuta Saito c5314bd3af Centralize KeyPath accessor calling convention logic to IRGen
KeyPath's getter/setter/hash/equals functions have their own calling
convention, which receives generic arguments and embedded indices from a
given KeyPath argument buffer.
The convention was previously implemented by:
1. Accepting an argument buffer as an UnsafeRawPointer and casting it to
   indices tuple pointer in SIL.
2. Bind generic arguments info from the given argument buffer while emitting
   prologue in IRGen by creating a new forwarding thunk.

This 2-phase lowering approach was not ideal, as it blocked KeyPath
projection optimization [^1], and also required having a target arch
specific signature lowering logic in SIL-level [^2].

This patch centralizes the KeyPath accessor calling convention logic to
IRGen, by introducing `@convention(keypath_accessor_XXX)` convention in
SIL and lowering it in IRGen. This change unblocks the KeyPath projection
optimization while capturing subscript indices, and also makes it easier
to support WebAssembly target.

[^1]: https://github.com/apple/swift/pull/28799
[^2]: https://forums.swift.org/t/wasm-support/16087/21
2023-09-20 11:25:39 -07:00

153 lines
8.4 KiB
Plaintext

// RUN: %empty-directory(%t)
// RUN: %{python} %utils/chex.py < %s > %t/property_descriptor.sil
// RUN: %target-swift-frontend -emit-ir %t/property_descriptor.sil | %FileCheck --check-prefix=CHECK-%target-ptrsize --check-prefix=CHECK %t/property_descriptor.sil
sil_stage canonical
import Swift
public struct ExternalGeneric<T: Comparable> {
@_hasStorage public var ro: T { get }
@_hasStorage public var rw: T { get set }
public var computedRO: T { get }
public var computedRW: T { get set }
public subscript<U: Hashable>(_: U) -> T { get set }
public var computedWithTrivialDescriptor: Int { get }
init()
}
public struct External {
@_hasStorage public var ro: Int { get }
@_hasStorage public var rw: Int { get set }
public var computedRO: Int { get }
public var computedRW: Int { get set }
public subscript(_: String) -> Int { get set }
public var computedWithTrivialDescriptor: Int { get }
init()
}
public struct ExternalReabstractions<T> {
@_hasStorage public var ro: T { get }
@_hasStorage public var reabstracted: () -> () { get set }
}
// -- struct property, offset resolved from field offset vector in metadata
// CHECK-64: @"$s19property_descriptor15ExternalGenericV2roxvpMV" =
// CHECK-32: @"$s19property_descriptor15ExternalGenericV2roxvpMV" =
// CHECK-SAME: <{ <i32 0x01ff_fffe>,
sil_property #ExternalGeneric.ro <T: Comparable> (
stored_property #ExternalGeneric.ro : $T)
// CHECK-64: @"$s19property_descriptor15ExternalGenericV2rwxvpMV" =
// CHECK-32: @"$s19property_descriptor15ExternalGenericV2rwxvpMV" =
// CHECK-SAME: <{ <i32 0x01ff_fffe>,
sil_property #ExternalGeneric.rw <T: Comparable> (
stored_property #ExternalGeneric.rw : $T)
// CHECK: @"$s19property_descriptor15ExternalGenericV10computedROxvpMV" =
// -- 0x0108_0000 - computed, readonly, has arguments, identified by indirect
// CHECK-SAME: <{ <i32 0x0208_0002>,
// CHECK-SAME: @{{got.|"\\01__imp__?}}id_computed
// CHECK-SAME: [[GET_COMPUTEDRO:@keypath_get[.0-9]*]]{{(\.ptrauth)?}}
// CHECK-SAME: [[GET_ARG_LAYOUT_COMPUTEDRO:@keypath_get_arg_layout[.0-9]*]]{{(\.ptrauth)?}}
// -- default witness table
// CHECK-SAME: i32 0
// CHECK-SAME: [[ARG_INIT_COMPUTEDRO:@keypath_arg_init[.0-9]*]]{{(\.ptrauth.*)?}}
sil_property #ExternalGeneric.computedRO <T: Comparable> (
gettable_property $T,
id @id_computed : $@convention(thin) () -> (),
getter @get_computed_generic : $@convention(keypath_accessor_getter) <T: Comparable> (@in_guaranteed ExternalGeneric<T>) -> @out T)
// CHECK: @"$s19property_descriptor15ExternalGenericV10computedRWxvpMV" =
// -- 0x01c8_0000 - computed, settable, mutating, has arguments, indirect id
// CHECK-SAME: <{ <i32 0x02c8_0002>,
// CHECK-SAME: @{{got.|"\\01__imp__?}}id_computed
// CHECK-SAME: [[GET_COMPUTEDRW:@keypath_get[.0-9]*]]{{(\.ptrauth)?}}
// CHECK-SAME: [[SET_COMPUTEDRW:@keypath_set[.0-9]*]]{{(\.ptrauth)?}}
// CHECK-SAME: [[GET_ARG_LAYOUT_COMPUTEDRW:@keypath_get_arg_layout[.0-9]*]]{{(\.ptrauth)?}}
// CHECK-SAME: i32 0
// CHECK-SAME: [[ARG_INIT_COMPUTEDRW:@keypath_arg_init[.0-9]*]]{{(\.ptrauth.*)?}}
sil_property #ExternalGeneric.computedRW <T: Comparable> (
settable_property $T,
id @id_computed : $@convention(thin) () -> (),
getter @get_computed_generic : $@convention(keypath_accessor_getter) <T: Comparable> (@in_guaranteed ExternalGeneric<T>) -> @out T,
setter @set_computed_generic : $@convention(keypath_accessor_setter) <T: Comparable> (@in_guaranteed T, @inout ExternalGeneric<T>) -> ())
// CHECK: @"$s19property_descriptor15ExternalGenericVyxqd__cSHRd__luipMV" =
// -- 0x01c8_0000 - computed, settable, mutating, has arguments, indirect id
// CHECK-SAME: <{ <i32 0x02c8_0002>,
// CHECK-SAME: @{{got.|"\\01__imp__?}}id_computed
// CHECK-SAME: [[GET_SUBSCRIPT:@keypath_get[.0-9]*]]{{(\.ptrauth)?}}
// CHECK-SAME: [[SET_SUBSCRIPT:@keypath_set[.0-9]*]]{{(\.ptrauth)?}}
// CHECK-SAME: [[GET_ARG_LAYOUT_SUBSCRIPT:@keypath_get_arg_layout[.0-9]*]]{{(\.ptrauth)?}}
// CHECK-SAME: i32 0
// CHECK-SAME: [[ARG_INIT_SUBSCRIPT:@keypath_arg_init[.0-9]*]]{{(\.ptrauth.*)?}}
sil_property #ExternalGeneric.subscript <T: Comparable><U: Hashable> (
settable_property $T,
id @id_computed : $@convention(thin) () -> (),
getter @get_computed_generic_subscript : $@convention(keypath_accessor_getter) <T: Comparable><U: Hashable> (@in_guaranteed ExternalGeneric<T>, @in_guaranteed U) -> @out T,
setter @set_computed_generic_subscript : $@convention(keypath_accessor_setter) <T: Comparable><U: Hashable> (@in_guaranteed T, @inout ExternalGeneric<T>, @in_guaranteed U) -> ())
// CHECK: @"$s19property_descriptor8ExternalV2roSivpMV" =
// CHECK-64: @"$s19property_descriptor8ExternalV2rwSivpMV" =
// CHECK-32: @"$s19property_descriptor8ExternalV2rwSivpMV" =
sil_property #External.ro (stored_property #External.ro : $Int)
sil_property #External.rw (stored_property #External.rw : $Int)
sil_property #External.computedRO (
gettable_property $Int,
id @id_computed : $@convention(thin) () -> (),
getter @get_computed : $@convention(keypath_accessor_getter) (@in_guaranteed External) -> @out Int)
sil_property #External.computedRW (
settable_property $Int,
id @id_computed : $@convention(thin) () -> (),
getter @get_computed : $@convention(keypath_accessor_getter) (@in_guaranteed External) -> @out Int,
setter @set_computed : $@convention(keypath_accessor_setter) (@in_guaranteed Int, @inout External) -> ())
sil_property #External.subscript (
settable_property $Int,
id @id_computed : $@convention(thin) () -> (),
getter @get_computed_subscript : $@convention(keypath_accessor_getter) (@in_guaranteed External, @in_guaranteed String) -> @out Int,
setter @set_computed_subscript : $@convention(keypath_accessor_setter) (@in_guaranteed Int, @inout External, @in_guaranteed String) -> ())
sil_property #ExternalReabstractions.ro <T> (
stored_property #ExternalReabstractions.ro : $T)
sil_property #ExternalReabstractions.reabstracted <T> (
settable_property $() -> (),
id ##ExternalReabstractions.reabstracted,
getter @get_reabstracted : $@convention(keypath_accessor_getter) <T> (@in_guaranteed ExternalReabstractions<T>) -> @out @callee_guaranteed @substituted <U> () -> @out U for <()>,
setter @set_reabstracted : $@convention(keypath_accessor_setter) <T> (@in_guaranteed @callee_guaranteed @substituted <U> () -> @out U for <()>, @inout ExternalReabstractions<T>) -> ())
// All trivial descriptors should share a definition by aliasing to the common
// definition
// CHECK-LABEL: @"$s19property_descriptor15ExternalGenericV29computedWithTrivialDescriptorSivpMV" =
// CHECK-SAME: { i32 } zeroinitializer, align 4
sil_property #ExternalGeneric.computedWithTrivialDescriptor <T: Comparable> ()
// CHECK-LABEL: @"$s19property_descriptor8ExternalV29computedWithTrivialDescriptorSivpMV" =
// CHECK-SAME: alias {{.*}} @"$s19property_descriptor15ExternalGenericV29computedWithTrivialDescriptorSivpMV"
sil_property #External.computedWithTrivialDescriptor ()
sil @id_computed : $@convention(thin) () -> ()
sil @get_computed : $@convention(keypath_accessor_getter) (@in_guaranteed External) -> @out Int
sil @set_computed : $@convention(keypath_accessor_setter) (@in_guaranteed Int, @inout External) -> ()
sil @get_computed_subscript : $@convention(keypath_accessor_getter) (@in_guaranteed External, @in_guaranteed String) -> @out Int
sil @set_computed_subscript : $@convention(keypath_accessor_setter) (@in_guaranteed Int, @inout External, @in_guaranteed String) -> ()
sil @get_computed_generic : $@convention(keypath_accessor_getter) <T: Comparable> (@in_guaranteed ExternalGeneric<T>) -> @out T
sil @set_computed_generic : $@convention(keypath_accessor_setter) <T: Comparable> (@in_guaranteed T, @inout ExternalGeneric<T>) -> ()
sil @get_computed_generic_subscript : $@convention(keypath_accessor_getter) <T: Comparable><U: Hashable> (@in_guaranteed ExternalGeneric<T>, @in_guaranteed U) -> @out T
sil @set_computed_generic_subscript : $@convention(keypath_accessor_setter) <T: Comparable><U: Hashable> (@in_guaranteed T, @inout ExternalGeneric<T>, @in_guaranteed U) -> ()
sil @get_reabstracted : $@convention(keypath_accessor_getter) <T> (@in_guaranteed ExternalReabstractions<T>) -> @out @callee_guaranteed @substituted <U> () -> @out U for <()>
sil @set_reabstracted : $@convention(keypath_accessor_setter) <T> (@in_guaranteed @callee_guaranteed @substituted <U> () -> @out U for <()>, @inout ExternalReabstractions<T>) -> ()