From 5ca66df7a2f7a3813fbf04334312137a46296702 Mon Sep 17 00:00:00 2001 From: Luis Miguel Sousa Date: Thu, 28 Nov 2024 13:57:12 +0000 Subject: [PATCH] Change name of option and flip the default value --- mlir/include/mlir/Target/Cpp/CppEmitter.h | 2 +- mlir/lib/Target/Cpp/TranslateRegistration.cpp | 11 ++++---- mlir/lib/Target/Cpp/TranslateToCpp.cpp | 27 ++++++++++--------- ...mlir => emitc-constants-as-variables.mlir} | 4 +-- 4 files changed, 22 insertions(+), 22 deletions(-) rename mlir/test/Target/Cpp/{emitc-constant-propagation.mlir => emitc-constants-as-variables.mlir} (79%) diff --git a/mlir/include/mlir/Target/Cpp/CppEmitter.h b/mlir/include/mlir/Target/Cpp/CppEmitter.h index a2d03739182af..1c7ba78eba0c9 100644 --- a/mlir/include/mlir/Target/Cpp/CppEmitter.h +++ b/mlir/include/mlir/Target/Cpp/CppEmitter.h @@ -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 diff --git a/mlir/lib/Target/Cpp/TranslateRegistration.cpp b/mlir/lib/Target/Cpp/TranslateRegistration.cpp index ca084a9c6ac9e..dfe8bcb106f12 100644 --- a/mlir/lib/Target/Cpp/TranslateRegistration.cpp +++ b/mlir/lib/Target/Cpp/TranslateRegistration.cpp @@ -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 propagateConstants( - "propagate-constants", - llvm::cl::desc("Emit constants in their usage location instead of " - "declaring variables"), - llvm::cl::init(false)); + static llvm::cl::opt 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", @@ -47,7 +46,7 @@ void registerToCppTranslation() { op, output, /*declareVariablesAtTop=*/declareVariablesAtTop, /*onlyTu=*/onlyTu, - /*propagateConstants=*/propagateConstants); + /*constantsAsVariables=*/constantsAsVariables); }, [](DialectRegistry ®istry) { // clang-format off diff --git a/mlir/lib/Target/Cpp/TranslateToCpp.cpp b/mlir/lib/Target/Cpp/TranslateToCpp.cpp index 4cd0aabec2f2f..89bd5b434e0d0 100644 --- a/mlir/lib/Target/Cpp/TranslateToCpp.cpp +++ b/mlir/lib/Target/Cpp/TranslateToCpp.cpp @@ -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); @@ -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; } @@ -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; @@ -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(); } @@ -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); } @@ -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(def)) { os << "(("; @@ -1700,7 +1700,7 @@ LogicalResult CppEmitter::emitOperation(Operation &op, bool trailingSemicolon) { } bool trailingNewline = true; - if (shouldPropagateConstants() && isa(op)) { + if (!shouldUseConstantsAsVariables() && isa(op)) { trailingSemicolon = false; trailingNewline = false; } @@ -1871,7 +1871,8 @@ LogicalResult CppEmitter::emitTupleType(Location loc, ArrayRef 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); } diff --git a/mlir/test/Target/Cpp/emitc-constant-propagation.mlir b/mlir/test/Target/Cpp/emitc-constants-as-variables.mlir similarity index 79% rename from mlir/test/Target/Cpp/emitc-constant-propagation.mlir rename to mlir/test/Target/Cpp/emitc-constants-as-variables.mlir index 2f2cc8d123e84..5774bdc47308f 100644 --- a/mlir/test/Target/Cpp/emitc-constant-propagation.mlir +++ b/mlir/test/Target/Cpp/emitc-constants-as-variables.mlir @@ -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 @@ -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: } \ No newline at end of file +// CPP-DEFAULT-NEXT: }