<div dir="auto"><div class="gmail_extra" dir="auto"><div class="gmail_quote">On 9 Feb 2017 11:54 am, "Martin J. O'Riordan" <<a href="mailto:martin.oriordan@movidius.com">martin.oriordan@movidius.com</a>> wrote:<br type="attribution"><blockquote class="quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div lang="EN-IE" link="blue" vlink="purple"><div class="m_-8017098969044671129WordSection1"><p class="MsoNormal"><span style="font-family:"Book Antiqua",serif;color:#943634">Hi Richard,<u></u><u></u></span></p><p class="MsoNormal"><span style="font-family:"Book Antiqua",serif;color:#943634"><u></u> <u></u></span></p><p class="MsoNormal"><span style="font-family:"Book Antiqua",serif;color:#943634">How does ‘</span><span style="font-family:"Courier New";color:black">packed</span><span style="font-family:"Book Antiqua",serif;color:#943634">’ work in this context?  I thought ‘</span><span style="font-family:"Courier New";color:black">packed</span><span style="font-family:"Book Antiqua",serif;color:#943634">’ would cause the whole aggregate to eliminate padding, rather than just changing the alignment requirements of a single member.</span></p></div></div></blockquote></div></div><div dir="auto">What packed actually does is to reduce the minimum alignment to 1. When applied to a struct, it is effectively applied to all fields.</div><div class="gmail_extra" dir="auto"></div><div class="gmail_extra" dir="auto"><div class="gmail_quote"><blockquote class="quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div lang="EN-IE" link="blue" vlink="purple"><div class="m_-8017098969044671129WordSection1"><p class="MsoNormal"><span style="font-family:"Book Antiqua",serif;color:#943634">What I want is that the outer aggregate is aligned in the normal way according the maximum alignment requirements of its members.  However, I want to also have a member which itself is an aggregate with its usual internal padding retained, but changed its alignment constraint (essentially lying) within the context of the enclosing aggregate.<u></u><u></u></span></p><p class="MsoNormal"><span style="font-family:"Book Antiqua",serif;color:#943634"><u></u> <u></u></span></p><p class="MsoNormal"><span style="font-family:"Book Antiqua",serif;color:#943634">Would ‘</span><span style="font-family:"Courier New";color:black">packed</span><span style="font-family:"Book Antiqua",serif;color:#943634">’ not just unilaterally remove the padding?<u></u><u></u></span></p><p class="MsoNormal"><span style="font-family:"Book Antiqua",serif;color:#943634"><u></u> <u></u></span></p><p class="MsoNormal"><span style="font-family:"Book Antiqua",serif;color:#943634">For example (assuming </span><span style="font-family:"Courier New";color:black">char</span><span style="font-family:"Book Antiqua",serif;color:black"> </span><span style="font-family:"Book Antiqua",serif;color:#943634">is 1-byte and </span><span style="font-family:"Courier New";color:black">int</span><span style="font-family:"Book Antiqua",serif;color:black"> </span><span style="font-family:"Book Antiqua",serif;color:#943634">is 4, and natural alignment):<u></u><u></u></span></p><p class="MsoNormal"><span style="font-family:"Book Antiqua",serif;color:#943634"><u></u> <u></u></span></p><p class="MsoNormal" style="margin-left:36.0pt"><span style="font-family:"Courier New";color:black">struct Inner {<u></u><u></u></span></p><p class="MsoNormal" style="margin-left:36.0pt"><span style="font-family:"Courier New";color:black">  char c;<u></u><u></u></span></p><p class="MsoNormal" style="margin-left:36.0pt"><span style="font-family:"Courier New";color:black">  int  i;<u></u><u></u></span></p><p class="MsoNormal" style="margin-left:36.0pt"><span style="font-family:"Courier New";color:black">};<u></u><u></u></span></p><p class="MsoNormal" style="margin-left:36.0pt"><span style="font-family:"Courier New";color:black">Inner x;<u></u><u></u></span></p><p class="MsoNormal"><span style="font-family:"Book Antiqua",serif;color:#943634"><u></u> <u></u></span></p><p class="MsoNormal"><span style="font-family:"Book Antiqua",serif;color:#943634">I can reasonably assume on a naturally aligned system that the following would hold true (undefined ISO behaviour ignored regarding address arithmetic - assume a simple conventional 32-bit Von Neumann address space):<u></u><u></u></span></p><p class="MsoNormal"><span style="font-family:"Book Antiqua",serif;color:#943634"><u></u> <u></u></span></p><p class="MsoNormal" style="margin-left:36.0pt"><span style="font-family:"Courier New";color:black">sizeof(Inner) == 8<u></u><u></u></span></p><p class="MsoNormal" style="margin-left:36.0pt"><span style="font-family:"Courier New";color:black">((unsigned)&x & 7) == 0<u></u><u></u></span></p><p class="MsoNormal" style="margin-left:36.0pt"><span style="font-family:"Courier New";color:black">((unsigned)&x.i - (unsigned)&x.c) == 4<u></u><u></u></span></p><p class="MsoNormal" style="margin-left:36.0pt"><span style="font-family:"Courier New";color:black">offsetof(Outer, c) == 0<u></u><u></u></span></p><p class="MsoNormal" style="margin-left:36.0pt"><span style="font-family:"Courier New";color:black">offsetof(Outer, i) == 4<u></u><u></u></span></p><p class="MsoNormal"><span style="font-family:"Book Antiqua",serif;color:#943634"><u></u> <u></u></span></p><p class="MsoNormal"><span style="font-family:"Book Antiqua",serif;color:#943634">Now what I want to do is:<u></u><u></u></span></p><p class="MsoNormal"><span style="font-family:"Book Antiqua",serif;color:#943634"><u></u> <u></u></span></p><p class="MsoNormal" style="margin-left:36.0pt"><span style="font-family:"Courier New";color:black">struct Outer {<u></u><u></u></span></p><p class="MsoNormal" style="margin-left:36.0pt"><span style="font-family:"Courier New";color:black">  char aa;<u></u><u></u></span></p><p class="MsoNormal" style="margin-left:36.0pt"><span style="font-family:"Courier New";color:black">  int  bb;<u></u><u></u></span></p><p class="MsoNormal" style="margin-left:36.0pt"><span style="font-family:"Courier New";color:black">  char cc;<u></u><u></u></span></p><p class="MsoNormal" style="margin-left:36.0pt"><span style="font-family:"Courier New";color:black">  [magic attribute] Inner xx;<u></u><u></u></span></p><p class="MsoNormal" style="margin-left:36.0pt"><span style="font-family:"Courier New";color:black">  char dd;<u></u><u></u></span></p><p class="MsoNormal" style="margin-left:36.0pt"><span style="font-family:"Courier New";color:black">  int  ee;<u></u><u></u></span></p><p class="MsoNormal" style="margin-left:36.0pt"><span style="font-family:"Courier New";color:black">};<u></u><u></u></span></p><p class="MsoNormal" style="margin-left:36.0pt"><span style="font-family:"Courier New";color:black">Outer y;<u></u><u></u></span></p><p class="MsoNormal"><span style="font-family:"Book Antiqua",serif;color:#943634"><u></u> <u></u></span></p><p class="MsoNormal"><span style="font-family:"Book Antiqua",serif;color:#943634">Such that:<u></u><u></u></span></p><p class="MsoNormal"><span style="font-family:"Book Antiqua",serif;color:#943634"><u></u> <u></u></span></p><p class="MsoNormal" style="margin-left:36.0pt"><span style="font-family:"Courier New";color:black">sizeof(Outer) == 24<u></u><u></u></span></p><p class="MsoNormal" style="margin-left:36.0pt"><span style="font-family:"Courier New";color:black">((unsigned)&y & 7) == 0<u></u><u></u></span></p><p class="MsoNormal" style="margin-left:36.0pt"><span style="font-family:"Courier New";color:black">((unsigned)&y.xx.i - (unsigned)&y.xx.c) == 4<u></u><u></u></span></p><p class="MsoNormal" style="margin-left:36.0pt"><span style="font-family:"Courier New";color:black">((unsigned)&y.xx.i - (unsigned)&y) == 13<u></u><u></u></span></p><p class="MsoNormal" style="margin-left:36.0pt"><span style="font-family:"Courier New";color:black">offsetof(Inner, bb) == 4<u></u><u></u></span></p><p class="MsoNormal" style="margin-left:36.0pt"><span style="font-family:"Courier New";color:black">offsetof(Inner, cc) == 8<u></u><u></u></span></p><p class="MsoNormal" style="margin-left:36.0pt"><span style="font-family:"Courier New";color:black">offsetof(Inner, xx) == 9<u></u><u></u></span></p><p class="MsoNormal" style="margin-left:36.0pt"><span style="font-family:"Courier New";color:black">offsetof(Inner, dd) == 17<u></u><u></u></span></p><p class="MsoNormal" style="margin-left:36.0pt"><span style="font-family:"Courier New";color:black">offsetof(Inner, ee) == 20<u></u><u></u></span></p><p class="MsoNormal"><span style="font-family:"Book Antiqua",serif;color:#943634"><u></u> <u></u></span></p><p class="MsoNormal"><span style="font-family:"Book Antiqua",serif;color:#943634">so padding is still present within the layout of ‘</span><span style="font-family:"Courier New";color:black">xx</span><span style="font-family:"Book Antiqua",serif;color:#943634">’, and also as normal for the member alignment of ‘</span><span style="font-family:"Courier New";color:black">aa</span><span style="font-family:"Book Antiqua",serif;color:#943634">’, ‘</span><span style="font-family:"Courier New";color:black">bb</span><span style="font-family:"Book Antiqua",serif;color:#943634">’, ‘</span><span style="font-family:"Courier New";color:black">cc</span><span style="font-family:"Book Antiqua",serif;color:#943634">’, ‘</span><span style="font-family:"Courier New";color:black">dd</span><span style="font-family:"Book Antiqua",serif;color:#943634">’ and ‘</span><span style="font-family:"Courier New";color:black">ee</span><span style="font-family:"Book Antiqua",serif;color:#943634">’ within ‘</span><span style="font-family:"Courier New";color:black">Outer</span><span style="font-family:"Book Antiqua",serif;color:#943634">’, but not for ‘</span><span style="font-family:"Courier New";color:black">xx</span><span style="font-family:"Book Antiqua",serif;color:#943634">’.<u></u><u></u></span></p><p class="MsoNormal"><span style="font-family:"Book Antiqua",serif;color:#943634"><u></u> <u></u></span></p><p class="MsoNormal"><span style="font-family:"Book Antiqua",serif;color:#943634">This is useful when overlaying structures on registers in a memory-mapped device which can have weird arrangements, and avoid the need for dirty little macro arithmetic tricks which are sadly very common in embedded systems device driver code.<u></u><u></u></span></p><p class="MsoNormal"><span style="font-family:"Book Antiqua",serif;color:#943634"><u></u> <u></u></span></p><p class="MsoNormal"><span style="font-family:"Book Antiqua",serif;color:#943634">So forgive me if I am misunderstanding ‘</span><span style="font-family:"Courier New";color:black">packed</span><span style="font-family:"Book Antiqua",serif;color:#943634">’ (and I probably am), but will this not cause the members of ‘</span><span style="font-family:"Courier New";color:black">Inner</span><span style="font-family:"Book Antiqua",serif;color:#943634">’ as a member of ‘</span><span style="font-family:"Courier New";color:black">Outer</span><span style="font-family:"Book Antiqua",serif;color:#943634">’ to become packed (</span><span style="font-family:"Courier New";color:black">(unsigned)&i == (unsigned)&c + 1</span><span style="font-family:"Book Antiqua",serif;color:#943634">), or does it simply loosen the alignment constraint within the members of ‘</span><span style="font-family:"Courier New";color:black">Outer</span><span style="font-family:"Book Antiqua",serif;color:#943634">’ to achieve what I intend?  I usually interpret ‘</span><span style="font-family:"Courier New";color:black">packed</span><span style="font-family:"Book Antiqua",serif;color:#943634">’ as all-or-nothing.  So if I replace ‘</span><span style="font-family:"Courier New";color:black">[magic attribute]</span><span style="font-family:"Book Antiqua",serif;color:#943634">’ with ‘</span><span style="font-family:"Courier New";color:black">__attribute__((packed))</span><span style="font-family:"Book Antiqua",serif;color:#943634">’, this will achieve the effect I require?  What if I want to force alignment of ‘</span><span style="font-family:"Courier New";color:black">xx</span><span style="font-family:"Book Antiqua",serif;color:#943634">’ to 2-bytes instead of 1?<u></u><u></u></span></p><p class="MsoNormal"><span style="font-family:"Book Antiqua",serif;color:#943634"><u></u> <u></u></span></p><p class="MsoNormal"><span style="font-family:"Book Antiqua",serif;color:#943634">The only reasonably portable way I know of doing this kind of thing, is to always pack structures, and to explicitly provide the dummy padding bytes (including bitfield alignment magic such as ‘</span><span style="font-family:"Courier New";color:black">int:0;</span><span style="font-family:"Book Antiqua",serif;color:#943634">’), but while these are mostly-portable, they are not absolute in the context of a specific implementation - which is where ‘</span><span style="font-family:"Courier New";color:black">__attribute__((aligned(x)))</span><span style="font-family:"Book Antiqua",serif;color:#943634">’ is necessary.<u></u><u></u></span></p><p class="MsoNormal"><span style="font-family:"Book Antiqua",serif;color:#943634"><u></u> <u></u></span></p><p class="MsoNormal"><span style="font-family:"Book Antiqua",serif;color:#943634">In any case, the incongruity of using ‘</span><span style="font-family:"Courier New";color:black">aligned</span><span style="font-family:"Book Antiqua",serif;color:#943634">’ in a ‘</span><span style="font-family:"Courier New";color:black">typedef</span><span style="font-family:"Book Antiqua",serif;color:#943634">’ versus explicitly on the member is quite unintuitive, though I fully understand the need for CLang to maintain compatibility with GCC in this regard.<u></u><u></u></span></p><p class="MsoNormal"><span style="font-family:"Book Antiqua",serif;color:#943634"><u></u> <u></u></span></p><p class="MsoNormal"><span style="font-family:"Book Antiqua",serif;color:#943634">Thanks,<u></u><u></u></span></p><p class="MsoNormal"><span style="font-family:"Book Antiqua",serif;color:#943634"><u></u> <u></u></span></p><p class="MsoNormal"><span style="font-family:"Book Antiqua",serif;color:#943634">            MartinO<u></u><u></u></span></p><p class="MsoNormal"><span style="font-family:"Book Antiqua",serif;color:#943634"><u></u> <u></u></span></p><p class="MsoNormal"><b><span lang="EN-US" style="font-size:11.0pt;font-family:"Calibri",sans-serif">From:</span></b><span lang="EN-US" style="font-size:11.0pt;font-family:"Calibri",sans-serif"> <a href="mailto:metafoo@gmail.com" target="_blank">metafoo@gmail.com</a> [mailto:<a href="mailto:metafoo@gmail.com" target="_blank">metafoo@gmail.com</a>] <b>On Behalf Of </b>Richard Smith<br><b>Sent:</b> 09 February 2017 18:03</span></p><div class="quoted-text"><br><b>To:</b> Martin J. O'Riordan <<a href="mailto:Martin.ORiordan@movidius.com" target="_blank">Martin.ORiordan@movidius.com</a>><br></div><b>Cc:</b> Clang Dev <<a href="mailto:cfe-dev@lists.llvm.org" target="_blank">cfe-dev@lists.llvm.org</a>>; Tim Northover <<a href="mailto:t.p.northover@gmail.com" target="_blank">t.p.northover@gmail.com</a>><div class="elided-text"><br><b>Subject:</b> Re: [cfe-dev] 'struct' member alignment query<u></u><u></u></div><p></p><div class="elided-text"><p class="MsoNormal"><u></u> <u></u></p><div><p class="MsoNormal">The "right" way to get this effect is to use __attribute__((packed)) on the field.<u></u><u></u></p></div><div><p class="MsoNormal"><u></u> <u></u></p><div><p class="MsoNormal">On 9 Feb 2017 9:33 am, "Martin J. O'Riordan via cfe-dev" <<a href="mailto:cfe-dev@lists.llvm.org" target="_blank">cfe-dev@lists.llvm.org</a>> wrote:<u></u><u></u></p><blockquote style="border:none;border-left:solid #cccccc 1.0pt;padding:0cm 0cm 0cm 6.0pt;margin-left:4.8pt;margin-right:0cm"><p class="MsoNormal">Wow!  Thanks for the response, that worked perfectly.<br><br>I never would have expected that the indirect 'typedef' approach would behave any differently to the more direct annotation!  I feel that this is probably an unintended GCC behaviour/bug, though CLang is 100% right to maintain compatibility with GCC.<br><br>But it solved my problem, thanks very much,<br><br>        MartinO<u></u><u></u></p><div><p class="MsoNormal"><br>-----Original Message-----<br>From: Tim Northover [mailto:<a href="mailto:t.p.northover@gmail.com" target="_blank">t.p.northover@gmail.<wbr>com</a>]<br>Sent: 09 February 2017 15:08<br>To: Martin J. O'Riordan <<a href="mailto:Martin.ORiordan@movidius.com" target="_blank">Martin.ORiordan@movidius.com</a>><br>Cc: cfe-dev <<a href="mailto:cfe-dev@lists.llvm.org" target="_blank">cfe-dev@lists.llvm.org</a>><br>Subject: Re: [cfe-dev] 'struct' member alignment query<br><br>On 9 February 2017 at 02:56, Martin J. O'Riordan via cfe-dev <<a href="mailto:cfe-dev@lists.llvm.org" target="_blank">cfe-dev@lists.llvm.org</a>> wrote:<br>> Is this expected behaviour in 'clang', and if so, is there another way<br>> I can coerce the 'struct' member 'alignCheck2' onto another less<br>> constrained alignment boundary?<br><br>It's expected, but I think only because of GCC compatibility because it's really not obvious behaviour. The workaround, which still mystifies me, is to go via a typedef:<br><br>typedef AlignCheck_t __attribute__((aligned(1))) UnAlignCheck_t;<br><br>then you can put that in the struct and it'll be laid out without any padding.<br><br>Tim.<br><br>______________________________<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" target="_blank">http://lists.llvm.org/cgi-bin/<wbr>mailman/listinfo/cfe-dev</a><u></u><u></u></p></div></blockquote></div><p class="MsoNormal"><u></u> <u></u></p></div></div></div></div></blockquote></div><br></div></div>