[Mlir-commits] [mlir] [MLIR:Python] Fix race on PyOperations. (PR #139721)

llvmlistbot at llvm.org llvmlistbot at llvm.org
Tue May 13 05:39:54 PDT 2025


github-actions[bot] wrote:

<!--LLVM CODE FORMAT COMMENT: {clang-format}-->


:warning: C/C++ code formatter, clang-format found issues in your code. :warning:

<details>
<summary>
You can test this locally with the following command:
</summary>

``````````bash
git-clang-format --diff HEAD~1 HEAD --extensions h,cpp -- mlir/lib/Bindings/Python/IRCore.cpp mlir/lib/Bindings/Python/IRModule.h
``````````

</details>

<details>
<summary>
View the diff from clang-format here.
</summary>

``````````diff
diff --git a/mlir/lib/Bindings/Python/IRCore.cpp b/mlir/lib/Bindings/Python/IRCore.cpp
index cc5a8bdb9..d6c94ef61 100644
--- a/mlir/lib/Bindings/Python/IRCore.cpp
+++ b/mlir/lib/Bindings/Python/IRCore.cpp
@@ -635,16 +635,14 @@ private:
   MlirOpOperand opOperand;
 };
 
-
-
 #if !defined(Py_GIL_DISABLED)
-inline void enableTryIncRef(nb::handle obj) noexcept { }
+inline void enableTryIncRef(nb::handle obj) noexcept {}
 inline bool tryIncRef(nb::handle obj) noexcept {
-    if (Py_REFCNT(obj.ptr()) > 0) {
-        Py_INCREF(obj.ptr());
-        return true;
-    }
-    return false;
+  if (Py_REFCNT(obj.ptr()) > 0) {
+    Py_INCREF(obj.ptr());
+    return true;
+  }
+  return false;
 }
 
 #elif PY_VERSION_HEX >= 0x030E00A5
@@ -664,42 +662,44 @@ inline bool tryIncRef(nb::handle obj) noexcept {
 void enableTryIncRef(nb::handle h) noexcept {
   // Since this is called during object construction, we know that we have
   // the only reference to the object and can use a non-atomic write.
-  PyObject* obj = h.ptr();
+  PyObject *obj = h.ptr();
   assert(h->ob_ref_shared == 0);
   h->ob_ref_shared = _Py_REF_MAYBE_WEAKREF;
 }
 
 bool tryIncRef(nb::handle h) noexcept {
   PyObject *obj = h.ptr();
-  // See https://github.com/python/cpython/blob/d05140f9f77d7dfc753dd1e5ac3a5962aaa03eff/Include/internal/pycore_object.h#L761
+  // See
+  // https://github.com/python/cpython/blob/d05140f9f77d7dfc753dd1e5ac3a5962aaa03eff/Include/internal/pycore_object.h#L761
   uint32_t local = _Py_atomic_load_uint32_relaxed(&obj->ob_ref_local);
   local += 1;
   if (local == 0) {
-      // immortal
-      return true;
+    // immortal
+    return true;
   }
   if (_Py_IsOwnedByCurrentThread(obj)) {
-      _Py_atomic_store_uint32_relaxed(&obj->ob_ref_local, local);
+    _Py_atomic_store_uint32_relaxed(&obj->ob_ref_local, local);
 #ifdef Py_REF_DEBUG
-      _Py_INCREF_IncRefTotal();
+    _Py_INCREF_IncRefTotal();
 #endif
-      return true;
+    return true;
   }
   Py_ssize_t shared = _Py_atomic_load_ssize_relaxed(&obj->ob_ref_shared);
   for (;;) {
-      // If the shared refcount is zero and the object is either merged
-      // or may not have weak references, then we cannot incref it.
-      if (shared == 0 || shared == _Py_REF_MERGED) {
-          return false;
-      }
+    // If the shared refcount is zero and the object is either merged
+    // or may not have weak references, then we cannot incref it.
+    if (shared == 0 || shared == _Py_REF_MERGED) {
+      return false;
+    }
 
-      if (_Py_atomic_compare_exchange_ssize(
-              &obj->ob_ref_shared, &shared, shared + (1 << _Py_REF_SHARED_SHIFT))) {
+    if (_Py_atomic_compare_exchange_ssize(&obj->ob_ref_shared, &shared,
+                                          shared +
+                                              (1 << _Py_REF_SHARED_SHIFT))) {
 #ifdef Py_REF_DEBUG
-          _Py_INCREF_IncRefTotal();
+      _Py_INCREF_IncRefTotal();
 #endif
-          return true;
-      }
+      return true;
+    }
   }
 }
 #endif
diff --git a/mlir/lib/Bindings/Python/IRModule.h b/mlir/lib/Bindings/Python/IRModule.h
index ba3ec4231..4ed0315c2 100644
--- a/mlir/lib/Bindings/Python/IRModule.h
+++ b/mlir/lib/Bindings/Python/IRModule.h
@@ -742,9 +742,7 @@ public:
     setInvalidLocked();
   }
   /// Like setInvalid(), but requires the liveOperations mutex to be held.
-  void setInvalidLocked() {
-    valid = false;
-  }
+  void setInvalidLocked() { valid = false; }
 
   PyOperation(PyMlirContextRef contextRef, MlirOperation operation);
 

``````````

</details>


https://github.com/llvm/llvm-project/pull/139721


More information about the Mlir-commits mailing list