Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
117 changes: 67 additions & 50 deletions mlir/include/mlir/Bindings/Python/NanobindAdaptors.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
#define MLIR_BINDINGS_PYTHON_NANOBINDADAPTORS_H

#include <cstdint>
#include <optional>

#include "mlir-c/Diagnostics.h"
#include "mlir-c/IR.h"
Expand All @@ -43,18 +44,14 @@ namespace detail {
/// with a raw handle (unowned). The returned object's lifetime may not extend
/// beyond the apiObject handle without explicitly having its refcount increased
/// (i.e. on return).
static nanobind::object mlirApiObjectToCapsule(nanobind::handle apiObject) {
static std::optional<nanobind::object>
mlirApiObjectToCapsule(nanobind::handle apiObject) {
if (PyCapsule_CheckExact(apiObject.ptr()))
return nanobind::borrow<nanobind::object>(apiObject);
nanobind::object api =
nanobind::getattr(apiObject, MLIR_PYTHON_CAPI_PTR_ATTR, nanobind::none());
if (api.is_none()) {
std::string repr = nanobind::cast<std::string>(nanobind::repr(apiObject));
throw nanobind::type_error(
(llvm::Twine("Expected an MLIR object (got ") + repr + ").")
.str()
.c_str());
}
if (api.is_none())
return std::nullopt;
return api;
}

Expand All @@ -67,12 +64,11 @@ static nanobind::object mlirApiObjectToCapsule(nanobind::handle apiObject) {
template <>
struct type_caster<MlirAffineMap> {
NB_TYPE_CASTER(MlirAffineMap, const_name("MlirAffineMap"))
bool from_python(handle src, uint8_t flags, cleanup_list *cleanup) {
nanobind::object capsule = mlirApiObjectToCapsule(src);
value = mlirPythonCapsuleToAffineMap(capsule.ptr());
if (mlirAffineMapIsNull(value)) {
bool from_python(handle src, uint8_t flags, cleanup_list *cleanup) noexcept {
std::optional<nanobind::object> capsule = mlirApiObjectToCapsule(src);
if (!capsule)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I did

    if (auto capsule = mlirApiObjectToCapsule(src))
      value = mlirPythonCapsuleToPassManager(capsule->ptr());

return false;
}
value = mlirPythonCapsuleToAffineMap(capsule.value().ptr());
return !mlirAffineMapIsNull(value);
}
static handle from_cpp(MlirAffineMap v, rv_policy,
Expand All @@ -90,9 +86,11 @@ struct type_caster<MlirAffineMap> {
template <>
struct type_caster<MlirAttribute> {
NB_TYPE_CASTER(MlirAttribute, const_name("MlirAttribute"))
bool from_python(handle src, uint8_t flags, cleanup_list *cleanup) {
nanobind::object capsule = mlirApiObjectToCapsule(src);
value = mlirPythonCapsuleToAttribute(capsule.ptr());
bool from_python(handle src, uint8_t flags, cleanup_list *cleanup) noexcept {
std::optional<nanobind::object> capsule = mlirApiObjectToCapsule(src);
if (!capsule)
return false;
value = mlirPythonCapsuleToAttribute(capsule.value().ptr());
return !mlirAttributeIsNull(value);
}
static handle from_cpp(MlirAttribute v, rv_policy,
Expand All @@ -111,9 +109,11 @@ struct type_caster<MlirAttribute> {
template <>
struct type_caster<MlirBlock> {
NB_TYPE_CASTER(MlirBlock, const_name("MlirBlock"))
bool from_python(handle src, uint8_t flags, cleanup_list *cleanup) {
nanobind::object capsule = mlirApiObjectToCapsule(src);
value = mlirPythonCapsuleToBlock(capsule.ptr());
bool from_python(handle src, uint8_t flags, cleanup_list *cleanup) noexcept {
std::optional<nanobind::object> capsule = mlirApiObjectToCapsule(src);
if (!capsule)
return false;
value = mlirPythonCapsuleToBlock(capsule.value().ptr());
return !mlirBlockIsNull(value);
}
};
Expand All @@ -122,7 +122,7 @@ struct type_caster<MlirBlock> {
template <>
struct type_caster<MlirContext> {
NB_TYPE_CASTER(MlirContext, const_name("MlirContext"))
bool from_python(handle src, uint8_t flags, cleanup_list *cleanup) {
bool from_python(handle src, uint8_t flags, cleanup_list *cleanup) noexcept {
if (src.is_none()) {
// Gets the current thread-bound context.
// TODO: This raises an error of "No current context" currently.
Expand All @@ -132,8 +132,8 @@ struct type_caster<MlirContext> {
.attr("Context")
.attr("current");
}
nanobind::object capsule = mlirApiObjectToCapsule(src);
value = mlirPythonCapsuleToContext(capsule.ptr());
std::optional<nanobind::object> capsule = mlirApiObjectToCapsule(src);
value = mlirPythonCapsuleToContext(capsule.value().ptr());
return !mlirContextIsNull(value);
}
};
Expand All @@ -142,9 +142,11 @@ struct type_caster<MlirContext> {
template <>
struct type_caster<MlirDialectRegistry> {
NB_TYPE_CASTER(MlirDialectRegistry, const_name("MlirDialectRegistry"))
bool from_python(handle src, uint8_t flags, cleanup_list *cleanup) {
nanobind::object capsule = mlirApiObjectToCapsule(src);
value = mlirPythonCapsuleToDialectRegistry(capsule.ptr());
bool from_python(handle src, uint8_t flags, cleanup_list *cleanup) noexcept {
std::optional<nanobind::object> capsule = mlirApiObjectToCapsule(src);
if (!capsule)
return false;
value = mlirPythonCapsuleToDialectRegistry(capsule.value().ptr());
return !mlirDialectRegistryIsNull(value);
}
static handle from_cpp(MlirDialectRegistry v, rv_policy,
Expand All @@ -162,15 +164,15 @@ struct type_caster<MlirDialectRegistry> {
template <>
struct type_caster<MlirLocation> {
NB_TYPE_CASTER(MlirLocation, const_name("MlirLocation"))
bool from_python(handle src, uint8_t flags, cleanup_list *cleanup) {
bool from_python(handle src, uint8_t flags, cleanup_list *cleanup) noexcept {
if (src.is_none()) {
// Gets the current thread-bound context.
src = nanobind::module_::import_(MAKE_MLIR_PYTHON_QUALNAME("ir"))
.attr("Location")
.attr("current");
}
nanobind::object capsule = mlirApiObjectToCapsule(src);
value = mlirPythonCapsuleToLocation(capsule.ptr());
std::optional<nanobind::object> capsule = mlirApiObjectToCapsule(src);
value = mlirPythonCapsuleToLocation(capsule.value().ptr());
return !mlirLocationIsNull(value);
}
static handle from_cpp(MlirLocation v, rv_policy,
Expand All @@ -188,9 +190,11 @@ struct type_caster<MlirLocation> {
template <>
struct type_caster<MlirModule> {
NB_TYPE_CASTER(MlirModule, const_name("MlirModule"))
bool from_python(handle src, uint8_t flags, cleanup_list *cleanup) {
nanobind::object capsule = mlirApiObjectToCapsule(src);
value = mlirPythonCapsuleToModule(capsule.ptr());
bool from_python(handle src, uint8_t flags, cleanup_list *cleanup) noexcept {
std::optional<nanobind::object> capsule = mlirApiObjectToCapsule(src);
if (!capsule)
return false;
value = mlirPythonCapsuleToModule(capsule.value().ptr());
return !mlirModuleIsNull(value);
}
static handle from_cpp(MlirModule v, rv_policy,
Expand All @@ -209,12 +213,15 @@ template <>
struct type_caster<MlirFrozenRewritePatternSet> {
NB_TYPE_CASTER(MlirFrozenRewritePatternSet,
const_name("MlirFrozenRewritePatternSet"))
bool from_python(handle src, uint8_t flags, cleanup_list *cleanup) {
nanobind::object capsule = mlirApiObjectToCapsule(src);
value = mlirPythonCapsuleToFrozenRewritePatternSet(capsule.ptr());
bool from_python(handle src, uint8_t flags, cleanup_list *cleanup) noexcept {
std::optional<nanobind::object> capsule = mlirApiObjectToCapsule(src);
if (!capsule)
return false;
value = mlirPythonCapsuleToFrozenRewritePatternSet(capsule.value().ptr());
return value.ptr != nullptr;
}
static handle from_cpp(MlirFrozenRewritePatternSet v, rv_policy, handle) {
static handle from_cpp(MlirFrozenRewritePatternSet v, rv_policy,
handle) noexcept {
nanobind::object capsule = nanobind::steal<nanobind::object>(
mlirPythonFrozenRewritePatternSetToCapsule(v));
return nanobind::module_::import_(MAKE_MLIR_PYTHON_QUALNAME("rewrite"))
Expand All @@ -228,9 +235,11 @@ struct type_caster<MlirFrozenRewritePatternSet> {
template <>
struct type_caster<MlirOperation> {
NB_TYPE_CASTER(MlirOperation, const_name("MlirOperation"))
bool from_python(handle src, uint8_t flags, cleanup_list *cleanup) {
nanobind::object capsule = mlirApiObjectToCapsule(src);
value = mlirPythonCapsuleToOperation(capsule.ptr());
bool from_python(handle src, uint8_t flags, cleanup_list *cleanup) noexcept {
std::optional<nanobind::object> capsule = mlirApiObjectToCapsule(src);
if (!capsule)
return false;
value = mlirPythonCapsuleToOperation(capsule.value().ptr());
return !mlirOperationIsNull(value);
}
static handle from_cpp(MlirOperation v, rv_policy,
Expand All @@ -250,9 +259,11 @@ struct type_caster<MlirOperation> {
template <>
struct type_caster<MlirValue> {
NB_TYPE_CASTER(MlirValue, const_name("MlirValue"))
bool from_python(handle src, uint8_t flags, cleanup_list *cleanup) {
nanobind::object capsule = mlirApiObjectToCapsule(src);
value = mlirPythonCapsuleToValue(capsule.ptr());
bool from_python(handle src, uint8_t flags, cleanup_list *cleanup) noexcept {
std::optional<nanobind::object> capsule = mlirApiObjectToCapsule(src);
if (!capsule)
return false;
value = mlirPythonCapsuleToValue(capsule.value().ptr());
return !mlirValueIsNull(value);
}
static handle from_cpp(MlirValue v, rv_policy,
Expand All @@ -273,9 +284,11 @@ struct type_caster<MlirValue> {
template <>
struct type_caster<MlirPassManager> {
NB_TYPE_CASTER(MlirPassManager, const_name("MlirPassManager"))
bool from_python(handle src, uint8_t flags, cleanup_list *cleanup) {
nanobind::object capsule = mlirApiObjectToCapsule(src);
value = mlirPythonCapsuleToPassManager(capsule.ptr());
bool from_python(handle src, uint8_t flags, cleanup_list *cleanup) noexcept {
std::optional<nanobind::object> capsule = mlirApiObjectToCapsule(src);
if (!capsule)
return false;
value = mlirPythonCapsuleToPassManager(capsule.value().ptr());
return !mlirPassManagerIsNull(value);
}
};
Expand All @@ -284,9 +297,11 @@ struct type_caster<MlirPassManager> {
template <>
struct type_caster<MlirTypeID> {
NB_TYPE_CASTER(MlirTypeID, const_name("MlirTypeID"))
bool from_python(handle src, uint8_t flags, cleanup_list *cleanup) {
nanobind::object capsule = mlirApiObjectToCapsule(src);
value = mlirPythonCapsuleToTypeID(capsule.ptr());
bool from_python(handle src, uint8_t flags, cleanup_list *cleanup) noexcept {
std::optional<nanobind::object> capsule = mlirApiObjectToCapsule(src);
if (!capsule)
return false;
value = mlirPythonCapsuleToTypeID(capsule.value().ptr());
return !mlirTypeIDIsNull(value);
}
static handle from_cpp(MlirTypeID v, rv_policy,
Expand All @@ -306,9 +321,11 @@ struct type_caster<MlirTypeID> {
template <>
struct type_caster<MlirType> {
NB_TYPE_CASTER(MlirType, const_name("MlirType"))
bool from_python(handle src, uint8_t flags, cleanup_list *cleanup) {
nanobind::object capsule = mlirApiObjectToCapsule(src);
value = mlirPythonCapsuleToType(capsule.ptr());
bool from_python(handle src, uint8_t flags, cleanup_list *cleanup) noexcept {
std::optional<nanobind::object> capsule = mlirApiObjectToCapsule(src);
if (!capsule)
return false;
value = mlirPythonCapsuleToType(capsule.value().ptr());
return !mlirTypeIsNull(value);
}
static handle from_cpp(MlirType t, rv_policy,
Expand Down
6 changes: 4 additions & 2 deletions mlir/test/python/lib/PythonTestModuleNanobind.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -113,8 +113,10 @@ NB_MODULE(_mlirPythonTestNanobind, m) {
.attr(MLIR_PYTHON_CAPI_VALUE_CASTER_REGISTER_ATTR)(
mlirRankedTensorTypeID)(
nanobind::cpp_function([valueCls](const nb::object &valueObj) {
nb::object capsule = mlirApiObjectToCapsule(valueObj);
MlirValue v = mlirPythonCapsuleToValue(capsule.ptr());
std::optional<nb::object> capsule =
mlirApiObjectToCapsule(valueObj);
// TODO(nicholasjng): Can this capsule be std::nullopt?
MlirValue v = mlirPythonCapsuleToValue(capsule.value().ptr());
MlirType t = mlirValueGetType(v);
// This is hyper-specific in order to exercise/test registering a
// value caster from cpp (but only for a single test case; see
Expand Down
Loading