DWARFv5 alignment attribute

Hello all, I am currently implementing support for DWARFv5 DW_AT_alignment attribute (). I am not 100% sure which entities we should mark with this attr. Consider the following C11 code (sample from ): // every object of type struct data will be aligned to 128-byte boundary struct data { char x; _Alignas(128) char cacheline[128]; // over-aligned array of char, // not array of over-aligned chars }; int main(void) { _Alignas(2048) struct data d; // this instance of data is aligned even stricter return 0; } What should be marked as DW_AT_alignment? The link above says that the attribute can be applied to: Here we have * DW_TAG_array_type * DW_TAG_subrange_type * DW_TAG_member * DW_TAG_structure_type * DW_TAG_basic_type * DW_TAG_variable Currently I mark only variable “d” as aligned, however I am not sure, that this is correct? Which of the listed should contain this attribute? What do you think? – Victor

The DWARF attribute should be attached to whatever entity is over-aligned in the source. The cppreference.com description says C11’s _Alignas applies to object declarations, rather than types, although putting it on a struct member will implicitly propagate to the containing struct type.

In this example, clearly the variable DIE for ‘d’ needs DW_AT_alignment(2048). However, that’s not sufficient, because if you had some other variable ‘struct data x;’ without an explicit alignment, you still need to specify that ‘x.cacheline’ is over-aligned. That means you also need DW_AT_alignment(128) on the member DIE for ‘cacheline’.

It’s debatable whether to put DW_AT_alignment(128) on the type DIE for ‘struct data’ as well, because of the implicit propagation. The argument against doing that is that consumers have always had to look at the content of the struct to determine its alignment, so putting it on the ‘cacheline’ member should be sufficient (a DWARF 5 consumer could reasonably be expected to notice the alignment attribute, given that it has to look at the member DIEs anyway).

There’s no reason to put DW_AT_alignment on anything related to the array type description or the base type.

–paulr