Skip to content

Commit 3050542

Browse files
committed
Add a scary warning comment to eliminateSimpleCopies
We're getting lucky that this utility isn't breaking people's code.
1 parent 96cac92 commit 3050542

File tree

1 file changed

+14
-0
lines changed

1 file changed

+14
-0
lines changed

lib/SILOptimizer/Utils/CanonicalizeInstruction.cpp

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -410,6 +410,18 @@ broadenSingleElementStores(StoreInst *storeInst,
410410
// Simple ARC Peepholes
411411
//===----------------------------------------------------------------------===//
412412

413+
/// "dead" copies are removed in OSSA, but this may shorten object lifetimes,
414+
/// changing program semantics in unexpected ways by releasing weak references
415+
/// and running deinitializers early. This copy may be the only thing keeping a
416+
/// variable's reference alive. But just because the copy's current SSA value
417+
/// contains no other uses does not mean that there aren't other uses that still
418+
/// correspond to the original variable whose lifetime is protected by this
419+
/// copy. The only way to guarantee the lifetime of a variable is to use a
420+
/// borrow scope--copy/destroy is insufficient by itself.
421+
///
422+
/// FIXME: Technically this should be guarded by a compiler flag like
423+
/// -enable-copy-propagation until SILGen protects scoped variables by borrow
424+
/// scopes.
413425
static SILBasicBlock::iterator
414426
eliminateSimpleCopies(CopyValueInst *cvi, CanonicalizeInstruction &pass) {
415427
auto next = std::next(cvi->getIterator());
@@ -432,6 +444,8 @@ eliminateSimpleCopies(CopyValueInst *cvi, CanonicalizeInstruction &pass) {
432444
return next;
433445
}
434446

447+
/// Unlike dead copy elimination, dead borrows can be safely removed because the
448+
/// semantics of a borrow scope
435449
static SILBasicBlock::iterator
436450
eliminateSimpleBorrows(BeginBorrowInst *bbi, CanonicalizeInstruction &pass) {
437451
auto next = std::next(bbi->getIterator());

0 commit comments

Comments
 (0)