[Mlir-commits] [mlir] [emitC]Option to mlir-translate class instead of function (PR #141158)

Mehdi Amini llvmlistbot at llvm.org
Thu May 29 12:19:20 PDT 2025


================
@@ -33,13 +33,50 @@ void registerToCppTranslation() {
       "file-id", llvm::cl::desc("Emit emitc.file ops with matching id"),
       llvm::cl::init(""));
 
+  static llvm::cl::opt<bool> emitClass(
+      "emit-class",
+      llvm::cl::desc("If specified, the output will be a class where "
+                     "the function(s) in the module are methods "
+                     "Enables class-related options"),
+      llvm::cl::init(false));
+
+  static llvm::cl::opt<std::string> className(
+      "class-name",
+      llvm::cl::desc("Mandatory class name if --emit-class is set"),
+      llvm::cl::init(""));
+
+  static llvm::cl::opt<std::string> fieldNameAttribute(
+      "field-name-attribute",
+      llvm::cl::desc("Mandatory name of the attribute to use as field name if "
+                     "--emit-class is set(default=tf_saved_model.index_path)"),
+      llvm::cl::init("tf_saved_model.index_path"));
+
   TranslateFromMLIRRegistration reg(
       "mlir-to-cpp", "translate from mlir to cpp",
       [](Operation *op, raw_ostream &output) {
+        if (emitClass) {
+          if (className.empty()) {
+            llvm::errs() << "Error: --class-name is mandatory when "
+                            "--emit-class is set.\n";
+            return mlir::failure();
+          }
+          if (fieldNameAttribute.empty()) {
+            llvm::errs() << "Error: --field-name-attribute is mandatory when "
+                            "--emit-class is set.\n";
+            return mlir::failure();
+          }
+          return emitc::translateToCpp(
+              op, output,
+              /*declareVariablesAtTop=*/declareVariablesAtTop,
+              /*fileId=*/fileId, /*emitClass=*/emitClass,
+              /*className=*/className,
+              /*fieldNameAttribute=*/fieldNameAttribute);
+        }
----------------
joker-eph wrote:

Some sanity check that isn't done here is that `fieldNameAttribute` better be unset if `emitClass` isn't set either.

https://github.com/llvm/llvm-project/pull/141158


More information about the Mlir-commits mailing list