1515#include " mlir-c/IR.h"
1616#include " mlir-c/Support.h"
1717#include " mlir/Bindings/Python/Diagnostics.h"
18- #include " mlir/Bindings/Python/PybindAdaptors.h"
18+ #include " mlir/Bindings/Python/NanobindAdaptors.h"
19+ #include " llvm/ADT/Twine.h"
1920
20- #include < pybind11/cast.h>
21- #include < pybind11/detail/common.h>
22- #include < pybind11/pybind11.h>
21+ #include < nanobind/nanobind.h>
2322
2423#include < cstdlib>
2524#include < stdexcept>
2625#include < string>
2726#include < unicodeobject.h>
2827#include < vector>
2928
30- using namespace mlir ::python::adaptors ;
31- namespace py = pybind11 ;
32- using namespace py ::literals;
29+ using namespace mlir ::python;
30+ namespace nb = nanobind ;
31+ using namespace nb ::literals;
3332
34- PYBIND11_MODULE (_aie, m) {
33+ NB_MODULE (_aie, m) {
3534
3635 aieRegisterAllPasses ();
3736
@@ -48,33 +47,35 @@ PYBIND11_MODULE(_aie, m) {
4847 " registry" _a);
4948
5049 // AIE types bindings
51- mlir_type_subclass (m, " ObjectFifoType" , aieTypeIsObjectFifoType)
50+ nanobind_adaptors::mlir_type_subclass (m, " ObjectFifoType" ,
51+ aieTypeIsObjectFifoType)
5252 .def_classmethod (
5353 " get" ,
54- [](const py ::object &cls, const MlirType type) {
54+ [](const nb ::object &cls, const MlirType type) {
5555 return cls (aieObjectFifoTypeGet (type));
5656 },
5757 " Get an instance of ObjectFifoType with given element type." ,
58- " self" _a, " type" _a = py ::none ());
58+ " self" _a, " type" _a = nb ::none ());
5959
60- mlir_type_subclass (m, " ObjectFifoSubviewType" , aieTypeIsObjectFifoSubviewType)
60+ nanobind_adaptors::mlir_type_subclass (m, " ObjectFifoSubviewType" ,
61+ aieTypeIsObjectFifoSubviewType)
6162 .def_classmethod (
6263 " get" ,
63- [](const py ::object &cls, const MlirType type) {
64+ [](const nb ::object &cls, const MlirType type) {
6465 return cls (aieObjectFifoSubviewTypeGet (type));
6566 },
6667 " Get an instance of ObjectFifoSubviewType with given element type." ,
67- " self" _a, " type" _a = py ::none ());
68+ " self" _a, " type" _a = nb ::none ());
6869
6970 auto stealCStr = [](MlirStringRef mlirString) {
7071 if (!mlirString.data || mlirString.length == 0 )
7172 throw std::runtime_error (" couldn't translate" );
7273 std::string cpp (mlirString.data , mlirString.length );
7374 free ((void *)mlirString.data );
74- py ::handle pyS = PyUnicode_DecodeLatin1 (cpp.data (), cpp.length (), nullptr );
75+ nb ::handle pyS = PyUnicode_DecodeLatin1 (cpp.data (), cpp.length (), nullptr );
7576 if (!pyS)
76- throw py::error_already_set ();
77- return py::reinterpret_steal<py ::str>(pyS);
77+ throw nb::python_error ();
78+ return nb::steal<nb ::str>(pyS);
7879 };
7980
8081 m.def (
@@ -101,28 +102,31 @@ PYBIND11_MODULE(_aie, m) {
101102 if (mlirLogicalResultIsFailure (aieTranslateToCDODirect (
102103 op, {workDirPath.data (), workDirPath.size ()}, bigendian,
103104 emitUnified, cdoDebug, aieSim, xaieDebug, enableCores)))
104- throw py::value_error (" Failed to generate cdo because: " +
105- scope.takeMessage ());
105+ throw nb::value_error (
106+ (llvm::Twine (" Failed to generate cdo because: " ) +
107+ llvm::Twine (scope.takeMessage ()))
108+ .str ()
109+ .c_str ());
106110 },
107111 " module" _a, " work_dir_path" _a, " bigendian" _a = false ,
108112 " emit_unified" _a = false , " cdo_debug" _a = false , " aiesim" _a = false ,
109113 " xaie_debug" _a = false , " enable_cores" _a = true );
110114
111115 m.def (
112116 " transaction_binary_to_mlir" ,
113- [](MlirContext ctx, py ::bytes bytes) {
114- std::string s = bytes;
115- MlirStringRef bin = {s. data (), s .size ()};
117+ [](MlirContext ctx, nb ::bytes bytes) {
118+ MlirStringRef bin = { static_cast < const char *>( bytes. data ()),
119+ bytes .size ()};
116120 return aieTranslateBinaryToTxn (ctx, bin);
117121 },
118122 " ctx" _a, " binary" _a);
119123
120124 m.def (
121125 " npu_instgen" ,
122126 [&stealCStr](MlirOperation op) {
123- py ::str npuInstructions = stealCStr (aieTranslateToNPU (op));
127+ nb ::str npuInstructions = stealCStr (aieTranslateToNPU (op));
124128 auto individualInstructions =
125- npuInstructions.attr (" split" )(). cast <py::list>( );
129+ nb::cast<nb::list>( npuInstructions.attr (" split" )());
126130 for (size_t i = 0 ; i < individualInstructions.size (); ++i)
127131 individualInstructions[i] = individualInstructions[i].attr (" strip" )();
128132 return individualInstructions;
@@ -132,10 +136,10 @@ PYBIND11_MODULE(_aie, m) {
132136 m.def (
133137 " generate_control_packets" ,
134138 [&stealCStr](MlirOperation op) {
135- py ::str ctrlPackets =
139+ nb ::str ctrlPackets =
136140 stealCStr (aieTranslateControlPacketsToUI32Vec (op));
137141 auto individualInstructions =
138- ctrlPackets.attr (" split" )(). cast <py::list>( );
142+ nb::cast<nb::list>( ctrlPackets.attr (" split" )());
139143 for (size_t i = 0 ; i < individualInstructions.size (); ++i)
140144 individualInstructions[i] = individualInstructions[i].attr (" strip" )();
141145 return individualInstructions;
@@ -171,7 +175,7 @@ PYBIND11_MODULE(_aie, m) {
171175 m.def (" get_target_model" ,
172176 [](uint32_t d) -> PyAieTargetModel { return aieGetTargetModel (d); });
173177
174- py ::class_<PyAieTargetModel>(m, " AIETargetModel" , py::module_local () )
178+ nb ::class_<PyAieTargetModel>(m, " AIETargetModel" )
175179 .def (
176180 " columns" ,
177181 [](PyAieTargetModel &self) {
0 commit comments