Skip to content

Commit ac50bb7

Browse files
committed
[CanonicalizeInst] Remove redundant lexical bbis.
Previously, CanonicalizeInstruction::eliminateSimpleBorrows bailed out when encountering any any [lexical] begin_borrow. While generally such borrow scopes must be preserved, they may be removed if they are redundant (when the borrowed value is guaranteed by another means: an outer lexical borrow scope or a guaranteed function argument). Here, removing such redundant lexical borrow scopes is enabled.
1 parent f0bd583 commit ac50bb7

File tree

1 file changed

+7
-3
lines changed

1 file changed

+7
-3
lines changed

lib/SILOptimizer/Utils/CanonicalizeInstruction.cpp

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -445,9 +445,13 @@ static SILBasicBlock::iterator
445445
eliminateSimpleBorrows(BeginBorrowInst *bbi, CanonicalizeInstruction &pass) {
446446
auto next = std::next(bbi->getIterator());
447447

448-
// Never eliminate lexical borrow scopes. They must be kept to ensure that
449-
// value lifetimes aren't observably shortened.
450-
if (bbi->isLexical())
448+
// Lexical borrow scopes can only be eliminated under certain circumstances:
449+
// (1) They can never be eliminated if the module is in the raw stage, because
450+
// they may be needed for diagnostic.
451+
// (2) They can never be eliminated if there is no enclosing lexical scope
452+
// which guarantees the lifetime of the value.
453+
if (bbi->isLexical() && (bbi->getModule().getStage() == SILStage::Raw ||
454+
!isNestedLexicalBeginBorrow(bbi)))
451455
return next;
452456

453457
// We know that our borrow is completely within the lifetime of its base value

0 commit comments

Comments
 (0)