[llvm] [MLGO] Fix logging verbosity in scripts (PR #107818)

Aiden Grossman via llvm-commits llvm-commits at lists.llvm.org
Mon Sep 9 11:25:43 PDT 2024


https://github.com/boomanaiden154 updated https://github.com/llvm/llvm-project/pull/107818

>From 3e0876e9410e55db1233f6d798733fbd06525f57 Mon Sep 17 00:00:00 2001
From: Aiden Grossman <aidengrossman at google.com>
Date: Mon, 9 Sep 2024 07:47:12 +0000
Subject: [PATCH 1/2] [MLGO] Fix logging verbosity in scripts

This patch fixes issues related to logging verbosity in the MLGO python
scripts. This was an oversight when converting from absl.logging to the
python logging API as absl natively supports a --verbosity flag to set
the desired logging level. This patch adds a flag to support similar
functionality in Python's logging library and additionally updates
docstrings where relevant to point to the new values.
---
 .../mlgo/corpus/combine_training_corpus.py          | 10 ++++++++++
 llvm/utils/mlgo-utils/mlgo/corpus/extract_ir.py     | 13 +++++++++++--
 2 files changed, 21 insertions(+), 2 deletions(-)

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 3b2077b4c0e0e6..85b58d44e01fe0 100644
--- a/llvm/utils/mlgo-utils/mlgo/corpus/combine_training_corpus.py
+++ b/llvm/utils/mlgo-utils/mlgo/corpus/combine_training_corpus.py
@@ -24,6 +24,7 @@
 """
 
 import argparse
+import logging
 
 from mlgo.corpus import combine_training_corpus_lib
 
@@ -35,11 +36,20 @@ def parse_args_and_run():
     parser.add_argument(
         "--root_dir", type=str, help="The root dir of module paths to combine."
     )
+    parser.add_argument(
+        "--verbosity",
+        type=int,
+        help="The verbosity level to use for logging",
+        default=0,
+        nargs="?",
+    )
     args = parser.parse_args()
     main(args)
 
 
 def main(args):
+    logging.basicConfig(level=args.verbosity)
+
     combine_training_corpus_lib.combine_corpus(args.root_dir)
 
 
diff --git a/llvm/utils/mlgo-utils/mlgo/corpus/extract_ir.py b/llvm/utils/mlgo-utils/mlgo/corpus/extract_ir.py
index a7d52daaedba3b..11f7bca796b22d 100644
--- a/llvm/utils/mlgo-utils/mlgo/corpus/extract_ir.py
+++ b/llvm/utils/mlgo-utils/mlgo/corpus/extract_ir.py
@@ -20,8 +20,8 @@
 
 To change the logging verbosity, pass an integer representing the desired
 verbosity to the --verbosity flag. Use 0 for all logs, status information,
-and detailed debug information, -1 for solely warnings, and -2 to not produce
-any output.
+and detailed debug information, 30 for solely warnings, and 50 or higher to
+not produce any output.
 """
 
 import argparse
@@ -113,11 +113,20 @@ def parse_args_and_run():
         default=".llvmbc",
         nargs="?",
     )
+    parser.add_argument(
+        "--verbosity",
+        type=int,
+        help="The verbosity level to use for logging",
+        default=0,
+        nargs="?",
+    )
     args = parser.parse_args()
     main(args)
 
 
 def main(args):
+    logging.basicConfig(level=args.verbosity)
+
     objs = []
     if args.input is not None and args.thinlto_build == "local":
         raise ValueError("--thinlto_build=local cannot be run with --input")

>From f21aabf4ad5acee22d5af2c93f682117cc4c927b Mon Sep 17 00:00:00 2001
From: Aiden Grossman <aidengrossman at google.com>
Date: Mon, 9 Sep 2024 18:25:31 +0000
Subject: [PATCH 2/2] Address feedback

---
 .../mlgo/corpus/combine_training_corpus.py           |  5 +++--
 llvm/utils/mlgo-utils/mlgo/corpus/extract_ir.py      | 12 ++++++------
 2 files changed, 9 insertions(+), 8 deletions(-)

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 85b58d44e01fe0..048876ab6224af 100644
--- a/llvm/utils/mlgo-utils/mlgo/corpus/combine_training_corpus.py
+++ b/llvm/utils/mlgo-utils/mlgo/corpus/combine_training_corpus.py
@@ -38,10 +38,11 @@ def parse_args_and_run():
     )
     parser.add_argument(
         "--verbosity",
-        type=int,
+        type=str,
         help="The verbosity level to use for logging",
-        default=0,
+        default="INFO",
         nargs="?",
+        choices=["DEBUG", "INFO", "WARNING", "ERROR"],
     )
     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 11f7bca796b22d..a8ad79e636a6ca 100644
--- a/llvm/utils/mlgo-utils/mlgo/corpus/extract_ir.py
+++ b/llvm/utils/mlgo-utils/mlgo/corpus/extract_ir.py
@@ -18,10 +18,9 @@
 In a local ThinLTO case, the compilation is assumedto have been performed
 specifying -Wl,--save-temps=import -Wl,--thinlto-emit-index-files
 
-To change the logging verbosity, pass an integer representing the desired
-verbosity to the --verbosity flag. Use 0 for all logs, status information,
-and detailed debug information, 30 for solely warnings, and 50 or higher to
-not produce any output.
+To change the logging verbosity, set the --verbosity flag to the desired level.
+Setting it to a specific level will enable all messages at that level and
+higher. Exact values can be found by invoking the script with --help.
 """
 
 import argparse
@@ -115,10 +114,11 @@ def parse_args_and_run():
     )
     parser.add_argument(
         "--verbosity",
-        type=int,
+        type=str,
         help="The verbosity level to use for logging",
-        default=0,
+        default="INFO",
         nargs="?",
+        choices=["DEBUG", "INFO", "WARNING", "ERROR"],
     )
     args = parser.parse_args()
     main(args)



More information about the llvm-commits mailing list