[Mlir-commits] [mlir] 462e3cc - [mlir][StandardDialect] Add some folding for operations in standard dialect.

llvmlistbot at llvm.org llvmlistbot at llvm.org
Mon Jun 15 22:52:55 PDT 2020


Author: MaheshRavishankar
Date: 2020-06-15T22:52:29-07:00
New Revision: 462e3ccdd0d0ce233b8844e976432906d56f8a43

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

LOG: [mlir][StandardDialect] Add some folding for operations in standard dialect.

Add the following canonicalization
- and(x, 1) -> x
- subi(x, 0) -> x

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

Added: 
    

Modified: 
    mlir/lib/Dialect/StandardOps/IR/Ops.cpp
    mlir/test/Transforms/constant-fold.mlir

Removed: 
    


################################################################################
diff  --git a/mlir/lib/Dialect/StandardOps/IR/Ops.cpp b/mlir/lib/Dialect/StandardOps/IR/Ops.cpp
index 1812b8435f55..0ce03b3cf114 100644
--- a/mlir/lib/Dialect/StandardOps/IR/Ops.cpp
+++ b/mlir/lib/Dialect/StandardOps/IR/Ops.cpp
@@ -426,6 +426,11 @@ OpFoldResult AndOp::fold(ArrayRef<Attribute> operands) {
   /// and(x, 0) -> 0
   if (matchPattern(rhs(), m_Zero()))
     return rhs();
+  /// and(x, allOnes) -> x
+  APInt intValue;
+  if (matchPattern(rhs(), m_ConstantInt(&intValue)) &&
+      intValue.isAllOnesValue())
+    return lhs();
   /// and(x,x) -> x
   if (lhs() == rhs())
     return rhs();
@@ -2257,6 +2262,9 @@ OpFoldResult SubIOp::fold(ArrayRef<Attribute> operands) {
   // subi(x,x) -> 0
   if (getOperand(0) == getOperand(1))
     return Builder(getContext()).getZeroAttr(getType());
+  // subi(x,0) -> x
+  if (matchPattern(rhs(), m_Zero()))
+    return lhs();
 
   return constFoldBinaryOp<IntegerAttr>(operands,
                                         [](APInt a, APInt b) { return a - b; });

diff  --git a/mlir/test/Transforms/constant-fold.mlir b/mlir/test/Transforms/constant-fold.mlir
index 5562450a6955..0677b9572370 100644
--- a/mlir/test/Transforms/constant-fold.mlir
+++ b/mlir/test/Transforms/constant-fold.mlir
@@ -92,6 +92,92 @@ func @simple_addi() -> i32 {
 
 // -----
 
+// CHECK: func @simple_and
+// CHECK-SAME: [[ARG0:%[a-zA-Z0-9]+]]: i1
+// CHECK-SAME: [[ARG1:%[a-zA-Z0-9]+]]: i32)
+func @simple_and(%arg0 : i1, %arg1 : i32) -> (i1, i32) {
+  %c1 = constant 1 : i1
+  %cAllOnes_32 = constant 4294967295 : i32
+
+  // CHECK: [[C31:%.*]] = constant 31 : i32
+  %c31 = constant 31 : i32
+  %1 = and %arg0, %c1 : i1
+  %2 = and %arg1, %cAllOnes_32 : i32
+
+  // CHECK: [[VAL:%.*]] = and [[ARG1]], [[C31]]
+  %3 = and %2, %c31 : i32
+
+  // CHECK: return [[ARG0]], [[VAL]]
+  return %1, %3 : i1, i32
+}
+
+// -----
+
+// CHECK-LABEL: func @and_index
+//  CHECK-SAME:   [[ARG:%[a-zA-Z0-9]+]]
+func @and_index(%arg0 : index) -> (index) {
+  // CHECK: [[C31:%.*]] = constant 31 : index
+  %c31 = constant 31 : index
+  %c_AllOnes = constant -1 : index
+  %1 = and %arg0, %c31 : index
+
+  // CHECK: and [[ARG]], [[C31]]
+  %2 = and %1, %c_AllOnes : index
+  return %2 : index
+}
+
+// -----
+
+// CHECK: func @tensor_and
+// CHECK-SAME: [[ARG0:%[a-zA-Z0-9]+]]: tensor<2xi32>
+func @tensor_and(%arg0 : tensor<2xi32>) -> tensor<2xi32> {
+  %cAllOnes_32 = constant dense<4294967295> : tensor<2xi32>
+
+  // CHECK: [[C31:%.*]] = constant dense<31> : tensor<2xi32>
+  %c31 = constant dense<31> : tensor<2xi32>
+
+  // CHECK: [[CMIXED:%.*]] = constant dense<[31, -1]> : tensor<2xi32>
+  %c_mixed = constant dense<[31, 4294967295]> : tensor<2xi32>
+
+  %0 = and %arg0, %cAllOnes_32 : tensor<2xi32>
+
+  // CHECK: [[T1:%.*]] = and [[ARG0]], [[C31]]
+  %1 = and %0, %c31 : tensor<2xi32>
+
+  // CHECK: [[T2:%.*]] = and [[T1]], [[CMIXED]]
+  %2 = and %1, %c_mixed : tensor<2xi32>
+
+  // CHECK: return [[T2]]
+  return %2 : tensor<2xi32>
+}
+
+// -----
+
+// CHECK: func @vector_and
+// CHECK-SAME: [[ARG0:%[a-zA-Z0-9]+]]: vector<2xi32>
+func @vector_and(%arg0 : vector<2xi32>) -> vector<2xi32> {
+  %cAllOnes_32 = constant dense<4294967295> : vector<2xi32>
+
+  // CHECK: [[C31:%.*]] = constant dense<31> : vector<2xi32>
+  %c31 = constant dense<31> : vector<2xi32>
+
+  // CHECK: [[CMIXED:%.*]] = constant dense<[31, -1]> : vector<2xi32>
+  %c_mixed = constant dense<[31, 4294967295]> : vector<2xi32>
+
+  %0 = and %arg0, %cAllOnes_32 : vector<2xi32>
+
+  // CHECK: [[T1:%.*]] = and [[ARG0]], [[C31]]
+  %1 = and %0, %c31 : vector<2xi32>
+
+  // CHECK: [[T2:%.*]] = and [[T1]], [[CMIXED]]
+  %2 = and %1, %c_mixed : vector<2xi32>
+
+  // CHECK: return [[T2]]
+  return %2 : vector<2xi32>
+}
+
+// -----
+
 // CHECK-LABEL: func @addi_splat_vector
 func @addi_splat_vector() -> vector<8xi32> {
   %0 = constant dense<1> : vector<8xi32>
@@ -134,16 +220,19 @@ func @subf_splat_vector() -> vector<4xf32> {
 
 // -----
 
-// CHECK-LABEL: func @simple_subi
-func @simple_subi() -> i32 {
+//      CHECK: func @simple_subi
+// CHECK-SAME:   [[ARG0:%[a-zA-Z0-9]+]]
+func @simple_subi(%arg0 : i32) -> (i32, i32) {
   %0 = constant 4 : i32
   %1 = constant 1 : i32
+  %2 = constant 0 : i32
 
   // CHECK-NEXT:[[C3:%.+]] = constant 3 : i32
-  %2 = subi %0, %1 : i32
+  %3 = subi %0, %1 : i32
+  %4 = subi %arg0, %2 : i32
 
-  // CHECK-NEXT: return [[C3]]
-  return %2 : i32
+  // CHECK-NEXT: return [[C3]], [[ARG0]]
+  return %3, %4 : i32, i32
 }
 
 // -----


        


More information about the Mlir-commits mailing list