1212#include " mlir-c/IR.h"
1313#include " mlir-c/Support.h"
1414#include " mlir/Bindings/Python/Diagnostics.h"
15- #include " mlir/Bindings/Python/PybindAdaptors.h"
15+ #include " mlir/Bindings/Python/NanobindAdaptors.h"
16+ #include " mlir/Bindings/Python/Nanobind.h"
17+
18+ namespace nb = nanobind;
19+
20+ using namespace nanobind ::literals;
1621
17- namespace py = pybind11;
1822using namespace llvm ;
1923using namespace mlir ;
2024using namespace mlir ::python;
21- using namespace mlir ::python::adaptors ;
25+ using namespace mlir ::python::nanobind_adaptors ;
2226
23- void populateDialectLLVMSubmodule (const pybind11:: module &m) {
27+ void populateDialectLLVMSubmodule (const nanobind::module_ &m) {
2428
2529 // ===--------------------------------------------------------------------===//
2630 // StructType
@@ -31,58 +35,58 @@ void populateDialectLLVMSubmodule(const pybind11::module &m) {
3135
3236 llvmStructType.def_classmethod (
3337 " get_literal" ,
34- [](py ::object cls, const std::vector<MlirType> &elements, bool packed,
38+ [](nb ::object cls, const std::vector<MlirType> &elements, bool packed,
3539 MlirLocation loc) {
3640 CollectDiagnosticsToStringScope scope (mlirLocationGetContext (loc));
3741
3842 MlirType type = mlirLLVMStructTypeLiteralGetChecked (
3943 loc, elements.size (), elements.data (), packed);
4044 if (mlirTypeIsNull (type)) {
41- throw py ::value_error (scope.takeMessage ());
45+ throw nb ::value_error (scope.takeMessage (). c_str ());
4246 }
4347 return cls (type);
4448 },
45- " cls" _a, " elements" _a, py ::kw_only (), " packed" _a = false ,
46- " loc" _a = py ::none ());
49+ " cls" _a, " elements" _a, nb ::kw_only (), " packed" _a = false ,
50+ " loc" _a. none () = nb ::none ());
4751
4852 llvmStructType.def_classmethod (
4953 " get_identified" ,
50- [](py ::object cls, const std::string &name, MlirContext context) {
54+ [](nb ::object cls, const std::string &name, MlirContext context) {
5155 return cls (mlirLLVMStructTypeIdentifiedGet (
5256 context, mlirStringRefCreate (name.data (), name.size ())));
5357 },
54- " cls" _a, " name" _a, py ::kw_only (), " context" _a = py ::none ());
58+ " cls" _a, " name" _a, nb ::kw_only (), " context" _a. none () = nb ::none ());
5559
5660 llvmStructType.def_classmethod (
5761 " get_opaque" ,
58- [](py ::object cls, const std::string &name, MlirContext context) {
62+ [](nb ::object cls, const std::string &name, MlirContext context) {
5963 return cls (mlirLLVMStructTypeOpaqueGet (
6064 context, mlirStringRefCreate (name.data (), name.size ())));
6165 },
62- " cls" _a, " name" _a, " context" _a = py ::none ());
66+ " cls" _a, " name" _a, " context" _a. none () = nb ::none ());
6367
6468 llvmStructType.def (
6569 " set_body" ,
6670 [](MlirType self, const std::vector<MlirType> &elements, bool packed) {
6771 MlirLogicalResult result = mlirLLVMStructTypeSetBody (
6872 self, elements.size (), elements.data (), packed);
6973 if (!mlirLogicalResultIsSuccess (result)) {
70- throw py ::value_error (
74+ throw nb ::value_error (
7175 " Struct body already set to different content." );
7276 }
7377 },
74- " elements" _a, py ::kw_only (), " packed" _a = false );
78+ " elements" _a, nb ::kw_only (), " packed" _a = false );
7579
7680 llvmStructType.def_classmethod (
7781 " new_identified" ,
78- [](py ::object cls, const std::string &name,
82+ [](nb ::object cls, const std::string &name,
7983 const std::vector<MlirType> &elements, bool packed, MlirContext ctx) {
8084 return cls (mlirLLVMStructTypeIdentifiedNewGet (
8185 ctx, mlirStringRefCreate (name.data (), name.length ()),
8286 elements.size (), elements.data (), packed));
8387 },
84- " cls" _a, " name" _a, " elements" _a, py ::kw_only (), " packed" _a = false ,
85- " context" _a = py ::none ());
88+ " cls" _a, " name" _a, " elements" _a, nb ::kw_only (), " packed" _a = false ,
89+ " context" _a. none () = nb ::none ());
8690
8791 llvmStructType.def_property_readonly (
8892 " name" , [](MlirType type) -> std::optional<std::string> {
@@ -93,12 +97,12 @@ void populateDialectLLVMSubmodule(const pybind11::module &m) {
9397 return StringRef (stringRef.data , stringRef.length ).str ();
9498 });
9599
96- llvmStructType.def_property_readonly (" body" , [](MlirType type) -> py ::object {
100+ llvmStructType.def_property_readonly (" body" , [](MlirType type) -> nb ::object {
97101 // Don't crash in absence of a body.
98102 if (mlirLLVMStructTypeIsOpaque (type))
99- return py ::none ();
103+ return nb ::none ();
100104
101- py ::list body;
105+ nb ::list body;
102106 for (intptr_t i = 0 , e = mlirLLVMStructTypeGetNumElementTypes (type); i < e;
103107 ++i) {
104108 body.append (mlirLLVMStructTypeGetElementType (type, i));
@@ -119,24 +123,24 @@ void populateDialectLLVMSubmodule(const pybind11::module &m) {
119123 mlir_type_subclass (m, " PointerType" , mlirTypeIsALLVMPointerType)
120124 .def_classmethod (
121125 " get" ,
122- [](py ::object cls, std::optional<unsigned > addressSpace,
126+ [](nb ::object cls, std::optional<unsigned > addressSpace,
123127 MlirContext context) {
124128 CollectDiagnosticsToStringScope scope (context);
125129 MlirType type = mlirLLVMPointerTypeGet (
126130 context, addressSpace.has_value () ? *addressSpace : 0 );
127131 if (mlirTypeIsNull (type)) {
128- throw py ::value_error (scope.takeMessage ());
132+ throw nb ::value_error (scope.takeMessage (). c_str ());
129133 }
130134 return cls (type);
131135 },
132- " cls" _a, " address_space" _a = py ::none (), py ::kw_only (),
133- " context" _a = py ::none ())
136+ " cls" _a, " address_space" _a. none () = nb ::none (), nb ::kw_only (),
137+ " context" _a. none () = nb ::none ())
134138 .def_property_readonly (" address_space" , [](MlirType type) {
135139 return mlirLLVMPointerTypeGetAddressSpace (type);
136140 });
137141}
138142
139- PYBIND11_MODULE (_mlirDialectsLLVM, m) {
143+ NB_MODULE (_mlirDialectsLLVM, m) {
140144 m.doc () = " MLIR LLVM Dialect" ;
141145
142146 populateDialectLLVMSubmodule (m);
0 commit comments