Skip to content

Commit db29d77

Browse files
committed
SILCombine: don't create a strong_retain/strong_release with an Optional<reference> as an operand
strong_retain/strong_release with an optional reference as operand are rejected by the verifier and are not supported by IRGen. SILCombine created such retains/releases when optimizing ref_cast instructions. This fixes a compiler crash. rdar://74146617
1 parent 30bcdd9 commit db29d77

File tree

2 files changed

+19
-0
lines changed

2 files changed

+19
-0
lines changed

lib/SILOptimizer/Analysis/SimplifyInstruction.cpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -341,8 +341,13 @@ static SILValue simplifyDeadCast(SingleValueInstruction *Cast) {
341341
for (Operand *op : Cast->getUses()) {
342342
switch (op->getUser()->getKind()) {
343343
case SILInstructionKind::DestroyValueInst:
344+
break;
344345
case SILInstructionKind::StrongReleaseInst:
345346
case SILInstructionKind::StrongRetainInst:
347+
// ref-casts can cast from an Optional<Classtype>. But strong_retain/
348+
// strong_release don't accept an optional.
349+
if (!Cast->getOperand(0)->getType().isReferenceCounted(Cast->getModule()))
350+
return SILValue();
346351
break;
347352
default:
348353
return SILValue();

test/SILOptimizer/sil_combine.sil

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -821,6 +821,20 @@ bb0(%0 : $C1):
821821
return %0 : $C1
822822
}
823823

824+
// strong_retain/strong_release don't accept an Optional<reference>.
825+
//
826+
// CHECK-LABEL: sil @dontOptimizeRefCastOfOptional
827+
// CHECK: %1 = unchecked_ref_cast %0
828+
// CHECK: strong_retain %1
829+
// CHECK: } // end sil function 'dontOptimizeRefCastOfOptional'
830+
sil @dontOptimizeRefCastOfOptional : $@convention(thin) (@guaranteed Optional<C1>) -> () {
831+
bb0(%0 : $Optional<C1>):
832+
%1 = unchecked_ref_cast %0 : $Optional<C1> to $C1
833+
strong_retain %1 : $C1
834+
%3 = tuple ()
835+
return %3 : $()
836+
}
837+
824838
// CHECK-LABEL: sil @dead_end_cow_mutation
825839
// CHECK: bb0
826840
// CHECK-NEXT: strong_retain %0

0 commit comments

Comments
 (0)