Skip to content

[closure-lifetime-fixup] Expose a flag instead of allocating a new in… #23539

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions include/swift/SIL/SILInstruction.h
Original file line number Diff line number Diff line change
Expand Up @@ -4186,9 +4186,17 @@ class ConvertEscapeToNoEscapeInst final
SILFunction &F, SILOpenedArchetypesState &OpenedArchetypes,
bool lifetimeGuaranteed);
public:
/// Return true if we have extended the lifetime of the argument of the
/// convert_escape_to_no_escape to be over all uses of the trivial type.
bool isLifetimeGuaranteed() const {
return lifetimeGuaranteed;
}

/// Mark that we have extended the lifetime of the argument of the
/// convert_escape_to_no_escape to be over all uses of the trivial type.
///
/// NOTE: This is a one way operation.
void setLifetimeGuaranteed() { lifetimeGuaranteed = true; }
};

/// ThinFunctionToPointerInst - Convert a thin function pointer to a
Expand Down
25 changes: 3 additions & 22 deletions lib/SILOptimizer/Mandatory/ClosureLifetimeFixup.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -123,12 +123,7 @@ static void extendLifetimeToEndOfFunction(SILFunction &Fn,
auto OptionalEscapingClosureTy = SILType::getOptionalType(EscapingClosureTy);
auto loc = RegularLocation::getAutoGeneratedLocation();

SILBuilderWithScope B(Cvt);
auto NewCvt = B.createConvertEscapeToNoEscape(
Cvt->getLoc(), Cvt->getOperand(), Cvt->getType(), true);
Cvt->replaceAllUsesWith(NewCvt);
Cvt->eraseFromParent();
Cvt = NewCvt;
Cvt->setLifetimeGuaranteed();

// If our Cvt is in the initial block, we do not need to use the SSA updater
// since we know Cvt can not be in a loop and must dominate all exits
Expand Down Expand Up @@ -417,14 +412,7 @@ static bool tryExtendLifetimeToLastUse(
// Insert a copy at the convert_escape_to_noescape [not_guaranteed] and
// change the instruction to the guaranteed form.
auto EscapingClosure = Cvt->getOperand();
{
SILBuilderWithScope B(Cvt);
auto NewCvt = B.createConvertEscapeToNoEscape(
Cvt->getLoc(), Cvt->getOperand(), Cvt->getType(), true);
Cvt->replaceAllUsesWith(NewCvt);
Cvt->eraseFromParent();
Cvt = NewCvt;
}
Cvt->setLifetimeGuaranteed();

SILBuilderWithScope B2(Cvt);
auto ClosureCopy = B2.createCopyValue(loc, EscapingClosure);
Expand Down Expand Up @@ -510,14 +498,7 @@ static bool trySwitchEnumPeephole(ConvertEscapeToNoEscapeInst *Cvt) {
if (!onlyDestroy)
return false;

// Replace the convert_escape_to_noescape instruction.
{
SILBuilderWithScope B(Cvt);
auto NewCvt = B.createConvertEscapeToNoEscape(
Cvt->getLoc(), Cvt->getOperand(), Cvt->getType(), true);
Cvt->replaceAllUsesWith(NewCvt);
Cvt->eraseFromParent();
}
Cvt->setLifetimeGuaranteed();

// Extend the lifetime.
SILBuilderWithScope B(SwitchEnum1);
Expand Down