Skip to content

Commit fb1143c

Browse files
authored
[FXML-4732] Fix TOSA cast constant op folding (#203)
1 parent 2127706 commit fb1143c

File tree

2 files changed

+12
-1
lines changed

2 files changed

+12
-1
lines changed

mlir/lib/Dialect/Tosa/IR/TosaCanonicalizations.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -888,7 +888,7 @@ OpFoldResult CastOp::fold(FoldAdaptor adaptor) {
888888
llvm::cast<IntegerType>(outETy).getIntOrFloatBitWidth(), unsign);
889889
auto floatVal = operand.getSplatValue<APFloat>();
890890
bool exact;
891-
floatVal.convertToInteger(intVal, llvm::RoundingMode::TowardZero, &exact);
891+
floatVal.convertToInteger(intVal, llvm::RoundingMode::NearestTiesToEven, &exact);
892892
return SplatElementsAttr::get(outTy, intVal);
893893
}
894894

mlir/test/Dialect/Tosa/constant-op-fold.mlir

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -602,6 +602,17 @@ func.func @cast_float_to_int() -> tensor<i16> {
602602

603603
// -----
604604

605+
// CHECK: func.func @cast_float_to_int_round
606+
func.func @cast_float_to_int_round() -> tensor<i16> {
607+
%splat = "tosa.const"() {value = dense<-3.5> : tensor<f32>} : () -> tensor<f32>
608+
// CHECK: %[[SPLAT:.+]] = "tosa.const"() <{value = dense<-4> : tensor<i16>}
609+
%cast = tosa.cast %splat : (tensor<f32>) -> tensor<i16>
610+
// CHECK: return %[[SPLAT]]
611+
return %cast : tensor<i16>
612+
}
613+
614+
// -----
615+
605616
// CHECK: func.func @cast_int_to_int_trunc
606617
func.func @cast_int_to_int_trunc() -> tensor<i16> {
607618
%splat = "tosa.const"() {value = dense<-1> : tensor<i32>} : () -> tensor<i32>

0 commit comments

Comments
 (0)