MLIR  18.0.0git
PybindAdaptors.h
Go to the documentation of this file.
1 //===- PybindAdaptors.h - Adaptors for interop with MLIR APIs -------------===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 // This file contains adaptors for clients of the core MLIR Python APIs to
9 // interop via MLIR CAPI types. The facilities here do not depend on
10 // implementation details of the MLIR Python API and do not introduce C++-level
11 // dependencies with it (requiring only Python and CAPI-level dependencies).
12 //
13 // It is encouraged to be used both in-tree and out-of-tree. For in-tree use
14 // cases, it should be used for dialect implementations (versus relying on
15 // Pybind-based internals of the core libraries).
16 //===----------------------------------------------------------------------===//
17 
18 #ifndef MLIR_BINDINGS_PYTHON_PYBINDADAPTORS_H
19 #define MLIR_BINDINGS_PYTHON_PYBINDADAPTORS_H
20 
21 #include <pybind11/pybind11.h>
22 #include <pybind11/pytypes.h>
23 #include <pybind11/stl.h>
24 
26 #include "mlir-c/IR.h"
27 
28 #include "llvm/ADT/Twine.h"
29 
30 namespace py = pybind11;
31 using namespace py::literals;
32 
33 // Raw CAPI type casters need to be declared before use, so always include them
34 // first.
35 namespace pybind11 {
36 namespace detail {
37 
38 /// Helper to convert a presumed MLIR API object to a capsule, accepting either
39 /// an explicit Capsule (which can happen when two C APIs are communicating
40 /// directly via Python) or indirectly by querying the MLIR_PYTHON_CAPI_PTR_ATTR
41 /// attribute (through which supported MLIR Python API objects export their
42 /// contained API pointer as a capsule). Throws a type error if the object is
43 /// neither. This is intended to be used from type casters, which are invoked
44 /// with a raw handle (unowned). The returned object's lifetime may not extend
45 /// beyond the apiObject handle without explicitly having its refcount increased
46 /// (i.e. on return).
47 static py::object mlirApiObjectToCapsule(py::handle apiObject) {
48  if (PyCapsule_CheckExact(apiObject.ptr()))
49  return py::reinterpret_borrow<py::object>(apiObject);
50  if (!py::hasattr(apiObject, MLIR_PYTHON_CAPI_PTR_ATTR)) {
51  auto repr = py::repr(apiObject).cast<std::string>();
52  throw py::type_error(
53  (llvm::Twine("Expected an MLIR object (got ") + repr + ").").str());
54  }
55  return apiObject.attr(MLIR_PYTHON_CAPI_PTR_ATTR);
56 }
57 
58 // Note: Currently all of the following support cast from py::object to the
59 // Mlir* C-API type, but only a few light-weight, context-bound ones
60 // implicitly cast the other way because the use case has not yet emerged and
61 // ownership is unclear.
62 
63 /// Casts object <-> MlirAffineMap.
64 template <>
65 struct type_caster<MlirAffineMap> {
66  PYBIND11_TYPE_CASTER(MlirAffineMap, _("MlirAffineMap"));
67  bool load(handle src, bool) {
68  py::object capsule = mlirApiObjectToCapsule(src);
69  value = mlirPythonCapsuleToAffineMap(capsule.ptr());
70  if (mlirAffineMapIsNull(value)) {
71  return false;
72  }
73  return !mlirAffineMapIsNull(value);
74  }
75  static handle cast(MlirAffineMap v, return_value_policy, handle) {
76  py::object capsule =
77  py::reinterpret_steal<py::object>(mlirPythonAffineMapToCapsule(v));
78  return py::module::import(MAKE_MLIR_PYTHON_QUALNAME("ir"))
79  .attr("AffineMap")
80  .attr(MLIR_PYTHON_CAPI_FACTORY_ATTR)(capsule)
81  .release();
82  }
83 };
84 
85 /// Casts object <-> MlirAttribute.
86 template <>
87 struct type_caster<MlirAttribute> {
88  PYBIND11_TYPE_CASTER(MlirAttribute, _("MlirAttribute"));
89  bool load(handle src, bool) {
90  py::object capsule = mlirApiObjectToCapsule(src);
91  value = mlirPythonCapsuleToAttribute(capsule.ptr());
92  return !mlirAttributeIsNull(value);
93  }
94  static handle cast(MlirAttribute v, return_value_policy, handle) {
95  py::object capsule =
96  py::reinterpret_steal<py::object>(mlirPythonAttributeToCapsule(v));
97  return py::module::import(MAKE_MLIR_PYTHON_QUALNAME("ir"))
98  .attr("Attribute")
99  .attr(MLIR_PYTHON_CAPI_FACTORY_ATTR)(capsule)
101  .release();
102  }
103 };
104 
105 /// Casts object -> MlirBlock.
106 template <>
107 struct type_caster<MlirBlock> {
108  PYBIND11_TYPE_CASTER(MlirBlock, _("MlirBlock"));
109  bool load(handle src, bool) {
110  py::object capsule = mlirApiObjectToCapsule(src);
111  value = mlirPythonCapsuleToBlock(capsule.ptr());
112  return !mlirBlockIsNull(value);
113  }
114 };
115 
116 /// Casts object -> MlirContext.
117 template <>
118 struct type_caster<MlirContext> {
119  PYBIND11_TYPE_CASTER(MlirContext, _("MlirContext"));
120  bool load(handle src, bool) {
121  if (src.is_none()) {
122  // Gets the current thread-bound context.
123  // TODO: This raises an error of "No current context" currently.
124  // Update the implementation to pretty-print the helpful error that the
125  // core implementations print in this case.
126  src = py::module::import(MAKE_MLIR_PYTHON_QUALNAME("ir"))
127  .attr("Context")
128  .attr("current");
129  }
130  py::object capsule = mlirApiObjectToCapsule(src);
131  value = mlirPythonCapsuleToContext(capsule.ptr());
132  return !mlirContextIsNull(value);
133  }
134 };
135 
136 /// Casts object <-> MlirDialectRegistry.
137 template <>
138 struct type_caster<MlirDialectRegistry> {
139  PYBIND11_TYPE_CASTER(MlirDialectRegistry, _("MlirDialectRegistry"));
140  bool load(handle src, bool) {
141  py::object capsule = mlirApiObjectToCapsule(src);
142  value = mlirPythonCapsuleToDialectRegistry(capsule.ptr());
143  return !mlirDialectRegistryIsNull(value);
144  }
145  static handle cast(MlirDialectRegistry v, return_value_policy, handle) {
146  py::object capsule = py::reinterpret_steal<py::object>(
148  return py::module::import(MAKE_MLIR_PYTHON_QUALNAME("ir"))
149  .attr("DialectRegistry")
150  .attr(MLIR_PYTHON_CAPI_FACTORY_ATTR)(capsule)
151  .release();
152  }
153 };
154 
155 /// Casts object <-> MlirLocation.
156 template <>
157 struct type_caster<MlirLocation> {
158  PYBIND11_TYPE_CASTER(MlirLocation, _("MlirLocation"));
159  bool load(handle src, bool) {
160  if (src.is_none()) {
161  // Gets the current thread-bound context.
162  src = py::module::import(MAKE_MLIR_PYTHON_QUALNAME("ir"))
163  .attr("Location")
164  .attr("current");
165  }
166  py::object capsule = mlirApiObjectToCapsule(src);
167  value = mlirPythonCapsuleToLocation(capsule.ptr());
168  return !mlirLocationIsNull(value);
169  }
170  static handle cast(MlirLocation v, return_value_policy, handle) {
171  py::object capsule =
172  py::reinterpret_steal<py::object>(mlirPythonLocationToCapsule(v));
173  return py::module::import(MAKE_MLIR_PYTHON_QUALNAME("ir"))
174  .attr("Location")
175  .attr(MLIR_PYTHON_CAPI_FACTORY_ATTR)(capsule)
176  .release();
177  }
178 };
179 
180 /// Casts object <-> MlirModule.
181 template <>
182 struct type_caster<MlirModule> {
183  PYBIND11_TYPE_CASTER(MlirModule, _("MlirModule"));
184  bool load(handle src, bool) {
185  py::object capsule = mlirApiObjectToCapsule(src);
186  value = mlirPythonCapsuleToModule(capsule.ptr());
187  return !mlirModuleIsNull(value);
188  }
189  static handle cast(MlirModule v, return_value_policy, handle) {
190  py::object capsule =
191  py::reinterpret_steal<py::object>(mlirPythonModuleToCapsule(v));
192  return py::module::import(MAKE_MLIR_PYTHON_QUALNAME("ir"))
193  .attr("Module")
194  .attr(MLIR_PYTHON_CAPI_FACTORY_ATTR)(capsule)
195  .release();
196  };
197 };
198 
199 /// Casts object <-> MlirOperation.
200 template <>
201 struct type_caster<MlirOperation> {
202  PYBIND11_TYPE_CASTER(MlirOperation, _("MlirOperation"));
203  bool load(handle src, bool) {
204  py::object capsule = mlirApiObjectToCapsule(src);
205  value = mlirPythonCapsuleToOperation(capsule.ptr());
206  return !mlirOperationIsNull(value);
207  }
208  static handle cast(MlirOperation v, return_value_policy, handle) {
209  if (v.ptr == nullptr)
210  return py::none();
211  py::object capsule =
212  py::reinterpret_steal<py::object>(mlirPythonOperationToCapsule(v));
213  return py::module::import(MAKE_MLIR_PYTHON_QUALNAME("ir"))
214  .attr("Operation")
215  .attr(MLIR_PYTHON_CAPI_FACTORY_ATTR)(capsule)
216  .release();
217  };
218 };
219 
220 /// Casts object <-> MlirValue.
221 template <>
222 struct type_caster<MlirValue> {
223  PYBIND11_TYPE_CASTER(MlirValue, _("MlirValue"));
224  bool load(handle src, bool) {
225  py::object capsule = mlirApiObjectToCapsule(src);
226  value = mlirPythonCapsuleToValue(capsule.ptr());
227  return !mlirValueIsNull(value);
228  }
229  static handle cast(MlirValue v, return_value_policy, handle) {
230  if (v.ptr == nullptr)
231  return py::none();
232  py::object capsule =
233  py::reinterpret_steal<py::object>(mlirPythonValueToCapsule(v));
234  return py::module::import(MAKE_MLIR_PYTHON_QUALNAME("ir"))
235  .attr("Value")
236  .attr(MLIR_PYTHON_CAPI_FACTORY_ATTR)(capsule)
238  .release();
239  };
240 };
241 
242 /// Casts object -> MlirPassManager.
243 template <>
244 struct type_caster<MlirPassManager> {
245  PYBIND11_TYPE_CASTER(MlirPassManager, _("MlirPassManager"));
246  bool load(handle src, bool) {
247  py::object capsule = mlirApiObjectToCapsule(src);
248  value = mlirPythonCapsuleToPassManager(capsule.ptr());
249  return !mlirPassManagerIsNull(value);
250  }
251 };
252 
253 /// Casts object <-> MlirTypeID.
254 template <>
255 struct type_caster<MlirTypeID> {
256  PYBIND11_TYPE_CASTER(MlirTypeID, _("MlirTypeID"));
257  bool load(handle src, bool) {
258  py::object capsule = mlirApiObjectToCapsule(src);
259  value = mlirPythonCapsuleToTypeID(capsule.ptr());
260  return !mlirTypeIDIsNull(value);
261  }
262  static handle cast(MlirTypeID v, return_value_policy, handle) {
263  if (v.ptr == nullptr)
264  return py::none();
265  py::object capsule =
266  py::reinterpret_steal<py::object>(mlirPythonTypeIDToCapsule(v));
267  return py::module::import(MAKE_MLIR_PYTHON_QUALNAME("ir"))
268  .attr("TypeID")
269  .attr(MLIR_PYTHON_CAPI_FACTORY_ATTR)(capsule)
270  .release();
271  };
272 };
273 
274 /// Casts object <-> MlirType.
275 template <>
276 struct type_caster<MlirType> {
277  PYBIND11_TYPE_CASTER(MlirType, _("MlirType"));
278  bool load(handle src, bool) {
279  py::object capsule = mlirApiObjectToCapsule(src);
280  value = mlirPythonCapsuleToType(capsule.ptr());
281  return !mlirTypeIsNull(value);
282  }
283  static handle cast(MlirType t, return_value_policy, handle) {
284  py::object capsule =
285  py::reinterpret_steal<py::object>(mlirPythonTypeToCapsule(t));
286  return py::module::import(MAKE_MLIR_PYTHON_QUALNAME("ir"))
287  .attr("Type")
288  .attr(MLIR_PYTHON_CAPI_FACTORY_ATTR)(capsule)
290  .release();
291  }
292 };
293 
294 } // namespace detail
295 } // namespace pybind11
296 
297 namespace mlir {
298 namespace python {
299 namespace adaptors {
300 
301 /// Provides a facility like py::class_ for defining a new class in a scope,
302 /// but this allows extension of an arbitrary Python class, defining methods
303 /// on it is a similar way. Classes defined in this way are very similar to
304 /// if defined in Python in the usual way but use Pybind11 machinery to do
305 /// it. These are not "real" Pybind11 classes but pure Python classes with no
306 /// relation to a concrete C++ class.
307 ///
308 /// Derived from a discussion upstream:
309 /// https://github.com/pybind/pybind11/issues/1193
310 /// (plus a fair amount of extra curricular poking)
311 /// TODO: If this proves useful, see about including it in pybind11.
313 public:
314  pure_subclass(py::handle scope, const char *derivedClassName,
315  const py::object &superClass) {
316  py::object pyType =
317  py::reinterpret_borrow<py::object>((PyObject *)&PyType_Type);
318  py::object metaclass = pyType(superClass);
319  py::dict attributes;
320 
321  thisClass =
322  metaclass(derivedClassName, py::make_tuple(superClass), attributes);
323  scope.attr(derivedClassName) = thisClass;
324  }
325 
326  template <typename Func, typename... Extra>
327  pure_subclass &def(const char *name, Func &&f, const Extra &...extra) {
328  py::cpp_function cf(
329  std::forward<Func>(f), py::name(name), py::is_method(thisClass),
330  py::sibling(py::getattr(thisClass, name, py::none())), extra...);
331  thisClass.attr(cf.name()) = cf;
332  return *this;
333  }
334 
335  template <typename Func, typename... Extra>
336  pure_subclass &def_property_readonly(const char *name, Func &&f,
337  const Extra &...extra) {
338  py::cpp_function cf(
339  std::forward<Func>(f), py::name(name), py::is_method(thisClass),
340  py::sibling(py::getattr(thisClass, name, py::none())), extra...);
341  auto builtinProperty =
342  py::reinterpret_borrow<py::object>((PyObject *)&PyProperty_Type);
343  thisClass.attr(name) = builtinProperty(cf);
344  return *this;
345  }
346 
347  template <typename Func, typename... Extra>
348  pure_subclass &def_staticmethod(const char *name, Func &&f,
349  const Extra &...extra) {
350  static_assert(!std::is_member_function_pointer<Func>::value,
351  "def_staticmethod(...) called with a non-static member "
352  "function pointer");
353  py::cpp_function cf(
354  std::forward<Func>(f), py::name(name), py::scope(thisClass),
355  py::sibling(py::getattr(thisClass, name, py::none())), extra...);
356  thisClass.attr(cf.name()) = py::staticmethod(cf);
357  return *this;
358  }
359 
360  template <typename Func, typename... Extra>
361  pure_subclass &def_classmethod(const char *name, Func &&f,
362  const Extra &...extra) {
363  static_assert(!std::is_member_function_pointer<Func>::value,
364  "def_classmethod(...) called with a non-static member "
365  "function pointer");
366  py::cpp_function cf(
367  std::forward<Func>(f), py::name(name), py::scope(thisClass),
368  py::sibling(py::getattr(thisClass, name, py::none())), extra...);
369  thisClass.attr(cf.name()) =
370  py::reinterpret_borrow<py::object>(PyClassMethod_New(cf.ptr()));
371  return *this;
372  }
373 
374  py::object get_class() const { return thisClass; }
375 
376 protected:
377  py::object superClass;
378  py::object thisClass;
379 };
380 
381 /// Creates a custom subclass of mlir.ir.Attribute, implementing a casting
382 /// constructor and type checking methods.
384 public:
385  using IsAFunctionTy = bool (*)(MlirAttribute);
386 
387  /// Subclasses by looking up the super-class dynamically.
388  mlir_attribute_subclass(py::handle scope, const char *attrClassName,
389  IsAFunctionTy isaFunction)
391  scope, attrClassName, isaFunction,
392  py::module::import(MAKE_MLIR_PYTHON_QUALNAME("ir"))
393  .attr("Attribute")) {}
394 
395  /// Subclasses with a provided mlir.ir.Attribute super-class. This must
396  /// be used if the subclass is being defined in the same extension module
397  /// as the mlir.ir class (otherwise, it will trigger a recursive
398  /// initialization).
399  mlir_attribute_subclass(py::handle scope, const char *typeClassName,
400  IsAFunctionTy isaFunction, const py::object &superCls)
401  : pure_subclass(scope, typeClassName, superCls) {
402  // Casting constructor. Note that it hard, if not impossible, to properly
403  // call chain to parent `__init__` in pybind11 due to its special handling
404  // for init functions that don't have a fully constructed self-reference,
405  // which makes it impossible to forward it to `__init__` of a superclass.
406  // Instead, provide a custom `__new__` and call that of a superclass, which
407  // eventually calls `__init__` of the superclass. Since attribute subclasses
408  // have no additional members, we can just return the instance thus created
409  // without amending it.
410  std::string captureTypeName(
411  typeClassName); // As string in case if typeClassName is not static.
412  py::cpp_function newCf(
413  [superCls, isaFunction, captureTypeName](py::object cls,
414  py::object otherAttribute) {
415  MlirAttribute rawAttribute = py::cast<MlirAttribute>(otherAttribute);
416  if (!isaFunction(rawAttribute)) {
417  auto origRepr = py::repr(otherAttribute).cast<std::string>();
418  throw std::invalid_argument(
419  (llvm::Twine("Cannot cast attribute to ") + captureTypeName +
420  " (from " + origRepr + ")")
421  .str());
422  }
423  py::object self = superCls.attr("__new__")(cls, otherAttribute);
424  return self;
425  },
426  py::name("__new__"), py::arg("cls"), py::arg("cast_from_attr"));
427  thisClass.attr("__new__") = newCf;
428 
429  // 'isinstance' method.
430  def_staticmethod(
431  "isinstance",
432  [isaFunction](MlirAttribute other) { return isaFunction(other); },
433  py::arg("other_attribute"));
434  }
435 };
436 
437 /// Creates a custom subclass of mlir.ir.Type, implementing a casting
438 /// constructor and type checking methods.
440 public:
441  using IsAFunctionTy = bool (*)(MlirType);
442  using GetTypeIDFunctionTy = MlirTypeID (*)();
443 
444  /// Subclasses by looking up the super-class dynamically.
445  mlir_type_subclass(py::handle scope, const char *typeClassName,
446  IsAFunctionTy isaFunction,
447  GetTypeIDFunctionTy getTypeIDFunction = nullptr)
449  scope, typeClassName, isaFunction,
450  py::module::import(MAKE_MLIR_PYTHON_QUALNAME("ir")).attr("Type"),
451  getTypeIDFunction) {}
452 
453  /// Subclasses with a provided mlir.ir.Type super-class. This must
454  /// be used if the subclass is being defined in the same extension module
455  /// as the mlir.ir class (otherwise, it will trigger a recursive
456  /// initialization).
457  mlir_type_subclass(py::handle scope, const char *typeClassName,
458  IsAFunctionTy isaFunction, const py::object &superCls,
459  GetTypeIDFunctionTy getTypeIDFunction = nullptr)
460  : pure_subclass(scope, typeClassName, superCls) {
461  // Casting constructor. Note that it hard, if not impossible, to properly
462  // call chain to parent `__init__` in pybind11 due to its special handling
463  // for init functions that don't have a fully constructed self-reference,
464  // which makes it impossible to forward it to `__init__` of a superclass.
465  // Instead, provide a custom `__new__` and call that of a superclass, which
466  // eventually calls `__init__` of the superclass. Since attribute subclasses
467  // have no additional members, we can just return the instance thus created
468  // without amending it.
469  std::string captureTypeName(
470  typeClassName); // As string in case if typeClassName is not static.
471  py::cpp_function newCf(
472  [superCls, isaFunction, captureTypeName](py::object cls,
473  py::object otherType) {
474  MlirType rawType = py::cast<MlirType>(otherType);
475  if (!isaFunction(rawType)) {
476  auto origRepr = py::repr(otherType).cast<std::string>();
477  throw std::invalid_argument((llvm::Twine("Cannot cast type to ") +
478  captureTypeName + " (from " +
479  origRepr + ")")
480  .str());
481  }
482  py::object self = superCls.attr("__new__")(cls, otherType);
483  return self;
484  },
485  py::name("__new__"), py::arg("cls"), py::arg("cast_from_type"));
486  thisClass.attr("__new__") = newCf;
487 
488  // 'isinstance' method.
489  def_staticmethod(
490  "isinstance",
491  [isaFunction](MlirType other) { return isaFunction(other); },
492  py::arg("other_type"));
493  def("__repr__", [superCls, captureTypeName](py::object self) {
494  return py::repr(superCls(self))
495  .attr("replace")(superCls.attr("__name__"), captureTypeName);
496  });
497  if (getTypeIDFunction) {
498  py::module::import(MAKE_MLIR_PYTHON_QUALNAME("ir"))
500  getTypeIDFunction())(pybind11::cpp_function(
501  [thisClass = thisClass](const py::object &mlirType) {
502  return thisClass(mlirType);
503  }));
504  }
505  }
506 };
507 
508 /// Creates a custom subclass of mlir.ir.Value, implementing a casting
509 /// constructor and type checking methods.
511 public:
512  using IsAFunctionTy = bool (*)(MlirValue);
513 
514  /// Subclasses by looking up the super-class dynamically.
515  mlir_value_subclass(py::handle scope, const char *valueClassName,
516  IsAFunctionTy isaFunction)
518  scope, valueClassName, isaFunction,
519  py::module::import(MAKE_MLIR_PYTHON_QUALNAME("ir")).attr("Value")) {
520  }
521 
522  /// Subclasses with a provided mlir.ir.Value super-class. This must
523  /// be used if the subclass is being defined in the same extension module
524  /// as the mlir.ir class (otherwise, it will trigger a recursive
525  /// initialization).
526  mlir_value_subclass(py::handle scope, const char *valueClassName,
527  IsAFunctionTy isaFunction, const py::object &superCls)
528  : pure_subclass(scope, valueClassName, superCls) {
529  // Casting constructor. Note that it hard, if not impossible, to properly
530  // call chain to parent `__init__` in pybind11 due to its special handling
531  // for init functions that don't have a fully constructed self-reference,
532  // which makes it impossible to forward it to `__init__` of a superclass.
533  // Instead, provide a custom `__new__` and call that of a superclass, which
534  // eventually calls `__init__` of the superclass. Since attribute subclasses
535  // have no additional members, we can just return the instance thus created
536  // without amending it.
537  std::string captureValueName(
538  valueClassName); // As string in case if valueClassName is not static.
539  py::cpp_function newCf(
540  [superCls, isaFunction, captureValueName](py::object cls,
541  py::object otherValue) {
542  MlirValue rawValue = py::cast<MlirValue>(otherValue);
543  if (!isaFunction(rawValue)) {
544  auto origRepr = py::repr(otherValue).cast<std::string>();
545  throw std::invalid_argument((llvm::Twine("Cannot cast value to ") +
546  captureValueName + " (from " +
547  origRepr + ")")
548  .str());
549  }
550  py::object self = superCls.attr("__new__")(cls, otherValue);
551  return self;
552  },
553  py::name("__new__"), py::arg("cls"), py::arg("cast_from_value"));
554  thisClass.attr("__new__") = newCf;
555 
556  // 'isinstance' method.
557  def_staticmethod(
558  "isinstance",
559  [isaFunction](MlirValue other) { return isaFunction(other); },
560  py::arg("other_value"));
561  }
562 };
563 
564 } // namespace adaptors
565 } // namespace python
566 } // namespace mlir
567 
568 #endif // MLIR_BINDINGS_PYTHON_PYBINDADAPTORS_H
static PyObject * mlirPythonModuleToCapsule(MlirModule module)
Creates a capsule object encapsulating the raw C-API MlirModule.
Definition: Interop.h:272
#define MLIR_PYTHON_MAYBE_DOWNCAST_ATTR
Attribute on MLIR Python objects that expose a function for downcasting the corresponding Python obje...
Definition: Interop.h:117
static MlirBlock mlirPythonCapsuleToBlock(PyObject *capsule)
Extracts an MlirBlock from a capsule as produced from mlirPythonBlockToCapsule.
Definition: Interop.h:205
static PyObject * mlirPythonTypeIDToCapsule(MlirTypeID typeID)
Creates a capsule object encapsulating the raw C-API MlirTypeID.
Definition: Interop.h:327
static MlirOperation mlirPythonCapsuleToOperation(PyObject *capsule)
Extracts an MlirOperations from a capsule as produced from mlirPythonOperationToCapsule.
Definition: Interop.h:317
#define MLIR_PYTHON_CAPI_PTR_ATTR
Attribute on MLIR Python objects that expose their C-API pointer.
Definition: Interop.h:96
static MlirAttribute mlirPythonCapsuleToAttribute(PyObject *capsule)
Extracts an MlirAttribute from a capsule as produced from mlirPythonAttributeToCapsule.
Definition: Interop.h:188
static PyObject * mlirPythonAttributeToCapsule(MlirAttribute attribute)
Creates a capsule object encapsulating the raw C-API MlirAttribute.
Definition: Interop.h:179
static PyObject * mlirPythonLocationToCapsule(MlirLocation loc)
Creates a capsule object encapsulating the raw C-API MlirLocation.
Definition: Interop.h:254
static MlirAffineMap mlirPythonCapsuleToAffineMap(PyObject *capsule)
Extracts an MlirAffineMap from a capsule as produced from mlirPythonAffineMapToCapsule.
Definition: Interop.h:374
#define MLIR_PYTHON_CAPI_FACTORY_ATTR
Attribute on MLIR Python objects that exposes a factory function for constructing the corresponding P...
Definition: Interop.h:109
static MlirModule mlirPythonCapsuleToModule(PyObject *capsule)
Extracts an MlirModule from a capsule as produced from mlirPythonModuleToCapsule.
Definition: Interop.h:281
static MlirContext mlirPythonCapsuleToContext(PyObject *capsule)
Extracts a MlirContext from a capsule as produced from mlirPythonContextToCapsule.
Definition: Interop.h:223
static MlirTypeID mlirPythonCapsuleToTypeID(PyObject *capsule)
Extracts an MlirTypeID from a capsule as produced from mlirPythonTypeIDToCapsule.
Definition: Interop.h:336
static PyObject * mlirPythonDialectRegistryToCapsule(MlirDialectRegistry registry)
Creates a capsule object encapsulating the raw C-API MlirDialectRegistry.
Definition: Interop.h:234
static PyObject * mlirPythonTypeToCapsule(MlirType type)
Creates a capsule object encapsulating the raw C-API MlirType.
Definition: Interop.h:346
static MlirDialectRegistry mlirPythonCapsuleToDialectRegistry(PyObject *capsule)
Extracts an MlirDialectRegistry from a capsule as produced from mlirPythonDialectRegistryToCapsule.
Definition: Interop.h:244
#define MAKE_MLIR_PYTHON_QUALNAME(local)
Definition: Interop.h:56
static MlirType mlirPythonCapsuleToType(PyObject *capsule)
Extracts an MlirType from a capsule as produced from mlirPythonTypeToCapsule.
Definition: Interop.h:355
static MlirValue mlirPythonCapsuleToValue(PyObject *capsule)
Extracts an MlirValue from a capsule as produced from mlirPythonValueToCapsule.
Definition: Interop.h:433
static PyObject * mlirPythonAffineMapToCapsule(MlirAffineMap affineMap)
Creates a capsule object encapsulating the raw C-API MlirAffineMap.
Definition: Interop.h:365
static MlirPassManager mlirPythonCapsuleToPassManager(PyObject *capsule)
Extracts an MlirPassManager from a capsule as produced from mlirPythonPassManagerToCapsule.
Definition: Interop.h:299
static PyObject * mlirPythonOperationToCapsule(MlirOperation operation)
Creates a capsule object encapsulating the raw C-API MlirOperation.
Definition: Interop.h:309
static MlirLocation mlirPythonCapsuleToLocation(PyObject *capsule)
Extracts an MlirLocation from a capsule as produced from mlirPythonLocationToCapsule.
Definition: Interop.h:263
static PyObject * mlirPythonValueToCapsule(MlirValue value)
Creates a capsule object encapsulating the raw C-API MlirValue.
Definition: Interop.h:424
#define MLIR_PYTHON_CAPI_TYPE_CASTER_REGISTER_ATTR
Attribute on main C extension module (_mlir) that corresponds to the type caster registration binding...
Definition: Interop.h:129
Creates a custom subclass of mlir.ir.Attribute, implementing a casting constructor and type checking ...
mlir_attribute_subclass(py::handle scope, const char *typeClassName, IsAFunctionTy isaFunction, const py::object &superCls)
Subclasses with a provided mlir.ir.Attribute super-class.
mlir_attribute_subclass(py::handle scope, const char *attrClassName, IsAFunctionTy isaFunction)
Subclasses by looking up the super-class dynamically.
Creates a custom subclass of mlir.ir.Type, implementing a casting constructor and type checking metho...
mlir_type_subclass(py::handle scope, const char *typeClassName, IsAFunctionTy isaFunction, const py::object &superCls, GetTypeIDFunctionTy getTypeIDFunction=nullptr)
Subclasses with a provided mlir.ir.Type super-class.
mlir_type_subclass(py::handle scope, const char *typeClassName, IsAFunctionTy isaFunction, GetTypeIDFunctionTy getTypeIDFunction=nullptr)
Subclasses by looking up the super-class dynamically.
Creates a custom subclass of mlir.ir.Value, implementing a casting constructor and type checking meth...
mlir_value_subclass(py::handle scope, const char *valueClassName, IsAFunctionTy isaFunction, const py::object &superCls)
Subclasses with a provided mlir.ir.Value super-class.
mlir_value_subclass(py::handle scope, const char *valueClassName, IsAFunctionTy isaFunction)
Subclasses by looking up the super-class dynamically.
Provides a facility like py::class_ for defining a new class in a scope, but this allows extension of...
pure_subclass & def_classmethod(const char *name, Func &&f, const Extra &...extra)
pure_subclass & def(const char *name, Func &&f, const Extra &...extra)
pure_subclass(py::handle scope, const char *derivedClassName, const py::object &superClass)
pure_subclass & def_property_readonly(const char *name, Func &&f, const Extra &...extra)
pure_subclass & def_staticmethod(const char *name, Func &&f, const Extra &...extra)
static bool mlirPassManagerIsNull(MlirPassManager passManager)
Checks if a PassManager is null.
Definition: Pass.h:65
static bool mlirAffineMapIsNull(MlirAffineMap affineMap)
Checks whether an affine map is null.
Definition: AffineMap.h:47
static bool mlirAttributeIsNull(MlirAttribute attr)
Checks whether an attribute is null.
Definition: IR.h:1008
static bool mlirModuleIsNull(MlirModule module)
Checks whether a module is null.
Definition: IR.h:314
static bool mlirValueIsNull(MlirValue value)
Returns whether the value is null.
Definition: IR.h:875
static bool mlirTypeIsNull(MlirType type)
Checks whether a type is null.
Definition: IR.h:973
static bool mlirContextIsNull(MlirContext context)
Checks whether a context is null.
Definition: IR.h:104
static bool mlirBlockIsNull(MlirBlock block)
Checks whether a block is null.
Definition: IR.h:797
static bool mlirLocationIsNull(MlirLocation location)
Checks if the location is null.
Definition: IR.h:282
static bool mlirDialectRegistryIsNull(MlirDialectRegistry registry)
Checks if the dialect registry is null.
Definition: IR.h:235
static bool mlirOperationIsNull(MlirOperation op)
Checks whether the underlying operation is null.
Definition: IR.h:507
static bool mlirTypeIDIsNull(MlirTypeID typeID)
Checks whether a type id is null.
Definition: Support.h:163
Include the generated interface declarations.
static py::object mlirApiObjectToCapsule(py::handle apiObject)
Helper to convert a presumed MLIR API object to a capsule, accepting either an explicit Capsule (whic...
PYBIND11_TYPE_CASTER(MlirAffineMap, _("MlirAffineMap"))
static handle cast(MlirAffineMap v, return_value_policy, handle)
PYBIND11_TYPE_CASTER(MlirAttribute, _("MlirAttribute"))
static handle cast(MlirAttribute v, return_value_policy, handle)
PYBIND11_TYPE_CASTER(MlirBlock, _("MlirBlock"))
PYBIND11_TYPE_CASTER(MlirContext, _("MlirContext"))
PYBIND11_TYPE_CASTER(MlirDialectRegistry, _("MlirDialectRegistry"))
static handle cast(MlirDialectRegistry v, return_value_policy, handle)
PYBIND11_TYPE_CASTER(MlirLocation, _("MlirLocation"))
static handle cast(MlirLocation v, return_value_policy, handle)
PYBIND11_TYPE_CASTER(MlirModule, _("MlirModule"))
static handle cast(MlirModule v, return_value_policy, handle)
PYBIND11_TYPE_CASTER(MlirOperation, _("MlirOperation"))
static handle cast(MlirOperation v, return_value_policy, handle)
PYBIND11_TYPE_CASTER(MlirPassManager, _("MlirPassManager"))
PYBIND11_TYPE_CASTER(MlirTypeID, _("MlirTypeID"))
static handle cast(MlirTypeID v, return_value_policy, handle)
static handle cast(MlirType t, return_value_policy, handle)
PYBIND11_TYPE_CASTER(MlirType, _("MlirType"))
static handle cast(MlirValue v, return_value_policy, handle)
PYBIND11_TYPE_CASTER(MlirValue, _("MlirValue"))