[Mlir-commits] [mlir] [MLIR] Refactor DCE helper to expose worklist entry-point, and use this helper in CSE (PR #195636)
lonely eagle
llvmlistbot at llvm.org
Mon May 4 07:44:45 PDT 2026
================
@@ -510,6 +510,74 @@ LogicalResult mlir::runRegionDCE(RewriterBase &rewriter,
return deleteDeadness(rewriter, regions, liveMap);
}
+int64_t mlir::eliminateTriviallyDeadOps(
+ RewriterBase &rewriter, Region ®ion, SmallVector<Operation *> &worklist,
+ DenseSet<Operation *> &visited, function_ref<void(Operation *)> preErase) {
+ LDBG(2) << "Initial worklist size: " << worklist.size();
+ int64_t numErased = 0;
+ while (!worklist.empty()) {
+ Operation *op = worklist.pop_back_val();
+ LDBG(2) << "Popped operation from worklist: "
+ << OpWithFlags(op, OpPrintingFlags().skipRegions());
+ /// Erase each operand to drop its use count before checking its defining
+ /// op: by the time we call isOpTriviallyDead on defOp, the
+ /// about-to-be-erased `op` is no longer counted as a user. Only
+ /// actually-dead ops enter the worklist.
+ ///
+ /// Walk nested operations as well because erasing `op` also implicitly
+ /// erases every operation nested under it and therefore drops their operand
+ /// uses.
+ op->walk([&](Operation *erasedOp) {
+ LDBG(3) << "Processing operands of operation erased: "
+ << OpWithFlags(erasedOp, OpPrintingFlags().skipRegions());
+ for (OpOperand &opOperand : erasedOp->getOpOperands()) {
+ Operation *defOp = opOperand.get().getDefiningOp();
+ if (!defOp) {
+ LDBG(4) << "Skipping operand #" << opOperand.getOperandNumber()
+ << ": value has no defining operation";
+ continue;
+ }
+ if (defOp->getParentRegion() != ®ion) {
----------------
linuxlonelyeagle wrote:
You can see this
```
module {
func.func @materialization(%arg0: i32) -> i32 {
%c0_i32 = arith.constant 0 : i32
%0 = affine.for %arg1 = 0 to 10 iter_args(%arg2 = %arg0) -> (i32) {
%1 = arith.addi %c0_i32, %c0_i32 : i32
%2 = arith.muli %arg2, %arg2 : i32
affine.yield %2 : i32
}
return %0 : i32
}
}
```
https://github.com/llvm/llvm-project/pull/195636
More information about the Mlir-commits
mailing list