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

llvmlistbot at llvm.org llvmlistbot at llvm.org
Tue May 28 09:24:28 PDT 2024


llvmbot wrote:


<!--LLVM PR SUMMARY COMMENT-->

@llvm/pr-subscribers-llvm-support

Author: David (Dabsunter)

<details>
<summary>Changes</summary>

This PR export and add the sanity check performed in `CommandLineParser::ParseCommandLineOptions()` to `PassOptions::parseFromString()`, so that `cl::Required` and `cl::OneOrMore` flags on Pass option behave the same as reguilar `cl::opt` in the global command parser.

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


3 Files Affected:

- (modified) llvm/include/llvm/Support/CommandLine.h (+3) 
- (modified) llvm/lib/Support/CommandLine.cpp (+13-14) 
- (modified) mlir/lib/Pass/PassRegistry.cpp (+3) 


``````````diff
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();
 }
 

``````````

</details>


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


More information about the Mlir-commits mailing list