[Mlir-commits] [mlir] [Tosa] : Fix integer overflow for computing intmax+1 in tosa.cast to linalg. (PR #112455)
Sayan Saha
llvmlistbot at llvm.org
Thu Oct 24 10:20:18 PDT 2024
https://github.com/sahas3 updated https://github.com/llvm/llvm-project/pull/112455
>From ec18d2b58a8c8c2f64d264946df19cbf5bb564f1 Mon Sep 17 00:00:00 2001
From: Sayan Saha <sayans at mathworks.com>
Date: Tue, 15 Oct 2024 20:10:27 -0400
Subject: [PATCH 1/4] [Tosa] : Fix integer overflow for computing intmax+1 in
tosa.cast to linalg.
---
.../Conversion/TosaToLinalg/TosaToLinalg.cpp | 2 +-
.../TosaToLinalg/tosa-to-linalg.mlir | 27 +++++++++++++++++++
2 files changed, 28 insertions(+), 1 deletion(-)
diff --git a/mlir/lib/Conversion/TosaToLinalg/TosaToLinalg.cpp b/mlir/lib/Conversion/TosaToLinalg/TosaToLinalg.cpp
index c88f4db27c304e..e6b3e4b677e4f2 100644
--- a/mlir/lib/Conversion/TosaToLinalg/TosaToLinalg.cpp
+++ b/mlir/lib/Conversion/TosaToLinalg/TosaToLinalg.cpp
@@ -563,7 +563,7 @@ static Value createLinalgBodyCalculationForElementwiseOp(
getElementTypeOrSelf(srcTy),
APInt::getSignedMaxValue(dstTy.getIntOrFloatBitWidth())
.getSExtValue() +
- 1));
+ 1.0));
auto intMax = rewriter.create<arith::ConstantOp>(
loc, rewriter.getIntegerAttr(
diff --git a/mlir/test/Conversion/TosaToLinalg/tosa-to-linalg.mlir b/mlir/test/Conversion/TosaToLinalg/tosa-to-linalg.mlir
index f9d37f9427d4f4..7e2ec67d38d378 100644
--- a/mlir/test/Conversion/TosaToLinalg/tosa-to-linalg.mlir
+++ b/mlir/test/Conversion/TosaToLinalg/tosa-to-linalg.mlir
@@ -1929,3 +1929,30 @@ func.func @test_dynamic_fft2d(%arg0: tensor<?x?x?xf32>, %arg1: tensor<?x?x?xf32>
%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>)
return %output_real, %output_imag : tensor<?x?x?xf32>, tensor<?x?x?xf32>
}
+
+
+// -----
+
+// CHECK: #[[$MAP0:.+]] = affine_map<(d0) -> (0)>
+// CHECK: #[[$MAP1:.+]] = affine_map<(d0) -> (d0)>
+
+// CHECK-LABEL: func.func @test_cast_fp32_i64(
+// CHECK-SAME: %[[ARG0:.*]]: tensor<1xf32>) -> tensor<1xi64> {
+// CHECK: %[[VAL_0:.*]] = tensor.empty() : tensor<1xi64>
+// CHECK: %[[RESULT:.*]] = linalg.generic {indexing_maps = [#[[$MAP0]], #[[$MAP1]]], iterator_types = ["parallel"]} ins(%[[ARG0]] : tensor<1xf32>) outs(%[[VAL_0]] : tensor<1xi64>) {
+// CHECK: ^bb0(%[[VAL_2:.*]]: f32, %[[VAL_3:.*]]: i64):
+// CHECK: %[[VAL_4:.*]] = math.roundeven %[[VAL_2]] : f32
+// CHECK: %[[VAL_5:.*]] = arith.constant -9.22337203E+18 : f32
+// CHECK: %[[VAL_6:.*]] = arith.constant 9.22337203E+18 : f32
+// CHECK: %[[VAL_7:.*]] = arith.constant 9223372036854775807 : i64
+// CHECK: %[[VAL_8:.*]] = arith.maximumf %[[VAL_4]], %[[VAL_5]] : f32
+// CHECK: %[[VAL_9:.*]] = arith.fptosi %[[VAL_8]] : f32 to i64
+// CHECK: %[[VAL_10:.*]] = arith.cmpf uge, %[[VAL_4]], %[[VAL_6]] : f32
+// CHECK: %[[VAL_11:.*]] = arith.select %[[VAL_10]], %[[VAL_7]], %[[VAL_9]] : i64
+// CHECK: linalg.yield %[[VAL_11]] : i64
+// CHECK: } -> tensor<1xi64>
+// CHECK: return %[[RESULT]] : tensor<1xi64>
+func.func @test_cast_fp32_i64(%arg0: tensor<1xf32>) -> (tensor<1xi64>) {
+ %0 = tosa.cast %arg0 : (tensor<1xf32>) -> tensor<1xi64>
+ return %0: tensor<1xi64>
+}
>From d8cc8c60e935761b725b6aa99018e61d3a1f722e Mon Sep 17 00:00:00 2001
From: Sayan Saha <sayans at mathworks.com>
Date: Tue, 15 Oct 2024 22:27:10 -0400
Subject: [PATCH 2/4] Explicit cast to double for i64 intmax.
---
mlir/lib/Conversion/TosaToLinalg/TosaToLinalg.cpp | 6 +++---
1 file changed, 3 insertions(+), 3 deletions(-)
diff --git a/mlir/lib/Conversion/TosaToLinalg/TosaToLinalg.cpp b/mlir/lib/Conversion/TosaToLinalg/TosaToLinalg.cpp
index e6b3e4b677e4f2..62d80ba1ef5589 100644
--- a/mlir/lib/Conversion/TosaToLinalg/TosaToLinalg.cpp
+++ b/mlir/lib/Conversion/TosaToLinalg/TosaToLinalg.cpp
@@ -561,9 +561,9 @@ static Value createLinalgBodyCalculationForElementwiseOp(
auto intMaxPlusOneFP = rewriter.create<arith::ConstantOp>(
loc, rewriter.getFloatAttr(
getElementTypeOrSelf(srcTy),
- APInt::getSignedMaxValue(dstTy.getIntOrFloatBitWidth())
- .getSExtValue() +
- 1.0));
+ static_cast<double>(APInt::getSignedMaxValue(dstTy.getIntOrFloatBitWidth())
+ .getSExtValue()) +
+ 1.0f));
auto intMax = rewriter.create<arith::ConstantOp>(
loc, rewriter.getIntegerAttr(
>From 1a181846c27b160d8775db9798857a0b8bef3021 Mon Sep 17 00:00:00 2001
From: Sayan Saha <sayans at mathworks.com>
Date: Tue, 15 Oct 2024 22:38:30 -0400
Subject: [PATCH 3/4] Run clang-format.
---
mlir/lib/Conversion/TosaToLinalg/TosaToLinalg.cpp | 3 ++-
1 file changed, 2 insertions(+), 1 deletion(-)
diff --git a/mlir/lib/Conversion/TosaToLinalg/TosaToLinalg.cpp b/mlir/lib/Conversion/TosaToLinalg/TosaToLinalg.cpp
index 62d80ba1ef5589..1f178ae2990ff8 100644
--- a/mlir/lib/Conversion/TosaToLinalg/TosaToLinalg.cpp
+++ b/mlir/lib/Conversion/TosaToLinalg/TosaToLinalg.cpp
@@ -561,7 +561,8 @@ static Value createLinalgBodyCalculationForElementwiseOp(
auto intMaxPlusOneFP = rewriter.create<arith::ConstantOp>(
loc, rewriter.getFloatAttr(
getElementTypeOrSelf(srcTy),
- static_cast<double>(APInt::getSignedMaxValue(dstTy.getIntOrFloatBitWidth())
+ static_cast<double>(
+ APInt::getSignedMaxValue(dstTy.getIntOrFloatBitWidth())
.getSExtValue()) +
1.0f));
>From 8de70ef8f478325b653ef9ddfce573d22bb7a317 Mon Sep 17 00:00:00 2001
From: Sayan Saha <sayans at mathworks.com>
Date: Thu, 24 Oct 2024 13:19:58 -0400
Subject: [PATCH 4/4] Add meaningful variable names in lit test.
---
.../TosaToLinalg/tosa-to-linalg.mlir | 24 +++++++++----------
1 file changed, 12 insertions(+), 12 deletions(-)
diff --git a/mlir/test/Conversion/TosaToLinalg/tosa-to-linalg.mlir b/mlir/test/Conversion/TosaToLinalg/tosa-to-linalg.mlir
index 7e2ec67d38d378..1a29d3f9f3507c 100644
--- a/mlir/test/Conversion/TosaToLinalg/tosa-to-linalg.mlir
+++ b/mlir/test/Conversion/TosaToLinalg/tosa-to-linalg.mlir
@@ -1938,18 +1938,18 @@ func.func @test_dynamic_fft2d(%arg0: tensor<?x?x?xf32>, %arg1: tensor<?x?x?xf32>
// CHECK-LABEL: func.func @test_cast_fp32_i64(
// CHECK-SAME: %[[ARG0:.*]]: tensor<1xf32>) -> tensor<1xi64> {
-// CHECK: %[[VAL_0:.*]] = tensor.empty() : tensor<1xi64>
-// CHECK: %[[RESULT:.*]] = linalg.generic {indexing_maps = [#[[$MAP0]], #[[$MAP1]]], iterator_types = ["parallel"]} ins(%[[ARG0]] : tensor<1xf32>) outs(%[[VAL_0]] : tensor<1xi64>) {
-// CHECK: ^bb0(%[[VAL_2:.*]]: f32, %[[VAL_3:.*]]: i64):
-// CHECK: %[[VAL_4:.*]] = math.roundeven %[[VAL_2]] : f32
-// CHECK: %[[VAL_5:.*]] = arith.constant -9.22337203E+18 : f32
-// CHECK: %[[VAL_6:.*]] = arith.constant 9.22337203E+18 : f32
-// CHECK: %[[VAL_7:.*]] = arith.constant 9223372036854775807 : i64
-// CHECK: %[[VAL_8:.*]] = arith.maximumf %[[VAL_4]], %[[VAL_5]] : f32
-// CHECK: %[[VAL_9:.*]] = arith.fptosi %[[VAL_8]] : f32 to i64
-// CHECK: %[[VAL_10:.*]] = arith.cmpf uge, %[[VAL_4]], %[[VAL_6]] : f32
-// CHECK: %[[VAL_11:.*]] = arith.select %[[VAL_10]], %[[VAL_7]], %[[VAL_9]] : i64
-// CHECK: linalg.yield %[[VAL_11]] : i64
+// CHECK: %[[EMPTY_TENSOR:.*]] = tensor.empty() : tensor<1xi64>
+// CHECK: %[[RESULT:.*]] = linalg.generic {indexing_maps = [#[[$MAP0]], #[[$MAP1]]], iterator_types = ["parallel"]} ins(%[[ARG0]] : tensor<1xf32>) outs(%[[EMPTY_TENSOR]] : tensor<1xi64>) {
+// CHECK: ^bb0(%[[IN:.*]]: f32, %[[OUT:.*]]: i64):
+// CHECK: %[[ROUND_EVEN:.*]] = math.roundeven %[[IN]] : f32
+// CHECK: %[[FP_INT_MIN:.*]] = arith.constant -9.22337203E+18 : f32
+// CHECK: %[[FP_INT_MAX_PLUS_ONE:.*]] = arith.constant 9.22337203E+18 : f32
+// CHECK: %[[INT_MAX:.*]] = arith.constant 9223372036854775807 : i64
+// CHECK: %[[MAX:.*]] = arith.maximumf %[[ROUND_EVEN]], %[[FP_INT_MIN]] : f32
+// CHECK: %[[FPTOSI:.*]] = arith.fptosi %[[MAX]] : f32 to i64
+// CHECK: %[[CMPF:.*]] = arith.cmpf uge, %[[ROUND_EVEN]], %[[FP_INT_MAX_PLUS_ONE]] : f32
+// CHECK: %[[SELECT:.*]] = arith.select %[[CMPF]], %[[INT_MAX]], %[[FPTOSI]] : i64
+// CHECK: linalg.yield %[[SELECT]] : i64
// CHECK: } -> tensor<1xi64>
// CHECK: return %[[RESULT]] : tensor<1xi64>
func.func @test_cast_fp32_i64(%arg0: tensor<1xf32>) -> (tensor<1xi64>) {
More information about the Mlir-commits
mailing list