@@ -115,7 +115,8 @@ static FailureOr<int> getOperatorPrecedence(Operation *operation) {
115115namespace {
116116// / Emitter that uses dialect specific emitters to emit C++ code.
117117struct CppEmitter {
118- explicit CppEmitter (raw_ostream &os, bool declareVariablesAtTop);
118+ explicit CppEmitter (raw_ostream &os, bool declareVariablesAtTop,
119+ StringRef onlyTu);
119120
120121 // / Emits attribute or returns failure.
121122 LogicalResult emitAttribute (Location loc, Attribute attr);
@@ -231,6 +232,9 @@ struct CppEmitter {
231232 // / be declared at the beginning of a function.
232233 bool shouldDeclareVariablesAtTop () { return declareVariablesAtTop; };
233234
235+ // / Returns whether this translation unit should be emitted
236+ bool shouldEmitTu (TranslationUnitOp tu) { return tu.getId () == onlyTu; }
237+
234238 // / Get expression currently being emitted.
235239 ExpressionOp getEmittedExpression () { return emittedExpression; }
236240
@@ -258,6 +262,9 @@ struct CppEmitter {
258262 // / includes results from ops located in nested regions.
259263 bool declareVariablesAtTop;
260264
265+ // / Only emit translation units whos id matches this value.
266+ std::string onlyTu;
267+
261268 // / Map from value to name of C++ variable that contain the name.
262269 ValueMapper valueMapper;
263270
@@ -936,6 +943,19 @@ static LogicalResult printOperation(CppEmitter &emitter, ModuleOp moduleOp) {
936943 return success ();
937944}
938945
946+ static LogicalResult printOperation (CppEmitter &emitter, TranslationUnitOp tu) {
947+ if (!emitter.shouldEmitTu (tu))
948+ return success ();
949+
950+ CppEmitter::Scope scope (emitter);
951+
952+ for (Operation &op : tu) {
953+ if (failed (emitter.emitOperation (op, /* trailingSemicolon=*/ false )))
954+ return failure ();
955+ }
956+ return success ();
957+ }
958+
939959template <class FuncOpClass >
940960static LogicalResult printFunctionArgs (CppEmitter &emitter,
941961 FuncOpClass functionOp,
@@ -1177,8 +1197,10 @@ static LogicalResult printOperation(CppEmitter &emitter,
11771197 return success ();
11781198}
11791199
1180- CppEmitter::CppEmitter (raw_ostream &os, bool declareVariablesAtTop)
1181- : os(os), declareVariablesAtTop(declareVariablesAtTop) {
1200+ CppEmitter::CppEmitter (raw_ostream &os, bool declareVariablesAtTop,
1201+ StringRef onlyTu)
1202+ : os(os), declareVariablesAtTop(declareVariablesAtTop),
1203+ onlyTu(onlyTu.str()) {
11821204 valueInScopeCount.push (0 );
11831205 labelInScopeCount.push (0 );
11841206}
@@ -1580,8 +1602,8 @@ LogicalResult CppEmitter::emitOperation(Operation &op, bool trailingSemicolon) {
15801602 emitc::GlobalOp, emitc::IfOp, emitc::IncludeOp,
15811603 emitc::LogicalAndOp, emitc::LogicalNotOp, emitc::LogicalOrOp,
15821604 emitc::MulOp, emitc::RemOp, emitc::ReturnOp, emitc::SubOp,
1583- emitc::UnaryMinusOp , emitc::UnaryPlusOp, emitc::VariableOp ,
1584- emitc::VerbatimOp>(
1605+ emitc::TranslationUnitOp , emitc::UnaryMinusOp ,
1606+ emitc::UnaryPlusOp, emitc::VariableOp, emitc:: VerbatimOp>(
15851607 [&](auto op) { return printOperation (*this , op); })
15861608 // Func ops.
15871609 .Case <func::CallOp, func::FuncOp, func::ReturnOp>(
@@ -1791,7 +1813,8 @@ LogicalResult CppEmitter::emitTupleType(Location loc, ArrayRef<Type> types) {
17911813}
17921814
17931815LogicalResult emitc::translateToCpp (Operation *op, raw_ostream &os,
1794- bool declareVariablesAtTop) {
1795- CppEmitter emitter (os, declareVariablesAtTop);
1816+ bool declareVariablesAtTop,
1817+ StringRef onlyTu) {
1818+ CppEmitter emitter (os, declareVariablesAtTop, onlyTu);
17961819 return emitter.emitOperation (*op, /* trailingSemicolon=*/ false );
17971820}
0 commit comments