Skip to content

[MLGO] Refactored verbosity flag in mlgo-utils to common location #128541

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Feb 24, 2025

Conversation

aliraeisdanaei
Copy link
Contributor

@aliraeisdanaei aliraeisdanaei commented Feb 24, 2025

add common lib file to setup arguments for parser #107898

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

Fixes #107898.

Copy link

Thank you for submitting a Pull Request (PR) to the LLVM Project!

This PR will be automatically labeled and the relevant teams will be notified.

If you wish to, you can add reviewers by using the "Reviewers" section on this page.

If this is not working for you, it is probably because you do not have write permissions for the repository. In which case you can instead tag reviewers by name in a comment by using @ followed by their GitHub username.

If you have received no comments on your PR for a week, you can request a review by "ping"ing the PR by adding a comment “Ping”. The common courtesy "ping" rate is once a week. Please remember that you are asking for valuable time from other developers.

If you have further questions, they may be answered by the LLVM GitHub User Guide.

You can also ask questions in a comment on this PR, on the LLVM Discord or on the forums.

@llvmbot llvmbot added the mlgo label Feb 24, 2025
@llvmbot
Copy link
Member

llvmbot commented Feb 24, 2025

@llvm/pr-subscribers-mlgo

Author: Ali Raeisdanaei (aliraeisdanaei)

Changes

add common lib file to setup arguments for parser #107898

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


Full diff: https://github.com/llvm/llvm-project/pull/128541.diff

3 Files Affected:

  • (modified) llvm/utils/mlgo-utils/mlgo/corpus/combine_training_corpus.py (+2-9)
  • (modified) llvm/utils/mlgo-utils/mlgo/corpus/extract_ir.py (+2-10)
  • (added) llvm/utils/mlgo-utils/mlgo/corpus/parser_setup_lib.py (+21)
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

@aliraeisdanaei
Copy link
Contributor Author

Hello @boomanaiden154 and @mtrofin, I noticed the comments on PR 107818, and I made the refactoring that was needed. I hope this is an easy merge. Thank you.

Copy link

github-actions bot commented Feb 24, 2025

✅ With the latest revision this PR passed the Python code formatter.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: can you rename this to flags or tool_flags (open to other suggestions):

  • we only use the _lib naming pattern when we have a tool and want to unittest it, so most of the tool logic is moved to the _lib, leaving the tool itself be basically the main
  • an advantage of your patch is that it encapsulates the dependency on a specific arg parsing library (argparse), so renaming it away from what it encapsulates and towards what it provides its users makes sense, imho.

Thanks!

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you I can name my file to parser_flags if you like

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That would mean they are flags for the parser :) Would flags work?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ye sure flags its is

default="INFO",
nargs="?",
choices=["DEBUG", "INFO", "WARNING", "ERROR"],
)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: missing empty line (makes, or will make, git diff unhappy later)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For sure. I added the new line at the end of my file. I didn't know it was a thing thank you for teaching me. Let me know if it still does not look good.

Copy link
Member

@mtrofin mtrofin left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

lgtm

once remaining comment is addressed and build bots pass - good to go!

@mtrofin
Copy link
Member

mtrofin commented Feb 24, 2025

lgtm

once remaining comment is addressed and build bots pass - good to go!

for the failing checks - see https://llvm.org/docs/CodingStandards.html#python-version-and-source-code-formatting. (in your case I think the fix can be done manually very easily, but it's worth getting the auto-formatting tools set up for later, too)

Copy link
Contributor

@boomanaiden154 boomanaiden154 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

One nit, otherwise LGTM. Formatting needs to be fixed.

@boomanaiden154 boomanaiden154 changed the title Refactored verbosity flag in mlgo-utils to common location #107898 [MLGO] Refactored verbosity flag in mlgo-utils to common location Feb 24, 2025
@boomanaiden154 boomanaiden154 merged commit fc09550 into llvm:main Feb 24, 2025
6 of 10 checks passed
Copy link

@aliraeisdanaei Congratulations on having your first Pull Request (PR) merged into the LLVM Project!

Your changes will be combined with recent changes from other authors, then tested by our build bots. If there is a problem with a build, you may receive a report in an email or a comment on this PR.

Please check whether problems have been caused by your change specifically, as the builds can include changes from many authors. It is not uncommon for your change to be included in a build that fails due to someone else's changes, or infrastructure issues.

How to do this, and the rest of the post-merge process, is covered in detail here.

If your change does cause a problem, it may be reverted, or you can revert it yourself. This is a normal part of LLVM development. You can fix your changes and open a new PR to merge them again.

If you don't get any reports, no action is required from you. Your changes are working as expected, well done!

@aliraeisdanaei
Copy link
Contributor Author

Thank you @mtrofin and @boomanaiden154. I had a really fun time making my first merge. Ye were the best :))))))

mtrofin added a commit that referenced this pull request Feb 26, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Refactor verbosity flag in mlgo-utils to common location
4 participants