Skip to content

Commit 354385e

Browse files
authored
Adding fallback when attribute isn't hashable (#479)
1 parent 9abe717 commit 354385e

File tree

1 file changed

+25
-13
lines changed

1 file changed

+25
-13
lines changed

src/kirin/rewrite/cse.py

Lines changed: 25 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,13 @@
11
from __future__ import annotations
22

3-
from dataclasses import dataclass
3+
import sys
4+
from dataclasses import field, dataclass
45

56
from kirin import ir
67
from kirin.rewrite.abc import RewriteRule, RewriteResult
78

9+
_IS_PYTHON_310 = sys.version_info[0] == 3 and sys.version_info[1] == 10
10+
811

912
@dataclass
1013
class Info:
@@ -15,22 +18,31 @@ class Info:
1518
attributes: tuple[ir.Attribute, ...]
1619
successors: tuple[ir.Block, ...]
1720
regions: tuple[ir.Region, ...]
21+
_hash: int = field(init=False, repr=False)
22+
_hashable: bool = field(init=False, repr=False)
23+
24+
def __post_init__(self):
25+
if _IS_PYTHON_310 and any(isinstance(attr, slice) for attr in self.attributes):
26+
self._hash = id(self)
27+
self._hashable = False
28+
else:
29+
self._hash = hash(
30+
(id(self.head),)
31+
+ tuple(id(ssa) for ssa in self.args)
32+
+ tuple(hash(attr) for attr in self.attributes)
33+
+ tuple(id(succ) for succ in self.successors)
34+
+ tuple(id(region) for region in self.regions)
35+
)
36+
self._hashable = True
1837

1938
def __hash__(self) -> int:
20-
return hash(
21-
(id(self.head),)
22-
+ tuple(id(ssa) for ssa in self.args)
23-
+ tuple(hash(attr) for attr in self.attributes)
24-
+ tuple(id(succ) for succ in self.successors)
25-
+ tuple(id(region) for region in self.regions)
26-
)
39+
return self._hash
2740

2841
def __eq__(self, other: object) -> bool:
29-
if not isinstance(other, Info):
30-
return False
31-
32-
return (
33-
self.head == other.head
42+
return self is other or (
43+
self._hashable
44+
and isinstance(other, Info)
45+
and self.head == other.head
3446
and self.args == other.args
3547
and self.attributes == other.attributes
3648
and self.successors == other.successors

0 commit comments

Comments
 (0)