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 319 320 321 322 323 324 325 326 327 328 329 330 331
|
// RUN: %target-sil-opt -enable-sil-verify-all -late-release-hoisting -module-name=test %s | %FileCheck %s
// REQUIRES: swift_in_compiler
import Builtin
import Swift
sil_stage canonical
//===----------------------------------------------------------------------===//
// Unit tests for reachability to and from local objects.
//===----------------------------------------------------------------------===//
class HasObj {
var o : AnyObject
}
class HasInt64 {
var i : Int64
}
class HasInt64Sub : HasObj {
var i : Int64
}
class HasHasInt64 : HasObj {
var hi : HasInt64
}
class HasHasObj {
var ho : HasObj
}
public final class C {}
struct FD: ~Copyable {
var c = C()
deinit {}
}
sil @$s4test8HasInt64CfD : $@convention(method) (@owned HasInt64) -> () {
[%0: noescape **]
}
// The release of %lo can be hoisted over the load because it does not
// point to any escaping references.
//
// CHECK-LABEL: sil @testLocalNotReachesEscaped : $@convention(thin) (Int64, @owned HasObj) -> AnyObject {
// CHECK: bb0(%0 : $Int64, %1 : $HasObj):
// CHECK: [[LO:%.*]] = alloc_ref $HasInt64
// CHECK: [[IADR:%.*]] = ref_element_addr [[LO]] : $HasInt64, #HasInt64.i
// CHECK: store %0 to [[IADR]] : $*Int64
// CHECK-NOT-SUPPORTED-YET: strong_release [[LO]] : $HasInt64
// CHECK: [[OADR:%.*]] = ref_element_addr %1 : $HasObj, #HasObj.o
// CHECK: [[O:%.*]] = load [[OADR]] : $*AnyObject
// CHECK: return [[O]] : $AnyObject
// CHECK-LABEL: } // end sil function 'testLocalNotReachesEscaped'
sil @testLocalNotReachesEscaped : $@convention(thin) (Int64, @owned HasObj) -> AnyObject {
bb0(%0 : $Int64, %1 : $HasObj):
%lo = alloc_ref $HasInt64
%iadr = ref_element_addr %lo : $HasInt64, #HasInt64.i
store %0 to %iadr : $*Int64
%oadr = ref_element_addr %1 : $HasObj, #HasObj.o
%o = load %oadr : $*AnyObject
strong_release %lo : $HasInt64
return %o : $AnyObject
}
// The release of %lo cannot be hoisted over the load because it the
// release of %lo causes all objects pointed to by its fields to
// escape. Since it has at least one reference-type field, it may
// point to escaping memory.
//
// CHECK-LABEL: sil @testLocalMayReachEscaped : $@convention(thin) (Int64, @owned HasObj) -> AnyObject {
// CHECK: bb0(%0 : $Int64, %1 : $HasObj):
// CHECK: [[LO:%.*]] = alloc_ref $HasInt64Sub
// CHECK: [[IADR:%.*]] = ref_element_addr [[LO]] : $HasInt64Sub, #HasInt64Sub.i
// CHECK: store %0 to [[IADR]] : $*Int64
// CHECK: [[OADR:%.*]] = ref_element_addr %1 : $HasObj, #HasObj.o
// CHECK: [[O:%.*]] = load [[OADR]] : $*AnyObject
// CHECK: strong_release [[LO]] : $HasInt64Sub
// CHECK: return [[O]] : $AnyObject
// CHECK-LABEL: } // end sil function 'testLocalMayReachEscaped'
sil @testLocalMayReachEscaped : $@convention(thin) (Int64, @owned HasObj) -> AnyObject {
bb0(%0 : $Int64, %1 : $HasObj):
%lo = alloc_ref $HasInt64Sub
%iadr = ref_element_addr %lo : $HasInt64Sub, #HasInt64Sub.i
store %0 to %iadr : $*Int64
%oadr = ref_element_addr %1 : $HasObj, #HasObj.o
%o = load %oadr : $*AnyObject
strong_release %lo : $HasInt64Sub
return %o : $AnyObject
}
// The release of %lo cannot be hoisted over the load.
//
// CHECK-LABEL: sil @testLocalReachesEscaped : $@convention(thin) (@owned AnyObject) -> AnyObject {
// CHECK: bb0(%0 : $AnyObject):
// CHECK: [[LO:%.*]] = alloc_ref $HasObj
// CHECK: [[IADR:%.*]] = ref_element_addr [[LO]] : $HasObj, #HasObj.o
// CHECK: store %0 to [[IADR]] : $*AnyObject
// CHECK: [[OADR:%.*]] = ref_element_addr [[LO]] : $HasObj, #HasObj.o
// CHECK: [[O:%.*]] = load [[OADR]] : $*AnyObject
// CHECK: strong_release [[LO]] : $HasObj
// CHECK: return [[O]] : $AnyObject
// CHECK-LABEL: } // end sil function 'testLocalReachesEscaped'
sil @testLocalReachesEscaped : $@convention(thin) (@owned AnyObject) -> AnyObject {
bb0(%0 : $AnyObject):
%lo = alloc_ref $HasObj
%iadr = ref_element_addr %lo : $HasObj, #HasObj.o
store %0 to %iadr : $*AnyObject
%oadr = ref_element_addr %lo : $HasObj, #HasObj.o
%o = load %oadr : $*AnyObject
strong_release %lo : $HasObj
return %o : $AnyObject
}
// The release of %lo cannot be hoisted above the load from %oadr
// because there is a points-to relation between %lo and %oadr.
//
// TODO: To hoist this release, AliasAnalysis also need to prove that
// it isn't being hoisted above the last use. This information is not
// properly communicated between AliasAnalysis and EscapeAnalysis.
//
// CHECK-LABEL: sil @testLocalReachesRCEscaped : $@convention(thin) (@owned HasObj) -> AnyObject {
// CHECK: bb0(%0 : $HasObj):
// CHECK: [[LO:%.*]] = alloc_ref $HasHasObj
// CHECK: [[HOADR:%.*]] = ref_element_addr [[LO]] : $HasHasObj, #HasHasObj.ho
// CHECK: strong_retain %0 : $HasObj
// CHECK: store %0 to [[HOADR]] : $*HasObj
// CHECK: [[HO:%.*]] = load [[HOADR]] : $*HasObj
// CHECK: [[OADR:%.*]] = ref_element_addr [[HO]] : $HasObj, #HasObj.o
// CHECK: [[O:%.*]] = load [[OADR]] : $*AnyObject
// CHECK: strong_release [[LO]] : $HasHasObj
// CHECK: return [[O]] : $AnyObject
// CHECK-LABEL: } // end sil function 'testLocalReachesRCEscaped'
sil @testLocalReachesRCEscaped : $@convention(thin) (@owned HasObj) -> AnyObject {
bb0(%0 : $HasObj):
%lo = alloc_ref $HasHasObj
%hoadr = ref_element_addr %lo : $HasHasObj, #HasHasObj.ho
strong_retain %0 : $HasObj
store %0 to %hoadr : $*HasObj
%ho = load %hoadr : $*HasObj
%oadr = ref_element_addr %ho : $HasObj, #HasObj.o
%o = load %oadr : $*AnyObject
// Normally a retain of %o would precede this release of %c. But
// let's assume some aggressive optimization happened.
strong_release %lo : $HasHasObj
return %o : $AnyObject
}
// Two local references, one reachable from the other.
//
// TODO: We do not currently hoist strong_release %hho above
// strong_retain %o because %hho is marked as escaping. However, it is
// only stored to another local object, so in theory it does not need
// to be marked escaping.
//
// CHECK-LABEL: sil @testLocalReachesEscapingLocal : $@convention(thin) (AnyObject) -> AnyObject {
// CHECK: bb0(%0 : $AnyObject):
// CHECK: [[LO:%.*]] = alloc_ref $HasObj
// CHECK: [[OADR:%.*]] = ref_element_addr %1 : $HasObj, #HasObj.o
// CHECK: strong_retain %0 : $AnyObject
// CHECK: store %0 to [[OADR]] : $*AnyObject
// CHECK: [[HHO:%.*]] = alloc_ref $HasHasObj
// CHECK: [[HOADR:%.*]] = ref_element_addr [[HHO]] : $HasHasObj, #HasHasObj.ho
// CHECK: store [[LO]] to [[HOADR]] : $*HasObj
// CHECK: [[HO:%.*]] = load [[HOADR]] : $*HasObj
// CHECK: [[OADR2:%.*]] = ref_element_addr [[HO]] : $HasObj, #HasObj.o
// CHECK: [[O:%.*]] = load [[OADR2]] : $*AnyObject
// CHECK: strong_retain [[O]] : $AnyObject
// CHECK: strong_release [[HHO]] : $HasHasObj
// CHECK: return [[O]] : $AnyObject
// CHECK-LABEL: } // end sil function 'testLocalReachesEscapingLocal'
sil @testLocalReachesEscapingLocal : $@convention(thin) (AnyObject) -> AnyObject {
bb0(%0 : $AnyObject):
%ho = alloc_ref $HasObj
%oadr = ref_element_addr %ho : $HasObj, #HasObj.o
strong_retain %0 : $AnyObject
store %0 to %oadr : $*AnyObject
%hho = alloc_ref $HasHasObj
%hoadr = ref_element_addr %hho : $HasHasObj, #HasHasObj.ho
store %ho to %hoadr : $*HasObj
%ho2 = load %hoadr : $*HasObj
%oadr2 = ref_element_addr %ho2 : $HasObj, #HasObj.o
%o = load %oadr2 : $*AnyObject
strong_retain %o : $AnyObject
strong_release %hho : $HasHasObj
return %o : $AnyObject
}
// Two local references, one reachable from the other. We cannot assume that
// the reachable one has its own reference count and hoist
// strong_release %hho above load %oadr without more information.
//
// TODO: To hoist this release, AliasAnalysis also need to prove that
// it isn't being hoisted above the last use. This information is not
// properly communicated between AliasAnalysis and EscapeAnalysis.
//
// CHECK-LABEL: sil @testLocalReachesRCLocal : $@convention(thin) (AnyObject) -> AnyObject {
// CHECK: bb0(%0 : $AnyObject):
// CHECK: [[LO:%.*]] = alloc_ref $HasObj
// CHECK: [[OADR:%.*]] = ref_element_addr %1 : $HasObj, #HasObj.o
// CHECK: strong_retain %0 : $AnyObject
// CHECK: store %0 to [[OADR]] : $*AnyObject
// CHECK: [[HHO:%.*]] = alloc_ref $HasHasObj
// CHECK: [[HOADR:%.*]] = ref_element_addr [[HHO]] : $HasHasObj, #HasHasObj.ho
// CHECK: store [[LO]] to [[HOADR]] : $*HasObj
// CHECK: [[HO:%.*]] = load [[HOADR]] : $*HasObj
// CHECK: [[OADR2:%.*]] = ref_element_addr [[HO]] : $HasObj, #HasObj.o
// CHECK: [[O:%.*]] = load [[OADR2]] : $*AnyObject
// CHECK: strong_release [[HHO]] : $HasHasObj
// CHECK: strong_retain [[O]] : $AnyObject
// CHECK: return [[O]] : $AnyObject
// CHECK-LABEL: } // end sil function 'testLocalReachesRCLocal'
sil @testLocalReachesRCLocal : $@convention(thin) (AnyObject) -> AnyObject {
bb0(%0 : $AnyObject):
%ho = alloc_ref $HasObj
%oadr = ref_element_addr %ho : $HasObj, #HasObj.o
strong_retain %0 : $AnyObject
store %0 to %oadr : $*AnyObject
%hho = alloc_ref $HasHasObj
%hoadr = ref_element_addr %hho : $HasHasObj, #HasHasObj.ho
store %ho to %hoadr : $*HasObj
%ho2 = load %hoadr : $*HasObj
%oadr2 = ref_element_addr %ho2 : $HasObj, #HasObj.o
%o = load %oadr2 : $*AnyObject
strong_release %hho : $HasHasObj
strong_retain %o : $AnyObject
return %o : $AnyObject
}
// Hoist the release of an escaping object above memory operations on
// a local object that never escapes.
//
// CHECK-LABEL: sil @testEscapeNotReachesLocal : $@convention(thin) (Int64, @owned HasObj) -> Int64 {
// CHECK: bb0(%0 : $Int64, %1 : $HasObj):
// CHECK: strong_release %1 : $HasObj
// CHECK: [[LO:%.*]] = alloc_ref $HasInt64
// CHECK: [[IADR:%.*]] = ref_element_addr [[LO]] : $HasInt64, #HasInt64.i
// CHECK: store %0 to [[IADR]] : $*Int64
// CHECK: [[I:%.*]] = load [[IADR]] : $*Int64
// CHECK: return [[I]] : $Int64
// CHECK-LABEL: } // end sil function 'testEscapeNotReachesLocal'
sil @testEscapeNotReachesLocal : $@convention(thin) (Int64, @owned HasObj) -> Int64 {
bb0(%0 : $Int64, %1 : $HasObj):
%lo = alloc_ref $HasInt64
%iadr = ref_element_addr %lo : $HasInt64, #HasInt64.i
store %0 to %iadr : $*Int64
%i = load %iadr : $*Int64
strong_release %1 : $HasObj
return %i : $Int64
}
// EscapeAnalysis must consider the local object %lo as globally
// escaping because it is stored into an object that escapes via an
// incoming argument. This creates an aliasing relationship preventing
// the strong_release from being hoisted.
//
// CHECK-LABEL: sil @testEscapeReachesLocal : $@convention(thin) (@owned AnyObject, @owned HasHasObj) -> AnyObject {
// CHECK: bb0(%0 : $AnyObject, %1 : $HasHasObj):
// CHECK: [[LO:%.*]] = alloc_ref $HasObj
// CHECK: [[OADR:%.*]] = ref_element_addr [[LO]] : $HasObj, #HasObj.o
// CHECK: store %0 to [[OADR]] : $*AnyObject
// CHECK: [[HOADR:%.*]] = ref_element_addr %1 : $HasHasObj, #HasHasObj.ho
// CHECK: store [[LO]] to [[HOADR]] : $*HasObj
// CHECK: [[O:%.*]] = load [[OADR]] : $*AnyObject
// CHECK: strong_release %1 : $HasHasObj
// CHECK: return [[O]] : $AnyObject
// CHECK-LABEL: } // end sil function 'testEscapeReachesLocal'
sil @testEscapeReachesLocal : $@convention(thin) (@owned AnyObject, @owned HasHasObj) -> AnyObject {
bb0(%0 : $AnyObject, %1 : $HasHasObj):
%lo = alloc_ref $HasObj
%oadr = ref_element_addr %lo : $HasObj, #HasObj.o
store %0 to %oadr : $*AnyObject
%hoadr = ref_element_addr %1 : $HasHasObj, #HasHasObj.ho
store %lo to %hoadr : $*HasObj
%o = load %oadr : $*AnyObject
strong_release %1 : $HasHasObj
return %o : $AnyObject
}
// 'strong_release %lo : $HasHasInt64' cannot be hoisted because it
// contains a reference that appears to escape. With the current
// information, we can't be sure if that escaping reference points to
// the load of '%oadr'.
//
// TODO: To hoist this release, AliasAnalysis also need to prove that
// it isn't being hoisted above the last use. This information is not
// properly communicated between AliasAnalysis and EscapeAnalysis.
//
// CHECK-LABEL: sil @testLocalWithReferenceProperty : $@convention(thin) (Int64, @owned HasObj) -> AnyObject {
// CHECK: bb0(%0 : $Int64, %1 : $HasObj):
// CHECK: [[O:%.*]] = load {{.*}} : $*AnyObject
// CHECK: strong_release %{{.*}} : $HasHasInt64
// CHECK: return [[O]] : $AnyObject
// CHECK-LABEL: } // end sil function 'testLocalWithReferenceProperty'
sil @testLocalWithReferenceProperty : $@convention(thin) (Int64, @owned HasObj) -> AnyObject {
bb0(%0 : $Int64, %1 : $HasObj):
%hi = alloc_ref $HasInt64
%iadr = ref_element_addr %hi : $HasInt64, #HasInt64.i
store %0 to %iadr : $*Int64
%lo = alloc_ref $HasHasInt64
%hiadr = ref_element_addr %lo : $HasHasInt64, #HasHasInt64.hi
store %hi to %hiadr : $*HasInt64
%oadr = ref_element_addr %1 : $HasObj, #HasObj.o
%o = load %oadr : $*AnyObject
strong_release %lo : $HasHasInt64
return %o : $AnyObject
}
// Test that a struct-with-deinit is not RC-identical to its single member.
//
// CHECK-LABEL: sil hidden @fdDeinit : $@convention(method) (@owned FD) -> () {
// CHECK: strong_release %{{.*}} : $C
// CHECK-LABEL: } // end sil function 'fdDeinit'
sil hidden @fdDeinit : $@convention(method) (@owned FD) -> () {
bb0(%0 : $FD):
%2 = struct_extract %0 : $FD, #FD.c
strong_release %2 : $C
%4 = tuple ()
return %4 : $()
}
|