[Mlir-commits] [mlir] [MLIR][Python] fix getOwner to return (typed) nb::object instead of abstract PyOpView (PR #165053)

Maksim Levental llvmlistbot at llvm.org
Sat Oct 25 10:57:46 PDT 2025


https://github.com/makslevental updated https://github.com/llvm/llvm-project/pull/165053

>From 1faa5e28f6b5dfce9aa64e3cf42645a05f4bfbf8 Mon Sep 17 00:00:00 2001
From: Maksim Levental <maksim.levental at gmail.com>
Date: Fri, 24 Oct 2025 15:57:53 -0700
Subject: [PATCH 1/2] [MLIR][Python] fix getOwner

---
 mlir/lib/Bindings/Python/IRCore.cpp | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/mlir/lib/Bindings/Python/IRCore.cpp b/mlir/lib/Bindings/Python/IRCore.cpp
index 06d0256e6287b..cda4fe19c16f8 100644
--- a/mlir/lib/Bindings/Python/IRCore.cpp
+++ b/mlir/lib/Bindings/Python/IRCore.cpp
@@ -598,7 +598,7 @@ class PyOpOperand {
 public:
   PyOpOperand(MlirOpOperand opOperand) : opOperand(opOperand) {}
 
-  PyOpView getOwner() {
+  nb::typed<nb::object, PyOpView> getOwner() {
     MlirOperation owner = mlirOpOperandGetOwner(opOperand);
     PyMlirContextRef context =
         PyMlirContext::forContext(mlirOperationGetContext(owner));

>From cbf934c1bfb5e8ed721f78cb1cc541a073ce010a Mon Sep 17 00:00:00 2001
From: makslevental <maksim.levental at gmail.com>
Date: Sat, 25 Oct 2025 10:49:06 -0700
Subject: [PATCH 2/2] add test

---
 mlir/test/python/ir/value.py | 14 +++++++++++++-
 1 file changed, 13 insertions(+), 1 deletion(-)

diff --git a/mlir/test/python/ir/value.py b/mlir/test/python/ir/value.py
index 4a241afb8e89d..422d4f4ec799a 100644
--- a/mlir/test/python/ir/value.py
+++ b/mlir/test/python/ir/value.py
@@ -2,7 +2,7 @@
 
 import gc
 from mlir.ir import *
-from mlir.dialects import func
+from mlir.dialects import func, arith
 
 
 def run(f):
@@ -452,3 +452,15 @@ def dont_cast_int(v) -> OpResult:
             new_value = Operation.create("custom.op2", results=[i32]).results[0]
             # CHECK: result 0 Value(%1 = "custom.op2"() : () -> i32)
             print("result", new_value.result_number, new_value)
+
+
+# CHECK-LABEL: TEST: testGetOwnerConcreteOpview
+ at run
+def testGetOwnerConcreteOpview():
+    with Context() as ctx, Location.unknown():
+        module = Module.create()
+        with InsertionPoint(module.body):
+            a = arith.ConstantOp(value=42, result=IntegerType.get_signless(32))
+            r = arith.AddIOp(a, a, overflowFlags=arith.IntegerOverflowFlags.nsw)
+            assert isinstance(a.result.owner, arith.ConstantOp)
+            assert isinstance(r.operands[0].owner, arith.ConstantOp)



More information about the Mlir-commits mailing list