Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ZIR-159: basic rust emit option moved to zirgen repo #19

Draft
wants to merge 2 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions zirgen/compiler/tools/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ cc_binary(
"//zirgen/Dialect/R1CS/Conversion/R1CSToBigInt:passes",
"//zirgen/Dialect/R1CS/IR",
"//zirgen/compiler/r1cs",
"//zirgen/compiler/codegen",
"@llvm-project//mlir:AllTranslations",
"@llvm-project//mlir:MlirOptLib",
],
Expand Down
53 changes: 46 additions & 7 deletions zirgen/compiler/tools/zirgen-r1cs.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
#include "zirgen/Dialect/IOP/IR/IR.h"
#include "zirgen/Dialect/R1CS/Conversion/R1CSToBigInt/Passes.h"
#include "zirgen/Dialect/R1CS/IR/R1CS.h"
#include "zirgen/compiler/codegen/codegen.h"
#include "zirgen/compiler/r1cs/lower.h"
#include "zirgen/compiler/r1cs/r1csfile.h"
#include "zirgen/compiler/r1cs/validate.h"
Expand All @@ -44,15 +45,48 @@ enum Action {
MLIR,
BigInt,
Zll,
Rust,
};
} // namespace

namespace {

std::unique_ptr<llvm::raw_fd_ostream> openOutputFile(llvm::StringRef path, llvm::StringRef name) {
std::string filename = (path + "/" + name).str();
std::error_code ec;
auto ofs = std::make_unique<llvm::raw_fd_ostream>(filename, ec);
if (ec) {
throw std::runtime_error("Unable to open file: " + filename);
}
return ofs;
}

void emitLang(llvm::StringRef langName,
zirgen::codegen::LanguageSyntax* lang,
llvm::StringRef path,
mlir::ModuleOp module) {
zirgen::codegen::CodegenOptions codegenOpts;
codegenOpts.lang = lang;
if (path.empty()) {
llvm::raw_ostream& output = llvm::outs();
zirgen::codegen::CodegenEmitter emitter(codegenOpts, &output, module.getContext());
emitter.emitModule(module);
} else {
auto ofs = openOutputFile(path, ("bigint." + langName + ".inc").str());
zirgen::codegen::CodegenEmitter emitter(codegenOpts, ofs.get(), module.getContext());
emitter.emitModule(module);
}
}

} // namespace

static cl::opt<enum Action>
emitAction("emit",
cl::desc("Desired output"),
cl::values(clEnumValN(MLIR, "mlir", "Plain MLIR representation of R1CS"),
clEnumValN(BigInt, "bigint", "Compute using integers"),
clEnumValN(Zll, "zll", "Lower to ZLL dialect")));
clEnumValN(Zll, "zll", "Lower to ZLL dialect"),
clEnumValN(Rust, "rust", "Generate Rust validation function")));

int main(int argc, char* argv[]) {
llvm::InitLLVM y(argc, argv);
Expand Down Expand Up @@ -123,15 +157,20 @@ int main(int argc, char* argv[]) {
return 0;
}

mlir::PassManager pm2(&context);
pm2.addPass(zirgen::BigInt::createLowerZllPass());
if (mlir::failed(pm2.run(*op))) {
throw std::runtime_error("Failed to apply bigint lowering passes");
}

if (emitAction == Action::Zll) {
mlir::PassManager pm2(&context);
pm2.addPass(zirgen::BigInt::createLowerZllPass());
if (mlir::failed(pm2.run(*op))) {
throw std::runtime_error("Failed to apply bigint lowering passes");
}

op->dump();
return 0;
} else if (emitAction == Action::Rust) {
static zirgen::codegen::RustLanguageSyntax rustLang;
rustLang.addContextArgument("ctx: &mut BigIntContext");
std::string dir = "";
emitLang("rs", &rustLang, dir, *op);
}

return 0;
Expand Down
Loading