17#include <mlir/Pass/Pass.h>
19#include <llvm/ADT/APInt.h>
20#include <llvm/ADT/StringExtras.h>
21#include <llvm/Support/CommandLine.h>
22#include <llvm/Support/ErrorHandling.h>
23#include <llvm/Support/raw_ostream.h>
41 template <
typename ValueT,
typename CreateFnT,
typename InitializeFnT>
43 CreateFnT &&createValue, llvm::StringRef kind, InitializeFnT &&initializeValue
45 auto value = createValue();
51 if (failed(initializeValue(*value,
str, error))) {
52 llvm::report_fatal_error(
53 llvm::Twine(
"failed to initialize previously-validated nested ") + kind +
71 static mlir::LogicalResult
72 initializePass(mlir::Pass &pass, llvm::StringRef options, std::string &error) {
73 return pass.initializeOptions(options, [&error](
const llvm::Twine &message) {
74 error = message.str();
75 return mlir::failure();
90 static mlir::LogicalResult
92 llvm::raw_string_ostream errorStream(error);
93 return options.parseFromString(value, errorStream);
112 if (options.consume_front(
"{") && !options.consume_back(
"}")) {
113 return O.error(llvm::Twine(
"expected nested ") + kind +
" options to end with '}'");
119 static void print(llvm::raw_ostream &OS,
const OptionsT &Val) { OS <<
'{' << Val.str <<
'}'; }
122 const Option &O,
const OptionsT &V,
const OptionValue<OptionsT> &Default,
size_t GlobalWidth
124 this->printOptionName(O, GlobalWidth);
125 print(llvm::outs(), V);
126 llvm::outs() <<
" (default: ";
127 if (Default.hasValue()) {
128 print(llvm::outs(), Default.getValue());
130 llvm::outs() <<
"<unspecified>";
132 llvm::outs() <<
")\n";
137template <auto CreatePass>
152 auto pass = CreatePass();
155 return O.error(error);
158 Val.
str = options.str();
165template <
typename PipelineOptionsT>
180 PipelineOptionsT pipelineOptions;
183 return O.error(error);
186 Val.
str = options.str();
192template <>
class parser<APInt> :
public basic_parser<APInt> {
196 bool parse(Option &O, StringRef, StringRef Arg, APInt &Val) {
198 return O.error(
"empty integer literal");
200 if (!all_of(Arg, [](
char c) {
return isDigit(c); })) {
201 return O.error(
"arg must be in base 10 (digits).");
205 APInt tmp(bits, Arg, 10);
206 unsigned active = tmp.getActiveBits();
210 Val = tmp.zextOrTrunc(active);
218 const Option &O,
const APInt &V,
const OptionValue<APInt> &Default,
size_t GlobalWidth
220 std::string Cur = llvm::toString(V, 10,
false);
222 std::string Def =
"<unspecified>";
223 if (Default.hasValue()) {
224 const APInt &D = Default.getValue();
225 Def = llvm::toString(D, 10,
false);
228 printOptionName(O, GlobalWidth);
229 llvm::outs() << Cur <<
" (default: " << Def <<
")\n";
bool parseNestedOptions(Option &O, StringRef Arg, StringRef kind, StringRef &options) const
Parse a nested pass or pipeline option payload, optionally stripping a surrounding {....
NestedOptionsParserBase(Option &O)
void printOptionDiff(const Option &O, const OptionsT &V, const OptionValue< OptionsT > &Default, size_t GlobalWidth) const
static void print(llvm::raw_ostream &OS, const OptionsT &Val)
void printOptionDiff(const Option &O, const APInt &V, const OptionValue< APInt > &Default, size_t GlobalWidth) const
bool parse(Option &O, StringRef, StringRef Arg, APInt &Val)
llzk::NestedPassOptions< CreatePass > OptionsT
bool parse(Option &O, StringRef, StringRef Arg, OptionsT &Val)
NestedOptionsParserBase< OptionsT > Base
llzk::NestedPipelineOptions< PipelineOptionsT > OptionsT
NestedOptionsParserBase< OptionsT > Base
bool parse(Option &O, StringRef, StringRef Arg, OptionsT &Val)
constexpr T checkedCast(U u) noexcept
Stores textual options for a constituent pass after validating them against that pass' native MLIR op...
static mlir::LogicalResult initializePass(mlir::Pass &pass, llvm::StringRef options, std::string &error)
std::unique_ptr< mlir::Pass > createPass() const
Build a fresh pass instance with the validated options applied.
Stores textual options for a constituent pipeline after validating them against that pipeline's nativ...
std::unique_ptr< PipelineOptionsT > createOptions() const
Build a fresh options object with the validated options applied.
static mlir::LogicalResult initializeOptions(StructInliningFlatteningOptions &options, llvm::StringRef value, std::string &error)
Shared storage and helpers for nested textual pass and pipeline options.
std::string str
The validated textual form without the outer {...} delimiters.
std::unique_ptr< ValueT > createValidatedValue(CreateFnT &&createValue, llvm::StringRef kind, InitializeFnT &&initializeValue) const
Recreate a nested value from the stored option string and re-validate it.