Skip to content

Commit 937c85c

Browse files
committed
Add documentation and some tests
Add case to SILVerifier
1 parent 5d0deaa commit 937c85c

File tree

3 files changed

+81
-19
lines changed

3 files changed

+81
-19
lines changed

test/IRGen/raw_layout.swift

Lines changed: 28 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
// RUN: %{python} %utils/chex.py < %s > %t/raw_layout.sil
33
// RUN: %target-swift-frontend -enable-experimental-feature RawLayout -emit-ir -disable-availability-checking %t/raw_layout.sil | %FileCheck %t/raw_layout.sil --check-prefix=CHECK --check-prefix=CHECK-%target-ptrsize
44

5+
import Builtin
56
import Swift
67

78
// CHECK-LABEL: @"$s{{[A-Za-z0-9_]*}}4LockVWV" = {{.*}} %swift.vwtable
@@ -142,6 +143,33 @@ struct BadBuffer: ~Copyable {
142143
let buffer: SmallVectorOf3<Int64?>
143144
}
144145

146+
sil @use_lock : $@convention(thin) (@in_guaranteed Lock) -> () {
147+
entry(%L: $*Lock):
148+
return undef : $()
149+
}
150+
151+
sil @use_keymaster_locks : $@convention(thin) (@in_guaranteed Keymaster) -> () {
152+
entry(%K: $*Keymaster):
153+
%f = function_ref @use_lock : $@convention(thin) (@in_guaranteed Lock) -> ()
154+
%a = struct_element_addr %K : $*Keymaster, #Keymaster.lock1
155+
apply %f(%a) : $@convention(thin) (@in_guaranteed Lock) -> ()
156+
%b = struct_element_addr %K : $*Keymaster, #Keymaster.lock2
157+
apply %f(%b) : $@convention(thin) (@in_guaranteed Lock) -> ()
158+
%c = struct_element_addr %K : $*Keymaster, #Keymaster.lock2
159+
apply %f(%c) : $@convention(thin) (@in_guaranteed Lock) -> ()
160+
return undef : $()
161+
}
162+
163+
// CHECK: define swiftcc ptr @get_cell_addr(ptr %"Cell<T>", ptr {{.*}} swiftself [[SELF:%.*]])
164+
// CHECK-NEXT: entry:
165+
// CHECK-NEXT: ret ptr [[SELF]]
166+
sil @get_cell_addr : $@convention(method) <T> (@in_guaranteed Cell<T>) -> UnsafeMutablePointer<T> {
167+
entry(%0 : $*Cell<T>):
168+
%1 = raw_layout_address_to_pointer %0 : $*Cell<T> to $Builtin.RawPointer
169+
%2 = struct $UnsafeMutablePointer<T> (%1 : $Builtin.RawPointer)
170+
return %2 : $UnsafeMutablePointer<T>
171+
}
172+
145173
// Dependent layout metadata initialization:
146174

147175
// Cell<T>
@@ -165,20 +193,3 @@ struct BadBuffer: ~Copyable {
165193

166194
// CHECK-LABEL: define {{.*}} swiftcc %swift.metadata_response @"$s{{[A-Za-z0-9_]*}}14SmallVectorBufVMr"(ptr %"SmallVectorBuf<T>", ptr {{.*}}, ptr {{.*}})
167195
// CHECK: call void @swift_initRawStructMetadata(ptr %"SmallVectorBuf<T>", {{i64|i32}} 0, ptr {{%.*}}, {{i64|i32}} 8)
168-
169-
sil @use_lock : $@convention(thin) (@in_guaranteed Lock) -> () {
170-
entry(%L: $*Lock):
171-
return undef : $()
172-
}
173-
174-
sil @use_keymaster_locks : $@convention(thin) (@in_guaranteed Keymaster) -> () {
175-
entry(%K: $*Keymaster):
176-
%f = function_ref @use_lock : $@convention(thin) (@in_guaranteed Lock) -> ()
177-
%a = struct_element_addr %K : $*Keymaster, #Keymaster.lock1
178-
apply %f(%a) : $@convention(thin) (@in_guaranteed Lock) -> ()
179-
%b = struct_element_addr %K : $*Keymaster, #Keymaster.lock2
180-
apply %f(%b) : $@convention(thin) (@in_guaranteed Lock) -> ()
181-
%c = struct_element_addr %K : $*Keymaster, #Keymaster.lock2
182-
apply %f(%c) : $@convention(thin) (@in_guaranteed Lock) -> ()
183-
return undef : $()
184-
}

test/SILGen/raw_layout.swift

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,13 @@
1-
// RUN: %target-swift-emit-silgen -enable-experimental-feature RawLayout %s | %FileCheck %s
1+
// RUN: %target-swift-emit-silgen -enable-experimental-feature RawLayout -enable-builtin-module %s | %FileCheck %s
22

33
// XFAIL: noncopyable_generics
44

55
// CHECK: @_rawLayout(size: 4, alignment: 4) @_moveOnly struct Lock
66
// CHECK: @_rawLayout(like: T) @_moveOnly struct Cell<T>
77
// CHECK: @_rawLayout(likeArrayOf: T, count: 8) @_moveOnly struct SmallVectorBuf<T>
88

9+
import Builtin
10+
911
@_rawLayout(size: 4, alignment: 4)
1012
struct Lock: ~Copyable {
1113
// Raw layout type should be lowered as address only
@@ -22,7 +24,18 @@ struct Lock: ~Copyable {
2224
}
2325

2426
@_rawLayout(like: T)
25-
struct Cell<T>: ~Copyable {}
27+
struct Cell<T>: ~Copyable {
28+
// CHECK-LABEL: sil {{.*}} @$s10raw_layout4CellV7addressSpyxGvg : $@convention(method) <T> (@in_guaranteed Cell<T>) -> UnsafeMutablePointer<T> {
29+
// CHECK: [[RAW_LAYOUT_ADDR:%.*]] = raw_layout_address_to_pointer {{%.*}} : $*Cell<T> to $Builtin.RawPointer
30+
// CHECK-LABEL: } // end sil function '$s10raw_layout4CellV7addressSpyxGvg'
31+
var address: UnsafeMutablePointer<T> {
32+
.init(Builtin.addressOfRawLayout(self))
33+
}
34+
35+
init(_ value: consuming T) {
36+
address.initialize(to: value)
37+
}
38+
}
2639

2740
@_rawLayout(likeArrayOf: T, count: 8)
2841
struct SmallVectorBuf<T>: ~Copyable {}

test/SILOptimizer/stdlib/Cell.swift

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
// RUN: %target-swift-frontend -O -emit-sil -disable-availability-checking %s -enable-builtin-module -enable-experimental-feature RawLayout -enable-experimental-feature NoncopyableGenerics | %FileCheck %s
2+
3+
import Builtin
4+
5+
@frozen
6+
@_rawLayout(like: T)
7+
public struct Cell<T: ~Copyable>: ~Copyable {
8+
// CHECK-LABEL: sil {{.*}} @$s4CellAAVAARiczrlE7addressSpyxGvg : $@convention(method) <T where T : ~Copyable> (@in_guaranteed Cell<T>) -> UnsafeMutablePointer<T> {
9+
// CHECK: bb0([[SELF:%.*]] : $*Cell<T>):
10+
// CHECK: [[RAW_LAYOUT_ADDR:%.*]] = raw_layout_address_to_pointer [[SELF]] : $*Cell<T> to $Builtin.RawPointer
11+
// CHECK-NEXT: [[POINTER:%.*]] = struct $UnsafeMutablePointer<T> ([[RAW_LAYOUT_ADDR]] : $Builtin.RawPointer)
12+
// CHECK-NEXT: return [[POINTER]] : $UnsafeMutablePointer<T>
13+
// CHECK-LABEL: } // end sil function '$s4CellAAVAARiczrlE7addressSpyxGvg'
14+
@_transparent
15+
public var address: UnsafeMutablePointer<T> {
16+
.init(Builtin.addressOfRawLayout(self))
17+
}
18+
19+
// CHECK-LABEL: sil {{.*}} @$s4CellAAVAARiczrlEyAByxGxcfC : $@convention(method) <T where T : ~Copyable> (@in T, @thin Cell<T>.Type) -> @out Cell<T> {
20+
// CHECK: bb0([[SELF:%.*]] : $*Cell<T>, [[VALUE:%.*]] : $*T, {{%.*}} : $@thin Cell<T>.Type):
21+
// CHECK-NEXT: {{%.*}} = builtin "zeroInitializer"<Cell<T>>([[SELF]] : $*Cell<T>) : $()
22+
// CHECK-NEXT: [[RAW_LAYOUT_ADDR:%.*]] = raw_layout_address_to_pointer [[SELF]] : $*Cell<T> to $Builtin.RawPointer
23+
// CHECK-NEXT: [[POINTER:%.*]] = struct $UnsafeMutablePointer<T> ([[RAW_LAYOUT_ADDR]] : $Builtin.RawPointer)
24+
// Calling 'UnsafeMutablePointer<T>.initialize(to:)'
25+
// CHECK: {{%.*}} = apply {{%.*}}<T>([[VALUE]], [[POINTER]])
26+
// CHECK-NEXT: [[TUPLE:%.*]] = tuple ()
27+
// CHECK-NEXT: return [[TUPLE]] : $()
28+
// CHECK-LABEL: } // end sil function '$s4CellAAVAARiczrlEyAByxGxcfC'
29+
@_transparent
30+
public init(_ value: consuming T) {
31+
address.initialize(to: value)
32+
}
33+
34+
@inlinable
35+
deinit {
36+
address.deinitialize(count: 1)
37+
}
38+
}

0 commit comments

Comments
 (0)