[Mlir-commits] [mlir] 5e9b6a2 - [MLIR] Add MlirValue to PybindAdapters

John Demme llvmlistbot at llvm.org
Sat Aug 6 22:05:42 PDT 2022


Author: John Demme
Date: 2022-08-06T21:58:46-07:00
New Revision: 5e9b6a2243199d19fb7f23f8a3dce5ac813a2151

URL: https://github.com/llvm/llvm-project/commit/5e9b6a2243199d19fb7f23f8a3dce5ac813a2151
DIFF: https://github.com/llvm/llvm-project/commit/5e9b6a2243199d19fb7f23f8a3dce5ac813a2151.diff

LOG: [MLIR] Add MlirValue to PybindAdapters

Allows out-of-tree users to automatically cast to/from MlirValue.

Added: 
    

Modified: 
    mlir/include/mlir/Bindings/Python/PybindAdaptors.h

Removed: 
    


################################################################################
diff  --git a/mlir/include/mlir/Bindings/Python/PybindAdaptors.h b/mlir/include/mlir/Bindings/Python/PybindAdaptors.h
index 351fb964eda38..564425b9beb2c 100644
--- a/mlir/include/mlir/Bindings/Python/PybindAdaptors.h
+++ b/mlir/include/mlir/Bindings/Python/PybindAdaptors.h
@@ -208,6 +208,27 @@ struct type_caster<MlirOperation> {
   };
 };
 
+/// Casts object <-> MlirValue.
+template <>
+struct type_caster<MlirValue> {
+  PYBIND11_TYPE_CASTER(MlirValue, _("MlirValue"));
+  bool load(handle src, bool) {
+    py::object capsule = mlirApiObjectToCapsule(src);
+    value = mlirPythonCapsuleToValue(capsule.ptr());
+    return !mlirValueIsNull(value);
+  }
+  static handle cast(MlirValue v, return_value_policy, handle) {
+    if (v.ptr == nullptr)
+      return py::none();
+    py::object capsule =
+        py::reinterpret_steal<py::object>(mlirPythonValueToCapsule(v));
+    return py::module::import(MAKE_MLIR_PYTHON_QUALNAME("ir"))
+        .attr("Value")
+        .attr(MLIR_PYTHON_CAPI_FACTORY_ATTR)(capsule)
+        .release();
+  };
+};
+
 /// Casts object -> MlirPassManager.
 template <>
 struct type_caster<MlirPassManager> {


        


More information about the Mlir-commits mailing list