@@ -186,10 +186,6 @@ struct CppEmitter {
186
186
// / Return the existing or a new name for a Value.
187
187
StringRef getOrCreateName (Value val);
188
188
189
- // / Return the existing or a new name for a loop induction variable of an
190
- // / emitc::ForOp.
191
- StringRef getOrCreateInductionVarName (Value val);
192
-
193
189
// Returns the textual representation of a subscript operation.
194
190
std::string getSubscriptName (emitc::SubscriptOp op);
195
191
@@ -205,39 +201,23 @@ struct CppEmitter {
205
201
// / Whether to map an mlir integer to a unsigned integer in C++.
206
202
bool shouldMapToUnsigned (IntegerType::SignednessSemantics val);
207
203
208
- // / Abstract RAII helper function to manage entering/exiting C++ scopes.
204
+ // / RAII helper function to manage entering/exiting C++ scopes.
209
205
struct Scope {
210
- ~Scope () { emitter.labelInScopeCount .pop (); }
211
-
212
- private:
213
- llvm::ScopedHashTableScope<Value, std::string> valueMapperScope;
214
- llvm::ScopedHashTableScope<Block *, std::string> blockMapperScope;
215
-
216
- protected:
217
206
Scope (CppEmitter &emitter)
218
207
: valueMapperScope(emitter.valueMapper),
219
208
blockMapperScope (emitter.blockMapper), emitter(emitter) {
209
+ emitter.valueInScopeCount .push (emitter.valueInScopeCount .top ());
220
210
emitter.labelInScopeCount .push (emitter.labelInScopeCount .top ());
221
211
}
222
- CppEmitter &emitter;
223
- };
224
-
225
- // / RAII helper function to manage entering/exiting functions, while re-using
226
- // / value names.
227
- struct FunctionScope : Scope {
228
- FunctionScope (CppEmitter &emitter) : Scope(emitter) {
229
- // Re-use value names.
230
- emitter.resetValueCounter ();
212
+ ~Scope () {
213
+ emitter.valueInScopeCount .pop ();
214
+ emitter.labelInScopeCount .pop ();
231
215
}
232
- };
233
216
234
- // / RAII helper function to manage entering/exiting emitc::forOp loops and
235
- // / handle induction variable naming.
236
- struct LoopScope : Scope {
237
- LoopScope (CppEmitter &emitter) : Scope(emitter) {
238
- emitter.increaseLoopNestingLevel ();
239
- }
240
- ~LoopScope () { emitter.decreaseLoopNestingLevel (); }
217
+ private:
218
+ llvm::ScopedHashTableScope<Value, std::string> valueMapperScope;
219
+ llvm::ScopedHashTableScope<Block *, std::string> blockMapperScope;
220
+ CppEmitter &emitter;
241
221
};
242
222
243
223
// / Returns wether the Value is assigned to a C++ variable in the scope.
@@ -273,15 +253,6 @@ struct CppEmitter {
273
253
return operandExpression == emittedExpression;
274
254
};
275
255
276
- // Resets the value counter to 0.
277
- void resetValueCounter ();
278
-
279
- // Increases the loop nesting level by 1.
280
- void increaseLoopNestingLevel ();
281
-
282
- // Decreases the loop nesting level by 1.
283
- void decreaseLoopNestingLevel ();
284
-
285
256
private:
286
257
using ValueMapper = llvm::ScopedHashTable<Value, std::string>;
287
258
using BlockMapper = llvm::ScopedHashTable<Block *, std::string>;
@@ -303,19 +274,11 @@ struct CppEmitter {
303
274
// / Map from block to name of C++ label.
304
275
BlockMapper blockMapper;
305
276
306
- // / Default values representing outermost scope.
307
- llvm::ScopedHashTableScope<Value, std::string> defaultValueMapperScope;
308
- llvm::ScopedHashTableScope<Block *, std::string> defaultBlockMapperScope;
309
-
277
+ // / The number of values in the current scope. This is used to declare the
278
+ // / names of values in a scope.
279
+ std::stack<int64_t > valueInScopeCount;
310
280
std::stack<int64_t > labelInScopeCount;
311
281
312
- // / Keeps track of the amount of nested loops the emitter currently operates
313
- // / in.
314
- uint64_t loopNestingLevel{0 };
315
-
316
- // / Emitter-level count of created values to enable unique identifiers.
317
- unsigned int valueCount{0 };
318
-
319
282
// / State of the current expression being emitted.
320
283
ExpressionOp emittedExpression;
321
284
SmallVector<int > emittedExpressionPrecedence;
@@ -897,6 +860,7 @@ static LogicalResult printOperation(CppEmitter &emitter,
897
860
}
898
861
899
862
static LogicalResult printOperation (CppEmitter &emitter, emitc::ForOp forOp) {
863
+
900
864
raw_indented_ostream &os = emitter.ostream ();
901
865
902
866
// Utility function to determine whether a value is an expression that will be
@@ -915,12 +879,12 @@ static LogicalResult printOperation(CppEmitter &emitter, emitc::ForOp forOp) {
915
879
emitter.emitType (forOp.getLoc (), forOp.getInductionVar ().getType ())))
916
880
return failure ();
917
881
os << " " ;
918
- os << emitter.getOrCreateInductionVarName (forOp.getInductionVar ());
882
+ os << emitter.getOrCreateName (forOp.getInductionVar ());
919
883
os << " = " ;
920
884
if (failed (emitter.emitOperand (forOp.getLowerBound ())))
921
885
return failure ();
922
886
os << " ; " ;
923
- os << emitter.getOrCreateInductionVarName (forOp.getInductionVar ());
887
+ os << emitter.getOrCreateName (forOp.getInductionVar ());
924
888
os << " < " ;
925
889
Value upperBound = forOp.getUpperBound ();
926
890
bool upperBoundRequiresParentheses = requiresParentheses (upperBound);
@@ -931,15 +895,13 @@ static LogicalResult printOperation(CppEmitter &emitter, emitc::ForOp forOp) {
931
895
if (upperBoundRequiresParentheses)
932
896
os << " )" ;
933
897
os << " ; " ;
934
- os << emitter.getOrCreateInductionVarName (forOp.getInductionVar ());
898
+ os << emitter.getOrCreateName (forOp.getInductionVar ());
935
899
os << " += " ;
936
900
if (failed (emitter.emitOperand (forOp.getStep ())))
937
901
return failure ();
938
902
os << " ) {\n " ;
939
903
os.indent ();
940
904
941
- CppEmitter::LoopScope lScope (emitter);
942
-
943
905
Region &forRegion = forOp.getRegion ();
944
906
auto regionOps = forRegion.getOps ();
945
907
@@ -1026,6 +988,8 @@ static LogicalResult printOperation(CppEmitter &emitter,
1026
988
}
1027
989
1028
990
static LogicalResult printOperation (CppEmitter &emitter, ModuleOp moduleOp) {
991
+ CppEmitter::Scope scope (emitter);
992
+
1029
993
for (Operation &op : moduleOp) {
1030
994
if (failed (emitter.emitOperation (op, /* trailingSemicolon=*/ false )))
1031
995
return failure ();
@@ -1034,6 +998,7 @@ static LogicalResult printOperation(CppEmitter &emitter, ModuleOp moduleOp) {
1034
998
}
1035
999
1036
1000
static LogicalResult printOperation (CppEmitter &emitter, ClassOp classOp) {
1001
+ CppEmitter::Scope classScope (emitter);
1037
1002
raw_indented_ostream &os = emitter.ostream ();
1038
1003
os << " class " << classOp.getSymName ();
1039
1004
if (classOp.getFinalSpecifier ())
@@ -1079,6 +1044,8 @@ static LogicalResult printOperation(CppEmitter &emitter, FileOp file) {
1079
1044
if (!emitter.shouldEmitFile (file))
1080
1045
return success ();
1081
1046
1047
+ CppEmitter::Scope scope (emitter);
1048
+
1082
1049
for (Operation &op : file) {
1083
1050
if (failed (emitter.emitOperation (op, /* trailingSemicolon=*/ false )))
1084
1051
return failure ();
@@ -1194,7 +1161,7 @@ static LogicalResult printOperation(CppEmitter &emitter,
1194
1161
return functionOp.emitOpError () << " cannot emit array type as result type" ;
1195
1162
}
1196
1163
1197
- CppEmitter::FunctionScope scope (emitter);
1164
+ CppEmitter::Scope scope (emitter);
1198
1165
raw_indented_ostream &os = emitter.ostream ();
1199
1166
if (failed (emitter.emitTypes (functionOp.getLoc (),
1200
1167
functionOp.getFunctionType ().getResults ())))
@@ -1222,7 +1189,7 @@ static LogicalResult printOperation(CppEmitter &emitter,
1222
1189
" with multiple blocks needs variables declared at top" );
1223
1190
}
1224
1191
1225
- CppEmitter::FunctionScope scope (emitter);
1192
+ CppEmitter::Scope scope (emitter);
1226
1193
raw_indented_ostream &os = emitter.ostream ();
1227
1194
if (functionOp.getSpecifiers ()) {
1228
1195
for (Attribute specifier : functionOp.getSpecifiersAttr ()) {
@@ -1256,6 +1223,7 @@ static LogicalResult printOperation(CppEmitter &emitter,
1256
1223
1257
1224
static LogicalResult printOperation (CppEmitter &emitter,
1258
1225
DeclareFuncOp declareFuncOp) {
1226
+ CppEmitter::Scope scope (emitter);
1259
1227
raw_indented_ostream &os = emitter.ostream ();
1260
1228
1261
1229
auto functionOp = SymbolTable::lookupNearestSymbolFrom<emitc::FuncOp>(
@@ -1287,8 +1255,8 @@ static LogicalResult printOperation(CppEmitter &emitter,
1287
1255
CppEmitter::CppEmitter (raw_ostream &os, bool declareVariablesAtTop,
1288
1256
StringRef fileId)
1289
1257
: os(os), declareVariablesAtTop(declareVariablesAtTop),
1290
- fileId (fileId.str()), defaultValueMapperScope(valueMapper),
1291
- defaultBlockMapperScope(blockMapper) {
1258
+ fileId(fileId.str()) {
1259
+ valueInScopeCount. push ( 0 );
1292
1260
labelInScopeCount.push (0 );
1293
1261
}
1294
1262
@@ -1329,26 +1297,7 @@ StringRef CppEmitter::getOrCreateName(Value val) {
1329
1297
assert (!hasDeferredEmission (val.getDefiningOp ()) &&
1330
1298
" cacheDeferredOpResult should have been called on this value, "
1331
1299
" update the emitOperation function." );
1332
-
1333
- valueMapper.insert (val, formatv (" v{0}" , ++valueCount));
1334
- }
1335
- return *valueMapper.begin (val);
1336
- }
1337
-
1338
- // / Return the existing or a new name for a loop induction variable Value.
1339
- // / Loop induction variables follow natural naming: i, j, k, ..., t, uX.
1340
- StringRef CppEmitter::getOrCreateInductionVarName (Value val) {
1341
- if (!valueMapper.count (val)) {
1342
-
1343
- int64_t identifier = ' i' + loopNestingLevel;
1344
-
1345
- if (identifier >= ' i' && identifier <= ' t' ) {
1346
- valueMapper.insert (val,
1347
- formatv (" {0}{1}" , (char )identifier, ++valueCount));
1348
- } else {
1349
- // If running out of letters, continue with uX.
1350
- valueMapper.insert (val, formatv (" u{0}" , ++valueCount));
1351
- }
1300
+ valueMapper.insert (val, formatv (" v{0}" , ++valueInScopeCount.top ()));
1352
1301
}
1353
1302
return *valueMapper.begin (val);
1354
1303
}
@@ -1889,12 +1838,6 @@ LogicalResult CppEmitter::emitTupleType(Location loc, ArrayRef<Type> types) {
1889
1838
return success ();
1890
1839
}
1891
1840
1892
- void CppEmitter::resetValueCounter () { valueCount = 0 ; }
1893
-
1894
- void CppEmitter::increaseLoopNestingLevel () { loopNestingLevel++; }
1895
-
1896
- void CppEmitter::decreaseLoopNestingLevel () { loopNestingLevel--; }
1897
-
1898
1841
LogicalResult emitc::translateToCpp (Operation *op, raw_ostream &os,
1899
1842
bool declareVariablesAtTop,
1900
1843
StringRef fileId) {
0 commit comments