Skip to content

Commit 228e2f7

Browse files
authored
Merge pull request #85147 from atrick/addressable-param
Fix ApplySite.isAddressable to handle inout arguments.
2 parents d7a42a0 + f8a8471 commit 228e2f7

File tree

3 files changed

+52
-1
lines changed

3 files changed

+52
-1
lines changed

SwiftCompilerSources/Sources/SIL/ApplySite.swift

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -256,6 +256,14 @@ extension ApplySite {
256256
}
257257

258258
public func isAddressable(operand: Operand) -> Bool {
259+
for targetOperand in argumentOperands {
260+
guard !targetOperand.value.isEscapable else {
261+
continue
262+
}
263+
if let dep = parameterDependence(target: targetOperand, source: operand), dep.isAddressable(for: operand.value) {
264+
return true
265+
}
266+
}
259267
if let dep = resultDependence(on: operand) {
260268
return dep.isAddressable(for: operand.value)
261269
}
@@ -399,3 +407,15 @@ extension FullApplySite {
399407
return values
400408
}
401409
}
410+
411+
let addressableTest = Test("addressable_arguments") {
412+
function, arguments, context in
413+
414+
let operand = arguments.takeOperand()
415+
guard let apply = operand.instruction as? ApplySite, let argIdx = apply.calleeArgumentIndex(of: operand) else {
416+
fatalError("tested operand must be an apply argument")
417+
}
418+
let isAddressable = apply.isAddressable(operand: operand)
419+
print("Arg Index: \(argIdx) of Apply: \(apply)")
420+
print(" isAddressable: \(isAddressable)")
421+
}

SwiftCompilerSources/Sources/SIL/Utilities/Test.swift

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -147,7 +147,8 @@ public func registerTests() {
147147
borrowIntroducersTest,
148148
enclosingValuesTest,
149149
forwardingDefUseTest,
150-
forwardingUseDefTest
150+
forwardingUseDefTest,
151+
addressableTest
151152
)
152153

153154
registerTestThunk(testThunk)

test/SILOptimizer/lifetime_dependence/lifetime_dependence_util.sil

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
11
// RUN: %target-sil-opt -test-runner %s \
22
// RUN: -module-name Swift \
33
// RUN: -enable-experimental-feature LifetimeDependence \
4+
// RUN: -enable-experimental-feature AddressableTypes \
45
// RUN: -o /dev/null 2>&1 | %FileCheck %s
56

67
// REQUIRES: swift_in_compiler
78
// REQUIRES: swift_feature_LifetimeDependence
9+
// REQUIRES: swift_feature_AddressableTypes
810

911
sil_stage raw
1012

@@ -62,6 +64,11 @@ struct NEWrap : ~Escapable {
6264
init() { }
6365
}
6466

67+
@_addressableForDependencies
68+
public struct InlineInt {
69+
var i: Builtin.Int64
70+
}
71+
6572
sil @coroutine : $@yield_once @convention(method) (@guaranteed NE) -> @yields @guaranteed NE
6673

6774
sil @capture : $@convention(thin) (@guaranteed NE) -> ()
@@ -347,3 +354,26 @@ bb0(%0 : @owned $NCNEInt):
347354
%99 = tuple()
348355
return %99 : $()
349356
}
357+
358+
// =============================================================================
359+
// isAddressable
360+
// =============================================================================
361+
362+
sil [ossa] @addressableForInoutDepHelper : $@convention(thin) (@in_guaranteed InlineInt, @lifetime(borrow address_for_deps 0) @inout NE) -> () {
363+
bb(%0 : $*InlineInt, %1 : $*NE):
364+
%99 = tuple()
365+
return %99 : $()
366+
}
367+
368+
// CHECK-LABEL: begin running test 1 of 1 on addressableForInoutDep: addressable_arguments with: @instruction.operand[1]
369+
// CHECK: Arg Index: 0 of Apply: %{{.*}} = apply %{{.*}}(%0, %1) : $@convention(thin) (@in_guaranteed InlineInt, @lifetime(borrow address_for_deps 0) @inout NE) -> ()
370+
// CHECK: isAddressable: true
371+
// CHECK-LABEL: end running test 1 of 1 on addressableForInoutDep: addressable_arguments with: @instruction.operand[1]
372+
sil [ossa] @addressableForInoutDep : $@convention(thin) (@in_guaranteed InlineInt, @lifetime(borrow address_for_deps 0) @inout NE) -> () {
373+
bb(%0 : $*InlineInt, %1 : $*NE):
374+
%f = function_ref @addressableForInoutDepHelper : $@convention(thin) (@in_guaranteed InlineInt, @lifetime(borrow address_for_deps 0) @inout NE) -> ()
375+
specify_test "addressable_arguments @instruction.operand[1]"
376+
%call = apply %f(%0, %1) : $@convention(thin) (@in_guaranteed InlineInt, @lifetime(borrow address_for_deps 0) @inout NE) -> ()
377+
%99 = tuple()
378+
return %99 : $()
379+
}

0 commit comments

Comments
 (0)