public inbox for libstdc++@gcc.gnu.org
 help / color / mirror / Atom feed
From: Jonathan Wakely <jwakely@redhat.com>
To: Rainer Orth <ro@cebitec.uni-bielefeld.de>
Cc: Jonathan Wakely via Gcc-patches <gcc-patches@gcc.gnu.org>,
	"libstdc++" <libstdc++@gcc.gnu.org>
Subject: [committed] libstdc++: Fix undefined shift when _Atomic_word is 64-bit
Date: Wed, 8 Dec 2021 23:47:13 +0000	[thread overview]
Message-ID: <CACb0b4kFPDWmG-u-QMznjWwbCn53etpq16X2F0=Lr3ftQzX-6w@mail.gmail.com> (raw)
In-Reply-To: <CACb0b4kT7FN=DMoYy3WMNCY-bq88FzSf-=dUERJFy6Rad4naPw@mail.gmail.com>

[-- Attachment #1: Type: text/plain, Size: 2443 bytes --]

On Wed, 8 Dec 2021 at 19:27, Jonathan Wakely wrote:
>
> On Wed, 8 Dec 2021 at 19:21, Jonathan Wakely wrote:
> >
> > On Wed, 8 Dec 2021 at 19:17, Rainer Orth wrote:
> > >
> > > Hi Jonathan,
> > >
> > > > I've pushed this change to trunk now (it was posted and reviewed in
> > > > stage 1, I just didn't get around to pushing it until now).
> > > >
> > > > The final version of the patch is attached to this mail.
> > >
> > > unfortunately, it breaks Solaris/SPARC bootstrap:
> > >
> > > In file included from /var/gcc/regression/master/11.4-gcc-gas/build/sparc-sun-solaris2.11/sparcv9/libstdc++-v3/include/bits/shared_ptr.h:53,
> > >                  from /var/gcc/regression/master/11.4-gcc-gas/build/sparc-sun-solaris2.11/sparcv9/libstdc++-v3/include/memory:77,
> > >                  from /vol/gcc/src/hg/master/local/libstdc++-v3/include/precompiled/stdc++.h:82:
> > > /var/gcc/regression/master/11.4-gcc-gas/build/sparc-sun-solaris2.11/sparcv9/libstdc++-v3/include/bits/shared_ptr_base.h: In member function 'void std::_Sp_counted_base<_Lp>::_M_release() [with __gnu_cxx::_Lock_policy _Lp = __gnu_cxx::_S_atomic]':
> > > /var/gcc/regression/master/11.4-gcc-gas/build/sparc-sun-solaris2.11/sparcv9/libstdc++-v3/include/bits/shared_ptr_base.h:329:26: error: right operand of shift expression '(1 << 64)' is greater than or equal to the precision 64 of the left operand [-fpermissive]
> > >   329 |             = 1LL + (1LL << (__CHAR_BIT__ * sizeof(_Atomic_word)));
> > >       |                     ~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
> > > make[9]: *** [Makefile:1875: sparc-sun-solaris2.11/bits/stdc++.h.gch/O2ggnu++0x.gch] Error 1
> > >
> > > For 64-bit SPARC, _Atomic_word is long.
> >
> > Ah yes, so we need to disable this optimization. Patch coming up ...
>
> Gah, I remembered to check that:
>
>       constexpr bool __double_word
>         = sizeof(long long) == 2 * sizeof(_Atomic_word);
>       // The ref-count members follow the vptr, so are aligned to
>       // alignof(void*).
>       constexpr bool __aligned = __alignof(long long) <= alignof(void*);
>       if _GLIBCXX17_CONSTEXPR (__lock_free && __double_word && __aligned)
>
>
> But for C++11 and C++14 that is a normal runtime condition not
> if-constexpr, so the undefined shift still gets compiled, even though
> it can never be reached at runtime.

Fixed like so. Tested sparc-sun-solaris2.11, pushed to trunk.

[-- Attachment #2: patch.txt --]
[-- Type: text/plain, Size: 1632 bytes --]

commit c15aa46cca0649b68613d3292cf71c7cc57ef78f
Author: Jonathan Wakely <jwakely@redhat.com>
Date:   Wed Dec 8 19:36:24 2021

    libstdc++: Fix undefined shift when _Atomic_word is 64-bit
    
    The check for _Atomic_word being 32-bit is just a normal runtime
    condition for C++11 and C++14, because it doesn't use if-constexpr. That
    means the 1LL << (CHAR_BIT * sizeof(_Atomic_word)) expression expands to
    1LL << 64 on Solaris, which is ill-formed.
    
    This adds another indirection so that the shift width is zero if the
    code is unreachable.
    
    libstdc++-v3/ChangeLog:
    
            * include/bits/shared_ptr_base.h (_Sp_counted_base::_M_release()):
            Make shift width conditional on __double_word condition.

diff --git a/libstdc++-v3/include/bits/shared_ptr_base.h b/libstdc++-v3/include/bits/shared_ptr_base.h
index 90ad30947b0..f315d8f354f 100644
--- a/libstdc++-v3/include/bits/shared_ptr_base.h
+++ b/libstdc++-v3/include/bits/shared_ptr_base.h
@@ -325,8 +325,9 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
       constexpr bool __aligned = __alignof(long long) <= alignof(void*);
       if _GLIBCXX17_CONSTEXPR (__lock_free && __double_word && __aligned)
 	{
-	  constexpr long long __unique_ref
-	    = 1LL + (1LL << (__CHAR_BIT__ * sizeof(_Atomic_word)));
+	  constexpr int __wordbits = __CHAR_BIT__ * sizeof(_Atomic_word);
+	  constexpr int __shiftbits = __double_word ? __wordbits : 0;
+	  constexpr long long __unique_ref = 1LL + (1LL << __shiftbits);
 	  auto __both_counts = reinterpret_cast<long long*>(&_M_use_count);
 
 	  _GLIBCXX_SYNCHRONIZATION_HAPPENS_BEFORE(&_M_weak_count);

  reply	other threads:[~2021-12-08 23:47 UTC|newest]

Thread overview: 21+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2020-12-17 20:49 [PATCH] libstdc++: Skip atomic instructions in _Sp_counted_base::_M_release when both counts are 1 Maged Michael
2021-01-05 16:19 ` Maged Michael
2021-06-10 14:41 ` Maged Michael
2021-07-16 13:54 ` Jonathan Wakely
2021-07-16 15:55   ` Maged Michael
2021-08-02 13:23     ` Maged Michael
2021-08-02 13:29       ` Maged Michael
2021-08-03 20:59         ` Jonathan Wakely
2021-08-04 15:32           ` Jonathan Wakely
2021-08-04 15:47             ` Maged Michael
2021-08-04 15:52               ` Jonathan Wakely
2021-08-04 17:19             ` Maged Michael
2021-08-04 17:34               ` Maged Michael
2021-08-04 19:32               ` Jonathan Wakely
2021-08-04 19:48                 ` Maged Michael
2021-12-08 11:49                   ` Jonathan Wakely
2021-12-08 19:15                     ` Rainer Orth
2021-12-08 19:21                       ` Jonathan Wakely
2021-12-08 19:27                         ` Jonathan Wakely
2021-12-08 23:47                           ` Jonathan Wakely [this message]
2021-12-09 12:28                             ` [committed] libstdc++: Fix undefined shift when _Atomic_word is 64-bit Rainer Orth

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='CACb0b4kFPDWmG-u-QMznjWwbCn53etpq16X2F0=Lr3ftQzX-6w@mail.gmail.com' \
    --to=jwakely@redhat.com \
    --cc=gcc-patches@gcc.gnu.org \
    --cc=libstdc++@gcc.gnu.org \
    --cc=ro@cebitec.uni-bielefeld.de \
    /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).