This is an archive of the discontinued LLVM Phabricator instance.

[mlir][python] Make Operation and Value hashable
ClosedPublic

Authored by rkayaith on Oct 27 2021, 3:28 PM.

Details

Summary

This allows operations and values to be used as dict keys

Diff Detail

Event Timeline

rkayaith created this revision.Oct 27 2021, 3:28 PM
rkayaith published this revision for review.Oct 27 2021, 8:14 PM
ftynse requested changes to this revision.Oct 27 2021, 11:52 PM
ftynse added inline comments.
mlir/lib/Bindings/Python/IRCore.cpp
2175

Let's use llvm::hash_value that has the desirable properties of hash values instead of blindly reusing the pointer. Also, please prefer C++ static_cast to C-style casts in C++ code.

This revision now requires changes to proceed.Oct 27 2021, 11:52 PM
ftynse added inline comments.Oct 27 2021, 11:55 PM
mlir/test/python/ir/operation.py
754

I don't think hashes are guaranteed to be different. They happen to be because they are currently pointer addresses.

rkayaith added inline comments.Oct 28 2021, 4:01 PM
mlir/lib/Bindings/Python/IRCore.cpp
2175

Should I update the hash functions for PyType and PyAttribute this way as well?

Also I noticed that there's llvm::hash_value overloads for Value, Type, and Attribute (but not Operation) defined already, should I thread those through the C bindings and use them here? Or is it sufficient to just hash the pointers that are currently being returned?

rkayaith updated this revision to Diff 383666.Oct 31 2021, 11:42 AM

hash pointers instead of returning them directly

rkayaith added inline comments.Oct 31 2021, 11:50 AM
mlir/lib/Bindings/Python/IRCore.cpp
2175

I went with hashing the pointers in here for now, and also updated PyType and PyAttribute to do this as well.

mlir/test/python/ir/operation.py
754

I removed all the tests that checked for hashes being different, since pointer addresses are no longer being used directly.

ftynse accepted this revision.Nov 2 2021, 4:46 AM

Let's hash pointers for now, my primary concern was the loss of hash function properties that defies the purpose of hash tables. FYI, there's no llvm::hash_value overload for Operation because we never use operations by value, only by pointer, and LLVM already knows how to hash pointers.

This revision is now accepted and ready to land.Nov 2 2021, 4:46 AM

Thanks, makes sense. Could you land the changes for me?

This revision was automatically updated to reflect the committed changes.