@@ -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.
@@ -1120,6 +1136,9 @@ StringRef CppEmitter::getOrCreateName(Value val) {
1120
1136
if (auto subscript =
1121
1137
dyn_cast_if_present<emitc::SubscriptOp>(val.getDefiningOp ())) {
1122
1138
valueMapper.insert (val, getSubscriptName (subscript));
1139
+ } else if (auto getGlobal = dyn_cast_if_present<emitc::GetGlobalOp>(
1140
+ val.getDefiningOp ())) {
1141
+ valueMapper.insert (val, getGlobal.getName ().str ());
1123
1142
} else {
1124
1143
valueMapper.insert (val, formatv (" v{0}" , ++valueInScopeCount.top ()));
1125
1144
}
@@ -1385,6 +1404,30 @@ LogicalResult CppEmitter::emitVariableDeclaration(OpResult result,
1385
1404
return success ();
1386
1405
}
1387
1406
1407
+ LogicalResult CppEmitter::emitGlobalVariable (GlobalOp op) {
1408
+ if (op.getExternSpecifier ())
1409
+ os << " extern " ;
1410
+ else if (op.getStaticSpecifier ())
1411
+ os << " static " ;
1412
+ if (op.getConstSpecifier ())
1413
+ os << " const " ;
1414
+
1415
+ if (failed (emitVariableDeclaration (op->getLoc (), op.getType (),
1416
+ op.getSymName ()))) {
1417
+ return failure ();
1418
+ }
1419
+
1420
+ std::optional<Attribute> initialValue = op.getInitialValue ();
1421
+ if (initialValue && !isa<UnitAttr>(*initialValue)) {
1422
+ os << " = " ;
1423
+ if (failed (emitAttribute (op->getLoc (), *initialValue)))
1424
+ return failure ();
1425
+ }
1426
+
1427
+ os << " ;" ;
1428
+ return success ();
1429
+ }
1430
+
1388
1431
LogicalResult CppEmitter::emitAssignPrefix (Operation &op) {
1389
1432
// If op is being emitted as part of an expression, bail out.
1390
1433
if (getEmittedExpression ())
@@ -1445,11 +1488,11 @@ LogicalResult CppEmitter::emitOperation(Operation &op, bool trailingSemicolon) {
1445
1488
emitc::CallOpaqueOp, emitc::CastOp, emitc::CmpOp,
1446
1489
emitc::ConditionalOp, emitc::ConstantOp, emitc::DeclareFuncOp,
1447
1490
emitc::DivOp, emitc::ExpressionOp, emitc::ForOp, emitc::FuncOp,
1448
- emitc::IfOp , emitc::IncludeOp , emitc::LogicalAndOp ,
1449
- emitc::LogicalNotOp , emitc::LogicalOrOp , emitc::MulOp ,
1450
- emitc::RemOp , emitc::ReturnOp , emitc::SubOp , emitc::SubscriptOp ,
1451
- emitc::UnaryMinusOp , emitc::UnaryPlusOp , emitc::VariableOp ,
1452
- emitc::VerbatimOp>(
1491
+ emitc::GlobalOp , emitc::GetGlobalOp , emitc::IfOp ,
1492
+ emitc::IncludeOp , emitc::LogicalAndOp , emitc::LogicalNotOp ,
1493
+ emitc::LogicalOrOp , emitc::MulOp , emitc::RemOp , emitc::ReturnOp ,
1494
+ emitc::SubOp , emitc::SubscriptOp , emitc::UnaryMinusOp ,
1495
+ emitc::UnaryPlusOp, emitc::VariableOp, emitc:: VerbatimOp>(
1453
1496
[&](auto op) { return printOperation (*this , op); })
1454
1497
// Func ops.
1455
1498
.Case <func::CallOp, func::FuncOp, func::ReturnOp>(
@@ -1462,7 +1505,7 @@ LogicalResult CppEmitter::emitOperation(Operation &op, bool trailingSemicolon) {
1462
1505
if (failed (status))
1463
1506
return failure ();
1464
1507
1465
- if (isa<emitc::LiteralOp, emitc::SubscriptOp>(op))
1508
+ if (isa<emitc::LiteralOp, emitc::SubscriptOp, emitc::GetGlobalOp >(op))
1466
1509
return success ();
1467
1510
1468
1511
if (getEmittedExpression () ||
0 commit comments