Skip to content

Fix Vector list()-iteration hang #873

Open
adityas-amd wants to merge 3 commits into
ROCm:mainfrom
adityas-amd:adityas-amd/fix-vector-list-hang
Open

Fix Vector list()-iteration hang #873
adityas-amd wants to merge 3 commits into
ROCm:mainfrom
adityas-amd:adityas-amd/fix-vector-list-hang

Conversation

@adityas-amd

Copy link
Copy Markdown
Member

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__ raises IndexError.

The integer branch never bounds-checked and never raised, so tracing looped forever, emitting an
unbounded stream of vector.extract ops and finalizing no IR.

Fix

  • Add __len__ (returns numel) and __iter__ (yields the numel lanes) so
    list(), unpacking, and for iterate correctly and terminate.
  • Bounds-check the integer branch of __getitem__: normalize negative indices
    and raise a clear IndexError when out of range, instead of silently emitting
    an out-of-range extract.

Behavior

Case Before After
list(vec) (numel=4) hangs, unbounded vector.extract [v0, v1, v2, v3]
len(vec) TypeError 4
for x in vec / unpacking infinite loop iterates the lanes
vec[4] (OOB) invalid extract, no error IndexError
vec[-1] invalid negative position last lane

Tests

  • New tests/unit/test_vector_iteration.py (5 cases, l0_backend_agnostic): list termination, len, unpacking, OOB IndexError, negative indexing.
  • Verified the new termination test hangs on the unpatched tree (killed by timeout) and passes with this change.
  • Full backend-agnostic unit sweep: 205 passed, 0 failed.

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.
Copilot AI review requested due to automatic review settings July 18, 2026 04:34

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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__ and Vector.__iter__ so list(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.

Comment thread tests/unit/test_vector_iteration.py Outdated
Comment thread python/flydsl/expr/typing.py Outdated
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).
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

list(Vector(buffer_load(vec_width>1))) hangs Python tracing (unbounded recursion, no IR emitted)

2 participants