diff --git a/mlir/lib/Bindings/Python/IRCore.cpp b/mlir/lib/Bindings/Python/IRCore.cpp --- a/mlir/lib/Bindings/Python/IRCore.cpp +++ b/mlir/lib/Bindings/Python/IRCore.cpp @@ -2892,6 +2892,10 @@ return self.get().ptr == other.get().ptr; }) .def("__eq__", [](PyBlock &self, py::object &other) { return false; }) + .def("__hash__", + [](PyBlock &self) { + return static_cast(llvm::hash_value(self.get().ptr)); + }) .def( "__str__", [](PyBlock &self) { diff --git a/mlir/test/python/ir/blocks.py b/mlir/test/python/ir/blocks.py --- a/mlir/test/python/ir/blocks.py +++ b/mlir/test/python/ir/blocks.py @@ -94,3 +94,17 @@ block.append_to(realop.operation.regions[0]) dummy.operation.erase() print(module) + + +# CHECK-LABEL: TEST: testBlockHash +@run +def testBlockHash(): + with Context() as ctx, Location.unknown(): + ctx.allow_unregistered_dialects = True + module = Module.create() + f32 = F32Type.get() + with InsertionPoint(module.body): + dummy = Operation.create("dummy", regions=1) + block1 = Block.create_at_start(dummy.operation.regions[0], [f32]) + block2 = Block.create_at_start(dummy.operation.regions[0], [f32]) + assert hash(block1) != hash(block2)