[AllocBoxToStack] Transfer var_decl flag.

As with the lexical flag, when creating an alloc_stack corresponding to
an alloc_box, transfer the var_decl flag from any begin_borrow users of
the box.
This commit is contained in:
Nate Chandler
2024-03-08 20:41:17 -08:00
parent dff0b2efaa
commit 08a832b803
16 changed files with 84 additions and 76 deletions

View File

@@ -624,12 +624,16 @@ static bool rewriteAllocBoxAsAllocStack(AllocBoxInst *ABI) {
&& "rewriting multi-field box not implemented");
auto ty = getSILBoxFieldType(TypeExpansionContext(*ABI->getFunction()),
ABI->getBoxType(), ABI->getModule().Types, 0);
auto isLexical = [&]() -> IsLexical_t {
struct Flags {
IsLexical_t isLexical;
IsFromVarDecl_t isVarDecl;
};
auto getFlags = [&]() -> Flags {
auto &mod = ABI->getFunction()->getModule();
bool lexicalLifetimesEnabled =
mod.getASTContext().SILOpts.supportsLexicalLifetimes(mod);
if (!lexicalLifetimesEnabled)
return IsNotLexical;
bool sawLexical = false;
bool sawVarDecl = false;
// Look for lexical borrows of the alloc_box.
GraphNodeWorklist<Operand *, 4> worklist;
worklist.initializeRange(ABI->getUses());
@@ -642,16 +646,20 @@ static bool rewriteAllocBoxAsAllocStack(AllocBoxInst *ABI) {
worklist.insert(use);
} else if (auto *bbi = dyn_cast<BeginBorrowInst>(use->getUser())) {
if (bbi->isLexical())
return IsLexical;
sawLexical = true;
if (bbi->isFromVarDecl())
sawVarDecl = true;
for (auto *use : bbi->getUses())
worklist.insert(use);
}
}
return IsNotLexical;
return Flags{IsLexical_t(lexicalLifetimesEnabled && sawLexical),
IsFromVarDecl_t(sawVarDecl)};
};
auto flags = getFlags();
auto *ASI = Builder.createAllocStack(
ABI->getLoc(), ty, ABI->getVarInfo(), ABI->hasDynamicLifetime(),
isLexical(), IsNotFromVarDecl, DoesNotUseMoveableValueDebugInfo
flags.isLexical, flags.isVarDecl, DoesNotUseMoveableValueDebugInfo
#ifndef NDEBUG
,
true