From: Frederik Gossen Date: Thu, 25 Jun 2020 08:42:40 +0000 (+0000) Subject: [MLIR][Shape] Lower `shape.rank` X-Git-Tag: llvmorg-12-init~1993 X-Git-Url: http://review.tizen.org/git/?a=commitdiff_plain;h=24debf5a76e03cfeab6df2530151e3061eb110f2;p=platform%2Fupstream%2Fllvm.git [MLIR][Shape] Lower `shape.rank` Lower `shape.rank` to standard dialect. A shape's size is the same as the extent of the first and only dimension of the `tensor` it is represented by. Differential Revision: https://reviews.llvm.org/D82080 --- diff --git a/mlir/lib/Conversion/ShapeToStandard/ShapeToStandard.cpp b/mlir/lib/Conversion/ShapeToStandard/ShapeToStandard.cpp index 6a02bdc..5fd9be0 100644 --- a/mlir/lib/Conversion/ShapeToStandard/ShapeToStandard.cpp +++ b/mlir/lib/Conversion/ShapeToStandard/ShapeToStandard.cpp @@ -90,6 +90,20 @@ public: } }; +class RankOpConverter : public OpConversionPattern { +public: + using OpConversionPattern::OpConversionPattern; + + LogicalResult + matchAndRewrite(shape::RankOp op, ArrayRef operands, + ConversionPatternRewriter &rewriter) const override { + shape::RankOp::Adaptor transformed(operands); + rewriter.replaceOpWithNewOp(op.getOperation(), transformed.shape(), + 0); + return success(); + } +}; + /// Type conversions. class ShapeTypeConverter : public TypeConverter { public: @@ -147,6 +161,7 @@ void mlir::populateShapeToStandardConversionPatterns( BinaryOpConversion, BinaryOpConversion, ConstSizeOpConverter, + RankOpConverter, ShapeOfOpConversion>(ctx); // clang-format on } diff --git a/mlir/test/Conversion/ShapeToStandard/shape-to-standard.mlir b/mlir/test/Conversion/ShapeToStandard/shape-to-standard.mlir index bfe3c2b..a9b4bf7 100644 --- a/mlir/test/Conversion/ShapeToStandard/shape-to-standard.mlir +++ b/mlir/test/Conversion/ShapeToStandard/shape-to-standard.mlir @@ -86,7 +86,6 @@ func @size_const() -> !shape.size { } // CHECK: %[[C1:.*]] = constant 1 : index // CHECK: return %[[C1]] : index - // ----- // Lower `shape_of` for statically shaped tensor. @@ -115,3 +114,16 @@ func @shape_of_dyn(%arg : tensor<1x5x?xf32>) { %shape = shape.shape_of %arg : tensor<1x5x?xf32> return } + +// ----- + +// Convert `rank` to `dim` of the first dimension. +// CHECK-LABEL: @rank +// CHECK-SAME: (%[[SHAPE:.*]]: tensor) -> index +func @rank(%shape : !shape.shape) -> !shape.size { + // CHECK-DAG: %[[C0:.*]] = constant 0 : index + // CHECK-DAG: %[[RESULT:.*]] = dim %[[SHAPE]], %[[C0]] + // CHECK-DAG: return %[[RESULT]] : index + %rank = shape.rank %shape + return %rank : !shape.size +}