[mlir][emitC] Add support to emitter for classop, fieldop and getfieldop (#145605)
Add support to the emitter for `ClassOp`, `FieldOp` and `GetFieldOp`. These ops were introduced in #141158
This commit is contained in:
@@ -997,6 +997,47 @@ static LogicalResult printOperation(CppEmitter &emitter, ModuleOp moduleOp) {
|
||||
return success();
|
||||
}
|
||||
|
||||
static LogicalResult printOperation(CppEmitter &emitter, ClassOp classOp) {
|
||||
CppEmitter::Scope classScope(emitter);
|
||||
raw_indented_ostream &os = emitter.ostream();
|
||||
os << "class " << classOp.getSymName() << " {\n";
|
||||
os << "public:\n";
|
||||
os.indent();
|
||||
|
||||
for (Operation &op : classOp) {
|
||||
if (failed(emitter.emitOperation(op, /*trailingSemicolon=*/false)))
|
||||
return failure();
|
||||
}
|
||||
|
||||
os.unindent();
|
||||
os << "};";
|
||||
return success();
|
||||
}
|
||||
|
||||
static LogicalResult printOperation(CppEmitter &emitter, FieldOp fieldOp) {
|
||||
raw_ostream &os = emitter.ostream();
|
||||
if (failed(emitter.emitType(fieldOp->getLoc(), fieldOp.getType())))
|
||||
return failure();
|
||||
os << " " << fieldOp.getSymName() << ";";
|
||||
return success();
|
||||
}
|
||||
|
||||
static LogicalResult printOperation(CppEmitter &emitter,
|
||||
GetFieldOp getFieldOp) {
|
||||
raw_indented_ostream &os = emitter.ostream();
|
||||
|
||||
Value result = getFieldOp.getResult();
|
||||
if (failed(emitter.emitType(getFieldOp->getLoc(), result.getType())))
|
||||
return failure();
|
||||
os << " ";
|
||||
if (failed(emitter.emitOperand(result)))
|
||||
return failure();
|
||||
os << " = ";
|
||||
|
||||
os << getFieldOp.getFieldName().str();
|
||||
return success();
|
||||
}
|
||||
|
||||
static LogicalResult printOperation(CppEmitter &emitter, FileOp file) {
|
||||
if (!emitter.shouldEmitFile(file))
|
||||
return success();
|
||||
@@ -1605,14 +1646,16 @@ LogicalResult CppEmitter::emitOperation(Operation &op, bool trailingSemicolon) {
|
||||
emitc::BitwiseAndOp, emitc::BitwiseLeftShiftOp,
|
||||
emitc::BitwiseNotOp, emitc::BitwiseOrOp,
|
||||
emitc::BitwiseRightShiftOp, emitc::BitwiseXorOp, emitc::CallOp,
|
||||
emitc::CallOpaqueOp, emitc::CastOp, emitc::CmpOp,
|
||||
emitc::ConditionalOp, emitc::ConstantOp, emitc::DeclareFuncOp,
|
||||
emitc::DivOp, emitc::ExpressionOp, emitc::FileOp, emitc::ForOp,
|
||||
emitc::FuncOp, emitc::GlobalOp, emitc::IfOp, emitc::IncludeOp,
|
||||
emitc::LoadOp, emitc::LogicalAndOp, emitc::LogicalNotOp,
|
||||
emitc::LogicalOrOp, emitc::MulOp, emitc::RemOp, emitc::ReturnOp,
|
||||
emitc::SubOp, emitc::SwitchOp, emitc::UnaryMinusOp,
|
||||
emitc::UnaryPlusOp, emitc::VariableOp, emitc::VerbatimOp>(
|
||||
emitc::CallOpaqueOp, emitc::CastOp, emitc::ClassOp,
|
||||
emitc::CmpOp, emitc::ConditionalOp, emitc::ConstantOp,
|
||||
emitc::DeclareFuncOp, emitc::DivOp, emitc::ExpressionOp,
|
||||
emitc::FieldOp, emitc::FileOp, emitc::ForOp, emitc::FuncOp,
|
||||
emitc::GetFieldOp, emitc::GlobalOp, emitc::IfOp,
|
||||
emitc::IncludeOp, emitc::LoadOp, emitc::LogicalAndOp,
|
||||
emitc::LogicalNotOp, emitc::LogicalOrOp, emitc::MulOp,
|
||||
emitc::RemOp, emitc::ReturnOp, emitc::SubOp, emitc::SwitchOp,
|
||||
emitc::UnaryMinusOp, emitc::UnaryPlusOp, emitc::VariableOp,
|
||||
emitc::VerbatimOp>(
|
||||
|
||||
[&](auto op) { return printOperation(*this, op); })
|
||||
// Func ops.
|
||||
|
||||
26
mlir/test/mlir-translate/emitc_classops.mlir
Normal file
26
mlir/test/mlir-translate/emitc_classops.mlir
Normal file
@@ -0,0 +1,26 @@
|
||||
// RUN: mlir-translate --mlir-to-cpp %s | FileCheck %s
|
||||
|
||||
emitc.class @modelClass {
|
||||
emitc.field @fieldName0 : !emitc.array<1xf32>
|
||||
emitc.field @fieldName1 : !emitc.array<1xf32>
|
||||
emitc.func @execute() {
|
||||
%0 = "emitc.constant"() <{value = 0 : index}> : () -> !emitc.size_t
|
||||
%1 = get_field @fieldName0 : !emitc.array<1xf32>
|
||||
%2 = get_field @fieldName1 : !emitc.array<1xf32>
|
||||
%3 = subscript %1[%0] : (!emitc.array<1xf32>, !emitc.size_t) -> !emitc.lvalue<f32>
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
// CHECK: class modelClass {
|
||||
// CHECK-NEXT: public:
|
||||
// CHECK-NEXT: float[1] fieldName0;
|
||||
// CHECK-NEXT: float[1] fieldName1;
|
||||
// CHECK-NEXT: void execute() {
|
||||
// CHECK-NEXT: size_t v1 = 0;
|
||||
// CHECK-NEXT: float[1] v2 = fieldName0;
|
||||
// CHECK-NEXT: float[1] v3 = fieldName1;
|
||||
// CHECK-NEXT: return;
|
||||
// CHECK-NEXT: }
|
||||
// CHECK-EMPTY:
|
||||
// CHECK-NEXT: };
|
||||
Reference in New Issue
Block a user