Skip to content
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

Optimize range() to enable more auto-vectorization #9428

Draft
wants to merge 2 commits into
base: main
Choose a base branch
from

Conversation

sklam
Copy link
Member

@sklam sklam commented Feb 8, 2024

It is known that Numba's range() implementation is not optimal for LLVM loop optimizers. Even when one manually unroll loop for a constant loop bound, the way range() is implemented prohibits the loop-vectorizer to compute the loop bounds; thus failing to auto-vectorize in many cases.

This patch focuses on adjusting the ind-var computation to avoid computing reminders and instead relies mainly on additions, multiplications and floordiv which are common in low-level address computations.

There is still a reminder computation only if user ask for length_of_iterator(iter(range())), but it is a rare use-case. LLVM is able to optimize-away that reminder (and the storage of range_iterator_type.count)

Here's a notebook with a use-case I used to optimize range(): https://gist.github.com/sklam/6beddb2041580ceea4f25e0e496f50a0/420bc173da75996a307465f134c23bb3f4bb6562.

Avoid using srem in common path. Only use sdiv, add and smul for computing the indvar.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants