Skip to content

Commit

Permalink
[mlir][tosa] fix a crash when sliceOp has invalid attribute (#68486) (#…
Browse files Browse the repository at this point in the history
…70063)

add a verifier for tosa::sliceOp
  • Loading branch information
lipracer authored Oct 25, 2023
1 parent ff67b68 commit ab7e8b7
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 0 deletions.
1 change: 1 addition & 0 deletions mlir/include/mlir/Dialect/Tosa/IR/TosaOps.td
Original file line number Diff line number Diff line change
Expand Up @@ -1621,6 +1621,7 @@ def Tosa_SliceOp : Tosa_InferShapedTypeOp<"slice"> {

let hasCanonicalizer = 1;
let hasFolder = 1;
let hasVerifier = 1;
}

//===----------------------------------------------------------------------===//
Expand Down
16 changes: 16 additions & 0 deletions mlir/lib/Dialect/Tosa/IR/TosaOps.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -820,6 +820,22 @@ LogicalResult tosa::SliceOp::inferReturnTypeComponents(
return success();
}

LogicalResult tosa::SliceOp::verify() {
auto inputType = llvm::dyn_cast<RankedTensorType>(getInput().getType());
if (!inputType)
return success();

if (static_cast<size_t>(inputType.getRank()) != getStart().size())
return emitOpError(
"length of start attribute is not equal rank of input shape");

if (static_cast<size_t>(inputType.getRank()) != getSize().size())
return emitOpError(
"length of size attribute is not equal rank of input shape");

return success();
}

LogicalResult tosa::TableOp::inferReturnTypeComponents(
MLIRContext *context, ::std::optional<Location> location,
TableOp::Adaptor adaptor,
Expand Down
18 changes: 18 additions & 0 deletions mlir/test/Dialect/Tosa/invalid.mlir
Original file line number Diff line number Diff line change
Expand Up @@ -303,3 +303,21 @@ func.func @test_variable_write_shape(%arg0: tensor<1x4x8xi32>) -> () {
tosa.variable.write @stored_var, %arg0 : tensor<1x4x8xi32>
return
}

// -----

func.func @test_slice_invalid_start() {
%0 = tensor.empty() : tensor<4x31x31xf32>
// expected-error@+1 {{'tosa.slice' op length of start attribute is not equal rank of input shape}}
%1 = tosa.slice %0 {size = array<i64: 1, 1, 1>, start = array<i64: 1, 1>} : (tensor<4x31x31xf32>) -> tensor<*xf32>
return
}

// -----

func.func @test_slice_invalid_size() {
%0 = tensor.empty() : tensor<4x31x31xf32>
// expected-error@+1 {{'tosa.slice' op length of size attribute is not equal rank of input shape}}
%1 = tosa.slice %0 {size = array<i64: 1>, start = array<i64: 1, 1, 1>} : (tensor<4x31x31xf32>) -> tensor<*xf32>
return
}

0 comments on commit ab7e8b7

Please sign in to comment.