public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
* [Bug tree-optimization/101518] New: Invalid value in bit-field when setting underlying data through reference
@ 2021-07-19 19:09 klimkin at gmail dot com
  2021-07-19 19:17 ` [Bug tree-optimization/101518] " pinskia at gcc dot gnu.org
  2021-07-19 19:22 ` klimkin at gmail dot com
  0 siblings, 2 replies; 3+ messages in thread
From: klimkin at gmail dot com @ 2021-07-19 19:09 UTC (permalink / raw)
  To: gcc-bugs

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

            Bug ID: 101518
           Summary: Invalid value in bit-field when setting underlying
                    data through reference
           Product: gcc
           Version: 11.1.0
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: tree-optimization
          Assignee: unassigned at gcc dot gnu.org
          Reporter: klimkin at gmail dot com
  Target Milestone: ---

In the example below, GCC starting v10 seems to produce invalid code for
accessing a bit field when the value for the underlying memory is set through a
reference. Consider example:

struct BitFieldLayout {
    using DataType     = unsigned;

    BitFieldLayout() { reinterpret_cast<DataType&>(*this) = {}; }

    DataType& raw_ref() { return reinterpret_cast<DataType&>(*this); }

    DataType a : 1;
};

class Ref {
public:
    operator unsigned&() { return value; }
    unsigned& value;
};

int f()
{
    BitFieldLayout bfl;
    Ref ref{bfl.raw_ref()};
    ref.value = 1;
    return bfl.a; // expected value is 1
}


g++ (Compiler-Explorer-Build) 11.1.0
f():
        xor     eax, eax
        ret
g++ (Compiler-Explorer-Build-gcc--binutils-2.36.1) 9.4.0
f():
        mov     eax, 1
        ret
clang version 12.0.1
f():                                  # @f()
        mov     eax, 1
        ret

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

* [Bug tree-optimization/101518] Invalid value in bit-field when setting underlying data through reference
  2021-07-19 19:09 [Bug tree-optimization/101518] New: Invalid value in bit-field when setting underlying data through reference klimkin at gmail dot com
@ 2021-07-19 19:17 ` pinskia at gcc dot gnu.org
  2021-07-19 19:22 ` klimkin at gmail dot com
  1 sibling, 0 replies; 3+ messages in thread
From: pinskia at gcc dot gnu.org @ 2021-07-19 19:17 UTC (permalink / raw)
  To: gcc-bugs

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

Andrew Pinski <pinskia at gcc dot gnu.org> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
             Status|UNCONFIRMED                 |RESOLVED
         Resolution|---                         |INVALID

--- Comment #1 from Andrew Pinski <pinskia at gcc dot gnu.org> ---
You are violating strict aliasing rules.
You can work around this either by the following:
may_alias attribute
memcpy (only portable option)
-fno-strict-aliasing
Unions

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

* [Bug tree-optimization/101518] Invalid value in bit-field when setting underlying data through reference
  2021-07-19 19:09 [Bug tree-optimization/101518] New: Invalid value in bit-field when setting underlying data through reference klimkin at gmail dot com
  2021-07-19 19:17 ` [Bug tree-optimization/101518] " pinskia at gcc dot gnu.org
@ 2021-07-19 19:22 ` klimkin at gmail dot com
  1 sibling, 0 replies; 3+ messages in thread
From: klimkin at gmail dot com @ 2021-07-19 19:22 UTC (permalink / raw)
  To: gcc-bugs

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

--- Comment #2 from Alexey Klimkin <klimkin at gmail dot com> ---
Thank you for your prompt reply!

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

end of thread, other threads:[~2021-07-19 19:22 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2021-07-19 19:09 [Bug tree-optimization/101518] New: Invalid value in bit-field when setting underlying data through reference klimkin at gmail dot com
2021-07-19 19:17 ` [Bug tree-optimization/101518] " pinskia at gcc dot gnu.org
2021-07-19 19:22 ` klimkin at gmail dot com

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