Files
swift-mirror/test/SILOptimizer/stack_promotion_isolated_deinit.swift
Erik Eckstein bb4045703a DeadAccessScopeElimination: remove dynamic accesses on locally allocated class instances
Remove dynamic access scopes when the accessed address is derived from a locally allocated class instance (an `alloc_ref` in the same function):

```
  %obj  = alloc_ref $MyClass
  %addr = ref_element_addr %obj : $MyClass, #MyClass.x
  %2    = begin_access [modify] [dynamic] %addr
  store %val to %2        // %2 has a use, so the scope is not "dead"
  end_access %2
```

Because the class instance is allocated locally, no caller can hold a reference to it, and therefore no conflicting access can originate from any caller.
Combined with the ordinary intra-function conflict check, this means the exclusivity check is provably unnecessary and the scope can be eliminated by replacing `begin_access` with its address operand.
2026-07-01 14:34:16 +02:00

77 lines
2.3 KiB
Swift

// RUN: %target-swift-frontend -parse-as-library -target %target-future-triple -O -module-name=test %s -Xllvm -sil-print-types -emit-sil | %FileCheck %s
@globalActor actor AnotherActor: GlobalActor {
static let shared = AnotherActor()
}
final class Inner {}
final class IsolatedDeinit {
var inner: Inner?
@AnotherActor deinit {}
}
final class Container {
var ref: IsolatedDeinit?
// To avoid dead-object elimination of Container
@inline(never)
deinit {
print("deinit")
}
}
// CHECK-LABEL: sil [noinline] {{.*}}@$s4test0A16ContainerOutsideyyF : $@convention(thin) () -> () {
// CHECK: [[C:%.*]] = alloc_ref [stack] $Container
// CHECK: [[ID:%.*]] = alloc_ref $IsolatedDeinit
// CHECK: dealloc_stack_ref [[C]] : $Container
// CHECK: return
@inline(never)
public func testContainerOutside() {
// container can be promoted
let container = Container()
let obj = IsolatedDeinit()
container.ref = obj
}
// CHECK-LABEL: sil [noinline] @$s4test0A15ContainerInsideyyF : $@convention(thin) () -> () {
// CHECK: [[D:%.*]] = alloc_ref $IsolatedDeinit
// CHECK: [[C:%.*]] = alloc_ref [stack] $Container
// CHECK: dealloc_stack_ref [[C]] : $Container
// CHECK: return
@inline(never)
public func testContainerInside() {
let obj = IsolatedDeinit()
// container can be promoted
let container = Container()
container.ref = obj
}
// CHECK-LABEL: sil [noinline] @$s4test0A12InnerOutsideyyF : $@convention(thin) () -> () {
// CHECK: [[I:%.*]] = alloc_ref $Inner
// CHECK: [[D:%.*]] = alloc_ref $IsolatedDeinit
// CHECK: [[DI:%.*]] = end_init_let_ref [[D]] : $IsolatedDeinit
// CHECK: strong_release [[DI]] : $IsolatedDeinit
// CHECK: return
@inline(never)
public func testInnerOutside() {
// inner cannot be promoted, because it escapes to isolated deinit
let inner = Inner()
let obj = IsolatedDeinit()
obj.inner = inner
}
// CHECK-LABEL: sil [noinline] @$s4test0A11InnerInsideyyF : $@convention(thin) () -> () {
// CHECK: [[D:%.*]] = alloc_ref $IsolatedDeinit
// CHECK: [[DI:%.*]] = end_init_let_ref [[D]] : $IsolatedDeinit
// CHECK: [[I:%.*]] = alloc_ref $Inner
// CHECK: strong_release [[DI]] : $IsolatedDeinit
// CHECK: return
@inline(never)
public func testInnerInside() {
let obj = IsolatedDeinit()
// inner cannot be promoted, because it escapes to isolated deinit
let inner = Inner()
obj.inner = inner
}