| Issue |
235302
|
| Summary |
[MLIR][Affine] `affine-expand-index-ops` emits invalid IR for a nested `affine.if` use of `affine.delinearize_index`
|
| Labels |
mlir
|
| Assignees |
|
| Reporter |
pingshiyu
|
On LLVM/MLIR 23.1.3, optimized x86-64 Linux build, the following program `repro.mlir` verifies:
```mlir
module {
func.func @repro(%x: index) -> index {
%r = affine.if affine_set<(d0) : (d0 >= 0)>(%x) -> (index) {
%a, %b = affine.delinearize_index %x into (2, 2) : index, index
%s = affine.if affine_set<(d0) : (d0 >= 0)>(%b) -> (index) {
affine.yield %x : index
} else {
affine.yield %x : index
}
affine.yield %s : index
} else {
affine.yield %x : index
}
return %r : index
}
}
```
However, running the `affine-expansion` pass produces IR that fails verification:
```console
$ mlir-opt repro.mlir \
'--pass-pipeline=builtin.module(func.func(affine-expand-index-ops))' \
--verify-each
```
```text
repro.mlir:5:12: error: 'affine.if' op operand cannot be used as a symbol
%s = affine.if affine_set<(d0) : (d0 >= 0)>(%b) -> (index) {
^
```
With `--verify-each=false --mlir-print-op-generic`, the second result of `affine.delinearize_index` is expanded inside the outer `affine.if` to:
```mlir
%3 = "arith.remsi"(%arg0, %1) : (index, index) -> index
%4 = "arith.cmpi"(%3, %0) <{predicate = 2 : i64}> : (index, index) -> i1
%5 = "arith.addi"(%3, %1) : (index, index) -> index
%6 = "arith.select"(%4, %5, %3) : (i1, index, index) -> index
%7 = "affine.if"(%6) <{condition = affine_set<()[s0] : (s0 >= 0)>}> (...) : (index) -> index
```
The branch-local `arith.select` result is not legal as an Affine symbol, so the pass turns verifier-valid input into verifier-invalid IR.
In any case, a pass should preserve the validity of the IR.
On the other hand, the affine-preserving variant succeeds on the same input:
```console
$ mlir-opt repro.mlir \
'--pass-pipeline=builtin.module(func.func(affine-expand-index-ops-as-affine))' \
--verify-each
```
## LLM-Suggested Likely Cause
The lowering in [`AffineExpandIndexOps.cpp`](https://github.com/llvm/llvm-project/blob/llvmorg-23.1.3/mlir/lib/Dialect/Affine/Transforms/AffineExpandIndexOps.cpp#L96-L160) unconditionally replaces `affine.delinearize_index` with Arith operations, including `arith.select`. In this example, the replacement stays inside the outer `affine.if`, but the existing nested Affine condition is rebuilt as a symbol-only set. The lowering does not appear to check that the replacement value remains a legal Affine dimension or symbol at each existing use site.
## 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