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

Stephen Neuendorffer via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Thu Feb 6 11:09:57 PST 2020


stephenneuendorffer updated this revision to Diff 242960.

Repository:
  rG LLVM Github Monorepo

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

https://reviews.llvm.org/D73672

Files:
  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
@@ -887,3 +887,15 @@
   // CHECK: return %[[ARG_0]] : 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: %[[C4_I16:.*]] = constant 4 : i16
+  // CHECK: %[[C4:.*]] = constant 4 : 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
@@ -1591,6 +1591,14 @@
   auto cast = dyn_cast_or_null<IndexCastOp>(getOperand().getDefiningOp());
   if (cast && cast.getOperand().getType() == getType())
     return cast.getOperand();
+
+  // Fold IndexCast(constant) -> constant
+  // A little hack because we go through int.  Otherwise, the size
+  // of the constant might need to change.
+  auto value = cstOperands[0].dyn_cast_or_null<IntegerAttr>();
+  if (value)
+    return IntegerAttr::get(getType(), value.getInt());
+
   return {};
 }
 


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D73672.242960.patch
Type: text/x-patch
Size: 1376 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20200206/59d19c1b/attachment.bin>


More information about the llvm-commits mailing list