Skip to content

Commit f3f4919

Browse files
authored
Readability: Add option to emit constants values in place
* Readability: Add option to emit constants values in place, instead of producing dedicated variables.
1 parent e25d207 commit f3f4919

File tree

4 files changed

+71
-10
lines changed

4 files changed

+71
-10
lines changed

mlir/include/mlir/Target/Cpp/CppEmitter.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,8 @@ namespace emitc {
2626
/// arguments are declared at the beginning of the function.
2727
LogicalResult translateToCpp(Operation *op, raw_ostream &os,
2828
bool declareVariablesAtTop = false,
29-
StringRef onlyTu = "");
29+
StringRef onlyTu = "",
30+
bool constantsAsVariables = true);
3031
} // namespace emitc
3132
} // namespace mlir
3233

mlir/lib/Target/Cpp/TranslateRegistration.cpp

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,13 +34,19 @@ void registerToCppTranslation() {
3434
llvm::cl::desc("Only emit the translation unit with the matching id"),
3535
llvm::cl::init(""));
3636

37+
static llvm::cl::opt<bool> constantsAsVariables(
38+
"constants-as-variables",
39+
llvm::cl::desc("Use variables to hold the constant values"),
40+
llvm::cl::init(true));
41+
3742
TranslateFromMLIRRegistration reg(
3843
"mlir-to-cpp", "translate from mlir to cpp",
3944
[](Operation *op, raw_ostream &output) {
4045
return emitc::translateToCpp(
4146
op, output,
4247
/*declareVariablesAtTop=*/declareVariablesAtTop,
43-
/*onlyTu=*/onlyTu);
48+
/*onlyTu=*/onlyTu,
49+
/*constantsAsVariables=*/constantsAsVariables);
4450
},
4551
[](DialectRegistry &registry) {
4652
// clang-format off

mlir/lib/Target/Cpp/TranslateToCpp.cpp

Lines changed: 43 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,7 @@ namespace {
116116
/// Emitter that uses dialect specific emitters to emit C++ code.
117117
struct CppEmitter {
118118
explicit CppEmitter(raw_ostream &os, bool declareVariablesAtTop,
119-
StringRef onlyTu);
119+
StringRef onlyTu, bool constantsAsVariables);
120120

121121
/// Emits attribute or returns failure.
122122
LogicalResult emitAttribute(Location loc, Attribute attr);
@@ -235,6 +235,10 @@ struct CppEmitter {
235235
/// Returns whether this translation unit should be emitted
236236
bool shouldEmitTu(TranslationUnitOp tu) { return tu.getId() == onlyTu; }
237237

238+
/// Returns whether the value of ConstantOps should be stored in variables
239+
/// or emmited directly in their usage locations.
240+
bool shouldUseConstantsAsVariables() { return constantsAsVariables; }
241+
238242
/// Get expression currently being emitted.
239243
ExpressionOp getEmittedExpression() { return emittedExpression; }
240244

@@ -265,6 +269,9 @@ struct CppEmitter {
265269
/// Only emit translation units whos id matches this value.
266270
std::string onlyTu;
267271

272+
/// Use variables to hold the constant values
273+
bool constantsAsVariables;
274+
268275
/// Map from value to name of C++ variable that contain the name.
269276
ValueMapper valueMapper;
270277

@@ -365,6 +372,10 @@ static LogicalResult printConstantOp(CppEmitter &emitter, Operation *operation,
365372

366373
static LogicalResult printOperation(CppEmitter &emitter,
367374
emitc::ConstantOp constantOp) {
375+
if (!emitter.shouldUseConstantsAsVariables()) {
376+
return success();
377+
}
378+
368379
Operation *operation = constantOp.getOperation();
369380
Attribute value = constantOp.getValue();
370381

@@ -1218,9 +1229,9 @@ static LogicalResult printOperation(CppEmitter &emitter,
12181229
}
12191230

12201231
CppEmitter::CppEmitter(raw_ostream &os, bool declareVariablesAtTop,
1221-
StringRef onlyTu)
1232+
StringRef onlyTu, bool constantsAsVariables)
12221233
: os(os), declareVariablesAtTop(declareVariablesAtTop),
1223-
onlyTu(onlyTu.str()) {
1234+
onlyTu(onlyTu.str()), constantsAsVariables(constantsAsVariables) {
12241235
valueInScopeCount.push(0);
12251236
labelInScopeCount.push(0);
12261237
}
@@ -1425,8 +1436,25 @@ LogicalResult CppEmitter::emitExpression(ExpressionOp expressionOp) {
14251436
}
14261437

14271438
LogicalResult CppEmitter::emitOperand(Value value) {
1439+
Operation *def = value.getDefiningOp();
1440+
if (!shouldUseConstantsAsVariables()) {
1441+
if (auto constant = dyn_cast_if_present<ConstantOp>(def)) {
1442+
os << "((";
1443+
1444+
if (failed(emitType(constant.getLoc(), constant.getType()))) {
1445+
return failure();
1446+
}
1447+
os << ") ";
1448+
1449+
if (failed(emitAttribute(constant.getLoc(), constant.getValue()))) {
1450+
return failure();
1451+
}
1452+
os << ")";
1453+
return success();
1454+
}
1455+
}
1456+
14281457
if (isPartOfCurrentExpression(value)) {
1429-
Operation *def = value.getDefiningOp();
14301458
assert(def && "Expected operand to be defined by an operation");
14311459
FailureOr<int> precedence = getOperatorPrecedence(def);
14321460
if (failed(precedence))
@@ -1452,7 +1480,7 @@ LogicalResult CppEmitter::emitOperand(Value value) {
14521480
return success();
14531481
}
14541482

1455-
auto expressionOp = dyn_cast_if_present<ExpressionOp>(value.getDefiningOp());
1483+
auto expressionOp = dyn_cast_if_present<ExpressionOp>(def);
14561484
if (expressionOp && shouldBeInlined(expressionOp))
14571485
return emitExpression(expressionOp);
14581486

@@ -1671,7 +1699,13 @@ LogicalResult CppEmitter::emitOperation(Operation &op, bool trailingSemicolon) {
16711699
trailingSemicolon = false;
16721700
}
16731701

1674-
os << (trailingSemicolon ? ";\n" : "\n");
1702+
bool trailingNewline = true;
1703+
if (!shouldUseConstantsAsVariables() && isa<emitc::ConstantOp>(op)) {
1704+
trailingSemicolon = false;
1705+
trailingNewline = false;
1706+
}
1707+
1708+
os << (trailingSemicolon ? ";" : "") << (trailingNewline ? "\n" : "");
16751709

16761710
return success();
16771711
}
@@ -1837,7 +1871,8 @@ LogicalResult CppEmitter::emitTupleType(Location loc, ArrayRef<Type> types) {
18371871

18381872
LogicalResult emitc::translateToCpp(Operation *op, raw_ostream &os,
18391873
bool declareVariablesAtTop,
1840-
StringRef onlyTu) {
1841-
CppEmitter emitter(os, declareVariablesAtTop, onlyTu);
1874+
StringRef onlyTu,
1875+
bool constantsAsVariables) {
1876+
CppEmitter emitter(os, declareVariablesAtTop, onlyTu, constantsAsVariables);
18421877
return emitter.emitOperation(*op, /*trailingSemicolon=*/false);
18431878
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
// RUN: mlir-translate -mlir-to-cpp -constants-as-variables=false %s | FileCheck %s -check-prefix=CPP-DEFAULT
2+
3+
func.func @test() {
4+
%start = "emitc.constant"() <{value = 0 : index}> : () -> !emitc.size_t
5+
%stop = "emitc.constant"() <{value = 10 : index}> : () -> !emitc.size_t
6+
%step = "emitc.constant"() <{value = 1 : index}> : () -> !emitc.size_t
7+
8+
emitc.for %iter = %start to %stop step %step {
9+
emitc.yield
10+
}
11+
12+
return
13+
}
14+
15+
// CPP-DEFAULT: void test() {
16+
// CPP-DEFAULT-NEXT: for (size_t v1 = ((size_t) 0); v1 < ((size_t) 10); v1 += ((size_t) 1)) {
17+
// CPP-DEFAULT-NEXT: }
18+
// CPP-DEFAULT-NEXT: return;
19+
// CPP-DEFAULT-NEXT: }

0 commit comments

Comments
 (0)