Skip to content

Some tosa verifiers #42

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 2 commits into from
Jun 12, 2023
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
1 change: 1 addition & 0 deletions mlir/include/mlir/Dialect/Tosa/IR/TosaOps.td
Original file line number Diff line number Diff line change
Expand Up @@ -1585,6 +1585,7 @@ def Tosa_SliceOp: Tosa_Op<"slice", [

let hasCanonicalizer = 1;
let hasFolder = 1;
let hasVerifier = 1;
}

//===----------------------------------------------------------------------===//
Expand Down
52 changes: 52 additions & 0 deletions mlir/lib/Dialect/Tosa/IR/TosaOps.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -742,13 +742,65 @@ mlir::LogicalResult tosa::ReshapeOp::verify() {
ShapedType outputType = getType().cast<ShapedType>();

if (inputType.hasStaticShape() && outputType.hasStaticShape()) {
if (getNewShape() != outputType.getShape()) {
return emitOpError() << "newShape attribute " << getNewShape()
<< " does not match output type "
<< outputType.getShape();
}

int64_t inputElementsNum = inputType.getNumElements();
int64_t outputElementsNum = outputType.getNumElements();
if (inputElementsNum != outputElementsNum) {
return emitOpError() << "Cannot reshape " << inputElementsNum
<< " elements into " << outputElementsNum;
}
}

return mlir::success();
}

mlir::LogicalResult tosa::SliceOp::verify() {
// TODO: Complete verification
ShapedType inputType = getInput().getType().cast<ShapedType>();
ShapedType outputType = getType().cast<ShapedType>();

if (inputType.getRank() != outputType.getRank()) {
return emitOpError() << "rank of input (" << inputType.getRank()
<< ") and output ("
<< outputType.getRank()
<< ") must match";
}

if (getSize() != outputType.getShape()) {
return emitOpError() << "size attribute " << getSize()
<< " does not match output type "
<< outputType.getShape();
}

if ((int64_t)getStart().size() != inputType.getRank()) {
return emitOpError() << "rank of start (" << getStart().size()
<< ") and input ("
<< inputType.getRank()
<< ") must match";
}
if ((int64_t)getSize().size() != inputType.getRank()) {
return emitOpError() << "rank of size (" << getSize().size()
<< ") and input ("
<< inputType.getRank()
<< ") must match";
}

for (int i = 0; i < outputType.getRank(); ++i) {
auto dimSize = inputType.getShape()[i];
if (dimSize != ShapedType::kDynamic && getStart()[i] + getSize()[i] > inputType.getShape()[i]) {
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What if we have their declarations outside auto start = getStart(), size = getSize(), etc. and then just index them here (and in the error message)?
Then we avoid having to read the vector every time.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Are you thinking about performance? I think getStart() will actually be an inline funciton that returns an ArrayRef, so the access should be free.

return emitOpError() << "start (" << getStart()[i]
<< ") plus size ("
<< getSize()[i]
<< ") goes out of bounds of input size ("
<< inputType.getShape()[i]
<< ") in dimension " << i;
}
}
return mlir::success();
}

Expand Down