File tree Expand file tree Collapse file tree 3 files changed +40
-1
lines changed
SwiftCompilerSources/Sources/Optimizer/FunctionPasses Expand file tree Collapse file tree 3 files changed +40
-1
lines changed Original file line number Diff line number Diff line change @@ -57,6 +57,7 @@ let computeSideEffects = FunctionPass(name: "compute-side-effects") {
57
57
// instruction the argument might have escaped.
58
58
for argument in function. arguments {
59
59
collectedEffects. addEffectsForEscapingArgument ( argument: argument)
60
+ collectedEffects. addEffectsForConsumingArgument ( argument: argument)
60
61
}
61
62
62
63
// Don't modify the effects if they didn't change. This avoids sending a change notification
@@ -236,7 +237,17 @@ private struct CollectedEffects {
236
237
addEffects ( . destroy, to: argument)
237
238
}
238
239
}
239
-
240
+
241
+ mutating func addEffectsForConsumingArgument( argument: FunctionArgument ) {
242
+ if argument. convention == . indirectIn {
243
+ // Usually there _must_ be a read from a consuming in-argument, because the function has to consume the argument.
244
+ // But in the special case if all control paths end up in an `unreachable`, the consuming read might have been
245
+ // dead-code eliminated. Therefore make sure to add the read-effect in any case. Otherwise it can result
246
+ // in memory lifetime failures at a call site.
247
+ addEffects ( . read, to: argument)
248
+ }
249
+ }
250
+
240
251
private mutating func handleApply( _ apply: ApplySite ) {
241
252
let callees = calleeAnalysis. getCallees ( callee: apply. callee)
242
253
let args = apply. argumentOperands. lazy. map {
Original file line number Diff line number Diff line change
1
+ // RUN: %target-swift-frontend -O -Xllvm -sil-disable-pass=function-signature-opts -emit-sil -primary-file %s | %FileCheck %s
2
+
3
+ public struct S {
4
+ @inline ( never)
5
+ init < T> ( _ t: T ) {
6
+ fatalError ( )
7
+ }
8
+ }
9
+
10
+ // check that this doesn't cause a memory lifetime failure
11
+
12
+ // CHECK-LABEL: sil @$s22generic_specialization6testityAA1SVSSF :
13
+ // CHECK: [[F:%.*]] = function_ref @$s22generic_specialization1SVyACxclufCSS_Tt0g5 :
14
+ // CHECK: = apply [[F]](%0) : $@convention(thin) (@owned String) -> S
15
+ // CHECK: } // end sil function '$s22generic_specialization6testityAA1SVSSF'
16
+ public func testit( _ s: String ) -> S {
17
+ return S ( s)
18
+ }
19
+
Original file line number Diff line number Diff line change @@ -1234,3 +1234,12 @@ bb0(%0 : $SP):
1234
1234
%r = tuple ()
1235
1235
return %r : $()
1236
1236
}
1237
+
1238
+ // CHECK-LABEL: sil @test_consuming_in_with_unreachable
1239
+ // CHECK-NEXT: [%0: read v**]
1240
+ // CHECK-NEXT: [global: ]
1241
+ // CHECK-NEXT: {{^[^[]}}
1242
+ sil @test_consuming_in_with_unreachable : $@convention(thin) (@in X) -> () {
1243
+ bb0(%0 : $*X):
1244
+ unreachable
1245
+ }
You can’t perform that action at this time.
0 commit comments