diff --git a/SwiftCompilerSources/Sources/SIL/ApplySite.swift b/SwiftCompilerSources/Sources/SIL/ApplySite.swift index ce4d502ba2221..a19dcbe32dbee 100644 --- a/SwiftCompilerSources/Sources/SIL/ApplySite.swift +++ b/SwiftCompilerSources/Sources/SIL/ApplySite.swift @@ -256,6 +256,14 @@ extension ApplySite { } public func isAddressable(operand: Operand) -> Bool { + for targetOperand in argumentOperands { + guard !targetOperand.value.isEscapable else { + continue + } + if let dep = parameterDependence(target: targetOperand, source: operand), dep.isAddressable(for: operand.value) { + return true + } + } if let dep = resultDependence(on: operand) { return dep.isAddressable(for: operand.value) } @@ -399,3 +407,15 @@ extension FullApplySite { return values } } + +let addressableTest = Test("addressable_arguments") { + function, arguments, context in + + let operand = arguments.takeOperand() + guard let apply = operand.instruction as? ApplySite, let argIdx = apply.calleeArgumentIndex(of: operand) else { + fatalError("tested operand must be an apply argument") + } + let isAddressable = apply.isAddressable(operand: operand) + print("Arg Index: \(argIdx) of Apply: \(apply)") + print(" isAddressable: \(isAddressable)") +} diff --git a/SwiftCompilerSources/Sources/SIL/Utilities/Test.swift b/SwiftCompilerSources/Sources/SIL/Utilities/Test.swift index 8206f5b7b40f8..be8fa8901d3a3 100644 --- a/SwiftCompilerSources/Sources/SIL/Utilities/Test.swift +++ b/SwiftCompilerSources/Sources/SIL/Utilities/Test.swift @@ -147,7 +147,8 @@ public func registerTests() { borrowIntroducersTest, enclosingValuesTest, forwardingDefUseTest, - forwardingUseDefTest + forwardingUseDefTest, + addressableTest ) registerTestThunk(testThunk) diff --git a/test/SILOptimizer/lifetime_dependence/lifetime_dependence_util.sil b/test/SILOptimizer/lifetime_dependence/lifetime_dependence_util.sil index d3b0f41194cb1..65aaaf3d59441 100644 --- a/test/SILOptimizer/lifetime_dependence/lifetime_dependence_util.sil +++ b/test/SILOptimizer/lifetime_dependence/lifetime_dependence_util.sil @@ -1,10 +1,12 @@ // RUN: %target-sil-opt -test-runner %s \ // RUN: -module-name Swift \ // RUN: -enable-experimental-feature LifetimeDependence \ +// RUN: -enable-experimental-feature AddressableTypes \ // RUN: -o /dev/null 2>&1 | %FileCheck %s // REQUIRES: swift_in_compiler // REQUIRES: swift_feature_LifetimeDependence +// REQUIRES: swift_feature_AddressableTypes sil_stage raw @@ -62,6 +64,11 @@ struct NEWrap : ~Escapable { init() { } } +@_addressableForDependencies +public struct InlineInt { + var i: Builtin.Int64 +} + sil @coroutine : $@yield_once @convention(method) (@guaranteed NE) -> @yields @guaranteed NE sil @capture : $@convention(thin) (@guaranteed NE) -> () @@ -347,3 +354,26 @@ bb0(%0 : @owned $NCNEInt): %99 = tuple() return %99 : $() } + +// ============================================================================= +// isAddressable +// ============================================================================= + +sil [ossa] @addressableForInoutDepHelper : $@convention(thin) (@in_guaranteed InlineInt, @lifetime(borrow address_for_deps 0) @inout NE) -> () { +bb(%0 : $*InlineInt, %1 : $*NE): + %99 = tuple() + return %99 : $() +} + +// CHECK-LABEL: begin running test 1 of 1 on addressableForInoutDep: addressable_arguments with: @instruction.operand[1] +// CHECK: Arg Index: 0 of Apply: %{{.*}} = apply %{{.*}}(%0, %1) : $@convention(thin) (@in_guaranteed InlineInt, @lifetime(borrow address_for_deps 0) @inout NE) -> () +// CHECK: isAddressable: true +// CHECK-LABEL: end running test 1 of 1 on addressableForInoutDep: addressable_arguments with: @instruction.operand[1] +sil [ossa] @addressableForInoutDep : $@convention(thin) (@in_guaranteed InlineInt, @lifetime(borrow address_for_deps 0) @inout NE) -> () { +bb(%0 : $*InlineInt, %1 : $*NE): + %f = function_ref @addressableForInoutDepHelper : $@convention(thin) (@in_guaranteed InlineInt, @lifetime(borrow address_for_deps 0) @inout NE) -> () + specify_test "addressable_arguments @instruction.operand[1]" + %call = apply %f(%0, %1) : $@convention(thin) (@in_guaranteed InlineInt, @lifetime(borrow address_for_deps 0) @inout NE) -> () + %99 = tuple() + return %99 : $() +}