Skip to content

Commit 3009336

Browse files
Automerge: [mlir] Dialect conversion: Print note when replacement types do not match legalized types (#161802)
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) ```
2 parents bab7d46 + 83c135c commit 3009336

File tree

1 file changed

+38
-8
lines changed

1 file changed

+38
-8
lines changed

mlir/lib/Transforms/Utils/DialectConversion.cpp

Lines changed: 38 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1856,6 +1856,44 @@ void ConversionPatternRewriterImpl::replaceOp(
18561856
Operation *op, SmallVector<SmallVector<Value>> &&newValues) {
18571857
assert(newValues.size() == op->getNumResults() &&
18581858
"incorrect number of replacement values");
1859+
LLVM_DEBUG({
1860+
logger.startLine() << "** Replace : '" << op->getName() << "'(" << op
1861+
<< ")\n";
1862+
if (currentTypeConverter) {
1863+
// If the user-provided replacement types are different from the
1864+
// legalized types, as per the current type converter, print a note.
1865+
// In most cases, the replacement types are expected to match the types
1866+
// produced by the type converter, so this could indicate a bug in the
1867+
// user code.
1868+
for (auto [result, repls] :
1869+
llvm::zip_equal(op->getResults(), newValues)) {
1870+
Type resultType = result.getType();
1871+
auto logProlog = [&, repls = repls]() {
1872+
logger.startLine() << " Note: Replacing op result of type "
1873+
<< resultType << " with value(s) of type (";
1874+
llvm::interleaveComma(repls, logger.getOStream(), [&](Value v) {
1875+
logger.getOStream() << v.getType();
1876+
});
1877+
logger.getOStream() << ")";
1878+
};
1879+
SmallVector<Type> convertedTypes;
1880+
if (failed(currentTypeConverter->convertTypes(resultType,
1881+
convertedTypes))) {
1882+
logProlog();
1883+
logger.getOStream() << ", but the type converter failed to legalize "
1884+
"the original type.\n";
1885+
continue;
1886+
}
1887+
if (TypeRange(convertedTypes) != TypeRange(ValueRange(repls))) {
1888+
logProlog();
1889+
logger.getOStream() << ", but the legalized type(s) is/are (";
1890+
llvm::interleaveComma(convertedTypes, logger.getOStream(),
1891+
[&](Type t) { logger.getOStream() << t; });
1892+
logger.getOStream() << ")\n";
1893+
}
1894+
}
1895+
}
1896+
});
18591897

18601898
if (!config.allowPatternRollback) {
18611899
// Pattern rollback is not allowed: materialize all IR changes immediately.
@@ -2072,10 +2110,6 @@ void ConversionPatternRewriter::replaceOp(Operation *op, Operation *newOp) {
20722110
void ConversionPatternRewriter::replaceOp(Operation *op, ValueRange newValues) {
20732111
assert(op->getNumResults() == newValues.size() &&
20742112
"incorrect # of replacement values");
2075-
LLVM_DEBUG({
2076-
impl->logger.startLine()
2077-
<< "** Replace : '" << op->getName() << "'(" << op << ")\n";
2078-
});
20792113

20802114
// If the current insertion point is before the erased operation, we adjust
20812115
// the insertion point to be after the operation.
@@ -2093,10 +2127,6 @@ void ConversionPatternRewriter::replaceOpWithMultiple(
20932127
Operation *op, SmallVector<SmallVector<Value>> &&newValues) {
20942128
assert(op->getNumResults() == newValues.size() &&
20952129
"incorrect # of replacement values");
2096-
LLVM_DEBUG({
2097-
impl->logger.startLine()
2098-
<< "** Replace : '" << op->getName() << "'(" << op << ")\n";
2099-
});
21002130

21012131
// If the current insertion point is before the erased operation, we adjust
21022132
// the insertion point to be after the operation.

0 commit comments

Comments
 (0)