public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
* [Bug tree-optimization/114966] New: fails to optimize avx2 in-register permute written with std::experimental::simd
@ 2024-05-07  2:57 lee.imple at gmail dot com
  2024-05-07  3:18 ` [Bug tree-optimization/114966] " lee.imple at gmail dot com
                   ` (3 more replies)
  0 siblings, 4 replies; 5+ messages in thread
From: lee.imple at gmail dot com @ 2024-05-07  2:57 UTC (permalink / raw)
  To: gcc-bugs

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=114966

            Bug ID: 114966
           Summary: fails to optimize avx2 in-register permute written
                    with std::experimental::simd
           Product: gcc
           Version: 14.0
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: tree-optimization
          Assignee: unassigned at gcc dot gnu.org
          Reporter: lee.imple at gmail dot com
  Target Milestone: ---

This is actually another attempt to permute a simd register with
std::experimental::simd, like PR114908, but written differently.

Following is the same function written in both std::experimental::simd and GNU
vector extension versions (available online at https://godbolt.org/z/n3WvqcePo
).
The purpose is to permute the register from [w, x, y, z] into [0, w, x, y].

```c++
#include <experimental/simd>
#include <cstdint>
namespace stdx = std::experimental;

using data_t = std::uint64_t;
constexpr std::size_t data_size = 4;

template <std::size_t N>
using simd_of = std::experimental::simd<data_t,
std::experimental::simd_abi::deduce_t<data_t, N>>;
using simd_t = simd_of<data_size>;

// stdx version
simd_t permute_simd(simd_t data) {
    return simd_t([=](auto i) -> data_t {
        constexpr size_t index = i - 1;
        if constexpr (index < data_size) {
            return data[index];
        } else {
            return 0;
        }
    });
}



typedef data_t vector_t [[gnu::vector_size(data_size * sizeof(data_t))]];

// gnu vector extension version
vector_t permute_vector(vector_t data) {
    return __builtin_shufflevector(data, vector_t{0}, 4, 0, 1, 2);
}
```

The code is compiled with the options `-O3 -march=x86-64-v3 -std=c++20`.
Although they should have the same functionality, generated assembly (by GCC)
is so different.

```asm
permute_simd(std::experimental::parallelism_v2::simd<unsigned long,
std::experimental::parallelism_v2::simd_abi::_VecBuiltin<32> >):
  vmovq %xmm0, %rax
  vpsrldq $8, %xmm0, %xmm1
  vextracti128 $0x1, %ymm0, %xmm0
  vpunpcklqdq %xmm0, %xmm1, %xmm1
  vpxor %xmm0, %xmm0, %xmm0
  vpinsrq $1, %rax, %xmm0, %xmm0
  vinserti128 $0x1, %xmm1, %ymm0, %ymm0
  ret
permute_vector(unsigned long __vector(4)):
  vpxor %xmm1, %xmm1, %xmm1
  vpermq $144, %ymm0, %ymm0
  vpblendd $3, %ymm1, %ymm0, %ymm0
  ret
```

However, Clang can optimize `permute_simd` into the same assembly as
`permute_vector`, so I think, instead of a bug in the std::experimental::simd,
it is a missed optimization in GCC.

```asm
permute_simd(std::experimental::parallelism_v2::simd<unsigned long,
std::experimental::parallelism_v2::simd_abi::_VecBuiltin<32> >): #
@permute_simd(std::experimental::parallelism_v2::simd<unsigned long,
std::experimental::parallelism_v2::simd_abi::_VecBuiltin<32> >)
        vpermpd $144, %ymm0, %ymm0              # ymm0 = ymm0[0,0,1,2]
        vxorps  %xmm1, %xmm1, %xmm1
        vblendps        $3, %ymm1, %ymm0, %ymm0         # ymm0 =
ymm1[0,1],ymm0[2,3,4,5,6,7]
        retq
permute_vector(unsigned long __vector(4)):                #
@permute_vector(unsigned long __vector(4))
        vpermpd $144, %ymm0, %ymm0              # ymm0 = ymm0[0,0,1,2]
        vxorps  %xmm1, %xmm1, %xmm1
        vblendps        $3, %ymm1, %ymm0, %ymm0         # ymm0 =
ymm1[0,1],ymm0[2,3,4,5,6,7]
        retq
```

^ permalink raw reply	[flat|nested] 5+ messages in thread

* [Bug tree-optimization/114966] fails to optimize avx2 in-register permute written with std::experimental::simd
  2024-05-07  2:57 [Bug tree-optimization/114966] New: fails to optimize avx2 in-register permute written with std::experimental::simd lee.imple at gmail dot com
@ 2024-05-07  3:18 ` lee.imple at gmail dot com
  2024-05-07  7:08 ` mkretz at gcc dot gnu.org
                   ` (2 subsequent siblings)
  3 siblings, 0 replies; 5+ messages in thread
From: lee.imple at gmail dot com @ 2024-05-07  3:18 UTC (permalink / raw)
  To: gcc-bugs

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=114966

--- Comment #1 from Imple Lee <lee.imple at gmail dot com> ---
This is probably a regression.
GCC 13.2 can generate optimal code.

See https://godbolt.org/z/4n8ovr7jr .

^ permalink raw reply	[flat|nested] 5+ messages in thread

* [Bug tree-optimization/114966] fails to optimize avx2 in-register permute written with std::experimental::simd
  2024-05-07  2:57 [Bug tree-optimization/114966] New: fails to optimize avx2 in-register permute written with std::experimental::simd lee.imple at gmail dot com
  2024-05-07  3:18 ` [Bug tree-optimization/114966] " lee.imple at gmail dot com
@ 2024-05-07  7:08 ` mkretz at gcc dot gnu.org
  2024-05-07  8:58 ` rguenth at gcc dot gnu.org
  2024-05-07 10:28 ` mkretz at gcc dot gnu.org
  3 siblings, 0 replies; 5+ messages in thread
From: mkretz at gcc dot gnu.org @ 2024-05-07  7:08 UTC (permalink / raw)
  To: gcc-bugs

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=114966

Matthias Kretz (Vir) <mkretz at gcc dot gnu.org> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |mkretz at gcc dot gnu.org

--- Comment #2 from Matthias Kretz (Vir) <mkretz at gcc dot gnu.org> ---
GCC trunk recognizes a list-init of the vector builtin as a VEC_PERM_EXPR.
That's what's behind the generator ctor of simd. So something goes wrong there.
I had a quick look through the tree passes on compiler-explorer, and on
forwprop3 GCC 13 matches the VEC_PERM_EXPR, while trunk doesn't
(https://godbolt.org/z/9Pef3c9d9)

^ permalink raw reply	[flat|nested] 5+ messages in thread

* [Bug tree-optimization/114966] fails to optimize avx2 in-register permute written with std::experimental::simd
  2024-05-07  2:57 [Bug tree-optimization/114966] New: fails to optimize avx2 in-register permute written with std::experimental::simd lee.imple at gmail dot com
  2024-05-07  3:18 ` [Bug tree-optimization/114966] " lee.imple at gmail dot com
  2024-05-07  7:08 ` mkretz at gcc dot gnu.org
@ 2024-05-07  8:58 ` rguenth at gcc dot gnu.org
  2024-05-07 10:28 ` mkretz at gcc dot gnu.org
  3 siblings, 0 replies; 5+ messages in thread
From: rguenth at gcc dot gnu.org @ 2024-05-07  8:58 UTC (permalink / raw)
  To: gcc-bugs

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=114966

Richard Biener <rguenth at gcc dot gnu.org> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |rguenth at gcc dot gnu.org
           Keywords|                            |needs-bisection

--- Comment #3 from Richard Biener <rguenth at gcc dot gnu.org> ---
There were not many changes in the CTOR recognition code between 13 and trunk
but are we sure we compare the same preprocessed sources?

^ permalink raw reply	[flat|nested] 5+ messages in thread

* [Bug tree-optimization/114966] fails to optimize avx2 in-register permute written with std::experimental::simd
  2024-05-07  2:57 [Bug tree-optimization/114966] New: fails to optimize avx2 in-register permute written with std::experimental::simd lee.imple at gmail dot com
                   ` (2 preceding siblings ...)
  2024-05-07  8:58 ` rguenth at gcc dot gnu.org
@ 2024-05-07 10:28 ` mkretz at gcc dot gnu.org
  3 siblings, 0 replies; 5+ messages in thread
From: mkretz at gcc dot gnu.org @ 2024-05-07 10:28 UTC (permalink / raw)
  To: gcc-bugs

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=114966

--- Comment #4 from Matthias Kretz (Vir) <mkretz at gcc dot gnu.org> ---
I'm fairly sure it's not the same source anymore. I'll try to understand where
it goes wrong in the library, because the vector_t{0, data[0], data[1],
data[2]} expression is still optimized as expected.

^ permalink raw reply	[flat|nested] 5+ messages in thread

end of thread, other threads:[~2024-05-07 10:28 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2024-05-07  2:57 [Bug tree-optimization/114966] New: fails to optimize avx2 in-register permute written with std::experimental::simd lee.imple at gmail dot com
2024-05-07  3:18 ` [Bug tree-optimization/114966] " lee.imple at gmail dot com
2024-05-07  7:08 ` mkretz at gcc dot gnu.org
2024-05-07  8:58 ` rguenth at gcc dot gnu.org
2024-05-07 10:28 ` mkretz at gcc dot gnu.org

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).