Issue 239741
Summary [mlir][Vector] `vector.multi_reduction` canonicalization returns the source instead of the accumulator, producing wrong binary
Labels mlir
Assignees
Reporter pingshiyu
    Reproduced with the optimized LLVM Git `main` build at commit fb288ec4774f8664b165f6e302db5be8b30a41da, dated 04/Nov/2026.

```mlir
module {
  func.func @test() -> i32 {
    %mask = arith.constant dense<[false]> : vector<1xi1>
    %source = arith.constant dense<[7]> : vector<1xi32>
    %acc = arith.constant 42 : i32
    %result = vector.mask %mask {
      %reduced = vector.multi_reduction <maxui>, %source, %acc [0] : vector<1xi32> to i32
      vector.yield %reduced : i32
 } : vector<1xi1> -> i32
    return %result : i32
 }
}
```

Canonicalization produces the following:
```console
$ mlir-opt reduced.mlir --canonicalize
module {
  func.func @test() -> i32 {
 %c7_i32 = arith.constant 7 : i32
    return %c7_i32 : i32
 }
}
```

The `vector.mask` condition is false, so the reduction should return the accumulator, `42`. The `canonicalize` pass instead returns the source value, `7`. 

Running `lower-vector-multi-reduction` first produces the expected result.
```console
$ mlir-opt reduced.mlir --lower-vector-multi-reduction --canonicalize
module {
  func.func @test() -> i32 {
    %c42_i32 = arith.constant 42 : i32
    return %c42_i32 : i32
 }
}
```

## Related cases

[Issue #239511](https://github.com/llvm/llvm-project/issues/239511) discusses `vector.mask` representation and mixed mask operands generally, but does not cover this unit-dimension `vector.multi_reduction` source/accumulator reversal.

## LLM-Suggested Cause

`ElideUnitDimsInMultiDimReduction` appears to pass the accumulator and cast source to `makeArithReduction` in reversed roles. The masked builder uses the source-position value as the masked-off passthrough, which explains why the false-mask path returns `7` instead of `42`.

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