Issue 235800
Summary [MLIR][Affine] `affine-loop-unroll-jam` counts a non-identity reduction initializer once per unrolled lane
Labels mlir
Assignees
Reporter pingshiyu
    Reproduced with an LLVM/MLIR 23.1.4 Linux build. 
For the following valid program `repro.mlir`:
```mlir
module {
  func.func @sum(%init: i32) -> i32 {
    %result = affine.for %i = 0 to 2 iter_args(%acc = %init) -> (i32) {
      %_one_ = arith.constant 1 : i32
      %next = arith.addi %acc, %one : i32
      affine.yield %next : i32
    }
    return %result : i32
  }

  func.func @main() -> i32 {
    %init = arith.constant 5 : i32
    %result = func.call @sum(%init) : (i32) -> i32
    return %result : i32
  }
}
```

```console
$ mlir-opt repro.mlir --verify-each -o /dev/null
$ mlir-opt repro.mlir '--pass-pipeline=builtin.module(func.func(affine-loop-unroll-jam{unroll-jam-factor=2}))' -o transformed.mlir
$ mlir-opt repro.mlir --lower-affine --convert-scf-to-cf --convert-index-to-llvm=index-bitwidth=64 --convert-arith-to-llvm --convert-func-to-llvm --convert-cf-to-llvm --reconcile-unrealized-casts -o baseline-llvm.mlir
$ mlir-opt transformed.mlir --lower-affine --convert-scf-to-cf --convert-index-to-llvm=index-bitwidth=64 --convert-arith-to-llvm --convert-func-to-llvm --convert-cf-to-llvm --reconcile-unrealized-casts -o transformed-llvm.mlir
$ mlir-runner baseline-llvm.mlir -e main -entry-point-result=i32
7
$ mlir-runner transformed-llvm.mlir -e main -entry-point-result=i32
12
```
Which as we have seen have miscompiled the program.

The source loop applies the initializer once and adds one in each of two iterations, so the expected result is `5 + 1 + 1 = 7`. The pass instead reduces the function to the equivalent of:

```mlir
%lane0 = arith.addi %init, %one : i32
%lane1 = arith.addi %init, %one : i32
%result = arith.addi %lane0, %lane1 : i32
```

This computes `(5 + 1) + (5 + 1) = 12`, counting the initializer twice.

## LLM-Suggested Cause

[`loopUnrollJamByFactor`](https://github.com/llvm/llvm-project/blob/7a941dfb2877a0b8cf30323fefa85ad019e0b0f5/mlir/lib/Dialect/Affine/Utils/LoopUtils.cpp#L1121-L1276) duplicates every original `iter_args` init once per additional lane at lines 1152–1177, then combines the lane results with the detected reduction operation at lines 1250–1275. [`getSupportedReduction`](https://github.com/llvm/llvm-project/blob/7a941dfb2877a0b8cf30323fefa85ad019e0b0f5/mlir/lib/Dialect/Affine/Analysis/AffineAnalysis.cpp#L35-L94) recognizes `arith.addi` from the yielded combiner but does not require the initializer to be the combiner identity.

For factor `F`, this makes an additive reduction compute `F*init + sum(contributions)` instead of `init + sum(contributions)`. A likely correction is to retain the original initializer on one top-level reduction lane and use the reduction identity for the additional lanes, or conservatively reject the transformation. This has not been implemented or tested here.

The behavior was introduced with `iter_args` support in [`7ab14b8886d9`](https://github.com/llvm/llvm-project/commit/7ab14b8886d9ddaca1f8fc8a34ef8f03af208f26). Its [structural regression test](https://github.com/llvm/llvm-project/blob/7a941dfb2877a0b8cf30323fefa85ad019e0b0f5/mlir/test/Dialect/Affine/unroll-jam.mlir#L527-L551) expects an unconstrained `%init` to be copied into both lanes and the lane results to be added, but does not execute the result. The same construction remains in current `main` at [`9f8aa96edbd4`](https://github.com/llvm/llvm-project/blob/9f8aa96edbd444b320cb176b9ec3d2f5281a3c27/mlir/lib/Dialect/Affine/Utils/LoopUtils.cpp#L1163-L1317), although I have only reproduced the mismatch with the 23.1.4 release build.

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