mirror of
https://github.com/zama-ai/concrete.git
synced 2026-02-09 12:15:09 -05:00
feat(python): compiler api with round_trip func
This commit is contained in:
committed by
Quentin Bourgerie
parent
4e6579e019
commit
30d33ee45f
@@ -11,6 +11,7 @@ add_mlir_python_extension(ZamalangBindingsPythonExtension _zamalang
|
||||
SOURCES
|
||||
ZamalangModule.cpp
|
||||
HLFHEModule.cpp
|
||||
CompilerAPIModule.cpp
|
||||
LINK_LIBS
|
||||
ZAMALANGCAPIHLFHE
|
||||
)
|
||||
|
||||
31
compiler/python/CompilerAPIModule.cpp
Normal file
31
compiler/python/CompilerAPIModule.cpp
Normal file
@@ -0,0 +1,31 @@
|
||||
#include "CompilerAPIModule.h"
|
||||
#include "zamalang/Dialect/HLFHE/IR/HLFHEDialect.h"
|
||||
#include "zamalang/Dialect/HLFHE/IR/HLFHETypes.h"
|
||||
#include <mlir/Parser.h>
|
||||
#include <mlir/Dialect/StandardOps/IR/Ops.h>
|
||||
|
||||
#include <pybind11/pybind11.h>
|
||||
#include <pybind11/pytypes.h>
|
||||
#include <pybind11/stl.h>
|
||||
#include <string>
|
||||
|
||||
using namespace zamalang;
|
||||
|
||||
/// Populate the compiler API python module.
|
||||
void zamalang::python::populateCompilerAPISubmodule(pybind11::module &m) {
|
||||
m.doc() = "Zamalang compiler python API";
|
||||
|
||||
m.def("round_trip", [](std::string mlir_input) {
|
||||
mlir::MLIRContext context;
|
||||
context.getOrLoadDialect<mlir::zamalang::HLFHE::HLFHEDialect>();
|
||||
context.getOrLoadDialect<mlir::StandardOpsDialect>();
|
||||
auto mlir_module = mlir::parseSourceString(mlir_input, &context);
|
||||
if (!mlir_module) {
|
||||
throw std::logic_error("mlir parsing failed");
|
||||
}
|
||||
std::string result;
|
||||
llvm::raw_string_ostream os(result);
|
||||
mlir_module->print(os);
|
||||
return os.str();
|
||||
});
|
||||
}
|
||||
14
compiler/python/CompilerAPIModule.h
Normal file
14
compiler/python/CompilerAPIModule.h
Normal file
@@ -0,0 +1,14 @@
|
||||
#ifndef ZAMALANG_PYTHON_COMPILER_API_MODULE_H
|
||||
#define ZAMALANG_PYTHON_COMPILER_API_MODULE_H
|
||||
|
||||
#include <pybind11/pybind11.h>
|
||||
|
||||
namespace zamalang {
|
||||
namespace python {
|
||||
|
||||
void populateCompilerAPISubmodule(pybind11::module &m);
|
||||
|
||||
} // namespace python
|
||||
} // namespace zamalang
|
||||
|
||||
#endif // ZAMALANG_PYTHON_DIALECTMODULES_H
|
||||
@@ -1,4 +1,5 @@
|
||||
#include "DialectModules.h"
|
||||
#include "CompilerAPIModule.h"
|
||||
|
||||
#include "mlir-c/Bindings/Python/Interop.h"
|
||||
#include "mlir-c/Registration.h"
|
||||
@@ -32,4 +33,7 @@ PYBIND11_MODULE(_zamalang, m) {
|
||||
|
||||
py::module hlfhe = m.def_submodule("_hlfhe", "HLFHE API");
|
||||
zamalang::python::populateDialectHLFHESubmodule(hlfhe);
|
||||
|
||||
py::module api = m.def_submodule("_compiler", "Compiler API");
|
||||
zamalang::python::populateCompilerAPISubmodule(api);
|
||||
}
|
||||
@@ -1 +1,2 @@
|
||||
from _zamalang import *
|
||||
import _zamalang._compiler as compiler
|
||||
|
||||
Reference in New Issue
Block a user