Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Hashing for IR Nodes #119

Open
bifunctor opened this issue Jan 5, 2021 · 2 comments
Open

Hashing for IR Nodes #119

bifunctor opened this issue Jan 5, 2021 · 2 comments

Comments

@bifunctor
Copy link

Hello, I'm quite new to PPCI. While I was looking in ir.py, I was wondering why the nodes don't have __eq__ of __hash__ implemented in them. Is there a reason behine this? Maybe a way to get around it? Thanks!

@windelbouwman
Copy link
Owner

Hi @bifunctor ,

Good point! Never really thought about it, but it might be convenient to make the ir nodes hashable. One reason why they perhaps are not hashable is that they might be modified (in other words, they might not be immutable). For example the python list is mutable, and hence not hashable. A tuple on the other hand is immutable, and hence hashable. The IR nodes shoud probably be immutable, but I know that they are sometimes modified in place, for example in some optimization passes.

So in short: no specific reason, just never thought about it.

@obround
Copy link
Contributor

obround commented Jan 5, 2021

tl;dr: Its a good idea to implement __eq__, and (maybe) an okay idea to implement __hash__.

I believe that support should be added for __eq__ and __hash__ (or at very least __eq__). As you said, hashing might be problematic in the case of mutability. A way to get around this would be to hash only some defining attributes. For example, Block's name and function won't be changed (once function is set that is). So Block could be hashed as so:

class Block:
    ...
    def __hash__(self):
        assert self.function, "function should have been set"
        return hash((self.name, self.function))

or if you wish to be able to compare blocks without having added them to functions:

class Block:
    ...
    def __hash__(self):
        return hash(self.name)

and so on.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants