Skip to content

Commit c3adbfb

Browse files
authored
Merge pull request #42107 from eeckstein/fix-mem-behvior
MemAccessUtils: handle the case of `enum` with no operand in findOwnershipReferenceAggregate
2 parents df913c9 + e297015 commit c3adbfb

File tree

2 files changed

+29
-1
lines changed

2 files changed

+29
-1
lines changed

lib/SIL/Utils/MemAccessUtils.cpp

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -912,7 +912,13 @@ SILValue swift::findOwnershipReferenceAggregate(SILValue ref) {
912912
|| isa<OwnershipForwardingConversionInst>(root)
913913
|| isa<OwnershipForwardingSelectEnumInstBase>(root)
914914
|| isa<OwnershipForwardingMultipleValueInstruction>(root)) {
915-
root = root->getDefiningInstruction()->getOperand(0);
915+
SILInstruction *inst = root->getDefiningInstruction();
916+
917+
// The `enum` instruction can have no operand.
918+
if (inst->getNumOperands() == 0)
919+
return root;
920+
921+
root = inst->getOperand(0);
916922
continue;
917923
}
918924
if (auto *arg = dyn_cast<SILArgument>(root)) {

test/SILOptimizer/mem-behavior.sil

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -699,6 +699,28 @@ bb4:
699699
return %5 : $Int32
700700
}
701701

702+
// CHECK-LABEL: @dontCrashWithOptionalNone
703+
// CHECK: PAIR #0.
704+
// CHECK-NEXT: %6 = apply %5() : $@convention(thin) () -> ()
705+
// CHECK-NEXT: %4 = ref_element_addr [immutable] %3 : $X, #X.a
706+
// CHECK-NEXT: r=1,w=1
707+
sil @dontCrashWithOptionalNone : $@convention(thin) () -> () {
708+
bb0:
709+
%26 = enum $Optional<X>, #Optional.none!enumelt
710+
switch_enum %26 : $Optional<X>, case #Optional.none!enumelt: bb1, case #Optional.some!enumelt: bb2
711+
bb1:
712+
br bb3
713+
bb2:
714+
%68 = unchecked_enum_data %26 : $Optional<X>, #Optional.some!enumelt
715+
%1 = ref_element_addr [immutable] %68 : $X, #X.a
716+
%5 = function_ref @nouser_func : $@convention(thin) () -> ()
717+
%6 = apply %5() : $@convention(thin) () -> ()
718+
br bb3
719+
bb3:
720+
%r = tuple ()
721+
return %r : $()
722+
}
723+
702724
// CHECK-LABEL: @testLoadTake
703725
// CHECK: PAIR #0.
704726
// CHECK-NEXT: %2 = load [take] %0 : $*C

0 commit comments

Comments
 (0)