[Mlir-commits] [mlir] 8aa16d1 - [mlir][index] Add identity folders for add and sub

Jeff Niu llvmlistbot at llvm.org
Mon Jun 26 11:49:26 PDT 2023


Author: Jeff Niu
Date: 2023-06-26T11:49:16-07:00
New Revision: 8aa16d1921f15a80be5890a8b4bf7f5d3ec8fa00

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

LOG: [mlir][index] Add identity folders for add and sub

Depends on D153736

Reviewed By: rriddle, jpienaar

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

Added: 
    

Modified: 
    mlir/lib/Dialect/Index/IR/IndexOps.cpp
    mlir/test/Dialect/Index/index-canonicalize.mlir

Removed: 
    


################################################################################
diff  --git a/mlir/lib/Dialect/Index/IR/IndexOps.cpp b/mlir/lib/Dialect/Index/IR/IndexOps.cpp
index 8762ce81d43ca..d315d48ca2d2a 100644
--- a/mlir/lib/Dialect/Index/IR/IndexOps.cpp
+++ b/mlir/lib/Dialect/Index/IR/IndexOps.cpp
@@ -122,9 +122,18 @@ static OpFoldResult foldBinaryOpChecked(
 //===----------------------------------------------------------------------===//
 
 OpFoldResult AddOp::fold(FoldAdaptor adaptor) {
-  return foldBinaryOpUnchecked(
-      adaptor.getOperands(),
-      [](const APInt &lhs, const APInt &rhs) { return lhs + rhs; });
+  if (OpFoldResult result = foldBinaryOpUnchecked(
+          adaptor.getOperands(),
+          [](const APInt &lhs, const APInt &rhs) { return lhs + rhs; }))
+    return result;
+
+  if (auto rhs = dyn_cast_or_null<IntegerAttr>(adaptor.getRhs())) {
+    // Fold `add(x, 0) -> x`.
+    if (rhs.getValue().isZero())
+      return getLhs();
+  }
+
+  return {};
 }
 
 //===----------------------------------------------------------------------===//
@@ -132,9 +141,18 @@ OpFoldResult AddOp::fold(FoldAdaptor adaptor) {
 //===----------------------------------------------------------------------===//
 
 OpFoldResult SubOp::fold(FoldAdaptor adaptor) {
-  return foldBinaryOpUnchecked(
-      adaptor.getOperands(),
-      [](const APInt &lhs, const APInt &rhs) { return lhs - rhs; });
+  if (OpFoldResult result = foldBinaryOpUnchecked(
+          adaptor.getOperands(),
+          [](const APInt &lhs, const APInt &rhs) { return lhs - rhs; }))
+    return result;
+
+  if (auto rhs = dyn_cast_or_null<IntegerAttr>(adaptor.getRhs())) {
+    // Fold `sub(x, 0) -> x`.
+    if (rhs.getValue().isZero())
+      return getLhs();
+  }
+
+  return {};
 }
 
 //===----------------------------------------------------------------------===//
@@ -144,8 +162,7 @@ OpFoldResult SubOp::fold(FoldAdaptor adaptor) {
 OpFoldResult MulOp::fold(FoldAdaptor adaptor) {
   if (OpFoldResult result = foldBinaryOpUnchecked(
           adaptor.getOperands(),
-          [](const APInt &lhs, const APInt &rhs) { return lhs * rhs; });
-      !result.isNull())
+          [](const APInt &lhs, const APInt &rhs) { return lhs * rhs; }))
     return result;
 
   if (auto rhs = dyn_cast_or_null<IntegerAttr>(adaptor.getRhs())) {

diff  --git a/mlir/test/Dialect/Index/index-canonicalize.mlir b/mlir/test/Dialect/Index/index-canonicalize.mlir
index 940e6b6812e5e..2a1fb39f1d483 100644
--- a/mlir/test/Dialect/Index/index-canonicalize.mlir
+++ b/mlir/test/Dialect/Index/index-canonicalize.mlir
@@ -531,3 +531,19 @@ func.func @mul_identity(%arg0: index) -> (index, index) {
   // CHECK: return %idx0, %arg0
   return %0, %1 : index, index
 }
+
+// CHECK-LABEL: @add_identity
+func.func @add_identity(%arg0: index) -> index {
+  %idx0 = index.constant 0
+  %0 = index.add %arg0, %idx0
+  // CHECK-NEXT: return %arg0
+  return %0 : index
+}
+
+// CHECK-LABEL: @sub_identity
+func.func @sub_identity(%arg0: index) -> index {
+  %idx0 = index.constant 0
+  %0 = index.sub %arg0, %idx0
+  // CHECK-NEXT: return %arg0
+  return %0 : index
+}


        


More information about the Mlir-commits mailing list