[Mlir-commits] [mlir] 965ec6d - [mlir] Add folder for shape.add
Jacques Pienaar
llvmlistbot at llvm.org
Fri Oct 15 17:30:26 PDT 2021
Author: Jacques Pienaar
Date: 2021-10-15T17:30:17-07:00
New Revision: 965ec6dbe7e000605f74112031509120aa019e82
URL: https://github.com/llvm/llvm-project/commit/965ec6dbe7e000605f74112031509120aa019e82
DIFF: https://github.com/llvm/llvm-project/commit/965ec6dbe7e000605f74112031509120aa019e82.diff
LOG: [mlir] Add folder for shape.add
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 606f906290e3a..a05f7f30dd170 100644
--- a/mlir/include/mlir/Dialect/Shape/IR/ShapeOps.td
+++ b/mlir/include/mlir/Dialect/Shape/IR/ShapeOps.td
@@ -55,6 +55,8 @@ def Shape_AddOp : Shape_Op<"add",
// InferTypeOpInterface
static bool isCompatibleReturnTypes(TypeRange l, TypeRange r);
}];
+
+ let hasFolder = 1;
}
def Shape_BroadcastOp : Shape_Op<"broadcast", [Commutative, NoSideEffect]> {
diff --git a/mlir/lib/Dialect/Shape/IR/Shape.cpp b/mlir/lib/Dialect/Shape/IR/Shape.cpp
index d9ced5693e14a..f21fafead0b49 100644
--- a/mlir/lib/Dialect/Shape/IR/Shape.cpp
+++ b/mlir/lib/Dialect/Shape/IR/Shape.cpp
@@ -9,12 +9,14 @@
#include "mlir/Dialect/Shape/IR/Shape.h"
#include "mlir/Dialect/Arithmetic/IR/Arithmetic.h"
+#include "mlir/Dialect/CommonFolders.h"
#include "mlir/Dialect/StandardOps/IR/Ops.h"
#include "mlir/Dialect/Tensor/IR/Tensor.h"
#include "mlir/Dialect/Traits.h"
#include "mlir/IR/Builders.h"
#include "mlir/IR/BuiltinTypes.h"
#include "mlir/IR/DialectImplementation.h"
+#include "mlir/IR/Matchers.h"
#include "mlir/IR/PatternMatch.h"
#include "mlir/IR/TypeUtilities.h"
#include "mlir/Transforms/InliningUtils.h"
@@ -436,6 +438,15 @@ bool mlir::shape::AddOp::isCompatibleReturnTypes(TypeRange l, TypeRange r) {
return eachHasOnlyOneOfTypes<SizeType, IndexType>(l, r);
}
+OpFoldResult mlir::shape::AddOp::fold(ArrayRef<Attribute> operands) {
+ // add(x, 0) -> x
+ if (matchPattern(rhs(), m_Zero()))
+ return lhs();
+
+ return constFoldBinaryOp<IntegerAttr>(operands,
+ [](APInt a, APInt b) { return a + b; });
+}
+
//===----------------------------------------------------------------------===//
// AssumingAllOp
//===----------------------------------------------------------------------===//
diff --git a/mlir/test/Dialect/Shape/canonicalize.mlir b/mlir/test/Dialect/Shape/canonicalize.mlir
index c3e72a0b3021a..4a8839ba2b49d 100644
--- a/mlir/test/Dialect/Shape/canonicalize.mlir
+++ b/mlir/test/Dialect/Shape/canonicalize.mlir
@@ -1020,6 +1020,19 @@ func @shape_eq_do_not_fold(%a : !shape.shape) -> i1 {
// -----
+// Fold `add` for constant sizes.
+// CHECK-LABEL: @fold_add_size
+func @fold_add_size() -> !shape.size {
+ // CHECK: %[[RESULT:.*]] = shape.const_size 5
+ // CHECK: return %[[RESULT]] : !shape.size
+ %c2 = shape.const_size 2
+ %c3 = shape.const_size 3
+ %result = shape.add %c2, %c3 : !shape.size, !shape.size -> !shape.size
+ return %result : !shape.size
+}
+
+// -----
+
// Fold `mul` for constant sizes.
// CHECK-LABEL: @fold_mul_size
func @fold_mul_size() -> !shape.size {
More information about the Mlir-commits
mailing list