From 4f557f75a1dc50821b7a737c36f1f336bd19c089 Mon Sep 17 00:00:00 2001 From: Mars Saxman Date: Mon, 26 Aug 2024 10:38:29 -0700 Subject: [PATCH 1/2] basic rust emit option moved to zirgen repo --- zirgen/compiler/tools/BUILD.bazel | 1 + zirgen/compiler/tools/zirgen-r1cs.cpp | 53 +++++++++++++++++++++++---- 2 files changed, 47 insertions(+), 7 deletions(-) diff --git a/zirgen/compiler/tools/BUILD.bazel b/zirgen/compiler/tools/BUILD.bazel index b3b30b7b..3e539cfc 100644 --- a/zirgen/compiler/tools/BUILD.bazel +++ b/zirgen/compiler/tools/BUILD.bazel @@ -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", ], diff --git a/zirgen/compiler/tools/zirgen-r1cs.cpp b/zirgen/compiler/tools/zirgen-r1cs.cpp index 5057b32e..21e879d4 100644 --- a/zirgen/compiler/tools/zirgen-r1cs.cpp +++ b/zirgen/compiler/tools/zirgen-r1cs.cpp @@ -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" @@ -44,15 +45,48 @@ enum Action { MLIR, BigInt, Zll, + Rust, }; } // namespace +namespace { + +std::unique_ptr openOutputFile(llvm::StringRef path, llvm::StringRef name) { + std::string filename = (path + "/" + name).str(); + std::error_code ec; + auto ofs = std::make_unique(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 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); @@ -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; From 6ebf7f13d74a9b94523f4d6467a1c2cc18dcde10 Mon Sep 17 00:00:00 2001 From: Mars Saxman Date: Mon, 26 Aug 2024 10:58:32 -0700 Subject: [PATCH 2/2] clang-format --- zirgen/compiler/tools/zirgen-r1cs.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/zirgen/compiler/tools/zirgen-r1cs.cpp b/zirgen/compiler/tools/zirgen-r1cs.cpp index 21e879d4..08b2958a 100644 --- a/zirgen/compiler/tools/zirgen-r1cs.cpp +++ b/zirgen/compiler/tools/zirgen-r1cs.cpp @@ -68,7 +68,7 @@ void emitLang(llvm::StringRef langName, zirgen::codegen::CodegenOptions codegenOpts; codegenOpts.lang = lang; if (path.empty()) { - llvm::raw_ostream &output = llvm::outs(); + llvm::raw_ostream& output = llvm::outs(); zirgen::codegen::CodegenEmitter emitter(codegenOpts, &output, module.getContext()); emitter.emitModule(module); } else {