[MLIR] [Python] ir.Value is now generic in the type of the value it holds#166148
Merged
superbobry merged 1 commit intollvm:mainfrom Nov 13, 2025
Merged
[MLIR] [Python] ir.Value is now generic in the type of the value it holds#166148superbobry merged 1 commit intollvm:mainfrom
ir.Value is now generic in the type of the value it holds#166148superbobry merged 1 commit intollvm:mainfrom
Conversation
Member
|
@llvm/pr-subscribers-mlir-core @llvm/pr-subscribers-mlir Author: Sergei Lebedev (superbobry) ChangesThis makes it similar to Full diff: https://github.com/llvm/llvm-project/pull/166148.diff 1 Files Affected:
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) {
|
e8d2178 to
7eec476
Compare
7eec476 to
46e29cb
Compare
46e29cb to
27e8308
Compare
|
✅ With the latest revision this PR passed the C/C++ code formatter. |
27e8308 to
35ea06e
Compare
makslevental
approved these changes
Nov 12, 2025
Contributor
makslevental
left a comment
There was a problem hiding this comment.
LGTM (modulo one nit)
5a54a04 to
1091d12
Compare
… 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 <maksim.levental@gmail.com>
1091d12 to
bce885c
Compare
Comment on lines
+440
to
+444
| if (std::strcmp(kind, "operand") == 0) { | ||
| StringRef pythonType = getPythonType(element.constraint.getCppType()); | ||
| if (!pythonType.empty()) | ||
| type += "[" + pythonType.str() + "]"; | ||
| } |
Contributor
There was a problem hiding this comment.
I think there is an error here. type may be "_ods_ir.OpOperandList" here which is not Generic.
Contributor
Author
There was a problem hiding this comment.
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 <nirvedh@gmail.com>
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 <nirvedh@gmail.com>
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 <nirvedh@gmail.com>
egebeysel
pushed a commit
to egebeysel/llvm-project
that referenced
this pull request
Feb 12, 2026
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 <nirvedh@gmail.com>
makslevental
added a commit
to llvm/eudsl
that referenced
this pull request
Mar 9, 2026
…otations (#356) This PR adds support for using the upstream types as bare arg annotions for `func.func`s: ```python @func def f_same_type_args( x: RankedTensorType[[2, 3], F32Type], y: RankedTensorType[[2, 3], F32Type], ) -> RankedTensorType[[2, 3], F32Type]: ... ``` emits ```mlir func.func private @f_same_type_args(tensor<2x3xf32>, tensor<2x3xf32>) -> tensor<2x3xf32> ``` This is thanks to a PR @PragmaTwice recently landed upstream (I can't remember which one exactly...). As well, we now support the completely "correct" version of this: ```python @func def f_mixed_args( a: Value[F32Type], b: Value[VectorType[[4], F32Type]], c: Value[MemRefType[[2, 3], F32Type]], ): return b ``` where tab-complete will now actually do the correct thing as well, and the IR emitted is ```mlir func.func @f_mixed_args(%[[A:.*]]: f32, %[[B:.*]]: vector<4xf32>, %[[C:.*]]: memref<2x3xf32>) -> vector<4xf32> ``` This is thanks to a [PR](llvm/llvm-project#166148) @superbobry landed recently. **NOTE**: this actually fixes a long-standing issue with the bindings/extras/DSL: namely that you could not write `@func`s without having an active `ir.Context` (because you cannot actually instantiate `ir.Type` without an active `ir.Context`). Since these `GenericAlias`es are unevaluated until we actually emit the `func.func`, we can now write these safely without an active context.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
This makes it similar to
mlir::TypedValuein the MLIR C++ API and allows users to be more specific about the values they produce or accept.