Skip to content

Commit b9a0eb6

Browse files
committed
[mlir][arithmetic] Add tests for IndexCast folding ops and fix assert
Fix assert in IndexCastUI folding and add tests for both IndexCastOp and IndexCastUIOp folding Differential Revision: https://reviews.llvm.org/D135098
1 parent 8927458 commit b9a0eb6

File tree

2 files changed

+19
-1
lines changed

2 files changed

+19
-1
lines changed

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1308,7 +1308,7 @@ OpFoldResult arith::IndexCastUIOp::fold(ArrayRef<Attribute> operands) {
13081308
// A little hack because we go through int. Otherwise, the size of the
13091309
// constant might need to change.
13101310
if (auto value = operands[0].dyn_cast_or_null<IntegerAttr>())
1311-
return IntegerAttr::get(getType(), value.getUInt());
1311+
return IntegerAttr::get(getType(), value.getValue().getZExtValue());
13121312

13131313
return {};
13141314
}

mlir/test/Dialect/Arith/canonicalize.mlir

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -317,6 +317,24 @@ func.func @indexCastUIOfUnsignedExtend(%arg0: i8) -> index {
317317
return %idx : index
318318
}
319319

320+
// CHECK-LABEL: @indexCastFold
321+
// CHECK: %[[res:.*]] = arith.constant -2 : index
322+
// CHECK: return %[[res]]
323+
func.func @indexCastFold(%arg0: i8) -> index {
324+
%c-2 = arith.constant -2 : i8
325+
%idx = arith.index_cast %c-2 : i8 to index
326+
return %idx : index
327+
}
328+
329+
// CHECK-LABEL: @indexCastUIFold
330+
// CHECK: %[[res:.*]] = arith.constant 254 : index
331+
// CHECK: return %[[res]]
332+
func.func @indexCastUIFold(%arg0: i8) -> index {
333+
%c-2 = arith.constant -2 : i8
334+
%idx = arith.index_castui %c-2 : i8 to index
335+
return %idx : index
336+
}
337+
320338
// CHECK-LABEL: @signExtendConstant
321339
// CHECK: %[[cres:.+]] = arith.constant -2 : i16
322340
// CHECK: return %[[cres]]

0 commit comments

Comments
 (0)