[Mlir-commits] [mlir] [mlir][Transforms] Refactor CSE side-effect cache access in read-conflict scan (PR #192178)
llvmlistbot at llvm.org
llvmlistbot at llvm.org
Tue Apr 14 21:16:56 PDT 2026
https://github.com/zackc6 updated https://github.com/llvm/llvm-project/pull/192178
>From 86d56250dcd999bc9f7ef77988d8a813a8c3c3de Mon Sep 17 00:00:00 2001
From: zack <zackchen666 at gmail.com>
Date: Wed, 15 Apr 2026 12:09:53 +0800
Subject: [PATCH] [mlir][Transforms] Refactor CSE side-effect cache access in
read-conflict scan
Refactors cache handling in hasOtherSideEffectingOpInBetween:
1. Replaces try_emplace + result.first->second usage with explicit cache initialization
and a reference (memEffectsCachePair)
2. Reuses that cached pair consistently for all update paths
(unknown effects, conflicting write, completion).
---
mlir/lib/Transforms/CSE.cpp | 38 ++++++++++++++++++++-----------------
1 file changed, 21 insertions(+), 17 deletions(-)
diff --git a/mlir/lib/Transforms/CSE.cpp b/mlir/lib/Transforms/CSE.cpp
index 4d25e5e7c92b6..ace19c6b120bd 100644
--- a/mlir/lib/Transforms/CSE.cpp
+++ b/mlir/lib/Transforms/CSE.cpp
@@ -194,20 +194,24 @@ bool CSEDriver::hasOtherSideEffectingOpInBetween(Operation *fromOp,
}
Operation *nextOp = fromOp->getNextNode();
- auto result =
- memEffectsCache.try_emplace(fromOp, std::make_pair(fromOp, nullptr));
- if (result.second) {
- auto memEffectsCachePair = result.first->second;
- if (memEffectsCachePair.second == nullptr) {
- // No MemoryEffects::Write has been detected until the cached operation.
- // Continue looking from the cached operation to toOp.
- nextOp = memEffectsCachePair.first;
- } else {
- // MemoryEffects::Write has been detected before so there is no need to
- // check further.
- return true;
- }
+
+ if (!memEffectsCache.contains(fromOp)) {
+ memEffectsCache.insert(
+ std::make_pair(fromOp, std::make_pair(fromOp, nullptr)));
}
+
+ auto &memEffectsCachePair = memEffectsCache[fromOp];
+ // Check if the cached operation has a MemoryEffects::Write.
+ if (memEffectsCachePair.second == nullptr) {
+ // No MemoryEffects::Write has been detected until the cached operation.
+ // Continue looking from the cached operation to toOp.
+ nextOp = memEffectsCachePair.first;
+ } else {
+ // MemoryEffects::Write has been detected before so there is no need to
+ // check further.
+ return true;
+ }
+
while (nextOp && nextOp != toOp) {
std::optional<SmallVector<MemoryEffects::EffectInstance>> effects =
getEffectsRecursively(nextOp);
@@ -215,8 +219,7 @@ bool CSEDriver::hasOtherSideEffectingOpInBetween(Operation *fromOp,
// TODO: Do we need to handle other effects generically?
// If the operation does not implement the MemoryEffectOpInterface we
// conservatively assume it writes.
- result.first->second =
- std::make_pair(nextOp, MemoryEffects::Write::get());
+ memEffectsCachePair = std::make_pair(nextOp, MemoryEffects::Write::get());
return true;
}
@@ -239,14 +242,15 @@ bool CSEDriver::hasOtherSideEffectingOpInBetween(Operation *fromOp,
return true;
});
if (canConflict) {
- result.first->second = {nextOp, MemoryEffects::Write::get()};
+ memEffectsCachePair =
+ std::make_pair(nextOp, MemoryEffects::Write::get());
return true;
}
}
}
nextOp = nextOp->getNextNode();
}
- result.first->second = std::make_pair(toOp, nullptr);
+ memEffectsCachePair = std::make_pair(toOp, nullptr);
return false;
}
More information about the Mlir-commits
mailing list