LLZK 2.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// SPDX-License-Identifier: Apache-2.0
9//
10//===----------------------------------------------------------------------===//
11
12#pragma once
13
14#include "llzk/Util/Compare.h"
15
16#include <llvm/ADT/APInt.h>
17#include <llvm/ADT/StringExtras.h>
18#include <llvm/Support/CommandLine.h>
19
20// Custom command line parsers
21namespace llvm {
22namespace cl {
23
24// Parser for APInt
25template <> class parser<APInt> : public basic_parser<APInt> {
26public:
27 parser(Option &O) : basic_parser(O) {}
28
29 bool parse(Option &O, StringRef, StringRef Arg, APInt &Val) {
30 if (Arg.empty()) {
31 return O.error("empty integer literal");
32 }
33 if (!all_of(Arg, [](char c) { return isDigit(c); })) {
34 return O.error("arg must be in base 10 (digits).");
35 }
36 // Decimal-only: allocate a safe width then shrink.
37 unsigned bits = std::max(1u, 4u * llzk::checkedCast<unsigned>(Arg.size()));
38 APInt tmp(bits, Arg, 10);
39 unsigned active = tmp.getActiveBits();
40 if (active == 0) {
41 active = 1;
42 }
43 Val = tmp.zextOrTrunc(active);
44 return false;
45 }
46
47 // Prints how the passed option differs from the default one specified in the pass
48 // For example, if V = 17 and Default = 11 then it should print
49 // [OptionName] 17 (default: 11)
51 const Option &O, const APInt &V, const OptionValue<APInt> &Default, size_t GlobalWidth
52 ) const {
53 std::string Cur = llvm::toString(V, 10, false);
54
55 std::string Def = "<unspecified>";
56 if (Default.hasValue()) {
57 const APInt &D = Default.getValue();
58 Def = llvm::toString(D, 10, false);
59 }
60
61 printOptionName(O, GlobalWidth);
62 llvm::outs() << Cur << " (default: " << Def << ")\n";
63 }
64};
65
66} // namespace cl
67} // namespace llvm
void printOptionDiff(const Option &O, const APInt &V, const OptionValue< APInt > &Default, size_t GlobalWidth) const
Definition Parsers.h:50
bool parse(Option &O, StringRef, StringRef Arg, APInt &Val)
Definition Parsers.h:29
constexpr T checkedCast(U u) noexcept
Definition Compare.h:81