public inbox for gcc-patches@gcc.gnu.org
 help / color / mirror / Atom feed
From: Jakub Jelinek <jakub@redhat.com>
To: Richard Biener <rguenther@suse.de>
Cc: Jonathan Wakely <jwakely@redhat.com>, gcc-patches@gcc.gnu.org
Subject: Re: [PATCH] Avoid default-initializing auto_vec<T, N> storage
Date: Fri, 24 Feb 2023 10:48:54 +0100	[thread overview]
Message-ID: <Y/iIBvBbpIKDSRTr@tucnak> (raw)
In-Reply-To: <nycvar.YFH.7.77.849.2302240919370.27913@jbgna.fhfr.qr>

On Fri, Feb 24, 2023 at 09:34:46AM +0000, Richard Biener wrote:
> > Looking at vec<T, A, vl_embed>::operator[] which just does
> > 
> > template<typename T, typename A>
> > inline const T &
> > vec<T, A, vl_embed>::operator[] (unsigned ix) const
> > {
> >   gcc_checking_assert (ix < m_vecpfx.m_num);
> >   return m_vecdata[ix];
> > } 
> > 
> > the whole thing looks fragile at best - we basically have
> > 
> > struct auto_vec
> > {
> >   struct vec<vl_embed>
> >   {
> > ...
> >     T m_vecdata[1];
> >   } m_auto;
> >   T m_data[N-1];
> > };

Assuming a compiler handles the T m_vecdata[1]; as flexible array member
like (which we need because standard C++ doesn't have flexible array members
nor [0] arrays), I wonder if we instead of the m_auto followed by m_data
trick couldn't make auto_vec have
alignas(vec<vl_embed>) unsigned char buf m_data[sizeof (vec<vl_embed>) + (N - 1) * sizeof (T)];
and do a placement new of vec<vl_embed> into that m_data during auto_vec
construction.  Isn't it then similar to how are flexible array members
normally used in C, where one uses malloc or alloca to allocate storage
for them and the storage can be larger than the structure itself and
flexible array member then can use storage after it?

Though, of course, we'd need to test it with various compilers,
GCC 4.8 till now, various versions of clang, ICC, ...

	Jakub


  reply	other threads:[~2023-02-24  9:49 UTC|newest]

Thread overview: 15+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2023-02-23 12:54 Richard Biener
2023-02-23 13:56 ` Jakub Jelinek
2023-02-23 15:02   ` Richard Biener
2023-02-23 15:20     ` Jakub Jelinek
2023-02-24  9:02       ` Richard Biener
2023-02-24  9:34         ` Richard Biener
2023-02-24  9:48           ` Jakub Jelinek [this message]
2023-02-24  9:50             ` Jonathan Wakely
2023-02-24  9:55               ` Jakub Jelinek
2023-02-24  9:55               ` Jonathan Wakely
2023-02-24 10:02                 ` Jakub Jelinek
2023-02-24 10:24                   ` Jakub Jelinek
2023-02-24 10:30                     ` Jonathan Wakely
2023-02-24 10:59                       ` Jakub Jelinek
2023-02-24 11:04                         ` Jakub Jelinek

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=Y/iIBvBbpIKDSRTr@tucnak \
    --to=jakub@redhat.com \
    --cc=gcc-patches@gcc.gnu.org \
    --cc=jwakely@redhat.com \
    --cc=rguenther@suse.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).