<div dir="ltr">the packed + aligned attribute will automatically introduce explicit padding byte array:<div><a href="https://godbolt.org/g/TlHX2g">https://godbolt.org/g/TlHX2g</a><br></div><div><br></div><div>Sometimes Clang will decide to automatically pack the struct/class in C++, I don't know the details here, but looks like it is related to inheritance.</div><div><br></div><div>Thanks</div><div>Hongbin</div><div><br></div></div><div class="gmail_extra"><br><div class="gmail_quote">On Thu, May 18, 2017 at 1:32 AM, mats petersson <span dir="ltr"><<a href="mailto:mats@planetcatfish.com" target="_blank">mats@planetcatfish.com</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div dir="ltr"><div><div><div><div><div><div><div><div>How do you mean that a byte array is added? Because at least in my experiments, I don't see that:<br><br>struct A<br>{<br>    int a;<br>    char b;<br>    long c;<br>};<br><br>struct A a;<br><br></div>produces:<br><br>; ModuleID = 'pad.c'<br>target datalayout = "e-m:e-i64:64-f80:128-n8:16:<wbr>32:64-S128"<br>target triple = "x86_64-unknown-linux-gnu"<br><br>%struct.A = type { i32, i8, i64 }<br><br>@a = common global %struct.A zeroinitializer, align 8<br><br>!llvm.ident = !{!0}<br><br><br></div>Adding a call to printf, <br><br>extern int printf(const char *fmt, ...);<br>void func(struct A* a)<br>{<br>    printf("c=%ld", a->c);<br>}<br><br>and outputting assembler, we can see that the offset to "c" in that struct is 8:<br><br>func:                         <wbr>          # @func<br>    .cfi_startproc<br># BB#0:                         <wbr>        # %entry<br>    movq    8(%rdi), %rsi<br>    movl    $.L.str, %edi<br>    xorl    %eax, %eax<br>    jmp    printf                  # TAILCALL<br><br></div>So, can you provide an example of this padding, because I don't see it. This is clang 3.8, but 3.9 did the same thing (I went back to 3.8 to check if it was different)<br><br></div>There will be padding in the actual data structure, based on the need  for aligning (better performance if not required by the hardware), so if we for example initalize the data:<br>struct A a = { 3, 'a', 4711 };<br></div>then there will be LLVM-code like this:<br>@a = global %struct.A { i32 3, i8 97, i64 4711 }, align 8<br></div>and in the machine code there will be:<br>a:<br>    .long    3                       # 0x3<br>    .byte    97                      # 0x61<br>    .zero    3<br>    .quad    4711                    # 0x1267<br><br></div>Because three bytes of zeros are needed to fill the data between the 'a' and the long of 4711. But nowhere other than in the machine-code is that padding anything  more than "difference between theoretical closest offset and aligned offset".<br><br>--<br></div>Mats<br></div><div class="HOEnZb"><div class="h5"><div class="gmail_extra"><br><div class="gmail_quote">On 18 May 2017 at 09:14, Hongbin Zheng <span dir="ltr"><<a href="mailto:etherzhhb@gmail.com" target="_blank">etherzhhb@gmail.com</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div dir="ltr">Hi Mats,<div><br></div><div>When the struct is packed, explicit byte array is introduced to pad the struct. (I saw this happened in clang 3.9.)</div><div><br></div><div>I want to check if a byte or byte array in an LLVM struct is introduce for explicit padding or not.</div><div><br></div><div>I don't need to worry about this problem in case the newest clang do not introduce byte array anymore.</div><div><br></div><div>Thanks</div><span class="m_2327969754097084487HOEnZb"><font color="#888888"><div>Hongbin</div></font></span></div><div class="m_2327969754097084487HOEnZb"><div class="m_2327969754097084487h5"><div class="gmail_extra"><br><div class="gmail_quote">On Thu, May 18, 2017 at 1:03 AM, mats petersson <span dir="ltr"><<a href="mailto:mats@planetcatfish.com" target="_blank">mats@planetcatfish.com</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div dir="ltr"><div><div>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].<br><br></div>Note that there is no actual field added for padding, it's just the size and alignment itself.<br><br>--<br></div>Mats<br></div><div class="gmail_extra"><br><div class="gmail_quote"><div><div class="m_2327969754097084487m_-4106980542756951347h5">On 18 May 2017 at 08:51, Hongbin Zheng via cfe-dev <span dir="ltr"><<a href="mailto:cfe-dev@lists.llvm.org" target="_blank">cfe-dev@lists.llvm.org</a>></span> wrote:<br></div></div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div><div class="m_2327969754097084487m_-4106980542756951347h5"><div dir="ltr">Hi Jonas,<div><br></div><div>Thanks a lot.</div><div>In an LLVM pass, how can I check the related information? will clang emit some metadata table?</div><div><br></div><div>Thanks</div><span class="m_2327969754097084487m_-4106980542756951347m_5865314057619861833HOEnZb"><font color="#888888"><div>Hongbin</div></font></span></div><div class="m_2327969754097084487m_-4106980542756951347m_5865314057619861833HOEnZb"><div class="m_2327969754097084487m_-4106980542756951347m_5865314057619861833h5"><div class="gmail_extra"><br><div class="gmail_quote">On Thu, May 18, 2017 at 12:47 AM, Jonas Devlieghere <span dir="ltr"><<a href="mailto:jonas@devlieghere.com" target="_blank">jonas@devlieghere.com</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div dir="ltr"><div class="gmail_default"><font face="tahoma, sans-serif" color="#20124d">Hi Hongbin,</font></div><div class="gmail_default"><font face="tahoma, sans-serif" color="#20124d"><br></font></div><div class="gmail_default"><font face="tahoma, sans-serif" color="#20124d">You can pass `-Wpadded` to clang. For your particular example it will print something along the lines of <br><br>```<br><div class="gmail_default">warning: padding struct 'foo1' with 7 bytes to align 'x' [-Wpadded]</div><div class="gmail_default">    long x;</div><div class="gmail_default">```</div><div class="gmail_default"><br></div><div class="gmail_default">Jonas</div></font></div><div class="gmail_extra">
<br><div class="gmail_quote"><div><div class="m_2327969754097084487m_-4106980542756951347m_5865314057619861833m_5593048658879213703h5">On Thu, May 18, 2017 at 9:15 AM, Hongbin Zheng via llvm-dev <span dir="ltr"><<a href="mailto:llvm-dev@lists.llvm.org" target="_blank">llvm-dev@lists.llvm.org</a>></span> wrote:<br></div></div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div><div class="m_2327969754097084487m_-4106980542756951347m_5865314057619861833m_5593048658879213703h5"><div dir="ltr">Hi,<div><br></div><div>I am wondering how I can tell whether a field of a struct is introduced by padding or not.</div><div><br></div><div>For example, if I have a struct:</div><br>struct foo1 {<br>    char *p;     /* 8 bytes */<br>    char c;      /* 1 byte<br>    long x;      /* 8 bytes */<br>};<div><br></div><div>clang may generate:</div><br>struct foo1 {<br>    char *p;     /* 8 bytes */<br>    char c;      /* 1 byte<br>    char pad[7]; /* 7 bytes */<br>    long x;      /* 8 bytes */<br>};<div><br></div><div>Is there any way that I can tell the "pad" array is generated by padding?</div><div><br></div><div>Thanks a lot</div><span class="m_2327969754097084487m_-4106980542756951347m_5865314057619861833m_5593048658879213703m_-9083378872937218573HOEnZb"><font color="#888888"><div>Hongbin</div></font></span></div>
<br></div></div>______________________________<wbr>_________________<br>
LLVM Developers mailing list<br>
<a href="mailto:llvm-dev@lists.llvm.org" target="_blank">llvm-dev@lists.llvm.org</a><br>
<a href="http://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-dev" rel="noreferrer" target="_blank">http://lists.llvm.org/cgi-bin/<wbr>mailman/listinfo/llvm-dev</a><br>
<br></blockquote></div><br></div></div>
</blockquote></div><br></div>
</div></div><br></div></div>______________________________<wbr>_________________<br>
cfe-dev mailing list<br>
<a href="mailto:cfe-dev@lists.llvm.org" target="_blank">cfe-dev@lists.llvm.org</a><br>
<a href="http://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-dev" rel="noreferrer" target="_blank">http://lists.llvm.org/cgi-bin/<wbr>mailman/listinfo/cfe-dev</a><br>
<br></blockquote></div><br></div>
</blockquote></div><br></div>
</div></div></blockquote></div><br></div>
</div></div></blockquote></div><br></div>