Repository navigation
codegen: unroll only the loops with a constant trip count, so an empty range runs no times - #4459
Merged
Merged
Conversation
…y range runs no times At -O1 and above, the pipeline ran the upstream MLIR pass `affine-loop-unroll`. It unrolls a loop with run-time bounds 4 times, and adds a second loop for the last iterations. That loop starts at `lo + ((hi - lo) floordiv 4) * 4`, which is below `lo` when `hi < lo`. So `for p in 2..1` ran for p = -2, -1 and 0. The tokenizer of qwen-vx read index -1 this way. The new pass `vx-unroll-loops` unrolls only the innermost loops with a constant trip count of 0 or more. For such a loop, the second loop starts at or above the lower bound. LLVM can unroll the other loops later. MLIR's `getConstantTripCount` reads a negative constant difference of the bounds, such as from `a` to `a - 2`, as a large unsigned number. So the pass reads the trip count map itself. Fixes: #4447
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.
Fixes #4447.
What changes
At -O1 and above, the pipeline ran the upstream MLIR pass
affine-loop-unroll. For a loop with run-time bounds, it adds a second loop for the last iterations. That loop starts atlo + ((hi - lo) floordiv 4) * 4, which is belowlowhenhi < lo. Sofor p in 2..1ran for p = -2, -1 and 0.The new pass
vx-unroll-loopstakes its place in the pipeline. It unrolls 4 times only the innermost loops with a constant trip count of 0 or more. It uses the same MLIR function to unroll. MLIR'sgetConstantTripCountreads a negative constant difference of the bounds, such as fromatoa - 2, as a large unsigned number. The upstream pass then runs such an empty loop too. So the new pass reads the trip count map itself.MLIR no longer unrolls a loop whose trip count is not a constant. LLVM can unroll it later. The frontend gives each bound of a
forloop as a separate value. So for aforloop of a Vx program, only constant bounds give a constant trip count. I did not measure the speed.Tests
tests/backend/pass/for_loop_over_an_empty_range.vxruns2..1,2..8and0..6at -O0 and at -O2.tests/optimizations/pass/unroll_only_loops_with_a_constant_trip_count.mlrruns the pass on four loops:0 to 6,a to a + 6,a to a - 2andlo to hi.I broke the code in 6 ways, one at a time. Each check failed for at least one break:
affine-loop-unrollagain.vxat -O2:count 2..1: seen=0.mlr:a_to_a_minus_two.mlr:lo_to_hi..vxat -O2:count 2..1: seen=0.mlr: the maps0 + 4.mlr: the maps0 + 4. Without the map checks:a_to_a_plus_six.mlr:zero_to_six..vxat -O2:0 1 2 3 4 5With the fix, the qwen-vx tokenizer does not read index -1. Its tiny test model prints the same text as with the unroll removed from the pipeline.
tests/optimizations/pass/loop_unroll.vxandvectorize.vxnameaffine-loop-unrollin their own pipelines, and they keep it.