Skip to content

Commit 452ae3b

Browse files
authored
Merge pull request #77568 from meg-gupta/dceupdate2
Insert end_borrow for dead phi operands only when required
2 parents cb6e3db + fd6d29d commit 452ae3b

File tree

2 files changed

+34
-15
lines changed

2 files changed

+34
-15
lines changed

lib/SILOptimizer/Transforms/DeadCodeElimination.cpp

Lines changed: 17 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -187,9 +187,8 @@ class DCE {
187187
llvm::SmallPtrSetImpl<SILBasicBlock *> &);
188188
SILBasicBlock *nearestUsefulPostDominator(SILBasicBlock *Block);
189189
void replaceBranchWithJump(SILInstruction *Inst, SILBasicBlock *Block);
190-
/// If \p value is live, insert a lifetime ending operation in ossa.
191-
/// destroy_value for @owned value and end_borrow for a @guaranteed value.
192-
void endLifetimeOfLiveValue(SILValue value, SILInstruction *insertPt);
190+
/// Insert lifetime ending instruction if defining value of \p op is live.
191+
void endLifetimeOfLiveValue(Operand *op, SILInstruction *insertPt);
193192

194193
public:
195194
DCE(SILFunction *F, PostDominanceInfo *PDT)
@@ -585,23 +584,24 @@ void DCE::replaceBranchWithJump(SILInstruction *Inst, SILBasicBlock *Block) {
585584
(void)Branch;
586585
}
587586

588-
void DCE::endLifetimeOfLiveValue(SILValue value, SILInstruction *insertPt) {
587+
void DCE::endLifetimeOfLiveValue(Operand *op, SILInstruction *insertPt) {
588+
auto value = op->get();
589589
if (SILInstruction *inst = value->getDefiningInstruction()) {
590590
if (!LiveInstructions.contains(inst))
591591
return;
592592
} else if (auto *arg = dyn_cast<SILArgument>(value)) {
593593
if (!LiveArguments.contains(arg))
594594
return;
595595
}
596+
597+
assert(op->isLifetimeEnding());
596598
SILBuilderWithScope builder(insertPt);
597599
if (value->getOwnershipKind() == OwnershipKind::Owned) {
598-
builder.emitDestroyOperation(RegularLocation::getAutoGeneratedLocation(),
599-
value);
600+
builder.createDestroyValue(RegularLocation::getAutoGeneratedLocation(),
601+
value);
600602
}
601-
BorrowedValue borrow(lookThroughBorrowedFromDef(value));
602-
if (borrow && borrow.isLocalScope()) {
603-
builder.emitEndBorrowOperation(RegularLocation::getAutoGeneratedLocation(),
604-
value);
603+
if (value->getOwnershipKind() == OwnershipKind::Guaranteed) {
604+
builder.createEndBorrow(RegularLocation::getAutoGeneratedLocation(), value);
605605
}
606606
}
607607

@@ -657,7 +657,7 @@ bool DCE::removeDead() {
657657
for (auto *pred : BB.getPredecessorBlocks()) {
658658
auto *predTerm = pred->getTerminator();
659659
SILInstruction *insertPt = predTerm;
660-
if (phiArg->getOwnershipKind() == OwnershipKind::Guaranteed) {
660+
if (phiArg->isReborrow()) {
661661
// If the phiArg is dead and had reborrow dependencies, its baseValue
662662
// may also have been dead and a destroy_value of its baseValue may
663663
// have been inserted before the pred's terminator. Make sure to
@@ -677,8 +677,10 @@ bool DCE::removeDead() {
677677
insertPt = &predInst;
678678
}
679679
}
680-
681-
endLifetimeOfLiveValue(phiArg->getIncomingPhiValue(pred), insertPt);
680+
auto *predOp = phiArg->getIncomingPhiOperand(pred);
681+
if (predOp->isLifetimeEnding()) {
682+
endLifetimeOfLiveValue(predOp, insertPt);
683+
}
682684
}
683685
erasePhiArgument(&BB, i, /*cleanupDeadPhiOps=*/true,
684686
InstModCallbacks().onCreateNewInst(
@@ -702,7 +704,7 @@ bool DCE::removeDead() {
702704

703705
for (auto &op : termInst->getAllOperands()) {
704706
if (op.isLifetimeEnding()) {
705-
endLifetimeOfLiveValue(op.get(), termInst);
707+
endLifetimeOfLiveValue(&op, termInst);
706708
}
707709
}
708710
LLVM_DEBUG(llvm::dbgs() << "Replacing branch: ");
@@ -725,7 +727,7 @@ bool DCE::removeDead() {
725727
if (F->hasOwnership()) {
726728
for (auto &Op : Inst->getAllOperands()) {
727729
if (Op.isLifetimeEnding()) {
728-
endLifetimeOfLiveValue(Op.get(), Inst);
730+
endLifetimeOfLiveValue(&Op, Inst);
729731
}
730732
}
731733
}

test/SILOptimizer/dead_code_elimination_nontrivial_ossa.sil

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1220,3 +1220,20 @@ bb0:
12201220
return %t : $()
12211221
}
12221222

1223+
// CHECK-LABEL: sil [ossa] @dce_dead_guaranteedphi :
1224+
// bb2:
1225+
// CHECK-LABEL: } // end sil function 'dce_dead_guaranteedphi'
1226+
sil [ossa] @dce_dead_guaranteedphi : $@convention(thin) (@owned NonTrivialStruct) -> () {
1227+
bb0(%0 : @owned $NonTrivialStruct):
1228+
%b = begin_borrow %0 : $NonTrivialStruct
1229+
%func = function_ref @$use_nontrivialstruct2 : $@convention(thin) (@guaranteed NonTrivialStruct) -> ()
1230+
%funcres = apply %func(%b) : $@convention(thin) (@guaranteed NonTrivialStruct) -> ()
1231+
br bb2(%b : $NonTrivialStruct, %b : $NonTrivialStruct)
1232+
1233+
bb2(%phi1 : @guaranteed $NonTrivialStruct, %phi2 : @guaranteed $NonTrivialStruct):
1234+
end_borrow %phi1 : $NonTrivialStruct
1235+
destroy_value %0 : $NonTrivialStruct
1236+
%9999 = tuple()
1237+
return %9999 : $()
1238+
}
1239+

0 commit comments

Comments
 (0)