SILMem2Reg: fix a problem with leaking enum values

When optimizing an enum `store` to an `alloc_stack`, require that all uses are in the same block.
Otherwise it could be a `switch_enum` of an optional where the none-case does not have a destroy of the enum value.
After transforming such an `alloc_stack`, the value would leak in the none-case block.

It fixes the same OSSA verification error as done for TempRValueOpt in a previous commit.
This commit is contained in:
Erik Eckstein
2022-03-08 20:48:08 +01:00
parent 0c529b0ede
commit a62a5caaf4
5 changed files with 63 additions and 3 deletions

View File

@@ -1757,6 +1757,14 @@ bool MemoryToRegisters::promoteSingleAllocation(AllocStackInst *alloc) {
b.createDeallocStack(next->getLoc(), alloc);
}
return true;
} else {
// For enums we require that all uses are in the same block.
// Otherwise there could be a switch_enum of an optional where the none-case
// does not have a destroy of the enum value.
// After transforming such an alloc_stack, the value would leak in the none-
// case block.
if (f.hasOwnership() && alloc->getType().isOrHasEnum())
return false;
}
LLVM_DEBUG(llvm::dbgs() << "*** Need to insert BB arguments for " << *alloc);