Skip to content

Adding special slice attribute #391

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
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 21 additions & 2 deletions src/kirin/dialects/py/indexing.py
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,17 @@

@interp.impl(GetItem)
def getindex(self, interp, frame: interp.Frame, stmt: GetItem):
return (frame.get(stmt.obj)[frame.get(stmt.index)],)
from kirin.dialects.py.slice import SliceAttribute

index = frame.get(stmt.index)

# need to handle special case of slice attribute
if isinstance(index, SliceAttribute):
index_value = index.unwrap()

Check warning on line 107 in src/kirin/dialects/py/indexing.py

View check run for this annotation

Codecov / codecov/patch

src/kirin/dialects/py/indexing.py#L107

Added line #L107 was not covered by tests
else:
index_value = index

return (frame.get(stmt.obj)[index_value],)


@dialect.register(key="typeinfer")
Expand Down Expand Up @@ -208,7 +218,16 @@
return (const.Unknown(),)

if isinstance(obj, const.Value):
return (const.Value(obj.data[index.data]),)
from kirin.dialects.py.slice import SliceAttribute

# need to handle special case of slice attribute
if isinstance(index.data, SliceAttribute):
index_value = index.data.unwrap()

Check warning on line 225 in src/kirin/dialects/py/indexing.py

View check run for this annotation

Codecov / codecov/patch

src/kirin/dialects/py/indexing.py#L225

Added line #L225 was not covered by tests
else:
index_value = index.data

return (const.Value(obj.data[index_value]),)

elif isinstance(obj, const.PartialTuple):
obj = obj.data
if isinstance(index.data, int) and 0 <= index.data < len(obj):
Expand Down
34 changes: 31 additions & 3 deletions src/kirin/dialects/py/slice.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@

from kirin import ir, types, interp, lowering
from kirin.decl import info, statement
from kirin.print.printer import Printer
from kirin.dialects.py.constant import Constant

dialect = ir.Dialect("py.slice")
Expand Down Expand Up @@ -62,18 +63,45 @@
)


@dataclass
class SliceAttribute(ir.Data[slice]):

start: int | None
stop: int | None
step: int | None

def __post_init__(self) -> None:
if self.start is None and self.step is None:
self.type = types.Slice[types.Literal(self.stop)]

Check warning on line 75 in src/kirin/dialects/py/slice.py

View check run for this annotation

Codecov / codecov/patch

src/kirin/dialects/py/slice.py#L75

Added line #L75 was not covered by tests
else:
self.type = types.Slice3[
types.Literal(self.start),
types.Literal(self.stop),
types.Literal(self.step),
]

def unwrap(self):
return slice(self.start, self.stop, self.step)

def __hash__(self):
return hash((type(self), slice, self.start, self.stop, self.step))

Check warning on line 87 in src/kirin/dialects/py/slice.py

View check run for this annotation

Codecov / codecov/patch

src/kirin/dialects/py/slice.py#L87

Added line #L87 was not covered by tests

def print_impl(self, printer: Printer) -> None:
return printer.plain_print(f"slice({self.start}, {self.stop}, {self.step})")

Check warning on line 90 in src/kirin/dialects/py/slice.py

View check run for this annotation

Codecov / codecov/patch

src/kirin/dialects/py/slice.py#L90

Added line #L90 was not covered by tests


@dialect.register
class Concrete(interp.MethodTable):

@interp.impl(Slice)
def _slice(self, interp, frame: interp.Frame, stmt: Slice):
start, stop, step = frame.get_values(stmt.args)
if start is None and step is None:
return (slice(stop),)
return (SliceAttribute(None, stop, None),)

Check warning on line 100 in src/kirin/dialects/py/slice.py

View check run for this annotation

Codecov / codecov/patch

src/kirin/dialects/py/slice.py#L100

Added line #L100 was not covered by tests
elif step is None:
return (slice(start, stop),)
return (SliceAttribute(start, stop, None),)
else:
return (slice(start, stop, step),)
return (SliceAttribute(start, stop, step),)

Check warning on line 104 in src/kirin/dialects/py/slice.py

View check run for this annotation

Codecov / codecov/patch

src/kirin/dialects/py/slice.py#L104

Added line #L104 was not covered by tests


@dialect.register
Expand Down
1 change: 1 addition & 0 deletions src/kirin/types.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
NoneType = PyClass(type(None))
List = Generic(list, TypeVar("T"))
Slice = Generic(slice, TypeVar("T"))
Slice3 = Generic(slice, TypeVar("T1"), TypeVar("T2"), TypeVar("T3"))
Tuple = Generic(tuple, Vararg(TypeVar("T")))
Dict = Generic(dict, TypeVar("K"), TypeVar("V"))
Set = Generic(set, TypeVar("T"))
Expand Down