[MLIR][Shape] Add `shape.rank` operation
authorFrederik Gossen <frgossen@google.com>
Thu, 25 Jun 2020 08:25:06 +0000 (08:25 +0000)
committerFrederik Gossen <frgossen@google.com>
Thu, 25 Jun 2020 08:26:00 +0000 (08:26 +0000)
Add `shape.rank` operation to the shape dialect.

Differential Revision: https://reviews.llvm.org/D82028

mlir/include/mlir/Dialect/Shape/IR/ShapeOps.td
mlir/test/Dialect/Shape/ops.mlir

index 49714c9..0785a40 100644 (file)
@@ -169,6 +169,18 @@ def Shape_FromExtentTensorOp : Shape_Op<"from_extent_tensor", [NoSideEffect]> {
   let assemblyFormat = "attr-dict $input `:` type($input)";
 }
 
+def Shape_RankOp : Shape_Op<"rank", [NoSideEffect]> {
+  let summary = "Gets the rank of a shape";
+  let description = [{
+    Returns the rank of the shape, i.e. the number of extents.
+  }];
+
+  let arguments = (ins Shape_ShapeType:$shape);
+  let results = (outs Shape_SizeType:$rank);
+
+  let assemblyFormat = "attr-dict $shape";
+}
+
 def Shape_ToExtentTensorOp : Shape_Op<"to_extent_tensor", [NoSideEffect]> {
   let summary = "Creates a dimension tensor from a shape";
   let description = [{
index 2e40211..18953ef 100644 (file)
@@ -111,3 +111,8 @@ func @test_from_extent_tensor(%arg: tensor<?xindex>) -> !shape.shape {
   %0 = shape.from_extent_tensor %arg : tensor<?xindex>
   return %0 : !shape.shape
 }
+
+func @rank(%shape : !shape.shape) -> !shape.size {
+  %rank = shape.rank %shape
+  return %rank : !shape.size
+}