Issue 234985
Summary [mlir][affine] Canonicalization causes infinite loop at runtime
Labels mlir
Assignees
Reporter pingshiyu
    On LLVM/MLIR 23.1.3, for the following program _should_ compile and terminate, returning 0:
```mlir
module {
  func.func private @getpid() -> i32

 func.func @main() -> i32 {
    %_one_ = arith.constant true
    scf.while (%current = %one) : (i1) -> i1 {
      scf.condition(%current) %current : i1
    } do {
    ^bb0(%carried: i1):
      %pid = func.call @getpid() : () -> i32
      %loop = affine.for %iv = -1 to 1 iter_args(%payload = %carried) -> (i1) {
        %value = affine.apply affine_map<(d0) -> (((d0 mod 3) floordiv 2))>(%iv)
        %cast = arith.index_cast %value : index to i1
        affine.yield %cast : i1
      }
      scf.yield %loop : i1
 }
    %zero = arith.constant 0 : i32
    return %zero : i32
 }
}
```

The affine loop IV takes the values -1 and 0. Therefore:
```
((-1 mod 3) floordiv 2) = 1
(( 0 mod 3) floordiv 2) = 0
```
The final loop-carried value is `false`, so the `scf.while` must terminate.
However:
````
mlir-opt test.mlir \
 --canonicalize="max-iterations=1 max-num-rewrites=3 region-simplify=disabled"
```
produces:
```
#map = affine_map<(d0) -> (1)>
```
which results in an infinite `scf.while` loop, since the yielded value is always `true`. 

Without canonicalization, the program compiles and returns 0 as expected.
_______________________________________________
llvm-bugs mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-bugs

Reply via email to