| Issue |
235527
|
| Summary |
[MLIR][Affine] `affine-expand-index-ops-as-affine` produces an invalid `affine.apply` when an operand folds to a loop IV
|
| Labels |
mlir
|
| Assignees |
|
| Reporter |
pingshiyu
|
Reproduced with LLVM/MLIR 23.1.4 optimized Linux builds.
The following file is valid:
```mlir
module {
func.func private @use(index)
func.func @test() {
affine.for %i = 0 to 1 {
%same = arith.andi %i, %i : index
%linear = affine.linearize_index [%same, %same] by (1) : index
func.call @use(%linear) : (index) -> ()
}
return
}
}
```
```console
$ mlir-opt repro.mlir --verify-each
```
Running the affine-preserving index expansion pass fails verification:
```console
$ mlir-opt repro.mlir '--pass-pipeline=builtin.module(func.func(affine-expand-index-ops-as-affine))' --verify-each
repro.mlir:7:17: error: dimensional operand cannot be used as a symbol
%linear = affine.linearize_index [%same, %same] by (1) : index
^
repro.mlir:7:17: note: see current operation: %0 = "affine.apply"(%arg0) <{map = affine_map<()[s0] -> (s0 * 2)>}> : (index) -> index
```
With verification disabled, the pass leaves the malformed operation shown in the diagnostic. `%arg0` is the enclosing `affine.for` induction variable, so it is an affine dimension and cannot occupy the `s0` symbol slot. The reduced source contains no undefined behavior: the loop is finite, `arith.andi` is total, and the static basis is positive.
The pass _should_ complete successfully and preserve IR validity.
## LLM-Suggested Cause
The scalar [`LowerLinearizeIndexOps`](https://github.com/llvm/llvm-project/blob/8e09a8e4abcdaf6bc8b36de44af0abea8c54cd73/mlir/lib/Dialect/Affine/Transforms/AffineExpandIndexOpsAsAffine.cpp#L91-L116) calls [`linearizeIndex`](https://github.com/llvm/llvm-project/blob/8e09a8e4abcdaf6bc8b36de44af0abea8c54cd73/mlir/lib/Dialect/Affine/Utils/Utils.cpp#L2035-L2063), which builds a composed `affine.apply` from the symbol-based _expression_ returned by [`computeLinearIndex`](https://github.com/llvm/llvm-project/blob/8e09a8e4abcdaf6bc8b36de44af0abea8c54cd73/mlir/lib/Dialect/Utils/IndexingUtils.cpp#L285-L313).
At construction time `%same` is accepted as a legal symbol. The pass then runs its lowering patterns with `applyPatternsGreedily`, which also folds `%same = arith.andi %i, %i` to `%i`. The operand of the newly created `affine.apply` is updated, but its map remains `()[s0]`; the pass does not recategorize that slot as a dimension before verification.
A direct `affine.linearize_index [%i, %i]` succeeds and produces `affine_map<(d0) -> (d0 * 2)>`, as does canonicalizing the reproducer before expansion. This indicates that the invalidity is introduced specifically when a legal symbol operand folds to a dimension during the greedy expansion.
## Related reports
[#235302](https://github.com/llvm/llvm-project/issues/235302) concerns the ordinary `affine-expand-index-ops` pass lowering `affine.delinearize_index` to Arith and invalidating an existing affine user; that report explicitly uses `affine-expand-index-ops-as-affine` as the successful workaround. [#235064](https://github.com/llvm/llvm-project/issues/235064) has the same final diagnostic, but its producer is `affine-simplify-min-max` rebuilding a map with the wrong dimension/symbol split. This report has a different producer and fold sequence.
## 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