From 007351223eb96a1a0132953a219b12d9b06ec0e7 Mon Sep 17 00:00:00 2001 From: Erik Eckstein Date: Wed, 14 Oct 2020 17:07:42 +0200 Subject: [PATCH] MemBehavior: handle begin_access when checking for apply side-effects. --- lib/SILOptimizer/Analysis/MemoryBehavior.cpp | 2 ++ test/SILOptimizer/mem-behavior.sil | 18 +++++++++++++++++- 2 files changed, 19 insertions(+), 1 deletion(-) diff --git a/lib/SILOptimizer/Analysis/MemoryBehavior.cpp b/lib/SILOptimizer/Analysis/MemoryBehavior.cpp index b867c18b6300a..bad7bc7ad72e0 100644 --- a/lib/SILOptimizer/Analysis/MemoryBehavior.cpp +++ b/lib/SILOptimizer/Analysis/MemoryBehavior.cpp @@ -364,6 +364,7 @@ static bool hasEscapingUses(SILValue address, int &numChecks) { case SILInstructionKind::CopyAddrInst: case SILInstructionKind::DestroyAddrInst: case SILInstructionKind::DeallocStackInst: + case SILInstructionKind::EndAccessInst: // Those instructions have no result and cannot escape the address. break; case SILInstructionKind::ApplyInst: @@ -373,6 +374,7 @@ static bool hasEscapingUses(SILValue address, int &numChecks) { // possible that an address, passed as an indirect parameter, escapes // the function in any way (which is not unsafe and undefined behavior). break; + case SILInstructionKind::BeginAccessInst: case SILInstructionKind::OpenExistentialAddrInst: case SILInstructionKind::UncheckedTakeEnumDataAddrInst: case SILInstructionKind::StructElementAddrInst: diff --git a/test/SILOptimizer/mem-behavior.sil b/test/SILOptimizer/mem-behavior.sil index c33643578c3c7..7cc35e4061631 100644 --- a/test/SILOptimizer/mem-behavior.sil +++ b/test/SILOptimizer/mem-behavior.sil @@ -269,7 +269,6 @@ bb2(%8 : $Error): unreachable } - // CHECK-LABEL: @beginapply_allocstack_and_copyaddr // CHECK: PAIR #0. // CHECK-NEXT: (%4, %5) = begin_apply %3(%0) : $@yield_once @convention(thin) (@in Int32) -> @yields Int32 @@ -324,6 +323,23 @@ bb0(%0 : $X): return %3 : $Int32 } +// CHECK-LABEL: @apply_and_begin_access +// CHECK: PAIR #0. +// CHECK-NEXT: %6 = apply %5(%1) : $@convention(thin) (@in Int32) -> Int32 +// CHECK-NEXT: %0 = argument of bb0 : $*Int32 +// CHECK-NEXT: r=0,w=0 +sil @apply_and_begin_access : $@convention(thin) (@in Int32) -> Int32 { +bb0(%0 : $*Int32): + %1 = alloc_stack $Int32 + %2 = begin_access [read] [static] %0 : $*Int32 + copy_addr %2 to %1 : $*Int32 + end_access %2 : $*Int32 + %5 = function_ref @single_indirect_arg : $@convention(thin) (@in Int32) -> Int32 + %6 = apply %5(%1) : $@convention(thin) (@in Int32) -> Int32 + dealloc_stack %1 : $*Int32 + return %6 : $Int32 +} + sil @load_from_in : $@convention(thin) (@in X) -> () { bb0(%0 : $*X): %1 = load %0 : $*X