Skip to content

Commit 0782241

Browse files
Make-remove-dead-values-remove-block-first
1 parent 466c526 commit 0782241

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
@@ -731,7 +731,25 @@ static void processBranchOp(BranchOpInterface branchOp, RunLivenessAnalysis &la,
731731
static void cleanUpDeadVals(RDVFinalCleanupList &list) {
732732
LDBG() << "Starting cleanup of dead values...";
733733

734-
// 1. Operations
734+
// 1. Blocks
735+
LDBG() << "Cleaning up " << list.blocks.size() << " block argument lists";
736+
for (auto &b : list.blocks) {
737+
// blocks that are accessed via multiple codepaths processed once
738+
if (b.b->getNumArguments() != b.nonLiveArgs.size())
739+
continue;
740+
LDBG() << "Erasing " << b.nonLiveArgs.count()
741+
<< " non-live arguments from block: " << b.b;
742+
// it iterates backwards because erase invalidates all successor indexes
743+
for (int i = b.nonLiveArgs.size() - 1; i >= 0; --i) {
744+
if (!b.nonLiveArgs[i])
745+
continue;
746+
LDBG() << " Erasing block argument " << i << ": " << b.b->getArgument(i);
747+
b.b->getArgument(i).dropAllUses();
748+
b.b->eraseArgument(i);
749+
}
750+
}
751+
752+
// 2. Operations
735753
LDBG() << "Cleaning up " << list.operations.size() << " operations";
736754
for (auto &op : list.operations) {
737755
LDBG() << "Erasing operation: "
@@ -740,14 +758,14 @@ static void cleanUpDeadVals(RDVFinalCleanupList &list) {
740758
op->erase();
741759
}
742760

743-
// 2. Values
761+
// 3. Values
744762
LDBG() << "Cleaning up " << list.values.size() << " values";
745763
for (auto &v : list.values) {
746764
LDBG() << "Dropping all uses of value: " << v;
747765
v.dropAllUses();
748766
}
749767

750-
// 3. Functions
768+
// 4. Functions
751769
LDBG() << "Cleaning up " << list.functions.size() << " functions";
752770
// Record which function arguments were erased so we can shrink call-site
753771
// argument segments for CallOpInterface operations (e.g. ops using
@@ -769,7 +787,7 @@ static void cleanUpDeadVals(RDVFinalCleanupList &list) {
769787
(void)f.funcOp.eraseResults(f.nonLiveRets);
770788
}
771789

772-
// 4. Operands
790+
// 5. Operands
773791
LDBG() << "Cleaning up " << list.operands.size() << " operand lists";
774792
for (OperationToCleanup &o : list.operands) {
775793
// Handle call-specific cleanup only when we have a cached callee reference.
@@ -811,7 +829,7 @@ static void cleanUpDeadVals(RDVFinalCleanupList &list) {
811829
}
812830
}
813831

814-
// 5. Results
832+
// 6. Results
815833
LDBG() << "Cleaning up " << list.results.size() << " result lists";
816834
for (auto &r : list.results) {
817835
LDBG() << "Erasing " << r.nonLive.count()
@@ -820,24 +838,6 @@ static void cleanUpDeadVals(RDVFinalCleanupList &list) {
820838
dropUsesAndEraseResults(r.op, r.nonLive);
821839
}
822840

823-
// 6. Blocks
824-
LDBG() << "Cleaning up " << list.blocks.size() << " block argument lists";
825-
for (auto &b : list.blocks) {
826-
// blocks that are accessed via multiple codepaths processed once
827-
if (b.b->getNumArguments() != b.nonLiveArgs.size())
828-
continue;
829-
LDBG() << "Erasing " << b.nonLiveArgs.count()
830-
<< " non-live arguments from block: " << b.b;
831-
// it iterates backwards because erase invalidates all successor indexes
832-
for (int i = b.nonLiveArgs.size() - 1; i >= 0; --i) {
833-
if (!b.nonLiveArgs[i])
834-
continue;
835-
LDBG() << " Erasing block argument " << i << ": " << b.b->getArgument(i);
836-
b.b->getArgument(i).dropAllUses();
837-
b.b->eraseArgument(i);
838-
}
839-
}
840-
841841
// 7. Successor Operands
842842
LDBG() << "Cleaning up " << list.successorOperands.size()
843843
<< " 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: i64, %arg1: i64, %arg2: i64, %arg3: i1) {
682+
omp.wsloop {
683+
omp.loop_nest (%arg4) : i64 = (%arg0) to (%arg1) step (%arg2) {
684+
cf.cond_br %arg3, ^bb1(%arg0 : i64), ^bb1(%arg1 : i64)
685+
^bb1(%0: i64):
686+
omp.yield
687+
}
688+
}
689+
// CHECK-NEXT: return
690+
return
691+
}

0 commit comments

Comments
 (0)