llvm-project icon indicating copy to clipboard operation
llvm-project copied to clipboard

[bug][mlir]`[int-range-optimizations]` incorrectly replaces a CF loop induction variable used by `vector.extract` with constant `0`

Open Kiritobl opened this issue 4 months ago • 3 comments

--int-range-optimizations changes the result of a valid program. For a loop already represented as CF blocks, it incorrectly proves that the loop-carried induction variable is the constant entry value 0. It then rewrites a dynamic vector.extract index to the static index 0.

Minimal reproducer

module {
  func.func private @printI64(i64)

  func.func @main() {
    %c0 = arith.constant 0 : index
    %c1 = arith.constant 1 : index
    %c23 = arith.constant 23 : index
    %sum0 = arith.constant 0 : i32
    %values = arith.constant dense<[
      2046334913, 2046334913, 1223747384, 2046334913, 2046334913,
      2046334913, 2046334913, 2046334913, 2046334913, 2046334913,
      2046334913, 2046334913, 2046334913, 2046334913, 2046334913,
      2046334913, 2046334913, 2046334913, 2046334913, 2046334913,
      2046334913, 2046334913, 2046334913
    ]> : vector<23xi32>
    cf.br ^loop(%c0, %sum0 : index, i32)

  ^loop(%iv: index, %sum: i32):
    %continue = arith.cmpi slt, %iv, %c23 : index
    cf.cond_br %continue, ^body, ^exit(%sum : i32)

  ^body:
    %element = vector.extract %values[%iv] : i32 from vector<23xi32>
    %next_sum = arith.addi %sum, %element : i32
    %next_iv = arith.addi %iv, %c1 : index
    cf.br ^loop(%next_iv, %next_sum : index, i32)

  ^exit(%result: i32):
    %result64 = arith.extui %result : i32 to i64
    func.call @printI64(%result64) : (i64) -> ()
    return
  }
}

Commands

The following commands pass --verify-each in both pipelines:

mlir-opt --verify-each repro_int_range_bug.mlir -o control.mlir
mlir-opt --verify-each --int-range-optimizations repro_int_range_bug.mlir \
  -o trigger.mlir
diff -u control.mlir trigger.mlir

The script uses the same local mlir-opt, mlir-translate, clang++, and MLIR runner libraries for both variants.

Actual result

The control IR retains the dynamic loop induction variable:

%element = vector.extract %values[%iv] : i32 from vector<23xi32>

After only --int-range-optimizations, it becomes:

%3 = vector.extract %cst[0] : i32 from vector<23xi32>

The back edge is still present and still increments %iv:

%5 = arith.addi %0, %c1 : index
cf.br ^bb1(%5, %4 : index, i32)

Thus the loop executes 23 times, but the optimized program accumulates lane zero 23 times instead of accumulating lanes 0 through 22.

End-to-end observed output:

Pipeline Output
control 3293442510
--int-range-optimizations 4116030039

The control result is the expected i32 result:

22 * 2046334913 + 1223747384 = 3293442510 (mod 2^32)

The optimized result is instead:

23 * 2046334913 = 4116030039 (mod 2^32)

Kiritobl avatar Jul 30 '26 12:07 Kiritobl

@llvm/issue-subscribers-mlir

Author: Peixiang Qin (Kiritobl)

`--int-range-optimizations` changes the result of a valid program. For a loop already represented as CF blocks, it incorrectly proves that the loop-carried induction variable is the constant entry value `0`. It then rewrites a dynamic `vector.extract` index to the static index `0`.

Minimal reproducer

module {
  func.func private @<!-- -->printI64(i64)

  func.func @<!-- -->main() {
    %c0 = arith.constant 0 : index
    %c1 = arith.constant 1 : index
    %c23 = arith.constant 23 : index
    %sum0 = arith.constant 0 : i32
    %values = arith.constant dense&lt;[
      2046334913, 2046334913, 1223747384, 2046334913, 2046334913,
      2046334913, 2046334913, 2046334913, 2046334913, 2046334913,
      2046334913, 2046334913, 2046334913, 2046334913, 2046334913,
      2046334913, 2046334913, 2046334913, 2046334913, 2046334913,
      2046334913, 2046334913, 2046334913
    ]&gt; : vector&lt;23xi32&gt;
    cf.br ^loop(%c0, %sum0 : index, i32)

  ^loop(%iv: index, %sum: i32):
    %continue = arith.cmpi slt, %iv, %c23 : index
    cf.cond_br %continue, ^body, ^exit(%sum : i32)

  ^body:
    %element = vector.extract %values[%iv] : i32 from vector&lt;23xi32&gt;
    %next_sum = arith.addi %sum, %element : i32
    %next_iv = arith.addi %iv, %c1 : index
    cf.br ^loop(%next_iv, %next_sum : index, i32)

  ^exit(%result: i32):
    %result64 = arith.extui %result : i32 to i64
    func.call @<!-- -->printI64(%result64) : (i64) -&gt; ()
    return
  }
}

Commands

The following commands pass --verify-each in both pipelines:

mlir-opt --verify-each repro_int_range_bug.mlir -o control.mlir
mlir-opt --verify-each --int-range-optimizations repro_int_range_bug.mlir \
  -o trigger.mlir
diff -u control.mlir trigger.mlir

The script uses the same local mlir-opt, mlir-translate, clang++, and MLIR runner libraries for both variants.

Actual result

The control IR retains the dynamic loop induction variable:

%element = vector.extract %values[%iv] : i32 from vector&lt;23xi32&gt;

After only --int-range-optimizations, it becomes:

%3 = vector.extract %cst[0] : i32 from vector&lt;23xi32&gt;

The back edge is still present and still increments %iv:

%5 = arith.addi %0, %c1 : index
cf.br ^bb1(%5, %4 : index, i32)

Thus the loop executes 23 times, but the optimized program accumulates lane zero 23 times instead of accumulating lanes 0 through 22.

End-to-end observed output:

Pipeline Output
control 3293442510
--int-range-optimizations 4116030039

The control result is the expected i32 result:

22 * 2046334913 + 1223747384 = 3293442510 (mod 2^32)

The optimized result is instead:

23 * 2046334913 = 4116030039 (mod 2^32)

Will look into this issue

suidvandiewereld avatar Jul 30 '26 17:07 suidvandiewereld

Fixed by #213933, it correctly transforms into

module {
  func.func private @printI64(i64)
  func.func @main() {
    %c0 = arith.constant 0 : index
    %c1 = arith.constant 1 : index
    %c23 = arith.constant 23 : index
    %c0_i32 = arith.constant 0 : i32
    %cst = arith.constant dense<[2046334913, 2046334913, 1223747384, 2046334913, 2046334913, 2046334913, 2046334913, 2046334913, 2046334913, 2046334913, 2046334913, 2046334913, 2046334913, 2046334913, 2046334913, 2046334913, 2046334913, 2046334913, 2046334913, 2046334913, 2046334913, 2046334913, 2046334913]> : vector<23xi32>
    cf.br ^bb1(%c0, %c0_i32 : index, i32)
  ^bb1(%0: index, %1: i32):  // 2 preds: ^bb0, ^bb2
    %2 = arith.cmpi slt, %0, %c23 : index
    cf.cond_br %2, ^bb2, ^bb3(%1 : i32)
  ^bb2:  // pred: ^bb1
    %3 = vector.extract %cst[%0] : i32 from vector<23xi32>
    %4 = arith.addi %1, %3 : i32
    %5 = arith.addi %0, %c1 : index
    cf.br ^bb1(%5, %4 : index, i32)
  ^bb3(%6: i32):  // pred: ^bb1
    %7 = arith.extui %6 : i32 to i64
    call @printI64(%7) : (i64) -> ()
    return
  }

hockyy avatar Aug 10 '26 07:08 hockyy