diff --git a/mlir/lib/Dialect/StandardOps/IR/Ops.cpp b/mlir/lib/Dialect/StandardOps/IR/Ops.cpp --- a/mlir/lib/Dialect/StandardOps/IR/Ops.cpp +++ b/mlir/lib/Dialect/StandardOps/IR/Ops.cpp @@ -1560,9 +1560,13 @@ /// or(x, 0) -> x if (matchPattern(rhs(), m_Zero())) return lhs(); - /// or(x,x) -> x + /// or(x, x) -> x if (lhs() == rhs()) return rhs(); + /// or(x, ) -> + if (auto rhsAttr = operands[1].dyn_cast_or_null()) + if (rhsAttr.getValue().isAllOnes()) + return rhsAttr; return constFoldBinaryOp(operands, [](APInt a, APInt b) { return a | b; }); diff --git a/mlir/test/Transforms/canonicalize.mlir b/mlir/test/Transforms/canonicalize.mlir --- a/mlir/test/Transforms/canonicalize.mlir +++ b/mlir/test/Transforms/canonicalize.mlir @@ -287,6 +287,18 @@ return %1 : tensor<4x5xi32> } +// CHECK-LABEL: func @or_all_ones +func @or_all_ones(%arg0: i1, %arg1: i4) -> (i1, i4) { + // CHECK-DAG: %c-1_i4 = constant -1 : i4 + // CHECK-DAG: %true = constant true + %c1_i1 = constant 1 : i1 + %c15 = constant 15 : i4 + // CHECK-NEXT: return %true + %1 = or %arg0, %c1_i1 : i1 + %2 = or %arg1, %c15 : i4 + return %1, %2 : i1, i4 +} + //CHECK-LABEL: func @xor_self func @xor_self(%arg0: i32) -> i32 { //CHECK-NEXT: %c0_i32 = constant 0