Skip to content

Commit 2d1045f

Browse files
committed
MemBehavior: handle guaranteed arguments in non-OSSA mode.
In non-OSSA, this is the only case for which we know that if an instruction is within a "borrow-scope".
1 parent f97876c commit 2d1045f

File tree

2 files changed

+24
-0
lines changed

2 files changed

+24
-0
lines changed

lib/SILOptimizer/Analysis/MemoryBehavior.cpp

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -566,6 +566,17 @@ static SILValue getBeginScopeInst(SILValue V) {
566566
if (BorrowedValue borrowedObj = getSingleBorrowIntroducingValue(object)) {
567567
return borrowedObj.value;
568568
}
569+
if (!object->getFunction()->hasOwnership()) {
570+
// In non-OSSA, do a quick check if the object is a guaranteed function
571+
// argument.
572+
// Note that in OSSA, getSingleBorrowIntroducingValue will detect a
573+
// guaranteed argument.
574+
SILValue root = findOwnershipReferenceAggregate(object);
575+
if (auto *funcArg = dyn_cast<SILFunctionArgument>(root)) {
576+
if (funcArg->getArgumentConvention().isGuaranteedConvention())
577+
return funcArg;
578+
}
579+
}
569580
return SILValue();
570581
}
571582

test/SILOptimizer/mem-behavior.sil

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -618,6 +618,19 @@ bb0(%0 : @guaranteed $X):
618618
return %3 : $Int32
619619
}
620620

621+
// CHECK-LABEL: @testNonOSSAImmutableRefWithGuaranteedParam
622+
// CHECK: PAIR #1.
623+
// CHECK-NEXT: %4 = apply %3() : $@convention(thin) () -> ()
624+
// CHECK-NEXT: %1 = ref_element_addr [immutable] %0 : $X, #X.a
625+
// CHECK-NEXT: r=1,w=0
626+
sil @testNonOSSAImmutableRefWithGuaranteedParam : $@convention(thin) (@guaranteed X) -> Int32 {
627+
bb0(%0 : $X):
628+
%1 = ref_element_addr [immutable] %0 : $X, #X.a
629+
%3 = load %1 : $*Int32
630+
%5 = function_ref @nouser_func : $@convention(thin) () -> ()
631+
%6 = apply %5() : $@convention(thin) () -> ()
632+
return %3 : $Int32
633+
}
621634
// CHECK-LABEL: @testImmutableRefWithBorrow
622635
// CHECK: PAIR #1.
623636
// CHECK-NEXT: %5 = apply %4() : $@convention(thin) () -> ()

0 commit comments

Comments
 (0)