Skip to content

Commit 37263b6

Browse files
authored
[mlir][tosa] Add verifier for tosa.pad (#106351)
This patch adds verifier to `tosa.pad` which fixes a crash. `tosa.pad` expect: - same input and output tensor rank. - 'padding' tensor rank equal to 2. Fix #106168.
1 parent f4b9839 commit 37263b6

File tree

3 files changed

+40
-0
lines changed

3 files changed

+40
-0
lines changed

mlir/include/mlir/Dialect/Tosa/IR/TosaOps.td

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1594,6 +1594,7 @@ def Tosa_PadOp : Tosa_InferShapedTypeOp<"pad"> {
15941594

15951595
let hasCanonicalizer = 1;
15961596
let hasFolder = 1;
1597+
let hasVerifier = 1;
15971598
}
15981599

15991600
//===----------------------------------------------------------------------===//

mlir/lib/Dialect/Tosa/IR/TosaOps.cpp

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -817,6 +817,20 @@ LogicalResult tosa::PadOp::inferReturnTypeComponents(
817817
return success();
818818
}
819819

820+
LogicalResult tosa::PadOp::verify() {
821+
RankedTensorType inputType = getInput1().getType();
822+
RankedTensorType outputType = getOutput().getType();
823+
TensorType paddingType = getPadding().getType();
824+
825+
if (inputType.getRank() != outputType.getRank())
826+
return emitOpError() << "expect same input and output tensor rank.";
827+
828+
if (paddingType.hasRank() && paddingType.getRank() != 2)
829+
return emitOpError() << "expect 'padding' tensor rank equal to 2.";
830+
831+
return success();
832+
}
833+
820834
static SmallVector<int64_t> convertToMlirShape(ArrayRef<int64_t> shape) {
821835
return to_vector(llvm::map_range(shape, [](int64_t dim) {
822836
return dim == -1 ? ShapedType::kDynamic : dim;

mlir/test/Dialect/Tosa/invalid.mlir

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,31 @@ func.func @test_pad_non_const(%arg0: tensor<13x21x3xi8>, %arg1: tensor<i8>) -> t
7272

7373
// -----
7474

75+
func.func @test_pad_io_rank_mismatch(%arg0: tensor<13x21xf32>, %arg1: tensor<2x2xi32>) {
76+
// expected-error@+1 {{'tosa.pad' op expect same input and output tensor rank.}}
77+
%1 = tosa.pad %arg0, %arg1 : (tensor<13x21xf32>, tensor<2x2xi32>) -> tensor<13x21x3xf32>
78+
return
79+
}
80+
81+
// -----
82+
83+
func.func @test_pad_invalid_padding_rank(%arg0: tensor<13x21xf32>, %arg1: tensor<2xi32>) {
84+
// expected-error@+1 {{'tosa.pad' op expect 'padding' tensor rank equal to 2.}}
85+
%1 = tosa.pad %arg0, %arg1 : (tensor<13x21xf32>, tensor<2xi32>) -> tensor<13x21xf32>
86+
return
87+
}
88+
89+
// -----
90+
91+
func.func @test_pad_invalid_padConst_rank(%arg0: tensor<13x21xf32>, %arg1: tensor<2x2xi32>) {
92+
%0 = "tosa.const"() {value = dense<3.14> : tensor<1xf32>} : () -> tensor<1xf32>
93+
// expected-error@+1 {{'tosa.pad' op operand #2 must be 0D tensor of number values, but got 'tensor<1xf32>'}}
94+
%1 = tosa.pad %arg0, %arg1, %0 : (tensor<13x21xf32>, tensor<2x2xi32>, tensor<1xf32>) -> tensor<13x21xf32>
95+
return
96+
}
97+
98+
// -----
99+
75100
func.func @test_transpose_non_const(%arg0: tensor<13x21x3xf32>, %arg1: tensor<3xi32>) -> tensor<3x13x21xf32> {
76101
// expected-error@+1 {{'tosa.transpose' op perms of transpose is not constant}}
77102
%0 = tosa.transpose %arg0, %arg1 : (tensor<13x21x3xf32>, tensor<3xi32>) -> tensor<3x13x21xf32>

0 commit comments

Comments
 (0)