[mlir][Translation] Make commandline option registration optional
authorrkayaith <rkayaith@gmail.com>
Sun, 23 Oct 2022 19:35:18 +0000 (15:35 -0400)
committerrkayaith <rkayaith@gmail.com>
Mon, 24 Oct 2022 18:40:07 +0000 (14:40 -0400)
This moves the commandline option registration into its own function, so
that users can register translations without registering the options.

Reviewed By: cota

Differential Revision: https://reviews.llvm.org/D136561

mlir/include/mlir/Tools/mlir-translate/Translation.h
mlir/lib/Tools/mlir-translate/MlirTranslateMain.cpp
mlir/lib/Tools/mlir-translate/Translation.cpp

index 4c090b7..d3cd817 100644 (file)
@@ -106,6 +106,9 @@ struct TranslationParser : public llvm::cl::parser<const TranslateFunction *> {
                        size_t globalWidth) const override;
 };
 
+/// Register command-line options used by the translation registry.
+void registerTranslationCLOptions();
+
 } // namespace mlir
 
 #endif // MLIR_TOOLS_MLIRTRANSLATE_TRANSLATION_H
index a8dc1df..ef2545b 100644 (file)
@@ -61,6 +61,7 @@ LogicalResult mlir::mlirTranslateMain(int argc, char **argv,
                            llvm::cl::Required);
   registerAsmPrinterCLOptions();
   registerMLIRContextCLOptions();
+  registerTranslationCLOptions();
   llvm::cl::ParseCommandLineOptions(argc, argv, toolName);
 
   std::string errorMessage;
index 9e97e1b..1eb285e 100644 (file)
 using namespace mlir;
 
 //===----------------------------------------------------------------------===//
+// Translation CommandLine Options
+//===----------------------------------------------------------------------===//
+
+struct TranslationOptions {
+  llvm::cl::opt<bool> noImplicitModule{
+      "no-implicit-module",
+      llvm::cl::desc("Disable the parsing of an implicit top-level module op"),
+      llvm::cl::init(false)};
+};
+
+static llvm::ManagedStatic<TranslationOptions> clOptions;
+
+void mlir::registerTranslationCLOptions() { *clOptions; }
+
+//===----------------------------------------------------------------------===//
 // Translation Registry
 //===----------------------------------------------------------------------===//
 
@@ -103,24 +118,22 @@ TranslateFromMLIRRegistration::TranslateFromMLIRRegistration(
     const TranslateFromMLIRFunction &function,
     const std::function<void(DialectRegistry &)> &dialectRegistration) {
 
-  static llvm::cl::opt<bool> noImplicitModule{
-      "no-implicit-module",
-      llvm::cl::desc("Disable the parsing of an implicit top-level module op"),
-      llvm::cl::init(false)};
-
-  registerTranslation(name, description,
-                      [function, dialectRegistration](
-                          llvm::SourceMgr &sourceMgr, raw_ostream &output,
-                          MLIRContext *context) {
-                        DialectRegistry registry;
-                        dialectRegistration(registry);
-                        context->appendDialectRegistry(registry);
-                        OwningOpRef<Operation *> op = parseSourceFileForTool(
-                            sourceMgr, context, !noImplicitModule);
-                        if (!op || failed(verify(*op)))
-                          return failure();
-                        return function(op.get(), output);
-                      });
+  registerTranslation(
+      name, description,
+      [function, dialectRegistration](llvm::SourceMgr &sourceMgr,
+                                      raw_ostream &output,
+                                      MLIRContext *context) {
+        DialectRegistry registry;
+        dialectRegistration(registry);
+        context->appendDialectRegistry(registry);
+        bool implicitModule =
+            (!clOptions.isConstructed() || !clOptions->noImplicitModule);
+        OwningOpRef<Operation *> op =
+            parseSourceFileForTool(sourceMgr, context, implicitModule);
+        if (!op || failed(verify(*op)))
+          return failure();
+        return function(op.get(), output);
+      });
 }
 
 //===----------------------------------------------------------------------===//