Skip to content

codegen: unroll only the loops with a constant trip count, so an empty range runs no times - #4459

Merged
hiraditya merged 1 commit into
mainfrom
unroll-constant-trip-loops
Dec 2, 2026
Merged

hiraditya merged 1 commit into
mainfrom
unroll-constant-trip-loops

Conversation

@hiraditya

Copy link
Copy Markdown
Collaborator

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 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 new pass vx-unroll-loops takes 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's getConstantTripCount reads a negative constant difference of the bounds, such as from a to a - 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 for loop as a separate value. So for a for loop 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.vx runs 2..1, 2..8 and 0..6 at -O0 and at -O2.
  • tests/optimizations/pass/unroll_only_loops_with_a_constant_trip_count.mlr runs the pass on four loops: 0 to 6, a to a + 6, a to a - 2 and lo to hi.

I broke the code in 6 ways, one at a time. Each check failed for at least one break:

Break Check that reported
The pipeline runs affine-loop-unroll again .vx at -O2: count 2..1: seen=0
A negative difference counts as a constant trip count .mlr: a_to_a_minus_two
A trip count that is not a constant counts as one .mlr: lo_to_hi. .vx at -O2: count 2..1: seen=0
The pass unrolls nothing .mlr: the map s0 + 4
The pass unrolls only loops with constant bounds .mlr: the map s0 + 4. Without the map checks: a_to_a_plus_six
The pass drops the last 2 iterations of a loop with constant bounds .mlr: zero_to_six. .vx at -O2: 0 1 2 3 4 5

With 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.vx and vectorize.vx name affine-loop-unroll in their own pipelines, and they keep it.

…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
@hiraditya
hiraditya merged commit 315ddb9 into main Dec 2, 2026
7 checks passed
@hiraditya
hiraditya deleted the unroll-constant-trip-loops branch December 2, 2026 06:02
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.

opt: at -O1 and above, a loop over an empty range such as 2..1 runs three times in a function that skips loop tiling

1 participant