20
20
#define MLIR_BINDINGS_PYTHON_NANOBINDADAPTORS_H
21
21
22
22
#include < cstdint>
23
+ #include < optional>
23
24
24
25
#include " mlir-c/Diagnostics.h"
25
26
#include " mlir-c/IR.h"
@@ -43,18 +44,14 @@ namespace detail {
43
44
// / with a raw handle (unowned). The returned object's lifetime may not extend
44
45
// / beyond the apiObject handle without explicitly having its refcount increased
45
46
// / (i.e. on return).
46
- static nanobind::object mlirApiObjectToCapsule (nanobind::handle apiObject) {
47
+ static std::optional<nanobind::object>
48
+ mlirApiObjectToCapsule (nanobind::handle apiObject) {
47
49
if (PyCapsule_CheckExact (apiObject.ptr ()))
48
50
return nanobind::borrow<nanobind::object>(apiObject);
49
51
nanobind::object api =
50
52
nanobind::getattr (apiObject, MLIR_PYTHON_CAPI_PTR_ATTR, nanobind::none ());
51
- if (api.is_none ()) {
52
- std::string repr = nanobind::cast<std::string>(nanobind::repr (apiObject));
53
- throw nanobind::type_error (
54
- (llvm::Twine (" Expected an MLIR object (got " ) + repr + " )." )
55
- .str ()
56
- .c_str ());
57
- }
53
+ if (api.is_none ())
54
+ return {};
58
55
return api;
59
56
}
60
57
@@ -67,12 +64,9 @@ static nanobind::object mlirApiObjectToCapsule(nanobind::handle apiObject) {
67
64
template <>
68
65
struct type_caster <MlirAffineMap> {
69
66
NB_TYPE_CASTER (MlirAffineMap, const_name(" MlirAffineMap" ))
70
- bool from_python (handle src, uint8_t flags, cleanup_list *cleanup) {
71
- nanobind::object capsule = mlirApiObjectToCapsule (src);
72
- value = mlirPythonCapsuleToAffineMap (capsule.ptr ());
73
- if (mlirAffineMapIsNull (value)) {
74
- return false ;
75
- }
67
+ bool from_python (handle src, uint8_t flags, cleanup_list *cleanup) noexcept {
68
+ if (auto capsule = mlirApiObjectToCapsule (src))
69
+ value = mlirPythonCapsuleToAffineMap (capsule->ptr ());
76
70
return !mlirAffineMapIsNull (value);
77
71
}
78
72
static handle from_cpp (MlirAffineMap v, rv_policy,
@@ -90,9 +84,9 @@ struct type_caster<MlirAffineMap> {
90
84
template <>
91
85
struct type_caster <MlirAttribute> {
92
86
NB_TYPE_CASTER (MlirAttribute, const_name(" MlirAttribute" ))
93
- bool from_python (handle src, uint8_t flags, cleanup_list *cleanup) {
94
- nanobind::object capsule = mlirApiObjectToCapsule (src);
95
- value = mlirPythonCapsuleToAttribute (capsule. ptr ());
87
+ bool from_python (handle src, uint8_t flags, cleanup_list *cleanup) noexcept {
88
+ if ( auto capsule = mlirApiObjectToCapsule (src))
89
+ value = mlirPythonCapsuleToAttribute (capsule-> ptr ());
96
90
return !mlirAttributeIsNull (value);
97
91
}
98
92
static handle from_cpp (MlirAttribute v, rv_policy,
@@ -111,9 +105,9 @@ struct type_caster<MlirAttribute> {
111
105
template <>
112
106
struct type_caster <MlirBlock> {
113
107
NB_TYPE_CASTER (MlirBlock, const_name(" MlirBlock" ))
114
- bool from_python (handle src, uint8_t flags, cleanup_list *cleanup) {
115
- nanobind::object capsule = mlirApiObjectToCapsule (src);
116
- value = mlirPythonCapsuleToBlock (capsule. ptr ());
108
+ bool from_python (handle src, uint8_t flags, cleanup_list *cleanup) noexcept {
109
+ if ( auto capsule = mlirApiObjectToCapsule (src))
110
+ value = mlirPythonCapsuleToBlock (capsule-> ptr ());
117
111
return !mlirBlockIsNull (value);
118
112
}
119
113
};
@@ -122,7 +116,7 @@ struct type_caster<MlirBlock> {
122
116
template <>
123
117
struct type_caster <MlirContext> {
124
118
NB_TYPE_CASTER (MlirContext, const_name(" MlirContext" ))
125
- bool from_python (handle src, uint8_t flags, cleanup_list *cleanup) {
119
+ bool from_python (handle src, uint8_t flags, cleanup_list *cleanup) noexcept {
126
120
if (src.is_none ()) {
127
121
// Gets the current thread-bound context.
128
122
// TODO: This raises an error of "No current context" currently.
@@ -132,8 +126,8 @@ struct type_caster<MlirContext> {
132
126
.attr (" Context" )
133
127
.attr (" current" );
134
128
}
135
- nanobind::object capsule = mlirApiObjectToCapsule (src);
136
- value = mlirPythonCapsuleToContext (capsule. ptr ());
129
+ std::optional< nanobind::object> capsule = mlirApiObjectToCapsule (src);
130
+ value = mlirPythonCapsuleToContext (capsule-> ptr ());
137
131
return !mlirContextIsNull (value);
138
132
}
139
133
};
@@ -142,9 +136,9 @@ struct type_caster<MlirContext> {
142
136
template <>
143
137
struct type_caster <MlirDialectRegistry> {
144
138
NB_TYPE_CASTER (MlirDialectRegistry, const_name(" MlirDialectRegistry" ))
145
- bool from_python (handle src, uint8_t flags, cleanup_list *cleanup) {
146
- nanobind::object capsule = mlirApiObjectToCapsule (src);
147
- value = mlirPythonCapsuleToDialectRegistry (capsule. ptr ());
139
+ bool from_python (handle src, uint8_t flags, cleanup_list *cleanup) noexcept {
140
+ if ( auto capsule = mlirApiObjectToCapsule (src))
141
+ value = mlirPythonCapsuleToDialectRegistry (capsule-> ptr ());
148
142
return !mlirDialectRegistryIsNull (value);
149
143
}
150
144
static handle from_cpp (MlirDialectRegistry v, rv_policy,
@@ -162,15 +156,15 @@ struct type_caster<MlirDialectRegistry> {
162
156
template <>
163
157
struct type_caster <MlirLocation> {
164
158
NB_TYPE_CASTER (MlirLocation, const_name(" MlirLocation" ))
165
- bool from_python (handle src, uint8_t flags, cleanup_list *cleanup) {
159
+ bool from_python (handle src, uint8_t flags, cleanup_list *cleanup) noexcept {
166
160
if (src.is_none ()) {
167
161
// Gets the current thread-bound context.
168
162
src = nanobind::module_::import_ (MAKE_MLIR_PYTHON_QUALNAME (" ir" ))
169
163
.attr (" Location" )
170
164
.attr (" current" );
171
165
}
172
- nanobind::object capsule = mlirApiObjectToCapsule (src);
173
- value = mlirPythonCapsuleToLocation (capsule. ptr ());
166
+ if ( auto capsule = mlirApiObjectToCapsule (src))
167
+ value = mlirPythonCapsuleToLocation (capsule-> ptr ());
174
168
return !mlirLocationIsNull (value);
175
169
}
176
170
static handle from_cpp (MlirLocation v, rv_policy,
@@ -188,9 +182,9 @@ struct type_caster<MlirLocation> {
188
182
template <>
189
183
struct type_caster <MlirModule> {
190
184
NB_TYPE_CASTER (MlirModule, const_name(" MlirModule" ))
191
- bool from_python (handle src, uint8_t flags, cleanup_list *cleanup) {
192
- nanobind::object capsule = mlirApiObjectToCapsule (src);
193
- value = mlirPythonCapsuleToModule (capsule. ptr ());
185
+ bool from_python (handle src, uint8_t flags, cleanup_list *cleanup) noexcept {
186
+ if ( auto capsule = mlirApiObjectToCapsule (src))
187
+ value = mlirPythonCapsuleToModule (capsule-> ptr ());
194
188
return !mlirModuleIsNull (value);
195
189
}
196
190
static handle from_cpp (MlirModule v, rv_policy,
@@ -209,12 +203,13 @@ template <>
209
203
struct type_caster <MlirFrozenRewritePatternSet> {
210
204
NB_TYPE_CASTER (MlirFrozenRewritePatternSet,
211
205
const_name (" MlirFrozenRewritePatternSet" ))
212
- bool from_python(handle src, uint8_t flags, cleanup_list *cleanup) {
213
- nanobind::object capsule = mlirApiObjectToCapsule (src);
214
- value = mlirPythonCapsuleToFrozenRewritePatternSet (capsule. ptr ());
206
+ bool from_python(handle src, uint8_t flags, cleanup_list *cleanup) noexcept {
207
+ if ( auto capsule = mlirApiObjectToCapsule (src))
208
+ value = mlirPythonCapsuleToFrozenRewritePatternSet (capsule-> ptr ());
215
209
return value.ptr != nullptr ;
216
210
}
217
- static handle from_cpp (MlirFrozenRewritePatternSet v, rv_policy, handle) {
211
+ static handle from_cpp (MlirFrozenRewritePatternSet v, rv_policy,
212
+ handle) noexcept {
218
213
nanobind::object capsule = nanobind::steal<nanobind::object>(
219
214
mlirPythonFrozenRewritePatternSetToCapsule (v));
220
215
return nanobind::module_::import_ (MAKE_MLIR_PYTHON_QUALNAME (" rewrite" ))
@@ -228,9 +223,9 @@ struct type_caster<MlirFrozenRewritePatternSet> {
228
223
template <>
229
224
struct type_caster <MlirOperation> {
230
225
NB_TYPE_CASTER (MlirOperation, const_name(" MlirOperation" ))
231
- bool from_python (handle src, uint8_t flags, cleanup_list *cleanup) {
232
- nanobind::object capsule = mlirApiObjectToCapsule (src);
233
- value = mlirPythonCapsuleToOperation (capsule. ptr ());
226
+ bool from_python (handle src, uint8_t flags, cleanup_list *cleanup) noexcept {
227
+ if ( auto capsule = mlirApiObjectToCapsule (src))
228
+ value = mlirPythonCapsuleToOperation (capsule-> ptr ());
234
229
return !mlirOperationIsNull (value);
235
230
}
236
231
static handle from_cpp (MlirOperation v, rv_policy,
@@ -250,9 +245,9 @@ struct type_caster<MlirOperation> {
250
245
template <>
251
246
struct type_caster <MlirValue> {
252
247
NB_TYPE_CASTER (MlirValue, const_name(" MlirValue" ))
253
- bool from_python (handle src, uint8_t flags, cleanup_list *cleanup) {
254
- nanobind::object capsule = mlirApiObjectToCapsule (src);
255
- value = mlirPythonCapsuleToValue (capsule. ptr ());
248
+ bool from_python (handle src, uint8_t flags, cleanup_list *cleanup) noexcept {
249
+ if ( auto capsule = mlirApiObjectToCapsule (src))
250
+ value = mlirPythonCapsuleToValue (capsule-> ptr ());
256
251
return !mlirValueIsNull (value);
257
252
}
258
253
static handle from_cpp (MlirValue v, rv_policy,
@@ -273,9 +268,9 @@ struct type_caster<MlirValue> {
273
268
template <>
274
269
struct type_caster <MlirPassManager> {
275
270
NB_TYPE_CASTER (MlirPassManager, const_name(" MlirPassManager" ))
276
- bool from_python (handle src, uint8_t flags, cleanup_list *cleanup) {
277
- nanobind::object capsule = mlirApiObjectToCapsule (src);
278
- value = mlirPythonCapsuleToPassManager (capsule. ptr ());
271
+ bool from_python (handle src, uint8_t flags, cleanup_list *cleanup) noexcept {
272
+ if ( auto capsule = mlirApiObjectToCapsule (src))
273
+ value = mlirPythonCapsuleToPassManager (capsule-> ptr ());
279
274
return !mlirPassManagerIsNull (value);
280
275
}
281
276
};
@@ -284,9 +279,9 @@ struct type_caster<MlirPassManager> {
284
279
template <>
285
280
struct type_caster <MlirTypeID> {
286
281
NB_TYPE_CASTER (MlirTypeID, const_name(" MlirTypeID" ))
287
- bool from_python (handle src, uint8_t flags, cleanup_list *cleanup) {
288
- nanobind::object capsule = mlirApiObjectToCapsule (src);
289
- value = mlirPythonCapsuleToTypeID (capsule. ptr ());
282
+ bool from_python (handle src, uint8_t flags, cleanup_list *cleanup) noexcept {
283
+ if ( auto capsule = mlirApiObjectToCapsule (src))
284
+ value = mlirPythonCapsuleToTypeID (capsule-> ptr ());
290
285
return !mlirTypeIDIsNull (value);
291
286
}
292
287
static handle from_cpp (MlirTypeID v, rv_policy,
@@ -306,9 +301,9 @@ struct type_caster<MlirTypeID> {
306
301
template <>
307
302
struct type_caster <MlirType> {
308
303
NB_TYPE_CASTER (MlirType, const_name(" MlirType" ))
309
- bool from_python (handle src, uint8_t flags, cleanup_list *cleanup) {
310
- nanobind::object capsule = mlirApiObjectToCapsule (src);
311
- value = mlirPythonCapsuleToType (capsule. ptr ());
304
+ bool from_python (handle src, uint8_t flags, cleanup_list *cleanup) noexcept {
305
+ if ( auto capsule = mlirApiObjectToCapsule (src))
306
+ value = mlirPythonCapsuleToType (capsule-> ptr ());
312
307
return !mlirTypeIsNull (value);
313
308
}
314
309
static handle from_cpp (MlirType t, rv_policy,
@@ -462,9 +457,10 @@ class mlir_attribute_subclass : public pure_subclass {
462
457
nanobind::object newCf = nanobind::cpp_function (
463
458
[superCls, isaFunction, captureTypeName](
464
459
nanobind::object cls, nanobind::object otherAttribute) {
465
- MlirAttribute rawAttribute =
466
- nanobind::cast<MlirAttribute>(otherAttribute);
467
- if (!isaFunction (rawAttribute)) {
460
+ MlirAttribute rawAttribute;
461
+ if (!nanobind::try_cast<MlirAttribute>(otherAttribute,
462
+ rawAttribute) ||
463
+ !isaFunction (rawAttribute)) {
468
464
auto origRepr =
469
465
nanobind::cast<std::string>(nanobind::repr (otherAttribute));
470
466
throw std::invalid_argument (
@@ -543,8 +539,9 @@ class mlir_type_subclass : public pure_subclass {
543
539
nanobind::object newCf = nanobind::cpp_function (
544
540
[superCls, isaFunction, captureTypeName](nanobind::object cls,
545
541
nanobind::object otherType) {
546
- MlirType rawType = nanobind::cast<MlirType>(otherType);
547
- if (!isaFunction (rawType)) {
542
+ MlirType rawType;
543
+ if (!nanobind::try_cast<MlirType>(otherType, rawType) ||
544
+ !isaFunction (rawType)) {
548
545
auto origRepr =
549
546
nanobind::cast<std::string>(nanobind::repr (otherType));
550
547
throw std::invalid_argument ((llvm::Twine (" Cannot cast type to " ) +
@@ -625,8 +622,9 @@ class mlir_value_subclass : public pure_subclass {
625
622
nanobind::object newCf = nanobind::cpp_function (
626
623
[superCls, isaFunction, captureValueName](nanobind::object cls,
627
624
nanobind::object otherValue) {
628
- MlirValue rawValue = nanobind::cast<MlirValue>(otherValue);
629
- if (!isaFunction (rawValue)) {
625
+ MlirValue rawValue;
626
+ if (!nanobind::try_cast<MlirValue>(otherValue, rawValue) ||
627
+ !isaFunction (rawValue)) {
630
628
auto origRepr =
631
629
nanobind::cast<std::string>(nanobind::repr (otherValue));
632
630
throw std::invalid_argument ((llvm::Twine (" Cannot cast value to " ) +
0 commit comments