[Mlir-commits] [flang] [mlir] [mlir][CSE] Add pruneDeadOps to CSE pass (PR #193778)

lonely eagle llvmlistbot at llvm.org
Thu Apr 30 06:08:52 PDT 2026


================
@@ -294,15 +297,38 @@ LogicalResult CSEDriver::simplifyOperation(ScopedMapTy &knownValues,
   return failure();
 }
 
+void CSEDriver::pruneDeadOps(Operation *root, ScopedMapTy &knownValues) {
+  // We use `SetVector` to prevent already inserted ops from being added to the
+  // `worklist` repeatedly, avoiding secondary access to erased operations.
+  llvm::SetVector<Operation *> worklist;
----------------
linuxlonelyeagle wrote:

There are a few issues here.

First, `eliminateTriviallyDeadOps` seems to require a region as a parameter, and its implementation differs from what we have here. Running it during `simplifyRegion` in CSE feels somewhat detached from the CSE algorithm itself. Since it would run on every region individually, it might be better to just run it once on the module’s region before starting CSE.

Second, `eliminateTriviallyDeadOps` doesn't seem capable of cross-region elimination. I looked into the implementation, and as I suspected, it starts from a region and deletes its sub-regions. This PR, however, finds a root op and can work its way up to delete ops in its parent regions. The root cause is that we are currently inside the `simplifyBlock` function of CSE.

Third, `eliminateTriviallyDeadOps` doesn't track the number of eliminated ops.

It's a bit of an awkward situation.😊

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


More information about the Mlir-commits mailing list