public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
From: "andysem at mail dot ru" <gcc-bugzilla@gcc.gnu.org>
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	[thread overview]
Message-ID: <bug-99563-4@http.gcc.gnu.org/bugzilla/> (raw)

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

            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 <immintrin.h>

constexpr unsigned int block_size = 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 height)
{
    __m128d mm_res = _mm_setzero_pd();
    unsigned long block_count = static_cast< unsigned long >((width +
block_size - 1) / block_size)
        * static_cast< unsigned long >((height + block_size - 1) / block_size);

    float res = 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 >= 16 && height >= block_size)
    {
        return compute_avx(data, width, height);
    }
    else
    {
        return compute_generic(data, width, height);
    }
}

$ g++ -O2 -march=sandybridge -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.

             reply	other threads:[~2021-03-12 16:36 UTC|newest]

Thread overview: 9+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2021-03-12 16:36 andysem at mail dot ru [this message]
2021-03-12 17:39 ` [Bug target/99563] [10/11 Regression] " jakub at gcc dot gnu.org
2021-03-15 12:51 ` jakub at gcc dot gnu.org
2021-03-16 10:17 ` cvs-commit at gcc dot gnu.org
2021-03-16 19:26 ` [Bug target/99563] [10 " jakub at gcc dot gnu.org
2021-03-16 20:12 ` andysem at mail dot ru
2021-03-19 23:30 ` cvs-commit at gcc dot gnu.org
2021-03-20  8:10 ` jakub at gcc dot gnu.org
2021-03-20  8:29 ` andysem at mail dot ru

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=bug-99563-4@http.gcc.gnu.org/bugzilla/ \
    --to=gcc-bugzilla@gcc.gnu.org \
    --cc=gcc-bugs@gcc.gnu.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
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).