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,12 @@ /// 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, 1) -> 1 + if (matchPattern(rhs(), m_One())) + return rhs(); 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,15 @@ return %1 : tensor<4x5xi32> } +// CHECK-LABEL: func @or_one +func @or_one(%arg0: i32) -> i32 { + // CHECK-NEXT: %c1_i32 = constant 1 : i32 + %c1_i32 = constant 1 : i32 + // CHECK-NEXT: return %c1_i32 + %1 = or %arg0, %c1_i32 : i32 + return %1 : i32 +} + //CHECK-LABEL: func @xor_self func @xor_self(%arg0: i32) -> i32 { //CHECK-NEXT: %c0_i32 = constant 0