[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:35:47 PDT 2026


================
@@ -314,41 +321,78 @@ void CSEDriver::simplifyBlock(ScopedMapTy &knownValues, Block *bb,
       if (op.mightHaveTrait<OpTrait::IsIsolatedFromAbove>()) {
         ScopedMapTy nestedKnownValues;
         for (auto &region : op.getRegions())
-          simplifyRegion(nestedKnownValues, region);
+          changed |= simplifyRegion(nestedKnownValues, region);
       } else {
         // Otherwise, process nested regions normally.
         for (auto &region : op.getRegions())
-          simplifyRegion(knownValues, region);
+          changed |= simplifyRegion(knownValues, region);
       }
     }
 
-    // If the operation is simplified, we don't process any held regions.
-    if (succeeded(simplifyOperation(knownValues, &op, hasSSADominance)))
+    if (succeeded(simplifyOperation(knownValues, &op, addCSEToWorklist,
+                                    hasSSADominance)))
       continue;
   }
   // Clear the MemoryEffects cache since its usage is by block only.
   memEffectsCache.clear();
+  return changed;
 }
 
-void CSEDriver::simplifyRegion(ScopedMapTy &knownValues, Region &region) {
+bool CSEDriver::simplifyRegion(ScopedMapTy &knownValues, Region &region) {
   // If the region is empty there is nothing to do.
   if (region.empty())
-    return;
+    return false;
 
   bool hasSSADominance = domInfo->hasSSADominance(&region);
+  bool changed = false;
+
+  SmallVector<Operation *> worklist;
+  DenseSet<Operation *> visited;
+  DenseSet<Operation *> cseErasedOps;
+  int64_t cseErasedCount = 0;
+  auto addToWorklist = [&](Operation *op) {
+    if (visited.insert(op).second)
+      worklist.push_back(op);
+  };
+  auto addCSEToWorklist = [&](Operation *op) {
----------------
linuxlonelyeagle wrote:

I don't think it's necessary to push the dead op after CSE into the worklist. Since it shares operands with the existing one, you could even delete it on the spot.

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


More information about the Mlir-commits mailing list