Skip to content

Commit f870d7d

Browse files
committed
Fix ApplySite.isAddressable to handle inout arguments.
1 parent a0d33c3 commit f870d7d

File tree

3 files changed

+51
-1
lines changed

3 files changed

+51
-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: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
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
@@ -62,6 +63,11 @@ struct NEWrap : ~Escapable {
6263
init() { }
6364
}
6465

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

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

0 commit comments

Comments
 (0)