Skip to content

Commit a928c61

Browse files
[mlir] Make remove-dead-values pass remove blocks arguments first (llvm#165725)
Fix llvm#163051. Some Ops which have multiple blocks, before deleting the ops, first remove the dead parameters within its blocks.
1 parent 9e6a31f commit a928c61

File tree

2 files changed

+38
-23
lines changed

2 files changed

+38
-23
lines changed

mlir/lib/Transforms/RemoveDeadValues.cpp

Lines changed: 23 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -742,7 +742,25 @@ 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
746+
LDBG() << "Cleaning up " << list.blocks.size() << " block argument lists";
747+
for (auto &b : list.blocks) {
748+
// blocks that are accessed via multiple codepaths processed once
749+
if (b.b->getNumArguments() != b.nonLiveArgs.size())
750+
continue;
751+
LDBG() << "Erasing " << b.nonLiveArgs.count()
752+
<< " non-live arguments from block: " << b.b;
753+
// it iterates backwards because erase invalidates all successor indexes
754+
for (int i = b.nonLiveArgs.size() - 1; i >= 0; --i) {
755+
if (!b.nonLiveArgs[i])
756+
continue;
757+
LDBG() << " Erasing block argument " << i << ": " << b.b->getArgument(i);
758+
b.b->getArgument(i).dropAllUses();
759+
b.b->eraseArgument(i);
760+
}
761+
}
762+
763+
// 2. Operations
746764
LDBG() << "Cleaning up " << list.operations.size() << " operations";
747765
for (auto &op : list.operations) {
748766
LDBG() << "Erasing operation: "
@@ -751,14 +769,14 @@ static void cleanUpDeadVals(RDVFinalCleanupList &list) {
751769
op->erase();
752770
}
753771

754-
// 2. Values
772+
// 3. Values
755773
LDBG() << "Cleaning up " << list.values.size() << " values";
756774
for (auto &v : list.values) {
757775
LDBG() << "Dropping all uses of value: " << v;
758776
v.dropAllUses();
759777
}
760778

761-
// 3. Functions
779+
// 4. Functions
762780
LDBG() << "Cleaning up " << list.functions.size() << " functions";
763781
// Record which function arguments were erased so we can shrink call-site
764782
// argument segments for CallOpInterface operations (e.g. ops using
@@ -780,7 +798,7 @@ static void cleanUpDeadVals(RDVFinalCleanupList &list) {
780798
(void)f.funcOp.eraseResults(f.nonLiveRets);
781799
}
782800

783-
// 4. Operands
801+
// 5. Operands
784802
LDBG() << "Cleaning up " << list.operands.size() << " operand lists";
785803
for (OperationToCleanup &o : list.operands) {
786804
// Handle call-specific cleanup only when we have a cached callee reference.
@@ -822,7 +840,7 @@ static void cleanUpDeadVals(RDVFinalCleanupList &list) {
822840
}
823841
}
824842

825-
// 5. Results
843+
// 6. Results
826844
LDBG() << "Cleaning up " << list.results.size() << " result lists";
827845
for (auto &r : list.results) {
828846
LDBG() << "Erasing " << r.nonLive.count()
@@ -831,24 +849,6 @@ static void cleanUpDeadVals(RDVFinalCleanupList &list) {
831849
dropUsesAndEraseResults(r.op, r.nonLive);
832850
}
833851

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-
852852
// 7. Successor Operands
853853
LDBG() << "Cleaning up " << list.successorOperands.size()
854854
<< " successor operand lists";

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

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -674,3 +674,18 @@ 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: index, %arg3: i1) {
682+
scf.for %iv = %arg0 to %arg1 step %arg2 {
683+
scf.execute_region {
684+
cf.cond_br %arg3, ^bb1(%arg0 : index), ^bb1(%arg1 : index)
685+
^bb1(%0: index):
686+
scf.yield
687+
}
688+
}
689+
// CHECK-NEXT: return
690+
return
691+
}

0 commit comments

Comments
 (0)