Skip to content

[mlir][tosa] Add error if and level checks for COND_IF & WHILE_LOOP #136194

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 29, 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
2 changes: 2 additions & 0 deletions mlir/include/mlir/Dialect/Tosa/IR/TosaOps.td
Original file line number Diff line number Diff line change
Expand Up @@ -2559,6 +2559,7 @@ def Tosa_IfOp : Tosa_Op<"cond_if",
);

let hasCustomAssemblyFormat = 1;
let hasVerifier = 1;
}

//===----------------------------------------------------------------------===//
Expand Down Expand Up @@ -2597,6 +2598,7 @@ def Tosa_WhileOp : Tosa_Op<"while_loop", [
);

let hasCustomAssemblyFormat = 1;
let hasVerifier = 1;
}

include "mlir/Dialect/Tosa/IR/TosaUtilOps.td"
Expand Down
129 changes: 129 additions & 0 deletions mlir/lib/Dialect/Tosa/IR/TosaOps.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -562,6 +562,57 @@ static LogicalResult verifyConvOpErrorIf(T op) {
return success();
}

// Verify whether same type and shape of the given two types.
static LogicalResult errorIfTypeOrShapeMismatch(Operation *op, Type type1,
StringRef name1, Type type2,
StringRef name2) {
auto shapeType1 = dyn_cast<ShapedType>(type1);
auto shapeType2 = dyn_cast<ShapedType>(type2);
if (!shapeType1 || !shapeType2)
return failure();

auto elemType1 = shapeType1.getElementType();
auto elemType2 = shapeType2.getElementType();
if (elemType1 != elemType2)
return op->emitOpError()
<< "require same element type for " << name1 << " (" << elemType1
<< ") and " << name2 << " (" << elemType2 << ")";

if (failed(verifyCompatibleShape(type1, type2)))
return op->emitOpError()
<< "require same shapes for " << name1 << " (" << type1 << ") and "
<< name2 << " (" << type2 << ")";

return success();
}

// Verify whether same length, type, and shape of the given two tensor lists.
static LogicalResult errorIfTypeOrShapeMismatch(Operation *op, ValueRange list1,
StringRef name1,
ValueRange list2,
StringRef name2) {
if (list1.size() != list2.size())
return op->emitOpError()
<< "require same number of values in " << name1 << " ("
<< list1.size() << ") and " << name2 << " (" << list2.size() << ")";

for (auto [type1, type2] :
llvm::zip_equal(list1.getTypes(), list2.getTypes())) {
if (errorIfTypeOrShapeMismatch(op, type1, name1, type2, name2).failed())
return failure();
}

return success();
}

static inline LogicalResult errorIfShapeNotSizeOne(Operation *op, Type type) {
ShapeAdaptor shapeAdaptor(type);
if (!shapeAdaptor.hasRank() || !shapeAdaptor.hasStaticShape())
return success();

return shapeAdaptor.getNumElements() == 1 ? success() : failure();
}

// verify that inType and outType have same element types
template <typename T>
static LogicalResult verifySameElementTypes(T op, Type inType, Type outType) {
Expand Down Expand Up @@ -3473,6 +3524,84 @@ void IfOp::print(OpAsmPrinter &p) {
p.printOptionalAttrDict((*this)->getAttrs());
}

LogicalResult IfOp::verify() {
if (errorIfTypeOrShapeMismatch(*this, getThenGraph().front().getArguments(),
"'then_graph' arguments", getInputList(),
"'input_list'")
.failed())
return failure();

if (errorIfTypeOrShapeMismatch(*this, getElseGraph().front().getArguments(),
"'else_graph' arguments", getInputList(),
"'input_list'")
.failed())
return failure();

auto thenYield = cast<tosa::YieldOp>(getThenGraph().front().getTerminator());
if (errorIfTypeOrShapeMismatch(*this, thenYield.getInputs(),
"'then_graph' results", getOutputList(),
"'output_list'")
.failed())
return failure();

auto elseYield = cast<tosa::YieldOp>(getElseGraph().front().getTerminator());
if (errorIfTypeOrShapeMismatch(*this, elseYield.getInputs(),
"'else_graph' results", getOutputList(),
"'output_list'")
.failed())
return failure();

auto condType = getCondition().getType();
if (errorIfShapeNotSizeOne(*this, condType).failed())
return emitOpError() << "'condition' must be a size 1 tensor, got "
<< condType;

return success();
}

LogicalResult WhileOp::verify() {
if (errorIfTypeOrShapeMismatch(*this, getInputList(), "'input_list'",
getOutputList(), "'output_list'")
.failed())
return failure();

if (errorIfTypeOrShapeMismatch(*this, getCondGraph().front().getArguments(),
"'cond_graph' arguments", getInputList(),
"'input_list'")
.failed())
return failure();

if (errorIfTypeOrShapeMismatch(*this, getBodyGraph().front().getArguments(),
"'body_graph' arguments", getInputList(),
"'input_list'")
.failed())
return failure();

auto bodyYield = cast<tosa::YieldOp>(getBodyGraph().front().getTerminator());
if (errorIfTypeOrShapeMismatch(*this, bodyYield.getInputs(),
"'body_graph' results", getInputList(),
"'input_list'")
.failed())
return failure();

// Condition block output must be a single element tensor with a single bool
// value.
auto condYield = cast<tosa::YieldOp>(getCondGraph().front().getTerminator());
if (condYield.getInputs().size() != 1)
return emitOpError() << "require 'cond_graph' only have one result";

auto condOutType = condYield.getInputs()[0].getType();
if (errorIfShapeNotSizeOne(*this, condOutType).failed())
return emitOpError() << "'cond_graph' result must be a size 1 tensor, got "
<< condOutType;

if (!getElementTypeOrSelf(condOutType).isInteger(1))
return emitOpError() << "'cond_graph' result must be a boolean tensor, got "
<< condOutType;

return success();
}

LogicalResult ReverseOp::verify() {
if (verifySameElementTypes(*this, /* inType = */ getInput1().getType(),
/* outType = */ getOutput().getType())
Expand Down
35 changes: 35 additions & 0 deletions mlir/lib/Dialect/Tosa/Transforms/TosaValidation.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -449,6 +449,35 @@ struct TosaValidation : public tosa::impl::TosaValidationBase<TosaValidation> {
return true;
}

// Recursively perform a bottom-up search to determine the maximum nesting
// depth, starting from a specific operation and continuing up to the function
// or module scope. Tosa nesting_depth starts at 0 and increments by one each
// time a new nested `region` is encountered.
static void getMaxNestedDepth(Operation *op, int32_t &depth) {
if (isa<mlir::func::FuncOp>(op) || isa<ModuleOp>(op))
return;

op = op->getParentOp();
if (!op)
return;

depth++;
getMaxNestedDepth(op, depth);
return;
}

bool levelCheckMaxNesting(Operation *op) {
int32_t maxNestedDepth = 0;
getMaxNestedDepth(op, maxNestedDepth);

if (maxNestedDepth >= tosaLevel.MAX_NESTING) {
op->emitOpError() << "failed level check: " << maxNestedDepth
<< " >= MAX_NESTING";
return false;
}
return true;
}

bool levelCheckListSize(Operation *op) {
if (auto concat = dyn_cast<tosa::ConcatOp>(op)) {
return levelCheckListSize(op, concat.getInput1().size(), "input1");
Expand Down Expand Up @@ -750,6 +779,12 @@ LogicalResult TosaValidation::applyLevelCheck(Operation *op) {
return failure();
}

if (isa<tosa::IfOp>(op) || isa<tosa::WhileOp>(op)) {
if (!levelCheckMaxNesting(op)) {
return failure();
}
}

return success();
}

Expand Down
Loading