@@ -114,7 +114,8 @@ static FailureOr<int> getOperatorPrecedence(Operation *operation) {
114114namespace {
115115// / Emitter that uses dialect specific emitters to emit C++ code.
116116struct CppEmitter {
117- explicit CppEmitter (raw_ostream &os, bool declareVariablesAtTop);
117+ explicit CppEmitter (raw_ostream &os, bool declareVariablesAtTop,
118+ StringRef fileId);
118119
119120 // / Emits attribute or returns failure.
120121 LogicalResult emitAttribute (Location loc, Attribute attr);
@@ -231,6 +232,11 @@ struct CppEmitter {
231232 // / be declared at the beginning of a function.
232233 bool shouldDeclareVariablesAtTop () { return declareVariablesAtTop; };
233234
235+ // / Returns whether this file op should be emitted
236+ bool shouldEmitFile (FileOp file) {
237+ return !fileId.empty () && file.getId () == fileId;
238+ }
239+
234240 // / Get expression currently being emitted.
235241 ExpressionOp getEmittedExpression () { return emittedExpression; }
236242
@@ -258,6 +264,9 @@ struct CppEmitter {
258264 // / includes results from ops located in nested regions.
259265 bool declareVariablesAtTop;
260266
267+ // / Only emit file ops whos id matches this value.
268+ std::string fileId;
269+
261270 // / Map from value to name of C++ variable that contain the name.
262271 ValueMapper valueMapper;
263272
@@ -963,6 +972,19 @@ static LogicalResult printOperation(CppEmitter &emitter, ModuleOp moduleOp) {
963972 return success ();
964973}
965974
975+ static LogicalResult printOperation (CppEmitter &emitter, FileOp file) {
976+ if (!emitter.shouldEmitFile (file))
977+ return success ();
978+
979+ CppEmitter::Scope scope (emitter);
980+
981+ for (Operation &op : file) {
982+ if (failed (emitter.emitOperation (op, /* trailingSemicolon=*/ false )))
983+ return failure ();
984+ }
985+ return success ();
986+ }
987+
966988static LogicalResult printFunctionArgs (CppEmitter &emitter,
967989 Operation *functionOp,
968990 ArrayRef<Type> arguments) {
@@ -1162,8 +1184,10 @@ static LogicalResult printOperation(CppEmitter &emitter,
11621184 return success ();
11631185}
11641186
1165- CppEmitter::CppEmitter (raw_ostream &os, bool declareVariablesAtTop)
1166- : os(os), declareVariablesAtTop(declareVariablesAtTop) {
1187+ CppEmitter::CppEmitter (raw_ostream &os, bool declareVariablesAtTop,
1188+ StringRef fileId)
1189+ : os(os), declareVariablesAtTop(declareVariablesAtTop),
1190+ fileId(fileId.str()) {
11671191 valueInScopeCount.push (0 );
11681192 labelInScopeCount.push (0 );
11691193}
@@ -1558,12 +1582,13 @@ LogicalResult CppEmitter::emitOperation(Operation &op, bool trailingSemicolon) {
15581582 emitc::BitwiseRightShiftOp, emitc::BitwiseXorOp, emitc::CallOp,
15591583 emitc::CallOpaqueOp, emitc::CastOp, emitc::CmpOp,
15601584 emitc::ConditionalOp, emitc::ConstantOp, emitc::DeclareFuncOp,
1561- emitc::DivOp, emitc::ExpressionOp, emitc::ForOp, emitc::FuncOp,
1562- emitc::GlobalOp, emitc::IfOp, emitc::IncludeOp, emitc::LoadOp,
1563- emitc::LogicalAndOp, emitc::LogicalNotOp, emitc::LogicalOrOp,
1564- emitc::MulOp, emitc::RemOp, emitc::ReturnOp, emitc::SubOp,
1565- emitc::SwitchOp, emitc::UnaryMinusOp, emitc::UnaryPlusOp,
1566- emitc::VariableOp, emitc::VerbatimOp>(
1585+ emitc::DivOp, emitc::ExpressionOp, emitc::FileOp, emitc::ForOp,
1586+ emitc::FuncOp, emitc::GlobalOp, emitc::IfOp, emitc::IncludeOp,
1587+ emitc::LoadOp, emitc::LogicalAndOp, emitc::LogicalNotOp,
1588+ emitc::LogicalOrOp, emitc::MulOp, emitc::RemOp, emitc::ReturnOp,
1589+ emitc::SubOp, emitc::SwitchOp, emitc::UnaryMinusOp,
1590+ emitc::UnaryPlusOp, emitc::VariableOp, emitc::VerbatimOp>(
1591+
15671592 [&](auto op) { return printOperation (*this , op); })
15681593 // Func ops.
15691594 .Case <func::CallOp, func::FuncOp, func::ReturnOp>(
@@ -1606,8 +1631,9 @@ LogicalResult CppEmitter::emitOperation(Operation &op, bool trailingSemicolon) {
16061631 // Never emit a semicolon for some operations, especially if endening with
16071632 // `}`.
16081633 trailingSemicolon &=
1609- !isa<cf::CondBranchOp, emitc::DeclareFuncOp, emitc::ForOp, emitc::IfOp,
1610- emitc::IncludeOp, emitc::SwitchOp, emitc::VerbatimOp>(op);
1634+ !isa<cf::CondBranchOp, emitc::DeclareFuncOp, emitc::FileOp, emitc::ForOp,
1635+ emitc::IfOp, emitc::IncludeOp, emitc::SwitchOp, emitc::VerbatimOp>(
1636+ op);
16111637
16121638 os << (trailingSemicolon ? " ;\n " : " \n " );
16131639
@@ -1743,7 +1769,8 @@ LogicalResult CppEmitter::emitTupleType(Location loc, ArrayRef<Type> types) {
17431769}
17441770
17451771LogicalResult emitc::translateToCpp (Operation *op, raw_ostream &os,
1746- bool declareVariablesAtTop) {
1747- CppEmitter emitter (os, declareVariablesAtTop);
1772+ bool declareVariablesAtTop,
1773+ StringRef fileId) {
1774+ CppEmitter emitter (os, declareVariablesAtTop, fileId);
17481775 return emitter.emitOperation (*op, /* trailingSemicolon=*/ false );
17491776}
0 commit comments