@@ -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);
119+ StringRef onlyTu, bool constantsAsVariables );
120120
121121 // / Emits attribute or returns failure.
122122 LogicalResult emitAttribute (Location loc, Attribute attr);
@@ -235,6 +235,10 @@ struct CppEmitter {
235235 // / Returns whether this translation unit should be emitted
236236 bool shouldEmitTu (TranslationUnitOp tu) { return tu.getId () == onlyTu; }
237237
238+ // / Returns whether the value of ConstantOps should be stored in variables
239+ // / or emmited directly in their usage locations.
240+ bool shouldUseConstantsAsVariables () { return constantsAsVariables; }
241+
238242 // / Get expression currently being emitted.
239243 ExpressionOp getEmittedExpression () { return emittedExpression; }
240244
@@ -265,6 +269,9 @@ struct CppEmitter {
265269 // / Only emit translation units whos id matches this value.
266270 std::string onlyTu;
267271
272+ // / Use variables to hold the constant values
273+ bool constantsAsVariables;
274+
268275 // / Map from value to name of C++ variable that contain the name.
269276 ValueMapper valueMapper;
270277
@@ -365,6 +372,10 @@ static LogicalResult printConstantOp(CppEmitter &emitter, Operation *operation,
365372
366373static LogicalResult printOperation (CppEmitter &emitter,
367374 emitc::ConstantOp constantOp) {
375+ if (!emitter.shouldUseConstantsAsVariables ()) {
376+ return success ();
377+ }
378+
368379 Operation *operation = constantOp.getOperation ();
369380 Attribute value = constantOp.getValue ();
370381
@@ -1218,9 +1229,9 @@ static LogicalResult printOperation(CppEmitter &emitter,
12181229}
12191230
12201231CppEmitter::CppEmitter (raw_ostream &os, bool declareVariablesAtTop,
1221- StringRef onlyTu)
1232+ StringRef onlyTu, bool constantsAsVariables )
12221233 : os(os), declareVariablesAtTop(declareVariablesAtTop),
1223- onlyTu(onlyTu.str()) {
1234+ onlyTu(onlyTu.str()), constantsAsVariables(constantsAsVariables) {
12241235 valueInScopeCount.push (0 );
12251236 labelInScopeCount.push (0 );
12261237}
@@ -1425,8 +1436,25 @@ LogicalResult CppEmitter::emitExpression(ExpressionOp expressionOp) {
14251436}
14261437
14271438LogicalResult CppEmitter::emitOperand (Value value) {
1439+ Operation *def = value.getDefiningOp ();
1440+ if (!shouldUseConstantsAsVariables ()) {
1441+ if (auto constant = dyn_cast_if_present<ConstantOp>(def)) {
1442+ os << " ((" ;
1443+
1444+ if (failed (emitType (constant.getLoc (), constant.getType ()))) {
1445+ return failure ();
1446+ }
1447+ os << " ) " ;
1448+
1449+ if (failed (emitAttribute (constant.getLoc (), constant.getValue ()))) {
1450+ return failure ();
1451+ }
1452+ os << " )" ;
1453+ return success ();
1454+ }
1455+ }
1456+
14281457 if (isPartOfCurrentExpression (value)) {
1429- Operation *def = value.getDefiningOp ();
14301458 assert (def && " Expected operand to be defined by an operation" );
14311459 FailureOr<int > precedence = getOperatorPrecedence (def);
14321460 if (failed (precedence))
@@ -1452,7 +1480,7 @@ LogicalResult CppEmitter::emitOperand(Value value) {
14521480 return success ();
14531481 }
14541482
1455- auto expressionOp = dyn_cast_if_present<ExpressionOp>(value. getDefiningOp () );
1483+ auto expressionOp = dyn_cast_if_present<ExpressionOp>(def );
14561484 if (expressionOp && shouldBeInlined (expressionOp))
14571485 return emitExpression (expressionOp);
14581486
@@ -1671,7 +1699,13 @@ LogicalResult CppEmitter::emitOperation(Operation &op, bool trailingSemicolon) {
16711699 trailingSemicolon = false ;
16721700 }
16731701
1674- os << (trailingSemicolon ? " ;\n " : " \n " );
1702+ bool trailingNewline = true ;
1703+ if (!shouldUseConstantsAsVariables () && isa<emitc::ConstantOp>(op)) {
1704+ trailingSemicolon = false ;
1705+ trailingNewline = false ;
1706+ }
1707+
1708+ os << (trailingSemicolon ? " ;" : " " ) << (trailingNewline ? " \n " : " " );
16751709
16761710 return success ();
16771711}
@@ -1837,7 +1871,8 @@ LogicalResult CppEmitter::emitTupleType(Location loc, ArrayRef<Type> types) {
18371871
18381872LogicalResult emitc::translateToCpp (Operation *op, raw_ostream &os,
18391873 bool declareVariablesAtTop,
1840- StringRef onlyTu) {
1841- CppEmitter emitter (os, declareVariablesAtTop, onlyTu);
1874+ StringRef onlyTu,
1875+ bool constantsAsVariables) {
1876+ CppEmitter emitter (os, declareVariablesAtTop, onlyTu, constantsAsVariables);
18421877 return emitter.emitOperation (*op, /* trailingSemicolon=*/ false );
18431878}
0 commit comments