Skip to content

Commit 2755d65

Browse files
committed
MemoryLifetimeVerifier: handle stores of tuples with trivial enums
The MemoryLifetimeVerifier issued a false alarm if a tuple with a trivial enum-case (e.g. an Optional nil) is stored to a memory location. rdar://79781939
1 parent efd9c45 commit 2755d65

File tree

2 files changed

+18
-1
lines changed

2 files changed

+18
-1
lines changed

lib/SIL/Verifier/MemoryLifetimeVerifier.cpp

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -178,6 +178,12 @@ static bool isTrivialEnumElem(EnumElementDecl *elem, SILType enumType,
178178
enumType.getEnumElementType(elem, function).isTrivial(*function);
179179
}
180180

181+
static bool isOrHasEnum(SILType type) {
182+
return type.getASTType().findIf([](Type ty) {
183+
return ty->getEnumOrBoundGenericEnum() != nullptr;
184+
});
185+
}
186+
181187
bool MemoryLifetimeVerifier::storesTrivialEnum(int locIdx,
182188
SILBasicBlock::reverse_iterator start,
183189
SILBasicBlock::reverse_iterator end) {
@@ -191,7 +197,7 @@ bool MemoryLifetimeVerifier::storesTrivialEnum(int locIdx,
191197
if (auto *SI = dyn_cast<StoreInst>(&inst)) {
192198
const Location *loc = locations.getLocation(SI->getDest());
193199
if (loc && loc->isSubLocation(locIdx) &&
194-
SI->getSrc()->getType().getEnumOrBoundGenericEnum()) {
200+
isOrHasEnum(SI->getSrc()->getType())) {
195201
return SI->getOwnershipQualifier() == StoreOwnershipQualifier::Trivial;
196202
}
197203
}

test/SIL/memory_lifetime.sil

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -464,6 +464,17 @@ bb0(%0 : @owned $T):
464464
return %r : $()
465465
}
466466

467+
sil [ossa] @test_store_enum_tuple : $@convention(thin) (Int) -> () {
468+
bb0(%0 : $Int):
469+
%2 = enum $Optional<T>, #Optional.none!enumelt
470+
%3 = tuple (%0 : $Int, %2 : $Optional<T>)
471+
%8 = alloc_stack $(Int, Optional<T>)
472+
store %3 to [trivial] %8 : $*(Int, Optional<T>)
473+
dealloc_stack %8 : $*(Int, Optional<T>)
474+
%13 = tuple ()
475+
return %13 : $()
476+
}
477+
467478
sil [ossa] @test_select_enum_addr : $@convention(thin) (@in_guaranteed Optional<T>) -> Builtin.Int1 {
468479
bb0(%0 : $*Optional<T>):
469480
%1 = integer_literal $Builtin.Int1, -1

0 commit comments

Comments
 (0)