I want to convert
module {
func.func @const_shape() -> tensor<3xindex> {
%shape = shape.const_shape [1, 2, 3] : tensor<3xindex>
return %shape : tensor<3xindex>
}
}
into
module {
func.func @const_shape() -> i32 {
%0 = "arith.constant"() <{value = 1 : i32}> : () -> i32
%1 = "arith.constant"() <{value = 2 : i32}> : () -> i32
%2 = "arith.addi"(%0, %1) : (i32, i32) -> i32
return %2 : i32
}
}
It’s meaningless but simple enough to illstrute the conversion process. I write a conversion to create two arith.constant
op and replace arith.addi
op. But it seems not enough due to the dismatch with return type. What should I do to change the return type?
Here is my Conversion
namespace {
using namespace mlir;
class MyOpConversion : public OpConversionPattern<shape::ConstShapeOp> {
public:
using OpConversionPattern<shape::ConstShapeOp>::OpConversionPattern;
LogicalResult
matchAndRewrite(shape::ConstShapeOp op, OpAdaptor adaptor,
ConversionPatternRewriter &rewriter) const override{
Value op1 = rewriter.create<arith::ConstantOp>(op.getLoc(), rewriter.getI32IntegerAttr(1));
Value op2 = rewriter.create<arith::ConstantOp>(op.getLoc(), rewriter.getI32IntegerAttr(2));
auto res = rewriter.replaceOpWithNewOp<arith::AddIOp>(op, op1, op2);
rewriter.replaceAllUsesWith(op, res);
op->getBlock()->dump();
return success();
}
};
Here is my error log
* Pattern : 'shape.const_shape -> ()' {
Trying to match "(anonymous namespace)::MyOpConversion"
ImplicitTypeIDRegistry::lookupOrInsert(mlir::arith::detail::ConstantOpGenericAdaptorBase::Properties)
ImplicitTypeIDRegistry::lookupOrInsert(mlir::InferIntRangeInterface::Trait<Empty>)
** Insert : 'arith.constant'(0x55b9d1c910d0)
** Insert : 'arith.constant'(0x55b9d1c91140)
** Insert : 'arith.addi'(0x55b9d1c89760)
** Replace : 'shape.const_shape'(0x55b9d1c789e0)
mlir-asm-printer: Verifying operation: builtin.module
type of return operand 0 ('i32') doesn't match function result type ('tensor<3xindex>') in function @const_shape
mlir-asm-printer: 'builtin.module' failed to verify and will be printed in generic form
^bb0:
%0 = "arith.constant"() <{value = 1 : i32}> : () -> i32
%1 = "arith.constant"() <{value = 2 : i32}> : () -> i32
%2 = "arith.addi"(%0, %1) : (i32, i32) -> i32
%3 = "shape.const_shape"() <{shape = dense<[1, 2, 3]> : tensor<3xindex>}> : () -> tensor<3xindex>
"func.return"(%2) : (i32) -> ()
"(anonymous namespace)::MyOpConversion" result 1
//===-------------------------------------------===//
Legalizing operation : 'func.return'(0x55b9d1c8bff0) {
"func.return"(%2) : (i32) -> ()
* Fold {
xxx
} -> FAILURE : unable to fold
} -> FAILURE : no matched legalization pattern
//===-------------------------------------------===//
} -> FAILURE : failed to legalize operation updated in-place 'func.return'
} -> FAILURE : pattern failed to match
} -> FAILURE : no matched legalization pattern
//===-------------------------------------------===//
//===-------------------------------------------===//
Legalizing operation : 'func.return'(0x55b9d1c8bff0) {
"func.return"(%0) : (tensor<3xindex>) -> ()
* Fold {
} -> FAILURE : unable to fold
} -> FAILURE : no matched legalization pattern
//===-------------------------------------------===//