Skip to content

Commit 7597641

Browse files
committed
EscapeUtils: fix handling a raw pointer during up-walk
When walking up we shouldn't end up at a load where followLoads is false, because going from a (non-followLoads) address to a load always involves a class indirection. There is one exception: loading a raw pointer
1 parent d602836 commit 7597641

File tree

2 files changed

+30
-0
lines changed

2 files changed

+30
-0
lines changed

SwiftCompilerSources/Sources/Optimizer/Utilities/EscapeUtils.swift

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -679,6 +679,14 @@ fileprivate struct EscapeWalker<V: EscapeVisitor> : ValueDefUseWalker,
679679
case let ap as ApplyInst:
680680
return walkUpApplyResult(apply: ap, path: path.with(knownType: nil))
681681
case is LoadInst, is LoadWeakInst, is LoadUnownedInst:
682+
if !followLoads(at: path.projectionPath) {
683+
// When walking up we shouldn't end up at a load where followLoads is false,
684+
// because going from a (non-followLoads) address to a load always involves a class indirection.
685+
// There is one exception: loading a raw pointer, e.g.
686+
// %l = load %a : $Builtin.RawPointer
687+
// %a = pointer_to_address %l // the up-walk starts at %a
688+
return isEscaping
689+
}
682690
return walkUp(address: (def as! UnaryInstruction).operand,
683691
path: path.with(followStores: true).with(knownType: nil))
684692
case let atp as AddressToPointerInst:

test/SILOptimizer/addr_escape_info.sil

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -500,6 +500,28 @@ bb0:
500500
return %7 : $()
501501
}
502502

503+
// CHECK-LABEL: Address escape information for load_rawpointer:
504+
// CHECK: value: %5 = pointer_to_address %4 : $Builtin.RawPointer to [strict] $*Int
505+
// CHECK-NEXT: ==> %8 = apply %7(%5) : $@convention(thin) (@in Int) -> ()
506+
// CHECK: End function load_rawpointer
507+
sil @load_rawpointer : $@convention(thin) () -> () {
508+
bb0:
509+
%0 = alloc_stack $Int
510+
%1 = address_to_pointer %0 : $*Int to $Builtin.RawPointer
511+
%2 = alloc_stack $Builtin.RawPointer
512+
store %1 to %2 : $*Builtin.RawPointer
513+
%4 = load %2 : $*Builtin.RawPointer
514+
%5 = pointer_to_address %4 : $Builtin.RawPointer to [strict] $*Int
515+
fix_lifetime %5 : $*Int
516+
%7 = function_ref @indirect_argument : $@convention(thin) (@in Int) -> ()
517+
%8 = apply %7(%5) : $@convention(thin) (@in Int) -> ()
518+
dealloc_stack %2 : $*Builtin.RawPointer
519+
dealloc_stack %0 : $*Int
520+
%r = tuple ()
521+
return %r : $()
522+
}
523+
524+
503525
// CHECK-LABEL: Address escape information for test_addr_alias1:
504526
// CHECK: pair 0 - 1
505527
// CHECK-NEXT: %2 = ref_element_addr %0 : $XandIntClass, #XandIntClass.x

0 commit comments

Comments
 (0)