Skip to content

Commit 53b46bb

Browse files
authored
[mlir][tosa] Fix crash on attempt to fold int_div by zero (#128682)
Fixes #118268. Signed-off-by: Luke Hutton <[email protected]>
1 parent e58f475 commit 53b46bb

File tree

2 files changed

+16
-4
lines changed

2 files changed

+16
-4
lines changed

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

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -680,10 +680,11 @@ OpFoldResult IntDivOp::fold(FoldAdaptor adaptor) {
680680
return getInput1();
681681
}
682682

683-
if (rhsAttr && lhsAttr && rhsAttr.isSplat() && lhsAttr.isSplat()) {
684-
if (llvm::isa<IntegerType>(resultETy)) {
685-
APInt l = lhsAttr.getSplatValue<APInt>();
686-
APInt r = rhsAttr.getSplatValue<APInt>();
683+
if (rhsAttr && lhsAttr && rhsAttr.isSplat() && lhsAttr.isSplat() &&
684+
llvm::isa<IntegerType>(resultETy)) {
685+
APInt l = lhsAttr.getSplatValue<APInt>();
686+
APInt r = rhsAttr.getSplatValue<APInt>();
687+
if (!r.isZero()) {
687688
APInt result = l.sdiv(r);
688689
return DenseElementsAttr::get(resultTy, result);
689690
}

mlir/test/Dialect/Tosa/canonicalize.mlir

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1012,3 +1012,14 @@ func.func nested @do_not_fold_reciprocal_int() -> tensor<3x600x1200xi32> {
10121012
%2 = "tosa.reciprocal"(%1): (tensor<3x600x1200xi32>) -> tensor<3x600x1200xi32>
10131013
return %2 : tensor<3x600x1200xi32>
10141014
}
1015+
1016+
// -----
1017+
1018+
// CHECK-LABEL: @do_not_fold_int_div_division_by_0
1019+
func.func @do_not_fold_int_div_division_by_0() -> tensor<1x24x2xi32> {
1020+
// CHECK: tosa.int_div
1021+
%1 = "tosa.const"() <{value = dense<0> : tensor<1x24x2xi32>}> : () -> tensor<1x24x2xi32>
1022+
%4 = "tosa.const"() <{value = dense<20> : tensor<1x24x2xi32>}> : () -> tensor<1x24x2xi32>
1023+
%16 = tosa.int_div %4, %1 : (tensor<1x24x2xi32>, tensor<1x24x2xi32>) -> tensor<1x24x2xi32>
1024+
return %16 : tensor<1x24x2xi32>
1025+
}

0 commit comments

Comments
 (0)