Skip to content

Commit 5d1f25e

Browse files
authored
Merge pull request #83058 from meg-gupta/fixsemanticarccp
[6.2] Bailout from an illegal transformation in semantic arc opts
2 parents 234a415 + 8f00bc4 commit 5d1f25e

File tree

2 files changed

+48
-1
lines changed

2 files changed

+48
-1
lines changed

lib/SILOptimizer/SemanticARC/CopyValueOpts.cpp

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -503,6 +503,13 @@ static bool tryJoinIfDestroyConsumingUseInSameBlock(
503503
if (visitedInsts.count(use->getUser()))
504504
return false;
505505

506+
// If the cvi's operand value has a non-consuming use in the same
507+
// instruction which consumes the copy, bailout. NOTE:
508+
// isUseBetweenInstAndBlockEnd does not check this.
509+
if (user == singleCVIConsumingUse->getUser()) {
510+
return false;
511+
}
512+
506513
// Ok, we have a use that isn't in our visitedInsts region. That being said,
507514
// we may still have a use that introduces a new BorrowScope onto our
508515
// copy_value's operand that overlaps with our forwarding value region. In
@@ -513,14 +520,15 @@ static bool tryJoinIfDestroyConsumingUseInSameBlock(
513520
// we need to only find scopes that end within the region in between the
514521
// singleConsumingUse (the original forwarded use) and the destroy_value. In
515522
// such a case, we must bail!
516-
if (auto operand = BorrowingOperand(use))
523+
if (auto operand = BorrowingOperand(use)) {
517524
if (!operand.visitScopeEndingUses([&](Operand *endScopeUse) {
518525
// Return false if we did see the relevant end scope instruction
519526
// in the block. That means that we are going to exit early and
520527
// return false.
521528
return !visitedInsts.count(endScopeUse->getUser());
522529
}))
523530
return false;
531+
}
524532
}
525533

526534
// Ok, we now know that we can eliminate this value.

test/SILOptimizer/semantic-arc-opts-lifetime-joining.sil

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -978,3 +978,42 @@ bb0:
978978
return %r : $()
979979
}
980980

981+
sil @consume_and_borrow : $@convention(thin) (@owned Klass, @guaranteed Klass) -> ()
982+
sil @borrow : $@convention(thin) (@guaranteed Klass) -> ()
983+
sil @consume : $@convention(thin) (@owned Klass) -> ()
984+
sil @get_klass : $@convention(thin) () -> @owned Klass
985+
986+
// CHECK-LABEL: sil hidden [ossa] @borrow_and_consume_copyable_test1 :
987+
// CHECK-NOT: copy_value
988+
// CHECK-LABEL: } // end sil function 'borrow_and_consume_copyable_test1'
989+
sil hidden [ossa] @borrow_and_consume_copyable_test1 : $@convention(thin) () -> () {
990+
bb0:
991+
%0 = function_ref @get_klass : $@convention(thin) () -> @owned Klass
992+
%1 = apply %0() : $@convention(thin) () -> @owned Klass
993+
%2 = move_value [var_decl] %1
994+
%3 = copy_value %2
995+
%4 = function_ref @borrow : $@convention(thin) (@guaranteed Klass) -> ()
996+
%5 = apply %4(%2) : $@convention(thin) (@guaranteed Klass) -> ()
997+
%6 = function_ref @consume : $@convention(thin) (@owned Klass) -> ()
998+
%7 = apply %6(%3) : $@convention(thin) (@owned Klass) -> ()
999+
destroy_value %2
1000+
%9 = tuple ()
1001+
return %9
1002+
}
1003+
1004+
// CHECK-LABEL: sil hidden [ossa] @borrow_and_consume_copyable_test2 :
1005+
// CHECK: copy_value
1006+
// CHECK-LABEL: } // end sil function 'borrow_and_consume_copyable_test2'
1007+
sil hidden [ossa] @borrow_and_consume_copyable_test2 : $@convention(thin) () -> () {
1008+
bb0:
1009+
%0 = function_ref @get_klass : $@convention(thin) () -> @owned Klass
1010+
%1 = apply %0() : $@convention(thin) () -> @owned Klass
1011+
%2 = move_value [var_decl] %1
1012+
%3 = copy_value %2
1013+
%4 = function_ref @consume_and_borrow : $@convention(thin) (@owned Klass, @guaranteed Klass) -> ()
1014+
%5 = apply %4(%3, %2) : $@convention(thin) (@owned Klass, @guaranteed Klass) -> ()
1015+
destroy_value %2
1016+
%7 = tuple ()
1017+
return %7
1018+
}
1019+

0 commit comments

Comments
 (0)