[Mlir-commits] [mlir] [mlir][CSE] Remove the opsToErase container and immediately delete dead ops. (PR #203702)
llvmlistbot at llvm.org
llvmlistbot at llvm.org
Sat Jun 13 07:30:40 PDT 2026
llvmorg-github-actions[bot] wrote:
<!--LLVM PR SUMMARY COMMENT-->
@llvm/pr-subscribers-mlir-core
Author: lonely eagle (linuxlonelyeagle)
<details>
<summary>Changes</summary>
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.
---
Full diff: https://github.com/llvm/llvm-project/pull/203702.diff
1 Files Affected:
- (modified) mlir/lib/Transforms/Utils/CSE.cpp (+23-23)
``````````diff
diff --git a/mlir/lib/Transforms/Utils/CSE.cpp b/mlir/lib/Transforms/Utils/CSE.cpp
index 90444e6201891..211e9aae16568 100644
--- a/mlir/lib/Transforms/Utils/CSE.cpp
+++ b/mlir/lib/Transforms/Utils/CSE.cpp
@@ -102,8 +102,9 @@ class CSEDriver {
void simplifyBlock(ScopedMapTy &knownValues, Block *bb, bool hasSSADominance);
void simplifyRegion(ScopedMapTy &knownValues, Region ®ion);
- /// 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);
@@ -115,8 +116,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;
@@ -137,7 +136,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.
@@ -157,7 +156,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
@@ -241,7 +240,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;
}
@@ -301,7 +304,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;
}
@@ -385,20 +388,15 @@ void CSEDriver::simplifyRegion(ScopedMapTy &knownValues, Region ®ion) {
}
}
-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 ®ion : op->getRegions())
- domInfo->invalidate(®ion);
- rewriter.eraseOp(op);
- }
- if (changed)
- *changed = !opsToErase.empty();
- opsToErase.clear();
+void CSEDriver::eraseDeadOp(Operation *op) {
+ for (Region ®ion : op->getRegions())
+ domInfo->invalidate(®ion);
+ 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) {
@@ -406,13 +404,15 @@ void CSEDriver::simplify(Operation *op, bool *changed) {
ScopedMapTy knownValues;
for (auto ®ion : op->getRegions())
simplifyRegion(knownValues, region);
- eraseDeadOps(changed);
+ if (changed)
+ *changed = numCSE || numDCE;
}
void CSEDriver::simplify(Region ®ion, bool *changed) {
ScopedMapTy knownValues;
simplifyRegion(knownValues, region);
- eraseDeadOps(changed);
+ if (changed)
+ *changed = numCSE || numDCE;
}
void mlir::eliminateCommonSubExpressions(RewriterBase &rewriter,
``````````
</details>
https://github.com/llvm/llvm-project/pull/203702
More information about the Mlir-commits
mailing list