[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
This commit is contained in:
MaheshRavishankar 2020-06-15 22:28:43 -07:00
parent e81bf67e8c
commit 462e3ccdd0
2 changed files with 102 additions and 5 deletions

View File

@ -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; });

View File

@ -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
}
// -----