-
Notifications
You must be signed in to change notification settings - Fork 2
Fix reverse slicing edge case #562
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
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,97 @@ | ||
| import pytest | ||
|
|
||
| from kirin import types | ||
| from kirin.prelude import basic_no_opt | ||
| from kirin.rewrite import Walk, Chain, Fixpoint, WrapConst | ||
| from kirin.analysis import const | ||
| from kirin.dialects import ilist | ||
| from kirin.rewrite.dce import DeadCodeElimination | ||
| from kirin.dialects.py.indexing import GetItem | ||
| from kirin.dialects.ilist.rewrite.inline_getitem import InlineGetItem | ||
|
|
||
|
|
||
| def apply_getitem_optimization(func): | ||
| constprop = const.Propagate(func.dialects) | ||
| frame, _ = constprop.run(func) | ||
| Fixpoint(Walk(WrapConst(frame))).rewrite(func.code) | ||
| inline_getitem = InlineGetItem() | ||
| Fixpoint(Walk(Chain([inline_getitem, DeadCodeElimination()]))).rewrite(func.code) | ||
|
|
||
|
|
||
| @pytest.mark.parametrize("index", [0, -1, 1]) | ||
| def test_getitem_index(index): | ||
| index = 0 | ||
|
|
||
| @basic_no_opt | ||
| def func(x: int): | ||
| ylist = ilist.New(values=(x, x, 1, x), elem_type=types.PyClass(int)) | ||
| return ylist[index] | ||
|
|
||
| before = func(1) | ||
| apply_getitem_optimization(func) | ||
| after = func(1) | ||
|
|
||
| assert before == after | ||
| assert len(func.callable_region.blocks[0].stmts) == 1 | ||
|
|
||
|
|
||
| @pytest.mark.parametrize( | ||
| "sl", | ||
| [ | ||
| slice(0, 2, 1), | ||
| slice(None, None, None), | ||
| slice(None, -1, None), | ||
| slice(-1, None, None), | ||
| slice(None, None, -1), | ||
| slice(1, 4, 2), | ||
| ], | ||
| ) | ||
| def test_getitem_slice(sl): | ||
|
|
||
| @basic_no_opt | ||
| def func(): | ||
| ylist = ilist.New(values=(0, 1, 2, 3, 4), elem_type=types.PyClass(int)) | ||
| return ylist[sl] | ||
|
|
||
| stmt_types = [type(stmt) for stmt in func.callable_region.blocks[0].stmts] | ||
| assert GetItem in stmt_types | ||
|
|
||
| before = func() | ||
| apply_getitem_optimization(func) | ||
| after = func() | ||
|
|
||
| assert before == after | ||
| stmt_types = [type(stmt) for stmt in func.callable_region.blocks[0].stmts] | ||
| assert GetItem not in stmt_types | ||
|
|
||
|
|
||
| @pytest.mark.parametrize( | ||
| "start, stop, step", | ||
| [ | ||
| (0, 2, 1), | ||
| (None, None, None), | ||
| (None, -1, None), | ||
| (-1, None, None), | ||
| (None, None, -1), | ||
| (1, 4, 2), | ||
| ], | ||
| ) | ||
| def test_getitem_slice_with_literal_indices(start, stop, step): | ||
|
|
||
| @basic_no_opt | ||
| def func(): | ||
| ylist = ilist.New(values=(0, 1, 2, 3, 4), elem_type=types.PyClass(int)) | ||
| return ylist[start:stop:step] | ||
|
|
||
| stmt_types = [type(stmt) for stmt in func.callable_region.blocks[0].stmts] | ||
| assert GetItem in stmt_types | ||
|
|
||
| before = func() | ||
|
|
||
| apply_getitem_optimization(func) | ||
|
|
||
| stmt_types = [type(stmt) for stmt in func.callable_region.blocks[0].stmts] | ||
| assert GetItem not in stmt_types | ||
| after = func() | ||
|
|
||
| assert before == after |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,26 +1,94 @@ | ||
| import pytest | ||
|
|
||
| from kirin.prelude import basic_no_opt | ||
| from kirin.rewrite import Walk, Chain, Fixpoint, WrapConst | ||
| from kirin.analysis import const | ||
| from kirin.rewrite.dce import DeadCodeElimination | ||
| from kirin.rewrite.getitem import InlineGetItem | ||
| from kirin.dialects.py.indexing import GetItem | ||
|
|
||
|
|
||
| @basic_no_opt | ||
| def main_simplify_getitem(x: int): | ||
| ylist = (x, x, 1, 2) | ||
| return ylist[0] | ||
| def apply_getitem_optimization(func): | ||
| constprop = const.Propagate(func.dialects) | ||
| frame, _ = constprop.run(func) | ||
| Fixpoint(Walk(WrapConst(frame))).rewrite(func.code) | ||
| inline_getitem = InlineGetItem() | ||
| Fixpoint(Walk(Chain([inline_getitem, DeadCodeElimination()]))).rewrite(func.code) | ||
|
|
||
|
|
||
| def test_getitem(): | ||
| before = main_simplify_getitem(1) | ||
| constprop = const.Propagate(main_simplify_getitem.dialects) | ||
| frame, _ = constprop.run(main_simplify_getitem) | ||
| Fixpoint(Walk(WrapConst(frame))).rewrite(main_simplify_getitem.code) | ||
| inline_getitem = InlineGetItem() | ||
| Fixpoint(Walk(Chain([inline_getitem, DeadCodeElimination()]))).rewrite( | ||
| main_simplify_getitem.code | ||
| ) | ||
| main_simplify_getitem.code.print() | ||
| after = main_simplify_getitem(1) | ||
| @pytest.mark.parametrize("index", [0, -1, 1]) | ||
| def test_getitem_index(index): | ||
|
|
||
| @basic_no_opt | ||
| def func(x: int): | ||
| ylist = (x, x, 1, x) | ||
| return ylist[index] | ||
|
|
||
| before = func(1) | ||
| apply_getitem_optimization(func) | ||
| after = func(1) | ||
|
|
||
| assert before == after | ||
| assert len(func.callable_region.blocks[0].stmts) == 1 | ||
|
|
||
|
|
||
| @pytest.mark.parametrize( | ||
| "sl", | ||
| [ | ||
| slice(0, 2, 1), | ||
| slice(None, None, None), | ||
| slice(None, -1, None), | ||
| slice(-1, None, None), | ||
| slice(None, None, -1), | ||
| slice(1, 4, 2), | ||
| ], | ||
| ) | ||
| def test_getitem_slice(sl): | ||
|
|
||
| @basic_no_opt | ||
| def func(): | ||
| ylist = (0, 1, 2, 3, 4) | ||
| return ylist[sl] | ||
|
|
||
| stmt_types = [type(stmt) for stmt in func.callable_region.blocks[0].stmts] | ||
| assert GetItem in stmt_types | ||
|
|
||
| before = func() | ||
| apply_getitem_optimization(func) | ||
| after = func() | ||
|
|
||
| assert before == after | ||
| stmt_types = [type(stmt) for stmt in func.callable_region.blocks[0].stmts] | ||
| assert GetItem not in stmt_types | ||
|
|
||
|
|
||
| @pytest.mark.parametrize( | ||
| "start, stop, step", | ||
| [ | ||
| (0, 2, 1), | ||
| (None, None, None), | ||
| (None, -1, None), | ||
| (-1, None, None), | ||
| (None, None, -1), | ||
| (1, 4, 2), | ||
| ], | ||
| ) | ||
| def test_getitem_slice_with_literal_indices(start, stop, step): | ||
|
|
||
| @basic_no_opt | ||
| def func(): | ||
| ylist = (0, 1, 2, 3, 4) | ||
| return ylist[start:stop:step] | ||
|
|
||
| stmt_types = [type(stmt) for stmt in func.callable_region.blocks[0].stmts] | ||
| assert GetItem in stmt_types | ||
|
|
||
| before = func() | ||
|
|
||
| apply_getitem_optimization(func) | ||
|
|
||
| stmt_types = [type(stmt) for stmt in func.callable_region.blocks[0].stmts] | ||
| assert GetItem not in stmt_types | ||
| after = func() | ||
|
|
||
| assert before == after | ||
| assert len(main_simplify_getitem.callable_region.blocks[0].stmts) == 1 |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This file is almost the same as
getitem.py(list vs ilist)Can code duplication be avoided?