[Mlir-commits] [mlir] 2af81c6 - [MLIR][Arith] Canonicalize cmpi of extui/extsi
William S. Moses
llvmlistbot at llvm.org
Wed Mar 2 09:30:07 PST 2022
Author: William S. Moses
Date: 2022-03-02T12:30:03-05:00
New Revision: 2af81c69783487b8389ca7bb8d46da7a777750fc
URL: https://github.com/llvm/llvm-project/commit/2af81c69783487b8389ca7bb8d46da7a777750fc
DIFF: https://github.com/llvm/llvm-project/commit/2af81c69783487b8389ca7bb8d46da7a777750fc.diff
LOG: [MLIR][Arith] Canonicalize cmpi of extui/extsi
Canonicalize cmpi(eq, ext a, ext b) and cmpi(ne, ext a, ext b)
Reviewed By: ftynse
Differential Revision: https://reviews.llvm.org/D120620
Added:
Modified:
mlir/include/mlir/Dialect/Arithmetic/IR/ArithmeticOps.td
mlir/lib/Dialect/Arithmetic/IR/ArithmeticCanonicalization.td
mlir/lib/Dialect/Arithmetic/IR/ArithmeticOps.cpp
mlir/test/Dialect/Arithmetic/canonicalize.mlir
Removed:
################################################################################
diff --git a/mlir/include/mlir/Dialect/Arithmetic/IR/ArithmeticOps.td b/mlir/include/mlir/Dialect/Arithmetic/IR/ArithmeticOps.td
index ac8348c890262..86a0bc19c3d1c 100644
--- a/mlir/include/mlir/Dialect/Arithmetic/IR/ArithmeticOps.td
+++ b/mlir/include/mlir/Dialect/Arithmetic/IR/ArithmeticOps.td
@@ -1104,6 +1104,7 @@ def Arith_CmpIOp : Arith_CompareOpOfAnyRank<"cmpi"> {
}];
let hasFolder = 1;
+ let hasCanonicalizer = 1;
}
//===----------------------------------------------------------------------===//
diff --git a/mlir/lib/Dialect/Arithmetic/IR/ArithmeticCanonicalization.td b/mlir/lib/Dialect/Arithmetic/IR/ArithmeticCanonicalization.td
index efe41f048ce38..6bab58f7320e7 100644
--- a/mlir/lib/Dialect/Arithmetic/IR/ArithmeticCanonicalization.td
+++ b/mlir/lib/Dialect/Arithmetic/IR/ArithmeticCanonicalization.td
@@ -106,6 +106,28 @@ def XOrINotCmpI :
(Arith_ConstantOp ConstantAttr<I1Attr, "1">)),
(Arith_CmpIOp (InvertPredicate $pred), $a, $b)>;
+//===----------------------------------------------------------------------===//
+// CmpIOp
+//===----------------------------------------------------------------------===//
+
+// cmpi(== or !=, a ext iNN, b ext iNN) == cmpi(== or !=, a, b)
+def CmpIExtSI :
+ Pat<(Arith_CmpIOp $pred,
+ (Arith_ExtSIOp $a),
+ (Arith_ExtSIOp $b)),
+ (Arith_CmpIOp $pred, $a, $b),
+ [(Constraint<CPred<"$0.getType() == $1.getType()">> $a, $b),
+ (Constraint<CPred<"$0.getValue() == arith::CmpIPredicate::eq || $0.getValue() == arith::CmpIPredicate::ne">> $pred)]>;
+
+// cmpi(== or !=, a ext iNN, b ext iNN) == cmpi(== or !=, a, b)
+def CmpIExtUI :
+ Pat<(Arith_CmpIOp $pred,
+ (Arith_ExtUIOp $a),
+ (Arith_ExtUIOp $b)),
+ (Arith_CmpIOp $pred, $a, $b),
+ [(Constraint<CPred<"$0.getType() == $1.getType()">> $a, $b),
+ (Constraint<CPred<"$0.getValue() == arith::CmpIPredicate::eq || $0.getValue() == arith::CmpIPredicate::ne">> $pred)]>;
+
//===----------------------------------------------------------------------===//
// IndexCastOp
//===----------------------------------------------------------------------===//
diff --git a/mlir/lib/Dialect/Arithmetic/IR/ArithmeticOps.cpp b/mlir/lib/Dialect/Arithmetic/IR/ArithmeticOps.cpp
index f7f3da1e49486..34aae6422d4e5 100644
--- a/mlir/lib/Dialect/Arithmetic/IR/ArithmeticOps.cpp
+++ b/mlir/lib/Dialect/Arithmetic/IR/ArithmeticOps.cpp
@@ -1322,6 +1322,11 @@ OpFoldResult arith::CmpIOp::fold(ArrayRef<Attribute> operands) {
return BoolAttr::get(getContext(), val);
}
+void arith::CmpIOp::getCanonicalizationPatterns(RewritePatternSet &patterns,
+ MLIRContext *context) {
+ patterns.insert<CmpIExtSI, CmpIExtUI>(context);
+}
+
//===----------------------------------------------------------------------===//
// CmpFOp
//===----------------------------------------------------------------------===//
diff --git a/mlir/test/Dialect/Arithmetic/canonicalize.mlir b/mlir/test/Dialect/Arithmetic/canonicalize.mlir
index ac50c4367a380..d4b551455d070 100644
--- a/mlir/test/Dialect/Arithmetic/canonicalize.mlir
+++ b/mlir/test/Dialect/Arithmetic/canonicalize.mlir
@@ -176,6 +176,48 @@ func @extSIOfExtSI(%arg0: i1) -> i64 {
// -----
+// CHECK-LABEL: @cmpIExtSINE
+// CHECK: %[[comb:.+]] = arith.cmpi ne, %arg0, %arg1 : i8
+// CHECK: return %[[comb]]
+func @cmpIExtSINE(%arg0: i8, %arg1: i8) -> i1 {
+ %ext0 = arith.extsi %arg0 : i8 to i64
+ %ext1 = arith.extsi %arg1 : i8 to i64
+ %res = arith.cmpi ne, %ext0, %ext1 : i64
+ return %res : i1
+}
+
+// CHECK-LABEL: @cmpIExtSIEQ
+// CHECK: %[[comb:.+]] = arith.cmpi eq, %arg0, %arg1 : i8
+// CHECK: return %[[comb]]
+func @cmpIExtSIEQ(%arg0: i8, %arg1: i8) -> i1 {
+ %ext0 = arith.extsi %arg0 : i8 to i64
+ %ext1 = arith.extsi %arg1 : i8 to i64
+ %res = arith.cmpi eq, %ext0, %ext1 : i64
+ return %res : i1
+}
+
+// CHECK-LABEL: @cmpIExtUINE
+// CHECK: %[[comb:.+]] = arith.cmpi ne, %arg0, %arg1 : i8
+// CHECK: return %[[comb]]
+func @cmpIExtUINE(%arg0: i8, %arg1: i8) -> i1 {
+ %ext0 = arith.extui %arg0 : i8 to i64
+ %ext1 = arith.extui %arg1 : i8 to i64
+ %res = arith.cmpi ne, %ext0, %ext1 : i64
+ return %res : i1
+}
+
+// CHECK-LABEL: @cmpIExtUIEQ
+// CHECK: %[[comb:.+]] = arith.cmpi eq, %arg0, %arg1 : i8
+// CHECK: return %[[comb]]
+func @cmpIExtUIEQ(%arg0: i8, %arg1: i8) -> i1 {
+ %ext0 = arith.extui %arg0 : i8 to i64
+ %ext1 = arith.extui %arg1 : i8 to i64
+ %res = arith.cmpi eq, %ext0, %ext1 : i64
+ return %res : i1
+}
+
+// -----
+
// CHECK-LABEL: @andOfExtSI
// CHECK: %[[comb:.+]] = arith.andi %arg0, %arg1 : i8
// CHECK: %[[ext:.+]] = arith.extsi %[[comb]] : i8 to i64
More information about the Mlir-commits
mailing list