Skip to content

Commit ac3e2cb

Browse files
committed
EscapeUtils: handle more use types for unchecked_addr_cast
Handle `apply` and `try_apply`. Also, improve test cases
1 parent 8f839f0 commit ac3e2cb

File tree

3 files changed

+34
-17
lines changed

3 files changed

+34
-17
lines changed

SwiftCompilerSources/Sources/Optimizer/Utilities/EscapeUtils.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -571,7 +571,7 @@ fileprivate struct EscapeWalker<V: EscapeVisitor> : ValueDefUseWalker,
571571
switch uacUse.instruction {
572572
case is IsUniqueInst:
573573
break
574-
case is LoadInst, is LoadBorrowInst:
574+
case is LoadInst, is LoadBorrowInst, is ApplyInst, is TryApplyInst:
575575
if followLoads(at: path) {
576576
return .abortWalk
577577
}

test/SILOptimizer/addr_escape_info.sil

Lines changed: 19 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,12 @@ sil @non_escaping_class_argument : $@convention(thin) (@guaranteed X) -> () {
4848
sil @inout_class_argument : $@convention(thin) (@inout X) -> () {
4949
[%0: noescape **]
5050
}
51+
sil @inout_class_argument2 : $@convention(thin) (@inout XandIntClass) -> () {
52+
[%0: noescape **]
53+
}
54+
sil @inout_class_argument3 : $@convention(thin) (@inout XandIntClass) -> @error Error {
55+
[%0: noescape **]
56+
}
5157
sil @container_argument : $@convention(thin) (@guaranteed Container) -> ()
5258
sil @take_closure_as_addr : $@convention(thin) (@in @callee_guaranteed () -> ()) -> ()
5359
sil @take_closure_as_addr_noescape : $@convention(thin) (@in @callee_guaranteed () -> ()) -> () {
@@ -813,6 +819,9 @@ bb0(%0 : @owned $X):
813819

814820
// CHECK-LABEL: Address escape information for test_unchecked_addr_cast:
815821
// CHECK: value: %1 = alloc_stack $X
822+
// CHECK: ==> %10 = apply %9(%4) : $@convention(thin) (@inout XandIntClass) -> ()
823+
// CHECK: ==> try_apply %11(%4) : $@convention(thin) (@inout XandIntClass) -> @error any Error, normal bb1, error bb2
824+
// CHECK: - %15 = apply %14() : $@convention(thin) () -> ()
816825
// CHECK-NEXT: End function test_unchecked_addr_cast
817826
sil [ossa] @test_unchecked_addr_cast : $@convention(thin) (@owned X) -> @owned XandIntClass {
818827
bb0(%0 : @owned $X):
@@ -824,25 +833,19 @@ bb0(%0 : @owned $X):
824833
%6 = load_borrow %4 : $*XandIntClass
825834
end_borrow %6 : $XandIntClass
826835
%8 = load [take] %4 : $*XandIntClass
836+
%9 = function_ref @inout_class_argument2 : $@convention(thin) (@inout XandIntClass) -> ()
837+
%10 = apply %9(%4) : $@convention(thin) (@inout XandIntClass) -> ()
838+
%11 = function_ref @inout_class_argument3 : $@convention(thin) (@inout XandIntClass) -> @error Error
839+
try_apply %11(%4) : $@convention(thin) (@inout XandIntClass) -> @error Error, normal bb1, error bb2
840+
841+
bb1(%13 : $()):
842+
%14 = function_ref @no_arguments : $@convention(thin) () -> ()
843+
%15 = apply %14() : $@convention(thin) () -> ()
827844
dealloc_stack %1 : $*X
828845
return %8 : $XandIntClass
829-
}
830846

831-
// CHECK-LABEL: Address escape information for test_unchecked_addr_cast_escaping:
832-
// CHECK: value: %1 = alloc_stack $X
833-
// CHECK-NEXT: ==> %6 = apply %5(%4) : $@convention(thin) (@inout X) -> ()
834-
// CHECK-NEXT: End function test_unchecked_addr_cast_escaping
835-
sil [ossa] @test_unchecked_addr_cast_escaping : $@convention(thin) (@owned XandIntClass) -> () {
836-
bb0(%0 : @owned $XandIntClass):
837-
%1 = alloc_stack $XandIntClass
838-
store %0 to [init] %1 : $*XandIntClass
839-
fix_lifetime %1 : $*XandIntClass
840-
%4 = unchecked_addr_cast %1 : $*XandIntClass to $*X
841-
%5 = function_ref @inout_class_argument : $@convention(thin) (@inout X) -> ()
842-
%6 = apply %5(%4) : $@convention(thin) (@inout X) -> ()
843-
dealloc_stack %1 : $*XandIntClass
844-
%8 = tuple ()
845-
return %8 : $()
847+
bb2(%17 : $Error):
848+
unreachable
846849
}
847850

848851
// CHECK-LABEL: Address escape information for test_store_borrow:

test/SILOptimizer/escape_info.sil

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1457,3 +1457,17 @@ bb0:
14571457
%3 = unchecked_ref_cast %1 : $Derived to $X
14581458
return %2 : $X
14591459
}
1460+
1461+
// CHECK-LABEL: Escape information for test_unchecked_addr_cast:
1462+
// CHECK: global: %1 = alloc_ref $X
1463+
// CHECK: End function test_unchecked_addr_cast
1464+
sil @test_unchecked_addr_cast : $@convention(thin) () -> @owned Builtin.RawPointer {
1465+
bb0:
1466+
%0 = alloc_stack $X
1467+
%1 = alloc_ref $X
1468+
store %1 to %0 : $*X
1469+
%3 = unchecked_addr_cast %0 : $*X to $*Builtin.RawPointer
1470+
%4 = load %3 : $*Builtin.RawPointer
1471+
dealloc_stack %0 : $*X
1472+
return %4 : $Builtin.RawPointer
1473+
}

0 commit comments

Comments
 (0)