Skip to content

Commit f1bbd5e

Browse files
lhutton1miguelcsx
authored andcommitted
[mlir][tosa] Check negative output size in resize shape inference (llvm#143382)
This commit adds a check to ensure that the calculated output height and width, during shape inference, should be non-negative. An error is output if this is the case. Fixes: llvm#142402
1 parent bfb47b9 commit f1bbd5e

File tree

2 files changed

+24
-2
lines changed

2 files changed

+24
-2
lines changed

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

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2533,16 +2533,26 @@ LogicalResult tosa::ResizeOp::inferReturnTypeComponents(
25332533
}
25342534

25352535
// Compute the output shape based on attributes: scale, offset, and border.
2536-
outputShape[1] =
2536+
const int64_t outputHeight =
25372537
(((inputHeight - 1) * scaleInt[0] - offsetInt[0] + borderInt[0]) /
25382538
scaleInt[1]) +
25392539
1;
25402540

2541-
outputShape[2] =
2541+
const int64_t outputWidth =
25422542
(((inputWidth - 1) * scaleInt[2] - offsetInt[1] + borderInt[1]) /
25432543
scaleInt[3]) +
25442544
1;
25452545

2546+
if (outputHeight < 0 || outputWidth < 0) {
2547+
return emitOptionalError(
2548+
location,
2549+
"calculated output height and width must be non-negative, "
2550+
"got height = ",
2551+
outputHeight, ", width = ", outputWidth);
2552+
}
2553+
2554+
outputShape[1] = outputHeight;
2555+
outputShape[2] = outputWidth;
25462556
inferredReturnShapes.push_back(ShapedTypeComponents(outputShape));
25472557
return success();
25482558
}

mlir/test/Dialect/Tosa/tosa-infer-shapes.mlir

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1115,6 +1115,18 @@ func.func @resize_fp_power_of_two_upscale_offsetted(%arg0: tensor<1x50x48x1xf32>
11151115

11161116
// -----
11171117

1118+
// CHECK-LABEL: @resize_negative_output_dim
1119+
func.func @resize_negative_output_dim(%arg0: tensor<1x3x1x1xi8>) {
1120+
%scale = tosa.const_shape { values = dense<[1, 3, 1, 1]> : tensor<4xindex> } : () -> !tosa.shape<4>
1121+
%offset = tosa.const_shape { values = dense<[6, 1]> : tensor<2xindex> } : () -> !tosa.shape<2>
1122+
%border = tosa.const_shape { values = dense<[-15, 0]> : tensor<2xindex> } : () -> !tosa.shape<2>
1123+
// expected-error@+1 {{calculated output height and width must be non-negative, got height = -5, width = 0}}
1124+
%0 = tosa.resize %arg0, %scale, %offset, %border {mode = "NEAREST_NEIGHBOR"} : (tensor<1x3x1x1xi8>, !tosa.shape<4>, !tosa.shape<2>, !tosa.shape<2>) -> tensor<*xi8>
1125+
return
1126+
}
1127+
1128+
// -----
1129+
11181130
// CHECK-LABEL: @if_test_simple
11191131
func.func @if_test_simple(%arg0 : tensor<f32>, %arg1 : tensor<f32>, %arg2 : tensor<i1>) -> () {
11201132
%a = tosa.log %arg0 : (tensor<f32>) -> tensor<f32>

0 commit comments

Comments
 (0)