[Mlir-commits] [mlir] Add Python bindings for operation structural equivalence and hashing (PR #216357)

Maksim Levental llvmlistbot at llvm.org
Fri Aug 14 15:55:29 PDT 2026


================
@@ -3946,6 +3957,34 @@ void populateIRCore(nb::module_ &m) {
             return mlirOperationHashValue(self.getOperation().get());
           },
           "Returns the hash value of the operation.")
+      .def(
+          "is_structurally_equivalent",
+          [](PyOperationBase &self, PyOperationBase &other,
+             PyOperationEquivalenceFlags flags) {
+            self.getOperation().checkValid();
+            other.getOperation().checkValid();
+            return mlirOperationIsStructurallyEquivalent(
+                self.getOperation().get(), other.getOperation().get(),
+                static_cast<uint32_t>(flags));
+          },
+          "other"_a, "flags"_a = PyOperationEquivalenceFlags::None,
+          R"(Checks whether two operations are structurally equivalent.
+
+The IGNORE_LOCATIONS, IGNORE_DISCARDABLE_ATTRS, IGNORE_PROPERTIES, and
+IGNORE_COMMUTATIVITY flags ignore locations, discardable attributes, properties,
+and commutativity, respectively. The predicate recursively compares regions.)")
+      .def(
+          "structural_hash",
+          [](PyOperationBase &self, PyOperationEquivalenceFlags flags) {
+            self.getOperation().checkValid();
+            return mlirOperationStructuralHashValue(
+                self.getOperation().get(), static_cast<uint32_t>(flags));
+          },
+          "flags"_a = PyOperationEquivalenceFlags::None,
+          R"(Computes a structural hash for the operation.
+
+Use the same flags as is_structurally_equivalent. The hash does not recurse into
+regions, unlike the predicate.)")
----------------
makslevental wrote:

```suggestion
          "Computes a structural hash for the operation. The hash does not recurse into
regions, unlike the predicate.")
```

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


More information about the Mlir-commits mailing list