Skip to content

Commit c1e0a1d

Browse files
committed
Fix assertion in InstructionDeleter::trackIfDead.
This assertion was triggering when deleting scope-ending operations whose operand had been replaced with an undef: end_borrow undef Related to rdar://156431548 This was triggered during simplify-cfg by after adding blocks to the worklist, then later cleaning up dead instructions in the worklist blocks. I'm unable to create a test case based on simplify-cfg because the assertion is completely unrelated to the optimization that adds blocks to the worklist.
1 parent ab80919 commit c1e0a1d

File tree

2 files changed

+31
-3
lines changed

2 files changed

+31
-3
lines changed

lib/SILOptimizer/Utils/InstructionDeleter.cpp

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -187,9 +187,8 @@ bool InstructionDeleter::trackIfDead(SILInstruction *inst) {
187187
bool fixLifetime = inst->getFunction()->hasOwnership();
188188
if (isInstructionTriviallyDead(inst)
189189
|| isScopeAffectingInstructionDead(inst, fixLifetime)) {
190-
assert(!isIncidentalUse(inst) &&
191-
(!isa<DestroyValueInst>(inst) ||
192-
canTriviallyDeleteOSSAEndScopeInst(inst)) &&
190+
assert(!isIncidentalUse(inst)
191+
|| canTriviallyDeleteOSSAEndScopeInst(inst) &&
193192
"Incidental uses cannot be removed in isolation. "
194193
"They would be removed iff the operand is dead");
195194
getCallbacks().notifyWillBeDeleted(inst);
@@ -356,6 +355,20 @@ static FunctionTest DeleterDeleteIfDeadTest(
356355
llvm::outs() << "deleteIfDead returned " << deleted << "\n";
357356
function.print(llvm::outs());
358357
});
358+
359+
// Arguments:
360+
// - instruction: the instruction to delete
361+
// Dumps:
362+
// - the function
363+
static FunctionTest DeleterTrackIfDeadTest(
364+
"deleter_track_if_dead", [](auto &function, auto &arguments, auto &test) {
365+
auto *inst = arguments.takeInstruction();
366+
InstructionDeleter deleter;
367+
llvm::outs() << "Tracking " << *inst;
368+
deleter.trackIfDead(inst);
369+
deleter.cleanupDeadInstructions();
370+
function.print(llvm::outs());
371+
});
359372
} // namespace swift::test
360373

361374
void InstructionDeleter::forceDeleteAndFixLifetimes(SILInstruction *inst) {

test/SILOptimizer/instruction_deleter.sil

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,3 +57,18 @@ sil [ossa] @doDeleteLoadTake : $() -> () {
5757
%retval = tuple ()
5858
return %retval : $()
5959
}
60+
61+
// CHECK-LABEL: begin running test {{.*}} on trackEndBorrow
62+
// CHECK: Tracking end_borrow undef : $C
63+
// CHECK: sil [ossa] @trackEndBorrow : $@convention(thin) () -> () {
64+
// CHECK: bb0:
65+
// CHECK-NOT: end_borrow
66+
// CHECK-LABEL: } // end sil function 'trackEndBorrow'
67+
// CHECK-LABEL: end running test {{.*}} on trackEndBorrow
68+
sil [ossa] @trackEndBorrow : $() -> () {
69+
bb0:
70+
specify_test "deleter_track_if_dead @instruction"
71+
end_borrow undef : $C
72+
%retval = tuple ()
73+
return %retval : $()
74+
}

0 commit comments

Comments
 (0)