public inbox for libstdc++@gcc.gnu.org
 help / color / mirror / Atom feed
From: Jonathan Wakely <jwakely@redhat.com>
To: "François Dumont" <frs.dumont@gmail.com>
Cc: Alexandre Oliva <oliva@adacore.com>,
	Jonathan Wakely <jwakely.gcc@gmail.com>,
	 "libstdc++" <libstdc++@gcc.gnu.org>
Subject: Re: [PATCH v3] libstdc++: optimize bit iterators assuming normalization [PR110807]
Date: Thu, 9 Nov 2023 08:16:12 +0000	[thread overview]
Message-ID: <CACb0b4mXrW0L7cpynt=BRJbEemW+UnDfXi84DYZHcyPUWZ6HuA@mail.gmail.com> (raw)
In-Reply-To: <0e2723e5-a60d-484e-b4f4-951bc8dfde8d@gmail.com>

On Thu, 9 Nov 2023 at 05:57, François Dumont <frs.dumont@gmail.com> wrote:
>
>
> On 09/11/2023 04:36, Alexandre Oliva wrote:
> > On Nov  8, 2023, Jonathan Wakely <jwakely.gcc@gmail.com> wrote:
> >
> >> ofst needs to be __ofst but OK for trunk with that change.
> > Oh, doh, thanks for catching that last-minute tweak.
> >
> > Retesting with that change completed successfully, so I've just pushed
> > the following:
> >
> >
> > libstdc++: optimize bit iterators assuming normalization [PR110807]
> >
> > The representation of bit iterators, using a pointer into an array of
> > words, and an unsigned bit offset into that word, makes for some
> > optimization challenges: because the compiler doesn't know that the
> > offset is always in a certain narrow range, beginning at zero and
> > ending before the word bitwidth, when a function loads an offset that
> > it hasn't normalized itself, it may fail to derive certain reasonable
> > conclusions, even to the point of retaining useless calls that elicit
> > incorrect warnings.
> >
> > Case at hand: The 110807.cc testcase for bit vectors assigns a 1-bit
> > list to a global bit vector variable.  Based on the compile-time
> > constant length of the list, we decide in _M_insert_range whether to
> > use the existing storage or to allocate new storage for the vector.
> > After allocation, we decide in _M_copy_aligned how to copy any
> > preexisting portions of the vector to the newly-allocated storage.
> > When copying two or more words, we use __builtin_memmove.
> >
> > However, because we compute the available room using bit offsets
> > without range information, even comparing them with constants, we fail
> > to infer ranges for the preexisting vector depending on word size, and
> > may thus retain the memmove call despite knowing we've only allocated
> > one word.
> >
> > Other parts of the compiler then detect the mismatch between the
> > constant allocation size and the much larger range that could
> > theoretically be copied into the newly-allocated storage if we could
> > reach the call.
> >
> > Ensuring the compiler is aware of the constraints on the offset range
> > enables it to do a much better job at optimizing.  Using attribute
> > assume (_M_offset <= ...) didn't work, because gimple lowered that to
> > something that vrp could only use to ensure 'this' was non-NULL.
> > Exposing _M_offset as an automatic variable/gimple register outside
> > the unevaluated assume operand enabled the optimizer to do its job.
> >
> > Rather than placing such load-then-assume constructs all over, I
> > introduced an always-inline member function in bit iterators that does
> > the job of conveying to the compiler the information that the
> > assumption is supposed to hold, and various calls throughout functions
> > pertaining to bit iterators that might not otherwise know that the
> > offsets have to be in range, so that the compiler no longer needs to
> > make conservative assumptions that prevent optimizations.
> >
> > With the explicit assumptions, the compiler can correlate the test for
> > available storage in the vector with the test for how much storage
> > might need to be copied, and determine that, if we're not asking for
> > enough room for two or more words, we can omit entirely the code to
> > copy two or more words, without any runtime overhead whatsoever: no
> > traces remain of the undefined behavior or of the tests that inform
> > the compiler about the assumptions that must hold.
> >
> >
> > for  libstdc++-v3/ChangeLog
> >
> >       PR libstdc++/110807
> >       * include/bits/stl_bvector.h (_Bit_iterator_base): Add
> >       _M_assume_normalized member function.  Call it in _M_bump_up,
> >       _M_bump_down, _M_incr, operator==, operator<=>, operator<, and
> >       operator-.
> >       (_Bit_iterator): Also call it in operator*.
> >       (_Bit_const_iterator): Likewise.
> > ---
> >   libstdc++-v3/include/bits/stl_bvector.h |   37 ++++++++++++++++++++++++++++---
> >   1 file changed, 34 insertions(+), 3 deletions(-)
> >
> > diff --git a/libstdc++-v3/include/bits/stl_bvector.h b/libstdc++-v3/include/bits/stl_bvector.h
> > index 8d18bcaffd434..2b91af2005f2d 100644
> > --- a/libstdc++-v3/include/bits/stl_bvector.h
> > +++ b/libstdc++-v3/include/bits/stl_bvector.h
> > @@ -56,6 +56,10 @@
> >   #ifndef _STL_BVECTOR_H
> >   #define _STL_BVECTOR_H 1
> >
> > +#ifndef _GLIBCXX_ALWAYS_INLINE
> > +#define _GLIBCXX_ALWAYS_INLINE inline __attribute__((__always_inline__))
> > +#endif
> > +
>
> IMHO using [[__gnu__::__always_inline__]] would give the same result,
> but without the macro !

But only valid for C++11 and later, not C++98.

I think the reason we use _GLIBCXX_ALWAYS_INLINE like that is in case
it causes compiler errors ("'always_inline' function might not be
inlinable"), so that users can define the macro themselves to disable
the attribute. At least, that's my assumption of why we do it via
macros in several places.

But I've just realised we probably want to #undef the macro at the end
of bits/stl_bvector.h too.


  reply	other threads:[~2023-11-09  8:16 UTC|newest]

Thread overview: 18+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2023-11-08 16:10 [PATCH] " Alexandre Oliva
2023-11-08 19:32 ` Jonathan Wakely
2023-11-09  1:17   ` [PATCH v2] " Alexandre Oliva
2023-11-09  1:22     ` Jonathan Wakely
2023-11-09  3:36       ` [PATCH v3] " Alexandre Oliva
2023-11-09  5:57         ` François Dumont
2023-11-09  8:16           ` Jonathan Wakely [this message]
2023-11-09 19:49             ` [PATCH] libstdc++: bvector: undef always_inline macro Alexandre Oliva
2023-11-09 20:18               ` Jonathan Wakely
2023-11-15  2:20                 ` Patrick Palka
2023-11-15  5:53                   ` Alexandre Oliva
2023-11-15  2:44                 ` Alexandre Oliva
2023-11-15  5:08                   ` Alexandre Oliva
2023-11-15  8:22                   ` Jonathan Wakely
2023-11-16  4:40                     ` Alexandre Oliva
2024-02-07 16:25       ` [PATCH v2] libstdc++: optimize bit iterators assuming normalization [PR110807] Torbjorn SVENSSON
2024-02-07 16:36         ` Jonathan Wakely
2024-02-09  8:49           ` Torbjorn SVENSSON

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='CACb0b4mXrW0L7cpynt=BRJbEemW+UnDfXi84DYZHcyPUWZ6HuA@mail.gmail.com' \
    --to=jwakely@redhat.com \
    --cc=frs.dumont@gmail.com \
    --cc=jwakely.gcc@gmail.com \
    --cc=libstdc++@gcc.gnu.org \
    --cc=oliva@adacore.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).