Skip to content

[mlir][tosa] Add verifier check for Slice Op #135853

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
Apr 16, 2025
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
29 changes: 18 additions & 11 deletions mlir/lib/Dialect/Tosa/IR/TosaOps.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1447,19 +1447,26 @@ LogicalResult tosa::SliceOp::verify() {
/* outType = */ getOutput().getType())
.failed())
return failure();
auto inputType = llvm::dyn_cast<RankedTensorType>(getInput1().getType());
if (!inputType)
return success();

auto startShapeRank =
llvm::cast<tosa::shapeType>(getStart().getType()).getRank();
if (inputType.getRank() != startShapeRank)
return emitOpError("length of start is not equal to rank of input shape");
const ShapeAdaptor inputShape(getInput1().getType());
if (inputShape.hasRank()) {
const auto inputRank = inputShape.getRank();
const ShapeAdaptor outputShape(getOutput().getType());
if (outputShape.hasRank() && inputRank != outputShape.getRank())
return emitOpError(
"expect input1 and output to have the same ranks, got ")
<< inputRank << " and " << outputShape.getRank();

const auto startShapeRank =
llvm::cast<tosa::shapeType>(getStart().getType()).getRank();
if (inputRank != startShapeRank)
return emitOpError("length of start is not equal to rank of input shape");

auto sizeShapeRank =
llvm::cast<tosa::shapeType>(getSize().getType()).getRank();
if (inputType.getRank() != sizeShapeRank)
return emitOpError("length of size is not equal to rank of input shape");
const auto sizeShapeRank =
llvm::cast<tosa::shapeType>(getSize().getType()).getRank();
if (inputRank != sizeShapeRank)
return emitOpError("length of size is not equal to rank of input shape");
}

return success();
}
Expand Down
32 changes: 0 additions & 32 deletions mlir/test/Dialect/Tosa/invalid.mlir
Original file line number Diff line number Diff line change
Expand Up @@ -660,28 +660,6 @@ func.func @test_variable_write_shape(%arg0: tensor<1x4x8xi8>) -> () {

// -----

func.func @test_slice_invalid_start() {
%0 = tensor.empty() : tensor<4x31x31xf32>
%start = tosa.const_shape {values = dense<[1, 1]> : tensor<2xindex>} : () -> !tosa.shape<2>
%size = tosa.const_shape {values = dense<[1, 1, 1]> : tensor<3xindex>} : () -> !tosa.shape<3>
// expected-error@+1 {{'tosa.slice' op length of start is not equal to rank of input shape}}
%3 = tosa.slice %0, %start, %size : (tensor<4x31x31xf32>, !tosa.shape<2>, !tosa.shape<3>) -> tensor<*xf32>
return
}

// -----

func.func @test_slice_invalid_size() {
%0 = tensor.empty() : tensor<4x31x31xf32>
%start = tosa.const_shape {values = dense<[1, 1, 1]> : tensor<3xindex>} : () -> !tosa.shape<3>
%size = tosa.const_shape {values = dense<[1]> : tensor<1xindex>} : () -> !tosa.shape<1>
// expected-error@+1 {{'tosa.slice' op length of size is not equal to rank of input shape}}
%3 = tosa.slice %0, %start, %size : (tensor<4x31x31xf32>, !tosa.shape<3>, !tosa.shape<1>) -> tensor<*xf32>
return
}

// -----

func.func @test_tile_invalid_multiples() {
%0 = tensor.empty() : tensor<4x31x31xf32>
%cst = tosa.const_shape { values = dense<1> : tensor<1xindex> } : () -> !tosa.shape<1>
Expand Down Expand Up @@ -1938,16 +1916,6 @@ func.func @test_scalar_reverse(%arg0: tensor<f32>) -> tensor<f32> {

// -----

func.func @test_scalar_slice(%arg0: tensor<f32>) -> tensor<f32> {
%0 = tosa.const_shape {values = dense<[]> : tensor<0xindex>} : () -> !tosa.shape<0>
%1 = tosa.const_shape {values = dense<[]> : tensor<0xindex>} : () -> !tosa.shape<0>
// expected-error@+1 {{'tosa.slice' op operand #0 must be tosa-conformant tensor of at least rank 1, but got 'tensor<f32>'}}
%2 = tosa.slice %arg0, %0, %1 : (tensor<f32>, !tosa.shape<0>, !tosa.shape<0>) -> tensor<f32>
return %2 : tensor<f32>
}

// -----

func.func @test_scalar_tile(%arg0: tensor<f32>) -> tensor<*xf32> {
%cst = tosa.const_shape { values = dense<[]> : tensor<0xindex> } : () -> !tosa.shape<0>
// expected-error@+1 {{'tosa.tile' op operand #0 must be tosa-conformant tensor of at least rank 1, but got 'tensor<f32>'}}
Expand Down
43 changes: 43 additions & 0 deletions mlir/test/Dialect/Tosa/verifier.mlir
Original file line number Diff line number Diff line change
Expand Up @@ -124,3 +124,46 @@ func.func @test_scalar_output_transpose(%arg0: tensor<*xf32>) -> tensor<f32> {
%1 = tosa.transpose %arg0 {perms = array<i32: 2, 0, 1>} : (tensor<*xf32>) -> tensor<f32>
return %1 : tensor<f32>
}

// -----

func.func @test_slice_invalid_output_rank() {
%0 = tensor.empty() : tensor<4x31x31xf32>
%start = tosa.const_shape {values = dense<[1, 1]> : tensor<2xindex>} : () -> !tosa.shape<2>
%size = tosa.const_shape {values = dense<[1, 1, 1]> : tensor<3xindex>} : () -> !tosa.shape<3>
// expected-error@+1 {{'tosa.slice' op expect input1 and output to have the same ranks, got 3 and 4}}
%3 = tosa.slice %0, %start, %size : (tensor<4x31x31xf32>, !tosa.shape<2>, !tosa.shape<3>) -> tensor<?x?x?x?xf32>
return
}

// -----

func.func @test_slice_invalid_start() {
%0 = tensor.empty() : tensor<4x31x31xf32>
%start = tosa.const_shape {values = dense<[1, 1]> : tensor<2xindex>} : () -> !tosa.shape<2>
%size = tosa.const_shape {values = dense<[1, 1, 1]> : tensor<3xindex>} : () -> !tosa.shape<3>
// expected-error@+1 {{'tosa.slice' op length of start is not equal to rank of input shape}}
%3 = tosa.slice %0, %start, %size : (tensor<4x31x31xf32>, !tosa.shape<2>, !tosa.shape<3>) -> tensor<*xf32>
return
}

// -----

func.func @test_slice_invalid_size() {
%0 = tensor.empty() : tensor<4x31x31xf32>
%start = tosa.const_shape {values = dense<[1, 1, 1]> : tensor<3xindex>} : () -> !tosa.shape<3>
%size = tosa.const_shape {values = dense<[1]> : tensor<1xindex>} : () -> !tosa.shape<1>
// expected-error@+1 {{'tosa.slice' op length of size is not equal to rank of input shape}}
%3 = tosa.slice %0, %start, %size : (tensor<4x31x31xf32>, !tosa.shape<3>, !tosa.shape<1>) -> tensor<*xf32>
return
}

// -----

func.func @test_scalar_slice(%arg0: tensor<f32>) -> tensor<f32> {
%0 = tosa.const_shape {values = dense<[]> : tensor<0xindex>} : () -> !tosa.shape<0>
%1 = tosa.const_shape {values = dense<[]> : tensor<0xindex>} : () -> !tosa.shape<0>
// expected-error@+1 {{'tosa.slice' op operand #0 must be tosa-conformant tensor of at least rank 1, but got 'tensor<f32>'}}
%2 = tosa.slice %arg0, %0, %1 : (tensor<f32>, !tosa.shape<0>, !tosa.shape<0>) -> tensor<f32>
return %2 : tensor<f32>
}
Loading