12
12
#include " mlir-c/IR.h"
13
13
#include " mlir-c/Support.h"
14
14
#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;
16
25
17
- namespace py = pybind11;
18
26
using namespace llvm ;
19
27
using namespace mlir ;
20
28
using namespace mlir ::python;
21
- using namespace mlir ::python::adaptors ;
29
+ using namespace mlir ::python::nanobind_adaptors ;
22
30
23
- void populateDialectLLVMSubmodule (const pybind11::module &m) {
31
+ void populateDialectLLVMSubmodule (const nanobind::module_ &m) {
24
32
25
33
// ===--------------------------------------------------------------------===//
26
34
// StructType
@@ -31,58 +39,58 @@ void populateDialectLLVMSubmodule(const pybind11::module &m) {
31
39
32
40
llvmStructType.def_classmethod (
33
41
" get_literal" ,
34
- [](py ::object cls, const std::vector<MlirType> &elements, bool packed,
42
+ [](nb ::object cls, const std::vector<MlirType> &elements, bool packed,
35
43
MlirLocation loc) {
36
44
CollectDiagnosticsToStringScope scope (mlirLocationGetContext (loc));
37
45
38
46
MlirType type = mlirLLVMStructTypeLiteralGetChecked (
39
47
loc, elements.size (), elements.data (), packed);
40
48
if (mlirTypeIsNull (type)) {
41
- throw py ::value_error (scope.takeMessage ());
49
+ throw nb ::value_error (scope.takeMessage (). c_str ());
42
50
}
43
51
return cls (type);
44
52
},
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 ());
47
55
48
56
llvmStructType.def_classmethod (
49
57
" get_identified" ,
50
- [](py ::object cls, const std::string &name, MlirContext context) {
58
+ [](nb ::object cls, const std::string &name, MlirContext context) {
51
59
return cls (mlirLLVMStructTypeIdentifiedGet (
52
60
context, mlirStringRefCreate (name.data (), name.size ())));
53
61
},
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 ());
55
63
56
64
llvmStructType.def_classmethod (
57
65
" get_opaque" ,
58
- [](py ::object cls, const std::string &name, MlirContext context) {
66
+ [](nb ::object cls, const std::string &name, MlirContext context) {
59
67
return cls (mlirLLVMStructTypeOpaqueGet (
60
68
context, mlirStringRefCreate (name.data (), name.size ())));
61
69
},
62
- " cls" _a, " name" _a, " context" _a = py ::none ());
70
+ " cls" _a, " name" _a, " context" _a. none () = nb ::none ());
63
71
64
72
llvmStructType.def (
65
73
" set_body" ,
66
74
[](MlirType self, const std::vector<MlirType> &elements, bool packed) {
67
75
MlirLogicalResult result = mlirLLVMStructTypeSetBody (
68
76
self, elements.size (), elements.data (), packed);
69
77
if (!mlirLogicalResultIsSuccess (result)) {
70
- throw py ::value_error (
78
+ throw nb ::value_error (
71
79
" Struct body already set to different content." );
72
80
}
73
81
},
74
- " elements" _a, py ::kw_only (), " packed" _a = false );
82
+ " elements" _a, nb ::kw_only (), " packed" _a = false );
75
83
76
84
llvmStructType.def_classmethod (
77
85
" new_identified" ,
78
- [](py ::object cls, const std::string &name,
86
+ [](nb ::object cls, const std::string &name,
79
87
const std::vector<MlirType> &elements, bool packed, MlirContext ctx) {
80
88
return cls (mlirLLVMStructTypeIdentifiedNewGet (
81
89
ctx, mlirStringRefCreate (name.data (), name.length ()),
82
90
elements.size (), elements.data (), packed));
83
91
},
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 ());
86
94
87
95
llvmStructType.def_property_readonly (
88
96
" name" , [](MlirType type) -> std::optional<std::string> {
@@ -93,12 +101,12 @@ void populateDialectLLVMSubmodule(const pybind11::module &m) {
93
101
return StringRef (stringRef.data , stringRef.length ).str ();
94
102
});
95
103
96
- llvmStructType.def_property_readonly (" body" , [](MlirType type) -> py ::object {
104
+ llvmStructType.def_property_readonly (" body" , [](MlirType type) -> nb ::object {
97
105
// Don't crash in absence of a body.
98
106
if (mlirLLVMStructTypeIsOpaque (type))
99
- return py ::none ();
107
+ return nb ::none ();
100
108
101
- py ::list body;
109
+ nb ::list body;
102
110
for (intptr_t i = 0 , e = mlirLLVMStructTypeGetNumElementTypes (type); i < e;
103
111
++i) {
104
112
body.append (mlirLLVMStructTypeGetElementType (type, i));
@@ -119,24 +127,24 @@ void populateDialectLLVMSubmodule(const pybind11::module &m) {
119
127
mlir_type_subclass (m, " PointerType" , mlirTypeIsALLVMPointerType)
120
128
.def_classmethod (
121
129
" get" ,
122
- [](py ::object cls, std::optional<unsigned > addressSpace,
130
+ [](nb ::object cls, std::optional<unsigned > addressSpace,
123
131
MlirContext context) {
124
132
CollectDiagnosticsToStringScope scope (context);
125
133
MlirType type = mlirLLVMPointerTypeGet (
126
134
context, addressSpace.has_value () ? *addressSpace : 0 );
127
135
if (mlirTypeIsNull (type)) {
128
- throw py ::value_error (scope.takeMessage ());
136
+ throw nb ::value_error (scope.takeMessage (). c_str ());
129
137
}
130
138
return cls (type);
131
139
},
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 ())
134
142
.def_property_readonly (" address_space" , [](MlirType type) {
135
143
return mlirLLVMPointerTypeGetAddressSpace (type);
136
144
});
137
145
}
138
146
139
- PYBIND11_MODULE (_mlirDialectsLLVM, m) {
147
+ NB_MODULE (_mlirDialectsLLVM, m) {
140
148
m.doc () = " MLIR LLVM Dialect" ;
141
149
142
150
populateDialectLLVMSubmodule (m);
0 commit comments