Skip to content

Commit caaeaaa

Browse files
Jaddyenrlavaee
authored andcommitted
Addfinal specifier to the classop (llvm#145977)
In some use cases of the `ClassOp`, eg MLGO, we would like to be able to declare the class as final. This specifier allows for that.
1 parent 71cc3eb commit caaeaaa

File tree

3 files changed

+43
-5
lines changed

3 files changed

+43
-5
lines changed

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

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1618,10 +1618,20 @@ def EmitC_ClassOp
16181618
return
16191619
}
16201620
}
1621+
// Class with a final speciferAdd commentMore actions
1622+
emitc.class final @modelClass {
1623+
emitc.field @fieldName0 : !emitc.array<1xf32> = {emitc.opaque = "input_tensor"}
1624+
emitc.func @execute() {
1625+
%0 = "emitc.constant"() <{value = 0 : index}> : () -> !emitc.size_t
1626+
%1 = get_field @fieldName0 : !emitc.array<1xf32>
1627+
%2 = subscript %1[%0] : (!emitc.array<1xf32>, !emitc.size_t) -> !emitc.lvalue<f32>
1628+
return
1629+
}
1630+
}
16211631
```
16221632
}];
16231633

1624-
let arguments = (ins SymbolNameAttr:$sym_name);
1634+
let arguments = (ins SymbolNameAttr:$sym_name, UnitAttr:$final_specifier);
16251635

16261636
let regions = (region AnyRegion:$body);
16271637

@@ -1632,7 +1642,8 @@ def EmitC_ClassOp
16321642

16331643
let hasCustomAssemblyFormat = 1;
16341644

1635-
let assemblyFormat = [{ $sym_name attr-dict-with-keyword $body }];
1645+
let assemblyFormat =
1646+
[{ (`final` $final_specifier^)? $sym_name attr-dict-with-keyword $body }];
16361647
}
16371648

16381649
def EmitC_FieldOp : EmitC_Op<"field", [Symbol]> {

mlir/lib/Target/Cpp/TranslateToCpp.cpp

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1000,8 +1000,10 @@ static LogicalResult printOperation(CppEmitter &emitter, ModuleOp moduleOp) {
10001000
static LogicalResult printOperation(CppEmitter &emitter, ClassOp classOp) {
10011001
CppEmitter::Scope classScope(emitter);
10021002
raw_indented_ostream &os = emitter.ostream();
1003-
os << "class " << classOp.getSymName() << " {\n";
1004-
os << "public:\n";
1003+
os << "class " << classOp.getSymName();
1004+
if (classOp.getFinalSpecifier())
1005+
os << " final";
1006+
os << " {\n public:\n";
10051007
os.indent();
10061008

10071009
for (Operation &op : classOp) {

mlir/test/mlir-translate/emitc_classops.mlir

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,32 @@ emitc.class @modelClass {
1212
}
1313
}
1414

15-
// CHECK: class modelClass {
15+
// CHECK-LABEL: class modelClass {
16+
// CHECK-NEXT: public:
17+
// CHECK-NEXT: float[1] fieldName0;
18+
// CHECK-NEXT: float[1] fieldName1;
19+
// CHECK-NEXT: void execute() {
20+
// CHECK-NEXT: size_t v1 = 0;
21+
// CHECK-NEXT: float[1] v2 = fieldName0;
22+
// CHECK-NEXT: float[1] v3 = fieldName1;
23+
// CHECK-NEXT: return;
24+
// CHECK-NEXT: }
25+
// CHECK-EMPTY:
26+
// CHECK-NEXT: };
27+
28+
emitc.class final @finalClass {
29+
emitc.field @fieldName0 : !emitc.array<1xf32>
30+
emitc.field @fieldName1 : !emitc.array<1xf32>
31+
emitc.func @execute() {
32+
%0 = "emitc.constant"() <{value = 0 : index}> : () -> !emitc.size_t
33+
%1 = get_field @fieldName0 : !emitc.array<1xf32>
34+
%2 = get_field @fieldName1 : !emitc.array<1xf32>
35+
%3 = subscript %1[%0] : (!emitc.array<1xf32>, !emitc.size_t) -> !emitc.lvalue<f32>
36+
return
37+
}
38+
}
39+
40+
// CHECK-LABEL: class finalClass final {
1641
// CHECK-NEXT: public:
1742
// CHECK-NEXT: float[1] fieldName0;
1843
// CHECK-NEXT: float[1] fieldName1;

0 commit comments

Comments
 (0)