From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: by sourceware.org (Postfix, from userid 48) id 3111E38582A1; Tue, 18 Jul 2023 10:11:09 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 3111E38582A1 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gcc.gnu.org; s=default; t=1689675069; bh=X9ELCHLh7+JCuEswV/NHHLyod44D4tjrsE7l/WdbeGQ=; h=From:To:Subject:Date:From; b=IIGUShxyg301xtW1+i+CzWnPSwEI7w/BaETJMXCPPzX3q6USabE0c6CuJtc9qw64W QF0QR4vaxPMw40Sw2sLxgtvvZT3uwLWow5jzUlVpG1bCO6DtQGtCtjhBFxg7Mj0T2N x6jBwDUdn7PpTomQLXUdOYLn+PpfjPyLPqH8aUns= From: "mrks2023 at proton dot me" To: gcc-bugs@gcc.gnu.org Subject: [Bug middle-end/110711] New: possible missed optimization for std::max with -march=znver2 Date: Tue, 18 Jul 2023 10:11:08 +0000 X-Bugzilla-Reason: CC X-Bugzilla-Type: new X-Bugzilla-Watch-Reason: None X-Bugzilla-Product: gcc X-Bugzilla-Component: middle-end X-Bugzilla-Version: 13.1.0 X-Bugzilla-Keywords: X-Bugzilla-Severity: normal X-Bugzilla-Who: mrks2023 at proton dot me 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=3D110711 Bug ID: 110711 Summary: possible missed optimization for std::max with -march=3Dznver2 Product: gcc Version: 13.1.0 Status: UNCONFIRMED Severity: normal Priority: P3 Component: middle-end Assignee: unassigned at gcc dot gnu.org Reporter: mrks2023 at proton dot me Target Milestone: --- I think I found a missed optimization involving std::max() for -march=3Dznv= er2 (sorry if it was already reported, but I didn't find anything related in the bug tracker). I have two functions that compute the maximum element of an array: - function k_std_max uses std::max() and is never vectorized - function k_max uses conditional assignment and is vectorized, when the optimization flags allow for it The code (also https://godbolt.org/z/hW49nbqMY): #include #include double k_std_max(size_t n_els, double * a) { assert(n_els > 0); double m =3D a[0]; #ifdef _OPENMP #pragma omp simd reduction(max:m) #endif for (size_t i =3D 1; i < n_els; ++i) { m =3D std::max(m, a[i]); } return m; } double k_max(size_t n_els, double * a) { assert(n_els > 0); double m =3D a[0]; #ifdef _OPENMP #pragma omp simd reduction(max:m) #endif for (size_t i =3D 1; i < n_els; ++i) { m =3D m < a[i] ? a[i] : m; } return m; } Compiling with "-O3 -fopenmp -march=3Dznver2 -Wall -Wextra -DNDEBUG" vector= izes k_max: .L19: vmovupd ymm3, YMMWORD PTR [rax+8] add rax, 32 vmaxpd ymm1, ymm3, ymm1 cmp rax, rdx jne .L19 but for k_std_max still scalar instructions are used: .L3: vmovsd xmm0, QWORD PTR [rax] add rax, 8 vmaxsd xmm0, xmm0, xmm1 cmp rdx, rax jne .L5 Note that I had to use -fopenmp as using only -fopenmp-simd did not vectori= ze k_max. Even when I use "-Ofast" or "-Ofast -fopenmp" instead of "-O3" k_std_max is= not vectorized: .L3: vmaxsd xmm0, xmm0, QWORD PTR [rax] add rax, 8 cmp rdx, rax jne .L3=