Skip to content

SIL: handle drop_deinit in WalkUtils #82014

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jun 6, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 13 additions & 8 deletions SwiftCompilerSources/Sources/SIL/Utilities/WalkUtils.swift
Original file line number Diff line number Diff line change
Expand Up @@ -537,8 +537,12 @@ extension AddressDefUseWalker {
return walkDownUses(ofAddress: ia, path: subPath.push(.anyIndexedElement, index: 0))
}
return walkDownUses(ofAddress: ia, path: path)
case let mmc as MarkUnresolvedNonCopyableValueInst:
return walkDownUses(ofAddress: mmc, path: path)
case is MarkUninitializedInst,
is MoveOnlyWrapperToCopyableAddrInst,
is CopyableToMoveOnlyWrapperAddrInst,
is MarkUnresolvedNonCopyableValueInst,
is DropDeinitInst:
return walkDownUses(ofAddress: instruction as! SingleValueInstruction, path: path)
case let ba as BeginAccessInst:
// Don't treat `end_access` as leaf-use. Just ignore it.
return walkDownNonEndAccessUses(of: ba, path: path)
Expand Down Expand Up @@ -828,18 +832,19 @@ extension AddressUseDefWalker {
return walkUp(address: uteda.operand.value, path: path.push(.enumCase, index: uteda.caseIndex))
case is InitExistentialAddrInst, is OpenExistentialAddrInst:
return walkUp(address: (def as! Instruction).operands[0].value, path: path.push(.existential, index: 0))
case is BeginAccessInst, is MarkUnresolvedNonCopyableValueInst:
return walkUp(address: (def as! Instruction).operands[0].value, path: path)
case let ia as IndexAddrInst:
if let idx = ia.constantIndex {
return walkUp(address: ia.base, path: path.push(.indexedElement, index: idx))
} else {
return walkUp(address: ia.base, path: path.push(.anyIndexedElement, index: 0))
}
case is MarkDependenceInst, is MarkUninitializedInst:
return walkUp(address: (def as! Instruction).operands[0].value, path: path)
case is MoveOnlyWrapperToCopyableAddrInst,
is CopyableToMoveOnlyWrapperAddrInst:
case is BeginAccessInst,
is MarkDependenceInst,
is MarkUninitializedInst,
is MoveOnlyWrapperToCopyableAddrInst,
is CopyableToMoveOnlyWrapperAddrInst,
is MarkUnresolvedNonCopyableValueInst,
is DropDeinitInst:
return walkUp(address: (def as! Instruction).operands[0].value, path: path)
default:
return rootDef(address: def, path: path)
Expand Down
30 changes: 29 additions & 1 deletion test/SILOptimizer/addr_escape_info.sil
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,12 @@ class X {
@_hasStorage var s: Str
}

struct NonCopyable: ~Copyable {
var a: Int

deinit
}

class D: X {}

struct Container {
Expand Down Expand Up @@ -52,6 +58,7 @@ struct NE : ~Escapable {}

sil @no_arguments : $@convention(thin) () -> ()
sil @indirect_argument : $@convention(thin) (@in Int) -> ()
sil @consume_nc : $@convention(thin) (@in NonCopyable) -> ()
sil @indirect_struct_argument : $@convention(thin) (@in Str) -> ()
sil @direct_argument : $@convention(thin) (Int) -> ()
sil @class_argument : $@convention(thin) (@guaranteed X) -> ()
Expand Down Expand Up @@ -870,7 +877,7 @@ bb2(%17 : $Error):
}

// CHECK-LABEL: Address escape information for test_store_borrow:
// CHECK: value: %2 = store_borrow %0 to %1 : $*X // users: %4, %3
// CHECK: value: %2 = store_borrow %0 to %1 : $*X
// CHECK-NEXT: End function test_store_borrow
sil [ossa] @test_store_borrow : $@convention(thin) (@guaranteed X) -> () {
bb0(%0 : @guaranteed $X):
Expand All @@ -883,6 +890,26 @@ bb0(%0 : @guaranteed $X):
return %3 : $()
}

// CHECK-LABEL: Address escape information for test_drop_deinit:
// CHECK: value: %1 = alloc_stack $NonCopyable
// CHECK-NEXT: - %6 = apply %5() : $@convention(thin) () -> ()
// CHECK-NEXT: ==> %8 = apply %7(%3) : $@convention(thin) (@in NonCopyable) -> ()
// CHECK-NEXT: End function test_drop_deinit
sil [ossa] @test_drop_deinit : $@convention(thin) (@owned NonCopyable) -> () {
bb0(%0 : @owned $NonCopyable):
%1 = alloc_stack $NonCopyable
store %0 to [init] %1
%3 = drop_deinit %1
fix_lifetime %1
%5 = function_ref @no_arguments : $@convention(thin) () -> ()
%6 = apply %5() : $@convention(thin) () -> ()
%7 = function_ref @consume_nc : $@convention(thin) (@in NonCopyable) -> ()
%8 = apply %7(%3) : $@convention(thin) (@in NonCopyable) -> ()
dealloc_stack %1
%10 = tuple ()
return %10 : $()
}

// CHECK-LABEL: Address escape information for noescape_via_independent_addressable:
// CHECK: pair 0 - 1
// CHECK-NEXT: apply %{{.*}}(%{{.*}}) : $@convention(thin) (@in_guaranteed Addressable) -> Builtin.RawPointer
Expand Down Expand Up @@ -956,3 +983,4 @@ bb0(%0 : $Int):
%9 = tuple ()
return %9 : $()
}

9 changes: 4 additions & 5 deletions test/SILOptimizer/lifetime_dependence/scope_fixup.sil
Original file line number Diff line number Diff line change
Expand Up @@ -404,15 +404,14 @@ bb0:
// Test that initializing LifetimeDependence.Scope from an unidentified enclosing access scope does not
// unwrap nil.
//
// TODO: the mark_dependence should redirect to the drop_deinit, not the struct_element_addr, but AccessBase does not
// currently preserve the address of an unidentified base.
//
// CHECK-LABEL: sil hidden [ossa] @testUnidentified : $@convention(method) (@owned B) -> () {
// CHECK: [[DD:%.*]] = drop_deinit
// CHECK: [[AS:%.*]] = alloc_stack $B
// CHECK: [[MU:%.*]] = mark_unresolved_non_copyable_value [consumable_and_assignable] [[AS]]
// CHECK: [[DD:%.*]] = drop_deinit [[MU]]
// CHECK: [[SE:%.*]] = struct_element_addr [[DD]], #B.ne
// CHECK: [[LB:%.*]] = load_borrow
// CHECK: apply %{{.*}} : $@convention(thin) (@guaranteed NE) -> UnsafePointer<A>
// CHECK: mark_dependence [unresolved] %{{.*}} on [[SE]]
// CHECK: mark_dependence [unresolved] %{{.*}} on [[AS]]
// CHECK-LABEL: } // end sil function 'testUnidentified'
sil hidden [ossa] @testUnidentified : $@convention(method) (@owned B) -> () {
bb0(%0 : @owned $B):
Expand Down
22 changes: 21 additions & 1 deletion test/SILOptimizer/optimal_arc.swift
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// RUN: %target-swift-frontend -module-name test -O -emit-sil -O -primary-file %s | %FileCheck %s
// RUN: %target-swift-frontend -module-name test -O -emit-sil -primary-file %s | %FileCheck %s

// REQUIRES: optimized_stdlib,swift_stdlib_no_asserts

Expand Down Expand Up @@ -50,5 +50,25 @@ struct TestCollection: RandomAccessCollection, RangeReplaceableCollection {
}
}

class C {
func foo() {}
}

struct S: ~Copyable {
var c: C

// Check that there is only a single release in the deinit.

// CHECK-LABEL: sil hidden @$s4test1SVfD :
// CHECK-NOT: retain
// CHECK-NOT: release
// CHECK: apply
// CHECK: release
// CHECK-NOT: release
// CHECK: } // end sil function '$s4test1SVfD'
deinit {
c.foo()
}
}