Skip to content

CSE crashes when a py.constant wraps an unhashable value #662

Description

@zhenrongliew

Summary

CSE hashes every statement attribute via
Info.__post_init__, which calls hash(attr)PyAttr.__hash__
hash((self.type, self.data)). When a py.constant wraps an unhashable
Python value (e.g. a default @dataclass instance), this raises
TypeError: unhashable type: ... from deep inside an optimization pass,
instead of a clean, actionable error.

Minimal reproduction

from dataclasses import dataclass

from kirin import ir
from kirin.dialects.py.constant import Constant
from kirin.rewrite.cse import CommonSubexpressionElimination

# A default dataclass instance is UNHASHABLE (eq=True sets __hash__ = None)
@dataclass
class DataclassObj:
    x: int = 1

obj = DataclassObj()

blk = ir.Block()
blk.stmts.append(Constant(obj))
blk.stmts.append(Constant(obj))

CommonSubexpressionElimination().rewrite_Block(blk)
# TypeError: unhashable type: 'DataclassObj'

Metadata

Metadata

Assignees

Labels

area: compiler passArea: compiler pass related issues.category: bugCategory: this is a bug or something isn't working as expected.

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions