Skip to content

Commit f8d1514

Browse files
committed
[closure-lifetime-fixup] Expose a flag instead of allocating a new instruction.
Small inefficiency I noticed.
1 parent 86888ce commit f8d1514

File tree

2 files changed

+11
-22
lines changed

2 files changed

+11
-22
lines changed

include/swift/SIL/SILInstruction.h

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4186,9 +4186,17 @@ class ConvertEscapeToNoEscapeInst final
41864186
SILFunction &F, SILOpenedArchetypesState &OpenedArchetypes,
41874187
bool lifetimeGuaranteed);
41884188
public:
4189+
/// Return true if we have extended the lifetime of the argument of the
4190+
/// convert_escape_to_no_escape to be over all uses of the trivial type.
41894191
bool isLifetimeGuaranteed() const {
41904192
return lifetimeGuaranteed;
41914193
}
4194+
4195+
/// Mark that we have extended the lifetime of the argument of the
4196+
/// convert_escape_to_no_escape to be over all uses of the trivial type.
4197+
///
4198+
/// NOTE: This is a one way operation.
4199+
void setLifetimeGuaranteed() { lifetimeGuaranteed = true; }
41924200
};
41934201

41944202
/// ThinFunctionToPointerInst - Convert a thin function pointer to a

lib/SILOptimizer/Mandatory/ClosureLifetimeFixup.cpp

Lines changed: 3 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -123,12 +123,7 @@ static void extendLifetimeToEndOfFunction(SILFunction &Fn,
123123
auto OptionalEscapingClosureTy = SILType::getOptionalType(EscapingClosureTy);
124124
auto loc = RegularLocation::getAutoGeneratedLocation();
125125

126-
SILBuilderWithScope B(Cvt);
127-
auto NewCvt = B.createConvertEscapeToNoEscape(
128-
Cvt->getLoc(), Cvt->getOperand(), Cvt->getType(), true);
129-
Cvt->replaceAllUsesWith(NewCvt);
130-
Cvt->eraseFromParent();
131-
Cvt = NewCvt;
126+
Cvt->setLifetimeGuaranteed();
132127

133128
// If our Cvt is in the initial block, we do not need to use the SSA updater
134129
// since we know Cvt can not be in a loop and must dominate all exits
@@ -417,14 +412,7 @@ static bool tryExtendLifetimeToLastUse(
417412
// Insert a copy at the convert_escape_to_noescape [not_guaranteed] and
418413
// change the instruction to the guaranteed form.
419414
auto EscapingClosure = Cvt->getOperand();
420-
{
421-
SILBuilderWithScope B(Cvt);
422-
auto NewCvt = B.createConvertEscapeToNoEscape(
423-
Cvt->getLoc(), Cvt->getOperand(), Cvt->getType(), true);
424-
Cvt->replaceAllUsesWith(NewCvt);
425-
Cvt->eraseFromParent();
426-
Cvt = NewCvt;
427-
}
415+
Cvt->setLifetimeGuaranteed();
428416

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

513-
// Replace the convert_escape_to_noescape instruction.
514-
{
515-
SILBuilderWithScope B(Cvt);
516-
auto NewCvt = B.createConvertEscapeToNoEscape(
517-
Cvt->getLoc(), Cvt->getOperand(), Cvt->getType(), true);
518-
Cvt->replaceAllUsesWith(NewCvt);
519-
Cvt->eraseFromParent();
520-
}
501+
Cvt->setLifetimeGuaranteed();
521502

522503
// Extend the lifetime.
523504
SILBuilderWithScope B(SwitchEnum1);

0 commit comments

Comments
 (0)