File tree Expand file tree Collapse file tree 4 files changed +24
-2
lines changed Expand file tree Collapse file tree 4 files changed +24
-2
lines changed Original file line number Diff line number Diff line change @@ -418,6 +418,9 @@ MLIR_CAPI_EXPORTED MlirModule mlirModuleFromOperation(MlirOperation op);
418
418
/// Checks if two modules are equal.
419
419
MLIR_CAPI_EXPORTED bool mlirModuleEqual (MlirModule lhs , MlirModule rhs );
420
420
421
+ /// Compute a hash for the given module.
422
+ MLIR_CAPI_EXPORTED size_t mlirModuleHashValue (MlirModule mod );
423
+
421
424
//===----------------------------------------------------------------------===//
422
425
// Operation state.
423
426
//===----------------------------------------------------------------------===//
@@ -622,6 +625,9 @@ static inline bool mlirOperationIsNull(MlirOperation op) { return !op.ptr; }
622
625
MLIR_CAPI_EXPORTED bool mlirOperationEqual (MlirOperation op ,
623
626
MlirOperation other );
624
627
628
+ /// Compute a hash for the given operation.
629
+ MLIR_CAPI_EXPORTED size_t mlirOperationHashValue (MlirOperation op );
630
+
625
631
/// Gets the context this operation is associated with
626
632
MLIR_CAPI_EXPORTED MlirContext mlirOperationGetContext (MlirOperation op );
627
633
Original file line number Diff line number Diff line change @@ -3317,7 +3317,9 @@ void mlir::python::populateIRCore(nb::module_ &m) {
3317
3317
[](PyModule &self, PyModule &other) {
3318
3318
return mlirModuleEqual (self.get (), other.get ());
3319
3319
},
3320
- " other" _a);
3320
+ " other" _a)
3321
+ .def (" __hash__" ,
3322
+ [](PyModule &self) { return mlirModuleHashValue (self.get ()); });
3321
3323
3322
3324
// ----------------------------------------------------------------------------
3323
3325
// Mapping of Operation.
@@ -3336,7 +3338,7 @@ void mlir::python::populateIRCore(nb::module_ &m) {
3336
3338
[](PyOperationBase &self, nb::object other) { return false ; })
3337
3339
.def (" __hash__" ,
3338
3340
[](PyOperationBase &self) {
3339
- return static_cast < size_t >( llvm::hash_value (& self.getOperation ()));
3341
+ return mlirOperationHashValue ( self.getOperation (). get ( ));
3340
3342
})
3341
3343
.def_prop_ro (" attributes" ,
3342
3344
[](PyOperationBase &self) {
Original file line number Diff line number Diff line change @@ -469,6 +469,10 @@ bool mlirModuleEqual(MlirModule lhs, MlirModule rhs) {
469
469
return unwrap (lhs) == unwrap (rhs);
470
470
}
471
471
472
+ size_t mlirModuleHashValue (MlirModule mod) {
473
+ return OperationEquivalence::computeHash (unwrap (mod).getOperation ());
474
+ }
475
+
472
476
// ===----------------------------------------------------------------------===//
473
477
// Operation state API.
474
478
// ===----------------------------------------------------------------------===//
@@ -640,6 +644,10 @@ bool mlirOperationEqual(MlirOperation op, MlirOperation other) {
640
644
return unwrap (op) == unwrap (other);
641
645
}
642
646
647
+ size_t mlirOperationHashValue (MlirOperation op) {
648
+ return OperationEquivalence::computeHash (unwrap (op));
649
+ }
650
+
643
651
MlirContext mlirOperationGetContext (MlirOperation op) {
644
652
return wrap (unwrap (op)->getContext ());
645
653
}
Original file line number Diff line number Diff line change @@ -1051,6 +1051,12 @@ def testOperationHash():
1051
1051
op = Operation .create ("custom.op1" )
1052
1052
assert hash (op ) == hash (op .operation )
1053
1053
1054
+ module = Module .create ()
1055
+ with InsertionPoint (module .body ):
1056
+ op2 = Operation .create ("custom.op2" )
1057
+ custom_op2 = module .body .operations [0 ]
1058
+ assert hash (op2 ) == hash (custom_op2 )
1059
+
1054
1060
1055
1061
# CHECK-LABEL: TEST: testOperationParse
1056
1062
@run
You can’t perform that action at this time.
0 commit comments