[PATCH] D73671: [MLIR][Standard] Add folding for indexCast(indexCast(x)) -> x

Stephen Neuendorffer via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Mon Feb 10 09:37:24 PST 2020


stephenneuendorffer updated this revision to Diff 243600.

Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D73671/new/

https://reviews.llvm.org/D73671

Files:
  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,12 @@
   // CHECK: return %[[C7]], %[[C11]]
   return %7, %8 : index, index
 }
+
+// CHECK-LABEL: func @index_cast
+// CHECK-SAME: %[[ARG_0:arg[0-9]+]]: i16
+func @index_cast(%arg0: i16) -> (i16) {
+  %11 = index_cast %arg0 : i16 to index
+  %12 = index_cast %11 : index to i16
+  // CHECK: return %[[ARG_0]] : i16
+  return %12 : i16
+}
Index: mlir/lib/Dialect/StandardOps/Ops.cpp
===================================================================
--- mlir/lib/Dialect/StandardOps/Ops.cpp
+++ mlir/lib/Dialect/StandardOps/Ops.cpp
@@ -1586,6 +1586,14 @@
          (a.isa<IntegerType>() && b.isIndex());
 }
 
+OpFoldResult IndexCastOp::fold(ArrayRef<Attribute> cstOperands) {
+  // Fold IndexCast(IndexCast(x)) -> x
+  auto cast = dyn_cast_or_null<IndexCastOp>(getOperand().getDefiningOp());
+  if (cast && cast.getOperand().getType() == getType())
+    return cast.getOperand();
+  return {};
+}
+
 //===----------------------------------------------------------------------===//
 // LoadOp
 //===----------------------------------------------------------------------===//
Index: mlir/include/mlir/Dialect/StandardOps/Ops.td
===================================================================
--- mlir/include/mlir/Dialect/StandardOps/Ops.td
+++ mlir/include/mlir/Dialect/StandardOps/Ops.td
@@ -758,7 +758,7 @@
     static bool areCastCompatible(Type a, Type b);
   }];
 
-  let hasFolder = 0;
+  let hasFolder = 1;
 }
 
 def FPExtOp : CastOp<"fpext">, Arguments<(ins AnyType:$in)> {


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D73671.243600.patch
Type: text/x-patch
Size: 1700 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20200210/7db08bf3/attachment.bin>


More information about the llvm-commits mailing list