1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318
|
// RUN: %empty-directory(%t)
// RUN: %target-swift-frontend -emit-module -enable-library-evolution -emit-module-path=%t/resilient_struct.swiftmodule -module-name=resilient_struct %S/../Inputs/resilient_struct.swift
// RUN: %target-swift-frontend -I %t -emit-ir -enable-library-evolution %s -target %target-pre-stable-abi-triple | %FileCheck %s --check-prefix=CHECK --check-prefix=CHECK-OLD --check-prefix=CHECK-%target-ptrsize --check-prefix=CHECK-OLD-%target-ptrsize -DINT=i%target-ptrsize
// RUN: %target-swift-frontend -I %t -emit-ir -enable-library-evolution %s -target %target-pre-stable-abi-triple
// RUN: %target-swift-frontend -I %t -emit-ir -enable-library-evolution -O %s -target %target-pre-stable-abi-triple
// REQUIRES: objc_interop
// UNSUPPORTED: swift_only_stable_abi
// With the old deployment target, these classes use the 'singleton' metadata
// initialization pattern. The class is not statically visible to Objective-C,
// and the Swift runtime adds the class using objc_readClassPair() once it
// has computed field offsets and so on.
//
// With the new deployment target, these classes use the 'update' metadata
// initialization pattern. These class *are* visible to Objective-C, and the
// Swift runtime realizes them using the new _objc_realizeClassFromSwift()
// entry point.
//
// See class_update_callback_without_fixed_layout_stable_abi.swift, which uses
// the same input file, for the stable ABI deployment target test.
sil_stage canonical
import Builtin
import Swift
import SwiftShims
import resilient_struct
public class ClassWithResilientField {
var first: Int
var second: Size
var third: Int
init(x: Int, y: Size, z: Int)
}
sil_vtable ClassWithResilientField {}
public class SubclassOfClassWithResilientField : ClassWithResilientField {}
sil_vtable SubclassOfClassWithResilientField {}
// Field offsets are not statically known:
// CHECK-32-DAG: @"$s42class_update_callback_without_fixed_layout23ClassWithResilientFieldC5firstSivpWvd" = hidden constant i32 8
// CHECK-64-DAG: @"$s42class_update_callback_without_fixed_layout23ClassWithResilientFieldC5firstSivpWvd" = hidden constant i64 16
// CHECK-DAG: @"$s42class_update_callback_without_fixed_layout23ClassWithResilientFieldC6second16resilient_struct4SizeVvpWvd" = hidden global [[INT]] 0
// CHECK-DAG: @"$s42class_update_callback_without_fixed_layout23ClassWithResilientFieldC5thirdSivpWvd" = hidden global [[INT]] 0
// RO-data for ClassWithResilientField:
// CHECK-LABEL: @_DATA__TtC42class_update_callback_without_fixed_layout23ClassWithResilientField = internal constant {
// -- flags: compiled with ARC
// CHECK-OLD-SAME: i32 128,
// -- flags: compiled with ARC, has update callback
// CHECK-NEW-SAME: i32 192,
// -- the update callback
// CHECK-NEW-SAME: @"$s42class_update_callback_without_fixed_layout23ClassWithResilientFieldCMU{{(\.ptrauth)?}}"
// CHECK-SAME: }, section "__DATA, {{.*}}"
// Class has static metadata:
// CHECK-LABEL: @"$s42class_update_callback_without_fixed_layout23ClassWithResilientFieldCMf"
// Subclass also has static metadata:
// CHECK-LABEL: @"$s42class_update_callback_without_fixed_layout33SubclassOfClassWithResilientFieldCMf" = internal global <{{.*}}> <{
// Superclass field is filled in dynamically:
// CHECK-OLD-SAME: ptr null,
// Superclass field is filled in statically:
// CHECK-NEW-SAME: @"$s42class_update_callback_without_fixed_layout23ClassWithResilientFieldCMf"
// CHECK-SAME: }>
enum EnumWithResilientPayload {
case none
case some(ClassWithResilientEnum)
case data(ResilientRef)
}
public class ClassWithResilientEnum {
var first: EnumWithResilientPayload
var second: Int8
init(x: EnumWithResilientPayload, y: Int8)
}
sil_vtable ClassWithResilientEnum {}
// Make sure extra inhabitants work when reading a legacy layout -- the
// Optional<ResilientRef> should be 8 bytes in size, not 9
public class ClassWithResilientRef {
var first: ResilientRef?
var second: Int
}
sil_vtable ClassWithResilientRef {}
// Field offsets are not statically known:
// CHECK-DAG: @"$s42class_update_callback_without_fixed_layout21ClassWithResilientRefC5first16resilient_struct0iJ0VSgvpWvd" = hidden global [[INT]] 0
// CHECK-DAG: @"$s42class_update_callback_without_fixed_layout21ClassWithResilientRefC6secondSivpWvd" = hidden global [[INT]] 0
// When allocating a class with resiliently-sized fields, we must still load
// the size and alignment from metadata, because its metadata may have been
// re-initialized.
// CHECK-LABEL: define {{(dllexport )?}}{{(protected )?}}swiftcc void @allocClassWithResilientField()
sil @allocClassWithResilientField : $@convention(thin) () -> () {
bb0:
// CHECK: [[T0:%.*]] = call swiftcc %swift.metadata_response @"$s42class_update_callback_without_fixed_layout23ClassWithResilientFieldCMa"([[INT]] 0)
// CHECK-NEXT: [[META:%.*]] = extractvalue %swift.metadata_response [[T0]], 0
// CHECK-32-NEXT: [[SIZE_ADDR:%.*]] = getelementptr inbounds i8, ptr [[META]], i32 28
// CHECK-64-NEXT: [[SIZE_ADDR:%.*]] = getelementptr inbounds i8, ptr [[META]], i32 48
// CHECK-32-NEXT: [[SIZE:%.*]] = load i32, ptr [[SIZE_ADDR]]
// CHECK-64-NEXT: [[SIZE_2:%.*]] = load i32, ptr [[SIZE_ADDR]]
// CHECK-64-NEXT: [[SIZE:%.*]] = zext i32 [[SIZE_2]] to i64
// CHECK-32-NEXT: [[ALIGN_ADDR:%.*]] = getelementptr inbounds i8, ptr [[META]], i32 32
// CHECK-64-NEXT: [[ALIGN_ADDR:%.*]] = getelementptr inbounds i8, ptr [[META]], i32 52
// CHECK-NEXT: [[ALIGN_2:%.*]] = load i16, ptr [[ALIGN_ADDR]]
// CHECK-NEXT: [[ALIGN:%.*]] = zext i16 [[ALIGN_2]] to [[INT]]
// CHECK-NEXT: [[ALLOC:%.*]] = call noalias ptr @swift_allocObject(ptr [[META]], [[INT]] [[SIZE]], [[INT]] [[ALIGN]])
%c = alloc_ref $ClassWithResilientField
// ... dealloc_ref also does the same thing.
// CHECK-NEXT: [[META:%.*]] = load ptr, ptr [[ALLOC]]
// CHECK-32-NEXT: [[SIZE_ADDR:%.*]] = getelementptr inbounds i8, ptr [[META]], i32 28
// CHECK-64-NEXT: [[SIZE_ADDR:%.*]] = getelementptr inbounds i8, ptr [[META]], i32 48
// CHECK-32-NEXT: [[SIZE:%.*]] = load i32, ptr [[SIZE_ADDR]]
// CHECK-64-NEXT: [[SIZE_2:%.*]] = load i32, ptr [[SIZE_ADDR]]
// CHECK-64-NEXT: [[SIZE:%.*]] = zext i32 [[SIZE_2]] to i64
// CHECK-32-NEXT: [[ALIGN_ADDR:%.*]] = getelementptr inbounds i8, ptr [[META]], i32 32
// CHECK-64-NEXT: [[ALIGN_ADDR:%.*]] = getelementptr inbounds i8, ptr [[META]], i32 52
// CHECK-NEXT: [[ALIGN_2:%.*]] = load i16, ptr [[ALIGN_ADDR]]
// CHECK-NEXT: [[ALIGN:%.*]] = zext i16 [[ALIGN_2]] to [[INT]]
// CHECK-NEXT: call void @swift_deallocClassInstance(ptr [[ALLOC]], [[INT]] [[SIZE]], [[INT]] [[ALIGN]])
dealloc_ref %c : $ClassWithResilientField
%result = tuple ()
return %result : $()
}
// Metadata accessor for ClassWithResilientField uses singleton initialization:
// CHECK-LABEL: define swiftcc %swift.metadata_response @"$s42class_update_callback_without_fixed_layout23ClassWithResilientFieldCMa
// CHECK: [[CACHE:%.*]] = load ptr, ptr @"$s42class_update_callback_without_fixed_layout23ClassWithResilientFieldCMl"
// CHECK-NEXT: [[COND:%.*]] = icmp eq ptr [[CACHE]], null
// CHECK-NEXT: br i1 [[COND]], label %cacheIsNull, label %cont
// CHECK: cacheIsNull:
// CHECK-NEXT: [[RESPONSE:%.*]] = call swiftcc %swift.metadata_response @swift_getSingletonMetadata([[INT]] %0, ptr @"$s42class_update_callback_without_fixed_layout23ClassWithResilientFieldCMn")
// CHECK-NEXT: [[METADATA:%.*]] = extractvalue %swift.metadata_response [[RESPONSE]], 0
// CHECK-NEXT: [[STATUS:%.*]] = extractvalue %swift.metadata_response [[RESPONSE]], 1
// CHECK-NEXT: br label %cont
// CHECK: cont:
// CHECK-NEXT: [[NEW_METADATA:%.*]] = phi ptr [ [[CACHE]], %entry ], [ [[METADATA]], %cacheIsNull ]
// CHECK-NEXT: [[NEW_STATUS:%.*]] = phi [[INT]] [ 0, %entry ], [ [[STATUS]], %cacheIsNull ]
// CHECK-NEXT: [[T0:%.*]] = insertvalue %swift.metadata_response undef, ptr [[NEW_METADATA]], 0
// CHECK-NEXT: [[T1:%.*]] = insertvalue %swift.metadata_response [[T0]], [[INT]] [[NEW_STATUS]], 1
// CHECK-NEXT: ret %swift.metadata_response [[T1]]
// Accessing a field whose offset depends on resilient types should
// use the field offset global.
// CHECK-32-LABEL: define swiftcc {{(dllexport )?}}{{(protected )?}}i32 @accessClassWithResilientField(ptr %0)
// CHECK-64-LABEL: define swiftcc {{(dllexport )?}}{{(protected )?}}i64 @accessClassWithResilientField(ptr %0)
sil @accessClassWithResilientField : $@convention(thin) (@guaranteed ClassWithResilientField) -> Int {
bb0(%0 : $ClassWithResilientField):
// CHECK: [[OFFSET:%.*]] = load [[INT]], ptr @"$s42class_update_callback_without_fixed_layout23ClassWithResilientFieldC5thirdSivpWvd"
// CHECK-NEXT: [[FIELD_ADDR:%.*]] = getelementptr inbounds i8, ptr %0, [[INT]] [[OFFSET]]
// CHECK-NEXT: [[FIELD_PTR:%.*]] = getelementptr inbounds %TSi, ptr [[FIELD_ADDR]], i32 0, i32 0
%1 = ref_element_addr %0 : $ClassWithResilientField, #ClassWithResilientField.third
// CHECK-NEXT: [[VALUE:%.*]] = load [[INT]], ptr [[FIELD_PTR]]
%2 = load %1 : $*Int
// CHECK-NEXT: ret [[INT]] [[VALUE]]
return %2 : $Int
}
// Objective-C metadata update callback function for ClassWithResilientField:
// CHECK-OLD-NOT: define internal ptr @"$s42class_update_callback_without_fixed_layout23ClassWithResilientFieldCMU"(ptr %0, ptr %1)
// CHECK-NEW-LABEL: define internal ptr @"$s42class_update_callback_without_fixed_layout23ClassWithResilientFieldCMU"(ptr %0, ptr %1)
// CHECK-NEW: entry:
// CHECK-NEW-NEXT: [[RESPONSE:%.*]] = call swiftcc %swift.metadata_response @"$s42class_update_callback_without_fixed_layout23ClassWithResilientFieldCMa"([[INT]] 0)
// CHECK-NEW-NEXT: [[METADATA:%.*]] = extractvalue %swift.metadata_response [[RESPONSE]], 0
// CHECK-NEW-NEXT: ret ptr [[METADATA]]
// Metadata initialization function for ClassWithResilientField calls swift_updateClassMetadata2()
// with the new deployment target:
// CHECK-LABEL: define internal swiftcc %swift.metadata_response @"$s42class_update_callback_without_fixed_layout23ClassWithResilientFieldCMr"(ptr %0, ptr %1, ptr %2)
// CHECK: entry:
// CHECK-NEXT: [[FIELDS:%.*]] = alloca [3 x ptr]
// CHECK-32-NEXT: [[FIELDS_DEST:%.*]] = getelementptr inbounds [[INT]], ptr %0, [[INT]] 13
// CHECK-64-NEXT: [[FIELDS_DEST:%.*]] = getelementptr inbounds [[INT]], ptr %0, [[INT]] 10
// CHECK-32-NEXT: call void @llvm.lifetime.start.p0(i64 12, ptr [[FIELDS]])
// CHECK-64-NEXT: call void @llvm.lifetime.start.p0(i64 24, ptr [[FIELDS]])
// CHECK-NEXT: [[FIELDS_PTR:%.*]] = getelementptr inbounds [3 x ptr], ptr [[FIELDS]], i32 0, i32 0
// CHECK: [[T0:%.*]] = call swiftcc %swift.metadata_response @"$s16resilient_struct4SizeVMa"([[INT]] 319)
// CHECK-NEXT: [[SIZE_METADATA:%.*]] = extractvalue %swift.metadata_response [[T0]], 0
// CHECK-NEXT: [[STATUS:%.*]] = extractvalue %swift.metadata_response [[T0]], 1
// CHECK-NEXT: [[RESULT:%.*]] = icmp ule [[INT]] [[STATUS]], 63
// CHECK-NEXT: br i1 [[RESULT]], label %dependency-satisfied, label %metadata-dependencies.cont
// CHECK: dependency-satisfied:
// -- ClassLayoutFlags = 0x100 (HasStaticVTable)
// CHECK-OLD: [[T0:%.*]] = call swiftcc %swift.metadata_response @swift_initClassMetadata2(ptr %0, [[INT]] 256, [[INT]] 3, ptr [[FIELDS_PTR]], ptr [[FIELDS_DEST]])
// CHECK-NEW: [[T0:%.*]] = call swiftcc %swift.metadata_response @swift_updateClassMetadata2(ptr %0, [[INT]] 256, [[INT]] 3, ptr [[FIELDS_PTR]], ptr [[FIELDS_DEST]])
// CHECK-NEXT: [[INITDEP_METADATA:%.*]] = extractvalue %swift.metadata_response [[T0]], 0
// CHECK-NEXT: [[INITDEP_STATUS:%.*]] = extractvalue %swift.metadata_response [[T0]], 1
// CHECK-NEXT: [[INITDEP_PRESENT:%.*]] = icmp eq ptr [[INITDEP_METADATA]], null
// CHECK-NEXT: br i1 [[INITDEP_PRESENT]], label %dependency-satisfied1, label %metadata-dependencies.cont
// CHECK: dependency-satisfied1:
// CHECK-native: [[FIELD_OFFSET:%.*]] = load [[INT]], ptr {{.*}}
// CHECK-native-NEXT: store [[INT]] [[FIELD_OFFSET]], ptr @"$s42class_update_callback_without_fixed_layout23ClassWithResilientFieldC1s16resilient_struct4SizeVvpWvd"
// CHECK-native: [[FIELD_OFFSET:%.*]] = load [[INT]], ptr {{.*}}
// CHECK-native-NEXT: store [[INT]] [[FIELD_OFFSET]], ptr @"$s42class_update_callback_without_fixed_layout23ClassWithResilientFieldC5colors5Int32VvpWvd"
// CHECK: br label %metadata-dependencies.cont
// CHECK: metadata-dependencies.cont:
// CHECK-NEXT: [[PENDING_METADATA:%.*]] = phi ptr [ [[SIZE_METADATA]], %entry ], [ [[INITDEP_METADATA]], %dependency-satisfied ], [ null, %dependency-satisfied1 ]
// CHECK-NEXT: [[NEW_STATUS:%.*]] = phi [[INT]] [ 63, %entry ], [ [[INITDEP_STATUS]], %dependency-satisfied ], [ 0, %dependency-satisfied1 ]
// CHECK-NEXT: [[T0:%.*]] = insertvalue %swift.metadata_response undef, ptr [[PENDING_METADATA]], 0
// CHECK-NEXT: [[T1:%.*]] = insertvalue %swift.metadata_response [[T0]], [[INT]] [[NEW_STATUS]], 1
// CHECK-NEXT: ret %swift.metadata_response [[T1]]
// Metadata initialization function for SubclassOfClassWithResilientField:
// CHECK-LABEL: define internal swiftcc %swift.metadata_response @"$s42class_update_callback_without_fixed_layout33SubclassOfClassWithResilientFieldCMr"(ptr %0, ptr %1, ptr %2)
// CHECK: entry:
// CHECK-NEXT: [[FIELDS:%.*]] = alloca [0 x ptr]
// CHECK-32-NEXT: [[FIELDS_DEST:%.*]] = getelementptr inbounds [[INT]], ptr %0, [[INT]] 17
// CHECK-64-NEXT: [[FIELDS_DEST:%.*]] = getelementptr inbounds [[INT]], ptr %0, [[INT]] 14
// CHECK-NEXT: call void @llvm.lifetime.start.p0(i64 0, ptr [[FIELDS]])
// CHECK-NEXT: [[FIELDS_PTR:%.*]] = getelementptr inbounds [0 x ptr], ptr [[FIELDS]], i32 0, i32 0
// -- ClassLayoutFlags = 0x100 (HasStaticVTable)
// CHECK-OLD: [[T0:%.*]] = call swiftcc %swift.metadata_response @swift_initClassMetadata2(ptr %0, [[INT]] 256, [[INT]] 0, ptr [[FIELDS_PTR]], ptr [[FIELDS_DEST]])
// CHECK-NEW: [[T0:%.*]] = call swiftcc %swift.metadata_response @swift_updateClassMetadata2(ptr %0, [[INT]] 256, [[INT]] 0, ptr [[FIELDS_PTR]], ptr [[FIELDS_DEST]])
// CHECK-NEXT: [[INITDEP_METADATA:%.*]] = extractvalue %swift.metadata_response [[T0]], 0
// CHECK-NEXT: [[INITDEP_STATUS:%.*]] = extractvalue %swift.metadata_response [[T0]], 1
// CHECK-NEXT: [[INITDEP_PRESENT:%.*]] = icmp eq ptr [[INITDEP_METADATA]], null
// CHECK-NEXT: br i1 [[INITDEP_PRESENT]], label %dependency-satisfied, label %metadata-dependencies.cont
// CHECK: dependency-satisfied:
// CHECK: br label %metadata-dependencies.cont
// CHECK: metadata-dependencies.cont:
// CHECK-NEXT: [[PENDING_METADATA:%.*]] = phi ptr [ [[INITDEP_METADATA]], %entry ], [ null, %dependency-satisfied ]
// CHECK-NEXT: [[NEW_STATUS:%.*]] = phi [[INT]] [ [[INITDEP_STATUS]], %entry ], [ 0, %dependency-satisfied ]
// CHECK-NEXT: [[T0:%.*]] = insertvalue %swift.metadata_response undef, ptr [[PENDING_METADATA]], 0
// CHECK-NEXT: [[T1:%.*]] = insertvalue %swift.metadata_response [[T0]], [[INT]] [[NEW_STATUS]], 1
// CHECK-NEXT: ret %swift.metadata_response [[T1]]
// Metadata accessor for ClassWithResilientEnum looks like singleton initialization:
// CHECK-LABEL: define swiftcc %swift.metadata_response @"$s42class_update_callback_without_fixed_layout22ClassWithResilientEnumCMa
// CHECK: [[CACHE:%.*]] = load ptr, ptr @"$s42class_update_callback_without_fixed_layout22ClassWithResilientEnumCMl"
// CHECK-NEXT: [[COND:%.*]] = icmp eq ptr [[CACHE]], null
// CHECK-NEXT: br i1 [[COND]], label %cacheIsNull, label %cont
// CHECK: cacheIsNull:
// CHECK-NEXT: [[RESPONSE:%.*]] = call swiftcc %swift.metadata_response @swift_getSingletonMetadata([[INT]] %0, ptr @"$s42class_update_callback_without_fixed_layout22ClassWithResilientEnumCMn")
// CHECK-NEXT: [[METADATA:%.*]] = extractvalue %swift.metadata_response [[RESPONSE]], 0
// CHECK-NEXT: [[STATUS:%.*]] = extractvalue %swift.metadata_response [[RESPONSE]], 1
// CHECK-NEXT: br label %cont
// CHECK: cont:
// CHECK-NEXT: [[NEW_METADATA:%.*]] = phi ptr [ [[CACHE]], %entry ], [ [[METADATA]], %cacheIsNull ]
// CHECK-NEXT: [[NEW_STATUS:%.*]] = phi [[INT]] [ 0, %entry ], [ [[STATUS]], %cacheIsNull ]
// CHECK-NEXT: [[T0:%.*]] = insertvalue %swift.metadata_response undef, ptr [[NEW_METADATA]], 0
// CHECK-NEXT: [[T1:%.*]] = insertvalue %swift.metadata_response [[T0]], [[INT]] [[NEW_STATUS]], 1
// CHECK-NEXT: ret %swift.metadata_response [[T1]]
// Make sure we cache the construction of fully bound optional types.
// CHECK-LABEL: define internal swiftcc %swift.metadata_response @"$s42class_update_callback_without_fixed_layout21ClassWithResilientRefCMr"
// CHECK: call swiftcc %swift.metadata_response @"$s16resilient_struct12ResilientRefVSgMa"
// CHECK: ret
// CHECK-LABEL: define linkonce_odr hidden swiftcc %swift.metadata_response @"$s16resilient_struct12ResilientRefVSgMa"
// CHECK: call swiftcc %swift.metadata_response @"$s16resilient_struct12ResilientRefVMa"
// CHECK: call swiftcc %swift.metadata_response @"$sSqMa"
// CHECK: ret
|