[Mlir-commits] [mlir] [mlir][Transforms] Improve `replaceOpWithMultiple` API (PR #132608)

Matthias Springer llvmlistbot at llvm.org
Tue Mar 25 10:28:30 PDT 2025


================
@@ -897,7 +897,34 @@ class ConversionPatternRewriter final : public PatternRewriter {
 
   /// Replace the given operation with the new value ranges. The number of op
   /// results and value ranges must match. The given  operation is erased.
-  void replaceOpWithMultiple(Operation *op, ArrayRef<ValueRange> newValues);
+  void replaceOpWithMultiple(Operation *op,
+                             ArrayRef<SmallVector<Value, 1>> newValues);
+  // Note: This overload matches SmallVector<ValueRange>,
+  // SmallVector<SmallVector<Value>>, etc.
+  template <typename RangeRangeT>
+  void replaceOpWithMultiple(Operation *op, RangeRangeT &&newValues) {
+    // Note: Prefer the ArrayRef<SmallVector<Value, 1>> overload because it
+    // does not copy the replacements vector.
+    auto vals = llvm::map_to_vector(newValues, [](const auto &r) {
+      // Note: Create intermediate ValueRange because SmallVector<Value, 1>
+      // is not constructible from SmallVector<Value>.
+      return SmallVector<Value, 1>(ValueRange(r));
+    });
+    replaceOpWithMultiple(op, ArrayRef(vals));
+  }
+  // Note: This overload matches initializer list of ValueRange,
+  // SmallVector<Value>, etc.
+  template <typename RangeT = ValueRange>
+  void replaceOpWithMultiple(Operation *op, ArrayRef<RangeT> newValues) {
+    // Note: Prefer the ArrayRef<SmallVector<Value, 1>> overload because it
----------------
matthias-springer wrote:

The `, 1` overload is the default one. Ideally, that one should be used all the time. The other two overloads are for convenience, but cause a copy of the vector. (See answer to MacDue's question above.)

I'm wondering if I can drop the `SmallVector<Value>` (without `, 1`) support entirely. It is not that important. What's important is that users can pass `{ ValueRange }`, `{ ArrayRef<Value> }` and `{ {Value, Value}, {Value} }`. Maybe I can make this work with just a single overload...

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


More information about the Mlir-commits mailing list