File: devirt_speculative.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 (199 lines) | stat: -rw-r--r-- 5,647 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
// RUN: %target-sil-opt -enable-objc-interop -module-name devirt_speculative -enable-sil-verify-all %s -specdevirt  | %FileCheck %s

sil_stage canonical

import Builtin
import Swift

@objc class MyNSObject {}
private class Base : MyNSObject {
   override init()
  @inline(never) func foo()
  @inline(never) func exit() -> Never
}

private class Sub : Base {
  override init()
  @inline(never) override func foo()
  @inline(never) override func exit() -> Never
}

sil private [noinline] @_TBaseFooFun : $@convention(method) (@guaranteed Base) -> () {
bb0(%0 : $Base):
  %1 = tuple()
  return %1 : $()
}

sil private [noinline] @_TSubFooFun : $@convention(method) (@guaranteed Sub) -> () {
bb0(%0 : $Sub):
  %1 = tuple()
  return %1 : $()
}


sil @Base_exit : $@convention(method) (@guaranteed Base) -> Never

sil @Sub_exit : $@convention(method) (@guaranteed Sub) -> Never

sil_vtable Base {
  #Base.foo: @_TBaseFooFun
  #Base.exit: @Base_exit 
}

sil_vtable Sub {
  #Base.foo: @_TSubFooFun [override]
  #Base.exit: @Sub_exit  [override]
}

sil @test_objc_ancestry : $@convention(thin) (@guaranteed Base) -> () {
bb0(%0: $Base):
  %1 = class_method %0 : $Base, #Base.foo : (Base) -> () -> (), $@convention(method) (@guaranteed Base) -> ()
  %2 = apply %1(%0) : $@convention(method) (@guaranteed Base) -> ()
  %3 = tuple()
  return %3 : $()
}

// Make sure we leave the generic method call because an objc derived class can be extended at runtime.

// CHECK-LABEL: sil @test_objc_ancestry
// CHECK: bb0
// CHECK:  [[METH:%.*]] = class_method %0 : $Base, #Base.foo : (Base) -> () -> (), $@convention(method) (@guaranteed Base) -> ()
// CHECK:  checked_cast_br [exact] Base in %0 : $Base to Base, bb{{.*}}, bb[[CHECK2:[0-9]+]]
// CHECK: bb[[CHECK2]]{{.*}}:
// CHECK:  checked_cast_br [exact] Base in %0 : $Base to Sub, bb{{.*}}, bb[[GENCALL:[0-9]+]]
// CHECK: bb[[GENCALL]]{{.*}}:
// CHECK:  apply [[METH]]

struct MyValue {}

private class Generic<T> : MyNSObject {
  override init()
}

private class Base2 : Generic<MyValue> {
   override init()
  @inline(never) func foo()
}

private class Sub2 : Base2 {
  override init()
  @inline(never) override func foo()
}

sil private [noinline] @_TBase2FooFun : $@convention(method) (@guaranteed Base2) -> () {
bb0(%0 : $Base2):
  %1 = tuple()
  return %1 : $()
}

sil private [noinline] @_TSub2FooFun : $@convention(method) (@guaranteed Sub2) -> () {
bb0(%0 : $Sub2):
  %1 = tuple()
  return %1 : $()
}

sil_vtable Base2 {
  #Base2.foo: @_TBase2FooFun
}

sil_vtable Sub2 {
  #Base2.foo: @_TSub2FooFun [override]
}

sil @test_objc_ancestry2 : $@convention(thin) (@guaranteed Base2) -> () {
bb0(%0: $Base2):
  %1 = class_method %0 : $Base2, #Base2.foo : (Base2) -> () -> (), $@convention(method) (@guaranteed Base2) -> ()
  %2 = apply %1(%0) : $@convention(method) (@guaranteed Base2) -> ()
  %3 = tuple()
  return %3 : $()
}

// CHECK-LABEL: sil @test_objc_ancestry2
// CHECK: bb0
// CHECK:  [[METH:%.*]] = class_method %0 : $Base2, #Base2.foo : (Base2) -> () -> (), $@convention(method) (@guaranteed Base2) -> ()
// CHECK:  checked_cast_br [exact] Base2 in %0 : $Base2 to Base2, bb{{.*}}, bb[[CHECK2:[0-9]+]]
// CHECK: bb[[CHECK2]]{{.*}}:
// CHECK:  checked_cast_br [exact] Base2 in %0 : $Base2 to Sub2, bb{{.*}}, bb[[GENCALL:[0-9]+]]
// CHECK: bb[[GENCALL]]{{.*}}:
// CHECK:  apply [[METH]]

// Check that NoReturn functions are devirtualized properly.
// The new apply should be followed by an unreachable instruction
// instead of a branch instruction.
// CHECK-LABEL: sil{{.*}}test_devirt_of_noreturn_function
// CHECK: checked_cast_br
// CHECK: function_ref @Base_exit
// CHECK-NEXT: apply
// CHECK-NEXT: unreachable
// CHECK: checked_cast_br
// CHECK: function_ref @Sub_exit
// CHECK-NEXT: apply
// CHECK-NEXT: unreachable
sil hidden [noinline] @test_devirt_of_noreturn_function : $@convention(thin) (@owned Base) -> () {
bb0(%0 : $Base):
  %2 = class_method %0 : $Base, #Base.exit : (Base) -> () -> Never, $@convention(method) (@guaranteed Base) -> Never 
  %3 = apply %2(%0) : $@convention(method) (@guaranteed Base) -> Never
  unreachable
} 

class Throw {
  func mayThrow() throws
}

class S1 : Throw {
  override func mayThrow()
}

class S2 : Throw {
  override func mayThrow() throws
}

sil hidden [thunk] @$S4main4ThrowC8mayThrowyyKF : $@convention(method) (@guaranteed Throw) -> @error any Error {
bb0(%0 : $Throw):
  %1 = tuple ()
  return %1 : $()
}

sil hidden @$S4main2S1C8mayThrowyyF : $@convention(method) (@guaranteed S1) -> () {
bb0(%0 : $S1):
  %1 = tuple ()
  return %1 : $()
}

sil hidden [thunk] [always_inline] @$S4main2S2C8mayThrowyyKF : $@convention(method) (@guaranteed S2) -> @error any Error {
bb0(%0 : $S2):
  %1 = tuple ()
  return %1 : $()
}

// CHECK-LABEL: sil{{.*}} @test_devirt_of_throw_without_error
// CHECK-NOT: checked_cast_br [exact] Throw in %0 : $Throw to S1
// CHECK: return

sil hidden [noinline] @test_devirt_of_throw_without_error : $@convention(thin) (@owned Throw) -> () {
bb0(%0 : $Throw):
  %80 = class_method %0 : $Throw, #Throw.mayThrow : (Throw) -> () throws -> (), $@convention(method) (@guaranteed Throw) -> @error any Error // user: %81
  try_apply %80(%0) : $@convention(method) (@guaranteed Throw) -> @error any Error, normal bb7, error bb6

bb6(%82 : $Error):
  br bb1

bb7(%84 : $()):
  br bb1

bb1:
  %3 = tuple ()
  return %3 : $()
}

sil_vtable Throw {
  #Throw.mayThrow: (Throw) -> () throws -> () : @$S4main4ThrowC8mayThrowyyKF
}

sil_vtable S1 {
  #Throw.mayThrow: (Throw) -> () throws -> () : @$S4main2S1C8mayThrowyyF [override]
}

sil_vtable S2 {
  #Throw.mayThrow: (Throw) -> () throws -> () : @$S4main2S2C8mayThrowyyKF [override]
}