LLZK 3.0.0
An open-source IR for Zero Knowledge (ZK) circuits
Loading...
Searching...
No Matches
Parsers.h
Go to the documentation of this file.
1//===-- Parsers.h -----------------------------------------------*- C++ -*-===//
2//
3// Command line parsers for LLZK transformation passes.
4//
5// Part of the LLZK Project, under the Apache License v2.0.
6// See LICENSE.txt for license information.
7// Copyright 2025 Veridise Inc.
8// Copyright 2026 Project LLZK
9// SPDX-License-Identifier: Apache-2.0
10//
11//===----------------------------------------------------------------------===//
12
13#pragma once
14
15#include "llzk/Util/Compare.h"
16
17#include <mlir/Pass/Pass.h>
18
19#include <llvm/ADT/StringExtras.h>
20#include <llvm/Support/CommandLine.h>
21#include <llvm/Support/ErrorHandling.h>
22#include <llvm/Support/raw_ostream.h>
23
24#include <string>
25
26namespace llzk {
27
28namespace detail {
29
33 std::string str;
34
40 template <typename ValueT, typename CreateFnT, typename InitializeFnT>
41 std::unique_ptr<ValueT> createValidatedValue(
42 CreateFnT &&createValue, llvm::StringRef kind, InitializeFnT &&initializeValue
43 ) const {
44 auto value = createValue();
45 if (str.empty()) {
46 return value;
47 }
48
49 std::string error;
50 if (failed(initializeValue(*value, str, error))) {
51 llvm::report_fatal_error(
52 llvm::Twine("failed to initialize previously-validated nested ") + kind +
53 " options: " + error
54 );
55 }
56 return value;
57 }
58};
59
60} // namespace detail
61
64template <auto CreatePass> struct NestedPassOptions : detail::NestedTextualOptions {
66 std::unique_ptr<mlir::Pass> createPass() const {
67 return this->createValidatedValue<mlir::Pass>(CreatePass, "pass", initializePass);
68 }
69
70 static mlir::LogicalResult
71 initializePass(mlir::Pass &pass, llvm::StringRef options, std::string &error) {
72 return pass.initializeOptions(options, [&error](const llvm::Twine &message) {
73 error = message.str();
74 return mlir::failure();
75 });
76 }
77};
78
81template <typename PipelineOptionsT> struct NestedPipelineOptions : detail::NestedTextualOptions {
83 std::unique_ptr<PipelineOptionsT> createOptions() const {
85 std::make_unique<PipelineOptionsT>, "pipeline", initializeOptions
86 );
87 }
88
89 static mlir::LogicalResult
90 initializeOptions(PipelineOptionsT &options, llvm::StringRef value, std::string &error) {
91 llvm::raw_string_ostream errorStream(error);
92 return options.parseFromString(value, errorStream);
93 }
94};
95
96} // namespace llzk
97
98// Custom command line parsers
99namespace llvm {
100namespace cl {
101
102template <typename OptionsT> class NestedOptionsParserBase : public basic_parser<OptionsT> {
103public:
104 NestedOptionsParserBase(Option &O) : basic_parser<OptionsT>(O) {}
105
106protected:
109 bool parseNestedOptions(Option &O, StringRef Arg, StringRef kind, StringRef &options) const {
110 options = Arg;
111 if (options.consume_front("{") && !options.consume_back("}")) {
112 return O.error(llvm::Twine("expected nested ") + kind + " options to end with '}'");
113 }
114 return false;
115 }
116
117public:
118 static void print(llvm::raw_ostream &OS, const OptionsT &Val) { OS << '{' << Val.str << '}'; }
119
121 const Option &O, const OptionsT &V, const OptionValue<OptionsT> &Default, size_t GlobalWidth
122 ) const {
123 this->printOptionName(O, GlobalWidth);
124 print(llvm::outs(), V);
125 llvm::outs() << " (default: ";
126 if (Default.hasValue()) {
127 print(llvm::outs(), Default.getValue());
128 } else {
129 llvm::outs() << "<unspecified>";
130 }
131 llvm::outs() << ")\n";
132 }
133};
134
136template <auto CreatePass>
137class parser<llzk::NestedPassOptions<CreatePass>>
138 : public NestedOptionsParserBase<llzk::NestedPassOptions<CreatePass>> {
139public:
142
143 parser(Option &O) : Base(O) {}
144
145 bool parse(Option &O, StringRef, StringRef Arg, OptionsT &Val) {
146 StringRef options;
147 if (this->parseNestedOptions(O, Arg, "pass", options)) {
148 return true;
149 }
150
151 auto pass = CreatePass();
152 std::string error;
153 if (failed(OptionsT::initializePass(*pass, options, error))) {
154 return O.error(error);
155 }
156
157 Val.str = options.str();
158 return false;
159 }
160};
161
164template <typename PipelineOptionsT>
165class parser<llzk::NestedPipelineOptions<PipelineOptionsT>>
166 : public NestedOptionsParserBase<llzk::NestedPipelineOptions<PipelineOptionsT>> {
167public:
170
171 parser(Option &O) : Base(O) {}
172
173 bool parse(Option &O, StringRef, StringRef Arg, OptionsT &Val) {
174 StringRef options;
175 if (this->parseNestedOptions(O, Arg, "pipeline", options)) {
176 return true;
177 }
178
179 PipelineOptionsT pipelineOptions;
180 std::string error;
181 if (failed(OptionsT::initializeOptions(pipelineOptions, options, error))) {
182 return O.error(error);
183 }
184
185 Val.str = options.str();
186 return false;
187 }
188};
189
190} // namespace cl
191} // namespace llvm
bool parseNestedOptions(Option &O, StringRef Arg, StringRef kind, StringRef &options) const
Parse a nested pass or pipeline option payload, optionally stripping a surrounding {....
Definition Parsers.h:109
void printOptionDiff(const Option &O, const OptionsT &V, const OptionValue< OptionsT > &Default, size_t GlobalWidth) const
Definition Parsers.h:120
static void print(llvm::raw_ostream &OS, const OptionsT &Val)
Definition Parsers.h:118
llzk::NestedPassOptions< CreatePass > OptionsT
Definition Parsers.h:140
bool parse(Option &O, StringRef, StringRef Arg, OptionsT &Val)
Definition Parsers.h:145
NestedOptionsParserBase< OptionsT > Base
Definition Parsers.h:141
llzk::NestedPipelineOptions< PipelineOptionsT > OptionsT
Definition Parsers.h:168
bool parse(Option &O, StringRef, StringRef Arg, OptionsT &Val)
Definition Parsers.h:173
Provides the necessary information to the RemoveUnusedDiscardableAllocations pass about operations th...
Definition Ops.cpp.inc:41
Stores textual options for a constituent pass after validating them against that pass' native MLIR op...
Definition Parsers.h:64
static mlir::LogicalResult initializePass(mlir::Pass &pass, llvm::StringRef options, std::string &error)
Definition Parsers.h:71
std::unique_ptr< mlir::Pass > createPass() const
Build a fresh pass instance with the validated options applied.
Definition Parsers.h:66
Stores textual options for a constituent pipeline after validating them against that pipeline's nativ...
Definition Parsers.h:81
std::unique_ptr< PipelineOptionsT > createOptions() const
Build a fresh options object with the validated options applied.
Definition Parsers.h:83
static mlir::LogicalResult initializeOptions(StructInliningFlatteningOptions &options, llvm::StringRef value, std::string &error)
Definition Parsers.h:90
Shared storage and helpers for nested textual pass and pipeline options.
Definition Parsers.h:31
std::string str
The validated textual form without the outer {...} delimiters.
Definition Parsers.h:33
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.
Definition Parsers.h:41