Skip to content

Commit 46db7ce

Browse files
committed
LifetimeDependenceDefUseWalker: recognize ConvertPointerToPointer
Look through a call to the ConvertPointerToPointerArgument compiler intrinsic just like it is a copy of the pointer. At the source level, that's all it is: Treat this like a direct use of the argument 'p' rather than the result of the invisible pointer conversion call: func foo(_: UnsafeRawPointer) func bar(p: UnsafePointer<T>) { foo(p) }
1 parent 98da813 commit 46db7ce

File tree

2 files changed

+19
-0
lines changed

2 files changed

+19
-0
lines changed

SwiftCompilerSources/Sources/Optimizer/Utilities/LifetimeDependenceUtils.swift

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -741,6 +741,11 @@ extension LifetimeDependenceUseDefWalker {
741741
return walkUp(newLifetime: store.source)
742742
case let srcDestInst as SourceDestAddrInstruction:
743743
return walkUp(address: srcDestInst.sourceOperand.value)
744+
case let apply as FullApplySite:
745+
if let f = apply.referencedFunction,
746+
f.isConvertPointerToPointerArgument {
747+
return walkUp(address: apply.parameterOperands[0].value)
748+
}
744749
default:
745750
break
746751
}

test/SILOptimizer/lifetime_dependence/lifetime_dependence_insertion.swift

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,3 +48,17 @@ func bv_borrow_var(p: UnsafeRawPointer, i: Int) {
4848
let bv = nc.getBV()
4949
use(bv)
5050
}
51+
52+
// LifetimeDependence.Scope needs to see through typed-to-raw pointer conversion.
53+
//
54+
// CHECK-LABEL: sil hidden [ossa] @$s4test18bv_pointer_convert1pAA2BVVSPySiG_tF : $@convention(thin) (UnsafePointer<Int>) -> @lifetime(borrow 0) @owned BV {
55+
// CHECK: bb0(%0 : $UnsafePointer<Int>):
56+
// CHECK: apply %{{.*}}<UnsafePointer<Int>, UnsafeRawPointer>([[RAW:%.*]], %{{.*}}) : $@convention(thin) <τ_0_0, τ_0_1 where τ_0_0 : _Pointer, τ_0_1 : _Pointer> (@in_guaranteed τ_0_0) -> @out τ_0_1
57+
// CHECK: [[RAW:%.*]] = load [trivial] %6 : $*UnsafeRawPointer
58+
// CHECK: [[BV:%.*]] = apply %13([[RAW]], {{.*}}) : $@convention(method) (UnsafeRawPointer, Int, @thin BV.Type) -> @lifetime(borrow 0) @owned BV
59+
// CHECK: return [[BV]] : $BV
60+
// CHECK-LABEL: } // end sil function '$s4test18bv_pointer_convert1pAA2BVVSPySiG_tF'
61+
@lifetime(borrow p)
62+
func bv_pointer_convert(p: UnsafePointer<Int>) -> BV {
63+
BV(p, 0)
64+
}

0 commit comments

Comments
 (0)