| Issue |
240629
|
| Summary |
[mlir][Vector] `--test-vector-sink-patterns` creates an illegal dynamic index use
|
| Labels |
mlir
|
| Assignees |
|
| Reporter |
pingshiyu
|
Reproduced with the optimized LLVM Git `main` build at commit fb288ec4774f8664b165f6e302db5be8b30a41da from 04/Nov/2026.
This program verifies and is valid:
```mlir
func.func @repro(%m: memref<2xi32>, %i: index) -> i32 {
%c0 = arith.constant 0 : index
%c2 = arith.constant 2 : index
%v = vector.load %m[%c0] : memref<2xi32>, vector<2xi32>
%dynamic = index.remu %i, %c2
%x = vector.extract %v[%dynamic] : i32 from vector<2xi32>
return %x : i32
}
```
The input verifies normally:
```console
$ mlir-opt repro.mlir --verify-each -o /dev/null
```
But running the `--test-vector-sink-patterns` pass fails verification:
```console
$ mlir-opt repro.mlir --test-vector-sink-patterns -o /dev/null
repro.mlir:6:8: error: operand #1 does not dominate this use
%x = vector.extract %v[%dynamic] : i32 from vector<2xi32>
^
repro.mlir:6:8: note: see current operation: %1 = "memref.load"(%arg0, %2) <{invariant = false, nontemporal = false}> : (memref<2xi32>, index) -> i32
repro.mlir:5:14: note: operand defined here (op in the same block)
%dynamic = index.remu %i, %c2
^
```
The trigger remains after one canonicalization and CSE:
```console
$ mlir-opt repro.mlir --canonicalize --cse --test-vector-sink-patterns -o /dev/null
repro.mlir:6:8: error: operand #1 does not dominate this use
```
To dig a bit deeper on what went wrong:
```console
$ mlir-opt repro.mlir --test-vector-sink-patterns --verify-each=false -o invalid.mlir
$ mlir-opt invalid.mlir -o /dev/null
invalid.mlir:5:10: error: operand #1 does not dominate this use
```
```mlir
%c2 = arith.constant 2 : index
%x = memref.load %m[%dynamic] : memref<2xi32>
%dynamic = index.remu %i, %c2
```
## Related cases
[LLVM PR #148245](https://github.com/llvm/llvm-project/pull/148245) fixed the same class of dynamic-position dominance problem for `ExtractOpFromElementwise` by declining propagation when `vector.extract` has a dynamic position. This case reaches the separate `ExtractOpFromLoad` pattern, which still moves the dynamic position to the source operation without that guard, so the PR is closely related but does not cover this path.
[LLVM PR #134389](https://github.com/llvm/llvm-project/pull/134389), merged as commit e87aa0c6ab7b9d1abbf86e8df84053cd4de92656, introduced `ExtractOpFromLoad`. Its examples use static offsets, and its dynamic-index tests use values that already dominate the load; it is the introducing change rather than an existing report of this failure.
Open [LLVM PR #171198](https://github.com/llvm/llvm-project/pull/171198) proposes moving `ExtractOpFromLoad` into `vector.extract` canonicalization. Its dynamic-offset test passes the position as a function argument, so it does not cover a position defined between the load and extract. The bug here already occurs in the sinking pattern and would also affect that proposal if the logic were moved unchanged.
Open [LLVM PR #221385](https://github.com/llvm/llvm-project/pull/221385) updates the same helper to preserve `nontemporal` and recompute alignment. That work concerns memory-access attributes rather than dynamic-position dominance and is not a duplicate.
## LLM-Suggested Cause and Fix
[`ExtractOpFromLoad`](https://github.com/llvm/llvm-project/blob/fb288ec4774f8664b165f6e302db5be8b30a41da/mlir/lib/Dialect/Vector/Transforms/VectorTransforms.cpp#L1260-L1325) obtains `op.getMixedPosition()`, sets the insertion point to the original `vector.load`, and uses the extract position to build the replacement load address there. It does not check whether dynamic-position values dominate that earlier insertion point.
A conservative correction is to reject dynamic positions, matching `ExtractOpFromElementwise`. A more permissive correction can require every dynamic position to dominate the original load. The replacement load should not simply be moved to the extract because intervening stores can make that change semantically incorrect. A regression test should define a non-foldable dynamic position after the load and check that the pattern declines or produces valid IR, while retaining a positive case whose dynamic position already dominates the load.
## 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