[Mlir-commits] [mlir] ed56633 - [MLIR][Standard] Implement constant folding for IndexCast

Stephen Neuendorffer llvmlistbot at llvm.org
Mon Feb 10 10:25:06 PST 2020


Author: Stephen Neuendorffer
Date: 2020-02-10T10:23:56-08:00
New Revision: ed56633fb926e76c34ab9959428ef0f965f409f7

URL: https://github.com/llvm/llvm-project/commit/ed56633fb926e76c34ab9959428ef0f965f409f7
DIFF: https://github.com/llvm/llvm-project/commit/ed56633fb926e76c34ab9959428ef0f965f409f7.diff

LOG: [MLIR][Standard] Implement constant folding for IndexCast

Differential Revision: https://reviews.llvm.org/D73672

Added: 
    

Modified: 
    mlir/lib/Dialect/StandardOps/Ops.cpp
    mlir/test/Transforms/canonicalize.mlir

Removed: 
    


################################################################################
diff  --git a/mlir/lib/Dialect/StandardOps/Ops.cpp b/mlir/lib/Dialect/StandardOps/Ops.cpp
index e7bfae7ed6db..0b58fb73e91d 100644
--- a/mlir/lib/Dialect/StandardOps/Ops.cpp
+++ b/mlir/lib/Dialect/StandardOps/Ops.cpp
@@ -1591,6 +1591,13 @@ OpFoldResult IndexCastOp::fold(ArrayRef<Attribute> cstOperands) {
   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.
+  if (auto value = cstOperands[0].dyn_cast_or_null<IntegerAttr>())
+    return IntegerAttr::get(getType(), value.getInt());
+
   return {};
 }
 

diff  --git a/mlir/test/Transforms/canonicalize.mlir b/mlir/test/Transforms/canonicalize.mlir
index fb6e9c433ae8..e375a43af6f9 100644
--- a/mlir/test/Transforms/canonicalize.mlir
+++ b/mlir/test/Transforms/canonicalize.mlir
@@ -887,3 +887,15 @@ func @index_cast(%arg0: i16) -> (i16) {
   // 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
+}


        


More information about the Mlir-commits mailing list