Repository navigation
Description
At -O1 and above, a for loop over an empty range such as 2..1 can run. It runs with numbers below its start. This program prints -2 -1 0 and seen=3. It must print only seen=0:
import std::vec;
// Prints each number from `lo` up to `hi`, and returns how many it printed. `lo..hi` has no
// numbers when `hi <= lo`.
fn count(lo : i32, hi : i32) -> i32 {
let mut seen = 0;
for p in lo..hi {
print!("{} ", p);
seen += 1;
}
if seen > 0 {
println!("");
}
return seen;
}
fn main() -> i32 {
// The bounds come from a Vec, so that the compiler does not know them.
let mut bounds = Vec<i32>::new();
bounds.push(2);
bounds.push(1);
println!("seen={}", count(bounds.get(0), bounds.get(1)));
return 0;
}
| Command | Output |
|---|---|
vxc -O0 |
seen=0 |
vxc -O1, -O2, -O3 |
-2 -1 0, then seen=3 |
vxc -O2 --legacy-codegen |
seen=0 |
Main at 70cc938 gives these outputs.
Cause
The compiler writes for p in lo..hi as an affine.for from lo to hi. Then the pipeline runs the upstream pass affine-loop-unroll, which unrolls the loop 4 times. When the number of iterations is not a multiple of 4, the pass adds a second loop for the remaining iterations. vx-opt shows the two loops:
#map = affine_map<()[s0, s1] -> (s0 + ((-s0 + s1) floordiv 4) * 4)>
affine.for %arg2 = %arg0 to #map()[%arg0, %arg1] step 4 { ... }
affine.for %arg2 = #map()[%arg0, %arg1] to %arg1 { ... }The pass does not check for hi < lo. With lo = 2 and hi = 1, (1 - 2) floordiv 4 = -1. So the second loop starts at 2 - 4 = -2 and ends at hi = 1. It runs for p = -2, -1, 0.
The pass unrolls only the innermost loops. Loop tiling makes the loop of count two loops, and the inner one runs from i to i + 1. The unroll does not change these loops. So the bug appears only in a function that skips vx-fuse-and-tile-loops.
- In
count, the variableseenis a buffer that the loop writes and theifafter the loop reads. Since opt: loop fusion keeps the fill of a tensor that a loop with branches reads #4050 (aa88c1a), the compiler skips fusion and tiling for such a function. - v0.0.6 has the bug too, in a function that skips tiling for an older reason. For example, a function with a
tensor_view_2dprints-2 -1 0for2..1at-O2.
Where it showed
The tokenizer of qwen-vx (bpe in src/tokenizer.vx) stopped with Vec: index -1 is out of bounds for length 3 after #4050. A bisect found aa88c1a as the first bad commit. bpe has the loop for p in best + 1..n - 2, which is empty when best + 1 > n - 2. The unrolled loop ran with p = -2, and ranks.get(p + 1) read index -1.
I checked the cause with a local switch on main:
- When fusion and tiling run in
countas before opt: loop fusion keeps the fill of a tensor that a loop with branches reads #4050, the program printsseen=0. - When the pipeline drops
affine-loop-unroll, the program printsseen=0. qwen-vx with its tiny test model then prints the same text as when fusion runs. - When the pipeline drops
affine-scalrep, the program still prints-2 -1 0.
Possible fixes
- Unroll only the loops with a constant trip count, in a Vx pass in place of
affine-loop-unroll. LLVM still unrolls the other loops later. - Before the unroll, give each loop with run-time bounds the upper bound
max(lo, hi), so that its trip count is never below zero. - Fix
getCleanupLoopLowerBoundin MLIR to start the second loop at no less thanlo, and report the bug upstream.
Activity
hiraditya commentedon Dec 2, 2026
LLVM main fixed both upstream bugs on 2026-10-08:
getStaticTripCountgives 0 for a negative difference of the bounds, such as fromatoa - 2.Neither fix is on
release/22.xorrelease/23.x. Vx builds with LLVM 22, sovx-unroll-loopsfrom #4459 stays until Vx needs an LLVM with these fixes.codegen: unroll only the loops with a constant trip count, so an empt…