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

Hocky Yudhiono llvmlistbot at llvm.org
Sun May 3 23:43:12 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;
----------------
hockyy wrote:

I think it's because CSE does a shallow DCE after the pass.

IMO, pruneDeadOps function is somehow duplicating the normal DCE. Complexity is not lower too as its iterating the entire blocks.

```
hocky at hocky:~/llvm/llvm-project/build_clion$ mlir-opt test.mlir
module {
  func.func @try(%arg0: memref<?x10xindex>) {
    %c1 = arith.constant 1 : index
    %dim = memref.dim %arg0, %c1 : memref<?x10xindex>
    return
  }
}

hocky at hocky:~/llvm/llvm-project/build_clion$ mlir-opt test.mlir --cse
module {
  func.func @try(%arg0: memref<?x10xindex>) {
    %c1 = arith.constant 1 : index
    return
  }
}

hocky at hocky:~/llvm/llvm-project/build_clion$ mlir-opt test.mlir --cse --cse
module {
  func.func @try(%arg0: memref<?x10xindex>) {
    return
  }
}
```

Either:
- Don't change anything.
- Loop the block from the back for better removal, and remove on the fly. (But will not support inter block dependency)
- Refactor the DCE so it can take a block.

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


More information about the Mlir-commits mailing list