Skip to content

[mlir][Transforms] Dialect conversion: Add missing "else if" branch #101148

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 11 additions & 7 deletions mlir/lib/Transforms/Utils/DialectConversion.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1328,15 +1328,19 @@ Block *ConversionPatternRewriterImpl::applySignatureConversion(
mapping.map(origArg, argMat);
appendRewrite<ReplaceBlockArgRewrite>(block, origArg);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Another thing that I noticed while debugging around here, the line 1329 are adding to the map and adding a pending block argument rewrite, and so is line 1349 below. Also the builtin.unresolve_conversion_cast generated in line 1325 is overriden by the one generated in line 1345 in some cases (with the mapping changed as well). I dont think that was intended. Might just be a harmless issue, but might be hiding a bug there.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There are two mapping steps: origArg -> argMat -> targetMat. Note that the mapping is not overwritten here. In the second step, we map argMat, not origArg.

And we also generate two unrealized_conversion_cast ops. Such casts cannot be folded, unless they type(origArg) == type(targetMat).

It was actually on purpose that two casts in a row are generated here. The first one is an argument materialization and the second one is target materialization. Depending on the configuration of the type converter, we may not generate unrealized_conversion_cast, but custom ops.


// FIXME: We simply pass through the replacement argument if there wasn't a
// converter, which isn't great as it allows implicit type conversions to
// appear. We should properly restructure this code to handle cases where a
// converter isn't provided and also to properly handle the case where an
// argument materialization is actually a temporary source materialization
// (e.g. in the case of 1->N).
Type legalOutputType;
if (converter)
if (converter) {
legalOutputType = converter->convertType(origArgType);
} else if (replArgs.size() == 1) {
// When there is no type converter, assume that the new block argument
// types are legal. This is reasonable to assume because they were
// specified by the user.
// FIXME: This won't work for 1->N conversions because multiple output
// types are not supported in parts of the dialect conversion. In such a
// case, we currently use the original block argument type (produced by
// the argument materialization).
legalOutputType = replArgs[0].getType();
}
if (legalOutputType && legalOutputType != origArgType) {
Value targetMat = buildUnresolvedTargetMaterialization(
origArg.getLoc(), argMat, legalOutputType, converter);
Expand Down
15 changes: 15 additions & 0 deletions mlir/test/Transforms/test-legalize-type-conversion.mlir
Original file line number Diff line number Diff line change
Expand Up @@ -127,3 +127,18 @@ llvm.func @unsupported_func_op_interface() {
// CHECK: llvm.return
llvm.return
}

// -----

// CHECK-LABEL: func @test_signature_conversion_no_converter()
func.func @test_signature_conversion_no_converter() {
// CHECK: "test.signature_conversion_no_converter"() ({
// CHECK: ^{{.*}}(%[[arg0:.*]]: f64):
"test.signature_conversion_no_converter"() ({
^bb0(%arg0: f32):
// CHECK: "test.legal_op_d"(%[[arg0]]) : (f64) -> ()
"test.replace_with_legal_op"(%arg0) : (f32) -> ()
"test.return"() : () -> ()
}) : () -> ()
return
}
1 change: 1 addition & 0 deletions mlir/test/lib/Dialect/Test/TestOps.td
Original file line number Diff line number Diff line change
Expand Up @@ -1884,6 +1884,7 @@ def LegalOpA : TEST_Op<"legal_op_a">,
def LegalOpB : TEST_Op<"legal_op_b">, Results<(outs I32)>;
def LegalOpC : TEST_Op<"legal_op_c">,
Arguments<(ins I32)>, Results<(outs I32)>;
def LegalOpD : TEST_Op<"legal_op_d">, Arguments<(ins AnyType)>;

// Check that the conversion infrastructure can properly undo the creation of
// operations where an operation was created before its parent, in this case,
Expand Down
15 changes: 14 additions & 1 deletion mlir/test/lib/Dialect/Test/TestPatterns.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1580,6 +1580,17 @@ struct TestTypeConversionAnotherProducer
}
};

struct TestReplaceWithLegalOp : public ConversionPattern {
TestReplaceWithLegalOp(MLIRContext *ctx)
: ConversionPattern("test.replace_with_legal_op", /*benefit=*/1, ctx) {}
LogicalResult
matchAndRewrite(Operation *op, ArrayRef<Value> operands,
ConversionPatternRewriter &rewriter) const final {
rewriter.replaceOpWithNewOp<LegalOpD>(op, operands[0]);
return success();
}
};

struct TestTypeConversionDriver
: public PassWrapper<TestTypeConversionDriver, OperationPass<>> {
MLIR_DEFINE_EXPLICIT_INTERNAL_INLINE_TYPE_ID(TestTypeConversionDriver)
Expand Down Expand Up @@ -1671,6 +1682,7 @@ struct TestTypeConversionDriver

// Initialize the conversion target.
mlir::ConversionTarget target(getContext());
target.addLegalOp<LegalOpD>();
target.addDynamicallyLegalOp<TestTypeProducerOp>([](TestTypeProducerOp op) {
auto recursiveType = dyn_cast<test::TestRecursiveType>(op.getType());
return op.getType().isF64() || op.getType().isInteger(64) ||
Expand All @@ -1696,7 +1708,8 @@ struct TestTypeConversionDriver
TestSignatureConversionUndo,
TestTestSignatureConversionNoConverter>(converter,
&getContext());
patterns.add<TestTypeConversionAnotherProducer>(&getContext());
patterns.add<TestTypeConversionAnotherProducer, TestReplaceWithLegalOp>(
&getContext());
mlir::populateAnyFunctionOpInterfaceTypeConversionPattern(patterns,
converter);

Expand Down
Loading