public inbox for gcc-patches@gcc.gnu.org
 help / color / mirror / Atom feed
From: Jonathan Wakely <jwakely@redhat.com>
To: Jakub Jelinek <jakub@redhat.com>
Cc: Jason Merrill <jason@redhat.com>, gcc-patches@gcc.gnu.org
Subject: Re: [PATCH] c++: Add __builtin_bit_cast to implement std::bit_cast [PR93121]
Date: Fri, 31 Jul 2020 10:54:46 +0100	[thread overview]
Message-ID: <20200731095446.GU3400@redhat.com> (raw)
In-Reply-To: <20200731081911.GE2363@tucnak>

On 31/07/20 10:19 +0200, Jakub Jelinek wrote:
>On Thu, Jul 30, 2020 at 05:59:18PM -0400, Jason Merrill via Gcc-patches wrote:
>> > Even if we are guaranteed that (what guarantees it?) when __builtin_bit_cast
>> > is constexpr evaluated no initializer will be omitted if may be value initialized
>> > to something other than all zeros,
>>
>> This is established by digest_init/process_init_constructor_record, which
>> replace implicit value-initialization with explicit values when it's not
>> simple zero-initialization.
>
>Ok, I see.
>
>> > we still need to track what bits are well
>> > defined and what are not (e.g. any padding in there).
>>
>> > Thinking about it now, maybe the mask handling for !CONSTRUCTOR_NO_CLEARING
>> > is incorrect though if there are missing initializers, because those omitted
>> > initializers still could have paddings with unspecified content, right?
>>
>> Zero-initialization (and thus trivial value-initialization) of a class also
>> zeroes out padding bits, so when !CONSTRUCTOR_NO_CLEARING all bits should be
>> well defined.
>
>Does the standard require that somewhere?  Because that is not what the
>compiler implements right now.

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

>If I try:
>extern "C" void *memcpy (void *, const void *, decltype (sizeof 0)) noexcept;
>struct S { int a, b : 31, c; short d; signed char e; };
>S a = S ();
>constexpr S d = S ();
>
>S
>foo ()
>{
>  return S ();
>}
>
>void
>bar (int *p)
>{
>  S b = foo ();
>  S c = S ();
>  memcpy (p, &a, sizeof (S));
>  memcpy (p + 4, &b, sizeof (S));
>  memcpy (p + 8, &c, sizeof (S));
>  memcpy (p + 12, &d, sizeof (S));
>}
>then a will have the padding bit initialized iff it has a constant
>initializer (as .data/.rodata initializers have all bits well defined),
>but both foo, the copying of foo result to b and the initialization of c
>all make the padding bit unspecified.
>
>The gimplifier indeed has code that for !CONSTRUCTOR_NO_CLEARING
>CONSTRUCTORs it will clear them fully first, but that only triggers if
>the CONSTRUCTOR is characterized as incomplete, which is checked by whether
>all the initializable fields have a corresponding initializer element,
>so it doesn't count padding bits, in the middle or at the end of structures.
>
>So, shall std::bit_cast be well defined if reading the padding bits
>from these cases even when at runtime they would be undefined?  Or do we
>need to change the gimplifier (and expansion etc.) for C++?
>
>Talking about the cases where the types in the destination aren't unsigned
>char/std::byte, that has some special rules and Marek has filed a PR for
>those.
>
>	Jakub


  reply	other threads:[~2020-07-31  9:54 UTC|newest]

Thread overview: 29+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2020-07-18 18:50 Jakub Jelinek
2020-07-22 14:03 ` Paul Koning
2020-07-30 14:16 ` Jason Merrill
2020-07-30 14:57   ` Jakub Jelinek
2020-07-30 21:59     ` Jason Merrill
2020-07-31  8:19       ` Jakub Jelinek
2020-07-31  9:54         ` Jonathan Wakely [this message]
2020-07-31 10:06           ` Jakub Jelinek
2020-07-31 20:28             ` Jason Merrill
2020-08-27 10:06               ` Jakub Jelinek
2020-08-27 10:19                 ` Richard Biener
2020-09-02 21:52                   ` Jason Merrill
2020-08-27 10:46                 ` Jakub Jelinek
2020-08-27 11:06                   ` Jonathan Wakely
2020-08-27 11:17                     ` Jakub Jelinek
2020-08-27 11:22                       ` Jonathan Wakely
2020-08-27 10:59                 ` Jonathan Wakely
2020-08-27 20:43                 ` Iain Buclaw
2020-11-02 19:21   ` [PATCH] c++: v2: " Jakub Jelinek
2020-11-25  0:31     ` Jeff Law
2020-11-25  9:23       ` Jakub Jelinek
2020-11-25 10:31         ` Jonathan Wakely
2020-11-25 16:24           ` Jakub Jelinek
2020-11-25 16:28             ` Jonathan Wakely
2020-11-25 17:26     ` Jason Merrill
2020-11-25 18:50       ` Jakub Jelinek
2020-11-26  0:52         ` Jason Merrill
2020-11-26 15:09           ` [PATCH] c++: v3: " Jakub Jelinek
2020-12-03 14:24             ` Jason Merrill

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=20200731095446.GU3400@redhat.com \
    --to=jwakely@redhat.com \
    --cc=gcc-patches@gcc.gnu.org \
    --cc=jakub@redhat.com \
    --cc=jason@redhat.com \
    /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).