Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Compiling an aligned struct gives strange warning in GCC

We have multiple structs, which are 16-byte aligned. In previous versions of GCC everything worked fine. Since we upgraded to GCC 4.8.2 (previously we used 4.6), we get a bunch of warnings regarding these structs.

An example struct as:

typedef struct _STRUCT
{
    _STRUCT(): a(0),
           b(0) {};

    uint32_t    a;
    uint32_t    b;
} STRUCT __attribute__((aligned (16)));

Compiling this code throws the following warning where this strcut is used:

warning: ignoring attributes on template argument '_STRUCT' [enabled by default]

I really do not understand, what this warning is trying to tell me and searching Google did not help either.

like image 219
evotion Avatar asked Mar 19 '14 16:03

evotion


2 Answers

This looks like was purposeful according to C++ PATCH for c++/48138 (losing __attribute ((aligned)) on template argument), which says:

...except that we don't want to retain attributes on template type arguments, since they aren't part of mangling, so you could get a class template instantiation that is the same type regardless of the alignment of the argument, but the effective argument varies depending on which alignment was first used to instantiate it.

The PR suggests a warning when we drop the attributes, which makes sense. This patch does not yet provide the warning in the case of function templates, but does for class templates. Warning for function templates will wait until after Nathan's patch to improve template overloading diagnostics.

So it seems like the warning is new but the way it was dealt with is the same.

like image 52
Shafik Yaghmour Avatar answered Oct 24 '22 07:10

Shafik Yaghmour


@Shafik Yaghmour pushed me in the right direction. Inspecting the bug-report of the patch he mentioned got me to the right answer:

... but the core problem seems to be that alignments on typedefs aren't supported. attribute((aligned)) on template arguments seems to have no effect at all.

Source: http://gcc.gnu.org/bugzilla/show_bug.cgi?id=48138

That led to the answer that the position of __attribute__(aligned(16)) is the key. If i put it either after the keyword struct or the closing curly bracket the warning disappears, e.g.:

typedef struct __attribute__((aligned (16))) _STRUCT
{

or

    _STRUCT(): a(0),
               b(0) {};

    uint32_t    a;
    uint32_t    b;
} __attribute__((aligned (16))) STRUCT;

The former is the suggestion from the GCC doc. http://gcc.gnu.org/onlinedocs/gcc/Type-Attributes.html

For an enum, struct or union type, you may specify attributes either between the enum, struct or union tag and the name of the type, or just past the closing curly brace of the definition. The former syntax is preferred.

So the case is, that the the former implementation set the alignment for the typedef, the solution sets the alignment for the struct, which was the goal in the first place.

like image 35
evotion Avatar answered Oct 24 '22 06:10

evotion