Skip to content

Commit 0749db1

Browse files
authored
Some tosa verifiers (#42)
* TOSA: Extend verifier to reshape of to check newShape attr * TOSA: add initial verifier for SliceOp
1 parent 9b67e54 commit 0749db1

File tree

2 files changed

+53
-0
lines changed

2 files changed

+53
-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
@@ -1587,6 +1587,7 @@ def Tosa_SliceOp: Tosa_Op<"slice", [
15871587

15881588
let hasCanonicalizer = 1;
15891589
let hasFolder = 1;
1590+
let hasVerifier = 1;
15901591
}
15911592

15921593
//===----------------------------------------------------------------------===//

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

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -742,13 +742,65 @@ mlir::LogicalResult tosa::ReshapeOp::verify() {
742742
ShapedType outputType = getType().cast<ShapedType>();
743743

744744
if (inputType.hasStaticShape() && outputType.hasStaticShape()) {
745+
if (getNewShape() != outputType.getShape()) {
746+
return emitOpError() << "newShape attribute " << getNewShape()
747+
<< " does not match output type "
748+
<< outputType.getShape();
749+
}
750+
745751
int64_t inputElementsNum = inputType.getNumElements();
746752
int64_t outputElementsNum = outputType.getNumElements();
747753
if (inputElementsNum != outputElementsNum) {
748754
return emitOpError() << "Cannot reshape " << inputElementsNum
749755
<< " elements into " << outputElementsNum;
750756
}
751757
}
758+
759+
return mlir::success();
760+
}
761+
762+
mlir::LogicalResult tosa::SliceOp::verify() {
763+
// TODO: Complete verification
764+
ShapedType inputType = getInput().getType().cast<ShapedType>();
765+
ShapedType outputType = getType().cast<ShapedType>();
766+
767+
if (inputType.getRank() != outputType.getRank()) {
768+
return emitOpError() << "rank of input (" << inputType.getRank()
769+
<< ") and output ("
770+
<< outputType.getRank()
771+
<< ") must match";
772+
}
773+
774+
if (getSize() != outputType.getShape()) {
775+
return emitOpError() << "size attribute " << getSize()
776+
<< " does not match output type "
777+
<< outputType.getShape();
778+
}
779+
780+
if ((int64_t)getStart().size() != inputType.getRank()) {
781+
return emitOpError() << "rank of start (" << getStart().size()
782+
<< ") and input ("
783+
<< inputType.getRank()
784+
<< ") must match";
785+
}
786+
if ((int64_t)getSize().size() != inputType.getRank()) {
787+
return emitOpError() << "rank of size (" << getSize().size()
788+
<< ") and input ("
789+
<< inputType.getRank()
790+
<< ") must match";
791+
}
792+
793+
for (int i = 0; i < outputType.getRank(); ++i) {
794+
auto dimSize = inputType.getShape()[i];
795+
if (dimSize != ShapedType::kDynamic && getStart()[i] + getSize()[i] > inputType.getShape()[i]) {
796+
return emitOpError() << "start (" << getStart()[i]
797+
<< ") plus size ("
798+
<< getSize()[i]
799+
<< ") goes out of bounds of input size ("
800+
<< inputType.getShape()[i]
801+
<< ") in dimension " << i;
802+
}
803+
}
752804
return mlir::success();
753805
}
754806

0 commit comments

Comments
 (0)