Skip to content

Commit fd1c55c

Browse files
committed
Handle switch_value in MemoryLifetimeVerifier
1 parent 8fd8217 commit fd1c55c

File tree

2 files changed

+32
-0
lines changed

2 files changed

+32
-0
lines changed

lib/SIL/Verifier/MemoryLifetimeVerifier.cpp

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -228,6 +228,13 @@ bool MemoryLifetimeVerifier::isTrivialEnumSuccessor(SILBasicBlock *block,
228228
} else if (auto *switchEnumAddr = dyn_cast<SwitchEnumAddrInst>(term)) {
229229
elem = switchEnumAddr->getUniqueCaseForDestination(succ);
230230
enumTy = switchEnumAddr->getOperand()->getType();
231+
} else if (auto *switchValue = dyn_cast<SwitchValueInst>(term)) {
232+
auto destCase = switchValue->getUniqueCaseForDestination(succ);
233+
assert(destCase.has_value());
234+
auto caseValue =
235+
cast<IntegerLiteralInst>(switchValue->getCase(*destCase).first);
236+
auto testValue = dyn_cast<IntegerLiteralInst>(switchValue->getOperand());
237+
return testValue ? testValue->getValue() != caseValue->getValue() : true;
231238
} else {
232239
return false;
233240
}

test/SIL/memory_lifetime.sil

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -781,3 +781,28 @@ bb0:
781781
%7 = tuple ()
782782
return %7 : $()
783783
}
784+
785+
sil [ossa] @test_init_enum_switch_value : $@convention(thin) (@in_guaranteed Optional<T>, @in_guaranteed T) -> () {
786+
bb0(%0 : $*Optional<T>, %1 : $*T):
787+
%stk = alloc_stack $Optional<T>
788+
%2 = init_enum_data_addr %stk : $*Optional<T>, #Optional.some!enumelt
789+
copy_addr %1 to [init] %2 : $*T
790+
inject_enum_addr %stk : $*Optional<T>, #Optional.some!enumelt
791+
%one = integer_literal $Builtin.Word, 1
792+
%two = integer_literal $Builtin.Word, 2
793+
switch_value %one : $Builtin.Word, case %one: bb1, case %two: bb2
794+
795+
bb1:
796+
destroy_addr %stk : $*Optional<T>
797+
dealloc_stack %stk : $*Optional<T>
798+
br bb3
799+
800+
bb2:
801+
dealloc_stack %stk : $*Optional<T>
802+
br bb3
803+
804+
bb3:
805+
%r = tuple ()
806+
return %r : $()
807+
}
808+

0 commit comments

Comments
 (0)