[llvm] Refactored verbosity flag in mlgo-utils to common location #107898 (PR #128541)

Ali Raeisdanaei via llvm-commits llvm-commits at lists.llvm.org
Mon Feb 24 09:46:42 PST 2025


https://github.com/aliraeisdanaei created https://github.com/llvm/llvm-project/pull/128541

add common lib file to setup arguments for parser #107898

This is my first pull request to LLVM, so I would appreciate your feedback :)

>From 651fbfe823fcaac469f92c2556d4ef35ea43d1ce Mon Sep 17 00:00:00 2001
From: aliraeisdanaei <mrrookie2 at gmail.com>
Date: Mon, 24 Feb 2025 12:37:14 -0500
Subject: [PATCH] add common lib file to setup arguments for parser #107898

---
 .../mlgo/corpus/combine_training_corpus.py    | 11 ++--------
 .../mlgo-utils/mlgo/corpus/extract_ir.py      | 12 ++---------
 .../mlgo/corpus/parser_setup_lib.py           | 21 +++++++++++++++++++
 3 files changed, 25 insertions(+), 19 deletions(-)
 create mode 100644 llvm/utils/mlgo-utils/mlgo/corpus/parser_setup_lib.py

diff --git a/llvm/utils/mlgo-utils/mlgo/corpus/combine_training_corpus.py b/llvm/utils/mlgo-utils/mlgo/corpus/combine_training_corpus.py
index 90cf51cad75ed..5b56e49e5abee 100644
--- a/llvm/utils/mlgo-utils/mlgo/corpus/combine_training_corpus.py
+++ b/llvm/utils/mlgo-utils/mlgo/corpus/combine_training_corpus.py
@@ -27,6 +27,7 @@
 import logging
 
 from mlgo.corpus import combine_training_corpus_lib
+from mlgo.corpus import parser_setup_lib
 
 
 def parse_args_and_run():
@@ -36,15 +37,7 @@ def parse_args_and_run():
     parser.add_argument(
         "--root_dir", type=str, help="The root dir of module paths to combine."
     )
-    # TODO(#107898): Refactor this into a common location.
-    parser.add_argument(
-        "--verbosity",
-        type=str,
-        help="The verbosity level to use for logging",
-        default="INFO",
-        nargs="?",
-        choices=["DEBUG", "INFO", "WARNING", "ERROR"],
-    )
+    parser_setup_lib.add_verbosity_arguments(parser)
     args = parser.parse_args()
     main(args)
 
diff --git a/llvm/utils/mlgo-utils/mlgo/corpus/extract_ir.py b/llvm/utils/mlgo-utils/mlgo/corpus/extract_ir.py
index 5edb429241d0c..2b54d47b03d5d 100644
--- a/llvm/utils/mlgo-utils/mlgo/corpus/extract_ir.py
+++ b/llvm/utils/mlgo-utils/mlgo/corpus/extract_ir.py
@@ -28,7 +28,7 @@
 import logging
 
 from mlgo.corpus import extract_ir_lib
-
+from mlgo.corpus import parser_setup_lib
 
 def parse_args_and_run():
     parser = argparse.ArgumentParser(
@@ -111,15 +111,7 @@ def parse_args_and_run():
         default=".llvmbc",
         nargs="?",
     )
-    # TODO(#107898): Refactor this into a common location.
-    parser.add_argument(
-        "--verbosity",
-        type=str,
-        help="The verbosity level to use for logging",
-        default="INFO",
-        nargs="?",
-        choices=["DEBUG", "INFO", "WARNING", "ERROR"],
-    )
+    parser_setup_lib.add_verbosity_arguments(parser) 
     args = parser.parse_args()
     main(args)
 
diff --git a/llvm/utils/mlgo-utils/mlgo/corpus/parser_setup_lib.py b/llvm/utils/mlgo-utils/mlgo/corpus/parser_setup_lib.py
new file mode 100644
index 0000000000000..238bae3cd1e46
--- /dev/null
+++ b/llvm/utils/mlgo-utils/mlgo/corpus/parser_setup_lib.py
@@ -0,0 +1,21 @@
+# Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+# See https://llvm.org/LICENSE.txt for license information.
+# SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+"""Library functions for setting up common parser arguments"""
+
+from argparse import ArgumentParser
+
+def add_verbosity_arguments(parser: ArgumentParser) -> None:
+    """Adds the arguments for verbosity to the ArgumentParser
+
+    Arguments:
+        parser -- the argument parser being modified with verbosity arguments
+    """
+    parser.add_argument(
+        "--verbosity",
+        type=str,
+        help="The verbosity level to use for logging",
+        default="INFO",
+        nargs="?",
+        choices=["DEBUG", "INFO", "WARNING", "ERROR"],
+    )
\ No newline at end of file



More information about the llvm-commits mailing list