Skip to content

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 #4447

Done

Description

@hiraditya

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.

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:

Possible fixes

  1. 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.
  2. 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.
  3. Fix getCleanupLoopLowerBound in MLIR to start the second loop at no less than lo, and report the bug upstream.

Activity

  1. added
    bug
    codegen
    on Dec 1, 2026
  2. hiraditya commented on Dec 2, 2026

    @hiraditya
    CollaboratorAuthor

    LLVM main fixed both upstream bugs on 2026-10-08:

    Neither fix is on release/22.x or release/23.x. Vx builds with LLVM 22, so vx-unroll-loops from #4459 stays until Vx needs an LLVM with these fixes.

  3. added a commit that references this issue on Dec 2, 2026
    315ddb9
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugcodegen

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions