-
Notifications
You must be signed in to change notification settings - Fork 13.3k
[mlir][tosa] Avoid overflow in reduction folders #132786
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[mlir][tosa] Avoid overflow in reduction folders #132786
Conversation
Thank you for submitting a Pull Request (PR) to the LLVM Project! This PR will be automatically labeled and the relevant teams will be notified. If you wish to, you can add reviewers by using the "Reviewers" section on this page. If this is not working for you, it is probably because you do not have write permissions for the repository. In which case you can instead tag reviewers by name in a comment by using If you have received no comments on your PR for a week, you can request a review by "ping"ing the PR by adding a comment “Ping”. The common courtesy "ping" rate is once a week. Please remember that you are asking for valuable time from other developers. If you have further questions, they may be answered by the LLVM GitHub User Guide. You can also ask questions in a comment on this PR, on the LLVM Discord or on the forums. |
@llvm/pr-subscribers-mlir @llvm/pr-subscribers-mlir-tosa Author: None (IanTaylerLessa-arm) ChangesAvoid operations that can overflow in constant folders for Includes tests to avoid regressions Full diff: https://github.com/llvm/llvm-project/pull/132786.diff 2 Files Affected:
diff --git a/mlir/include/mlir/Dialect/Tosa/IR/TosaOps.td b/mlir/include/mlir/Dialect/Tosa/IR/TosaOps.td
index 14e15173de7bc..1ae6463179970 100644
--- a/mlir/include/mlir/Dialect/Tosa/IR/TosaOps.td
+++ b/mlir/include/mlir/Dialect/Tosa/IR/TosaOps.td
@@ -1731,8 +1731,7 @@ def Tosa_ReduceMaxOp : Tosa_InferTensorTypeOp<"reduce_max"> {
/// Return the max of the two integer operands
static inline APInt calcOneElement(APInt leftOperand, APInt rightOperand) {
- const llvm::APInt subtractRes = leftOperand - rightOperand;
- return (!subtractRes.isNegative()) ? leftOperand : rightOperand;
+ return (leftOperand.sge(rightOperand)) ? leftOperand : rightOperand;
}
}];
}
@@ -1772,8 +1771,7 @@ def Tosa_ReduceMinOp : Tosa_InferTensorTypeOp<"reduce_min"> {
/// Return the min of the two integer operands
static inline APInt calcOneElement(APInt leftOperand, APInt rightOperand) {
- const llvm::APInt subtractRes = leftOperand - rightOperand;
- return (!subtractRes.isNegative()) ? rightOperand : leftOperand;
+ return (leftOperand.sle(rightOperand)) ? leftOperand : rightOperand;
}
}];
}
diff --git a/mlir/test/Dialect/Tosa/constant-op-fold.mlir b/mlir/test/Dialect/Tosa/constant-op-fold.mlir
index 8ac1e177ae4d4..b8b8e8d69fb16 100644
--- a/mlir/test/Dialect/Tosa/constant-op-fold.mlir
+++ b/mlir/test/Dialect/Tosa/constant-op-fold.mlir
@@ -883,6 +883,18 @@ func.func @reduce_max_constant() -> tensor<1x1x1xi32> {
return %0 : tensor<1x1x1xi32>
}
+// -----
+
+func.func @reduce_max_constant_no_overflow() -> tensor<1xi8> {
+ // CHECK-LABEL: func.func @reduce_max_constant_no_overflow() -> tensor<1xi8> {
+ // CHECK: %[[VAL_0:.*]] = "tosa.const"() <{values = dense<120> : tensor<1xi8>}> : () -> tensor<1xi8>
+ // CHECK: return %[[VAL_0]] : tensor<1xi8>
+ // CHECK: }
+ %const = "tosa.const"() <{values = dense<[-127, 120, -126]> : tensor<3xi8>}> : () -> tensor<3xi8>
+ %0 = tosa.reduce_max %const {axis = 0 : i32} : (tensor<3xi8>) -> tensor<1xi8>
+ return %0 : tensor<1xi8>
+}
+
// -----
func.func @reduce_min_constant() -> tensor<1x3xi32> {
@@ -968,6 +980,19 @@ func.func @reduce_min_constant() -> tensor<1x1x1xi32> {
return %0 : tensor<1x1x1xi32>
}
+// -----
+
+func.func @reduce_min_constant_no_overflow() -> tensor<1xi8> {
+ // CHECK-LABEL: func.func @reduce_min_constant_no_overflow() -> tensor<1xi8> {
+ // CHECK: %[[VAL_0:.*]] = "tosa.const"() <{values = dense<-127> : tensor<1xi8>}> : () -> tensor<1xi8>
+ // CHECK: return %[[VAL_0]] : tensor<1xi8>
+ // CHECK: }
+ %const = "tosa.const"() <{values = dense<[-127, 120, -126]> : tensor<3xi8>}> : () -> tensor<3xi8>
+ %0 = tosa.reduce_min %const {axis = 0 : i32} : (tensor<3xi8>) -> tensor<1xi8>
+ return %0 : tensor<1xi8>
+}
+
+
// -----
func.func @reduce_any_constant() -> tensor<1x3xi1> {
|
cc for review: @lhutton1 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks, the changes look good to me! The CI failure seems unrelated, could you force push to retrigger?
ede11f4
to
095f8be
Compare
Rebased and force-pushed. |
It's still failing with
I see the same error in at least one other open PR (#132904) but curiously most other mlir-related open PRs don't seem to have the same error. |
Avoid operations that can overflow in constant folders for tosa.reduce_max and tosa.reduce_min Includes tests to avoid regressions Signed-off-by: Ian Tayler Lessa <ian.taylerlessa@arm.com>
c04ca68
to
9391bbb
Compare
@IanTaylerLessa-arm Congratulations on having your first Pull Request (PR) merged into the LLVM Project! Your changes will be combined with recent changes from other authors, then tested by our build bots. If there is a problem with a build, you may receive a report in an email or a comment on this PR. Please check whether problems have been caused by your change specifically, as the builds can include changes from many authors. It is not uncommon for your change to be included in a build that fails due to someone else's changes, or infrastructure issues. How to do this, and the rest of the post-merge process, is covered in detail here. If your change does cause a problem, it may be reverted, or you can revert it yourself. This is a normal part of LLVM development. You can fix your changes and open a new PR to merge them again. If you don't get any reports, no action is required from you. Your changes are working as expected, well done! |
Avoid operations that can overflow in constant folders for
tosa.reduce_max
andtosa.reduce_min
Includes tests to avoid regressions