[Mlir-commits] [mlir] [mlir] Refactor transform.apply_dce into a eliminateTriviallyDeadOps region-utils helper (PR #194041)

Matthias Springer llvmlistbot at llvm.org
Wed Apr 29 05:53:21 PDT 2026


================
@@ -506,6 +507,78 @@ LogicalResult mlir::runRegionDCE(RewriterBase &rewriter,
   return deleteDeadness(rewriter, regions, liveMap);
 }
 
+bool mlir::eliminateTriviallyDeadOps(RewriterBase &rewriter, Region &region,
+                                     bool includeNestedRegions) {
+  bool changed = false;
+
+  // Step 1: walk each op in reverse program order. If the op is already
+  // trivially dead, erase it outright — there's no point recursing into
+  // regions that will be destroyed with it. Otherwise, if
+  // `includeNestedRegions` is set, recurse into its nested regions so values
+  // defined in `region` may lose their last user and show up as dead in
+  // step 2's seed. Reverse iteration lets dead chains propagate within this
+  // single pass.
+  for (Block &block : llvm::reverse(region)) {
----------------
matthias-springer wrote:

Yes. I thought of cases like [this](https://github.com/llvm/llvm-project/pull/194041/changes#diff-f7004976c66153e2dbb66ae257f116016c5bd9391a8d80abe0eb04a39359437cR72) one. You have to visit `bb1` before `bb2`. Here it seems to work out because dead ops are already removed during Step 1. But I suspect that when you add yet another block, you can construct an example where the old implementation in `TransformOps.cpp` was able to DCE but the new one is not.


https://github.com/llvm/llvm-project/pull/194041


More information about the Mlir-commits mailing list