LLZK 3.0.0
An open-source IR for Zero Knowledge (ZK) circuits
Loading...
Searching...
No Matches
llzk-opt.cpp
Go to the documentation of this file.
1//===-- llzk-opt.cpp - LLZK opt tool ----------------------------*- C++ -*-===//
2//
3// Part of the LLZK Project, under the Apache License v2.0.
4// See LICENSE.txt for license information.
5// Copyright 2025 Veridise Inc.
6// SPDX-License-Identifier: Apache-2.0
7//
8//===----------------------------------------------------------------------===//
14//===----------------------------------------------------------------------===//
15
16#include "r1cs/DialectRegistration.h"
17#include "tools/config.h"
18#include "zklean/DialectRegistration.h"
19
20#include "llzk/Config/Config.h"
23
24#include <mlir/IR/DialectRegistry.h>
25#include <mlir/IR/MLIRContext.h>
26#include <mlir/IR/OperationSupport.h>
27#include <mlir/Support/LogicalResult.h>
28#include <mlir/Tools/mlir-opt/MlirOptMain.h>
29
30#include <llvm/ADT/ArrayRef.h>
31#include <llvm/ADT/StringRef.h>
32#include <llvm/Support/CommandLine.h>
33#include <llvm/Support/PrettyStackTrace.h>
34#include <llvm/Support/Signals.h>
35#include <llvm/Support/raw_ostream.h>
36
37#include <cstdlib>
38#include <exception>
39#include <string>
40#include <tuple>
41
42#if LLZK_WITH_PCL
43#include "pcl/DialectRegistration.h"
44#endif // LLZK_WITH_PCL
45
47static int runMain(int argc, char **argv) {
48 llvm::cl::list<std::string> IncludeDirs(
49 "I", llvm::cl::desc("Directory of include files"), llvm::cl::value_desc("directory"),
50 llvm::cl::Prefix
51 );
52
53 llvm::cl::opt<bool> PrintAllOps(
54 "print-llzk-ops", llvm::cl::desc("Print a list of all ops registered in LLZK")
55 );
56
57 llvm::sys::PrintStackTraceOnErrorSignal(llvm::StringRef());
58 llvm::setBugReportMsg(
59 "PLEASE submit a bug report to " BUG_REPORT_URL
60 " and include the crash backtrace, relevant LLZK files,"
61 " and associated run script(s).\n"
62 );
63 llvm::cl::AddExtraVersionPrinter([](llvm::raw_ostream &os) {
64 os << "\nLLZK (" LLZK_URL "):\n LLZK version " LLZK_VERSION_STRING "\n";
65 });
66
67 // Register dialects and passes
68 mlir::DialectRegistry registry;
69 llzk::registerDialects(registry);
70 r1cs::registerDialects(registry);
71 zklean::registerDialects(registry);
72#if LLZK_WITH_PCL
73 pcl::registerDialects(registry);
74#endif // LLZK_WITH_PCL
75
76 llzk::registerPasses(registry);
77 r1cs::registerPasses(registry);
78 zklean::registerPasses(registry);
79#if LLZK_WITH_PCL
80 pcl::registerPasses(registry);
81#endif // LLZK_WITH_PCL
82
83 // Register and parse command line options.
84 std::string inputFilename, outputFilename;
85 std::tie(inputFilename, outputFilename) =
86 registerAndParseCLIOptions(argc, argv, "llzk-opt", registry);
87
88 if (PrintAllOps) {
89 mlir::MLIRContext context;
90 context.appendDialectRegistry(registry);
91 context.loadAllAvailableDialects();
92 llvm::outs() << "All ops registered in LLZK IR: {\n";
93 for (const auto &opName : context.getRegisteredOperations()) {
94 llvm::outs().indent(2) << opName.getStringRef() << '\n';
95 }
96 llvm::outs() << "}\n";
97 return EXIT_SUCCESS;
98 }
99
100 // Set the include directories from CL option
101 if (mlir::failed(llzk::GlobalSourceMgr::get().setup(IncludeDirs))) {
102 return EXIT_FAILURE;
103 }
104
105 // Run 'mlir-opt'
106 auto result = mlir::MlirOptMain(argc, argv, inputFilename, outputFilename, registry);
107 return mlir::asMainReturnCode(result);
108}
109
110int main(int argc, char **argv) noexcept {
111 try {
112 return runMain(argc, argv);
113 } catch (const std::exception &ex) {
114 llvm::errs() << "llzk-opt: unhandled exception: " << ex.what() << '\n';
115 } catch (...) {
116 llvm::errs() << "llzk-opt: unhandled non-standard exception\n";
117 }
118 return EXIT_FAILURE;
119}
#define LLZK_VERSION_STRING
LLZK version string configured by CMake for this build.
Definition Config.h:13
#define LLZK_URL
Project homepage URL configured by CMake.
Definition Config.h:22
static GlobalSourceMgr & get()
#define BUG_REPORT_URL
Definition config.h:15
int main(int argc, char **argv) noexcept
Definition llzk-opt.cpp:110
Declares dialect and pass registration functions for core LLZK dialects.
void registerDialects(mlir::DialectRegistry &registry)
Register core LLZK dialects, the MLIR arith, scf, and smt dialects, and verification extensions in re...
void registerPasses(mlir::DialectRegistry &registry)
Register LLZK analysis, validation, and transformation passes and pipelines, SMT conversion passes,...