[Mlir-commits] [mlir] [mlir] Dialect conversion: Print note when replacement types do not match legalized types (PR #161802)
llvmlistbot at llvm.org
llvmlistbot at llvm.org
Fri Oct 3 01:16:55 PDT 2025
llvmbot wrote:
<!--LLVM PR SUMMARY COMMENT-->
@llvm/pr-subscribers-mlir
@llvm/pr-subscribers-mlir-core
Author: Matthias Springer (matthias-springer)
<details>
<summary>Changes</summary>
When running with `-debug`, print a note when the replacement types (during a `ConversionPatternRewriter::replaceOp`) do not match the legalized types of the current type converter. That's not an API violation, but it could indicate a bug in user code.
Example output:
```
[dialect-conversion:1] ** Replace : 'test.multiple_1_to_n_replacement'(0x56b745f99470)
[dialect-conversion:1] Note: Replacing op result of type f16 with value(s) of type (f16, f16), but the legalized type(s) is/are (f16)
```
---
Full diff: https://github.com/llvm/llvm-project/pull/161802.diff
1 Files Affected:
- (modified) mlir/lib/Transforms/Utils/DialectConversion.cpp (+38-8)
``````````diff
diff --git a/mlir/lib/Transforms/Utils/DialectConversion.cpp b/mlir/lib/Transforms/Utils/DialectConversion.cpp
index bf0136b39e03c..65bc4df9571b8 100644
--- a/mlir/lib/Transforms/Utils/DialectConversion.cpp
+++ b/mlir/lib/Transforms/Utils/DialectConversion.cpp
@@ -1856,6 +1856,44 @@ void ConversionPatternRewriterImpl::replaceOp(
Operation *op, SmallVector<SmallVector<Value>> &&newValues) {
assert(newValues.size() == op->getNumResults() &&
"incorrect number of replacement values");
+ LLVM_DEBUG({
+ logger.startLine() << "** Replace : '" << op->getName() << "'(" << op
+ << ")\n";
+ if (currentTypeConverter) {
+ // If the user-provided replacement types are different from the
+ // legalized types, as per the current type converter, print a note.
+ // In most cases, the replacement types are expected to match the types
+ // produced by the type converter, so this could indicate a bug in the
+ // user code.
+ for (auto [result, repls] :
+ llvm::zip_equal(op->getResults(), newValues)) {
+ Type resultType = result.getType();
+ auto logProlog = [&, repls = repls]() {
+ logger.startLine() << " Note: Replacing op result of type "
+ << resultType << " with value(s) of type (";
+ llvm::interleaveComma(repls, logger.getOStream(), [&](Value v) {
+ logger.getOStream() << v.getType();
+ });
+ logger.getOStream() << ")";
+ };
+ SmallVector<Type> convertedTypes;
+ if (failed(currentTypeConverter->convertTypes(resultType,
+ convertedTypes))) {
+ logProlog();
+ logger.getOStream() << ", but the type converter failed to legalize "
+ "the original type.\n";
+ continue;
+ }
+ if (TypeRange(convertedTypes) != TypeRange(ValueRange(repls))) {
+ logProlog();
+ logger.getOStream() << ", but the legalized type(s) is/are (";
+ llvm::interleaveComma(convertedTypes, logger.getOStream(),
+ [&](Type t) { logger.getOStream() << t; });
+ logger.getOStream() << ")\n";
+ }
+ }
+ }
+ });
if (!config.allowPatternRollback) {
// Pattern rollback is not allowed: materialize all IR changes immediately.
@@ -2072,10 +2110,6 @@ void ConversionPatternRewriter::replaceOp(Operation *op, Operation *newOp) {
void ConversionPatternRewriter::replaceOp(Operation *op, ValueRange newValues) {
assert(op->getNumResults() == newValues.size() &&
"incorrect # of replacement values");
- LLVM_DEBUG({
- impl->logger.startLine()
- << "** Replace : '" << op->getName() << "'(" << op << ")\n";
- });
// If the current insertion point is before the erased operation, we adjust
// the insertion point to be after the operation.
@@ -2093,10 +2127,6 @@ void ConversionPatternRewriter::replaceOpWithMultiple(
Operation *op, SmallVector<SmallVector<Value>> &&newValues) {
assert(op->getNumResults() == newValues.size() &&
"incorrect # of replacement values");
- LLVM_DEBUG({
- impl->logger.startLine()
- << "** Replace : '" << op->getName() << "'(" << op << ")\n";
- });
// If the current insertion point is before the erased operation, we adjust
// the insertion point to be after the operation.
``````````
</details>
https://github.com/llvm/llvm-project/pull/161802
More information about the Mlir-commits
mailing list