[Mlir-commits] [mlir] fdaa391 - [MLIR] Add `num_elements` to the shape dialect

Frederik Gossen llvmlistbot at llvm.org
Thu May 28 07:06:41 PDT 2020


Author: Frederik Gossen
Date: 2020-05-28T14:05:58Z
New Revision: fdaa391e3df3c3a555d933122b0ef85eaf5eb63c

URL: https://github.com/llvm/llvm-project/commit/fdaa391e3df3c3a555d933122b0ef85eaf5eb63c
DIFF: https://github.com/llvm/llvm-project/commit/fdaa391e3df3c3a555d933122b0ef85eaf5eb63c.diff

LOG: [MLIR] Add `num_elements` to the shape dialect

The operation `num_elements` determines the number of elements for a given
shape.
That is the product of its dimensions.

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

Added: 
    

Modified: 
    mlir/include/mlir/Dialect/Shape/IR/ShapeOps.td
    mlir/lib/Dialect/Shape/IR/Shape.cpp
    mlir/test/Dialect/Shape/canonicalize.mlir

Removed: 
    


################################################################################
diff  --git a/mlir/include/mlir/Dialect/Shape/IR/ShapeOps.td b/mlir/include/mlir/Dialect/Shape/IR/ShapeOps.td
index 57d1954a3199..0d300d3c64c8 100644
--- a/mlir/include/mlir/Dialect/Shape/IR/ShapeOps.td
+++ b/mlir/include/mlir/Dialect/Shape/IR/ShapeOps.td
@@ -278,6 +278,24 @@ def Shape_MulOp : Shape_Op<"mul", [SameOperandsAndResultType]> {
   let results = (outs Shape_SizeType:$result);
 }
 
+def Shape_NumElementsOp : Shape_Op<"num_elements", [
+    NoSideEffect,
+    DeclareOpInterfaceMethods<InferTypeOpInterface>]> {
+
+  let summary = "Returns the number of elements for a given shape";
+  let description = [{
+    Returns the number of elements for a given shape which is the product of its
+    dimensions.
+  }];
+
+  let arguments = (ins Shape_ShapeType:$shape);
+  let results = (outs Shape_SizeType:$result);
+
+  let assemblyFormat = "attr-dict $shape";
+
+  let hasFolder = 1;
+}
+
 def Shape_ReduceOp : Shape_Op<"reduce", []> {
   let summary = "Returns an expression reduced over a shape";
   let description = [{

diff  --git a/mlir/lib/Dialect/Shape/IR/Shape.cpp b/mlir/lib/Dialect/Shape/IR/Shape.cpp
index a077948fdd31..b0103e15fa35 100644
--- a/mlir/lib/Dialect/Shape/IR/Shape.cpp
+++ b/mlir/lib/Dialect/Shape/IR/Shape.cpp
@@ -332,6 +332,32 @@ OpFoldResult GetExtentOp::fold(ArrayRef<Attribute> operands) {
   return elements.getValue({dimToGet});
 }
 
+//===----------------------------------------------------------------------===//
+// NumElementsOp
+//===----------------------------------------------------------------------===//
+
+OpFoldResult NumElementsOp::fold(ArrayRef<Attribute> operands) {
+
+  // Fold only when argument constant.
+  Attribute shape = operands[0];
+  if (!shape)
+    return {};
+
+  APInt product(64, 1);
+  for (auto value : shape.cast<DenseIntElementsAttr>())
+    product *= value;
+  Builder builder(getContext());
+  return builder.getIndexAttr(product.getLimitedValue());
+}
+
+LogicalResult NumElementsOp::inferReturnTypes(
+    MLIRContext *context, Optional<Location> location, ValueRange operands,
+    DictionaryAttr attributes, RegionRange regions,
+    SmallVectorImpl<Type> &inferredReturnTypes) {
+  inferredReturnTypes.push_back(SizeType::get(context));
+  return success();
+}
+
 //===----------------------------------------------------------------------===//
 // ShapeOfOp
 //===----------------------------------------------------------------------===//

diff  --git a/mlir/test/Dialect/Shape/canonicalize.mlir b/mlir/test/Dialect/Shape/canonicalize.mlir
index 106171de6087..69c312e6dad7 100644
--- a/mlir/test/Dialect/Shape/canonicalize.mlir
+++ b/mlir/test/Dialect/Shape/canonicalize.mlir
@@ -160,6 +160,28 @@ func @nonfoldable_index_to_size(%ci : index) -> !shape.size {
   return %cs : !shape.size
 }
 
+// -----
+// Fold number of elements computation.
+// CHECK-LABEL: func @num_elements
+func @num_elements() -> !shape.size {
+  // CHECK-NOT: shape.const_shape
+  %shape = shape.const_shape [4, 5, 6]
+  // CHECK-NOT: shape.num_elements
+  %num_elements = shape.num_elements %shape
+  // CHECK: %[[NUM:.*]] = shape.const_size 120
+  // CHECK-NEXT: return %[[NUM]] : !shape.size
+  return %num_elements : !shape.size
+}
+
+// -----
+// No folding.
+// CHECK-LABEL: func @nonfoldable_num_elements
+func @nonfoldable_num_elements(%shape : !shape.shape) -> !shape.size {
+  // CHECK-NOT: shape.const_{{.*}}
+  %num_elements = shape.num_elements %shape
+  return %num_elements : !shape.size
+}
+
 // -----
 
 // Canonicalization of shape.get_extent


        


More information about the Mlir-commits mailing list