Skip to content

Commit

Permalink
[mlir][tosa] Fix crash on attempt to fold int_div by zero
Browse files Browse the repository at this point in the history
Fixes #118268.

Change-Id: Ib3eeed6e796a573b30f04a992f4213862f0e0eb6
Signed-off-by: Luke Hutton <[email protected]>
  • Loading branch information
lhutton1 committed Feb 25, 2025
1 parent 49f60b4 commit 759f7fb
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 4 deletions.
8 changes: 4 additions & 4 deletions mlir/lib/Dialect/Tosa/IR/TosaCanonicalizations.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down
11 changes: 11 additions & 0 deletions mlir/test/Dialect/Tosa/canonicalize.mlir
Original file line number Diff line number Diff line change
Expand Up @@ -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>
}

0 comments on commit 759f7fb

Please sign in to comment.