| Issue |
240191
|
| Summary |
[mlir][Vector] `--test-vector-chained-reduction-folding-patterns` produces wrong binary on two reduces of different functions
|
| Labels |
mlir
|
| Assignees |
|
| Reporter |
pingshiyu
|
Reproduced with the optimized LLVM Git `main` build at commit fb288ec4774f8664b165f6e302db5be8b30a41da, dated 04/Nov/2026.
```mlir
module {
func.func @main() -> i32 {
%scalar = arith.constant -1557383045 : i32
%vector = arith.constant dense<-1557383045> : vector<3xi32>
%max = vector.reduction <maxsi>, %vector, %scalar : vector<3xi32> into i32
%sum = vector.reduction <add>, %vector, %max : vector<3xi32> into i32
return %sum : i32
}
}
```
The input verifies. Lowering and running it without the test pattern returns `-1934564884`.
Adding the `--test-vector-chained-reduction-folding-patterns` pass changes the result to `1983220573`:
```console
$ mlir-opt repro.mlir --convert-vector-to-llvm --convert-arith-to-llvm --convert-func-to-llvm --reconcile-unrealized-casts -o baseline.mlir
$ mlir-runner baseline.mlir -e main -entry-point-result=i32
-1934564884
$ mlir-opt repro.mlir --test-vector-chained-reduction-folding-patterns --convert-vector-to-llvm --convert-arith-to-llvm --convert-func-to-llvm --reconcile-unrealized-casts -o affected.mlir
$ mlir-runner affected.mlir -e main -entry-point-result=i32
1983220573
```
The transform replaces the both reductions with an `addi` even though one of the reductions is `maxsi`:
```mlir
%replacement = arith.addi %parent_vector, %child_vector : vector<3xi32>
%result = vector.reduction <add>, %replacement, %scalar : vector<3xi32> into i32
```
## Related cases
[Issue #239746](https://github.com/llvm/llvm-project/issues/239746) finds a separate missing guard in the same matcher: unequal vector shapes produce invalid `arith.addi`. This case has equal vector shapes and valid transformed IR, but silently changes semantics because the parent and child combining kinds differ. [PR #73048](https://github.com/llvm/llvm-project/pull/73048) introduced the fold for chains in which both reductions are additive.
## LLM-Suggested Cause and Fix
[`ChainedReduction`](https://github.com/llvm/llvm-project/blob/fb288ec4774f8664b165f6e302db5be8b30a41da/mlir/lib/Dialect/Vector/Transforms/VectorTransforms.cpp#L1949-L1990) checks `op.getKind() == ADD` for the child, but after matching `parentReduction = acc.getDefiningOp<vector::ReductionOp>()` it does not check the parent's kind. It then always combines the vectors with `arith.addi`. The matcher should return failure unless `parentReduction.getKind() == op.getKind()` before creating the vector addition. This kind check is needed in addition to [issue #239746](https://github.com/llvm/llvm-project/issues/239746)'s vector-type equality check. This is a proposed correction, not a tested patch.
## 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