Skip to content

Commit

Permalink
MemBehavior: handle begin_access when checking for apply side-effects.
Browse files Browse the repository at this point in the history
  • Loading branch information
eeckstein committed Oct 16, 2020
1 parent b3e9a7f commit 0073512
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 1 deletion.
2 changes: 2 additions & 0 deletions lib/SILOptimizer/Analysis/MemoryBehavior.cpp
Expand Up @@ -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:
Expand All @@ -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:
Expand Down
18 changes: 17 additions & 1 deletion test/SILOptimizer/mem-behavior.sil
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit 0073512

Please sign in to comment.