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+
17+ #include < nanobind/nanobind.h>
18+ #include < nanobind/stl/optional.h>
19+ #include < nanobind/stl/string.h>
20+ #include < nanobind/stl/vector.h>
21+
22+ namespace nb = nanobind;
23+
24+ using namespace nanobind ::literals;
1625
17- namespace py = pybind11;
1826using namespace llvm ;
1927using namespace mlir ;
2028using namespace mlir ::python;
21- using namespace mlir ::python::adaptors ;
29+ using namespace mlir ::python::nanobind_adaptors ;
2230
23- void populateDialectLLVMSubmodule (const pybind11:: module &m) {
31+ void populateDialectLLVMSubmodule (const nanobind::module_ &m) {
2432
2533 // ===--------------------------------------------------------------------===//
2634 // StructType
@@ -31,58 +39,58 @@ void populateDialectLLVMSubmodule(const pybind11::module &m) {
3139
3240 llvmStructType.def_classmethod (
3341 " get_literal" ,
34- [](py ::object cls, const std::vector<MlirType> &elements, bool packed,
42+ [](nb ::object cls, const std::vector<MlirType> &elements, bool packed,
3543 MlirLocation loc) {
3644 CollectDiagnosticsToStringScope scope (mlirLocationGetContext (loc));
3745
3846 MlirType type = mlirLLVMStructTypeLiteralGetChecked (
3947 loc, elements.size (), elements.data (), packed);
4048 if (mlirTypeIsNull (type)) {
41- throw py ::value_error (scope.takeMessage ());
49+ throw nb ::value_error (scope.takeMessage (). c_str ());
4250 }
4351 return cls (type);
4452 },
45- " cls" _a, " elements" _a, py ::kw_only (), " packed" _a = false ,
46- " loc" _a = py ::none ());
53+ " cls" _a, " elements" _a, nb ::kw_only (), " packed" _a = false ,
54+ " loc" _a. none () = nb ::none ());
4755
4856 llvmStructType.def_classmethod (
4957 " get_identified" ,
50- [](py ::object cls, const std::string &name, MlirContext context) {
58+ [](nb ::object cls, const std::string &name, MlirContext context) {
5159 return cls (mlirLLVMStructTypeIdentifiedGet (
5260 context, mlirStringRefCreate (name.data (), name.size ())));
5361 },
54- " cls" _a, " name" _a, py ::kw_only (), " context" _a = py ::none ());
62+ " cls" _a, " name" _a, nb ::kw_only (), " context" _a. none () = nb ::none ());
5563
5664 llvmStructType.def_classmethod (
5765 " get_opaque" ,
58- [](py ::object cls, const std::string &name, MlirContext context) {
66+ [](nb ::object cls, const std::string &name, MlirContext context) {
5967 return cls (mlirLLVMStructTypeOpaqueGet (
6068 context, mlirStringRefCreate (name.data (), name.size ())));
6169 },
62- " cls" _a, " name" _a, " context" _a = py ::none ());
70+ " cls" _a, " name" _a, " context" _a. none () = nb ::none ());
6371
6472 llvmStructType.def (
6573 " set_body" ,
6674 [](MlirType self, const std::vector<MlirType> &elements, bool packed) {
6775 MlirLogicalResult result = mlirLLVMStructTypeSetBody (
6876 self, elements.size (), elements.data (), packed);
6977 if (!mlirLogicalResultIsSuccess (result)) {
70- throw py ::value_error (
78+ throw nb ::value_error (
7179 " Struct body already set to different content." );
7280 }
7381 },
74- " elements" _a, py ::kw_only (), " packed" _a = false );
82+ " elements" _a, nb ::kw_only (), " packed" _a = false );
7583
7684 llvmStructType.def_classmethod (
7785 " new_identified" ,
78- [](py ::object cls, const std::string &name,
86+ [](nb ::object cls, const std::string &name,
7987 const std::vector<MlirType> &elements, bool packed, MlirContext ctx) {
8088 return cls (mlirLLVMStructTypeIdentifiedNewGet (
8189 ctx, mlirStringRefCreate (name.data (), name.length ()),
8290 elements.size (), elements.data (), packed));
8391 },
84- " cls" _a, " name" _a, " elements" _a, py ::kw_only (), " packed" _a = false ,
85- " context" _a = py ::none ());
92+ " cls" _a, " name" _a, " elements" _a, nb ::kw_only (), " packed" _a = false ,
93+ " context" _a. none () = nb ::none ());
8694
8795 llvmStructType.def_property_readonly (
8896 " name" , [](MlirType type) -> std::optional<std::string> {
@@ -93,12 +101,12 @@ void populateDialectLLVMSubmodule(const pybind11::module &m) {
93101 return StringRef (stringRef.data , stringRef.length ).str ();
94102 });
95103
96- llvmStructType.def_property_readonly (" body" , [](MlirType type) -> py ::object {
104+ llvmStructType.def_property_readonly (" body" , [](MlirType type) -> nb ::object {
97105 // Don't crash in absence of a body.
98106 if (mlirLLVMStructTypeIsOpaque (type))
99- return py ::none ();
107+ return nb ::none ();
100108
101- py ::list body;
109+ nb ::list body;
102110 for (intptr_t i = 0 , e = mlirLLVMStructTypeGetNumElementTypes (type); i < e;
103111 ++i) {
104112 body.append (mlirLLVMStructTypeGetElementType (type, i));
@@ -119,24 +127,24 @@ void populateDialectLLVMSubmodule(const pybind11::module &m) {
119127 mlir_type_subclass (m, " PointerType" , mlirTypeIsALLVMPointerType)
120128 .def_classmethod (
121129 " get" ,
122- [](py ::object cls, std::optional<unsigned > addressSpace,
130+ [](nb ::object cls, std::optional<unsigned > addressSpace,
123131 MlirContext context) {
124132 CollectDiagnosticsToStringScope scope (context);
125133 MlirType type = mlirLLVMPointerTypeGet (
126134 context, addressSpace.has_value () ? *addressSpace : 0 );
127135 if (mlirTypeIsNull (type)) {
128- throw py ::value_error (scope.takeMessage ());
136+ throw nb ::value_error (scope.takeMessage (). c_str ());
129137 }
130138 return cls (type);
131139 },
132- " cls" _a, " address_space" _a = py ::none (), py ::kw_only (),
133- " context" _a = py ::none ())
140+ " cls" _a, " address_space" _a. none () = nb ::none (), nb ::kw_only (),
141+ " context" _a. none () = nb ::none ())
134142 .def_property_readonly (" address_space" , [](MlirType type) {
135143 return mlirLLVMPointerTypeGetAddressSpace (type);
136144 });
137145}
138146
139- PYBIND11_MODULE (_mlirDialectsLLVM, m) {
147+ NB_MODULE (_mlirDialectsLLVM, m) {
140148 m.doc () = " MLIR LLVM Dialect" ;
141149
142150 populateDialectLLVMSubmodule (m);
0 commit comments