Skip to content

[6.2] EscapeUtils: consider that a pointer argument can escape a function call #82799

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jul 7, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -640,7 +640,7 @@ fileprivate struct EscapeWalker<V: EscapeVisitor> : ValueDefUseWalker,
}

// Indirect arguments cannot escape the function, but loaded values from such can.
if !followLoads(at: path) {
if argOp.value.type.isAddress && !followLoads(at: path) {
if let beginApply = apply as? BeginApplyInst {
// begin_apply can yield an address value.
if !indirectResultEscapes(of: beginApply, path: path) {
Expand Down
18 changes: 18 additions & 0 deletions test/SILOptimizer/addr_escape_info.sil
Original file line number Diff line number Diff line change
Expand Up @@ -956,3 +956,21 @@ bb0(%0 : $Int):
%9 = tuple ()
return %9 : $()
}

// CHECK-LABEL: Address escape information for escaping_pointer_through_function:
// CHECK: value: %1 = alloc_stack $Int
// CHECK-NEXT: ==> %4 = apply undef(%3) : $@convention(thin) (Builtin.RawPointer) -> UnsafeMutableRawBufferPointer
// CHECK-NEXT: ==> %5 = apply undef(%4) : $@convention(thin) (UnsafeMutableRawBufferPointer) -> ()
// CHECK-NEXT: End function escaping_pointer_through_function
sil [ossa] @escaping_pointer_through_function : $@convention(thin) (Int) -> () {
bb0(%0 : $Int):
%1 = alloc_stack $Int
fix_lifetime %1
%3 = address_to_pointer %1 to $Builtin.RawPointer
%4 = apply undef(%3) : $@convention(thin) (Builtin.RawPointer) -> UnsafeMutableRawBufferPointer
%5 = apply undef(%4) : $@convention(thin) (UnsafeMutableRawBufferPointer) -> ()
dealloc_stack %1
%7 = tuple ()
return %7
}

22 changes: 22 additions & 0 deletions test/SILOptimizer/redundant_load_elim_ossa.sil
Original file line number Diff line number Diff line change
Expand Up @@ -1761,3 +1761,25 @@ bb0(%0 : $Int):
dealloc_stack %3
return %6
}

sil @forwardPtr : $@convention(thin) (Builtin.RawPointer) -> UnsafeMutableRawBufferPointer {
[global: read]
}

// CHECK-LABEL: sil [ossa] @escaping_pointer_through_function :
// CHECK: [[LD:%.*]] = load [trivial] %1
// CHECK: return [[LD]]
// CHECK-LABEL: } // end sil function 'escaping_pointer_through_function'
sil [ossa] @escaping_pointer_through_function : $@convention(thin) (Int) -> Int {
bb0(%0 : $Int):
%1 = alloc_stack $Int
store %0 to [trivial] %1
%3 = address_to_pointer %1 to $Builtin.RawPointer
%4 = function_ref @forwardPtr : $@convention(thin) (Builtin.RawPointer) -> UnsafeMutableRawBufferPointer
%5 = apply %4(%3) : $@convention(thin) (Builtin.RawPointer) -> UnsafeMutableRawBufferPointer
%6 = apply undef(%5) : $@convention(thin) (UnsafeMutableRawBufferPointer) -> ()
%7 = load [trivial] %1
dealloc_stack %1
return %7
}