[Mlir-commits] [mlir] [MLIR] [Python] `ir.Value` is now generic in the type of the value it holds (PR #166148)
Sergei Lebedev
llvmlistbot at llvm.org
Mon Nov 3 02:58:30 PST 2025
https://github.com/superbobry updated https://github.com/llvm/llvm-project/pull/166148
>From 7eec476c5753ad8f29340bc6d02c29099319e32f Mon Sep 17 00:00:00 2001
From: Sergei Lebedev <slebedev at google.com>
Date: Mon, 3 Nov 2025 10:55:04 +0000
Subject: [PATCH] [MLIR] [Python] `ir.Value` is now generic in the type of the
value it holds
This makes it similar to `mlir::TypedValue` in the MLIR C++ API and allows
users to be more specific about the values they produce or accept.
---
mlir/lib/Bindings/Python/IRCore.cpp | 22 ++++++++++++++--------
1 file changed, 14 insertions(+), 8 deletions(-)
diff --git a/mlir/lib/Bindings/Python/IRCore.cpp b/mlir/lib/Bindings/Python/IRCore.cpp
index cda4fe19c16f8..1ef058af2c1ef 100644
--- a/mlir/lib/Bindings/Python/IRCore.cpp
+++ b/mlir/lib/Bindings/Python/IRCore.cpp
@@ -18,6 +18,7 @@
#include "mlir/Bindings/Python/Nanobind.h"
#include "mlir/Bindings/Python/NanobindAdaptors.h"
#include "nanobind/nanobind.h"
+#include "nanobind/typing.h"
#include "llvm/ADT/ArrayRef.h"
#include "llvm/ADT/SmallVector.h"
@@ -4278,7 +4279,10 @@ void mlir::python::populateIRCore(nb::module_ &m) {
//----------------------------------------------------------------------------
// Mapping of Value.
//----------------------------------------------------------------------------
- nb::class_<PyValue>(m, "Value")
+ m.attr("_T") = nb::type_var("_T", nb::arg("bound") = m.attr("Type"));
+
+ nb::class_<PyValue>(m, "Value", nb::is_generic(),
+ nb::sig("class Value(Generic[_T])"))
.def(nb::init<PyValue &>(), nb::keep_alive<0, 1>(), nb::arg("value"))
.def_prop_ro(MLIR_PYTHON_CAPI_PTR_ATTR, &PyValue::getCapsule)
.def_static(MLIR_PYTHON_CAPI_FACTORY_ATTR, &PyValue::createFromCapsule)
@@ -4371,18 +4375,20 @@ void mlir::python::populateIRCore(nb::module_ &m) {
return printAccum.join();
},
nb::arg("state"), kGetNameAsOperand)
- .def_prop_ro("type",
- [](PyValue &self) -> nb::typed<nb::object, PyType> {
- return PyType(self.getParentOperation()->getContext(),
- mlirValueGetType(self.get()))
- .maybeDownCast();
- })
+ .def_prop_ro(
+ "type",
+ [](PyValue &self) {
+ return PyType(self.getParentOperation()->getContext(),
+ mlirValueGetType(self.get()))
+ .maybeDownCast();
+ },
+ nb::sig("def type(self) -> _T"))
.def(
"set_type",
[](PyValue &self, const PyType &type) {
return mlirValueSetType(self.get(), type);
},
- nb::arg("type"))
+ nb::arg("type"), nb::sig("def set_type(self, type: _T)"))
.def(
"replace_all_uses_with",
[](PyValue &self, PyValue &with) {
More information about the Mlir-commits
mailing list