Skip to content

Commit 070e712

Browse files
committed
Revert "[MLIR][Target/Cpp] Natural induction variable naming. (llvm#136102)"
This reverts commit dcc692a.
1 parent 0f391d6 commit 070e712

File tree

2 files changed

+27
-157
lines changed

2 files changed

+27
-157
lines changed

mlir/lib/Target/Cpp/TranslateToCpp.cpp

Lines changed: 27 additions & 84 deletions
Original file line numberDiff line numberDiff line change
@@ -186,10 +186,6 @@ struct CppEmitter {
186186
/// Return the existing or a new name for a Value.
187187
StringRef getOrCreateName(Value val);
188188

189-
/// Return the existing or a new name for a loop induction variable of an
190-
/// emitc::ForOp.
191-
StringRef getOrCreateInductionVarName(Value val);
192-
193189
// Returns the textual representation of a subscript operation.
194190
std::string getSubscriptName(emitc::SubscriptOp op);
195191

@@ -205,39 +201,23 @@ struct CppEmitter {
205201
/// Whether to map an mlir integer to a unsigned integer in C++.
206202
bool shouldMapToUnsigned(IntegerType::SignednessSemantics val);
207203

208-
/// Abstract RAII helper function to manage entering/exiting C++ scopes.
204+
/// RAII helper function to manage entering/exiting C++ scopes.
209205
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:
217206
Scope(CppEmitter &emitter)
218207
: valueMapperScope(emitter.valueMapper),
219208
blockMapperScope(emitter.blockMapper), emitter(emitter) {
209+
emitter.valueInScopeCount.push(emitter.valueInScopeCount.top());
220210
emitter.labelInScopeCount.push(emitter.labelInScopeCount.top());
221211
}
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();
231215
}
232-
};
233216

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;
241221
};
242222

243223
/// Returns wether the Value is assigned to a C++ variable in the scope.
@@ -273,15 +253,6 @@ struct CppEmitter {
273253
return operandExpression == emittedExpression;
274254
};
275255

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-
285256
private:
286257
using ValueMapper = llvm::ScopedHashTable<Value, std::string>;
287258
using BlockMapper = llvm::ScopedHashTable<Block *, std::string>;
@@ -303,19 +274,11 @@ struct CppEmitter {
303274
/// Map from block to name of C++ label.
304275
BlockMapper blockMapper;
305276

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;
310280
std::stack<int64_t> labelInScopeCount;
311281

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-
319282
/// State of the current expression being emitted.
320283
ExpressionOp emittedExpression;
321284
SmallVector<int> emittedExpressionPrecedence;
@@ -897,6 +860,7 @@ static LogicalResult printOperation(CppEmitter &emitter,
897860
}
898861

899862
static LogicalResult printOperation(CppEmitter &emitter, emitc::ForOp forOp) {
863+
900864
raw_indented_ostream &os = emitter.ostream();
901865

902866
// 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) {
915879
emitter.emitType(forOp.getLoc(), forOp.getInductionVar().getType())))
916880
return failure();
917881
os << " ";
918-
os << emitter.getOrCreateInductionVarName(forOp.getInductionVar());
882+
os << emitter.getOrCreateName(forOp.getInductionVar());
919883
os << " = ";
920884
if (failed(emitter.emitOperand(forOp.getLowerBound())))
921885
return failure();
922886
os << "; ";
923-
os << emitter.getOrCreateInductionVarName(forOp.getInductionVar());
887+
os << emitter.getOrCreateName(forOp.getInductionVar());
924888
os << " < ";
925889
Value upperBound = forOp.getUpperBound();
926890
bool upperBoundRequiresParentheses = requiresParentheses(upperBound);
@@ -931,15 +895,13 @@ static LogicalResult printOperation(CppEmitter &emitter, emitc::ForOp forOp) {
931895
if (upperBoundRequiresParentheses)
932896
os << ")";
933897
os << "; ";
934-
os << emitter.getOrCreateInductionVarName(forOp.getInductionVar());
898+
os << emitter.getOrCreateName(forOp.getInductionVar());
935899
os << " += ";
936900
if (failed(emitter.emitOperand(forOp.getStep())))
937901
return failure();
938902
os << ") {\n";
939903
os.indent();
940904

941-
CppEmitter::LoopScope lScope(emitter);
942-
943905
Region &forRegion = forOp.getRegion();
944906
auto regionOps = forRegion.getOps();
945907

@@ -1026,6 +988,8 @@ static LogicalResult printOperation(CppEmitter &emitter,
1026988
}
1027989

1028990
static LogicalResult printOperation(CppEmitter &emitter, ModuleOp moduleOp) {
991+
CppEmitter::Scope scope(emitter);
992+
1029993
for (Operation &op : moduleOp) {
1030994
if (failed(emitter.emitOperation(op, /*trailingSemicolon=*/false)))
1031995
return failure();
@@ -1034,6 +998,7 @@ static LogicalResult printOperation(CppEmitter &emitter, ModuleOp moduleOp) {
1034998
}
1035999

10361000
static LogicalResult printOperation(CppEmitter &emitter, ClassOp classOp) {
1001+
CppEmitter::Scope classScope(emitter);
10371002
raw_indented_ostream &os = emitter.ostream();
10381003
os << "class " << classOp.getSymName();
10391004
if (classOp.getFinalSpecifier())
@@ -1079,6 +1044,8 @@ static LogicalResult printOperation(CppEmitter &emitter, FileOp file) {
10791044
if (!emitter.shouldEmitFile(file))
10801045
return success();
10811046

1047+
CppEmitter::Scope scope(emitter);
1048+
10821049
for (Operation &op : file) {
10831050
if (failed(emitter.emitOperation(op, /*trailingSemicolon=*/false)))
10841051
return failure();
@@ -1194,7 +1161,7 @@ static LogicalResult printOperation(CppEmitter &emitter,
11941161
return functionOp.emitOpError() << "cannot emit array type as result type";
11951162
}
11961163

1197-
CppEmitter::FunctionScope scope(emitter);
1164+
CppEmitter::Scope scope(emitter);
11981165
raw_indented_ostream &os = emitter.ostream();
11991166
if (failed(emitter.emitTypes(functionOp.getLoc(),
12001167
functionOp.getFunctionType().getResults())))
@@ -1222,7 +1189,7 @@ static LogicalResult printOperation(CppEmitter &emitter,
12221189
"with multiple blocks needs variables declared at top");
12231190
}
12241191

1225-
CppEmitter::FunctionScope scope(emitter);
1192+
CppEmitter::Scope scope(emitter);
12261193
raw_indented_ostream &os = emitter.ostream();
12271194
if (functionOp.getSpecifiers()) {
12281195
for (Attribute specifier : functionOp.getSpecifiersAttr()) {
@@ -1256,6 +1223,7 @@ static LogicalResult printOperation(CppEmitter &emitter,
12561223

12571224
static LogicalResult printOperation(CppEmitter &emitter,
12581225
DeclareFuncOp declareFuncOp) {
1226+
CppEmitter::Scope scope(emitter);
12591227
raw_indented_ostream &os = emitter.ostream();
12601228

12611229
auto functionOp = SymbolTable::lookupNearestSymbolFrom<emitc::FuncOp>(
@@ -1287,8 +1255,8 @@ static LogicalResult printOperation(CppEmitter &emitter,
12871255
CppEmitter::CppEmitter(raw_ostream &os, bool declareVariablesAtTop,
12881256
StringRef fileId)
12891257
: os(os), declareVariablesAtTop(declareVariablesAtTop),
1290-
fileId(fileId.str()), defaultValueMapperScope(valueMapper),
1291-
defaultBlockMapperScope(blockMapper) {
1258+
fileId(fileId.str()) {
1259+
valueInScopeCount.push(0);
12921260
labelInScopeCount.push(0);
12931261
}
12941262

@@ -1329,26 +1297,7 @@ StringRef CppEmitter::getOrCreateName(Value val) {
13291297
assert(!hasDeferredEmission(val.getDefiningOp()) &&
13301298
"cacheDeferredOpResult should have been called on this value, "
13311299
"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()));
13521301
}
13531302
return *valueMapper.begin(val);
13541303
}
@@ -1889,12 +1838,6 @@ LogicalResult CppEmitter::emitTupleType(Location loc, ArrayRef<Type> types) {
18891838
return success();
18901839
}
18911840

1892-
void CppEmitter::resetValueCounter() { valueCount = 0; }
1893-
1894-
void CppEmitter::increaseLoopNestingLevel() { loopNestingLevel++; }
1895-
1896-
void CppEmitter::decreaseLoopNestingLevel() { loopNestingLevel--; }
1897-
18981841
LogicalResult emitc::translateToCpp(Operation *op, raw_ostream &os,
18991842
bool declareVariablesAtTop,
19001843
StringRef fileId) {

mlir/test/Target/Cpp/for_loop_induction_vars.mlir

Lines changed: 0 additions & 73 deletions
This file was deleted.

0 commit comments

Comments
 (0)