[mlir][arithmetic] Add tests for IndexCast folding ops and fix assert
authorThomas Raoux <thomasraoux@google.com>
Mon, 3 Oct 2022 19:39:32 +0000 (19:39 +0000)
committerThomas Raoux <thomasraoux@google.com>
Mon, 3 Oct 2022 20:28:09 +0000 (20:28 +0000)
Fix assert in IndexCastUI folding and add tests for both IndexCastOp and
IndexCastUIOp folding

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

mlir/lib/Dialect/Arith/IR/ArithOps.cpp
mlir/test/Dialect/Arith/canonicalize.mlir

index 190a1ef..0378f5f 100644 (file)
@@ -1308,7 +1308,7 @@ OpFoldResult arith::IndexCastUIOp::fold(ArrayRef<Attribute> operands) {
   // A little hack because we go through int. Otherwise, the size of the
   // constant might need to change.
   if (auto value = operands[0].dyn_cast_or_null<IntegerAttr>())
-    return IntegerAttr::get(getType(), value.getUInt());
+    return IntegerAttr::get(getType(), value.getValue().getZExtValue());
 
   return {};
 }
index be680ac..337eec0 100644 (file)
@@ -317,6 +317,24 @@ func.func @indexCastUIOfUnsignedExtend(%arg0: i8) -> index {
   return %idx : index
 }
 
+// CHECK-LABEL: @indexCastFold
+//       CHECK:   %[[res:.*]] = arith.constant -2 : index
+//       CHECK:   return %[[res]]
+func.func @indexCastFold(%arg0: i8) -> index {
+  %c-2 = arith.constant -2 : i8
+  %idx = arith.index_cast %c-2 : i8 to index
+  return %idx : index
+}
+
+// CHECK-LABEL: @indexCastUIFold
+//       CHECK:   %[[res:.*]] = arith.constant 254 : index
+//       CHECK:   return %[[res]]
+func.func @indexCastUIFold(%arg0: i8) -> index {
+  %c-2 = arith.constant -2 : i8
+  %idx = arith.index_castui %c-2 : i8 to index
+  return %idx : index
+}
+
 // CHECK-LABEL: @signExtendConstant
 //       CHECK:   %[[cres:.+]] = arith.constant -2 : i16
 //       CHECK:   return %[[cres]]