@@ -154,6 +154,9 @@ struct CppEmitter {
154
154
// / any result type could not be converted.
155
155
LogicalResult emitAssignPrefix (Operation &op);
156
156
157
+ // / Emits a global variable declaration or definition.
158
+ LogicalResult emitGlobalVariable (GlobalOp op);
159
+
157
160
// / Emits a label for the block.
158
161
LogicalResult emitLabel (Block &block);
159
162
@@ -344,6 +347,12 @@ static LogicalResult printOperation(CppEmitter &emitter,
344
347
return printConstantOp (emitter, operation, value);
345
348
}
346
349
350
+ static LogicalResult printOperation (CppEmitter &emitter,
351
+ emitc::GlobalOp globalOp) {
352
+
353
+ return emitter.emitGlobalVariable (globalOp);
354
+ }
355
+
347
356
static LogicalResult printOperation (CppEmitter &emitter,
348
357
emitc::AssignOp assignOp) {
349
358
OpResult result = assignOp.getVar ().getDefiningOp ()->getResult (0 );
@@ -354,6 +363,13 @@ static LogicalResult printOperation(CppEmitter &emitter,
354
363
return emitter.emitOperand (assignOp.getValue ());
355
364
}
356
365
366
+ static LogicalResult printOperation (CppEmitter &emitter,
367
+ emitc::GetGlobalOp op) {
368
+ // Add name to cache so that `hasValueInScope` works.
369
+ emitter.getOrCreateName (op.getResult ());
370
+ return success ();
371
+ }
372
+
357
373
static LogicalResult printOperation (CppEmitter &emitter,
358
374
emitc::SubscriptOp subscriptOp) {
359
375
// Add name to cache so that `hasValueInScope` works.
@@ -1119,6 +1135,9 @@ StringRef CppEmitter::getOrCreateName(Value val) {
1119
1135
if (auto subscript =
1120
1136
dyn_cast_if_present<emitc::SubscriptOp>(val.getDefiningOp ())) {
1121
1137
valueMapper.insert (val, getSubscriptName (subscript));
1138
+ } else if (auto getGlobal = dyn_cast_if_present<emitc::GetGlobalOp>(
1139
+ val.getDefiningOp ())) {
1140
+ valueMapper.insert (val, getGlobal.getName ().str ());
1122
1141
} else {
1123
1142
valueMapper.insert (val, formatv (" v{0}" , ++valueInScopeCount.top ()));
1124
1143
}
@@ -1384,6 +1403,30 @@ LogicalResult CppEmitter::emitVariableDeclaration(OpResult result,
1384
1403
return success ();
1385
1404
}
1386
1405
1406
+ LogicalResult CppEmitter::emitGlobalVariable (GlobalOp op) {
1407
+ if (op.getExternSpecifier ())
1408
+ os << " extern " ;
1409
+ else if (op.getStaticSpecifier ())
1410
+ os << " static " ;
1411
+ if (op.getConstSpecifier ())
1412
+ os << " const " ;
1413
+
1414
+ if (failed (emitVariableDeclaration (op->getLoc (), op.getType (),
1415
+ op.getSymName ()))) {
1416
+ return failure ();
1417
+ }
1418
+
1419
+ std::optional<Attribute> initialValue = op.getInitialValue ();
1420
+ if (initialValue) {
1421
+ os << " = " ;
1422
+ if (failed (emitAttribute (op->getLoc (), *initialValue)))
1423
+ return failure ();
1424
+ }
1425
+
1426
+ os << " ;" ;
1427
+ return success ();
1428
+ }
1429
+
1387
1430
LogicalResult CppEmitter::emitAssignPrefix (Operation &op) {
1388
1431
// If op is being emitted as part of an expression, bail out.
1389
1432
if (getEmittedExpression ())
@@ -1444,11 +1487,11 @@ LogicalResult CppEmitter::emitOperation(Operation &op, bool trailingSemicolon) {
1444
1487
emitc::CallOpaqueOp, emitc::CastOp, emitc::CmpOp,
1445
1488
emitc::ConditionalOp, emitc::ConstantOp, emitc::DeclareFuncOp,
1446
1489
emitc::DivOp, emitc::ExpressionOp, emitc::ForOp, emitc::FuncOp,
1447
- emitc::IfOp , emitc::IncludeOp , emitc::LogicalAndOp ,
1448
- emitc::LogicalNotOp , emitc::LogicalOrOp , emitc::MulOp ,
1449
- emitc::RemOp , emitc::ReturnOp , emitc::SubOp , emitc::SubscriptOp ,
1450
- emitc::UnaryMinusOp , emitc::UnaryPlusOp , emitc::VariableOp ,
1451
- emitc::VerbatimOp>(
1490
+ emitc::GlobalOp , emitc::GetGlobalOp , emitc::IfOp ,
1491
+ emitc::IncludeOp , emitc::LogicalAndOp , emitc::LogicalNotOp ,
1492
+ emitc::LogicalOrOp , emitc::MulOp , emitc::RemOp , emitc::ReturnOp ,
1493
+ emitc::SubOp , emitc::SubscriptOp , emitc::UnaryMinusOp ,
1494
+ emitc::UnaryPlusOp, emitc::VariableOp, emitc:: VerbatimOp>(
1452
1495
[&](auto op) { return printOperation (*this , op); })
1453
1496
// Func ops.
1454
1497
.Case <func::CallOp, func::FuncOp, func::ReturnOp>(
@@ -1461,7 +1504,7 @@ LogicalResult CppEmitter::emitOperation(Operation &op, bool trailingSemicolon) {
1461
1504
if (failed (status))
1462
1505
return failure ();
1463
1506
1464
- if (isa<emitc::LiteralOp, emitc::SubscriptOp>(op))
1507
+ if (isa<emitc::LiteralOp, emitc::SubscriptOp, emitc::GetGlobalOp >(op))
1465
1508
return success ();
1466
1509
1467
1510
if (getEmittedExpression () ||
0 commit comments