File: definite_init_markuninitialized_derivedself.sil

package info (click to toggle)
swiftlang 6.0.3-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 2,519,992 kB
  • sloc: cpp: 9,107,863; ansic: 2,040,022; asm: 1,135,751; python: 296,500; objc: 82,456; f90: 60,502; lisp: 34,951; pascal: 19,946; sh: 18,133; perl: 7,482; ml: 4,937; javascript: 4,117; makefile: 3,840; awk: 3,535; xml: 914; fortran: 619; cs: 573; ruby: 573
file content (281 lines) | stat: -rw-r--r-- 10,928 bytes parent folder | download
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
// RUN: %target-sil-opt -enable-sil-verify-all %s -definite-init -raw-sil-inst-lowering -verify | %FileCheck %s

// This test only tests mark_uninitialized [derivedself]

sil_stage raw

import Builtin
import Swift

/////////////////
// Definitions //
/////////////////

sil @takes_Int_inout : $@convention(thin) (@inout Int) -> ()
sil @makesInt : $@convention(thin) () -> Int

class RootClassWithIVars {
  var x: Int
  var y: Int
  var z: (Int, Int)
  init()
}

class DerivedClassWithIVars : RootClassWithIVars {
  var a: Int
  override init()
}

sil @superinit : $@convention(method) (@owned RootClassWithIVars) -> @owned RootClassWithIVars

protocol P {}
class C : P {}

class SomeClass {}

sil @getSomeClass : $@convention(thin) () -> @owned SomeClass
sil @getSomeOptionalClass : $@convention(thin) () -> Optional<SomeClass>

///////////
// Tests //
///////////

// This can be generated by using:
//
// func makesInt() -> Int { return 0 }
// func takesIntInout(i: inout Int) -> () {}
// 
// class RootClassWithIVars {
//   var x: Int
//   var y: Int
//   var z: (Int, Int)
//   init() {
//   }
// }
// 
// class DerivedClassWithIVars : RootClassWithIVars {
//   var a: Int
//   override init() {
//     a = makesInt()
//   }
// }
//
sil [ossa] @derived_test1 :  $@convention(method) (@owned DerivedClassWithIVars) -> @owned DerivedClassWithIVars {
bb0(%0 : @owned $DerivedClassWithIVars):
  %1 = alloc_box $<τ_0_0> { var τ_0_0 } <DerivedClassWithIVars>
  %1a = mark_uninitialized [derivedself] %1 : $<τ_0_0> { var τ_0_0 } <DerivedClassWithIVars>
  %3 = project_box %1a : $<τ_0_0> { var τ_0_0 } <DerivedClassWithIVars>, 0
  store %0 to [init] %3 : $*DerivedClassWithIVars

  // Get an int
  %5 = function_ref @makesInt : $@convention(thin) () -> Int
  %7 = apply %5() : $@convention(thin) () -> Int

  // Initialize the 'a' ivar with the int.
  %8 = load_borrow %3 : $*DerivedClassWithIVars
  %9 = ref_element_addr %8 : $DerivedClassWithIVars, #DerivedClassWithIVars.a
  assign %7 to %9 : $*Int
  end_borrow %8 : $DerivedClassWithIVars

  // Then perform the self.init call.
  %11 = load [take] %3 : $*DerivedClassWithIVars
  %13 = upcast %11 : $DerivedClassWithIVars to $RootClassWithIVars
  %14 = function_ref @superinit : $@convention(method) (@owned RootClassWithIVars) -> @owned RootClassWithIVars
  %15 = apply %14(%13) : $@convention(method) (@owned RootClassWithIVars) -> @owned RootClassWithIVars
  %16 = unchecked_ref_cast %15 : $RootClassWithIVars to $DerivedClassWithIVars
  store %16 to [init] %3 : $*DerivedClassWithIVars

  // Finally perform the epilog.
  %18 = load [copy] %3 : $*DerivedClassWithIVars
  destroy_value %1a : $<τ_0_0> { var τ_0_0 } <DerivedClassWithIVars>
  return %18 : $DerivedClassWithIVars
}

// This is testing the following swift:
//
// func makesInt() -> Int { return 0 }
// func takesIntInout(i: inout Int) -> () {}
// 
// class RootClassWithIVars {
//   var x: Int
//   var y: Int
//   var z: (Int, Int)
//   init() {
//   }
// }
// 
// class DerivedClassWithIVars : RootClassWithIVars {
//   var a: Int
//   override init() {
//   }
// }
sil [ossa] @derived_test2 :  $@convention(method) (@owned DerivedClassWithIVars) -> @owned DerivedClassWithIVars {
bb0(%0 : @owned $DerivedClassWithIVars):
  %1 = alloc_box $<τ_0_0> { var τ_0_0 } <DerivedClassWithIVars>
  %1a = mark_uninitialized [derivedself] %1 : $<τ_0_0> { var τ_0_0 } <DerivedClassWithIVars>
  %3 = project_box %1a : $<τ_0_0> { var τ_0_0 } <DerivedClassWithIVars>, 0
  store %0 to [init] %3 : $*DerivedClassWithIVars

  %11 = load [take] %3 : $*DerivedClassWithIVars
  %13 = upcast %11 : $DerivedClassWithIVars to $RootClassWithIVars
  %14 = function_ref @superinit : $@convention(method) (@owned RootClassWithIVars) -> @owned RootClassWithIVars
  %15 = apply %14(%13) : $@convention(method) (@owned RootClassWithIVars) -> @owned RootClassWithIVars     // expected-error {{property 'self.a' not initialized at super.init call}}
  %16 = unchecked_ref_cast %15 : $RootClassWithIVars to $DerivedClassWithIVars
  store %16 to [init] %3 : $*DerivedClassWithIVars

  %18 = load [copy] %3 : $*DerivedClassWithIVars
  destroy_value %1a : $<τ_0_0> { var τ_0_0 } <DerivedClassWithIVars>
  return %18 : $DerivedClassWithIVars
}

struct MyStruct : P {}

struct MyStruct2 {
  var x: P
    init(delegate: ())
    init()
}

class RootClassWithNontrivialStoredProperties {
  var x, y: SomeClass

  init()
}

class DerivedClassWithNontrivialStoredProperties : RootClassWithNontrivialStoredProperties {
  var a, b: SomeClass
  override init()
}

// I was unable to find a test case that could reproduce this since there isn't
// a way before DI today to eliminate the super.init call. I believe it may have
// come from when mandatory inlining ran very early. That being said, we should
// still dot he right thing.
//
// CHECK-LABEL: sil [ossa] @test_derived_release
// CHECK: bb0(%0 : @owned $DerivedClassWithNontrivialStoredProperties):
// CHECK-NEXT: [[SELFBOX:%[0-9]+]] = alloc_stack
// CHECK-NEXT: store
// CHECK-NEXT: [[SELF:%[0-9]+]] = load [take] [[SELFBOX]]
// CHECK-NEXT: [[METATYPE:%[0-9]+]] = metatype $@thick DerivedClassWithNontrivialStoredProperties.Type
// CHECK-NEXT: dealloc_partial_ref [[SELF]] : $DerivedClassWithNontrivialStoredProperties, [[METATYPE]] : $@thick DerivedClassWithNontrivialStoredProperties.Type
// CHECK-NEXT: dealloc_stack [[SELFBOX]]
sil [ossa] @test_derived_release : $@convention(method) (@owned DerivedClassWithNontrivialStoredProperties) -> () {
bb0(%0 : @owned $DerivedClassWithNontrivialStoredProperties):
  %1 = alloc_stack $DerivedClassWithNontrivialStoredProperties
  %4 = mark_uninitialized [derivedself] %1 : $*DerivedClassWithNontrivialStoredProperties
  store %0 to [init] %4 : $*DerivedClassWithNontrivialStoredProperties

  destroy_addr %4 : $*DerivedClassWithNontrivialStoredProperties
  dealloc_stack %1 : $*DerivedClassWithNontrivialStoredProperties

  %13 = tuple ()
  return %13 : $()
}

// I was unable to find a test case that could reproduce this since there isn't
// a way before DI today to eliminate the super.init call. I believe it may have
// come from when mandatory inlining ran very early. That being said, we should
// still dot he right thing.
//
// CHECK-LABEL: sil [ossa] @test_derived_partial_release
// CHECK: bb0([[ARG:%.*]] : @owned $DerivedClassWithNontrivialStoredProperties):
//
// Initialize the self box.
// CHECK: [[SELFBOX:%[0-9]+]] = alloc_stack
// CHECK: store [[ARG]] to [init] [[SELFBOX]]
//
// Update field of self.
// CHECK: [[FIELD_VAL:%.*]] = alloc_ref $SomeClass
// CHECK: [[SELF:%[0-9]+]] = load_borrow [[SELFBOX]]
// CHECK: [[SELF_FIELD:%.*]] = ref_element_addr [[SELF]]
// CHECK: store [[FIELD_VAL]] to [init] [[SELF_FIELD]]
// CHECK: end_borrow [[SELF]]
//
// Now we destroy the stored value.
// CHECK: [[SELF:%[0-9]+]] = load_borrow [[SELFBOX]]
// CHECK: [[SELF_FIELD:%.*]] = ref_element_addr [[SELF]]
// CHECK: [[SELF_FIELD_ACCESS:%.*]] = begin_access [deinit] [static] [[SELF_FIELD]]
// CHECK: destroy_addr [[SELF_FIELD_ACCESS]]
// CHECK: end_access [[SELF_FIELD_ACCESS]]
// CHECK: end_borrow [[SELF]]
//
// And then perform dealloc_partial_ref.
// CHECK: [[SELF:%[0-9]+]] = load [take] [[SELFBOX]]
// CHECK: [[METATYPE:%[0-9]+]] = metatype $@thick DerivedClassWithNontrivialStoredProperties.Type
// CHECK: dealloc_partial_ref [[SELF]] : $DerivedClassWithNontrivialStoredProperties, [[METATYPE]] : $@thick DerivedClassWithNontrivialStoredProperties.Type
// CHECK: dealloc_stack [[SELFBOX]]
// CHECK: } // end sil function 'test_derived_partial_release'
sil [ossa] @test_derived_partial_release : $@convention(method) (@owned DerivedClassWithNontrivialStoredProperties) -> () {
bb0(%0 : @owned $DerivedClassWithNontrivialStoredProperties):
  %1 = alloc_stack $DerivedClassWithNontrivialStoredProperties
  %4 = mark_uninitialized [derivedself] %1 : $*DerivedClassWithNontrivialStoredProperties
  store %0 to [init] %4 : $*DerivedClassWithNontrivialStoredProperties

  %8 = alloc_ref $SomeClass
  %9 = load_borrow %4 : $*DerivedClassWithNontrivialStoredProperties
  %10 = ref_element_addr %9 : $DerivedClassWithNontrivialStoredProperties, #DerivedClassWithNontrivialStoredProperties.a
  assign %8 to %10 : $*SomeClass
  end_borrow %9 : $DerivedClassWithNontrivialStoredProperties

  destroy_addr %4 : $*DerivedClassWithNontrivialStoredProperties
  dealloc_stack %1 : $*DerivedClassWithNontrivialStoredProperties

  %13 = tuple ()
  return %13 : $()
}

// <rdar://problem/18199087> DI doesn't catch use of super properties lexically inside super.init call
//
// *NOTE* There is currently a SILGen ownership bug where SILGen will emit the
// end_borrow too early. This does not need to be fixed in the short term since
// we are only going to verify swift stdlibCore. But it will need to be fixed.
//
// To recreate this:
//
// class Foo {
//   var x: Int
//   init() {}
//   init(i: Int) {}
// }
// 
// class Foo2 : Foo {
//   override init() {    
//     super.init(i: self.x) // <--- The important part.
//   }
// }
//
sil [ossa] @super_init_out_of_order :  $@convention(method) (@owned DerivedClassWithIVars, Int) -> @owned DerivedClassWithIVars {
bb0(%0 : @owned $DerivedClassWithIVars, %i : $Int):
  %1 = alloc_box $<τ_0_0> { var τ_0_0 } <DerivedClassWithIVars>
  %1a = mark_uninitialized [derivedself] %1 : $<τ_0_0> { var τ_0_0 } <DerivedClassWithIVars>
  %3 = project_box %1a : $<τ_0_0> { var τ_0_0 } <DerivedClassWithIVars>, 0
  store %0 to [init] %3 : $*DerivedClassWithIVars

  // Initialize properties in derived class.
  %8 = load_borrow %3 : $*DerivedClassWithIVars
  %9 = ref_element_addr %8 : $DerivedClassWithIVars, #DerivedClassWithIVars.a
  assign %i to %9 : $*Int
  end_borrow %8 : $DerivedClassWithIVars

  // Get the super.init function information, but don't apply it.
  %11 = load [take] %3 : $*DerivedClassWithIVars
  %13 = upcast %11 : $DerivedClassWithIVars to $RootClassWithIVars
  %14 = function_ref @superinit : $@convention(method) (@owned RootClassWithIVars) -> @owned RootClassWithIVars

  // Access a super property before super.init is called.
  %13a = begin_borrow %13 : $RootClassWithIVars
  %13b = unchecked_ref_cast %13a : $RootClassWithIVars to $DerivedClassWithIVars
  %13c = upcast %13b : $DerivedClassWithIVars to $RootClassWithIVars
  %13d = ref_element_addr %13c : $RootClassWithIVars, #RootClassWithIVars.x  // expected-error {{'self' used in property access 'x' before 'super.init' call}}
  load [trivial] %13d : $*Int
  end_borrow %13a : $RootClassWithIVars

  // Call super.init.
  %15 = apply %14(%13) : $@convention(method) (@owned RootClassWithIVars) -> @owned RootClassWithIVars
  %16 = unchecked_ref_cast %15 : $RootClassWithIVars to $DerivedClassWithIVars
  store %16 to [init] %3 : $*DerivedClassWithIVars
  %18 = load [copy] %3 : $*DerivedClassWithIVars
  destroy_value %1a : $<τ_0_0> { var τ_0_0 } <DerivedClassWithIVars>
  return %18 : $DerivedClassWithIVars
}