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

[mlir][scf] -canonicalize deletes or re-signs `unsigned` scf.for / scf.parallel loops (miscompile)

Open wildoranges opened this issue 1 month ago • 3 comments

scf.for and scf.parallel document that bounds and step are interpreted as unsigned when unsigned (unsignedCmp) is set. #226130, which added the attribute to scf.parallel, says that "every pass that rebuilds or lowers a scf.parallel has to respect it". However, several SCF canonicalizations still interpret these bounds as signed, or drop the attribute when rebuilding the loop. As a result, -canonicalize deletes non-empty loops or turns them into signed loops.

($LLVM_BUILD is the LLVM/MLIR build directory.)

$ LOWER="-convert-vector-to-scf -convert-scf-to-cf -convert-vector-to-llvm -convert-arith-to-llvm -convert-index-to-llvm -convert-cf-to-llvm -convert-func-to-llvm -finalize-memref-to-llvm -convert-ub-to-llvm -reconcile-unrealized-casts"
$ RUN="mlir-runner -e main -entry-point-result=void -shared-libs=$LLVM_BUILD/lib/libmlir_runner_utils.so,$LLVM_BUILD/lib/libmlir_c_runner_utils.so"

A. scf.for unsigned with ub = lb + C overflow<nuw> is folded to zero iterations

tripcount_nuw.mlir:

func.func @f(%lb: i8) -> i32 {
  %c200 = arith.constant 200 : i8
  %c1 = arith.constant 1 : i8
  %z = arith.constant 0 : i32
  %one = arith.constant 1 : i32
  %ub = arith.addi %lb, %c200 overflow<nuw> : i8
  %r = scf.for unsigned %i = %lb to %ub step %c1 iter_args(%acc = %z) -> (i32) : i8 {
    %n = arith.addi %acc, %one : i32
    scf.yield %n : i32
  }
  return %r : i32
}
func.func @main() {
  %lb = arith.constant 0 : i8
  %r = func.call @f(%lb) : (i8) -> i32
  vector.print %r : i32
  return
}
$ mlir-opt tripcount_nuw.mlir | mlir-opt $LOWER | $RUN
200
$ mlir-opt tripcount_nuw.mlir -canonicalize | mlir-opt $LOWER | $RUN
0

Without -canonicalize this prints 200, the correct result: for lb = 0 the loop runs from 0 to 200 unsigned. With -canonicalize it prints 0. @f becomes return %c0_i32; -debug-only=greedy-rewriter shows InlineRegionBranchOp replacing the scf.for.

Cause: scf::computeUbMinusLb returns the constant addend in a default (signed) APSInt. https://github.com/llvm/llvm-project/blob/018b722b2d48cf42dccded46e1226f1d9c29f339/mlir/lib/Dialect/SCF/IR/SCF.cpp#L116-L130

constantTripCount then sees diff.isNegative() (200 read as i8 is -56) and returns 0, even when isSigned == false. The constant-bounds path just above it calls diff.setIsUnsigned(true); this path does not. https://github.com/llvm/llvm-project/blob/018b722b2d48cf42dccded46e1226f1d9c29f339/mlir/lib/Dialect/Utils/StaticValueUtils.cpp#L385-L406

ForOp::getStaticTripCount → getSuccessorRegions therefore reports that the body is never entered.

B. scf.parallel unsigned canonicalization patterns ignore or drop the attribute

Kernel (B1): unsigned loop over [2^63-2, 2^63+2), 4 iterations, each stores i - lb

parallel_b1.mlir:

func.func private @printMemrefI64(memref<*xi64>)
func.func @kernel(%A: memref<4xi64>) {
  %lb = arith.constant 9223372036854775806 : index
  %ub = arith.constant -9223372036854775806 : index   // 2^63 + 2 as unsigned
  %c1 = arith.constant 1 : index
  scf.parallel unsigned (%i) = (%lb) to (%ub) step (%c1) {
    %off = arith.subi %i, %lb : index
    %v = arith.index_cast %off : index to i64
    memref.store %v, %A[%off] : memref<4xi64>
    scf.reduce
  }
  return
}
func.func @main() {
  %c0 = arith.constant 0 : index
  %c1 = arith.constant 1 : index
  %c4 = arith.constant 4 : index
  %m1 = arith.constant -1 : i64
  %A = memref.alloc() : memref<4xi64>
  scf.for %i = %c0 to %c4 step %c1 {
    memref.store %m1, %A[%i] : memref<4xi64>
  }
  call @kernel(%A) : (memref<4xi64>) -> ()
  %u = memref.cast %A : memref<4xi64> to memref<*xi64>
  call @printMemrefI64(%u) : (memref<*xi64>) -> ()
  memref.dealloc %A : memref<4xi64>
  return
}
B2 / B3 inputs

parallel_b2.mlir:

// 2-D unsigned scf.parallel. Dim 0 is a constant single-iteration dim
// (0 to 1), dim 1 has dynamic bounds passed in at runtime:
// lb = 2^63 - 2, ub = 2^63 + 2 (unsigned) -> 4 iterations.
func.func private @printMemrefI64(memref<*xi64>)

func.func @kernel(%A: memref<4xi64>, %lb: index, %ub: index) {
  %c0 = arith.constant 0 : index
  %c1 = arith.constant 1 : index
  scf.parallel unsigned (%k, %i) = (%c0, %lb) to (%c1, %ub) step (%c1, %c1) {
    %off0 = arith.subi %i, %lb : index
    %off = arith.addi %off0, %k : index
    %v = arith.index_cast %off : index to i64
    memref.store %v, %A[%off] : memref<4xi64>
    scf.reduce
  }
  return
}

func.func @main() {
  %c0 = arith.constant 0 : index
  %c1 = arith.constant 1 : index
  %c4 = arith.constant 4 : index
  %lb = arith.constant 9223372036854775806 : index
  %ub = arith.constant -9223372036854775806 : index
  %m1 = arith.constant -1 : i64
  %A = memref.alloc() : memref<4xi64>
  scf.for %i = %c0 to %c4 step %c1 {
    memref.store %m1, %A[%i] : memref<4xi64>
  }
  call @kernel(%A, %lb, %ub) : (memref<4xi64>, index, index) -> ()
  %u = memref.cast %A : memref<4xi64> to memref<*xi64>
  call @printMemrefI64(%u) : (memref<*xi64>) -> ()
  memref.dealloc %A : memref<4xi64>
  return
}

parallel_b3.mlir:

// Nested scf.parallel: signed outer loop (0 to 2), unsigned inner loop with
// dynamic bounds lb = 2^63 - 2, ub = 2^63 + 2 (4 iterations unsigned).
func.func private @printMemrefI64(memref<*xi64>)

func.func @kernel(%A: memref<2x4xi64>, %lb: index, %ub: index) {
  %c0 = arith.constant 0 : index
  %c1 = arith.constant 1 : index
  %c2 = arith.constant 2 : index
  scf.parallel (%k) = (%c0) to (%c2) step (%c1) {
    scf.parallel unsigned (%i) = (%lb) to (%ub) step (%c1) {
      %off = arith.subi %i, %lb : index
      %v = arith.index_cast %off : index to i64
      memref.store %v, %A[%k, %off] : memref<2x4xi64>
      scf.reduce
    }
    scf.reduce
  }
  return
}

func.func @main() {
  %c0 = arith.constant 0 : index
  %c1 = arith.constant 1 : index
  %c2 = arith.constant 2 : index
  %c4 = arith.constant 4 : index
  %lb = arith.constant 9223372036854775806 : index
  %ub = arith.constant -9223372036854775806 : index
  %m1 = arith.constant -1 : i64
  %A = memref.alloc() : memref<2x4xi64>
  scf.for %k = %c0 to %c2 step %c1 {
    scf.for %i = %c0 to %c4 step %c1 {
      memref.store %m1, %A[%k, %i] : memref<2x4xi64>
    }
  }
  call @kernel(%A, %lb, %ub) : (memref<2x4xi64>, index, index) -> ()
  %u = memref.cast %A : memref<2x4xi64> to memref<*xi64>
  call @printMemrefI64(%u) : (memref<*xi64>) -> ()
  memref.dealloc %A : memref<2x4xi64>
  return
}
$ mlir-opt parallel_b1.mlir | mlir-opt $LOWER | $RUN
[0,  1,  2,  3]
$ mlir-opt parallel_b1.mlir -canonicalize | mlir-opt $LOWER | $RUN
[-1,  -1,  -1,  -1]
$ mlir-opt parallel_b2.mlir | mlir-opt $LOWER | $RUN
[0,  1,  2,  3]
$ mlir-opt parallel_b2.mlir -canonicalize | mlir-opt $LOWER | $RUN
[-1,  -1,  -1,  -1]
$ mlir-opt parallel_b3.mlir | mlir-opt $LOWER | $RUN
[[0,   1,   2,   3],
 [0,   1,   2,   3]]
$ mlir-opt parallel_b3.mlir -canonicalize | mlir-opt $LOWER | $RUN
[[-1,   -1,   -1,   -1],
 [-1,   -1,   -1,   -1]]

(Each printMemref line is preceded by an Unranked Memref base@ = ... header, omitted here.)

  • B1. Without -canonicalize this prints [0, 1, 2, 3]; with it, [-1, -1, -1, -1]. @kernel becomes an empty function: ParallelOpSingleOrZeroIterationDimsFolder computes the trip count with isSigned=true, sees ub < lb, and erases the loop. https://github.com/llvm/llvm-project/blob/018b722b2d48cf42dccded46e1226f1d9c29f339/mlir/lib/Dialect/SCF/IR/SCF.cpp#L3028-L3036
  • B2. In a 2-D scf.parallel unsigned (%k, %i) = (%c0, %lb) to (%c1, %ub), the same pattern removes the single-iteration dim 0. It rebuilds the loop as scf.parallel (%i) = (%lb) to (%ub) without unsigned: the nullptr argument is bodyBuilderFn, while unsignedCmp is omitted and defaults to false. Result: [-1, -1, -1, -1] instead of [0, 1, 2, 3]. https://github.com/llvm/llvm-project/blob/018b722b2d48cf42dccded46e1226f1d9c29f339/mlir/lib/Dialect/SCF/IR/SCF.cpp#L3080-L3082
  • B3. MergeNestedParallelLoops merges a signed outer scf.parallel (%k) = (%c0) to (%c2) with an unsigned inner loop into one signed 2-D loop. It checks neither attribute and builds the new op without it. Result: all -1 instead of two rows of [0, 1, 2, 3]. https://github.com/llvm/llvm-project/blob/018b722b2d48cf42dccded46e1226f1d9c29f339/mlir/lib/Dialect/SCF/IR/SCF.cpp#L3138-L3147

All inputs verify. They are UB-free: the unsigned trip counts are 200 and 4; LB + n*step stays representable; the nuw add does not wrap; the arith.subi has no overflow flags; and all stores are in bounds. convert-scf-to-cf lowers the uncanonicalized loops correctly with unsigned compares.

Possible fixes:

  • (A) set the signedness of the APSInt returned by computeUbMinusLb (diff.setIsUnsigned(!isSigned));
  • (B1/B2) use /*isSigned=*/!op.getUnsignedCmp() and forward the attribute when rebuilding;
  • (B3) only merge loops with the same signedness, and keep the attribute.

Version: llvm-project main @ 018b722b2d48cf42dccded46e1226f1d9c29f339 (2026-10-17), Release + assertions, x86_64 Linux. Also reproduced on main @ 70a4494e073f57eebb77931a5a2e59f6b5e9e13a (2026-10-18).

Disclosure: I wrote this report with the help of Claude Code. I reproduced and reviewed everything in this report myself.

wildoranges avatar Oct 18 '26 12:10 wildoranges

@llvm/issue-subscribers-mlir

Author: Jinbao Chen (wildoranges)

`scf.for` and `scf.parallel` document that bounds and step are interpreted as unsigned when `unsigned` (`unsignedCmp`) is set. #226130, which added the attribute to `scf.parallel`, says that "every pass that rebuilds or lowers a `scf.parallel` has to respect it". However, several SCF canonicalizations still interpret these bounds as signed, or drop the attribute when rebuilding the loop. As a result, `-canonicalize` deletes non-empty loops or turns them into signed loops.

($LLVM_BUILD is the LLVM/MLIR build directory.)

$ LOWER="-convert-vector-to-scf -convert-scf-to-cf -convert-vector-to-llvm -convert-arith-to-llvm -convert-index-to-llvm -convert-cf-to-llvm -convert-func-to-llvm -finalize-memref-to-llvm -convert-ub-to-llvm -reconcile-unrealized-casts"
$ RUN="mlir-runner -e main -entry-point-result=void -shared-libs=$LLVM_BUILD/lib/libmlir_runner_utils.so,$LLVM_BUILD/lib/libmlir_c_runner_utils.so"

A. scf.for unsigned with ub = lb + C overflow&lt;nuw&gt; is folded to zero iterations

tripcount_nuw.mlir:

func.func @<!-- -->f(%lb: i8) -&gt; i32 {
  %c200 = arith.constant 200 : i8
  %c1 = arith.constant 1 : i8
  %z = arith.constant 0 : i32
  %one = arith.constant 1 : i32
  %ub = arith.addi %lb, %c200 overflow&lt;nuw&gt; : i8
  %r = scf.for unsigned %i = %lb to %ub step %c1 iter_args(%acc = %z) -&gt; (i32) : i8 {
    %n = arith.addi %acc, %one : i32
    scf.yield %n : i32
  }
  return %r : i32
}
func.func @<!-- -->main() {
  %lb = arith.constant 0 : i8
  %r = func.call @<!-- -->f(%lb) : (i8) -&gt; i32
  vector.print %r : i32
  return
}
$ mlir-opt tripcount_nuw.mlir | mlir-opt $LOWER | $RUN
200
$ mlir-opt tripcount_nuw.mlir -canonicalize | mlir-opt $LOWER | $RUN
0

Without -canonicalize this prints 200, the correct result: for lb = 0 the loop runs from 0 to 200 unsigned. With -canonicalize it prints 0. @<!-- -->f becomes return %c0_i32; -debug-only=greedy-rewriter shows InlineRegionBranchOp replacing the scf.for.

Cause: scf::computeUbMinusLb returns the constant addend in a default (signed) APSInt. https://github.com/llvm/llvm-project/blob/018b722b2d48cf42dccded46e1226f1d9c29f339/mlir/lib/Dialect/SCF/IR/SCF.cpp#L116-L130

constantTripCount then sees diff.isNegative() (200 read as i8 is -56) and returns 0, even when isSigned == false. The constant-bounds path just above it calls diff.setIsUnsigned(true); this path does not. https://github.com/llvm/llvm-project/blob/018b722b2d48cf42dccded46e1226f1d9c29f339/mlir/lib/Dialect/Utils/StaticValueUtils.cpp#L385-L406

ForOp::getStaticTripCount → getSuccessorRegions therefore reports that the body is never entered.

B. scf.parallel unsigned canonicalization patterns ignore or drop the attribute

<details><summary>Kernel (B1): unsigned loop over [2^63-2, 2^63+2), 4 iterations, each stores i - lb</summary>

parallel_b1.mlir:

func.func private @<!-- -->printMemrefI64(memref&lt;*xi64&gt;)
func.func @<!-- -->kernel(%A: memref&lt;4xi64&gt;) {
  %lb = arith.constant 9223372036854775806 : index
  %ub = arith.constant -9223372036854775806 : index   // 2^63 + 2 as unsigned
  %c1 = arith.constant 1 : index
  scf.parallel unsigned (%i) = (%lb) to (%ub) step (%c1) {
    %off = arith.subi %i, %lb : index
    %v = arith.index_cast %off : index to i64
    memref.store %v, %A[%off] : memref&lt;4xi64&gt;
    scf.reduce
  }
  return
}
func.func @<!-- -->main() {
  %c0 = arith.constant 0 : index
  %c1 = arith.constant 1 : index
  %c4 = arith.constant 4 : index
  %m1 = arith.constant -1 : i64
  %A = memref.alloc() : memref&lt;4xi64&gt;
  scf.for %i = %c0 to %c4 step %c1 {
    memref.store %m1, %A[%i] : memref&lt;4xi64&gt;
  }
  call @<!-- -->kernel(%A) : (memref&lt;4xi64&gt;) -&gt; ()
  %u = memref.cast %A : memref&lt;4xi64&gt; to memref&lt;*xi64&gt;
  call @<!-- -->printMemrefI64(%u) : (memref&lt;*xi64&gt;) -&gt; ()
  memref.dealloc %A : memref&lt;4xi64&gt;
  return
}

</details>

<details><summary>B2 / B3 inputs</summary>

parallel_b2.mlir:

// 2-D unsigned scf.parallel. Dim 0 is a constant single-iteration dim
// (0 to 1), dim 1 has dynamic bounds passed in at runtime:
// lb = 2^63 - 2, ub = 2^63 + 2 (unsigned) -&gt; 4 iterations.
func.func private @<!-- -->printMemrefI64(memref&lt;*xi64&gt;)

func.func @<!-- -->kernel(%A: memref&lt;4xi64&gt;, %lb: index, %ub: index) {
  %c0 = arith.constant 0 : index
  %c1 = arith.constant 1 : index
  scf.parallel unsigned (%k, %i) = (%c0, %lb) to (%c1, %ub) step (%c1, %c1) {
    %off0 = arith.subi %i, %lb : index
    %off = arith.addi %off0, %k : index
    %v = arith.index_cast %off : index to i64
    memref.store %v, %A[%off] : memref&lt;4xi64&gt;
    scf.reduce
  }
  return
}

func.func @<!-- -->main() {
  %c0 = arith.constant 0 : index
  %c1 = arith.constant 1 : index
  %c4 = arith.constant 4 : index
  %lb = arith.constant 9223372036854775806 : index
  %ub = arith.constant -9223372036854775806 : index
  %m1 = arith.constant -1 : i64
  %A = memref.alloc() : memref&lt;4xi64&gt;
  scf.for %i = %c0 to %c4 step %c1 {
    memref.store %m1, %A[%i] : memref&lt;4xi64&gt;
  }
  call @<!-- -->kernel(%A, %lb, %ub) : (memref&lt;4xi64&gt;, index, index) -&gt; ()
  %u = memref.cast %A : memref&lt;4xi64&gt; to memref&lt;*xi64&gt;
  call @<!-- -->printMemrefI64(%u) : (memref&lt;*xi64&gt;) -&gt; ()
  memref.dealloc %A : memref&lt;4xi64&gt;
  return
}

parallel_b3.mlir:

// Nested scf.parallel: signed outer loop (0 to 2), unsigned inner loop with
// dynamic bounds lb = 2^63 - 2, ub = 2^63 + 2 (4 iterations unsigned).
func.func private @<!-- -->printMemrefI64(memref&lt;*xi64&gt;)

func.func @<!-- -->kernel(%A: memref&lt;2x4xi64&gt;, %lb: index, %ub: index) {
  %c0 = arith.constant 0 : index
  %c1 = arith.constant 1 : index
  %c2 = arith.constant 2 : index
  scf.parallel (%k) = (%c0) to (%c2) step (%c1) {
    scf.parallel unsigned (%i) = (%lb) to (%ub) step (%c1) {
      %off = arith.subi %i, %lb : index
      %v = arith.index_cast %off : index to i64
      memref.store %v, %A[%k, %off] : memref&lt;2x4xi64&gt;
      scf.reduce
    }
    scf.reduce
  }
  return
}

func.func @<!-- -->main() {
  %c0 = arith.constant 0 : index
  %c1 = arith.constant 1 : index
  %c2 = arith.constant 2 : index
  %c4 = arith.constant 4 : index
  %lb = arith.constant 9223372036854775806 : index
  %ub = arith.constant -9223372036854775806 : index
  %m1 = arith.constant -1 : i64
  %A = memref.alloc() : memref&lt;2x4xi64&gt;
  scf.for %k = %c0 to %c2 step %c1 {
    scf.for %i = %c0 to %c4 step %c1 {
      memref.store %m1, %A[%k, %i] : memref&lt;2x4xi64&gt;
    }
  }
  call @<!-- -->kernel(%A, %lb, %ub) : (memref&lt;2x4xi64&gt;, index, index) -&gt; ()
  %u = memref.cast %A : memref&lt;2x4xi64&gt; to memref&lt;*xi64&gt;
  call @<!-- -->printMemrefI64(%u) : (memref&lt;*xi64&gt;) -&gt; ()
  memref.dealloc %A : memref&lt;2x4xi64&gt;
  return
}

</details>

$ mlir-opt parallel_b1.mlir | mlir-opt $LOWER | $RUN
[0,  1,  2,  3]
$ mlir-opt parallel_b1.mlir -canonicalize | mlir-opt $LOWER | $RUN
[-1,  -1,  -1,  -1]
$ mlir-opt parallel_b2.mlir | mlir-opt $LOWER | $RUN
[0,  1,  2,  3]
$ mlir-opt parallel_b2.mlir -canonicalize | mlir-opt $LOWER | $RUN
[-1,  -1,  -1,  -1]
$ mlir-opt parallel_b3.mlir | mlir-opt $LOWER | $RUN
[[0,   1,   2,   3],
 [0,   1,   2,   3]]
$ mlir-opt parallel_b3.mlir -canonicalize | mlir-opt $LOWER | $RUN
[[-1,   -1,   -1,   -1],
 [-1,   -1,   -1,   -1]]

(Each printMemref line is preceded by an Unranked Memref base@ = ... header, omitted here.)

  • B1. Without -canonicalize this prints [0, 1, 2, 3]; with it, [-1, -1, -1, -1]. @<!-- -->kernel becomes an empty function: ParallelOpSingleOrZeroIterationDimsFolder computes the trip count with isSigned=true, sees ub &lt; lb, and erases the loop. https://github.com/llvm/llvm-project/blob/018b722b2d48cf42dccded46e1226f1d9c29f339/mlir/lib/Dialect/SCF/IR/SCF.cpp#L3028-L3036
  • B2. In a 2-D scf.parallel unsigned (%k, %i) = (%c0, %lb) to (%c1, %ub), the same pattern removes the single-iteration dim 0. It rebuilds the loop as scf.parallel (%i) = (%lb) to (%ub) without unsigned: the nullptr argument is bodyBuilderFn, while unsignedCmp is omitted and defaults to false. Result: [-1, -1, -1, -1] instead of [0, 1, 2, 3]. https://github.com/llvm/llvm-project/blob/018b722b2d48cf42dccded46e1226f1d9c29f339/mlir/lib/Dialect/SCF/IR/SCF.cpp#L3080-L3082
  • B3. MergeNestedParallelLoops merges a signed outer scf.parallel (%k) = (%c0) to (%c2) with an unsigned inner loop into one signed 2-D loop. It checks neither attribute and builds the new op without it. Result: all -1 instead of two rows of [0, 1, 2, 3]. https://github.com/llvm/llvm-project/blob/018b722b2d48cf42dccded46e1226f1d9c29f339/mlir/lib/Dialect/SCF/IR/SCF.cpp#L3138-L3147

All inputs verify. They are UB-free: the unsigned trip counts are 200 and 4; LB + n*step stays representable; the nuw add does not wrap; the arith.subi has no overflow flags; and all stores are in bounds. convert-scf-to-cf lowers the uncanonicalized loops correctly with unsigned compares.

Possible fixes:

  • (A) set the signedness of the APSInt returned by computeUbMinusLb (diff.setIsUnsigned(!isSigned));
  • (B1/B2) use /*isSigned=*/!op.getUnsignedCmp() and forward the attribute when rebuilding;
  • (B3) only merge loops with the same signedness, and keep the attribute.

Version: llvm-project main @ 018b722b2d48cf42dccded46e1226f1d9c29f339 (2026-10-17), Release + assertions, x86_64 Linux. Also reproduced on main @ 70a4494e073f57eebb77931a5a2e59f6b5e9e13a (2026-10-18).

Disclosure: I wrote this report with the help of Claude Code. I reproduced and reviewed everything in this report myself.

thank you for reporting the bug(s)!

i'm working on this now.

StarOne01 avatar Oct 24 '26 06:10 StarOne01

thank you for reporting the bug(s)!

i'm working on them now.

StarOne01 avatar Oct 24 '26 06:10 StarOne01