-
-
Couldn't load subscription status.
- Fork 3k
[mypyc] feat: enhance ForSequence with start, stop, step for IndexExpr case #20106
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
Draft
BobTheBuidler
wants to merge
15
commits into
python:master
Choose a base branch
from
BobTheBuidler:patch-23
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+56
−9
Draft
Changes from 11 commits
Commits
Show all changes
15 commits
Select commit
Hold shift + click to select a range
d161662
[mypyc] feat: enhance ForSequence with start, stop, step
BobTheBuidler fcd4264
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] e4835ae
Update for_helpers.py
BobTheBuidler 7dd362a
Update for_helpers.py
BobTheBuidler 17a5d6d
Update for_helpers.py
BobTheBuidler 2ac9a73
Update for_helpers.py
BobTheBuidler 62adcfa
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] 2e1b999
Update for_helpers.py
BobTheBuidler 090a440
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] 0cf4c31
Update for_helpers.py
BobTheBuidler fbf88f6
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] 5d48f2e
Update for_helpers.py
BobTheBuidler 662b030
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] c1f1096
Update for_helpers.py
BobTheBuidler ff72e93
Update for_helpers.py
BobTheBuidler 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -16,12 +16,14 @@ | |
| DictionaryComprehension, | ||
| Expression, | ||
| GeneratorExpr, | ||
| IndexExpr, | ||
| ListExpr, | ||
| Lvalue, | ||
| MemberExpr, | ||
| NameExpr, | ||
| RefExpr, | ||
| SetExpr, | ||
| SliceExpr, | ||
| StarExpr, | ||
| StrExpr, | ||
| TupleExpr, | ||
|
|
@@ -67,6 +69,7 @@ | |
| short_int_rprimitive, | ||
| ) | ||
| from mypyc.irbuild.builder import IRBuilder | ||
| from mypyc.irbuild.constant_fold import constant_fold_expr | ||
| from mypyc.irbuild.prepare import GENERATOR_HELPER_NAME | ||
| from mypyc.irbuild.targets import AssignmentTarget, AssignmentTargetTuple | ||
| from mypyc.primitives.dict_ops import ( | ||
|
|
@@ -436,12 +439,32 @@ def make_for_loop_generator( | |
|
|
||
| rtyp = builder.node_type(expr) | ||
| if is_sequence_rprimitive(rtyp): | ||
| # Special case "for x in <list>". | ||
| expr_reg = builder.accept(expr) | ||
| # Special case "for x in <list/tuple/str/bytes>". | ||
| target_type = builder.get_sequence_type(expr) | ||
|
|
||
| for_list = ForSequence(builder, index, body_block, loop_exit, line, nested) | ||
| for_list.init(expr_reg, target_type, reverse=False) | ||
|
|
||
| if isinstance(expr, IndexExpr) and isinstance(expr.index, SliceExpr): | ||
| # TODO: maybe we must not apply this optimization to list type specifically | ||
| # because the need to check length changes at each iteration? | ||
| start = expr.index.start | ||
| stop = expr.index.stop | ||
| step = expr.index.step | ||
|
|
||
| if all( | ||
| s is None or isinstance(constant_fold_expr(builder, s), int) | ||
| for s in (start, stop, step) | ||
| ): | ||
| for_list.init( | ||
| builder.accept(expr.base), | ||
| target_type, | ||
| reverse=False, | ||
| start=constant_fold_expr(builder, start), | ||
| stop=constant_fold_expr(builder, stop), | ||
| step=constant_fold_expr(builder, step), | ||
| ) | ||
| return for_list | ||
|
|
||
| for_list.init(builder.accept(expr), target_type, reverse=False) | ||
| return for_list | ||
|
|
||
| if is_dict_rprimitive(rtyp): | ||
|
|
@@ -821,27 +844,50 @@ class ForSequence(ForGenerator): | |
| length_reg: Value | AssignmentTarget | None | ||
|
|
||
| def init( | ||
| self, expr_reg: Value, target_type: RType, reverse: bool, length: Value | None = None | ||
| self, | ||
| expr_reg: Value, | ||
| target_type: RType, | ||
| reverse: bool, | ||
| length: Value | None = None, | ||
| *, | ||
| start: int | None = None, | ||
| stop: int | None = None, | ||
| step: int | None = None, | ||
| ) -> None: | ||
| assert is_sequence_rprimitive(expr_reg.type), (expr_reg, expr_reg.type) | ||
| builder = self.builder | ||
| # Record a Value indicating the length of the sequence, if known at compile time. | ||
| self.length = length | ||
| self.reverse = reverse | ||
|
|
||
| self.start = 0 if start is None else start | ||
| assert self.start >= 0, "implement me!" | ||
|
|
||
| self.stop = -1 if stop is None else stop | ||
| assert self.stop == -1, "implement me!" | ||
|
|
||
| self.step = 1 if step is None else step | ||
| assert self.step and self.step >= 1, "this should be unreachable for step None and step 0" | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. this is currently reachable |
||
| if reverse: | ||
| self.step *= -1 | ||
|
|
||
| # Define target to contain the expression, along with the index that will be used | ||
| # for the for-loop. If we are inside of a generator function, spill these into the | ||
| # environment class. | ||
| self.expr_target = builder.maybe_spill(expr_reg) | ||
| if is_immutable_rprimitive(expr_reg.type): | ||
| # If the expression is an immutable type, we can load the length just once. | ||
| self.length_reg = builder.maybe_spill(self.length or self.load_len(self.expr_target)) | ||
| # TODO: if stop != -1 implement a safety check and then set self.stop_reg | ||
| # gen_condition will need to read stop_reg if present | ||
| else: | ||
| # Otherwise, even if the length is known, we must recalculate the length | ||
| # at every iteration for compatibility with python semantics. | ||
| self.length_reg = None | ||
| if not reverse: | ||
| index_reg: Value = Integer(0, c_pyssize_t_rprimitive) | ||
| index_reg: Value = Integer(self.start, c_pyssize_t_rprimitive) | ||
| else: | ||
| # TODO implement start logic | ||
| if self.length_reg is not None: | ||
| len_val = builder.read(self.length_reg) | ||
| else: | ||
|
|
@@ -854,6 +900,7 @@ def gen_condition(self) -> None: | |
| builder = self.builder | ||
| line = self.line | ||
| if self.reverse: | ||
| # TODO implement start stop step | ||
| # If we are iterating in reverse order, we obviously need | ||
| # to check that the index is still positive. Somewhat less | ||
| # obviously we still need to check against the length, | ||
|
|
@@ -898,8 +945,7 @@ def gen_step(self) -> None: | |
| # Step to the next item. | ||
| builder = self.builder | ||
| line = self.line | ||
| step = 1 if not self.reverse else -1 | ||
| add = builder.builder.int_add(builder.read(self.index_target, line), step) | ||
| add = builder.builder.int_add(builder.read(self.index_target, line), self.step) | ||
| builder.assign(self.index_target, add, line) | ||
|
|
||
|
|
||
|
|
||
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.
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 can probably still be valid for ListComp and ListExpr
not sure how useful that is though