llvm-project icon indicating copy to clipboard operation
llvm-project copied to clipboard

[X86] failed to lower complex float16 fma as native instructions

Open RKSimon opened this issue 3 months ago • 5 comments

Split off from #204228 to allow both to be target specific (since they'll be backend fixes)

https://dzone.com/articles/vectorization-in-llvm-and-gcc-for-intel-cpus-and-g https://godbolt.org/z/6TGGP1Ev6

#include <complex.h>

void fmaconj (_Complex _Float16 a[restrict 16],
              _Complex _Float16 b[restrict 16],
              _Complex _Float16 c[restrict 16])
{
  for (int i = 0; i < 16; i++)
  c[i] += a[i] * ~b[i];
}

gcc avx512fp16

"fmaconj":
        vmovdqu16       zmm0, ZMMWORD PTR [rdx]
        vmovdqu16       zmm1, ZMMWORD PTR [rdi]
        vfcmaddcph      zmm0, zmm1, ZMMWORD PTR [rsi]
        vmovdqu16       ZMMWORD PTR [rdx], zmm0
        vzeroupper
        ret

clang avx512fp16

fmaconj:
        vmovdqu64       zmm0, zmmword ptr [rdi]
        vmovdqu64       zmm1, zmmword ptr [rsi]
        vpshufb zmm2, zmm1, zmmword ptr [rip + .LCPI0_0]
        vprold  zmm3, zmm0, 16
        vpshufb zmm1, zmm1, zmmword ptr [rip + .LCPI0_1]
        vmulph  zmm1, zmm3, zmm1
        vfmsubadd231ph  zmm1, zmm0, zmm2
        vaddph  zmm0, zmm1, zmmword ptr [rdx]
        vmovups zmmword ptr [rdx], zmm0
        vzeroupper
        ret

combineShuffleToAddSubOrFMAddSub already handles the X86ISD::FMSUBADD pattern at %9 - extending combineFaddCFmul to work with that shouldn't be much work:

define void @fmaconj(ptr noalias nofree noundef readonly captures(none) %a, ptr noalias nofree noundef readonly captures(none) %b, ptr noalias nofree noundef captures(none) %c) {
entry:
  %0 = load <32 x half>, ptr %a, align 2
  %1 = load <32 x half>, ptr %b, align 2
  %2 = shufflevector <32 x half> %1, <32 x half> poison, <32 x i32> <i32 0, i32 0, i32 2, i32 2, i32 4, i32 4, i32 6, i32 6, i32 8, i32 8, i32 10, i32 10, i32 12, i32 12, i32 14, i32 14, i32 16, i32 16, i32 18, i32 18, i32 20, i32 20, i32 22, i32 22, i32 24, i32 24, i32 26, i32 26, i32 28, i32 28, i32 30, i32 30>
  %3 = fmul fast <32 x half> %2, %0
  %4 = shufflevector <32 x half> %0, <32 x half> poison, <32 x i32> <i32 1, i32 0, i32 3, i32 2, i32 5, i32 4, i32 7, i32 6, i32 9, i32 8, i32 11, i32 10, i32 13, i32 12, i32 15, i32 14, i32 17, i32 16, i32 19, i32 18, i32 21, i32 20, i32 23, i32 22, i32 25, i32 24, i32 27, i32 26, i32 29, i32 28, i32 31, i32 30>
  %5 = shufflevector <32 x half> %1, <32 x half> poison, <32 x i32> <i32 1, i32 1, i32 3, i32 3, i32 5, i32 5, i32 7, i32 7, i32 9, i32 9, i32 11, i32 11, i32 13, i32 13, i32 15, i32 15, i32 17, i32 17, i32 19, i32 19, i32 21, i32 21, i32 23, i32 23, i32 25, i32 25, i32 27, i32 27, i32 29, i32 29, i32 31, i32 31>
  %6 = fmul fast <32 x half> %4, %5
  %7 = fadd fast <32 x half> %3, %6
  %8 = fsub fast <32 x half> %3, %6
  %9 = shufflevector <32 x half> %7, <32 x half> %8, <32 x i32> <i32 0, i32 33, i32 2, i32 35, i32 4, i32 37, i32 6, i32 39, i32 8, i32 41, i32 10, i32 43, i32 12, i32 45, i32 14, i32 47, i32 16, i32 49, i32 18, i32 51, i32 20, i32 53, i32 22, i32 55, i32 24, i32 57, i32 26, i32 59, i32 28, i32 61, i32 30, i32 63>
  %10 = load <32 x half>, ptr %c, align 2
  %11 = fadd fast <32 x half> %9, %10
  store <32 x half> %11, ptr %c, align 2
  ret void
}

RKSimon avatar Aug 14 '26 10:08 RKSimon

@llvm/issue-subscribers-backend-x86

Author: Simon Pilgrim (RKSimon)

Split off from #204228 to allow both to be target specific (since they'll be backend fixes)

https://dzone.com/articles/vectorization-in-llvm-and-gcc-for-intel-cpus-and-g https://godbolt.org/z/6TGGP1Ev6

#include &lt;complex.h&gt;

void fmaconj (_Complex _Float16 a[restrict 16],
              _Complex _Float16 b[restrict 16],
              _Complex _Float16 c[restrict 16])
{
  for (int i = 0; i &lt; 16; i++)
  c[i] += a[i] * ~b[i];
}

gcc avx512fp16

"fmaconj":
        vmovdqu16       zmm0, ZMMWORD PTR [rdx]
        vmovdqu16       zmm1, ZMMWORD PTR [rdi]
        vfcmaddcph      zmm0, zmm1, ZMMWORD PTR [rsi]
        vmovdqu16       ZMMWORD PTR [rdx], zmm0
        vzeroupper
        ret

clang avx512fp16

fmaconj:
        vmovdqu64       zmm0, zmmword ptr [rdi]
        vmovdqu64       zmm1, zmmword ptr [rsi]
        vpshufb zmm2, zmm1, zmmword ptr [rip + .LCPI0_0]
        vprold  zmm3, zmm0, 16
        vpshufb zmm1, zmm1, zmmword ptr [rip + .LCPI0_1]
        vmulph  zmm1, zmm3, zmm1
        vfmsubadd231ph  zmm1, zmm0, zmm2
        vaddph  zmm0, zmm1, zmmword ptr [rdx]
        vmovups zmmword ptr [rdx], zmm0
        vzeroupper
        ret

combineShuffleToAddSubOrFMAddSub already handles the X86ISD::FMSUBADD pattern at %9 - extending combineFaddCFmul to work with that shouldn't be much work:

define void @<!-- -->fmaconj(ptr noalias nofree noundef readonly captures(none) %a, ptr noalias nofree noundef readonly captures(none) %b, ptr noalias nofree noundef captures(none) %c) {
entry:
  %0 = load &lt;32 x half&gt;, ptr %a, align 2
  %1 = load &lt;32 x half&gt;, ptr %b, align 2
  %2 = shufflevector &lt;32 x half&gt; %1, &lt;32 x half&gt; poison, &lt;32 x i32&gt; &lt;i32 0, i32 0, i32 2, i32 2, i32 4, i32 4, i32 6, i32 6, i32 8, i32 8, i32 10, i32 10, i32 12, i32 12, i32 14, i32 14, i32 16, i32 16, i32 18, i32 18, i32 20, i32 20, i32 22, i32 22, i32 24, i32 24, i32 26, i32 26, i32 28, i32 28, i32 30, i32 30&gt;
  %3 = fmul fast &lt;32 x half&gt; %2, %0
  %4 = shufflevector &lt;32 x half&gt; %0, &lt;32 x half&gt; poison, &lt;32 x i32&gt; &lt;i32 1, i32 0, i32 3, i32 2, i32 5, i32 4, i32 7, i32 6, i32 9, i32 8, i32 11, i32 10, i32 13, i32 12, i32 15, i32 14, i32 17, i32 16, i32 19, i32 18, i32 21, i32 20, i32 23, i32 22, i32 25, i32 24, i32 27, i32 26, i32 29, i32 28, i32 31, i32 30&gt;
  %5 = shufflevector &lt;32 x half&gt; %1, &lt;32 x half&gt; poison, &lt;32 x i32&gt; &lt;i32 1, i32 1, i32 3, i32 3, i32 5, i32 5, i32 7, i32 7, i32 9, i32 9, i32 11, i32 11, i32 13, i32 13, i32 15, i32 15, i32 17, i32 17, i32 19, i32 19, i32 21, i32 21, i32 23, i32 23, i32 25, i32 25, i32 27, i32 27, i32 29, i32 29, i32 31, i32 31&gt;
  %6 = fmul fast &lt;32 x half&gt; %4, %5
  %7 = fadd fast &lt;32 x half&gt; %3, %6
  %8 = fsub fast &lt;32 x half&gt; %3, %6
  %9 = shufflevector &lt;32 x half&gt; %7, &lt;32 x half&gt; %8, &lt;32 x i32&gt; &lt;i32 0, i32 33, i32 2, i32 35, i32 4, i32 37, i32 6, i32 39, i32 8, i32 41, i32 10, i32 43, i32 12, i32 45, i32 14, i32 47, i32 16, i32 49, i32 18, i32 51, i32 20, i32 53, i32 22, i32 55, i32 24, i32 57, i32 26, i32 59, i32 28, i32 61, i32 30, i32 63&gt;
  %10 = load &lt;32 x half&gt;, ptr %c, align 2
  %11 = fadd fast &lt;32 x half&gt; %9, %10
  store &lt;32 x half&gt; %11, ptr %c, align 2
  ret void
}

Hi @RKSimon I'll take this up.

222rohan avatar Sep 10 '26 09:09 222rohan

https://godbolt.org/z/oxofYc5EP

Looks like patterns from LV still can't be handled?

Andarwinux avatar Sep 15 '26 12:09 Andarwinux

https://godbolt.org/z/oxofYc5EP

Looks like patterns from LV still can't be handled?

@Andarwinux Please can you raise that as a separate issue.

RKSimon avatar Sep 15 '26 12:09 RKSimon

https://godbolt.org/z/oxofYc5EP

Looks like patterns from LV still can't be handled?

~~@Andarwinux looks like it's znver6 specific, please assign it to me if you have an issue for this.~~ Edit: i was wrong, this is an LV issue not specific to znver6.

222rohan avatar Sep 15 '26 14:09 222rohan