-
Notifications
You must be signed in to change notification settings - Fork 12.8k
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] Fix crash on attempt to fold int_div by zero #128682
base: main
Are you sure you want to change the base?
Conversation
@llvm/pr-subscribers-mlir-tosa Author: Luke Hutton (lhutton1) ChangesFixes #118268. Full diff: https://github.com/llvm/llvm-project/pull/128682.diff 2 Files Affected:
diff --git a/mlir/lib/Dialect/Tosa/IR/TosaCanonicalizations.cpp b/mlir/lib/Dialect/Tosa/IR/TosaCanonicalizations.cpp
index 9bfc2aae1d6a5..3702dfa74a135 100644
--- a/mlir/lib/Dialect/Tosa/IR/TosaCanonicalizations.cpp
+++ b/mlir/lib/Dialect/Tosa/IR/TosaCanonicalizations.cpp
@@ -680,10 +680,10 @@ OpFoldResult IntDivOp::fold(FoldAdaptor adaptor) {
return getInput1();
}
- if (rhsAttr && lhsAttr && rhsAttr.isSplat() && lhsAttr.isSplat()) {
- if (llvm::isa<IntegerType>(resultETy)) {
- APInt l = lhsAttr.getSplatValue<APInt>();
- APInt r = rhsAttr.getSplatValue<APInt>();
+ if (rhsAttr && lhsAttr && rhsAttr.isSplat() && lhsAttr.isSplat() && llvm::isa<IntegerType>(resultETy)) {
+ APInt l = lhsAttr.getSplatValue<APInt>();
+ APInt r = rhsAttr.getSplatValue<APInt>();
+ if (!r.isZero()) {
APInt result = l.sdiv(r);
return DenseElementsAttr::get(resultTy, result);
}
diff --git a/mlir/test/Dialect/Tosa/canonicalize.mlir b/mlir/test/Dialect/Tosa/canonicalize.mlir
index 0e177a076ee7a..c08517b33b0f9 100644
--- a/mlir/test/Dialect/Tosa/canonicalize.mlir
+++ b/mlir/test/Dialect/Tosa/canonicalize.mlir
@@ -1012,3 +1012,14 @@ func.func nested @do_not_fold_reciprocal_int() -> tensor<3x600x1200xi32> {
%2 = "tosa.reciprocal"(%1): (tensor<3x600x1200xi32>) -> tensor<3x600x1200xi32>
return %2 : tensor<3x600x1200xi32>
}
+
+// -----
+
+// CHECK-LABEL: @do_not_fold_int_div_division_by_0
+func.func @do_not_fold_int_div_division_by_0() -> tensor<1x24x2xi32> {
+ // CHECK: tosa.int_div
+ %1 = "tosa.const"() <{value = dense<0> : tensor<1x24x2xi32>}> : () -> tensor<1x24x2xi32>
+ %4 = "tosa.const"() <{value = dense<20> : tensor<1x24x2xi32>}> : () -> tensor<1x24x2xi32>
+ %16 = tosa.int_div %4, %1 : (tensor<1x24x2xi32>, tensor<1x24x2xi32>) -> tensor<1x24x2xi32>
+ return %16 : tensor<1x24x2xi32>
+}
|
@llvm/pr-subscribers-mlir Author: Luke Hutton (lhutton1) ChangesFixes #118268. Full diff: https://github.com/llvm/llvm-project/pull/128682.diff 2 Files Affected:
diff --git a/mlir/lib/Dialect/Tosa/IR/TosaCanonicalizations.cpp b/mlir/lib/Dialect/Tosa/IR/TosaCanonicalizations.cpp
index 9bfc2aae1d6a5..3702dfa74a135 100644
--- a/mlir/lib/Dialect/Tosa/IR/TosaCanonicalizations.cpp
+++ b/mlir/lib/Dialect/Tosa/IR/TosaCanonicalizations.cpp
@@ -680,10 +680,10 @@ OpFoldResult IntDivOp::fold(FoldAdaptor adaptor) {
return getInput1();
}
- if (rhsAttr && lhsAttr && rhsAttr.isSplat() && lhsAttr.isSplat()) {
- if (llvm::isa<IntegerType>(resultETy)) {
- APInt l = lhsAttr.getSplatValue<APInt>();
- APInt r = rhsAttr.getSplatValue<APInt>();
+ if (rhsAttr && lhsAttr && rhsAttr.isSplat() && lhsAttr.isSplat() && llvm::isa<IntegerType>(resultETy)) {
+ APInt l = lhsAttr.getSplatValue<APInt>();
+ APInt r = rhsAttr.getSplatValue<APInt>();
+ if (!r.isZero()) {
APInt result = l.sdiv(r);
return DenseElementsAttr::get(resultTy, result);
}
diff --git a/mlir/test/Dialect/Tosa/canonicalize.mlir b/mlir/test/Dialect/Tosa/canonicalize.mlir
index 0e177a076ee7a..c08517b33b0f9 100644
--- a/mlir/test/Dialect/Tosa/canonicalize.mlir
+++ b/mlir/test/Dialect/Tosa/canonicalize.mlir
@@ -1012,3 +1012,14 @@ func.func nested @do_not_fold_reciprocal_int() -> tensor<3x600x1200xi32> {
%2 = "tosa.reciprocal"(%1): (tensor<3x600x1200xi32>) -> tensor<3x600x1200xi32>
return %2 : tensor<3x600x1200xi32>
}
+
+// -----
+
+// CHECK-LABEL: @do_not_fold_int_div_division_by_0
+func.func @do_not_fold_int_div_division_by_0() -> tensor<1x24x2xi32> {
+ // CHECK: tosa.int_div
+ %1 = "tosa.const"() <{value = dense<0> : tensor<1x24x2xi32>}> : () -> tensor<1x24x2xi32>
+ %4 = "tosa.const"() <{value = dense<20> : tensor<1x24x2xi32>}> : () -> tensor<1x24x2xi32>
+ %16 = tosa.int_div %4, %1 : (tensor<1x24x2xi32>, tensor<1x24x2xi32>) -> tensor<1x24x2xi32>
+ return %16 : tensor<1x24x2xi32>
+}
|
✅ With the latest revision this PR passed the C/C++ code formatter. |
Fixes llvm#118268. Change-Id: Ib3eeed6e796a573b30f04a992f4213862f0e0eb6 Signed-off-by: Luke Hutton <[email protected]>
759f7fb
to
c4b0ab4
Compare
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.
LGTM, thanks!
Fixes #118268.