Skip to content

Commit 9d73a19

Browse files
committed
ClosureLifetimeFixup : Ensure borrow scope is inserted on both none/some
path for an optional type.
1 parent bbb6516 commit 9d73a19

File tree

2 files changed

+11
-9
lines changed

2 files changed

+11
-9
lines changed

lib/SILOptimizer/Mandatory/ClosureLifetimeFixup.cpp

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -286,17 +286,13 @@ static void extendLifetimeToEndOfFunction(SILFunction &fn,
286286
cvtUser->setOperand(cvtUse->getOperandNumber(), mdi);
287287
}
288288

289-
auto fixupSILForLifetimeExtension = [&](SILValue value) {
289+
auto fixupSILForLifetimeExtension = [&](SILValue value, SILValue entryValue) {
290290
// Use SSAUpdater to find insertion points for lifetime ends.
291291
updater.initialize(optionalEscapingClosureTy, value->getOwnershipKind());
292292
SmallVector<SILPhiArgument *, 8> insertedPhis;
293293
updater.setInsertedPhis(&insertedPhis);
294294

295-
// Create an optional none at the function entry.
296-
auto *optionalNone =
297-
SILBuilderWithScope(fn.getEntryBlock()->begin())
298-
.createOptionalNone(loc, optionalEscapingClosureTy);
299-
updater.addAvailableValue(fn.getEntryBlock(), optionalNone);
295+
updater.addAvailableValue(fn.getEntryBlock(), entryValue);
300296
updater.addAvailableValue(value->getParentBlock(), value);
301297
{
302298
// Since value maybe in a loop, insert an extra lifetime end. Since we
@@ -316,12 +312,17 @@ static void extendLifetimeToEndOfFunction(SILFunction &fn,
316312
// TODO: Should we sort inserted phis before or after we initialize
317313
// the worklist or maybe backwards? We should investigate how the
318314
// SSA updater adds phi nodes to this list to resolve this question.
319-
cleanupDeadTrivialPhiArgs(optionalNone, insertedPhis);
315+
cleanupDeadTrivialPhiArgs(entryValue, insertedPhis);
320316
};
321317

318+
// Create an optional none at the function entry.
319+
auto *optionalNone = SILBuilderWithScope(fn.getEntryBlock()->begin())
320+
.createOptionalNone(loc, optionalEscapingClosureTy);
321+
auto *borrowNone = SILBuilderWithScope(optionalNone->getNextInstruction())
322+
.createBeginBorrow(loc, optionalNone);
322323
// Use the SSAUpdater to create lifetime ends for the copy and the borrow.
323-
fixupSILForLifetimeExtension(borrow);
324-
fixupSILForLifetimeExtension(optionalSome);
324+
fixupSILForLifetimeExtension(borrow, borrowNone);
325+
fixupSILForLifetimeExtension(optionalSome, optionalNone);
325326
}
326327

327328
static SILInstruction *lookThroughRebastractionUsers(

test/SILOptimizer/closure-lifetime-fixup.sil

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -211,6 +211,7 @@ bb3(%13 : $@noescape @callee_guaranteed () -> ()):
211211
// CHECK-LABEL: sil [ossa] @test_lifetime_extension_until_function_exit :
212212
// CHECK: [[NONE1:%.*]] = enum $Optional<@callee_guaranteed () -> ()>, #Optional.none!enumelt
213213
// CHECK: [[NONE2:%.*]] = enum $Optional<@callee_guaranteed () -> ()>, #Optional.none!enumelt
214+
// CHECK: [[BORROWNONE:%.*]] = begin_borrow [[NONE2]] : $Optional<@callee_guaranteed () -> ()>
214215
// CHECK: cond_br undef, bb1, bb2
215216
// CHECK: bb1:
216217
// CHECK: [[ORIG:%.*]] = function_ref @originalClosure : $@convention(thin) () -> ()

0 commit comments

Comments
 (0)