Skip to content

Commit c7fe393

Browse files
committed
In OSSA rauw utilities use auto generated location for creating copy_value/destroy_value etc
Branch instructions and frontiers can have LocationKind::ReturnKind/ImplicitReturnKind which are not correct locations to use for copy_value/destroy_value etc
1 parent 8f136f3 commit c7fe393

File tree

1 file changed

+20
-11
lines changed

1 file changed

+20
-11
lines changed

lib/SILOptimizer/Utils/OwnershipOptUtils.cpp

Lines changed: 20 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -221,8 +221,12 @@ OwnershipLifetimeExtender::copyBorrowAndExtendForRAUW(SILValue newValue,
221221
while (!frontier.empty()) {
222222
auto *insertPt = frontier.pop_back_val();
223223
SILBuilderWithScope frontierBuilder(insertPt);
224-
auto *ebi = frontierBuilder.createEndBorrow(insertPt->getLoc(), borrow);
225-
auto *dvi = frontierBuilder.createDestroyValue(insertPt->getLoc(), copy);
224+
// Use an auto-generated location here, because insertPt may have an
225+
// incompatible LocationKind
226+
auto loc = RegularLocation::getAutoGeneratedLocation(
227+
insertPt->getLoc().getSourceLoc());
228+
auto *ebi = frontierBuilder.createEndBorrow(loc, borrow);
229+
auto *dvi = frontierBuilder.createDestroyValue(loc, copy);
226230
if (ctx.newInstNotify) {
227231
ctx.newInstNotify(ebi);
228232
ctx.newInstNotify(dvi);
@@ -460,10 +464,13 @@ void OwnershipRAUWUtility::eliminateReborrowsOfRecursiveBorrows(
460464
SILValue value = borrowingOperand->get();
461465
auto *bi = cast<BranchInst>(borrowingOperand->getUser());
462466
SILBuilderWithScope reborrowBuilder(bi);
463-
auto *innerCopy = reborrowBuilder.createCopyValue(bi->getLoc(), value);
464-
auto *innerBorrow =
465-
reborrowBuilder.createBeginBorrow(bi->getLoc(), innerCopy);
466-
auto *outerEndBorrow = reborrowBuilder.createEndBorrow(bi->getLoc(), value);
467+
// Use an auto-generated location here, because the branch may have an
468+
// incompatible LocationKind
469+
auto loc =
470+
RegularLocation::getAutoGeneratedLocation(bi->getLoc().getSourceLoc());
471+
auto *innerCopy = reborrowBuilder.createCopyValue(loc, value);
472+
auto *innerBorrow = reborrowBuilder.createBeginBorrow(loc, innerCopy);
473+
auto *outerEndBorrow = reborrowBuilder.createEndBorrow(loc, value);
467474
if (ctx.newInstNotify) {
468475
ctx.newInstNotify(innerCopy);
469476
ctx.newInstNotify(innerBorrow);
@@ -526,12 +533,14 @@ void OwnershipRAUWUtility::rewriteReborrows(
526533
for (auto reborrow : foundReborrows) {
527534
auto *bi = cast<BranchInst>(reborrow.op->getUser());
528535
SILBuilderWithScope reborrowBuilder(bi);
529-
auto *innerCopy =
530-
reborrowBuilder.createCopyValue(bi->getLoc(), newBorrowedValue);
531-
auto *innerBorrow =
532-
reborrowBuilder.createBeginBorrow(bi->getLoc(), innerCopy);
536+
// Use an auto-generated location here, because the branch may have an
537+
// incompatible LocationKind
538+
auto loc =
539+
RegularLocation::getAutoGeneratedLocation(bi->getLoc().getSourceLoc());
540+
auto *innerCopy = reborrowBuilder.createCopyValue(loc, newBorrowedValue);
541+
auto *innerBorrow = reborrowBuilder.createBeginBorrow(loc, innerCopy);
533542
auto *outerEndBorrow =
534-
reborrowBuilder.createEndBorrow(bi->getLoc(), reborrow.op->get());
543+
reborrowBuilder.createEndBorrow(loc, reborrow.op->get());
535544
if (ctx.newInstNotify) {
536545
ctx.newInstNotify(innerCopy);
537546
ctx.newInstNotify(innerBorrow);

0 commit comments

Comments
 (0)