Fix Vector list()-iteration hang #873
Open
adityas-amd wants to merge 3 commits into
Open
Conversation
Vector defined __getitem__ but neither __iter__ nor __len__, so list(Vec(buffer_load(vec_width>1))) fell back to CPython's legacy sequence protocol (v[0], v[1], ...). That protocol only stops on IndexError, and the integer branch of __getitem__ never raised one, so tracing spun forever emitting vector.extract ops and never finalized any IR. - Add __len__ (returns numel) and __iter__ (yields the numel lanes). - Bounds-check the integer branch of __getitem__: normalize negative indices and raise IndexError when out of range. - Add tests/unit/test_vector_iteration.py regression coverage.
Contributor
There was a problem hiding this comment.
Pull request overview
Fixes a tracing hang when list(Vector(...)) is called on multi-lane vectors by making Vector a well-behaved Python sequence: it now has a terminating iterator/length and raises IndexError for out-of-bounds integer indexing.
Changes:
- Add
Vector.__len__andVector.__iter__solist(vec),for x in vec, and unpacking terminate correctly. - Add bounds checking + negative-index normalization to the integer branch of
Vector.__getitem__. - Add backend-agnostic unit tests covering list conversion,
len(), iteration, negative indexing, and OOB indexing.
Reviewed changes
Copilot reviewed 2 out of 2 changed files in this pull request and generated 2 comments.
| File | Description |
|---|---|
python/flydsl/expr/typing.py |
Implements __len__/__iter__ and adds integer-index bounds checking to prevent infinite list() fallback iteration. |
tests/unit/test_vector_iteration.py |
Adds regression tests to ensure vector iteration and indexing terminate/raise correctly. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
Add blank line after the docstring in test_negative_index to satisfy the repo's black style check (pre-checks CI).
- Split the iteration test: keep list-comprehension coverage as test_vector_iteration and add test_vector_unpacking exercising the tuple-unpacking path (a, b, c = v). - Report the caller-supplied index in the out-of-range IndexError instead of the negative-normalized value (e.g. v[-5] now says -5, not -1).
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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Summary
Fixes #793.
list(Vec(buffer_load(vec_width>1)))hung indefinitely during tracing and never emitted IR.(https://github.com/ROCm/FlyDSL/blob/main/python/flydsl/expr/typing.py) defines
__getitem__but neither__iter__nor__len__. When an object has__getitem__and no__iter__,list()falls back to CPython's legacy sequence protocol —v[0], v[1], v[2], …- which stops only when__getitem__raisesIndexError.The integer branch never bounds-checked and never raised, so tracing looped forever, emitting an
unbounded stream of
vector.extractops and finalizing no IR.Fix
__len__(returnsnumel) and__iter__(yields thenumellanes) solist(), unpacking, andforiterate correctly and terminate.__getitem__: normalize negative indicesand raise a clear
IndexErrorwhen out of range, instead of silently emittingan out-of-range extract.
Behavior
list(vec)(numel=4)vector.extract[v0, v1, v2, v3]len(vec)TypeError4for x in vec/ unpackingvec[4](OOB)IndexErrorvec[-1]Tests
tests/unit/test_vector_iteration.py(5 cases,l0_backend_agnostic): list termination,len, unpacking, OOBIndexError, negative indexing.