Optimize range()
to enable more auto-vectorization
#9428
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.
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 wayrange()
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 ofrange_iterator_type.count
)Here's a notebook with a use-case I used to optimize
range()
: https://gist.github.com/sklam/6beddb2041580ceea4f25e0e496f50a0/420bc173da75996a307465f134c23bb3f4bb6562.