[llvm-branch-commits] [mlir] [mlir][Transforms] Add 1:N support to `replaceUsesOfBlockArgument` (PR #145171)

2025-06-22 Thread Matthias Springer via llvm-branch-commits


@@ -1434,12 +1439,15 @@ Block 
*ConversionPatternRewriterImpl::applySignatureConversion(
 if (!inputMap) {
   // This block argument was dropped and no replacement value was provided.
   // Materialize a replacement value "out of thin air".
-  buildUnresolvedMaterialization(
-  MaterializationKind::Source,
-  OpBuilder::InsertPoint(newBlock, newBlock->begin()), 
origArg.getLoc(),
-  /*valuesToMap=*/{origArg}, /*inputs=*/ValueRange(),
-  /*outputTypes=*/origArgType, /*originalType=*/Type(), converter);
-  appendRewrite(block, origArg, converter);

matthias-springer wrote:

No, `buildUnresolvedMaterialization` does that if `valuesToMap` is provided. 
That's why `valuesToMap` is now empty.


https://github.com/llvm/llvm-project/pull/145171
___
llvm-branch-commits mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-branch-commits


[llvm-branch-commits] [mlir] [mlir][Transforms] Add 1:N support to `replaceUsesOfBlockArgument` (PR #145171)

2025-06-21 Thread Jeremy Kun via llvm-branch-commits

https://github.com/j2kun approved this pull request.


https://github.com/llvm/llvm-project/pull/145171
___
llvm-branch-commits mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-branch-commits


[llvm-branch-commits] [mlir] [mlir][Transforms] Add 1:N support to `replaceUsesOfBlockArgument` (PR #145171)

2025-06-21 Thread Jeremy Kun via llvm-branch-commits


@@ -1434,12 +1439,15 @@ Block 
*ConversionPatternRewriterImpl::applySignatureConversion(
 if (!inputMap) {
   // This block argument was dropped and no replacement value was provided.
   // Materialize a replacement value "out of thin air".
-  buildUnresolvedMaterialization(
-  MaterializationKind::Source,
-  OpBuilder::InsertPoint(newBlock, newBlock->begin()), 
origArg.getLoc(),
-  /*valuesToMap=*/{origArg}, /*inputs=*/ValueRange(),
-  /*outputTypes=*/origArgType, /*originalType=*/Type(), converter);
-  appendRewrite(block, origArg, converter);

j2kun wrote:

Was it a bug here that this step did not update `mapping`? (and in this PR now 
it does via replaceUsesOfBlockArgument)

https://github.com/llvm/llvm-project/pull/145171
___
llvm-branch-commits mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-branch-commits


[llvm-branch-commits] [mlir] [mlir][Transforms] Add 1:N support to `replaceUsesOfBlockArgument` (PR #145171)

2025-06-21 Thread via llvm-branch-commits

llvmbot wrote:




@llvm/pr-subscribers-mlir

Author: Matthias Springer (matthias-springer)


Changes

This commit adds 1:N support to 
`ConversionPatternRewriter::replaceUsesOfBlockArgument`. This was one of the 
few remaining dialect conversion APIs that does not support 1:N conversions yet.

This commit also reuses `replaceUsesOfBlockArgument` in the implementation of 
`applySignatureConversion`. This is in preparation of the One-Shot Dialect 
Conversion refactoring. The goal is to bring the `applySignatureConversion` 
implementation into a state where it works both with and without rollbacks. To 
that end, `applySignatureConversion` should not directly access the `mapping`.

Depends on #145155.


---
Full diff: https://github.com/llvm/llvm-project/pull/145171.diff


5 Files Affected:

- (modified) mlir/include/mlir/Transforms/DialectConversion.h (+3-2) 
- (modified) mlir/lib/Conversion/FuncToLLVM/FuncToLLVM.cpp (+1-1) 
- (modified) mlir/lib/Transforms/Utils/DialectConversion.cpp (+25-15) 
- (modified) mlir/test/Transforms/test-legalizer.mlir (+24-7) 
- (modified) mlir/test/lib/Dialect/Test/TestPatterns.cpp (+29-22) 


``diff
diff --git a/mlir/include/mlir/Transforms/DialectConversion.h 
b/mlir/include/mlir/Transforms/DialectConversion.h
index 5a5f116073a9a..81858812d2623 100644
--- a/mlir/include/mlir/Transforms/DialectConversion.h
+++ b/mlir/include/mlir/Transforms/DialectConversion.h
@@ -763,8 +763,9 @@ class ConversionPatternRewriter final : public 
PatternRewriter {
   Region *region, const TypeConverter &converter,
   TypeConverter::SignatureConversion *entryConversion = nullptr);
 
-  /// Replace all the uses of the block argument `from` with value `to`.
-  void replaceUsesOfBlockArgument(BlockArgument from, Value to);
+  /// Replace all the uses of the block argument `from` with `to`. This
+  /// function supports both 1:1 and 1:N replacements.
+  void replaceUsesOfBlockArgument(BlockArgument from, ValueRange to);
 
   /// Return the converted value of 'key' with a type defined by the type
   /// converter of the currently executing pattern. Return nullptr in the case
diff --git a/mlir/lib/Conversion/FuncToLLVM/FuncToLLVM.cpp 
b/mlir/lib/Conversion/FuncToLLVM/FuncToLLVM.cpp
index 538016927256b..9e8e746507557 100644
--- a/mlir/lib/Conversion/FuncToLLVM/FuncToLLVM.cpp
+++ b/mlir/lib/Conversion/FuncToLLVM/FuncToLLVM.cpp
@@ -294,7 +294,7 @@ static void restoreByValRefArgumentType(
 Type resTy = typeConverter.convertType(
 cast(byValRefAttr->getValue()).getValue());
 
-auto valueArg = rewriter.create(arg.getLoc(), resTy, arg);
+Value valueArg = rewriter.create(arg.getLoc(), resTy, arg);
 rewriter.replaceUsesOfBlockArgument(arg, valueArg);
   }
 }
diff --git a/mlir/lib/Transforms/Utils/DialectConversion.cpp 
b/mlir/lib/Transforms/Utils/DialectConversion.cpp
index 774d58973eb91..9cb6f2ba1eaae 100644
--- a/mlir/lib/Transforms/Utils/DialectConversion.cpp
+++ b/mlir/lib/Transforms/Utils/DialectConversion.cpp
@@ -948,6 +948,11 @@ struct ConversionPatternRewriterImpl : public 
RewriterBase::Listener {
   /// uses.
   void replaceOp(Operation *op, SmallVector> &&newValues);
 
+  /// Replace the given block argument with the given values. The specified
+  /// converter is used to build materializations (if necessary).
+  void replaceUsesOfBlockArgument(BlockArgument from, ValueRange to,
+  const TypeConverter *converter);
+
   /// Erase the given block and its contents.
   void eraseBlock(Block *block);
 
@@ -1434,12 +1439,15 @@ Block 
*ConversionPatternRewriterImpl::applySignatureConversion(
 if (!inputMap) {
   // This block argument was dropped and no replacement value was provided.
   // Materialize a replacement value "out of thin air".
-  buildUnresolvedMaterialization(
-  MaterializationKind::Source,
-  OpBuilder::InsertPoint(newBlock, newBlock->begin()), 
origArg.getLoc(),
-  /*valuesToMap=*/{origArg}, /*inputs=*/ValueRange(),
-  /*outputTypes=*/origArgType, /*originalType=*/Type(), converter);
-  appendRewrite(block, origArg, converter);
+  Value mat =
+  buildUnresolvedMaterialization(
+  MaterializationKind::Source,
+  OpBuilder::InsertPoint(newBlock, newBlock->begin()),
+  origArg.getLoc(),
+  /*valuesToMap=*/{}, /*inputs=*/ValueRange(),
+  /*outputTypes=*/origArgType, /*originalType=*/Type(), converter)
+  .front();
+  replaceUsesOfBlockArgument(origArg, mat, converter);
   continue;
 }
 
@@ -1448,17 +1456,15 @@ Block 
*ConversionPatternRewriterImpl::applySignatureConversion(
   assert(inputMap->size == 0 &&
  "invalid to provide a replacement value when the argument isn't "
  "dropped");
-  mapping.map(origArg, inputMap->replacementValues);
-  appendRewrite(block, origArg, converter);
+  replaceUsesOfBlockArgument(origArg, inputMap->replac

[llvm-branch-commits] [mlir] [mlir][Transforms] Add 1:N support to `replaceUsesOfBlockArgument` (PR #145171)

2025-06-21 Thread via llvm-branch-commits

llvmbot wrote:




@llvm/pr-subscribers-mlir-core

Author: Matthias Springer (matthias-springer)


Changes

This commit adds 1:N support to 
`ConversionPatternRewriter::replaceUsesOfBlockArgument`. This was one of the 
few remaining dialect conversion APIs that does not support 1:N conversions yet.

This commit also reuses `replaceUsesOfBlockArgument` in the implementation of 
`applySignatureConversion`. This is in preparation of the One-Shot Dialect 
Conversion refactoring. The goal is to bring the `applySignatureConversion` 
implementation into a state where it works both with and without rollbacks. To 
that end, `applySignatureConversion` should not directly access the `mapping`.

Depends on #145155.


---
Full diff: https://github.com/llvm/llvm-project/pull/145171.diff


5 Files Affected:

- (modified) mlir/include/mlir/Transforms/DialectConversion.h (+3-2) 
- (modified) mlir/lib/Conversion/FuncToLLVM/FuncToLLVM.cpp (+1-1) 
- (modified) mlir/lib/Transforms/Utils/DialectConversion.cpp (+25-15) 
- (modified) mlir/test/Transforms/test-legalizer.mlir (+24-7) 
- (modified) mlir/test/lib/Dialect/Test/TestPatterns.cpp (+29-22) 


``diff
diff --git a/mlir/include/mlir/Transforms/DialectConversion.h 
b/mlir/include/mlir/Transforms/DialectConversion.h
index 5a5f116073a9a..81858812d2623 100644
--- a/mlir/include/mlir/Transforms/DialectConversion.h
+++ b/mlir/include/mlir/Transforms/DialectConversion.h
@@ -763,8 +763,9 @@ class ConversionPatternRewriter final : public 
PatternRewriter {
   Region *region, const TypeConverter &converter,
   TypeConverter::SignatureConversion *entryConversion = nullptr);
 
-  /// Replace all the uses of the block argument `from` with value `to`.
-  void replaceUsesOfBlockArgument(BlockArgument from, Value to);
+  /// Replace all the uses of the block argument `from` with `to`. This
+  /// function supports both 1:1 and 1:N replacements.
+  void replaceUsesOfBlockArgument(BlockArgument from, ValueRange to);
 
   /// Return the converted value of 'key' with a type defined by the type
   /// converter of the currently executing pattern. Return nullptr in the case
diff --git a/mlir/lib/Conversion/FuncToLLVM/FuncToLLVM.cpp 
b/mlir/lib/Conversion/FuncToLLVM/FuncToLLVM.cpp
index 538016927256b..9e8e746507557 100644
--- a/mlir/lib/Conversion/FuncToLLVM/FuncToLLVM.cpp
+++ b/mlir/lib/Conversion/FuncToLLVM/FuncToLLVM.cpp
@@ -294,7 +294,7 @@ static void restoreByValRefArgumentType(
 Type resTy = typeConverter.convertType(
 cast(byValRefAttr->getValue()).getValue());
 
-auto valueArg = rewriter.create(arg.getLoc(), resTy, arg);
+Value valueArg = rewriter.create(arg.getLoc(), resTy, arg);
 rewriter.replaceUsesOfBlockArgument(arg, valueArg);
   }
 }
diff --git a/mlir/lib/Transforms/Utils/DialectConversion.cpp 
b/mlir/lib/Transforms/Utils/DialectConversion.cpp
index 774d58973eb91..9cb6f2ba1eaae 100644
--- a/mlir/lib/Transforms/Utils/DialectConversion.cpp
+++ b/mlir/lib/Transforms/Utils/DialectConversion.cpp
@@ -948,6 +948,11 @@ struct ConversionPatternRewriterImpl : public 
RewriterBase::Listener {
   /// uses.
   void replaceOp(Operation *op, SmallVector> &&newValues);
 
+  /// Replace the given block argument with the given values. The specified
+  /// converter is used to build materializations (if necessary).
+  void replaceUsesOfBlockArgument(BlockArgument from, ValueRange to,
+  const TypeConverter *converter);
+
   /// Erase the given block and its contents.
   void eraseBlock(Block *block);
 
@@ -1434,12 +1439,15 @@ Block 
*ConversionPatternRewriterImpl::applySignatureConversion(
 if (!inputMap) {
   // This block argument was dropped and no replacement value was provided.
   // Materialize a replacement value "out of thin air".
-  buildUnresolvedMaterialization(
-  MaterializationKind::Source,
-  OpBuilder::InsertPoint(newBlock, newBlock->begin()), 
origArg.getLoc(),
-  /*valuesToMap=*/{origArg}, /*inputs=*/ValueRange(),
-  /*outputTypes=*/origArgType, /*originalType=*/Type(), converter);
-  appendRewrite(block, origArg, converter);
+  Value mat =
+  buildUnresolvedMaterialization(
+  MaterializationKind::Source,
+  OpBuilder::InsertPoint(newBlock, newBlock->begin()),
+  origArg.getLoc(),
+  /*valuesToMap=*/{}, /*inputs=*/ValueRange(),
+  /*outputTypes=*/origArgType, /*originalType=*/Type(), converter)
+  .front();
+  replaceUsesOfBlockArgument(origArg, mat, converter);
   continue;
 }
 
@@ -1448,17 +1456,15 @@ Block 
*ConversionPatternRewriterImpl::applySignatureConversion(
   assert(inputMap->size == 0 &&
  "invalid to provide a replacement value when the argument isn't "
  "dropped");
-  mapping.map(origArg, inputMap->replacementValues);
-  appendRewrite(block, origArg, converter);
+  replaceUsesOfBlockArgument(origArg, inputMap->r

[llvm-branch-commits] [mlir] [mlir][Transforms] Add 1:N support to `replaceUsesOfBlockArgument` (PR #145171)

2025-06-21 Thread Matthias Springer via llvm-branch-commits

https://github.com/matthias-springer created 
https://github.com/llvm/llvm-project/pull/145171

This commit adds 1:N support to 
`ConversionPatternRewriter::replaceUsesOfBlockArgument`. This was one of the 
few remaining dialect conversion APIs that does not support 1:N conversions yet.

This commit also reuses `replaceUsesOfBlockArgument` in the implementation of 
`applySignatureConversion`. This is in preparation of the One-Shot Dialect 
Conversion refactoring. The goal is to bring the `applySignatureConversion` 
implementation into a state where it works both with and without rollbacks. To 
that end, `applySignatureConversion` should not directly access the `mapping`.

Depends on #145155.


>From b3760c623e4fe8161232533a0b1e65d7bf883d2d Mon Sep 17 00:00:00 2001
From: Matthias Springer 
Date: Sat, 21 Jun 2025 14:29:50 +
Subject: [PATCH] [mlir][Transforms] Add 1:N support to
 `replaceUsesOfBlockArgument`

---
 .../mlir/Transforms/DialectConversion.h   |  5 +-
 mlir/lib/Conversion/FuncToLLVM/FuncToLLVM.cpp |  2 +-
 .../Transforms/Utils/DialectConversion.cpp| 40 +--
 mlir/test/Transforms/test-legalizer.mlir  | 31 ---
 mlir/test/lib/Dialect/Test/TestPatterns.cpp   | 51 +++
 5 files changed, 82 insertions(+), 47 deletions(-)

diff --git a/mlir/include/mlir/Transforms/DialectConversion.h 
b/mlir/include/mlir/Transforms/DialectConversion.h
index 5a5f116073a9a..81858812d2623 100644
--- a/mlir/include/mlir/Transforms/DialectConversion.h
+++ b/mlir/include/mlir/Transforms/DialectConversion.h
@@ -763,8 +763,9 @@ class ConversionPatternRewriter final : public 
PatternRewriter {
   Region *region, const TypeConverter &converter,
   TypeConverter::SignatureConversion *entryConversion = nullptr);
 
-  /// Replace all the uses of the block argument `from` with value `to`.
-  void replaceUsesOfBlockArgument(BlockArgument from, Value to);
+  /// Replace all the uses of the block argument `from` with `to`. This
+  /// function supports both 1:1 and 1:N replacements.
+  void replaceUsesOfBlockArgument(BlockArgument from, ValueRange to);
 
   /// Return the converted value of 'key' with a type defined by the type
   /// converter of the currently executing pattern. Return nullptr in the case
diff --git a/mlir/lib/Conversion/FuncToLLVM/FuncToLLVM.cpp 
b/mlir/lib/Conversion/FuncToLLVM/FuncToLLVM.cpp
index 538016927256b..9e8e746507557 100644
--- a/mlir/lib/Conversion/FuncToLLVM/FuncToLLVM.cpp
+++ b/mlir/lib/Conversion/FuncToLLVM/FuncToLLVM.cpp
@@ -294,7 +294,7 @@ static void restoreByValRefArgumentType(
 Type resTy = typeConverter.convertType(
 cast(byValRefAttr->getValue()).getValue());
 
-auto valueArg = rewriter.create(arg.getLoc(), resTy, arg);
+Value valueArg = rewriter.create(arg.getLoc(), resTy, arg);
 rewriter.replaceUsesOfBlockArgument(arg, valueArg);
   }
 }
diff --git a/mlir/lib/Transforms/Utils/DialectConversion.cpp 
b/mlir/lib/Transforms/Utils/DialectConversion.cpp
index 774d58973eb91..9cb6f2ba1eaae 100644
--- a/mlir/lib/Transforms/Utils/DialectConversion.cpp
+++ b/mlir/lib/Transforms/Utils/DialectConversion.cpp
@@ -948,6 +948,11 @@ struct ConversionPatternRewriterImpl : public 
RewriterBase::Listener {
   /// uses.
   void replaceOp(Operation *op, SmallVector> &&newValues);
 
+  /// Replace the given block argument with the given values. The specified
+  /// converter is used to build materializations (if necessary).
+  void replaceUsesOfBlockArgument(BlockArgument from, ValueRange to,
+  const TypeConverter *converter);
+
   /// Erase the given block and its contents.
   void eraseBlock(Block *block);
 
@@ -1434,12 +1439,15 @@ Block 
*ConversionPatternRewriterImpl::applySignatureConversion(
 if (!inputMap) {
   // This block argument was dropped and no replacement value was provided.
   // Materialize a replacement value "out of thin air".
-  buildUnresolvedMaterialization(
-  MaterializationKind::Source,
-  OpBuilder::InsertPoint(newBlock, newBlock->begin()), 
origArg.getLoc(),
-  /*valuesToMap=*/{origArg}, /*inputs=*/ValueRange(),
-  /*outputTypes=*/origArgType, /*originalType=*/Type(), converter);
-  appendRewrite(block, origArg, converter);
+  Value mat =
+  buildUnresolvedMaterialization(
+  MaterializationKind::Source,
+  OpBuilder::InsertPoint(newBlock, newBlock->begin()),
+  origArg.getLoc(),
+  /*valuesToMap=*/{}, /*inputs=*/ValueRange(),
+  /*outputTypes=*/origArgType, /*originalType=*/Type(), converter)
+  .front();
+  replaceUsesOfBlockArgument(origArg, mat, converter);
   continue;
 }
 
@@ -1448,17 +1456,15 @@ Block 
*ConversionPatternRewriterImpl::applySignatureConversion(
   assert(inputMap->size == 0 &&
  "invalid to provide a replacement value when the argument isn't "
  "dropped");
-  mapping.map(origArg