[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:50:14 PST 2020


stephenneuendorffer edited the summary of this revision.
stephenneuendorffer updated this revision to Diff 241322.

Repository:
  rG LLVM Github Monorepo

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

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.241322.patch
Type: text/x-patch
Size: 1794 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20200130/87a83021/attachment.bin>


More information about the llvm-commits mailing list