[Mlir-commits] [mlir] [mlir][IR] Tweak `RewriterBase::replaceUsesWithIf` call (PR #172883)
llvmlistbot at llvm.org
llvmlistbot at llvm.org
Thu Dec 18 09:18:52 PST 2025
https://github.com/BabakkGraphcore created https://github.com/llvm/llvm-project/pull/172883
`ConversionPatternRewriter::replaceUsesWithIf` does not support the `allUsesReplaced` flag and asserts that it's not set. However the `ValueRange` overload of `RewriterBase::replaceUsesWithIf` always passes this flag when calling the virtual overload of `replaceUsesWithIf`. This means calls made to `RewriterBase::replaceUsesWithIf` from a `ConversionPattern` crash, whether the `allUsesReplaced` flag is set or not.
This change tweaks `RewriterBase::replaceUsesWithIf` to only pass that flag if the callee has set it.
>From 16613bd820f54cb1573acecfef79a20c149a5547 Mon Sep 17 00:00:00 2001
From: Babak Khataee <babakk at graphcore.ai>
Date: Thu, 18 Dec 2025 16:49:54 +0000
Subject: [PATCH] Tweak repalceUsesWithIf call so that it only sets the
allUsesReplacedCall flag if called with that flag set.
---
mlir/lib/IR/PatternMatch.cpp | 17 +++++++++--------
1 file changed, 9 insertions(+), 8 deletions(-)
diff --git a/mlir/lib/IR/PatternMatch.cpp b/mlir/lib/IR/PatternMatch.cpp
index 8bc0fcd4517d8..07fd52e0812c6 100644
--- a/mlir/lib/IR/PatternMatch.cpp
+++ b/mlir/lib/IR/PatternMatch.cpp
@@ -276,15 +276,16 @@ void RewriterBase::replaceUsesWithIf(ValueRange from, ValueRange to,
function_ref<bool(OpOperand &)> functor,
bool *allUsesReplaced) {
assert(from.size() == to.size() && "incorrect number of replacements");
- bool allReplaced = true;
- for (auto it : llvm::zip_equal(from, to)) {
- bool r;
- replaceUsesWithIf(std::get<0>(it), std::get<1>(it), functor,
- /*allUsesReplaced=*/&r);
- allReplaced &= r;
+ for (auto [fromVal, toVal] : llvm::zip_equal(from, to)) {
+ if (allUsesReplaced) {
+ bool r;
+ replaceUsesWithIf(fromVal, toVal, functor,
+ /*allUsesReplaced=*/&r);
+ *allUsesReplaced &= r;
+ } else {
+ replaceUsesWithIf(fromVal, toVal, functor);
+ }
}
- if (allUsesReplaced)
- *allUsesReplaced = allReplaced;
}
void RewriterBase::inlineBlockBefore(Block *source, Block *dest,
More information about the Mlir-commits
mailing list