[Mlir-commits] [mlir] e79e056 - [mlir][CSE] Remove the opsToErase container and immediately delete dead ops. (#203702)

llvmlistbot at llvm.org llvmlistbot at llvm.org
Sun Jun 14 18:36:04 PDT 2026


Author: lonely eagle
Date: 2026-06-15T09:36:00+08:00
New Revision: e79e056ee982450ee75477d1b315b2f747d7790c

URL: https://github.com/llvm/llvm-project/commit/e79e056ee982450ee75477d1b315b2f747d7790c
DIFF: https://github.com/llvm/llvm-project/commit/e79e056ee982450ee75477d1b315b2f747d7790c.diff

LOG: [mlir][CSE] Remove the opsToErase container and immediately delete dead ops. (#203702)

This PR removes the `opsToErase` container and immediately erases dead
operations. Since dead ops are deleted on the fly, the value in the
`MemEffectsCache` map now correctly tracks the previous operation of
`toOp`. This change improves the storage efficiency of CSE. Furthermore,
it is part of https://github.com/llvm/llvm-project/pull/180556 and
substantially simplifies the implementation.

Added: 
    

Modified: 
    mlir/lib/Transforms/Utils/CSE.cpp

Removed: 
    


################################################################################
diff  --git a/mlir/lib/Transforms/Utils/CSE.cpp b/mlir/lib/Transforms/Utils/CSE.cpp
index 6caaadca15d54..eed77f20dd2da 100644
--- a/mlir/lib/Transforms/Utils/CSE.cpp
+++ b/mlir/lib/Transforms/Utils/CSE.cpp
@@ -99,8 +99,9 @@ class CSEDriver {
   void simplifyBlock(ScopedMapTy &knownValues, Block *bb, bool hasSSADominance);
   void simplifyRegion(ScopedMapTy &knownValues, Region &region);
 
-  /// Erase all operations queued for deletion by the simplification routines.
-  void eraseDeadOps(bool *changed);
+  /// Erase opertion that were marked as dead during simplification, and remove
+  /// their associated dominator trees.
+  void eraseDeadOp(Operation *op);
 
   void replaceUsesAndDelete(ScopedMapTy &knownValues, Operation *op,
                             Operation *existing, bool hasSSADominance);
@@ -112,8 +113,6 @@ class CSEDriver {
   /// A rewriter for modifying the IR.
   RewriterBase &rewriter;
 
-  /// Operations marked as dead and to be erased.
-  std::vector<Operation *> opsToErase;
   DominanceInfo *domInfo = nullptr;
   MemEffectsCache memEffectsCache;
 
@@ -134,7 +133,7 @@ void CSEDriver::replaceUsesAndDelete(ScopedMapTy &knownValues, Operation *op,
     // visited any use of the current operation.
     // Replace all uses, but do not remove the operation yet.
     rewriter.replaceAllOpUsesWith(op, existing->getResults());
-    opsToErase.push_back(op);
+    eraseDeadOp(op);
   } else {
     // When the region does not have SSA dominance, we need to check if we
     // have visited a use before replacing any use.
@@ -154,7 +153,7 @@ void CSEDriver::replaceUsesAndDelete(ScopedMapTy &knownValues, Operation *op,
 
     // There may be some remaining uses of the operation.
     if (op->use_empty())
-      opsToErase.push_back(op);
+      eraseDeadOp(op);
   }
 
   // If the existing operation has an unknown location and the current
@@ -238,7 +237,11 @@ bool CSEDriver::hasOtherSideEffectingOpInBetween(Operation *fromOp,
     }
     nextOp = nextOp->getNextNode();
   }
-  result.first->second = std::make_pair(toOp, nullptr);
+  // Record the previous op of `toOp` as the insertion point, since `toOp`
+  // will be erased immediately after this. Using `toOp` itself would leave
+  // a dangling pointer, so its predecessor is sufficient to reconstruct
+  // the position.
+  result.first->second = std::make_pair(toOp->getPrevNode(), nullptr);
   return false;
 }
 
@@ -298,7 +301,7 @@ void CSEDriver::simplifyBlock(ScopedMapTy &knownValues, Block *bb,
     // This also avoids calling `simplifyRegion` on dead region ops
     // unnecessarily.
     if (isOpTriviallyDead(&op)) {
-      opsToErase.push_back(&op);
+      eraseDeadOp(&op);
       ++numDCE;
       continue;
     }
@@ -382,20 +385,15 @@ void CSEDriver::simplifyRegion(ScopedMapTy &knownValues, Region &region) {
   }
 }
 
-void CSEDriver::eraseDeadOps(bool *changed) {
-  // Erase any operations that were marked as dead during simplification, and
-  // remove their associated dominator trees.
-  for (auto *op : opsToErase) {
-    for (Region &region : op->getRegions())
-      domInfo->invalidate(&region);
-    rewriter.eraseOp(op);
-  }
-  if (changed)
-    *changed = !opsToErase.empty();
-  opsToErase.clear();
+void CSEDriver::eraseDeadOp(Operation *op) {
+  for (Region &region : op->getRegions())
+    domInfo->invalidate(&region);
+  rewriter.eraseOp(op);
 
-  // Note: CSE does currently not remove ops with regions, so DominanceInfo
-  // does not have to be invalidated.
+  // Note: CSE only removes ops within blocks, without adding or removing
+  // blocks themselves. Since DominanceInfo captures relationships between
+  // the direct blocks of the region being analyzed, not the blocks inside
+  // any nested regions of those ops, it remains valid after CSE.
 }
 
 void CSEDriver::simplify(Operation *op, bool *changed) {
@@ -403,13 +401,15 @@ void CSEDriver::simplify(Operation *op, bool *changed) {
   ScopedMapTy knownValues;
   for (auto &region : op->getRegions())
     simplifyRegion(knownValues, region);
-  eraseDeadOps(changed);
+  if (changed)
+    *changed = numCSE || numDCE;
 }
 
 void CSEDriver::simplify(Region &region, bool *changed) {
   ScopedMapTy knownValues;
   simplifyRegion(knownValues, region);
-  eraseDeadOps(changed);
+  if (changed)
+    *changed = numCSE || numDCE;
 }
 
 void mlir::eliminateCommonSubExpressions(RewriterBase &rewriter,


        


More information about the Mlir-commits mailing list