Skip to content

Commit dfa1335

Browse files
authored
[mlir][emitc] Add verification for the emitc.get_field op (#152577)
This MR adds a `verifier` for the `emitc.get_field` op. - The `verifier` checks that the `emitc.get_field` operation is nested inside an `emitc.class` op. - Additionally, appropriate tests for erroneous cases were added for class-related operations in `invalid_ops.mlir`.
1 parent 0e9b6d6 commit dfa1335

File tree

3 files changed

+43
-0
lines changed

3 files changed

+43
-0
lines changed

mlir/include/mlir/Dialect/EmitC/IR/EmitC.td

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1697,6 +1697,7 @@ def EmitC_GetFieldOp
16971697
let arguments = (ins FlatSymbolRefAttr:$field_name);
16981698
let results = (outs EmitCType:$result);
16991699
let assemblyFormat = "$field_name `:` type($result) attr-dict";
1700+
let hasVerifier = 1;
17001701
}
17011702

17021703
#endif // MLIR_DIALECT_EMITC_IR_EMITC

mlir/lib/Dialect/EmitC/IR/EmitC.cpp

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1395,6 +1395,7 @@ void FileOp::build(OpBuilder &builder, OperationState &state, StringRef id) {
13951395
//===----------------------------------------------------------------------===//
13961396
// FieldOp
13971397
//===----------------------------------------------------------------------===//
1398+
13981399
static void printEmitCFieldOpTypeAndInitialValue(OpAsmPrinter &p, FieldOp op,
13991400
TypeAttr type,
14001401
Attribute initialValue) {
@@ -1452,6 +1453,15 @@ LogicalResult FieldOp::verify() {
14521453
//===----------------------------------------------------------------------===//
14531454
// GetFieldOp
14541455
//===----------------------------------------------------------------------===//
1456+
1457+
LogicalResult GetFieldOp::verify() {
1458+
auto parentClassOp = getOperation()->getParentOfType<emitc::ClassOp>();
1459+
if (!parentClassOp.getOperation())
1460+
return emitOpError(" must be nested within an emitc.class operation");
1461+
1462+
return success();
1463+
}
1464+
14551465
LogicalResult GetFieldOp::verifySymbolUses(SymbolTableCollection &symbolTable) {
14561466
mlir::FlatSymbolRefAttr fieldNameAttr = getFieldNameAttr();
14571467
FieldOp fieldOp =

mlir/test/Dialect/EmitC/invalid_ops.mlir

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -676,3 +676,35 @@ func.func @test_verbatim(%arg0 : !emitc.ptr<i32>, %arg1 : i32) {
676676
emitc.verbatim "{a} " args %arg0, %arg1 : !emitc.ptr<i32>, i32
677677
return
678678
}
679+
680+
// -----
681+
682+
// expected-error @+1 {{'emitc.field' op field must be nested within an emitc.class operation}}
683+
emitc.field @testField : !emitc.array<1xf32>
684+
685+
// -----
686+
687+
// expected-error @+1 {{'emitc.get_field' op must be nested within an emitc.class operation}}
688+
%1 = emitc.get_field @testField : !emitc.array<1xf32>
689+
690+
// -----
691+
692+
emitc.func @testMethod() {
693+
%0 = "emitc.constant"() <{value = 0 : index}> : () -> !emitc.size_t
694+
// expected-error @+1 {{'emitc.get_field' op must be nested within an emitc.class operation}}
695+
%1 = get_field @testField : !emitc.array<1xf32>
696+
%2 = subscript %1[%0] : (!emitc.array<1xf32>, !emitc.size_t) -> !emitc.lvalue<f32>
697+
return
698+
}
699+
700+
// -----
701+
702+
emitc.class @testClass {
703+
emitc.func @testMethod() {
704+
%0 = "emitc.constant"() <{value = 0 : index}> : () -> !emitc.size_t
705+
// expected-error @+1 {{'emitc.get_field' op field '@testField' not found in the class}}
706+
%1 = get_field @testField : !emitc.array<1xf32>
707+
%2 = subscript %1[%0] : (!emitc.array<1xf32>, !emitc.size_t) -> !emitc.lvalue<f32>
708+
return
709+
}
710+
}

0 commit comments

Comments
 (0)