[cfe-dev] [llvm-dev] Struct padding

mats petersson via cfe-dev cfe-dev at lists.llvm.org
Thu May 18 01:03:39 PDT 2017


What are you actually trying to achieve? LLVM knows the alignment and size
of each component. You could iterate over the different types and identify
when there is a difference in "calculated total size and the current
alignment requirement", but LLVM does automatically pad structures [unless
you specifically ask it not to].

Note that there is no actual field added for padding, it's just the size
and alignment itself.

--
Mats

On 18 May 2017 at 08:51, Hongbin Zheng via cfe-dev <cfe-dev at lists.llvm.org>
wrote:

> Hi Jonas,
>
> Thanks a lot.
> In an LLVM pass, how can I check the related information? will clang emit
> some metadata table?
>
> Thanks
> Hongbin
>
> On Thu, May 18, 2017 at 12:47 AM, Jonas Devlieghere <jonas at devlieghere.com
> > wrote:
>
>> Hi Hongbin,
>>
>> You can pass `-Wpadded` to clang. For your particular example it will
>> print something along the lines of
>>
>> ```
>> warning: padding struct 'foo1' with 7 bytes to align 'x' [-Wpadded]
>>     long x;
>> ```
>>
>> Jonas
>>
>> On Thu, May 18, 2017 at 9:15 AM, Hongbin Zheng via llvm-dev <
>> llvm-dev at lists.llvm.org> wrote:
>>
>>> Hi,
>>>
>>> I am wondering how I can tell whether a field of a struct is introduced
>>> by padding or not.
>>>
>>> For example, if I have a struct:
>>>
>>> struct foo1 {
>>>     char *p;     /* 8 bytes */
>>>     char c;      /* 1 byte
>>>     long x;      /* 8 bytes */
>>> };
>>>
>>> clang may generate:
>>>
>>> struct foo1 {
>>>     char *p;     /* 8 bytes */
>>>     char c;      /* 1 byte
>>>     char pad[7]; /* 7 bytes */
>>>     long x;      /* 8 bytes */
>>> };
>>>
>>> Is there any way that I can tell the "pad" array is generated by padding?
>>>
>>> Thanks a lot
>>> Hongbin
>>>
>>> _______________________________________________
>>> LLVM Developers mailing list
>>> llvm-dev at lists.llvm.org
>>> http://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-dev
>>>
>>>
>>
>
> _______________________________________________
> cfe-dev mailing list
> cfe-dev at lists.llvm.org
> http://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-dev
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.llvm.org/pipermail/cfe-dev/attachments/20170518/43968b07/attachment.html>


More information about the cfe-dev mailing list