Skip to content

Commit cb513ea

Browse files
authored
[MLIR][Python] fix operation hashing (#156514)
llvm/llvm-project#155114 broke op hashing (because the python objects ceased to be reference equivalent). This PR fixes by binding `OperationEquivalence::computeHash`.
1 parent 479cccd commit cb513ea

File tree

3 files changed

+18
-2
lines changed

3 files changed

+18
-2
lines changed

mlir/include/mlir-c/IR.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -418,6 +418,9 @@ MLIR_CAPI_EXPORTED MlirModule mlirModuleFromOperation(MlirOperation op);
418418
/// Checks if two modules are equal.
419419
MLIR_CAPI_EXPORTED bool mlirModuleEqual(MlirModule lhs, MlirModule rhs);
420420

421+
/// Compute a hash for the given module.
422+
MLIR_CAPI_EXPORTED size_t mlirModuleHashValue(MlirModule mod);
423+
421424
//===----------------------------------------------------------------------===//
422425
// Operation state.
423426
//===----------------------------------------------------------------------===//
@@ -622,6 +625,9 @@ static inline bool mlirOperationIsNull(MlirOperation op) { return !op.ptr; }
622625
MLIR_CAPI_EXPORTED bool mlirOperationEqual(MlirOperation op,
623626
MlirOperation other);
624627

628+
/// Compute a hash for the given operation.
629+
MLIR_CAPI_EXPORTED size_t mlirOperationHashValue(MlirOperation op);
630+
625631
/// Gets the context this operation is associated with
626632
MLIR_CAPI_EXPORTED MlirContext mlirOperationGetContext(MlirOperation op);
627633

mlir/lib/Bindings/Python/IRCore.cpp

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3317,7 +3317,9 @@ void mlir::python::populateIRCore(nb::module_ &m) {
33173317
[](PyModule &self, PyModule &other) {
33183318
return mlirModuleEqual(self.get(), other.get());
33193319
},
3320-
"other"_a);
3320+
"other"_a)
3321+
.def("__hash__",
3322+
[](PyModule &self) { return mlirModuleHashValue(self.get()); });
33213323

33223324
//----------------------------------------------------------------------------
33233325
// Mapping of Operation.
@@ -3336,7 +3338,7 @@ void mlir::python::populateIRCore(nb::module_ &m) {
33363338
[](PyOperationBase &self, nb::object other) { return false; })
33373339
.def("__hash__",
33383340
[](PyOperationBase &self) {
3339-
return static_cast<size_t>(llvm::hash_value(&self.getOperation()));
3341+
return mlirOperationHashValue(self.getOperation().get());
33403342
})
33413343
.def_prop_ro("attributes",
33423344
[](PyOperationBase &self) {

mlir/lib/CAPI/IR/IR.cpp

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -469,6 +469,10 @@ bool mlirModuleEqual(MlirModule lhs, MlirModule rhs) {
469469
return unwrap(lhs) == unwrap(rhs);
470470
}
471471

472+
size_t mlirModuleHashValue(MlirModule mod) {
473+
return OperationEquivalence::computeHash(unwrap(mod).getOperation());
474+
}
475+
472476
//===----------------------------------------------------------------------===//
473477
// Operation state API.
474478
//===----------------------------------------------------------------------===//
@@ -640,6 +644,10 @@ bool mlirOperationEqual(MlirOperation op, MlirOperation other) {
640644
return unwrap(op) == unwrap(other);
641645
}
642646

647+
size_t mlirOperationHashValue(MlirOperation op) {
648+
return OperationEquivalence::computeHash(unwrap(op));
649+
}
650+
643651
MlirContext mlirOperationGetContext(MlirOperation op) {
644652
return wrap(unwrap(op)->getContext());
645653
}

0 commit comments

Comments
 (0)