Skip to content

Commit

Permalink
Change name of option and flip the default value
Browse files Browse the repository at this point in the history
  • Loading branch information
lmendesp-amd committed Nov 28, 2024
1 parent 62bf9fa commit 5ca66df
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 22 deletions.
2 changes: 1 addition & 1 deletion mlir/include/mlir/Target/Cpp/CppEmitter.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ namespace emitc {
LogicalResult translateToCpp(Operation *op, raw_ostream &os,
bool declareVariablesAtTop = false,
StringRef onlyTu = "",
bool propagateConstants = false);
bool constantsAsVariables = true);
} // namespace emitc
} // namespace mlir

Expand Down
11 changes: 5 additions & 6 deletions mlir/lib/Target/Cpp/TranslateRegistration.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,11 +34,10 @@ void registerToCppTranslation() {
llvm::cl::desc("Only emit the translation unit with the matching id"),
llvm::cl::init(""));

static llvm::cl::opt<bool> propagateConstants(
"propagate-constants",
llvm::cl::desc("Emit constants in their usage location instead of "
"declaring variables"),
llvm::cl::init(false));
static llvm::cl::opt<bool> constantsAsVariables(
"constants-as-variables",
llvm::cl::desc("Use variables to hold the constant values"),
llvm::cl::init(true));

TranslateFromMLIRRegistration reg(
"mlir-to-cpp", "translate from mlir to cpp",
Expand All @@ -47,7 +46,7 @@ void registerToCppTranslation() {
op, output,
/*declareVariablesAtTop=*/declareVariablesAtTop,
/*onlyTu=*/onlyTu,
/*propagateConstants=*/propagateConstants);
/*constantsAsVariables=*/constantsAsVariables);
},
[](DialectRegistry &registry) {
// clang-format off
Expand Down
27 changes: 14 additions & 13 deletions mlir/lib/Target/Cpp/TranslateToCpp.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ namespace {
/// Emitter that uses dialect specific emitters to emit C++ code.
struct CppEmitter {
explicit CppEmitter(raw_ostream &os, bool declareVariablesAtTop,
StringRef onlyTu, bool propagateConstants);
StringRef onlyTu, bool constantsAsVariables);

/// Emits attribute or returns failure.
LogicalResult emitAttribute(Location loc, Attribute attr);
Expand Down Expand Up @@ -235,9 +235,9 @@ struct CppEmitter {
/// Returns whether this translation unit should be emitted
bool shouldEmitTu(TranslationUnitOp tu) { return tu.getId() == onlyTu; }

/// Returns whether the value of ConstantOps should be emmited directly in
/// their usage locations instead of holding them in dedicated variables.
bool shouldPropagateConstants() { return propagateConstants; }
/// Returns whether the value of ConstantOps should be stored in variables
/// or emmited directly in their usage locations.
bool shouldUseConstantsAsVariables() { return constantsAsVariables; }

/// Get expression currently being emitted.
ExpressionOp getEmittedExpression() { return emittedExpression; }
Expand Down Expand Up @@ -269,8 +269,8 @@ struct CppEmitter {
/// Only emit translation units whos id matches this value.
std::string onlyTu;

/// Emit constants in their usage location instead of declaring variables
bool propagateConstants;
/// Use variables to hold the constant values
bool constantsAsVariables;

/// Map from value to name of C++ variable that contain the name.
ValueMapper valueMapper;
Expand Down Expand Up @@ -372,7 +372,7 @@ static LogicalResult printConstantOp(CppEmitter &emitter, Operation *operation,

static LogicalResult printOperation(CppEmitter &emitter,
emitc::ConstantOp constantOp) {
if (emitter.shouldPropagateConstants()) {
if (!emitter.shouldUseConstantsAsVariables()) {
return success();
}

Expand Down Expand Up @@ -1229,9 +1229,9 @@ static LogicalResult printOperation(CppEmitter &emitter,
}

CppEmitter::CppEmitter(raw_ostream &os, bool declareVariablesAtTop,
StringRef onlyTu, bool propagateConstants)
StringRef onlyTu, bool constantsAsVariables)
: os(os), declareVariablesAtTop(declareVariablesAtTop),
onlyTu(onlyTu.str()), propagateConstants(propagateConstants) {
onlyTu(onlyTu.str()), constantsAsVariables(constantsAsVariables) {
valueInScopeCount.push(0);
labelInScopeCount.push(0);
}
Expand Down Expand Up @@ -1437,7 +1437,7 @@ LogicalResult CppEmitter::emitExpression(ExpressionOp expressionOp) {

LogicalResult CppEmitter::emitOperand(Value value) {
Operation *def = value.getDefiningOp();
if (shouldPropagateConstants()) {
if (!shouldUseConstantsAsVariables()) {
if (auto constant = dyn_cast_if_present<ConstantOp>(def)) {
os << "((";

Expand Down Expand Up @@ -1700,7 +1700,7 @@ LogicalResult CppEmitter::emitOperation(Operation &op, bool trailingSemicolon) {
}

bool trailingNewline = true;
if (shouldPropagateConstants() && isa<emitc::ConstantOp>(op)) {
if (!shouldUseConstantsAsVariables() && isa<emitc::ConstantOp>(op)) {
trailingSemicolon = false;
trailingNewline = false;
}
Expand Down Expand Up @@ -1871,7 +1871,8 @@ LogicalResult CppEmitter::emitTupleType(Location loc, ArrayRef<Type> types) {

LogicalResult emitc::translateToCpp(Operation *op, raw_ostream &os,
bool declareVariablesAtTop,
StringRef onlyTu, bool propagateConstants) {
CppEmitter emitter(os, declareVariablesAtTop, onlyTu, propagateConstants);
StringRef onlyTu,
bool constantsAsVariables) {
CppEmitter emitter(os, declareVariablesAtTop, onlyTu, constantsAsVariables);
return emitter.emitOperation(*op, /*trailingSemicolon=*/false);
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// RUN: mlir-translate -mlir-to-cpp -propagate-constants %s | FileCheck %s -check-prefix=CPP-DEFAULT
// RUN: mlir-translate -mlir-to-cpp -constants-as-variables=false %s | FileCheck %s -check-prefix=CPP-DEFAULT

func.func @test() {
%start = "emitc.constant"() <{value = 0 : index}> : () -> !emitc.size_t
Expand All @@ -16,4 +16,4 @@ func.func @test() {
// CPP-DEFAULT-NEXT: for (size_t v1 = ((size_t) 0); v1 < ((size_t) 10); v1 += ((size_t) 1)) {
// CPP-DEFAULT-NEXT: }
// CPP-DEFAULT-NEXT: return;
// CPP-DEFAULT-NEXT: }
// CPP-DEFAULT-NEXT: }

0 comments on commit 5ca66df

Please sign in to comment.