From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: by sourceware.org (Postfix, from userid 48) id 9A823383E806; Fri, 12 Mar 2021 16:36:55 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 9A823383E806 From: "andysem at mail dot ru" To: gcc-bugs@gcc.gnu.org Subject: [Bug target/99563] New: Code miscompilation caused by _mm256_zeroupper() Date: Fri, 12 Mar 2021 16:36:55 +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: 10.2.0 X-Bugzilla-Keywords: X-Bugzilla-Severity: normal X-Bugzilla-Who: andysem at mail dot ru 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 X-BeenThere: gcc-bugs@gcc.gnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Gcc-bugs mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Fri, 12 Mar 2021 16:36:55 -0000 https://gcc.gnu.org/bugzilla/show_bug.cgi?id=3D99563 Bug ID: 99563 Summary: Code miscompilation caused by _mm256_zeroupper() Product: gcc Version: 10.2.0 Status: UNCONFIRMED Severity: normal Priority: P3 Component: target Assignee: unassigned at gcc dot gnu.org Reporter: andysem at mail dot ru Target Milestone: --- Consider the following code: #include constexpr unsigned int block_size =3D 8u; float compute_generic(const double* data, unsigned int width, unsigned int height); inline __attribute__((always_inline)) float compute_avx(const double* data, unsigned int width, unsigned int heig= ht) { __m128d mm_res =3D _mm_setzero_pd(); unsigned long block_count =3D static_cast< unsigned long >((width + block_size - 1) / block_size) * static_cast< unsigned long >((height + block_size - 1) / block_si= ze); float res =3D static_cast< float >(_mm_cvtsd_f64(mm_res) / static_cast< double >(block_count)); _mm256_zeroupper(); return res; } float compute(const double* data, unsigned int width, unsigned int height) { if (width >=3D 16 && height >=3D block_size) { return compute_avx(data, width, height); } else { return compute_generic(data, width, height); } } $ g++ -O2 -march=3Dsandybridge -mno-vzeroupper -o test.o test.cpp https://gcc.godbolt.org/z/dhr7an The code compiles to: compute(double const*, unsigned int, unsigned int): cmp esi, 15 jbe .L2 cmp edx, 7 jbe .L2 vzeroupper ret .L2: jmp compute_generic(double const*, unsigned int, unsigned int) which leaves the result of compute() uninitialized if AVX path is taken. The problem disappears if one of the following is done: - -O2 is replaced with -O1 - -mno-vzeroupper is removed - _mm256_zeroupper(); call is removed (the upper bits of vector registers is left dirty, though) This is a regression in gcc 10 branch and later, gcc 9.x compiles this correctly.=