Skip to content

Conversation

bartchr808
Copy link
Contributor

No description provided.

@llvmbot llvmbot added the bazel "Peripheral" support tier build system: utils/bazel label Sep 22, 2025
@llvmbot llvmbot added the mlir label Sep 22, 2025
@llvmbot
Copy link
Member

llvmbot commented Sep 22, 2025

@llvm/pr-subscribers-mlir

Author: Bart Chrzaszcz (bartchr808)

Changes

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

4 Files Affected:

  • (modified) mlir/lib/Bindings/Python/IRCore.cpp (+8-8)
  • (modified) mlir/lib/Bindings/Python/IRModule.h (+2-2)
  • (modified) mlir/lib/Bindings/Python/NanobindUtils.h (+1-1)
  • (modified) utils/bazel/llvm-project-overlay/mlir/python/BUILD.bazel (-15)
diff --git a/mlir/lib/Bindings/Python/IRCore.cpp b/mlir/lib/Bindings/Python/IRCore.cpp
index 609502041f4ae..9e60bc6a65bcd 100644
--- a/mlir/lib/Bindings/Python/IRCore.cpp
+++ b/mlir/lib/Bindings/Python/IRCore.cpp
@@ -1550,8 +1550,8 @@ nb::typed<nb::object, PyOpView> PyOperation::createOpView() {
   auto operationCls = PyGlobals::get().lookupOperationClass(
       StringRef(identStr.data, identStr.length));
   if (operationCls)
-    return PyOpView::constructDerived(*operationCls, getRef().getObject());
-  return nb::cast(PyOpView(getRef().getObject()));
+    return nanobind::cast<nanobind::typed<nanobind::object, PyOpView>>(PyOpView::constructDerived(*operationCls, getRef().getObject()));
+  return nanobind::cast<nanobind::typed<nanobind::object, PyOpView>>(nb::cast(PyOpView(getRef().getObject())));
 }
 
 void PyOperation::erase() {
@@ -2143,8 +2143,8 @@ nb::typed<nb::object, PyAttribute> PyAttribute::maybeDownCast() {
   // contents into a new instance that will be owned by Python.
   nb::object thisObj = nb::cast(this, nb::rv_policy::move);
   if (!typeCaster)
-    return thisObj;
-  return typeCaster.value()(thisObj);
+    return nanobind::cast<nanobind::typed<nanobind::object, PyAttribute>>(thisObj);
+  return nanobind::cast<nanobind::typed<nanobind::object, PyAttribute>>(typeCaster.value()(thisObj));
 }
 
 //------------------------------------------------------------------------------
@@ -2189,8 +2189,8 @@ nb::typed<nb::object, PyType> PyType::maybeDownCast() {
   // contents into a new instance that will be owned by Python.
   nb::object thisObj = nb::cast(this, nb::rv_policy::move);
   if (!typeCaster)
-    return thisObj;
-  return typeCaster.value()(thisObj);
+    return nanobind::cast<nanobind::typed<nanobind::object, PyType>>(thisObj);
+  return nanobind::cast<nanobind::typed<nanobind::object, PyType>>(typeCaster.value()(thisObj));
 }
 
 //------------------------------------------------------------------------------
@@ -2230,8 +2230,8 @@ nanobind::typed<nanobind::object, PyValue> PyValue::maybeDownCast() {
   // contents into a new instance that will be owned by Python.
   nb::object thisObj = nb::cast(this, nb::rv_policy::move);
   if (!valueCaster)
-    return thisObj;
-  return valueCaster.value()(thisObj);
+    return nanobind::cast<nanobind::typed<nanobind::object, PyValue>>(thisObj);
+  return nanobind::cast<nanobind::typed<nanobind::object, PyValue>>(valueCaster.value()(thisObj));
 }
 
 PyValue PyValue::createFromCapsule(nb::object capsule) {
diff --git a/mlir/lib/Bindings/Python/IRModule.h b/mlir/lib/Bindings/Python/IRModule.h
index 414f37cc97f2a..633b11f6fe96e 100644
--- a/mlir/lib/Bindings/Python/IRModule.h
+++ b/mlir/lib/Bindings/Python/IRModule.h
@@ -53,7 +53,7 @@ template <typename T>
 class PyObjectRef {
 public:
   PyObjectRef(T *referrent, nanobind::object object)
-      : referrent(referrent), object(std::move(object)) {
+      : referrent(referrent), object(nanobind::cast<nanobind::typed<nanobind::object, T>>(object)) {
     assert(this->referrent &&
            "cannot construct PyObjectRef with null referrent");
     assert(this->object && "cannot construct PyObjectRef with null object");
@@ -96,7 +96,7 @@ class PyObjectRef {
 
 private:
   T *referrent;
-  nanobind::object object;
+  nanobind::typed<nanobind::object, T> object;
 };
 
 /// Tracks an entry in the thread context stack. New entries are pushed onto
diff --git a/mlir/lib/Bindings/Python/NanobindUtils.h b/mlir/lib/Bindings/Python/NanobindUtils.h
index 40b3215f6f5fe..64ea4329f65f1 100644
--- a/mlir/lib/Bindings/Python/NanobindUtils.h
+++ b/mlir/lib/Bindings/Python/NanobindUtils.h
@@ -276,7 +276,7 @@ class Sliceable {
   /// Returns the element at the given slice index. Supports negative indices
   /// by taking elements in inverse order. Returns a nullptr object if out
   /// of bounds.
-  nanobind::typed<nanobind::object, ElementTy> getItem(intptr_t index) {
+  nanobind::object getItem(intptr_t index) {
     // Negative indices mean we count from the end.
     index = wrapIndex(index);
     if (index < 0) {
diff --git a/utils/bazel/llvm-project-overlay/mlir/python/BUILD.bazel b/utils/bazel/llvm-project-overlay/mlir/python/BUILD.bazel
index a6e8c515f0874..3daf9290921e7 100644
--- a/utils/bazel/llvm-project-overlay/mlir/python/BUILD.bazel
+++ b/utils/bazel/llvm-project-overlay/mlir/python/BUILD.bazel
@@ -53,14 +53,6 @@ filegroup(
     ]),
 )
 
-filegroup(
-    name = "IRPyIFiles",
-    srcs = [
-        "mlir/_mlir_libs/_mlir/__init__.pyi",
-        "mlir/_mlir_libs/_mlir/ir.pyi",
-    ],
-)
-
 filegroup(
     name = "MlirLibsPyFiles",
     srcs = [
@@ -75,13 +67,6 @@ filegroup(
     ],
 )
 
-filegroup(
-    name = "PassManagerPyIFiles",
-    srcs = [
-        "mlir/_mlir_libs/_mlir/passmanager.pyi",
-    ],
-)
-
 filegroup(
     name = "RewritePyFiles",
     srcs = [

Copy link

⚠️ C/C++ code formatter, clang-format found issues in your code. ⚠️

You can test this locally with the following command:
git-clang-format --diff origin/main HEAD --extensions h,cpp -- mlir/lib/Bindings/Python/IRCore.cpp mlir/lib/Bindings/Python/IRModule.h mlir/lib/Bindings/Python/NanobindUtils.h

⚠️
The reproduction instructions above might return results for more than one PR
in a stack if you are using a stacked PR workflow. You can limit the results by
changing origin/main to the base branch/commit you want to compare against.
⚠️

View the diff from clang-format here.
diff --git a/mlir/lib/Bindings/Python/IRCore.cpp b/mlir/lib/Bindings/Python/IRCore.cpp
index 9e60bc6a6..70058d8ca 100644
--- a/mlir/lib/Bindings/Python/IRCore.cpp
+++ b/mlir/lib/Bindings/Python/IRCore.cpp
@@ -1550,8 +1550,10 @@ nb::typed<nb::object, PyOpView> PyOperation::createOpView() {
   auto operationCls = PyGlobals::get().lookupOperationClass(
       StringRef(identStr.data, identStr.length));
   if (operationCls)
-    return nanobind::cast<nanobind::typed<nanobind::object, PyOpView>>(PyOpView::constructDerived(*operationCls, getRef().getObject()));
-  return nanobind::cast<nanobind::typed<nanobind::object, PyOpView>>(nb::cast(PyOpView(getRef().getObject())));
+    return nanobind::cast<nanobind::typed<nanobind::object, PyOpView>>(
+        PyOpView::constructDerived(*operationCls, getRef().getObject()));
+  return nanobind::cast<nanobind::typed<nanobind::object, PyOpView>>(
+      nb::cast(PyOpView(getRef().getObject())));
 }
 
 void PyOperation::erase() {
@@ -2143,8 +2145,10 @@ nb::typed<nb::object, PyAttribute> PyAttribute::maybeDownCast() {
   // contents into a new instance that will be owned by Python.
   nb::object thisObj = nb::cast(this, nb::rv_policy::move);
   if (!typeCaster)
-    return nanobind::cast<nanobind::typed<nanobind::object, PyAttribute>>(thisObj);
-  return nanobind::cast<nanobind::typed<nanobind::object, PyAttribute>>(typeCaster.value()(thisObj));
+    return nanobind::cast<nanobind::typed<nanobind::object, PyAttribute>>(
+        thisObj);
+  return nanobind::cast<nanobind::typed<nanobind::object, PyAttribute>>(
+      typeCaster.value()(thisObj));
 }
 
 //------------------------------------------------------------------------------
@@ -2190,7 +2194,8 @@ nb::typed<nb::object, PyType> PyType::maybeDownCast() {
   nb::object thisObj = nb::cast(this, nb::rv_policy::move);
   if (!typeCaster)
     return nanobind::cast<nanobind::typed<nanobind::object, PyType>>(thisObj);
-  return nanobind::cast<nanobind::typed<nanobind::object, PyType>>(typeCaster.value()(thisObj));
+  return nanobind::cast<nanobind::typed<nanobind::object, PyType>>(
+      typeCaster.value()(thisObj));
 }
 
 //------------------------------------------------------------------------------
@@ -2231,7 +2236,8 @@ nanobind::typed<nanobind::object, PyValue> PyValue::maybeDownCast() {
   nb::object thisObj = nb::cast(this, nb::rv_policy::move);
   if (!valueCaster)
     return nanobind::cast<nanobind::typed<nanobind::object, PyValue>>(thisObj);
-  return nanobind::cast<nanobind::typed<nanobind::object, PyValue>>(valueCaster.value()(thisObj));
+  return nanobind::cast<nanobind::typed<nanobind::object, PyValue>>(
+      valueCaster.value()(thisObj));
 }
 
 PyValue PyValue::createFromCapsule(nb::object capsule) {
diff --git a/mlir/lib/Bindings/Python/IRModule.h b/mlir/lib/Bindings/Python/IRModule.h
index 633b11f6f..c4e87e5ea 100644
--- a/mlir/lib/Bindings/Python/IRModule.h
+++ b/mlir/lib/Bindings/Python/IRModule.h
@@ -53,7 +53,8 @@ template <typename T>
 class PyObjectRef {
 public:
   PyObjectRef(T *referrent, nanobind::object object)
-      : referrent(referrent), object(nanobind::cast<nanobind::typed<nanobind::object, T>>(object)) {
+      : referrent(referrent),
+        object(nanobind::cast<nanobind::typed<nanobind::object, T>>(object)) {
     assert(this->referrent &&
            "cannot construct PyObjectRef with null referrent");
     assert(this->object && "cannot construct PyObjectRef with null object");

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bazel "Peripheral" support tier build system: utils/bazel mlir
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants