[PATCH] D73672: [MLIR][Standard] Implement constant folding for IndexCast

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, lucyrfox, mgester, arpith-jacob, antiagainst, shauheen, burmako, jpienaar, rriddle, mehdi_amini, arphaman.
Herald added a reviewer: nicolasvasilache.
Herald added a project: LLVM.

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


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D73672

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
@@ -886,3 +886,13 @@
   // CHECK: return %arg0 : i16
   return %12 : i16
 }
+
+// CHECK-LABEL: func @index_cast_fold
+func @index_cast_fold() -> (i16, index) {
+  %c4 = constant 4 : index
+  %1 = index_cast %c4 : index to i16
+  %c4_i16 = constant 4 : i16
+  %2 = index_cast %c4_i16 : i16 to index
+  // CHECK: return %c4_i16, %c4 : i16, index
+  return %1, %2 : i16, index
+}
Index: mlir/lib/Dialect/StandardOps/Ops.cpp
===================================================================
--- mlir/lib/Dialect/StandardOps/Ops.cpp
+++ mlir/lib/Dialect/StandardOps/Ops.cpp
@@ -1637,6 +1637,16 @@
   results.insert<CombineIndexCastPattern>(context);
 }
 
+OpFoldResult IndexCastOp::fold(ArrayRef<Attribute> operands) {
+    auto a = operands[0].dyn_cast_or_null<IntegerAttr>();
+    if(a) {
+      // A little hack because we go through int.  Otherwise, the size
+      // of the constant might need to change.
+      return IntegerAttr::get(getType(), a.getInt());
+    }
+    return IntegerAttr();
+}
+
 //===----------------------------------------------------------------------===//
 // LoadOp
 //===----------------------------------------------------------------------===//
Index: mlir/include/mlir/Dialect/StandardOps/Ops.td
===================================================================
--- mlir/include/mlir/Dialect/StandardOps/Ops.td
+++ mlir/include/mlir/Dialect/StandardOps/Ops.td
@@ -754,7 +754,7 @@
   }];
 
   let hasCanonicalizer = 1;
-  let hasFolder = 0;
+  let hasFolder = 1;
 }
 def TypesAreIdentical : Constraint<CPred<"$0.getType() == $1.getType()">>;
 def CombineIndexCastPattern :


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D73672.241312.patch
Type: text/x-patch
Size: 1807 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20200130/b483a201/attachment.bin>


More information about the llvm-commits mailing list