@@ -68,14 +68,6 @@ inline LogicalResult interleaveCommaWithError(const Container &c,
68
68
return interleaveWithError (c.begin (), c.end (), eachFn, [&]() { os << " , " ; });
69
69
}
70
70
71
- template <typename Container, typename UnaryFunctor>
72
- inline LogicalResult interleaveWithNewLineWithError (const Container &c,
73
- raw_ostream &os,
74
- UnaryFunctor eachFn) {
75
- return interleaveWithError (c.begin (), c.end (), eachFn,
76
- [&]() { os << " ;\n " ; });
77
- }
78
-
79
71
// / Return the precedence of a operator as an integer, higher values
80
72
// / imply higher precedence.
81
73
static FailureOr<int > getOperatorPrecedence (Operation *operation) {
@@ -124,8 +116,7 @@ namespace {
124
116
// / Emitter that uses dialect specific emitters to emit C++ code.
125
117
struct CppEmitter {
126
118
explicit CppEmitter (raw_ostream &os, bool declareVariablesAtTop,
127
- StringRef fileId, bool emitClass, StringRef className,
128
- StringRef fieldNameAttribute);
119
+ StringRef fileId);
129
120
130
121
// / Emits attribute or returns failure.
131
122
LogicalResult emitAttribute (Location loc, Attribute attr);
@@ -242,15 +233,6 @@ struct CppEmitter {
242
233
// / be declared at the beginning of a function.
243
234
bool shouldDeclareVariablesAtTop () { return declareVariablesAtTop; };
244
235
245
- // Returns whether we should emit a C++ class
246
- bool shouldPrintClass () { return emitClass; };
247
-
248
- // Returns the class name to emit
249
- std::string getClassName () { return className; };
250
-
251
- // Returns the field name to use in the map
252
- std::string getfieldNameAttribute () { return fieldNameAttribute; };
253
-
254
236
// / Returns whether this file op should be emitted
255
237
bool shouldEmitFile (FileOp file) {
256
238
return !fileId.empty () && file.getId () == fileId;
@@ -286,18 +268,6 @@ struct CppEmitter {
286
268
// / Only emit file ops whos id matches this value.
287
269
std::string fileId;
288
270
289
- // / Controls whether the output should be a C++ class.
290
- // / If true, the generated C++ code will be encapsulated within a class,
291
- // / and functions from the input module will become its member functions.
292
- const bool emitClass;
293
-
294
- // / The specified name for the generated C++ class
295
- const std::string className;
296
-
297
- // / Name of the MLIR attribute to use as a field name within the generated
298
- // / class
299
- const std::string fieldNameAttribute;
300
-
301
271
// / Map from value to name of C++ variable that contain the name.
302
272
ValueMapper valueMapper;
303
273
@@ -1063,17 +1033,6 @@ static LogicalResult printFunctionArgs(CppEmitter &emitter,
1063
1033
}));
1064
1034
}
1065
1035
1066
- static LogicalResult printFields (CppEmitter &emitter, Operation *functionOp,
1067
- Region::BlockArgListType arguments) {
1068
- raw_indented_ostream &os = emitter.ostream ();
1069
-
1070
- return (interleaveWithNewLineWithError (
1071
- arguments, os, [&](BlockArgument arg) -> LogicalResult {
1072
- return emitter.emitVariableDeclaration (
1073
- functionOp->getLoc (), arg.getType (), emitter.getOrCreateName (arg));
1074
- }));
1075
- }
1076
-
1077
1036
static LogicalResult printFunctionBody (CppEmitter &emitter,
1078
1037
Operation *functionOp,
1079
1038
Region::BlockListType &blocks) {
@@ -1178,45 +1137,6 @@ static LogicalResult printOperation(CppEmitter &emitter,
1178
1137
return success ();
1179
1138
}
1180
1139
1181
- static LogicalResult emitClassFields (CppEmitter &emitter,
1182
- emitc::FuncOp functionOp) {
1183
- raw_indented_ostream &os = emitter.ostream ();
1184
- auto argAttrs = functionOp.getArgAttrs ();
1185
- Operation *operation = functionOp.getOperation ();
1186
- if (failed (printFields (emitter, operation, functionOp.getArguments ())))
1187
- return failure ();
1188
- os << " ;\n " ;
1189
-
1190
- std::map<std::string, Value> fields;
1191
- os << " \n std::map<std::string, char*> _buffer_map {" ;
1192
- if (argAttrs) {
1193
- for (const auto [a, v] : zip (*argAttrs, functionOp.getArguments ())) {
1194
- if (auto da = dyn_cast<mlir::DictionaryAttr>(a)) {
1195
- auto nv = da.getNamed (emitter.getfieldNameAttribute ())->getValue ();
1196
- auto name = cast<mlir::StringAttr>(cast<mlir::ArrayAttr>(nv)[0 ]).str ();
1197
- auto Ins = fields.insert ({name, v});
1198
- if (!Ins.second )
1199
- return failure ();
1200
- os << " { \" " << name << " \" " << " , reinterpret_cast<char*>("
1201
- << emitter.getOrCreateName (v) << " ) }, " ;
1202
- }
1203
- }
1204
- } else
1205
- return failure ();
1206
-
1207
- os << " };\n " ;
1208
- os << " char* getBufferForName(const std::string& name) const {\n " ;
1209
- os.indent ();
1210
- os.indent ();
1211
- os << " auto it = _buffer_map.find(name);\n " ;
1212
- os << " return (it == _buffer_map.end()) ? nullptr : it->second;\n " ;
1213
- os.unindent ();
1214
- os.unindent ();
1215
- os << " }\n\n " ;
1216
-
1217
- return success ();
1218
- }
1219
-
1220
1140
static LogicalResult printOperation (CppEmitter &emitter,
1221
1141
emitc::FuncOp functionOp) {
1222
1142
// We need to declare variables at top if the function has multiple blocks.
@@ -1228,30 +1148,6 @@ static LogicalResult printOperation(CppEmitter &emitter,
1228
1148
1229
1149
CppEmitter::Scope scope (emitter);
1230
1150
raw_indented_ostream &os = emitter.ostream ();
1231
- Operation *operation = functionOp.getOperation ();
1232
- if (emitter.shouldPrintClass ()) {
1233
- if (functionOp.isExternal ()) {
1234
- // TODO: Determine the best long-term strategy for external functions.
1235
- // Currently, we're skipping over this functionOp.
1236
- // We have considered using emitWarning() which would return
1237
- // InFlightDiagnostic which seems can be automatically converted to
1238
- // LogicalResult since this is done in emitAttributes where emitError is
1239
- // converted to LogicalResult. However, it requires that we pass in a
1240
- // location which at first glance we don't have in this scope. Open to
1241
- // further discussion on this.
1242
- os << " Warning: Cannot process external function '"
1243
- << functionOp.getName () << " '. "
1244
- << " This functionOp lacks a body so we will skip over it." ;
1245
- return success ();
1246
- }
1247
- os << " class " << emitter.getClassName () << " final {\n " ;
1248
- os << " public: \n " ;
1249
- os.indent ();
1250
-
1251
- if (failed (emitClassFields (emitter, functionOp)))
1252
- return failure ();
1253
- }
1254
-
1255
1151
if (functionOp.getSpecifiers ()) {
1256
1152
for (Attribute specifier : functionOp.getSpecifiersAttr ()) {
1257
1153
os << cast<StringAttr>(specifier).str () << " " ;
@@ -1261,37 +1157,23 @@ static LogicalResult printOperation(CppEmitter &emitter,
1261
1157
if (failed (emitter.emitTypes (functionOp.getLoc (),
1262
1158
functionOp.getFunctionType ().getResults ())))
1263
1159
return failure ();
1264
- // TODO: We may wanna consider having the name of the function be execute in
1265
- // the case that we want to emit a class instead of main. Leaving as is for
1266
- // now to make the change smaller.
1267
1160
os << " " << functionOp.getName ();
1268
1161
1269
1162
os << " (" ;
1270
-
1271
- if (!emitter.shouldPrintClass ()) {
1272
- if (functionOp.isExternal ()) {
1273
- if (failed (printFunctionArgs (emitter, operation,
1274
- functionOp.getArgumentTypes ())))
1275
- return failure ();
1276
- os << " );" ;
1277
- return success ();
1278
- }
1279
- if (failed (
1280
- printFunctionArgs (emitter, operation, functionOp.getArguments ())))
1163
+ Operation *operation = functionOp.getOperation ();
1164
+ if (functionOp.isExternal ()) {
1165
+ if (failed (printFunctionArgs (emitter, operation,
1166
+ functionOp.getArgumentTypes ())))
1281
1167
return failure ();
1168
+ os << " );" ;
1169
+ return success ();
1282
1170
}
1171
+ if (failed (printFunctionArgs (emitter, operation, functionOp.getArguments ())))
1172
+ return failure ();
1283
1173
os << " ) {\n " ;
1284
-
1285
1174
if (failed (printFunctionBody (emitter, operation, functionOp.getBlocks ())))
1286
1175
return failure ();
1287
-
1288
- if (emitter.shouldPrintClass ()) {
1289
- os << " }\n " ;
1290
- os.unindent ();
1291
- os << " };\n " ;
1292
- } else {
1293
- os << " }\n " ;
1294
- }
1176
+ os << " }\n " ;
1295
1177
1296
1178
return success ();
1297
1179
}
@@ -1328,11 +1210,9 @@ static LogicalResult printOperation(CppEmitter &emitter,
1328
1210
}
1329
1211
1330
1212
CppEmitter::CppEmitter (raw_ostream &os, bool declareVariablesAtTop,
1331
- StringRef fileId, bool emitClass, StringRef className,
1332
- StringRef fieldNameAttribute)
1213
+ StringRef fileId)
1333
1214
: os(os), declareVariablesAtTop(declareVariablesAtTop),
1334
- fileId(fileId.str()), emitClass(emitClass), className(className.str()),
1335
- fieldNameAttribute(fieldNameAttribute.str()) {
1215
+ fileId(fileId.str()) {
1336
1216
valueInScopeCount.push (0 );
1337
1217
labelInScopeCount.push (0 );
1338
1218
}
@@ -1915,10 +1795,7 @@ LogicalResult CppEmitter::emitTupleType(Location loc, ArrayRef<Type> types) {
1915
1795
1916
1796
LogicalResult emitc::translateToCpp (Operation *op, raw_ostream &os,
1917
1797
bool declareVariablesAtTop,
1918
- StringRef fileId, bool emitClass,
1919
- StringRef className,
1920
- StringRef fieldNameAttribute) {
1921
- CppEmitter emitter (os, declareVariablesAtTop, fileId, emitClass, className,
1922
- fieldNameAttribute);
1798
+ StringRef fileId) {
1799
+ CppEmitter emitter (os, declareVariablesAtTop, fileId);
1923
1800
return emitter.emitOperation (*op, /* trailingSemicolon=*/ false );
1924
- }
1801
+ }
0 commit comments