<div dir="ltr"><div dir="ltr">On Sat, Apr 25, 2020 at 10:01 PM Hubert Tong via cfe-dev <<a href="mailto:cfe-dev@lists.llvm.org">cfe-dev@lists.llvm.org</a>> wrote:<br></div><div class="gmail_quote"><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex"><div dir="ltr"><div class="gmail_quote"><div dir="ltr" class="gmail_attr">On Fri, Apr 24, 2020 at 3:55 PM Eli Friedman via cfe-dev <<a href="mailto:cfe-dev@lists.llvm.org" target="_blank">cfe-dev@lists.llvm.org</a>> wrote:<br></div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex">
<div lang="EN-US">
<div>
<p class="MsoNormal">Assuming I’m understanding the rule correctly, it doesn’t actually affect “alignment” of the type in the sense we would normally understand it. A struct with a double as the first member can be stored at an address with four-byte alignment.
The rule only involves inserting extra padding at the end of the struct. In particular, from the way you’re describing the rule, I think the special alignment rule isn’t supposed to affect the result of _Alignof. Does that seem right?</p></div></div></blockquote><div>The reference implementations on AIX indicate that the result of <span style="font-family:monospace">_Alignof</span> is affected.</div><div><br></div><div>C:<br></div><div><span style="font-family:monospace">typedef struct A { double x; int y; } A;<br>extern char x[_Alignof(A)];<br>extern char x[8];</span></div><div><br></div><div>C++:</div><div><span style="font-family:monospace">struct A {<br> A(const A &);<br> double x; int y;<br>};<br>struct B : A { int z; };<br><br>extern char x[sizeof(B)];<br>extern char x[16];<br><br>extern char y[alignof(A)];<br>extern char y[8];<br></span></div><div></div></div></div></blockquote><div><br></div><div>Unfortunately, this is invalid behavior. _Alignof/alignof must return the <i>required</i> alignment for a type. Given that per your previous example:</div><div><font face="monospace">struct C {</font></div><div><font face="monospace"> int n;</font></div><div><font face="monospace"> struct A a;</font></div><div><font face="monospace">};</font></div><div>would place the type A at an offset of 4, alignof(A) cannot correctly return greater than 4.</div><div><br></div><div>You also need to ensure that the alignment values that Clang specifies when creating memory operations in LLVM IR are correct, and not higher than they should be, which they will be, if you've set the alignment of the structure too high.</div><div><br></div><div>I believe the best option would be to modify the struct layout code to round up the size as Eli suggests, and then modify ASTContext::getPreferredTypeAlign so that variables of these types get an increased alignment. This function is also used to implement the non-standard GCC extension "__alignof__(T)", which unlike alignof/_Alignof, returns the "preferred" alignment of T, rather than the minimum alignment.</div><div><br></div><div>(BTW, the name "Preferred alignment" in this context is somewhat of a misnomer. It makes it sound like this "preferred" alignment is not part of the ABI. But it actually is, as implemented in clang today, because it's used as the assumed alignment of an extern (or common or comdat) global variable of that type, not just when defining a variable. I suspect Clang doesn't actually get this correct for some of the other non-i386 ABIs which have underaligned doubles.)</div><div><br></div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex"><div dir="ltr"><div class="gmail_quote"><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex"><div lang="EN-US">
<p class="MsoNormal">Given that, do you actually need to store anything in the RecordLayout at all?</p></div></blockquote><div>Does the new information change your analysis or it is still worthwhile to internally treat the alignment as the lower value (and add special code to introduce the padding-for-preferred-alignment)?<br></div><div> </div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex"><div lang="EN-US"><div>Also, probably worth checking the layout for a testcase like the following, to check the interaction between the extra padding and the nvsize:
<br>[ ... ]<br></div></div></blockquote><div>Thanks; done above. The padding does not occur within the nvsize. I guess you also mean that we should check this against the new implementation as well.<br></div></div></div>
_______________________________________________<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="https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-dev" rel="noreferrer" target="_blank">https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-dev</a><br>
</blockquote></div></div>