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/Dialect/IR/Dialect.h"
17#include "r1cs/DialectRegistration.h"
18#include "r1cs/Transforms/TransformationPassPipelines.h"
19#include "r1cs/Transforms/TransformationPasses.h"
20#include "smt/Conversions/ConversionPasses.h"
21#include "tools/config.h"
22#include "zklean/Conversions/Passes.h"
23#include "zklean/DialectRegistration.h"
24
26#include "llzk/Config/Config.h"
39
40#include <mlir/Dialect/Func/Extensions/InlinerExtension.h>
41#include <mlir/Dialect/Func/IR/FuncOps.h>
42#include <mlir/Dialect/SCF/IR/SCF.h>
43#include <mlir/IR/DialectRegistry.h>
44#include <mlir/Pass/PassManager.h>
45#include <mlir/Pass/PassRegistry.h>
46#include <mlir/Tools/mlir-opt/MlirOptMain.h>
47#include <mlir/Transforms/Passes.h>
48
49#include <llvm/ADT/StringRef.h>
50#include <llvm/Support/CommandLine.h>
51#include <llvm/Support/PrettyStackTrace.h>
52#include <llvm/Support/Signals.h>
53
54#if LLZK_WITH_PCL
55#include "pcl/Conversion/ConversionPasses.h"
56#include "pcl/Dialect/IR/Dialect.h"
57#include "pcl/DialectRegistration.h"
58#endif // LLZK_WITH_PCL
59
60static llvm::cl::list<std::string> IncludeDirs(
61 "I", llvm::cl::desc("Directory of include files"), llvm::cl::value_desc("directory"),
62 llvm::cl::Prefix
63);
64
65static llvm::cl::opt<bool>
66 PrintAllOps("print-llzk-ops", llvm::cl::desc("Print a list of all ops registered in LLZK"));
67
71namespace mlir_hotfix {
72
73inline static void registerTransformsPasses() {
74 mlir::registerCSE();
75 mlir::registerCanonicalizer();
76 mlir::registerCompositeFixedPointPass();
77 mlir::registerControlFlowSink();
78 mlir::registerGenerateRuntimeVerification();
79 mlir::registerInliner();
80 mlir::registerLocationSnapshot();
81 mlir::registerLoopInvariantCodeMotion();
82 mlir::registerLoopInvariantSubsetHoisting();
83 mlir::registerMem2Reg();
84 mlir::registerPrintIRPass();
85 mlir::registerPrintOpStats();
87 mlir::registerSCCP();
88 mlir::registerSROA();
89 mlir::registerStripDebugInfo();
90 mlir::registerSymbolDCE();
91 mlir::registerSymbolPrivatize();
92 mlir::registerTopologicalSort();
93 mlir::registerViewOpGraph();
94}
95
96} // namespace mlir_hotfix
97
98int main(int argc, char **argv) {
99 llvm::sys::PrintStackTraceOnErrorSignal(llvm::StringRef());
100 llvm::setBugReportMsg(
101 "PLEASE submit a bug report to " BUG_REPORT_URL
102 " and include the crash backtrace, relevant LLZK files,"
103 " and associated run script(s).\n"
104 );
105 llvm::cl::AddExtraVersionPrinter([](llvm::raw_ostream &os) {
106 os << "\nLLZK (" LLZK_URL "):\n LLZK version " LLZK_VERSION_STRING "\n";
107 });
108
109 // MLIR initialization
110 mlir::DialectRegistry registry;
111 // registers CSE, etc
112 mlir_hotfix::registerTransformsPasses();
114 r1cs::registerAllDialects(registry);
115 zklean::registerAllDialects(registry);
116 mlir::func::registerInlinerExtension(registry);
117#if LLZK_WITH_PCL
118 pcl::registerAllDialects(registry);
119#endif // LLZK_WITH_PCL
120
130 r1cs::registerTransformationPasses();
131 zklean::registerConversionPasses();
132#if LLZK_WITH_PCL
133 pcl::registerPCLConversionPasses();
134#endif // LLZK_WITH_PCL
135 llzk::smt::registerConversionPasses();
136
138 r1cs::registerTransformationPassPipelines();
139
140 // Register and parse command line options.
141 std::string inputFilename, outputFilename;
142 std::tie(inputFilename, outputFilename) =
143 registerAndParseCLIOptions(argc, argv, "llzk-opt", registry);
144
145 if (PrintAllOps) {
146 mlir::MLIRContext context;
147 context.appendDialectRegistry(registry);
148 context.loadAllAvailableDialects();
149 llvm::outs() << "All ops registered in LLZK IR: {\n";
150 for (const auto &opName : context.getRegisteredOperations()) {
151 llvm::outs().indent(2) << opName.getStringRef() << '\n';
152 }
153 llvm::outs() << "}\n";
154 return EXIT_SUCCESS;
155 }
156
157 // Set the include directories from CL option
158 if (mlir::failed(llzk::GlobalSourceMgr::get().setup(IncludeDirs))) {
159 return EXIT_FAILURE;
160 }
161
162 // Run 'mlir-opt'
163 auto result = mlir::MlirOptMain(argc, argv, inputFilename, outputFilename, registry);
164 return mlir::asMainReturnCode(result);
165}
#define LLZK_VERSION_STRING
Definition Config.h:12
#define LLZK_URL
Definition Config.h:17
Provides SpecializedSROA<AllocOpTy> and SpecializedMem2Reg<AllocOpTy>: pass templates that replicate ...
static GlobalSourceMgr & get()
#define BUG_REPORT_URL
Definition config.h:15
int main(int argc, char **argv)
Definition llzk-opt.cpp:98
This file defines llzk::registerAllDialects.
void registerTransformationPasses()
void registerTransformationPasses()
void registerTransformationPasses()
void registerTransformationPasses()
std::unique_ptr< mlir::Pass > createRemoveDeadValuesWorkaroundPass()
void registerValidationPasses()
void registerAllDialects(mlir::DialectRegistry &registry)
void registerAnalysisPasses()
Replace mlir::registerTransformsPasses() to register a custom remove-dead-values pass because MLIR ve...
Definition llzk-opt.cpp:71