public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
* [Bug c++/114537] New: bit_cast does not work NSDMI of bitfields
@ 2024-03-31 19:33 pinskia at gcc dot gnu.org
  2024-04-01  9:40 ` [Bug c++/114537] " fchelnokov at gmail dot com
                   ` (5 more replies)
  0 siblings, 6 replies; 7+ messages in thread
From: pinskia at gcc dot gnu.org @ 2024-03-31 19:33 UTC (permalink / raw)
  To: gcc-bugs

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

            Bug ID: 114537
           Summary: bit_cast does not work NSDMI of bitfields
           Product: gcc
           Version: 14.0
            Status: UNCONFIRMED
          Keywords: FIXME
          Severity: normal
          Priority: P3
         Component: c++
          Assignee: unassigned at gcc dot gnu.org
          Reporter: pinskia at gcc dot gnu.org
  Target Milestone: ---

Take:
```
#include <bit>

struct A {  signed char b:1 = 0; signed char b1:7 = 0; };

struct B { unsigned char b; };

static_assert( std::bit_cast<B>(A{}).b == 0 );
```

This should work as the bitfields span all of the char space but currently we
get a sorry:
```
<source>:9:40: error: non-constant condition for static assertion
    9 | static_assert( std::bit_cast<B>(A{}).b == 0 );
      |                ~~~~~~~~~~~~~~~~~~~~~~~~^~~~
In file included from <source>:2:
<source>:9:32:   in 'constexpr' expansion of 'std::bit_cast<B, A>(A{0, 0})'
/opt/compiler-explorer/gcc-trunk-20240331/include/c++/14.0.1/bit:94:33: sorry,
unimplemented: '__builtin_bit_cast' cannot be constant evaluated because the
argument cannot be encoded
   94 |       return __builtin_bit_cast(_To, __from);
      |                                 ^~~
```

I had been looking for a quick workaround for PR 114536 (for little-endian
only) but it looked like NSDMI for bitfields is not implemented fully.

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

* [Bug c++/114537] bit_cast does not work NSDMI of bitfields
  2024-03-31 19:33 [Bug c++/114537] New: bit_cast does not work NSDMI of bitfields pinskia at gcc dot gnu.org
@ 2024-04-01  9:40 ` fchelnokov at gmail dot com
  2024-04-01 13:54 ` pinskia at gcc dot gnu.org
                   ` (4 subsequent siblings)
  5 siblings, 0 replies; 7+ messages in thread
From: fchelnokov at gmail dot com @ 2024-04-01  9:40 UTC (permalink / raw)
  To: gcc-bugs

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

--- Comment #1 from Fedor Chelnokov <fchelnokov at gmail dot com> ---
Probably related:
```
#include <bit>

struct A { int a: 7; };

static_assert( 1 == std::bit_cast<A>(std::bit_cast<int>(A{1})).a );
```
It looks valid and accepted by MSVC, but GCC prints:
error: '__builtin_bit_cast' accessing uninitialized byte at offset 0

Online demo: https://gcc.godbolt.org/z/3W5onY955

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

* [Bug c++/114537] bit_cast does not work NSDMI of bitfields
  2024-03-31 19:33 [Bug c++/114537] New: bit_cast does not work NSDMI of bitfields pinskia at gcc dot gnu.org
  2024-04-01  9:40 ` [Bug c++/114537] " fchelnokov at gmail dot com
@ 2024-04-01 13:54 ` pinskia at gcc dot gnu.org
  2024-04-03 15:10 ` jakub at gcc dot gnu.org
                   ` (3 subsequent siblings)
  5 siblings, 0 replies; 7+ messages in thread
From: pinskia at gcc dot gnu.org @ 2024-04-01 13:54 UTC (permalink / raw)
  To: gcc-bugs

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

--- Comment #2 from Andrew Pinski <pinskia at gcc dot gnu.org> ---
(In reply to Fedor Chelnokov from comment #1)
> Probably related:
> ```
> #include <bit>
> 
> struct A { int a: 7; };
> 
> static_assert( 1 == std::bit_cast<A>(std::bit_cast<int>(A{1})).a );
> ```
> It looks valid and accepted by MSVC, but GCC prints:
> error: '__builtin_bit_cast' accessing uninitialized byte at offset 0
> 
> Online demo: https://gcc.godbolt.org/z/3W5onY955

That is unrelated and gcc is actually correct there see pr 99637.

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

* [Bug c++/114537] bit_cast does not work NSDMI of bitfields
  2024-03-31 19:33 [Bug c++/114537] New: bit_cast does not work NSDMI of bitfields pinskia at gcc dot gnu.org
  2024-04-01  9:40 ` [Bug c++/114537] " fchelnokov at gmail dot com
  2024-04-01 13:54 ` pinskia at gcc dot gnu.org
@ 2024-04-03 15:10 ` jakub at gcc dot gnu.org
  2024-04-04  8:48 ` cvs-commit at gcc dot gnu.org
                   ` (2 subsequent siblings)
  5 siblings, 0 replies; 7+ messages in thread
From: jakub at gcc dot gnu.org @ 2024-04-03 15:10 UTC (permalink / raw)
  To: gcc-bugs

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

Jakub Jelinek <jakub at gcc dot gnu.org> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
             Status|UNCONFIRMED                 |ASSIGNED
   Last reconfirmed|                            |2024-04-03
     Ever confirmed|0                           |1
           Assignee|unassigned at gcc dot gnu.org      |jakub at gcc dot gnu.org

--- Comment #3 from Jakub Jelinek <jakub at gcc dot gnu.org> ---
Created attachment 57863
  --> https://gcc.gnu.org/bugzilla/attachment.cgi?id=57863&action=edit
gcc14-pr114537.patch

Untested fix.

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

* [Bug c++/114537] bit_cast does not work NSDMI of bitfields
  2024-03-31 19:33 [Bug c++/114537] New: bit_cast does not work NSDMI of bitfields pinskia at gcc dot gnu.org
                   ` (2 preceding siblings ...)
  2024-04-03 15:10 ` jakub at gcc dot gnu.org
@ 2024-04-04  8:48 ` cvs-commit at gcc dot gnu.org
  2024-04-05 18:44 ` fchelnokov at gmail dot com
  2024-04-21  4:08 ` cvs-commit at gcc dot gnu.org
  5 siblings, 0 replies; 7+ messages in thread
From: cvs-commit at gcc dot gnu.org @ 2024-04-04  8:48 UTC (permalink / raw)
  To: gcc-bugs

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

--- Comment #4 from GCC Commits <cvs-commit at gcc dot gnu.org> ---
The master branch has been updated by Jakub Jelinek <jakub@gcc.gnu.org>:

https://gcc.gnu.org/g:1baec8deb014b8a7da58879a407a4c00cdeb5a09

commit r14-9784-g1baec8deb014b8a7da58879a407a4c00cdeb5a09
Author: Jakub Jelinek <jakub@redhat.com>
Date:   Thu Apr 4 10:47:52 2024 +0200

    fold-const: Handle NON_LVALUE_EXPR in native_encode_initializer [PR114537]

    The following testcase is incorrectly rejected.  The problem is that
    for bit-fields native_encode_initializer expects the corresponding
    CONSTRUCTOR elt value must be INTEGER_CST, but that isn't the case
    here, it is wrapped into NON_LVALUE_EXPR by maybe_wrap_with_location.
    We could STRIP_ANY_LOCATION_WRAPPER as well, but as all we are looking for
    is INTEGER_CST inside, just looking through NON_LVALUE_EXPR seems easier.

    2024-04-04  Jakub Jelinek  <jakub@redhat.com>

            PR c++/114537
            * fold-const.cc (native_encode_initializer): Look through
            NON_LVALUE_EXPR if val is INTEGER_CST.

            * g++.dg/cpp2a/bit-cast16.C: New test.

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

* [Bug c++/114537] bit_cast does not work NSDMI of bitfields
  2024-03-31 19:33 [Bug c++/114537] New: bit_cast does not work NSDMI of bitfields pinskia at gcc dot gnu.org
                   ` (3 preceding siblings ...)
  2024-04-04  8:48 ` cvs-commit at gcc dot gnu.org
@ 2024-04-05 18:44 ` fchelnokov at gmail dot com
  2024-04-21  4:08 ` cvs-commit at gcc dot gnu.org
  5 siblings, 0 replies; 7+ messages in thread
From: fchelnokov at gmail dot com @ 2024-04-05 18:44 UTC (permalink / raw)
  To: gcc-bugs

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

--- Comment #5 from Fedor Chelnokov <fchelnokov at gmail dot com> ---
Sorry, in above example I had to use `unsigned char` type:

```
#include <bit>

struct A { unsigned char a : 7; };

static_assert( std::bit_cast<A>(std::bit_cast<unsigned char>(A{1})).a == 1 );
```

This program even after the fix in trunk, prints the same error as in the
original post:
<source>:5:32:   in 'constexpr' expansion of 'std::bit_cast<A, unsigned
char>(std::bit_cast<unsigned char, A>(A{1}))'
/opt/compiler-explorer/gcc-trunk-20240405/include/c++/14.0.1/bit:94:33: sorry,
unimplemented: '__builtin_bit_cast' cannot be constant evaluated because the
argument cannot be encoded
   94 |       return __builtin_bit_cast(_To, __from);

Online demo: https://gcc.godbolt.org/z/eqrsfGEnT

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

* [Bug c++/114537] bit_cast does not work NSDMI of bitfields
  2024-03-31 19:33 [Bug c++/114537] New: bit_cast does not work NSDMI of bitfields pinskia at gcc dot gnu.org
                   ` (4 preceding siblings ...)
  2024-04-05 18:44 ` fchelnokov at gmail dot com
@ 2024-04-21  4:08 ` cvs-commit at gcc dot gnu.org
  5 siblings, 0 replies; 7+ messages in thread
From: cvs-commit at gcc dot gnu.org @ 2024-04-21  4:08 UTC (permalink / raw)
  To: gcc-bugs

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

--- Comment #6 from GCC Commits <cvs-commit at gcc dot gnu.org> ---
The releases/gcc-13 branch has been updated by Jakub Jelinek
<jakub@gcc.gnu.org>:

https://gcc.gnu.org/g:a297f9bbb9611414fe48f6d61a8829bf5808bd2c

commit r13-8626-ga297f9bbb9611414fe48f6d61a8829bf5808bd2c
Author: Jakub Jelinek <jakub@redhat.com>
Date:   Thu Apr 4 10:47:52 2024 +0200

    fold-const: Handle NON_LVALUE_EXPR in native_encode_initializer [PR114537]

    The following testcase is incorrectly rejected.  The problem is that
    for bit-fields native_encode_initializer expects the corresponding
    CONSTRUCTOR elt value must be INTEGER_CST, but that isn't the case
    here, it is wrapped into NON_LVALUE_EXPR by maybe_wrap_with_location.
    We could STRIP_ANY_LOCATION_WRAPPER as well, but as all we are looking for
    is INTEGER_CST inside, just looking through NON_LVALUE_EXPR seems easier.

    2024-04-04  Jakub Jelinek  <jakub@redhat.com>

            PR c++/114537
            * fold-const.cc (native_encode_initializer): Look through
            NON_LVALUE_EXPR if val is INTEGER_CST.

            * g++.dg/cpp2a/bit-cast16.C: New test.

    (cherry picked from commit 1baec8deb014b8a7da58879a407a4c00cdeb5a09)

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

end of thread, other threads:[~2024-04-21  4:08 UTC | newest]

Thread overview: 7+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2024-03-31 19:33 [Bug c++/114537] New: bit_cast does not work NSDMI of bitfields pinskia at gcc dot gnu.org
2024-04-01  9:40 ` [Bug c++/114537] " fchelnokov at gmail dot com
2024-04-01 13:54 ` pinskia at gcc dot gnu.org
2024-04-03 15:10 ` jakub at gcc dot gnu.org
2024-04-04  8:48 ` cvs-commit at gcc dot gnu.org
2024-04-05 18:44 ` fchelnokov at gmail dot com
2024-04-21  4:08 ` cvs-commit at gcc dot gnu.org

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