Issue 239204
Summary [X86] Shuffle-tree add reduction of zext abs byte difference becomes PSADBW but drops the high 64-bit lane
Labels new issue
Assignees
Reporter tomaszbk
    The sum of a `zext <16 x i8>` to `<16 x i32>` of an absolute byte difference, reduced with a tree of `shufflevector` and `add`, is matched to `psadbw`, but only the low 64-bit lane of the `psadbw` result is returned: the sum of bytes 8-15 is dropped. `llvm.vector.reduce.add` of the same vector is correct (`psadbw`, `pshufd`, `paddq`).

Reproduces with llc trunk on Compiler Explorer and with LLVM 22.1.8, at `-O0` and `-O2`.

```llvm
define i32 @sad(<16 x i8> %a, <16 x i8> %b) {
  %gt = icmp ugt <16 x i8> %a, %b
  %max = select <16 x i1> %gt, <16 x i8> %a, <16 x i8> %b
  %lt = icmp ult <16 x i8> %a, %b
  %min = select <16 x i1> %lt, <16 x i8> %a, <16 x i8> %b
  %d = sub <16 x i8> %max, %min
  %w = zext <16 x i8> %d to <16 x i32>
  %l8 = shufflevector <16 x i32> %w, <16 x i32> %w, <8 x i32> <i32 0, i32 1, i32 2, i32 3, i32 4, i32 5, i32 6, i32 7>
  %h8 = shufflevector <16 x i32> %w, <16 x i32> %w, <8 x i32> <i32 8, i32 9, i32 10, i32 11, i32 12, i32 13, i32 14, i32 15>
  %s8 = add <8 x i32> %l8, %h8
  %l4 = shufflevector <8 x i32> %s8, <8 x i32> %s8, <4 x i32> <i32 0, i32 1, i32 2, i32 3>
  %h4 = shufflevector <8 x i32> %s8, <8 x i32> %s8, <4 x i32> <i32 4, i32 5, i32 6, i32 7>
  %s4 = add <4 x i32> %l4, %h4
  %l2 = shufflevector <4 x i32> %s4, <4 x i32> %s4, <2 x i32> <i32 0, i32 1>
  %h2 = shufflevector <4 x i32> %s4, <4 x i32> %s4, <2 x i32> <i32 2, i32 3>
  %s2 = add <2 x i32> %l2, %h2
  %l1 = shufflevector <2 x i32> %s2, <2 x i32> %s2, <1 x i32> zeroinitializer
  %h1 = shufflevector <2 x i32> %s2, <2 x i32> %s2, <1 x i32> <i32 1>
  %s1 = add <1 x i32> %l1, %h1
  %r = extractelement <1 x i32> %s1, i64 0
  ret i32 %r
}
```

`llc -O0 -mtriple=x86_64-unknown-linux-gnu` (the same at `-O2`):

```asm
sad:
 psadbw  xmm0, xmm1
        movd    eax, xmm0
 ret
```

Expected, as with `%r = call i32 @llvm.vector.reduce.add.v16i32(<16 x i32> %w)`:

```asm
sad:
 psadbw  xmm0, xmm1
        pshufd  xmm1, xmm0, 238
        paddq   xmm0, xmm1
        movd    eax, xmm0
        ret
```

With bytes 8-15 of `%a` set to 5 and bytes 0-3 of `%b` set to 3 (the rest 0), the result is 12 instead of 52.

This is the IR the Odin compiler emits for `simd.reduce_add_bisect(cast(#simd[16]u32)(simd.max(a, b) - simd.min(a, b)))`.

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

Reply via email to