Skip to content

Unhashable built-in type: slice #388

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

Open
weinbe58 opened this issue May 1, 2025 · 4 comments · May be fixed by #391
Open

Unhashable built-in type: slice #388

weinbe58 opened this issue May 1, 2025 · 4 comments · May be fixed by #391
Assignees
Labels
bug Something isn't working

Comments

@weinbe58
Copy link
Member

weinbe58 commented May 1, 2025

Using the default hash for slice doesn't work for python < 3.12

from kirin.dialects import ilist
from kirin.prelude import structural
from kirin.rewrite import CommonSubexpressionElimination, Walk, ConstantFold, Chain
from kirin.passes import HintConst


@structural(fold=False)
def test():
    a = ilist.range(0, 100)
    
    return (a[::20], a[::20])
    
HintConst(dialects=test.dialects)(test)  


Walk(Chain(ConstantFold(), CommonSubexpressionElimination())).rewrite(test.code)

stack trace:

Traceback (most recent call last):
  File "/Users/pweinberg/Documents/atom_compiler_dev/kirin-workspace/debug/mwe.py", line 17, in <module>
    Walk(Chain(ConstantFold(), CommonSubexpressionElimination())).rewrite(test.code)
  File "/Users/pweinberg/Documents/atom_compiler_dev/kirin-workspace/kirin/src/kirin/rewrite/walk.py", line 38, in rewrite
    result = self.rule.rewrite(subnode)
  File "/Users/pweinberg/Documents/atom_compiler_dev/kirin-workspace/kirin/src/kirin/rewrite/chain.py", line 29, in rewrite
    result = rule.rewrite(node)
  File "/Users/pweinberg/Documents/atom_compiler_dev/kirin-workspace/kirin/src/kirin/rewrite/abc.py", line 37, in rewrite
    return self.rewrite_Statement(node)
  File "/Users/pweinberg/Documents/atom_compiler_dev/kirin-workspace/kirin/src/kirin/rewrite/cse.py", line 44, in rewrite_Statement
    result = self.rewrite_Block(block)
  File "/Users/pweinberg/Documents/atom_compiler_dev/kirin-workspace/kirin/src/kirin/rewrite/cse.py", line 20, in rewrite_Block
    hash_value = hash(
  File "/Users/pweinberg/Documents/atom_compiler_dev/kirin-workspace/kirin/src/kirin/ir/attrs/py.py", line 38, in __hash__
    return hash(self.data) + hash(self.type)
TypeError: unhashable type: 'slice'
@weinbe58 weinbe58 added the bug Something isn't working label May 1, 2025
@weinbe58 weinbe58 changed the title Unhashable built-in value: slice Unhashable built-in type: slice May 1, 2025
@Roger-luo
Copy link
Member

right I think the fix is just define an Attribute e.g

class Slice(Attribute):
     start: int
     stop: int
     step: int

and lower all slice into this attribute.

@weinbe58
Copy link
Member Author

weinbe58 commented May 1, 2025

What if the input values are not literals when lowering?

@Roger-luo
Copy link
Member

then it won't be attribute, but a statement? https://github.com/QuEraComputing/kirin/blob/main/src/kirin/dialects/py/slice.py

@weinbe58
Copy link
Member Author

weinbe58 commented May 2, 2025

Sure, I'll give it a shot, so far seems a bit tricky because of how const fold works

@weinbe58 weinbe58 self-assigned this May 2, 2025
@weinbe58 weinbe58 linked a pull request May 2, 2025 that will close this issue
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants