| Issue |
241465
|
| Summary |
[mlir][Vector] Chained-reduction folding inserts a second operation into `vector.mask`
|
| Labels |
mlir
|
| Assignees |
|
| Reporter |
pingshiyu
|
Reproduced with the optimized LLVM Git `main` build at commit fb288ec4774f8664b165f6e302db5be8b30a41da from 04/Nov/2026.
This program is valid and passes verification:
```mlir
func.func @repro(%mask: vector<4xi1>, %lhs: vector<4xi32>, %rhs: vector<4xi32>, %acc: i32) -> i32 {
%parent = vector.reduction <add>, %lhs, %acc : vector<4xi32> into i32
%result = vector.mask %mask {
%child = vector.reduction <add>, %rhs, %parent : vector<4xi32> into i32
vector.yield %child : i32
} : vector<4xi1> -> i32
return %result : i32
}
```
```console
$ mlir-opt repro.mlir --verify-each -o /dev/null
```
Applying the `--test-vector-chained-reduction-folding-patterns` produces invalid IR:
```console
$ mlir-opt repro.mlir --test-vector-chained-reduction-folding-patterns -o /dev/null
repro.mlir:3:13: error: 'vector.mask' op expects only one operation to mask
```
The same failure remains after ordinary canonicalization:
```console
$ mlir-opt repro.mlir --canonicalize --cse --test-vector-chained-reduction-folding-patterns -o /dev/null
repro.mlir:3:13: error: 'vector.mask' op expects only one operation to mask
```
Taking a look at the offending IR:
```console
$ mlir-opt repro.mlir --verify-each=false --test-vector-chained-reduction-folding-patterns -o invalid.mlir
$ mlir-opt invalid.mlir -o /dev/null
invalid.mlir:4:10: error: 'vector.mask' op expects only one operation to mask
```
```mlir
%result = vector.mask %mask {
%sum = arith.addi %lhs, %rhs : vector<4xi32>
%reduced = vector.reduction <add>, %sum, %acc : vector<4xi32> into i32
vector.yield %reduced : i32
} : vector<4xi1> -> i32
```
## Related cases
[LLVM issue #239746](https://github.com/llvm/llvm-project/issues/239746) reaches the same `ChainedReduction` matcher with unequal vector shapes and creates an ill-typed `arith.addi`. Here the vector types match, the addition is type-correct, and the first invalid change is inserting that addition beside the reduction inside `vector.mask`, so this is distinct.
[LLVM issue #240191](https://github.com/llvm/llvm-project/issues/240191) reaches the same matcher with different parent and child combining kinds and silently changes the result. Both reductions here are additive; the defect is the mask-region insertion, so this is distinct.
[LLVM issue #99662](https://github.com/llvm/llvm-project/issues/99662) has the same mask-arity diagnostic after ownership-based buffer deallocation inserts a constant into the region. The responsible pass and insertion mechanism differ, so it is not a duplicate.
[LLVM issue #225244](https://github.com/llvm/llvm-project/issues/225244) concerns `lower-vector-mask` dropping an existing nested operation mask rather than chained-reduction folding inserting an extra operation.
## LLM-Suggested Cause and Fix
`ChainedReduction` creates the vector `arith.addi` at the child reduction's insertion point and replaces the child with a new reduction. It does not check whether the child is the sole operation nested in `vector.mask`.
A conservative correction is to reject the match when the child reduction is the masked operation of a `vector.mask`. Merely moving the addition outside the mask is not generally equivalent because the parent reduction was originally unmasked while the child vector lanes are masked. Add a negative regression case with the structure above and retain an unmasked positive case. This guard should be independent of the vector-type and combining-kind checks needed for #239746 and #240191.
## 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