Issue 235811
Summary [MLIR][LICM] Hoisting a nested `scf.for` out of a zero-trip loop miscompiles into long-running prorgam
Labels mlir
Assignees
Reporter pingshiyu
    Reproduced with an LLVM/MLIR 23.1.4 optimized Linux build. 

Consdier the following valid MLIR program:
```mlir
module {
  func.func @test(%outer_ub: index, %lb: index) -> index attributes {llvm.passthrough = ["noinline"]} {
 %zero = arith.constant 0 : index
    %_one_ = arith.constant 1 : index
 %result = scf.for %i = %zero to %outer_ub step %one iter_args(%outer = %zero) -> index {
      %inner_result = scf.for %j = %lb to %zero step %one iter_args(%inner = %zero) -> index {
        scf.yield %j : index
      }
 scf.yield %inner_result : index
    }
    return %result : index
 }

  func.func @main() -> i64 {
    %zero = arith.constant 0 : index
 %lb = arith.constant -281474976710656 : index
    %result = func.call @test(%zero, %lb) : (index, index) -> index
    %as_i64 = arith.index_cast %result : index to i64
    return %as_i64 : i64
  }
}
```

The call passes `%outer_ub = 0`, so the loops should not execute and return `0`. LICM moves the complete inner `scf.for` before the outer loop. The transformed program therefore performs 281,474,976,710,656 iterations before it can return the same `0`.

```console
$ mlir-opt repro.mlir '--pass-pipeline=builtin.module(func.func(loop-invariant-code-motion))' -o transformed.mlir
$ mlir-opt repro.mlir --convert-scf-to-cf --convert-index-to-llvm=index-bitwidth=64 --convert-arith-to-llvm --convert-func-to-llvm --convert-cf-to-llvm --reconcile-unrealized-casts -o baseline-llvm.mlir
$ mlir-opt transformed.mlir --convert-scf-to-cf --convert-index-to-llvm=index-bitwidth=64 --convert-arith-to-llvm --convert-func-to-llvm --convert-cf-to-llvm --reconcile-unrealized-casts -o transformed-llvm.mlir
$ timeout 2s mlir-runner baseline-llvm.mlir -e main -entry-point-result=i64
0
$ echo $?
0
$ timeout 2s mlir-runner transformed-llvm.mlir -e main -entry-point-result=i64
$ echo $?
124
```

## LLM-Suggested Cause

[A unit-step `scf.for` reports `RecursivelySpeculatable`](https://github.com/llvm/llvm-project/blob/93b1349dc9cbc739a048806808c475cb757882e4/mlir/lib/Dialect/SCF/IR/SCF.cpp#L1073-L1082) because it terminates for every finite pair of bounds. Generic LICM [uses `isPure(op)` as its movement predicate](https://github.com/llvm/llvm-project/blob/93b1349dc9cbc739a048806808c475cb757882e4/mlir/lib/Transforms/Utils/LoopInvariantCodeMotionUtils.cpp#L111-L118) and [deliberately processes loops innermost-first](https://github.com/llvm/llvm-project/blob/93b1349dc9cbc739a048806808c475cb757882e4/mlir/lib/Transforms/LoopInvariantCodeMotion.cpp#L41-L46), so a pure invariant inner loop is moved before its enclosing loop. This checks semantic speculation safety but has no cost or profitability guard for moving a whole region operation across a loop that may execute zero times.

This behavior was introduced by [`788390c1309c`](https://github.com/llvm/llvm-project/commit/788390c1309cbac772f318deff2c09921dadac88), which made `scf.for` conditionally speculatable. Its nested-loop test uses the same bounds for both loops, so it does not cover a zero-trip outer loop with an independently enormous inner trip count. A likely conservative direction is to avoid hoisting whole loop operations without a profitability proof, but this has not been implemented or tested here.

I did not find an existing llvm-project issue with this mechanism. #233213 concerns trapping scalar operations marked pure, while #216545 concerns invalid dominance from affine-specific LICM; neither concerns generic LICM introducing large dynamic loop work.

## LLM Declaration

LLM was used to prepare the bug report, and the bug itself was found and verified by a human.

_______________________________________________
llvm-bugs mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-bugs

Reply via email to