[PATCH] D73671: [MLIR][Standard] Add canonicalization for indexCast(indexCast(x))

Stephen Neuendorffer via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Wed Jan 29 16:06:05 PST 2020


stephenneuendorffer created this revision.
Herald added subscribers: llvm-commits, liufengdb, aartbik, lucyrfox, mgester, arpith-jacob, antiagainst, shauheen, burmako, jpienaar, rriddle, mehdi_amini, arphaman, mgorny.
Herald added a reviewer: nicolasvasilache.
Herald added a project: LLVM.
stephenneuendorffer added a child revision: D73672: [MLIR][Standard] Implement constant folding for IndexCast.

Depends on D73670 <https://reviews.llvm.org/D73670>


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D73671

Files:
  mlir/include/mlir/Dialect/StandardOps/CMakeLists.txt
  mlir/include/mlir/Dialect/StandardOps/Ops.td
  mlir/lib/Dialect/StandardOps/Ops.cpp
  mlir/test/Transforms/canonicalize.mlir


Index: mlir/test/Transforms/canonicalize.mlir
===================================================================
--- mlir/test/Transforms/canonicalize.mlir
+++ mlir/test/Transforms/canonicalize.mlir
@@ -878,3 +878,11 @@
   // CHECK: return %[[C7]], %[[C11]]
   return %7, %8 : index, index
 }
+
+// CHECK-LABEL: func @index_cast
+func @index_cast(%arg0: i16) -> (i16) {
+  %11 = index_cast %arg0 : i16 to index
+  %12 = index_cast %11 : index to i16
+  // CHECK: return %arg0 : i16
+  return %12 : i16
+}
Index: mlir/lib/Dialect/StandardOps/Ops.cpp
===================================================================
--- mlir/lib/Dialect/StandardOps/Ops.cpp
+++ mlir/lib/Dialect/StandardOps/Ops.cpp
@@ -30,6 +30,10 @@
 // Pull in all enum type definitions and utility function declarations.
 #include "mlir/Dialect/StandardOps/OpsEnums.cpp.inc"
 
+namespace mlir {
+#include "mlir/Dialect/StandardOps/Ops.inc"
+}
+
 using namespace mlir;
 
 //===----------------------------------------------------------------------===//
@@ -1627,6 +1631,12 @@
          (a.isa<IntegerType>() && b.isIndex());
 }
 
+void IndexCastOp::getCanonicalizationPatterns(OwningRewritePatternList &results,
+                                              MLIRContext *context) {
+  /// indexCast(indexCast x) -> x
+  results.insert<CombineIndexCastPattern>(context);
+}
+
 //===----------------------------------------------------------------------===//
 // LoadOp
 //===----------------------------------------------------------------------===//
Index: mlir/include/mlir/Dialect/StandardOps/Ops.td
===================================================================
--- mlir/include/mlir/Dialect/StandardOps/Ops.td
+++ mlir/include/mlir/Dialect/StandardOps/Ops.td
@@ -753,8 +753,14 @@
     static bool areCastCompatible(Type a, Type b);
   }];
 
+  let hasCanonicalizer = 1;
   let hasFolder = 0;
 }
+def TypesAreIdentical : Constraint<CPred<"$0.getType() == $1.getType()">>;
+def CombineIndexCastPattern :
+	 Pat<(IndexCastOp:$out (IndexCastOp $in)),
+		  (replaceWithValue $in),
+		  [(TypesAreIdentical $in, $out)]>;
 
 def FPExtOp : CastOp<"fpext">, Arguments<(ins AnyType:$in)> {
   let summary = "cast from floating-point to wider floating-point";
Index: mlir/include/mlir/Dialect/StandardOps/CMakeLists.txt
===================================================================
--- mlir/include/mlir/Dialect/StandardOps/CMakeLists.txt
+++ mlir/include/mlir/Dialect/StandardOps/CMakeLists.txt
@@ -1,4 +1,5 @@
 set(LLVM_TARGET_DEFINITIONS Ops.td)
+mlir_tablegen(Ops.inc -gen-rewriters)
 mlir_tablegen(Ops.h.inc -gen-op-decls)
 mlir_tablegen(Ops.cpp.inc -gen-op-defs)
 mlir_tablegen(OpsEnums.h.inc -gen-enum-decls)


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D73671.241311.patch
Type: text/x-patch
Size: 2691 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20200130/15cec23e/attachment.bin>


More information about the llvm-commits mailing list