Skip to content

Commit 6b33768

Browse files
authored
Merge pull request #62864 from eeckstein/fix-compute-side-effects
ComputeSideEffects: correct side effects for destroy_addr
2 parents 5683a61 + 1f0f9d6 commit 6b33768

File tree

3 files changed

+29
-7
lines changed

3 files changed

+29
-7
lines changed

SwiftCompilerSources/Sources/Optimizer/FunctionPasses/ComputeSideEffects.swift

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,11 @@ private struct CollectedEffects {
9898
addDestroyEffects(of: inst.operands[0].value)
9999

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

103108
case let copy as CopyAddrInst:
@@ -108,12 +113,16 @@ private struct CollectedEffects {
108113
addEffects(.copy, to: copy.source)
109114
}
110115
if !copy.isInitializationOfDest {
116+
// Like for destroy_addr, the destroy also involves a read.
117+
addEffects(.read, to: copy.destination)
111118
addDestroyEffects(of: copy.destination)
112119
}
113120

114121
case let store as StoreInst:
115122
addEffects(.write, to: store.destination)
116123
if store.destinationOwnership == .assign {
124+
// Like for destroy_addr, the destroy also involves a read.
125+
addEffects(.read, to: store.destination)
117126
addDestroyEffects(of: store.destination)
118127
}
119128

@@ -433,7 +442,8 @@ private struct ArgumentEscapingWalker : ValueDefUseWalker, AddressDefUseWalker {
433442
return .continueWalk
434443

435444
// Warning: all instruction listed here, must also be handled in `CollectedEffects.addInstructionEffects`
436-
case is StoreInst, is StoreWeakInst, is StoreUnownedInst, is ApplySite, is DestroyAddrInst:
445+
case is StoreInst, is StoreWeakInst, is StoreUnownedInst, is ApplySite, is DestroyAddrInst,
446+
is DebugValueInst:
437447
return .continueWalk
438448

439449
default:

test/SILOptimizer/assemblyvision_remark/chacha.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ public func run_ChaCha(_ N: Int) {
3333

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

3838

3939
var plaintext = Array(repeating: UInt8(0), count: 30720)
@@ -43,7 +43,7 @@ public func run_ChaCha(_ N: Int) {
4343
// expected-remark @-1:27 {{release of type '}}
4444
}
4545
} // expected-remark {{release of type '}}
46-
// expected-note @-7 {{of 'plaintext}}
46+
4747
// expected-remark @-2 {{release of type '}}
4848
// expected-note @-16 {{of 'nonce}}
4949
// expected-remark @-4 {{release of type '}}

test/SILOptimizer/side_effects.sil

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -436,7 +436,7 @@ bb0(%0 : @owned $SP):
436436
}
437437

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

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

473473
// CHECK-LABEL: sil [ossa] @copy_destoys
474-
// CHECK-NEXT: [%0: write v**, destroy v**]
474+
// CHECK-NEXT: [%0: read v**, write v**, destroy v**]
475475
// CHECK-NEXT: [%1: read v**, copy v**]
476476
// CHECK-NEXT: [global: write,copy]
477477
// CHECK-NEXT: {{^[^[]}}
@@ -518,7 +518,7 @@ bb0(%0 : @owned $SP):
518518
}
519519

520520
// CHECK-LABEL: sil [ossa] @destroy_addr_effects
521-
// CHECK-NEXT: [%0: destroy v**]
521+
// CHECK-NEXT: [%0: read v**, write v**, destroy v**]
522522
// CHECK-NEXT: [global: write,copy]
523523
// CHECK-NEXT: {{^[^[]}}
524524
sil [ossa] @destroy_addr_effects : $@convention(thin) (@in X) -> () {
@@ -1144,3 +1144,15 @@ bb3:
11441144
return %r : $()
11451145
}
11461146

1147+
// CHECK-LABEL: sil @test_debug_value_address
1148+
// CHECK-NEXT: [%0: read v**, write v**, destroy v**]
1149+
// CHECK-NEXT: [global: read,write,copy,destroy,allocate,deinit_barrier]
1150+
// CHECK-NEXT: {{^[^[]}}
1151+
sil @test_debug_value_address : $@convention(thin) <T> (@in T, @inout T) -> () {
1152+
bb0(%0 : $*T, %1 : $*T):
1153+
destroy_addr %0 : $*T
1154+
debug_value %1 : $*T
1155+
%4 = tuple ()
1156+
return %4 : $()
1157+
}
1158+

0 commit comments

Comments
 (0)