Skip to content

Commit b727b55

Browse files
authored
Revert "Adding special slice attribute (#391)" (#561)
This reverts commit dfcd096. This removes `SliceAttribute` which was introduced to support hashing (for CSE). Closes #559
1 parent a664b05 commit b727b55

File tree

4 files changed

+6
-94
lines changed

4 files changed

+6
-94
lines changed

src/kirin/dialects/py/indexing.py

Lines changed: 2 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -98,17 +98,7 @@ class Concrete(interp.MethodTable):
9898

9999
@interp.impl(GetItem)
100100
def getindex(self, interp, frame: interp.Frame, stmt: GetItem):
101-
from kirin.dialects.py.slice import SliceAttribute
102-
103-
index = frame.get(stmt.index)
104-
105-
# need to handle special case of slice attribute
106-
if isinstance(index, SliceAttribute):
107-
index_value = index.unwrap()
108-
else:
109-
index_value = index
110-
111-
return (frame.get(stmt.obj)[index_value],)
101+
return (frame.get(stmt.obj)[frame.get(stmt.index)],)
112102

113103

114104
@dialect.register(key="typeinfer")
@@ -218,16 +208,7 @@ def getitem(
218208
return (const.Unknown(),)
219209

220210
if isinstance(obj, const.Value):
221-
from kirin.dialects.py.slice import SliceAttribute
222-
223-
# need to handle special case of slice attribute
224-
if isinstance(index.data, SliceAttribute):
225-
index_value = index.data.unwrap()
226-
else:
227-
index_value = index.data
228-
229-
return (const.Value(obj.data[index_value]),)
230-
211+
return (const.Value(obj.data[index.data]),)
231212
elif isinstance(obj, const.PartialTuple):
232213
obj = obj.data
233214
if isinstance(index.data, int) and 0 <= index.data < len(obj):

src/kirin/dialects/py/slice.py

Lines changed: 3 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@
1313

1414
from kirin import ir, types, interp, lowering
1515
from kirin.decl import info, statement
16-
from kirin.print.printer import Printer
1716
from kirin.dialects.py.constant import Constant
1817

1918
dialect = ir.Dialect("py.slice")
@@ -63,55 +62,18 @@ def __init__(
6362
)
6463

6564

66-
@dataclass
67-
class SliceAttribute(ir.Data[slice]):
68-
69-
start: int | None
70-
stop: int | None
71-
step: int | None
72-
73-
def __post_init__(self) -> None:
74-
if self.start is None and self.step is None:
75-
self.type = types.Slice[types.Literal(self.stop)]
76-
else:
77-
self.type = types.Slice3[
78-
types.Literal(self.start),
79-
types.Literal(self.stop),
80-
types.Literal(self.step),
81-
]
82-
83-
def unwrap(self):
84-
return slice(self.start, self.stop, self.step)
85-
86-
def __hash__(self):
87-
return hash((type(self), self.start, self.stop, self.step))
88-
89-
def print_impl(self, printer: Printer) -> None:
90-
return printer.plain_print(f"slice({self.start}, {self.stop}, {self.step})")
91-
92-
def is_structurally_equal(
93-
self, other: ir.Attribute, context: dict | None = None
94-
) -> bool:
95-
return (
96-
isinstance(other, SliceAttribute)
97-
and self.start == other.start
98-
and self.stop == other.stop
99-
and self.step == other.step
100-
)
101-
102-
10365
@dialect.register
10466
class Concrete(interp.MethodTable):
10567

10668
@interp.impl(Slice)
10769
def _slice(self, interp, frame: interp.Frame, stmt: Slice):
10870
start, stop, step = frame.get_values(stmt.args)
10971
if start is None and step is None:
110-
return (SliceAttribute(None, stop, None),)
72+
return (slice(stop),)
11173
elif step is None:
112-
return (SliceAttribute(start, stop, None),)
74+
return (slice(start, stop),)
11375
else:
114-
return (SliceAttribute(start, stop, step),)
76+
return (slice(start, stop, step),)
11577

11678

11779
@dialect.register

src/kirin/types.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,6 @@
2828
NoneType = PyClass(type(None))
2929
List = Generic(list, TypeVar("T"))
3030
Slice = Generic(slice, TypeVar("T"))
31-
Slice3 = Generic(slice, TypeVar("T1"), TypeVar("T2"), TypeVar("T3"))
3231
Tuple = Generic(tuple, Vararg(TypeVar("T")))
3332
Dict = Generic(dict, TypeVar("K"), TypeVar("V"))
3433
Set = Generic(set, TypeVar("T"))

test/dialects/pystmts/test_slice.py

Lines changed: 1 addition & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
from kirin import types
22
from kirin.prelude import basic_no_opt
3-
from kirin.dialects import py, ilist
4-
from kirin.dialects.py.slice import SliceAttribute
3+
from kirin.dialects import py
54

65

76
@basic_no_opt
@@ -45,32 +44,3 @@ def test_wrong_slice():
4544

4645
stmt: py.slice.Slice = wrong_slice.code.body.blocks[0].stmts.at(7)
4746
assert stmt.result.type.is_subseteq(types.Bottom)
48-
49-
50-
def test_slice_attr():
51-
52-
@basic_no_opt
53-
def test():
54-
55-
return (slice(0, 20), slice(30), slice(1, 40, 5))
56-
57-
result = test()
58-
assert result == (
59-
SliceAttribute(0, 20, None),
60-
SliceAttribute(None, 30, None),
61-
SliceAttribute(1, 40, 5),
62-
)
63-
64-
65-
def test_slice_attr_hash():
66-
assert hash(SliceAttribute(0, 20, None)) == hash((SliceAttribute, 0, 20, None))
67-
68-
69-
def test_slice_get_index():
70-
@basic_no_opt
71-
def test():
72-
x = slice(0, 20, None)
73-
y = range(40)
74-
return y[x]
75-
76-
assert test() == ilist.IList(range(0, 20, 1))

0 commit comments

Comments
 (0)