Skip to content

Commit adf9fed

Browse files
authored
[mlir][tosa] Add assembly format validation for COND_IF op (#142254)
COND_IF's simplified form - where redundant operand notations are omitted - is not conformant to the specification. According to the specification, all operands passed into an operation must be explicitly declared at each operation's structure. Add optional check to verify if the given form complies with the specification.
1 parent 8f04a24 commit adf9fed

File tree

2 files changed

+66
-1
lines changed

2 files changed

+66
-1
lines changed

mlir/lib/Dialect/Tosa/Transforms/TosaValidation.cpp

Lines changed: 52 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1167,10 +1167,61 @@ bool checkErrorIfPad(Operation *op) {
11671167
return true;
11681168
}
11691169

1170+
// Returns true if the operation takes no input operands, excluding attributes.
1171+
static bool isNullaryOperation(Operation *op) {
1172+
if (isa<tosa::ConstOp>(op) || isa<tosa::ConstShapeOp>(op) ||
1173+
isa<tosa::YieldOp>(op) || isa<tosa::VariableOp>(op))
1174+
return true;
1175+
return false;
1176+
}
1177+
1178+
bool checkErrorIfCondIf(Operation *op) {
1179+
auto ifOp = dyn_cast<tosa::IfOp>(op);
1180+
if (!ifOp)
1181+
return true;
1182+
1183+
// Whether the types and shapes of operands between the input/output list and
1184+
// internal regions are validated by the operation verifier. However, with
1185+
// support for the simplified form - where redundant operand notations are
1186+
// omitted - is not conformant to the specification. According to the
1187+
// specification, all operands passed into an operation must be explicitly
1188+
// declared at each operation's structure. This code section verify that the
1189+
// operation's form complies with this requirement.
1190+
1191+
// Returns true if the region uses no external input operands.
1192+
auto isNullaryRegion = [](Region &region) -> bool {
1193+
bool noLiveInValue = true;
1194+
region.walk([&noLiveInValue](Operation *op) {
1195+
if (!isNullaryOperation(op)) {
1196+
noLiveInValue = false;
1197+
return WalkResult::interrupt();
1198+
}
1199+
return WalkResult::advance();
1200+
});
1201+
return noLiveInValue;
1202+
};
1203+
1204+
mlir::Region &thenGraph = ifOp.getThenGraph();
1205+
mlir::Region &elseGraph = ifOp.getElseGraph();
1206+
bool isThenGraphNullaryRegion = isNullaryRegion(thenGraph);
1207+
bool isElseGraphNullaryRegion = isNullaryRegion(elseGraph);
1208+
bool isInputListEmpty = ifOp.getInputList().size() == 0;
1209+
1210+
if ((isInputListEmpty != isThenGraphNullaryRegion) ||
1211+
(isInputListEmpty != isElseGraphNullaryRegion)) {
1212+
op->emitOpError()
1213+
<< "the current simplified form is not strictly conformant to the "
1214+
"spec, please use the generic format\n";
1215+
return false;
1216+
}
1217+
1218+
return true;
1219+
}
1220+
11701221
LogicalResult TosaValidation::applyErrorIfCheck(Operation *op) {
11711222
if (!checkErrorIfResize(op) || !checkErrorIfMul(op) ||
11721223
!checkErrorIfTable(op) || !checkErrorIfRescale(op) ||
1173-
!checkErrorIfPad(op))
1224+
!checkErrorIfPad(op) || !checkErrorIfCondIf(op))
11741225
return failure();
11751226
return success();
11761227
}

mlir/test/Dialect/Tosa/error_if_check.mlir

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -225,3 +225,17 @@ func.func @test_error_i32_unsigned_output(%arg0: tensor<1xi8>) -> tensor<1xi32>
225225
%0 = tosa.rescale %arg0, %multiplier, %shift, %input_zp, %output_zp {scale32 = false, rounding_mode = "SINGLE_ROUND", per_channel = false, input_unsigned = false, output_unsigned = true} : (tensor<1xi8>, tensor<1xi16>, tensor<1xi8>, tensor<1xi8>, tensor<1xi32>) -> tensor<1xi32>
226226
return %0 : tensor<1xi32>
227227
}
228+
229+
// -----
230+
// CHECK-LABEL: cond_if_simplified_form
231+
func.func @test_cond_if_simplified_form(%arg0: tensor<f32>, %arg1: tensor<f32>, %arg2: tensor<i1>) -> tensor<f32> {
232+
// expected-error@+1 {{'tosa.cond_if' op the current simplified form is not strictly conformant to the spec, please use the generic format}}
233+
%0 = tosa.cond_if %arg2 -> (tensor<f32>) {
234+
%1 = tosa.add %arg0, %arg1 : (tensor<f32>, tensor<f32>) -> tensor<f32>
235+
tosa.yield %1 : tensor<f32>
236+
} else {
237+
%1 = tosa.sub %arg0, %arg1 : (tensor<f32>, tensor<f32>) -> tensor<f32>
238+
tosa.yield %1 : tensor<f32>
239+
}
240+
return %0 : tensor<f32>
241+
}

0 commit comments

Comments
 (0)