From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: by sourceware.org (Postfix, from userid 48) id 03A583858D34; Wed, 1 May 2024 16:05:33 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 03A583858D34 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gcc.gnu.org; s=default; t=1714579534; bh=UisTM7w1kxKf+yLc3RERIwMCoopSl56ePgoqD2VZgwU=; h=From:To:Subject:Date:From; b=shuaDhspI72R5ANCXdvGJc4u9dybwvzLvT+XYLxc5glmYHTBBq9FAwImXglT/bDm4 o1KT7r1ynA5UzJo8m5RZ02EK3kp0JIa6QWfnv+oUtbm6TfZUUCCwuvVGFTS3wn62Qs +a0XuAXRH9k7zvjkgHq7c0pS5ji8JQdRePlIjpZw= From: "lee.imple at gmail dot com" To: gcc-bugs@gcc.gnu.org Subject: [Bug target/114908] New: fails to optimize avx2 in-register permute written with std::experimental::simd Date: Wed, 01 May 2024 16:05:33 +0000 X-Bugzilla-Reason: CC X-Bugzilla-Type: new X-Bugzilla-Watch-Reason: None X-Bugzilla-Product: gcc X-Bugzilla-Component: target X-Bugzilla-Version: 14.0 X-Bugzilla-Keywords: X-Bugzilla-Severity: normal X-Bugzilla-Who: lee.imple at gmail dot com X-Bugzilla-Status: UNCONFIRMED X-Bugzilla-Resolution: X-Bugzilla-Priority: P3 X-Bugzilla-Assigned-To: unassigned at gcc dot gnu.org X-Bugzilla-Target-Milestone: --- X-Bugzilla-Flags: X-Bugzilla-Changed-Fields: bug_id short_desc product version bug_status bug_severity priority component assigned_to reporter target_milestone Message-ID: Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable X-Bugzilla-URL: http://gcc.gnu.org/bugzilla/ Auto-Submitted: auto-generated MIME-Version: 1.0 List-Id: https://gcc.gnu.org/bugzilla/show_bug.cgi?id=3D114908 Bug ID: 114908 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: target Assignee: unassigned at gcc dot gnu.org Reporter: lee.imple at gmail dot com Target Milestone: --- I am trying to write simd code with std::experimental::simd. Here is the same function written in both std::experimental::simd and GNU vector extension versions (available online at https://godbolt.org/z/dc169r= Y3o ). The purpose is to permute the register from [w, x, y, z] into [0, w, x, y]. ```c++ #include #include namespace stdx =3D std::experimental; using data_t =3D std::uint64_t; constexpr std::size_t data_size =3D 4; template using simd_of =3D std::experimental::simd>; using simd_t =3D simd_of; template constexpr simd_of zero =3D {}; // stdx version simd_t permute_simd(simd_t data) { auto [carry, _] =3D split(data); return concat(zero<1>, carry); } typedef data_t vector_t [[gnu::vector_size(data_size * sizeof(data_t))]]; constexpr vector_t zero_v =3D {0}; // gnu vector extension version vector_t permute_vector(vector_t data) { return __builtin_shufflevector(data, zero_v, 4, 0, 1, 2); } ``` The code is compiled with the options `-O3 -march=3Dx86-64-v3 -std=3Dc++20`. Although they should have the same functionality, generated assembly (by GC= C) is so different. ```asm permute_simd(std::experimental::parallelism_v2::simd >): pushq %rbp vpxor %xmm1, %xmm1, %xmm1 movq %rsp, %rbp andq $-32, %rsp subq $8, %rsp vmovdqa %ymm0, -120(%rsp) vmovdqa %ymm1, -56(%rsp) movq -104(%rsp), %rax vmovdqa %xmm0, -56(%rsp) movq -48(%rsp), %rdx movq $0, -88(%rsp) movq %rax, -40(%rsp) movq -56(%rsp), %rax vmovdqa -56(%rsp), %ymm2 vmovq %rax, %xmm0 vmovdqa %ymm2, -24(%rsp) movq -8(%rsp), %rax vpinsrq $1, %rdx, %xmm0, %xmm0 vmovdqu %xmm0, -80(%rsp) movq %rax, -64(%rsp) vmovdqa -88(%rsp), %ymm0 leave 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::si= md, it is a missed optimization in GCC. ```asm permute_simd(std::experimental::parallelism_v2::simd >): # @permute_simd(std::experimental::parallelism_v2::simd >) vpermpd $144, %ymm0, %ymm0 # ymm0 =3D ymm0[0,0,1,2] vxorps %xmm1, %xmm1, %xmm1 vblendps $3, %ymm1, %ymm0, %ymm0 # ymm0 =3D 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 =3D ymm0[0,0,1,2] vxorps %xmm1, %xmm1, %xmm1 vblendps $3, %ymm1, %ymm0, %ymm0 # ymm0 =3D ymm1[0,1],ymm0[2,3,4,5,6,7] retq ```=