Skip to content

Commit 765208b

Browse files
[mlir] Make remove-dead-values remove block and successorOperands before delete ops (#166766)
Reland #165725, fix the Failed test by removing successor operands before delete operations. Following the deletion of cond.branch, its successor operands will subsequently be removed.
1 parent b39a9db commit 765208b

File tree

2 files changed

+60
-47
lines changed

2 files changed

+60
-47
lines changed

mlir/lib/Transforms/RemoveDeadValues.cpp

Lines changed: 47 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -742,7 +742,49 @@ static void processBranchOp(BranchOpInterface branchOp, RunLivenessAnalysis &la,
742742
static void cleanUpDeadVals(RDVFinalCleanupList &list) {
743743
LDBG() << "Starting cleanup of dead values...";
744744

745-
// 1. Operations
745+
// 1. Blocks, We must remove the block arguments and successor operands before
746+
// deleting the operation, as they may reside in the region operation.
747+
LDBG() << "Cleaning up " << list.blocks.size() << " block argument lists";
748+
for (auto &b : list.blocks) {
749+
// blocks that are accessed via multiple codepaths processed once
750+
if (b.b->getNumArguments() != b.nonLiveArgs.size())
751+
continue;
752+
LDBG() << "Erasing " << b.nonLiveArgs.count()
753+
<< " non-live arguments from block: " << b.b;
754+
// it iterates backwards because erase invalidates all successor indexes
755+
for (int i = b.nonLiveArgs.size() - 1; i >= 0; --i) {
756+
if (!b.nonLiveArgs[i])
757+
continue;
758+
LDBG() << " Erasing block argument " << i << ": " << b.b->getArgument(i);
759+
b.b->getArgument(i).dropAllUses();
760+
b.b->eraseArgument(i);
761+
}
762+
}
763+
764+
// 2. Successor Operands
765+
LDBG() << "Cleaning up " << list.successorOperands.size()
766+
<< " successor operand lists";
767+
for (auto &op : list.successorOperands) {
768+
SuccessorOperands successorOperands =
769+
op.branch.getSuccessorOperands(op.successorIndex);
770+
// blocks that are accessed via multiple codepaths processed once
771+
if (successorOperands.size() != op.nonLiveOperands.size())
772+
continue;
773+
LDBG() << "Erasing " << op.nonLiveOperands.count()
774+
<< " non-live successor operands from successor "
775+
<< op.successorIndex << " of branch: "
776+
<< OpWithFlags(op.branch, OpPrintingFlags().skipRegions());
777+
// it iterates backwards because erase invalidates all successor indexes
778+
for (int i = successorOperands.size() - 1; i >= 0; --i) {
779+
if (!op.nonLiveOperands[i])
780+
continue;
781+
LDBG() << " Erasing successor operand " << i << ": "
782+
<< successorOperands[i];
783+
successorOperands.erase(i);
784+
}
785+
}
786+
787+
// 3. Operations
746788
LDBG() << "Cleaning up " << list.operations.size() << " operations";
747789
for (auto &op : list.operations) {
748790
LDBG() << "Erasing operation: "
@@ -751,14 +793,14 @@ static void cleanUpDeadVals(RDVFinalCleanupList &list) {
751793
op->erase();
752794
}
753795

754-
// 2. Values
796+
// 4. Values
755797
LDBG() << "Cleaning up " << list.values.size() << " values";
756798
for (auto &v : list.values) {
757799
LDBG() << "Dropping all uses of value: " << v;
758800
v.dropAllUses();
759801
}
760802

761-
// 3. Functions
803+
// 5. Functions
762804
LDBG() << "Cleaning up " << list.functions.size() << " functions";
763805
// Record which function arguments were erased so we can shrink call-site
764806
// argument segments for CallOpInterface operations (e.g. ops using
@@ -780,7 +822,7 @@ static void cleanUpDeadVals(RDVFinalCleanupList &list) {
780822
(void)f.funcOp.eraseResults(f.nonLiveRets);
781823
}
782824

783-
// 4. Operands
825+
// 6. Operands
784826
LDBG() << "Cleaning up " << list.operands.size() << " operand lists";
785827
for (OperationToCleanup &o : list.operands) {
786828
// Handle call-specific cleanup only when we have a cached callee reference.
@@ -822,56 +864,14 @@ static void cleanUpDeadVals(RDVFinalCleanupList &list) {
822864
}
823865
}
824866

825-
// 5. Results
867+
// 7. Results
826868
LDBG() << "Cleaning up " << list.results.size() << " result lists";
827869
for (auto &r : list.results) {
828870
LDBG() << "Erasing " << r.nonLive.count()
829871
<< " non-live results from operation: "
830872
<< OpWithFlags(r.op, OpPrintingFlags().skipRegions());
831873
dropUsesAndEraseResults(r.op, r.nonLive);
832874
}
833-
834-
// 6. Blocks
835-
LDBG() << "Cleaning up " << list.blocks.size() << " block argument lists";
836-
for (auto &b : list.blocks) {
837-
// blocks that are accessed via multiple codepaths processed once
838-
if (b.b->getNumArguments() != b.nonLiveArgs.size())
839-
continue;
840-
LDBG() << "Erasing " << b.nonLiveArgs.count()
841-
<< " non-live arguments from block: " << b.b;
842-
// it iterates backwards because erase invalidates all successor indexes
843-
for (int i = b.nonLiveArgs.size() - 1; i >= 0; --i) {
844-
if (!b.nonLiveArgs[i])
845-
continue;
846-
LDBG() << " Erasing block argument " << i << ": " << b.b->getArgument(i);
847-
b.b->getArgument(i).dropAllUses();
848-
b.b->eraseArgument(i);
849-
}
850-
}
851-
852-
// 7. Successor Operands
853-
LDBG() << "Cleaning up " << list.successorOperands.size()
854-
<< " successor operand lists";
855-
for (auto &op : list.successorOperands) {
856-
SuccessorOperands successorOperands =
857-
op.branch.getSuccessorOperands(op.successorIndex);
858-
// blocks that are accessed via multiple codepaths processed once
859-
if (successorOperands.size() != op.nonLiveOperands.size())
860-
continue;
861-
LDBG() << "Erasing " << op.nonLiveOperands.count()
862-
<< " non-live successor operands from successor "
863-
<< op.successorIndex << " of branch: "
864-
<< OpWithFlags(op.branch, OpPrintingFlags().skipRegions());
865-
// it iterates backwards because erase invalidates all successor indexes
866-
for (int i = successorOperands.size() - 1; i >= 0; --i) {
867-
if (!op.nonLiveOperands[i])
868-
continue;
869-
LDBG() << " Erasing successor operand " << i << ": "
870-
<< successorOperands[i];
871-
successorOperands.erase(i);
872-
}
873-
}
874-
875875
LDBG() << "Finished cleanup of dead values";
876876
}
877877

mlir/test/Transforms/remove-dead-values.mlir

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -674,3 +674,16 @@ func.func @dead_value_loop_ivs_no_result(%lb: index, %ub: index, %step: index, %
674674
}
675675
return
676676
}
677+
678+
// -----
679+
680+
// CHECK-LABEL: func @op_block_have_dead_arg
681+
func.func @op_block_have_dead_arg(%arg0: index, %arg1: index, %arg2: i1) {
682+
scf.execute_region {
683+
cf.cond_br %arg2, ^bb1(%arg0 : index), ^bb1(%arg1 : index)
684+
^bb1(%0: index):
685+
scf.yield
686+
}
687+
// CHECK-NEXT: return
688+
return
689+
}

0 commit comments

Comments
 (0)