diff --git a/mlir/lib/Conversion/TosaToLinalg/TosaToLinalg.cpp b/mlir/lib/Conversion/TosaToLinalg/TosaToLinalg.cpp --- a/mlir/lib/Conversion/TosaToLinalg/TosaToLinalg.cpp +++ b/mlir/lib/Conversion/TosaToLinalg/TosaToLinalg.cpp @@ -339,6 +339,21 @@ } }; +// At the codegen level any identity operations should be removed. Any cases +// where identity is load-bearing (e.g. cross device computation) should be +// handled before lowering to codegen. +template +class IdentityNConverter : public OpRewritePattern { +public: + using OpRewritePattern::OpRewritePattern; + + LogicalResult matchAndRewrite(SrcOp op, + PatternRewriter &rewriter) const final { + rewriter.replaceOp(op, op.getOperation()->getOperands()); + return success(); + } +}; + } // namespace void mlir::tosa::populateTosaToLinalgOnTensorsConversionPatterns( @@ -358,6 +373,7 @@ PointwiseConverter, PointwiseConverter, PointwiseConverter, PointwiseConverter, PointwiseConverter, - PointwiseConverter, PointwiseConverter>( - context); + PointwiseConverter, PointwiseConverter, + IdentityNConverter, + IdentityNConverter>(context); } diff --git a/mlir/test/Conversion/TosaToLinalg/tosa-to-linalg.mlir b/mlir/test/Conversion/TosaToLinalg/tosa-to-linalg.mlir --- a/mlir/test/Conversion/TosaToLinalg/tosa-to-linalg.mlir +++ b/mlir/test/Conversion/TosaToLinalg/tosa-to-linalg.mlir @@ -258,3 +258,14 @@ return } +// ----- + +func @test_identity(%arg0: tensor<1xf32>, %arg1: tensor<1xi32>) -> (tensor<1xf32>, tensor<1xi32>) { + %0 = "tosa.identity"(%arg0) : (tensor<1xf32>) -> tensor<1xf32> + %1 = "tosa.identity"(%arg1) : (tensor<1xi32>) -> tensor<1xi32> + + %2:2 = "tosa.identityn"(%0, %1) : (tensor<1xf32>, tensor<1xi32>) -> (tensor<1xf32>, tensor<1xi32>) + + // CHECK: return %arg0, %arg1 + return %2#0, %2#1 : tensor<1xf32>, tensor<1xi32> +}