[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.
This commit is contained in:
Nate Chandler
2021-12-07 16:59:54 -08:00
parent f0bd5839a2
commit ac50bb7a7c

View File

@@ -445,9 +445,13 @@ static SILBasicBlock::iterator
eliminateSimpleBorrows(BeginBorrowInst *bbi, CanonicalizeInstruction &pass) {
auto next = std::next(bbi->getIterator());
// Never eliminate lexical borrow scopes. They must be kept to ensure that
// value lifetimes aren't observably shortened.
if (bbi->isLexical())
// Lexical borrow scopes can only be eliminated under certain circumstances:
// (1) They can never be eliminated if the module is in the raw stage, because
// they may be needed for diagnostic.
// (2) They can never be eliminated if there is no enclosing lexical scope
// which guarantees the lifetime of the value.
if (bbi->isLexical() && (bbi->getModule().getStage() == SILStage::Raw ||
!isNestedLexicalBeginBorrow(bbi)))
return next;
// We know that our borrow is completely within the lifetime of its base value