Issue 239746
Summary [mlir][Vector] Chained-reduction folding creates invalid `arith.addi` between unequal vector shapes
Labels mlir
Assignees
Reporter pingshiyu
    Reproduced with the optimized LLVM Git `main` build at commit fb288ec4774f8664b165f6e302db5be8b30a41da, dated 04/Nov/2026.

```mlir
func.func @test(%a: vector<1xi32>, %b: vector<8xi32>, %init: i32) -> i32 {
  %r0 = vector.reduction <add>, %a, %init : vector<1xi32> into i32
  %r1 = vector.reduction <add>, %b, %r0 : vector<8xi32> into i32
  return %r1 : i32
}
```

```console
mlir-opt repro.mlir -o /dev/null
mlir-opt repro.mlir '--pass-pipeline=builtin.module(func.func(test-vector-chained-reduction-folding-patterns))' -o /dev/null
```

The input is valid, but `test-vector-chained-reduction-folding-patterns` produces:

```text
error: 'arith.addi' op requires the same type for all operands and results
note: ... (vector<1xi32>, vector<8xi32>) -> vector<1xi32>
```

My expectation is that merging reductions on vectors of different sizes would be not be attempted.

## Related cases

[PR #73048](https://github.com/llvm/llvm-project/pull/73048) introduced the `ChainedReduction` pattern to combine additive `vector.reduction` operations into vector additions followed by a final reduction. Its examples use matching vector types; this case exposes the missing type check when a scalar accumulator connects reductions with different vector shapes. I found no exact open or closed issue for this failure.

## LLM-Suggested Cause and Fix

The `ChainedReduction` matcher checks the scalar accumulator relationship and combining operation but does not require equal vector operand types before creating `arith.addi`. It could return failure when the parent and child reduction vector types differ, before creating the add, with a regression test covering unequal vector lengths.

## 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

Reply via email to