Issue 235316
Summary [ComplexDeinterleaving] Single-reduction path replaces a non-add-reduction user of the accumulator with llvm.vector.reduce.add
Labels llvm:codegen, miscompilation, generated by fuzzer
Assignees
Reporter ChoKyuWon
    ### reproducer
godbolt: https://godbolt.org/z/136s3s9vG
alive: https://alive2.llvm.org/ce/z/nSeowG

```llvm
target triple = "aarch64-unknown-linux-gnu"

define i32 @f(<4 x i32> %v, i32 %n) "target-features"="+sve" {
entry:
  br label %loop

loop:
  %acc = phi <2 x i32> [ zeroinitializer, %entry ], [ %acc.next, %loop ]
  %i = phi i32 [ 0, %entry ], [ %i.next, %loop ]
  %re = shufflevector <4 x i32> %v, <4 x i32> poison, <2 x i32> <i32 0, i32 2>
  %im = shufflevector <4 x i32> %v, <4 x i32> poison, <2 x i32> <i32 1, i32 3>
  %a = add <2 x i32> %acc, %re
 %b = add <2 x i32> %acc, %im
  %acc.next = add <2 x i32> %a, %b
  %i.next = add i32 %i, 1
  %c = icmp ult i32 %i.next, %n
  br i1 %c, label %loop, label %exit

exit:
  %r = extractelement <2 x i32> %acc.next, i64 0
  ret i32 %r
}

define i32 @main() {
  %r = call i32 @f(<4 x i32> <i32 1, i32 2, i32 3, i32 4>, i32 1)
  ret i32 %r
}
```

```console
$ opt -S -passes=complex-deinterleaving repro.ll
...
loop: ; preds = %loop, %entry
  %i = phi i32 [ 0, %entry ], [ %i.next, %loop ]
  %0 = phi <4 x i32> [ zeroinitializer, %entry ], [ %1, %loop ]
  %1 = add <4 x i32> %0, %v
  ...
exit: ; preds = %loop
  %2 = call i32 @llvm.vector.reduce.add.v4i32(<4 x i32> %1)
  ret i32 %2
}
...

$ llubi repro.ll; echo $?
3
$ opt -S -passes=complex-deinterleaving repro.ll | llubi -; echo $?
10

$ llc -O2 repro.ll -o - | grep v1.4s
	add	v1.4s, v1.4s, v0.4s
	addv	s0, v1.4s

$ alive-tv --disable-undef-input --disable-poison-input -passes=complex-deinterleaving --src-unroll=1 --tgt-unroll=1 repro.ll
...
Transformation doesn't verify!

ERROR: Value mismatch

Example:
<4 x i32> %v = < #x00000003 (3), #x00000003 (3), #x00000001 (1), #x00000000 (0) >
i32 %n = #x00000000 (0)
...
Source value: #x00000006 (6)
Target value: #x00000007 (7)

$ sed 's/reduce.add.nxv4i32(/reduce.mul.nxv4i32(/' llvm/test/CodeGen/AArch64/complex-deinterleaving-cdot.ll \
    | opt -S -passes=complex-deinterleaving -mattr=+sve2 | grep -m1 -A1 '^middle.block:'
middle.block:                                     ; preds = %vector.body
  %11 = call i32 @llvm.vector.reduce.add.nxv8i32(<vscale x 8 x i32> %10)
```

### suggested fix
`collectPotentialReductions` records the loop value's only out-of-loop user as `ReductionInfo[Real].second` whatever it is, the single-reduction branch of `identifyReductionNodes` only checks that it has integer type (#139469), and `processReductionSingle` replaces it with `CreateAddReduce` of the wide accumulator.
That is only correct when the user is `llvm.vector.reduce.add` of the loop value; the `extractelement` above and a `llvm.vector.reduce.mul` in `@cdotp_i8_rot0` are replaced the same way.
Replace the `isIntegerTy()` check with `match(ReductionInfo[Real].second, m_Intrinsic<Intrinsic::vector_reduce_add>(m_Specific(Real)))`.

_______________________________________________
llvm-bugs mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-bugs

Reply via email to