Skip to content

[mlir][tosa] Add error_if checks for Mul Op #135075

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 11, 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
35 changes: 34 additions & 1 deletion mlir/lib/Dialect/Tosa/Transforms/TosaValidation.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -979,8 +979,41 @@ bool checkErrorIfResize(Operation *op) {
return true;
}

bool checkErrorIfMul(Operation *op) {
auto mul = dyn_cast<tosa::MulOp>(op);
if (!mul)
return true;

// REQUIRE(0 <= shift && shift <= 63);
// REQUIRE(is_same<in_t,int32_t>() || shift == 0);
ElementsAttr shift_elem;
if (!matchPattern(mul.getShift(), m_Constant(&shift_elem))) {
return true;
}
int32_t shift = shift_elem.getValues<IntegerAttr>()[0].getInt();
auto inputElemType = getElementTypeOrSelf(mul.getInput1());
if (inputElemType.isInteger(32)) {
// 0 <= shift <= 63 for int32_t type
if (shift < 0 || shift > 63) {
op->emitOpError() << "requires 0 <= shift && shift <= 63, but got: "
<< shift;
return false;
}
} else {
// shift must be 0 for all other types
if (shift != 0) {
op->emitOpError() << "requires shift = 0 for all input data types that "
"are not int32_t, but got: "
<< shift;
return false;
}
}

return true;
}

LogicalResult TosaValidation::applyErrorIfCheck(Operation *op) {
if (!checkErrorIfResize(op))
if (!checkErrorIfResize(op) || !checkErrorIfMul(op))
return failure();
return success();
}
Expand Down
30 changes: 30 additions & 0 deletions mlir/test/Dialect/Tosa/error_if_check.mlir
Original file line number Diff line number Diff line change
Expand Up @@ -83,3 +83,33 @@ func.func @test_resize_invalid_boarder_x(%arg0: tensor<1x8x8x8xf32>) -> tensor<?
%1 = tosa.resize %arg0, %scale, %offset, %border { mode = "BILINEAR" } : (tensor<1x8x8x8xf32>, !tosa.shape<4>, !tosa.shape<2>, !tosa.shape<2>) -> tensor<?x?x?x?xf32>
return %1 : tensor<?x?x?x?xf32>
}

// -----

// CHECK-LABEL: test_mul_negative_shift
func.func @test_mul_negative_shift(%arg0: tensor<1x8x8x8xi32>, %arg1: tensor<1x8x8x8xi32>) -> tensor<1x8x8x8xi32> {
%shift = "tosa.const" () { values = dense<-1> : tensor<1xi8> } : () -> tensor<1xi8>
// expected-error@+1 {{'tosa.mul' op requires 0 <= shift && shift <= 63, but got: -1}}
%mul = tosa.mul %arg0, %arg1, %shift : (tensor<1x8x8x8xi32>, tensor<1x8x8x8xi32>, tensor<1xi8>) -> tensor<1x8x8x8xi32>
return %mul : tensor<1x8x8x8xi32>
}

// -----

// CHECK-LABEL: test_mul_too_big_shift
func.func @test_mul_too_big_shift(%arg0: tensor<1x8x8x8xi32>, %arg1: tensor<1x8x8x8xi32>) -> tensor<1x8x8x8xi32> {
%shift = "tosa.const" () { values = dense<64> : tensor<1xi8> } : () -> tensor<1xi8>
// expected-error@+1 {{'tosa.mul' op requires 0 <= shift && shift <= 63, but got: 64}}
%mul = tosa.mul %arg0, %arg1, %shift : (tensor<1x8x8x8xi32>, tensor<1x8x8x8xi32>, tensor<1xi8>) -> tensor<1x8x8x8xi32>
return %mul : tensor<1x8x8x8xi32>
}

// -----

// CHECK-LABEL: test_mul_non_zero_shift
func.func @test_mul_non_zero_shift(%arg0: tensor<1x8x8x8xi16>, %arg1: tensor<1x8x8x8xi16>) -> tensor<1x8x8x8xi32> {
%shift = "tosa.const" () { values = dense<1> : tensor<1xi8> } : () -> tensor<1xi8>
// expected-error@+1 {{'tosa.mul' op requires shift = 0 for all input data types that are not int32_t, but got: 1}}
%mul = tosa.mul %arg0, %arg1, %shift : (tensor<1x8x8x8xi16>, tensor<1x8x8x8xi16>, tensor<1xi8>) -> tensor<1x8x8x8xi32>
return %mul : tensor<1x8x8x8xi32>
}