[Mlir-commits] [flang] [mlir] [mlir][CSE] Introduce hoist-pure-ops logic to CSE pass (PR #180556)
Mehdi Amini
llvmlistbot at llvm.org
Sat Feb 28 12:50:25 PST 2026
================
@@ -272,49 +386,77 @@ LogicalResult CSEDriver::simplifyOperation(ScopedMapTy &knownValues,
return success();
}
- // Otherwise, we add this operation to the known values map.
- knownValues.insert(op, op);
+ if (auto *existing = knownPureOps.lookup(op)) {
+ replaceUsesAndDelete(knownPureOps, op, existing, hasSSADominance);
+ ++numCSE;
+ return success();
+ }
+
+ if (mlir::isPure(op)) {
+ LDBG() << "insert op: " << OpWithFlags(op, OpPrintingFlags().skipRegions())
+ << "to pureMap";
+ knownPureOps.insert(op, op);
+ } else {
+ // Otherwise, we add this operation to the known values map.
+ LDBG() << "insert op: " << OpWithFlags(op, OpPrintingFlags().skipRegions())
+ << "to map";
+ knownValues.insert(op, op);
+ }
return failure();
}
-void CSEDriver::simplifyBlock(ScopedMapTy &knownValues, Block *bb,
+void CSEDriver::simplifyBlock(ScopedMapTy &knownValues,
+ ScopedMapTy &knownPureOps, Block *bb,
bool hasSSADominance) {
- for (auto &op : *bb) {
+ LDBG() << "visit block #" << bb->computeBlockNumber() << " of "
+ << OpWithFlags(bb->getParentOp(), OpPrintingFlags().skipRegions());
+ for (auto &op : llvm::make_early_inc_range(*bb)) {
// Most operations don't have regions, so fast path that case.
if (op.getNumRegions() != 0) {
// If this operation is isolated above, we can't process nested regions
// with the given 'knownValues' map. This would cause the insertion of
// implicit captures in explicit capture only regions.
if (op.mightHaveTrait<OpTrait::IsIsolatedFromAbove>()) {
ScopedMapTy nestedKnownValues;
+ ScopedMapTy nestedKnownPureOps;
+ ScopedMapTy::ScopeTy scope(nestedKnownValues);
+ ScopedMapTy::ScopeTy pureScope(nestedKnownPureOps);
for (auto ®ion : op.getRegions())
- simplifyRegion(nestedKnownValues, region);
+ simplifyRegion(nestedKnownValues, nestedKnownPureOps, region);
} else {
// Otherwise, process nested regions normally.
for (auto ®ion : op.getRegions())
- simplifyRegion(knownValues, region);
+ simplifyRegion(knownValues, knownPureOps, region);
}
}
// If the operation is simplified, we don't process any held regions.
- if (succeeded(simplifyOperation(knownValues, &op, hasSSADominance)))
+ if (succeeded(
+ simplifyOperation(knownValues, knownPureOps, &op, hasSSADominance)))
continue;
}
// Clear the MemoryEffects cache since its usage is by block only.
memEffectsCache.clear();
}
-void CSEDriver::simplifyRegion(ScopedMapTy &knownValues, Region ®ion) {
+void CSEDriver::simplifyRegion(ScopedMapTy &knownValues,
+ ScopedMapTy &knownPureOps, Region ®ion) {
// If the region is empty there is nothing to do.
if (region.empty())
return;
+ LDBG() << "visit region #" << region.getRegionNumber() << " of "
+ << OpWithFlags(region.getParentOp(), OpPrintingFlags().skipRegions());
+ // Prevent CSE of pure operations across function boundaries.
+ std::unique_ptr<ScopedMapTy::ScopeTy> funcPureScope;
+ if (isa<FunctionOpInterface>(region.getParentOp())) {
+ funcPureScope = std::make_unique<ScopedMapTy::ScopeTy>(knownPureOps);
+ }
----------------
joker-eph wrote:
Where is this used?
https://github.com/llvm/llvm-project/pull/180556
More information about the Mlir-commits
mailing list