@@ -116,7 +116,7 @@ namespace {
116
116
// / Emitter that uses dialect specific emitters to emit C++ code.
117
117
struct CppEmitter {
118
118
explicit CppEmitter (raw_ostream &os, bool declareVariablesAtTop,
119
- StringRef onlyTu);
119
+ StringRef onlyTu, bool constantsAsVariables );
120
120
121
121
// / Emits attribute or returns failure.
122
122
LogicalResult emitAttribute (Location loc, Attribute attr);
@@ -235,6 +235,10 @@ struct CppEmitter {
235
235
// / Returns whether this translation unit should be emitted
236
236
bool shouldEmitTu (TranslationUnitOp tu) { return tu.getId () == onlyTu; }
237
237
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
+
238
242
// / Get expression currently being emitted.
239
243
ExpressionOp getEmittedExpression () { return emittedExpression; }
240
244
@@ -265,6 +269,9 @@ struct CppEmitter {
265
269
// / Only emit translation units whos id matches this value.
266
270
std::string onlyTu;
267
271
272
+ // / Use variables to hold the constant values
273
+ bool constantsAsVariables;
274
+
268
275
// / Map from value to name of C++ variable that contain the name.
269
276
ValueMapper valueMapper;
270
277
@@ -365,6 +372,10 @@ static LogicalResult printConstantOp(CppEmitter &emitter, Operation *operation,
365
372
366
373
static LogicalResult printOperation (CppEmitter &emitter,
367
374
emitc::ConstantOp constantOp) {
375
+ if (!emitter.shouldUseConstantsAsVariables ()) {
376
+ return success ();
377
+ }
378
+
368
379
Operation *operation = constantOp.getOperation ();
369
380
Attribute value = constantOp.getValue ();
370
381
@@ -1218,9 +1229,9 @@ static LogicalResult printOperation(CppEmitter &emitter,
1218
1229
}
1219
1230
1220
1231
CppEmitter::CppEmitter (raw_ostream &os, bool declareVariablesAtTop,
1221
- StringRef onlyTu)
1232
+ StringRef onlyTu, bool constantsAsVariables )
1222
1233
: os(os), declareVariablesAtTop(declareVariablesAtTop),
1223
- onlyTu(onlyTu.str()) {
1234
+ onlyTu(onlyTu.str()), constantsAsVariables(constantsAsVariables) {
1224
1235
valueInScopeCount.push (0 );
1225
1236
labelInScopeCount.push (0 );
1226
1237
}
@@ -1425,8 +1436,25 @@ LogicalResult CppEmitter::emitExpression(ExpressionOp expressionOp) {
1425
1436
}
1426
1437
1427
1438
LogicalResult 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
+
1428
1457
if (isPartOfCurrentExpression (value)) {
1429
- Operation *def = value.getDefiningOp ();
1430
1458
assert (def && " Expected operand to be defined by an operation" );
1431
1459
FailureOr<int > precedence = getOperatorPrecedence (def);
1432
1460
if (failed (precedence))
@@ -1452,7 +1480,7 @@ LogicalResult CppEmitter::emitOperand(Value value) {
1452
1480
return success ();
1453
1481
}
1454
1482
1455
- auto expressionOp = dyn_cast_if_present<ExpressionOp>(value. getDefiningOp () );
1483
+ auto expressionOp = dyn_cast_if_present<ExpressionOp>(def );
1456
1484
if (expressionOp && shouldBeInlined (expressionOp))
1457
1485
return emitExpression (expressionOp);
1458
1486
@@ -1671,7 +1699,13 @@ LogicalResult CppEmitter::emitOperation(Operation &op, bool trailingSemicolon) {
1671
1699
trailingSemicolon = false ;
1672
1700
}
1673
1701
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 " : " " );
1675
1709
1676
1710
return success ();
1677
1711
}
@@ -1837,7 +1871,8 @@ LogicalResult CppEmitter::emitTupleType(Location loc, ArrayRef<Type> types) {
1837
1871
1838
1872
LogicalResult emitc::translateToCpp (Operation *op, raw_ostream &os,
1839
1873
bool declareVariablesAtTop,
1840
- StringRef onlyTu) {
1841
- CppEmitter emitter (os, declareVariablesAtTop, onlyTu);
1874
+ StringRef onlyTu,
1875
+ bool constantsAsVariables) {
1876
+ CppEmitter emitter (os, declareVariablesAtTop, onlyTu, constantsAsVariables);
1842
1877
return emitter.emitOperation (*op, /* trailingSemicolon=*/ false );
1843
1878
}
0 commit comments