@@ -116,7 +116,7 @@ namespace {
116116// / Emitter that uses dialect specific emitters to emit C++ code.
117117struct CppEmitter {
118118 explicit CppEmitter (raw_ostream &os, bool declareVariablesAtTop,
119- StringRef onlyTu , bool constantsAsVariables);
119+ StringRef fileId , bool constantsAsVariables);
120120
121121 // / Emits attribute or returns failure.
122122 LogicalResult emitAttribute (Location loc, Attribute attr);
@@ -257,7 +257,9 @@ struct CppEmitter {
257257 bool shouldDeclareVariablesAtTop () { return declareVariablesAtTop; };
258258
259259 // / Returns whether this translation unit should be emitted
260- bool shouldEmitTu (TranslationUnitOp tu) { return tu.getId () == onlyTu; }
260+ bool shouldEmitFile (FileOp file) {
261+ return !fileId.empty () && file.getId () == fileId;
262+ }
261263
262264 // / Returns whether the value of ConstantOps should be stored in variables
263265 // / or emmited directly in their usage locations.
@@ -285,8 +287,8 @@ struct CppEmitter {
285287 // / taken care of by transformations run by the backend.
286288 bool shouldBeInlined (ExpressionOp expressionOp);
287289
288- // / This emitter will only emit translation units whos id matches this value.
289- StringRef willOnlyEmitTu () { return onlyTu ; }
290+ // / This emitter will only emit a file whos id matches this value.
291+ StringRef willOnlyEmitFile () { return fileId ; }
290292
291293 // Resets the value counter to 0
292294 void resetValueCounter ();
@@ -309,8 +311,8 @@ struct CppEmitter {
309311 // / includes results from ops located in nested regions.
310312 bool declareVariablesAtTop;
311313
312- // / Only emit translation units whos id matches this value.
313- std::string onlyTu ;
314+ // / Only emit file ops whos id matches this value.
315+ std::string fileId ;
314316
315317 // / Use variables to hold the constant values
316318 bool constantsAsVariables;
@@ -431,7 +433,7 @@ static LogicalResult printOperation(CppEmitter &emitter,
431433 // / Temporary emitter object that writes to our stream instead of the output
432434 // / allowing for the capture and caching of the produced string.
433435 CppEmitter sniffer = CppEmitter (ss, emitter.shouldDeclareVariablesAtTop (),
434- emitter.willOnlyEmitTu (),
436+ emitter.willOnlyEmitFile (),
435437 emitter.shouldUseConstantsAsVariables ());
436438
437439 ss << " (" ;
@@ -1091,11 +1093,11 @@ static LogicalResult printOperation(CppEmitter &emitter, ModuleOp moduleOp) {
10911093 return success ();
10921094}
10931095
1094- static LogicalResult printOperation (CppEmitter &emitter, TranslationUnitOp tu ) {
1095- if (!emitter.shouldEmitTu (tu ))
1096+ static LogicalResult printOperation (CppEmitter &emitter, FileOp file ) {
1097+ if (!emitter.shouldEmitFile (file ))
10961098 return success ();
10971099
1098- for (Operation &op : tu ) {
1100+ for (Operation &op : file ) {
10991101 if (failed (emitter.emitOperation (op, /* trailingSemicolon=*/ false )))
11001102 return failure ();
11011103 }
@@ -1348,9 +1350,9 @@ static LogicalResult printOperation(CppEmitter &emitter,
13481350}
13491351
13501352CppEmitter::CppEmitter (raw_ostream &os, bool declareVariablesAtTop,
1351- StringRef onlyTu , bool constantsAsVariables)
1353+ StringRef fileId , bool constantsAsVariables)
13521354 : os(os), declareVariablesAtTop(declareVariablesAtTop),
1353- onlyTu (onlyTu .str()), constantsAsVariables(constantsAsVariables),
1355+ fileId (fileId .str()), constantsAsVariables(constantsAsVariables),
13541356 defaultValueMapperScope(valueMapper),
13551357 defaultBlockMapperScope(blockMapper) {
13561358 labelInScopeCount.push (0 );
@@ -1778,11 +1780,11 @@ LogicalResult CppEmitter::emitOperation(Operation &op, bool trailingSemicolon) {
17781780 emitc::BitwiseRightShiftOp, emitc::BitwiseXorOp, emitc::CallOp,
17791781 emitc::CallOpaqueOp, emitc::CastOp, emitc::CmpOp,
17801782 emitc::ConditionalOp, emitc::ConstantOp, emitc::DeclareFuncOp,
1781- emitc::DivOp, emitc::ExpressionOp, emitc::ForOp , emitc::FuncOp ,
1782- emitc::GlobalOp , emitc::IfOp , emitc::IncludeOp , emitc::LoadOp ,
1783- emitc::LogicalAndOp , emitc::LogicalNotOp , emitc::LogicalOrOp ,
1784- emitc::MulOp , emitc::RemOp , emitc::ReturnOp , emitc::SubOp ,
1785- emitc::SwitchOp , emitc::TranslationUnitOp , emitc::UnaryMinusOp,
1783+ emitc::DivOp, emitc::ExpressionOp, emitc::FileOp , emitc::ForOp ,
1784+ emitc::FuncOp , emitc::GlobalOp , emitc::IfOp , emitc::IncludeOp ,
1785+ emitc::LoadOp , emitc::LogicalAndOp , emitc::LogicalNotOp ,
1786+ emitc::LogicalOrOp , emitc::MulOp , emitc::RemOp , emitc::ReturnOp ,
1787+ emitc::SubOp , emitc::SwitchOp , emitc::UnaryMinusOp,
17861788 emitc::UnaryPlusOp, emitc::VariableOp, emitc::VerbatimOp>(
17871789 [&](auto op) { return printOperation (*this , op); })
17881790 // Func ops.
@@ -1814,16 +1816,17 @@ LogicalResult CppEmitter::emitOperation(Operation &op, bool trailingSemicolon) {
18141816 if (hasDeferredEmission (&op))
18151817 return success ();
18161818
1817- if (isa<ModuleOp, TranslationUnitOp >(op))
1819+ if (isa<ModuleOp, FileOp >(op))
18181820 return success (); // skip adding newlines
18191821
18201822 if (getEmittedExpression () ||
18211823 (isa<emitc::ExpressionOp>(op) &&
18221824 shouldBeInlined (cast<emitc::ExpressionOp>(op))))
18231825 return success ();
18241826
1825- if (isa<cf::CondBranchOp, emitc::DeclareFuncOp, emitc::ForOp, emitc::IfOp,
1826- emitc::IncludeOp, emitc::SwitchOp, emitc::VerbatimOp>(op)) {
1827+ if (isa<cf::CondBranchOp, emitc::DeclareFuncOp, emitc::FileOp, emitc::ForOp,
1828+ emitc::IfOp, emitc::IncludeOp, emitc::SwitchOp, emitc::VerbatimOp>(
1829+ op)) {
18271830 trailingSemicolon = false ;
18281831 }
18291832
@@ -2015,8 +2018,8 @@ void CppEmitter::decreaseLoopNestingLevel() { loopNestingLevel--; }
20152018
20162019LogicalResult emitc::translateToCpp (Operation *op, raw_ostream &os,
20172020 bool declareVariablesAtTop,
2018- StringRef onlyTu ,
2021+ StringRef fileId ,
20192022 bool constantsAsVariables) {
2020- CppEmitter emitter (os, declareVariablesAtTop, onlyTu , constantsAsVariables);
2023+ CppEmitter emitter (os, declareVariablesAtTop, fileId , constantsAsVariables);
20212024 return emitter.emitOperation (*op, /* trailingSemicolon=*/ false );
20222025}
0 commit comments