[llvm] [mlir] [mlir][cl_parser] Support cl::Required/cl::OneOrMore flags for Pass options (PR #93570)

via llvm-commits llvm-commits at lists.llvm.org
Tue May 28 09:18:24 PDT 2024


https://github.com/Dabsunter created https://github.com/llvm/llvm-project/pull/93570

None

>From c30e6a00766aeefcb396b3d642961656a24bba2b Mon Sep 17 00:00:00 2001
From: David Nicolazo <davidchr at amd.com>
Date: Tue, 28 May 2024 17:16:58 +0100
Subject: [PATCH] [mlir][cl_parser] Support cl::Required/cl::OneOrMore flags
 for Pass options

---
 llvm/include/llvm/Support/CommandLine.h |  3 +++
 llvm/lib/Support/CommandLine.cpp        | 27 ++++++++++++-------------
 mlir/lib/Pass/PassRegistry.cpp          |  3 +++
 3 files changed, 19 insertions(+), 14 deletions(-)

diff --git a/llvm/include/llvm/Support/CommandLine.h b/llvm/include/llvm/Support/CommandLine.h
index b035209406b68..18b62301fbd41 100644
--- a/llvm/include/llvm/Support/CommandLine.h
+++ b/llvm/include/llvm/Support/CommandLine.h
@@ -2290,6 +2290,9 @@ void ResetCommandLineParser();
 /// Parses `Arg` into the option handler `Handler`.
 bool ProvidePositionalOption(Option *Handler, StringRef Arg, int i);
 
+/// Verify that every Required/OneOrMore option has a value.
+bool CheckRequiredValues(const StringMap<Option *> &OptionsMap);
+
 } // end namespace cl
 
 } // end namespace llvm
diff --git a/llvm/lib/Support/CommandLine.cpp b/llvm/lib/Support/CommandLine.cpp
index 8a00d4798f33d..4ebd76f70f306 100644
--- a/llvm/lib/Support/CommandLine.cpp
+++ b/llvm/lib/Support/CommandLine.cpp
@@ -792,6 +792,17 @@ static bool EatsUnboundedNumberOfValues(const Option *O) {
          O->getNumOccurrencesFlag() == cl::OneOrMore;
 }
 
+bool llvm::cl::CheckRequiredValues(const StringMap<Option *> &OptionsMap) {
+  // Loop over args and make sure all required args are specified!
+  for (const auto &Opt : OptionsMap) {
+    if (RequiresValue(Opt.second) && Opt.second->getNumOccurrences() == 0) {
+      Opt.second->error("must be specified at least once!");
+      return false;
+    }
+  }
+  return true;
+}
+
 static bool isWhitespace(char C) {
   return C == ' ' || C == '\t' || C == '\r' || C == '\n';
 }
@@ -1795,20 +1806,8 @@ bool CommandLineParser::ParseCommandLineOptions(int argc,
                                   PositionalVals[ValNo].second);
   }
 
-  // Loop over args and make sure all required args are specified!
-  for (const auto &Opt : OptionsMap) {
-    switch (Opt.second->getNumOccurrencesFlag()) {
-    case Required:
-    case OneOrMore:
-      if (Opt.second->getNumOccurrences() == 0) {
-        Opt.second->error("must be specified at least once!");
-        ErrorParsing = true;
-      }
-      [[fallthrough]];
-    default:
-      break;
-    }
-  }
+  if (!CheckRequiredValues(OptionsMap))
+    ErrorParsing = true;
 
   // Now that we know if -debug is specified, we can use it.
   // Note that if ReadResponseFiles == true, this must be done before the
diff --git a/mlir/lib/Pass/PassRegistry.cpp b/mlir/lib/Pass/PassRegistry.cpp
index f8149673a4093..cc7c700850d61 100644
--- a/mlir/lib/Pass/PassRegistry.cpp
+++ b/mlir/lib/Pass/PassRegistry.cpp
@@ -299,6 +299,9 @@ LogicalResult detail::PassOptions::parseFromString(StringRef options,
       return failure();
   }
 
+  if (!llvm::cl::CheckRequiredValues(OptionsMap))
+    return failure();
+
   return success();
 }
 



More information about the llvm-commits mailing list