Skip to content

ComputeSideEffects: correct side effects for destroy_addr #62864

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 2 commits into from
Jan 5, 2023
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
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,11 @@ private struct CollectedEffects {
addDestroyEffects(of: inst.operands[0].value)

case let da as DestroyAddrInst:
// A destroy_addr also involves a read from the address. It's equivalent to a `%x = load [take]` and `destroy_value %x`.
addEffects(.read, to: da.operand)
// Conceptually, it's also a write, because the stored value is not available anymore after the destroy
addEffects(.write, to: da.operand)

addDestroyEffects(of: da.operand)

case let copy as CopyAddrInst:
Expand All @@ -108,12 +113,16 @@ private struct CollectedEffects {
addEffects(.copy, to: copy.source)
}
if !copy.isInitializationOfDest {
// Like for destroy_addr, the destroy also involves a read.
addEffects(.read, to: copy.destination)
addDestroyEffects(of: copy.destination)
}

case let store as StoreInst:
addEffects(.write, to: store.destination)
if store.destinationOwnership == .assign {
// Like for destroy_addr, the destroy also involves a read.
addEffects(.read, to: store.destination)
addDestroyEffects(of: store.destination)
}

Expand Down Expand Up @@ -433,7 +442,8 @@ private struct ArgumentEscapingWalker : ValueDefUseWalker, AddressDefUseWalker {
return .continueWalk

// Warning: all instruction listed here, must also be handled in `CollectedEffects.addInstructionEffects`
case is StoreInst, is StoreWeakInst, is StoreUnownedInst, is ApplySite, is DestroyAddrInst:
case is StoreInst, is StoreWeakInst, is StoreUnownedInst, is ApplySite, is DestroyAddrInst,
is DebugValueInst:
return .continueWalk

default:
Expand Down
4 changes: 2 additions & 2 deletions test/SILOptimizer/assemblyvision_remark/chacha.swift
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ public func run_ChaCha(_ N: Int) {

var checkedtext = Array(repeating: UInt8(0), count: 1024)
ChaCha20.encrypt(bytes: &checkedtext, key: key, nonce: nonce)
checkResult(checkedtext)// expected-note @-2 {{of 'checkedtext}}
checkResult(checkedtext)


var plaintext = Array(repeating: UInt8(0), count: 30720)
Expand All @@ -43,7 +43,7 @@ public func run_ChaCha(_ N: Int) {
// expected-remark @-1:27 {{release of type '}}
}
} // expected-remark {{release of type '}}
// expected-note @-7 {{of 'plaintext}}

// expected-remark @-2 {{release of type '}}
// expected-note @-16 {{of 'nonce}}
// expected-remark @-4 {{release of type '}}
Expand Down
20 changes: 16 additions & 4 deletions test/SILOptimizer/side_effects.sil
Original file line number Diff line number Diff line change
Expand Up @@ -436,7 +436,7 @@ bb0(%0 : @owned $SP):
}

// CHECK-LABEL: sil [ossa] @store_destoys
// CHECK-NEXT: [%0: write v**, destroy v**]
// CHECK-NEXT: [%0: read v**, write v**, destroy v**]
// CHECK-NEXT: [%1: write c*.v**, copy c*.v**]
// CHECK-NEXT: [global: write,copy]
// CHECK-NEXT: {{^[^[]}}
Expand All @@ -448,7 +448,7 @@ bb0(%0 : $*X, %1 : @owned $X):
}

// CHECK-LABEL: sil [ossa] @unknown_destructor_effects
// CHECK-NEXT: [%0: write v**, destroy v**]
// CHECK-NEXT: [%0: read v**, write v**, destroy v**]
// CHECK-NEXT: [%1: read c*.v**, write c*.v**, copy c*.v**, destroy c*.v**]
// CHECK-NEXT: [global: read,write,copy,destroy,allocate,deinit_barrier]
// CHECK-NEXT: {{^[^[]}}
Expand All @@ -471,7 +471,7 @@ bb0(%0 : $*X, %1 : @owned $X):
}

// CHECK-LABEL: sil [ossa] @copy_destoys
// CHECK-NEXT: [%0: write v**, destroy v**]
// CHECK-NEXT: [%0: read v**, write v**, destroy v**]
// CHECK-NEXT: [%1: read v**, copy v**]
// CHECK-NEXT: [global: write,copy]
// CHECK-NEXT: {{^[^[]}}
Expand Down Expand Up @@ -518,7 +518,7 @@ bb0(%0 : @owned $SP):
}

// CHECK-LABEL: sil [ossa] @destroy_addr_effects
// CHECK-NEXT: [%0: destroy v**]
// CHECK-NEXT: [%0: read v**, write v**, destroy v**]
// CHECK-NEXT: [global: write,copy]
// CHECK-NEXT: {{^[^[]}}
sil [ossa] @destroy_addr_effects : $@convention(thin) (@in X) -> () {
Expand Down Expand Up @@ -1144,3 +1144,15 @@ bb3:
return %r : $()
}

// CHECK-LABEL: sil @test_debug_value_address
// CHECK-NEXT: [%0: read v**, write v**, destroy v**]
// CHECK-NEXT: [global: read,write,copy,destroy,allocate,deinit_barrier]
// CHECK-NEXT: {{^[^[]}}
sil @test_debug_value_address : $@convention(thin) <T> (@in T, @inout T) -> () {
bb0(%0 : $*T, %1 : $*T):
destroy_addr %0 : $*T
debug_value %1 : $*T
%4 = tuple ()
return %4 : $()
}