Skip to content

Commit a0c6eca

Browse files
authored
Merge pull request #82762 from eeckstein/fix-escape-utils
EscapeUtils: consider that a pointer argument can escape a function call
2 parents 65d9f5f + 63da299 commit a0c6eca

File tree

3 files changed

+40
-1
lines changed

3 files changed

+40
-1
lines changed

SwiftCompilerSources/Sources/Optimizer/Utilities/EscapeUtils.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -640,7 +640,7 @@ fileprivate struct EscapeWalker<V: EscapeVisitor> : ValueDefUseWalker,
640640
}
641641

642642
// Indirect arguments cannot escape the function, but loaded values from such can.
643-
if !followLoads(at: path) {
643+
if argOp.value.type.isAddress && !followLoads(at: path) {
644644
if let beginApply = apply as? BeginApplyInst {
645645
// begin_apply can yield an address value.
646646
if !indirectResultEscapes(of: beginApply, path: path) {

test/SILOptimizer/addr_escape_info.sil

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -984,3 +984,21 @@ bb0(%0 : $Int):
984984
return %9 : $()
985985
}
986986

987+
// CHECK-LABEL: Address escape information for escaping_pointer_through_function:
988+
// CHECK: value: %1 = alloc_stack $Int
989+
// CHECK-NEXT: ==> %4 = apply undef(%3) : $@convention(thin) (Builtin.RawPointer) -> UnsafeMutableRawBufferPointer
990+
// CHECK-NEXT: ==> %5 = apply undef(%4) : $@convention(thin) (UnsafeMutableRawBufferPointer) -> ()
991+
// CHECK-NEXT: End function escaping_pointer_through_function
992+
sil [ossa] @escaping_pointer_through_function : $@convention(thin) (Int) -> () {
993+
bb0(%0 : $Int):
994+
%1 = alloc_stack $Int
995+
fix_lifetime %1
996+
%3 = address_to_pointer %1 to $Builtin.RawPointer
997+
%4 = apply undef(%3) : $@convention(thin) (Builtin.RawPointer) -> UnsafeMutableRawBufferPointer
998+
%5 = apply undef(%4) : $@convention(thin) (UnsafeMutableRawBufferPointer) -> ()
999+
dealloc_stack %1
1000+
%7 = tuple ()
1001+
return %7
1002+
}
1003+
1004+

test/SILOptimizer/redundant_load_elim_ossa.sil

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1776,3 +1776,24 @@ bb0(%0 : $*Builtin.FixedArray<10, Int>, %1 : $Int, %2 : $Int):
17761776
return %6
17771777
}
17781778

1779+
sil @forwardPtr : $@convention(thin) (Builtin.RawPointer) -> UnsafeMutableRawBufferPointer {
1780+
[global: read]
1781+
}
1782+
1783+
// CHECK-LABEL: sil [ossa] @escaping_pointer_through_function :
1784+
// CHECK: [[LD:%.*]] = load [trivial] %1
1785+
// CHECK: return [[LD]]
1786+
// CHECK-LABEL: } // end sil function 'escaping_pointer_through_function'
1787+
sil [ossa] @escaping_pointer_through_function : $@convention(thin) (Int) -> Int {
1788+
bb0(%0 : $Int):
1789+
%1 = alloc_stack $Int
1790+
store %0 to [trivial] %1
1791+
%3 = address_to_pointer %1 to $Builtin.RawPointer
1792+
%4 = function_ref @forwardPtr : $@convention(thin) (Builtin.RawPointer) -> UnsafeMutableRawBufferPointer
1793+
%5 = apply %4(%3) : $@convention(thin) (Builtin.RawPointer) -> UnsafeMutableRawBufferPointer
1794+
%6 = apply undef(%5) : $@convention(thin) (UnsafeMutableRawBufferPointer) -> ()
1795+
%7 = load [trivial] %1
1796+
dealloc_stack %1
1797+
return %7
1798+
}
1799+

0 commit comments

Comments
 (0)