This is the mail archive of the gcc-help@gcc.gnu.org mailing list for the GCC project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

Re: Weird optimization for tuples?


On 12 February 2017 at 00:55, David Guillen Fandos <david@davidgf.net> wrote:
> Hello!
>
> I have the following function
> std::tuple<uint32_t, uint8_t> getRawIdx(uint16_t tidx) {
>   return std::make_tuple(localidx.entries[tidx].indx_ptr,
> localidx.entries[tidx].indx_size);
> }
>
> Where s is a struct like
>
> typedef struct __attribute__((packed)) {
>         uint32_t indx_ptr;
>         uint8_t  indx_size;
> } _i_idx_entry ;
>
> The error I get is:
>
>  cannot bind packed field
> ‘((DBIndex*)this)->DBIndex::localidx.DBIndex::_i_idx::entries[((int)tidx)].DBIndex::_i_idx_entry::indx_ptr’
> to ‘unsigned int&’
>
> If extend the tuple to include a 3rd field and I return some other thing
> it just works:
>
> return std::make_tuple(localidx.entries[tidx].indx_ptr,
>                        localidx.entries[tidx].indx_size,
>                        tidx, prefb);
>
> I think gcc is trying to do some smart optimization or something and
> packed does not work because of tuple alignments not being packed.
>
> Any ideas?
> Thanks!
> David
>

This is the wrong mailing list for questions about possible bugs or
for help using GCC, please read https://gcc.gnu.org/lists.html and use
the list where this is on-topic (gcc-help). I've redirected this reply
that list.

This has nothing to do with std::tuple (the alignment of your struct
has no effect on the alignment of std::tuple<uint32_t, uin8_t>,
they're completely different types).

You get the same error without std::tuple:

struct __attribute__((packed)) S {
  int  indx_ptr;
  char indx_size;
};

S s;

struct T {
  T(int&, char&) { }
};

T getRawIdx() {
  return T(s.indx_ptr, s.indx_size);
}

It's related to binding references to unaligned variables, because
int& is a reference to an int with normal alignment, but the member of
your struct has different alignment to a normal int.

I think this is https://gcc.gnu.org/bugzilla/show_bug.cgi?id=36566


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]