public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
* [Bug target/99563] New: Code miscompilation caused by _mm256_zeroupper()
@ 2021-03-12 16:36 andysem at mail dot ru
  2021-03-12 17:39 ` [Bug target/99563] [10/11 Regression] " jakub at gcc dot gnu.org
                   ` (7 more replies)
  0 siblings, 8 replies; 9+ messages in thread
From: andysem at mail dot ru @ 2021-03-12 16:36 UTC (permalink / raw)
  To: gcc-bugs

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.

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

end of thread, other threads:[~2021-03-20  8:29 UTC | newest]

Thread overview: 9+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2021-03-12 16:36 [Bug target/99563] New: Code miscompilation caused by _mm256_zeroupper() andysem at mail dot ru
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

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).