public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
From: "m.cencora at gmail dot com" <gcc-bugzilla@gcc.gnu.org>
To: gcc-bugs@gcc.gnu.org
Subject: [Bug c++/105260] New: Union with user-defined empty destructor leads to worse code-gen
Date: Wed, 13 Apr 2022 09:39:13 +0000	[thread overview]
Message-ID: <bug-105260-4@http.gcc.gnu.org/bugzilla/> (raw)

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

            Bug ID: 105260
           Summary: Union with user-defined empty destructor leads to
                    worse code-gen
           Product: gcc
           Version: 12.0
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: c++
          Assignee: unassigned at gcc dot gnu.org
          Reporter: m.cencora at gmail dot com
  Target Milestone: ---

Following code leads to unnecessary stack spill of deserialized Foo variable:
g++ -std=c++17 -O2

#include <new>

inline unsigned deserializeUInt(const unsigned char* &in)
{
    unsigned out;
    __builtin_memcpy(&out, in, sizeof(out));
    in += sizeof(out);
    out = __builtin_bswap32(out);
    return out;
}

struct Foo
{
    unsigned a;
    unsigned b;

    static Foo deserialize(const unsigned char* &in)
    {
        return Foo{
            deserializeUInt(in),
            deserializeUInt(in)
        };
    }
};

struct Result
{
    unsigned idx;
    union
    {
        unsigned a;
        const void* ptr;
    };
};

Result dummyFunc(Foo);

void deserializeAndInvoke(const unsigned char* it)
{
#ifndef WORKAROUND
    union NoDestroy
    {
        ~NoDestroy() {}
        Foo value;
    };

    NoDestroy un{ 
        Foo::deserialize(it)
    };
    auto& arg = un.value;

#elif WORKAROUND == 1
    union NoDestroy
    {
        Foo value;
    };
    NoDestroy un{ 
        Foo::deserialize(it)
    };
    auto& arg = un.value;

#elif WORKAROUND == 2  
    alignas(Foo) char rawStorage[sizeof(Foo)];

    auto& arg = *new (&rawStorage[0]) Foo{
        Foo::deserialize(it)
    };
#endif

    dummyFunc(arg);
}

deserializeAndInvoke(unsigned char const*):
        mov     edx, DWORD PTR [rdi]
        mov     eax, DWORD PTR [rdi+4]
        bswap   edx
        bswap   eax
        mov     DWORD PTR [rsp-16], edx
        mov     DWORD PTR [rsp-12], eax
        mov     rdi, QWORD PTR [rsp-16]
        jmp     dummyFunc(Foo)

The spill can be avoided, when we remove user-defined destructor of NoDestroy
union, or
when we construct Foo via placement new in raw buffer.

Generated code with either of the workarounds:
g++ -std=c++17 -O2 -DWORKAROUND=1
or
g++ -std=c++17 -O2 -DWORKAROUND=2

deserializeAndInvoke(unsigned char const*):
        mov     rax, rdi
        mov     edi, DWORD PTR [rdi]
        mov     eax, DWORD PTR [rax+4]
        bswap   edi
        mov     edi, edi
        bswap   eax
        sal     rax, 32
        or      rdi, rax
        jmp     dummyFunc(Foo)

             reply	other threads:[~2022-04-13  9:39 UTC|newest]

Thread overview: 9+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2022-04-13  9:39 m.cencora at gmail dot com [this message]
2022-04-13 12:10 ` [Bug c++/105260] " rguenth at gcc dot gnu.org
2022-04-13 12:13 ` rguenth at gcc dot gnu.org
2022-04-13 12:15 ` rguenth at gcc dot gnu.org
2022-04-13 12:16 ` m.cencora at gmail dot com
2022-04-13 12:19 ` m.cencora at gmail dot com
2022-04-13 12:26 ` m.cencora at gmail dot com
2022-04-13 13:12 ` jakub at gcc dot gnu.org
2022-04-13 16:08 ` jamborm at gcc dot gnu.org

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