Skip to content

Commit ec18d2b

Browse files
committed
[Tosa] : Fix integer overflow for computing intmax+1 in tosa.cast to linalg.
1 parent a3a9ba8 commit ec18d2b

File tree

2 files changed

+28
-1
lines changed

2 files changed

+28
-1
lines changed

mlir/lib/Conversion/TosaToLinalg/TosaToLinalg.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -563,7 +563,7 @@ static Value createLinalgBodyCalculationForElementwiseOp(
563563
getElementTypeOrSelf(srcTy),
564564
APInt::getSignedMaxValue(dstTy.getIntOrFloatBitWidth())
565565
.getSExtValue() +
566-
1));
566+
1.0));
567567

568568
auto intMax = rewriter.create<arith::ConstantOp>(
569569
loc, rewriter.getIntegerAttr(

mlir/test/Conversion/TosaToLinalg/tosa-to-linalg.mlir

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1929,3 +1929,30 @@ func.func @test_dynamic_fft2d(%arg0: tensor<?x?x?xf32>, %arg1: tensor<?x?x?xf32>
19291929
%output_real, %output_imag = "tosa.fft2d"(%arg0, %arg1) {inverse = true} : (tensor<?x?x?xf32>, tensor<?x?x?xf32>) -> (tensor<?x?x?xf32>, tensor<?x?x?xf32>)
19301930
return %output_real, %output_imag : tensor<?x?x?xf32>, tensor<?x?x?xf32>
19311931
}
1932+
1933+
1934+
// -----
1935+
1936+
// CHECK: #[[$MAP0:.+]] = affine_map<(d0) -> (0)>
1937+
// CHECK: #[[$MAP1:.+]] = affine_map<(d0) -> (d0)>
1938+
1939+
// CHECK-LABEL: func.func @test_cast_fp32_i64(
1940+
// CHECK-SAME: %[[ARG0:.*]]: tensor<1xf32>) -> tensor<1xi64> {
1941+
// CHECK: %[[VAL_0:.*]] = tensor.empty() : tensor<1xi64>
1942+
// CHECK: %[[RESULT:.*]] = linalg.generic {indexing_maps = [#[[$MAP0]], #[[$MAP1]]], iterator_types = ["parallel"]} ins(%[[ARG0]] : tensor<1xf32>) outs(%[[VAL_0]] : tensor<1xi64>) {
1943+
// CHECK: ^bb0(%[[VAL_2:.*]]: f32, %[[VAL_3:.*]]: i64):
1944+
// CHECK: %[[VAL_4:.*]] = math.roundeven %[[VAL_2]] : f32
1945+
// CHECK: %[[VAL_5:.*]] = arith.constant -9.22337203E+18 : f32
1946+
// CHECK: %[[VAL_6:.*]] = arith.constant 9.22337203E+18 : f32
1947+
// CHECK: %[[VAL_7:.*]] = arith.constant 9223372036854775807 : i64
1948+
// CHECK: %[[VAL_8:.*]] = arith.maximumf %[[VAL_4]], %[[VAL_5]] : f32
1949+
// CHECK: %[[VAL_9:.*]] = arith.fptosi %[[VAL_8]] : f32 to i64
1950+
// CHECK: %[[VAL_10:.*]] = arith.cmpf uge, %[[VAL_4]], %[[VAL_6]] : f32
1951+
// CHECK: %[[VAL_11:.*]] = arith.select %[[VAL_10]], %[[VAL_7]], %[[VAL_9]] : i64
1952+
// CHECK: linalg.yield %[[VAL_11]] : i64
1953+
// CHECK: } -> tensor<1xi64>
1954+
// CHECK: return %[[RESULT]] : tensor<1xi64>
1955+
func.func @test_cast_fp32_i64(%arg0: tensor<1xf32>) -> (tensor<1xi64>) {
1956+
%0 = tosa.cast %arg0 : (tensor<1xf32>) -> tensor<1xi64>
1957+
return %0: tensor<1xi64>
1958+
}

0 commit comments

Comments
 (0)