Skip to content

Commit

Permalink
[mlir][tosa] Fix segmentation fault in case of folding unranked tensor
Browse files Browse the repository at this point in the history
Trying to fold the unranked tensor for "tosa.equal" crashes due to null reference.
We need to check the dynamic cast result beforehand. This is reported in
#60192.

Reviewed By: rsuderman

Differential Revision: https://reviews.llvm.org/D143034
  • Loading branch information
Lewuathe authored and rsuderman committed Feb 13, 2023
1 parent f36fe00 commit 9d0b596
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 2 deletions.
4 changes: 2 additions & 2 deletions mlir/lib/Dialect/Tosa/IR/TosaCanonicalizations.cpp
Expand Up @@ -803,8 +803,8 @@ OpFoldResult EqualOp::fold(FoldAdaptor adaptor) {

// If we are comparing an integer value to itself it is always true. We can
// not do this with float due to float values.
if (lhsTy.getElementType().isa<IntegerType>() && resultTy.hasStaticShape() &&
lhs == rhs) {
if (lhsTy.getElementType().isa<IntegerType>() && resultTy &&
resultTy.hasStaticShape() && lhs == rhs) {
return DenseElementsAttr::get(resultTy, true);
}

Expand Down
8 changes: 8 additions & 0 deletions mlir/test/Dialect/Tosa/constant_folding.mlir
Expand Up @@ -6,3 +6,11 @@ func.func @test_const(%arg0 : index) -> tensor<4xi32> {
%0 = "tosa.const"() {value = dense<[3, 0, 1, 2]> : tensor<4xi32>} : () -> tensor<4xi32>
return %0 : tensor<4xi32>
}

// CHECK-LABEL: func @try_fold_equal_with_unranked_tensor
func.func @try_fold_equal_with_unranked_tensor(%arg0: tensor<4xi32>, %arg1: tensor<i32>) {
// CHECK: "tosa.equal"
// CHECK-NEXT: return
%0 = "tosa.equal"(%arg0, %arg1) : (tensor<4xi32>, tensor<i32>) -> tensor<*xi1>
return
}

0 comments on commit 9d0b596

Please sign in to comment.