Skip to content

Conversation

@superbobry
Copy link
Contributor

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.

@llvmbot
Copy link
Member

llvmbot commented Nov 3, 2025

@llvm/pr-subscribers-mlir-core

@llvm/pr-subscribers-mlir

Author: Sergei Lebedev (superbobry)

Changes

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.


Full diff: https://github.com/llvm/llvm-project/pull/166148.diff

1 Files Affected:

  • (modified) mlir/lib/Bindings/Python/IRCore.cpp (+14-8)
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) {

@github-actions
Copy link

github-actions bot commented Nov 11, 2025

✅ With the latest revision this PR passed the C/C++ code formatter.

Copy link
Contributor

@makslevental makslevental left a comment

Choose a reason for hiding this comment

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

LGTM (modulo one nit)

… 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.

Co-authored-by: Maksim Levental <[email protected]>
@superbobry superbobry merged commit 31536e6 into llvm:main Nov 13, 2025
10 checks passed
Comment on lines +440 to +444
if (std::strcmp(kind, "operand") == 0) {
StringRef pythonType = getPythonType(element.constraint.getCppType());
if (!pythonType.empty())
type += "[" + pythonType.str() + "]";
}
Copy link
Contributor

Choose a reason for hiding this comment

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

I think there is an error here. type may be "_ods_ir.OpOperandList" here which is not Generic.

Copy link
Contributor

Choose a reason for hiding this comment

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

Fixed here #167930

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Thanks for the fix!

nirvedhmeshram added a commit that referenced this pull request Nov 13, 2025
Change in #166148 caused breaks
for some other types.
Specifically this error was seen in a downstream project
```
 _ods_ir.OpOperandList[_ods_ir.IntegerType]:

TypeError: type 'iree.compiler._mlir_libs._mlir.ir.OpOperandList' is not subscriptable

```
This PR tries to make those changes not affect the other types

---------

Signed-off-by: Nirvedh Meshram <[email protected]>
llvm-sync bot pushed a commit to arm/arm-toolchain that referenced this pull request Nov 13, 2025
…(#167930)

Change in llvm/llvm-project#166148 caused breaks
for some other types.
Specifically this error was seen in a downstream project
```
 _ods_ir.OpOperandList[_ods_ir.IntegerType]:

TypeError: type 'iree.compiler._mlir_libs._mlir.ir.OpOperandList' is not subscriptable

```
This PR tries to make those changes not affect the other types

---------

Signed-off-by: Nirvedh Meshram <[email protected]>
nirvedhmeshram added a commit to iree-org/llvm-project that referenced this pull request Nov 15, 2025
Change in llvm#166148 caused breaks
for some other types.
Specifically this error was seen in a downstream project
```
 _ods_ir.OpOperandList[_ods_ir.IntegerType]:

TypeError: type 'iree.compiler._mlir_libs._mlir.ir.OpOperandList' is not subscriptable

```
This PR tries to make those changes not affect the other types

---------

Signed-off-by: Nirvedh Meshram <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

mlir:core MLIR Core Infrastructure mlir

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants