Skip to content

[mlir][tosa] Add missing check for new_shape of tosa.reshape #104394

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

Merged
merged 1 commit into from
Aug 16, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion mlir/lib/Dialect/Tosa/IR/TosaOps.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -990,11 +990,16 @@ llvm::LogicalResult tosa::ReshapeOp::verify() {
return emitOpError() << "new shape does not match result rank";

for (auto [newShapeDim, outputShapeDim] :
zip(getNewShape(), outputType.getShape()))
zip(getNewShape(), outputType.getShape())) {
if (newShapeDim != -1 && outputShapeDim != ShapedType::kDynamic &&
newShapeDim != outputShapeDim)
return emitOpError() << "new shape is inconsistent with result shape";

if (newShapeDim != ShapedType::kDynamic && newShapeDim < -1)
return emitOpError() << "new shape has invalid tensor dimension size "
<< newShapeDim;
}

if (inputType.hasStaticShape() && outputType.hasStaticShape()) {
int64_t inputElementsNum = inputType.getNumElements();
int64_t outputElementsNum = outputType.getNumElements();
Expand Down
8 changes: 8 additions & 0 deletions mlir/test/Dialect/Tosa/invalid.mlir
Original file line number Diff line number Diff line change
Expand Up @@ -291,6 +291,14 @@ func.func @test_reshape_invalid_placeholders(%arg0 : tensor<?xf32>) -> () {

// -----

func.func @test_reshape_invalid_tensor_dim(%arg0 : tensor<4x?xf32>) -> () {
// expected-error@+1 {{'tosa.reshape' op new shape has invalid tensor dimension size -2}}
%0 = "tosa.reshape" (%arg0) {new_shape = array<i64: -2, -1>} : (tensor<4x?xf32>) -> tensor<?x4xf32>
return
}

// -----

func.func @test_reverse_axis_out_of_range(%arg0 : tensor<13x21x3xf32>) -> () {
// expected-error@+1 {{'tosa.reverse' op expect input tensor rank (3) to be larger than reverse axis (5)}}
%0 = tosa.reverse %arg0 {axis = 5 : i32} : (tensor<13x21x3xf32>) -> tensor<?x?x?xi32>
Expand Down
Loading