<div dir="ltr"><div dir="ltr"><br></div><br><div class="gmail_quote"><div dir="ltr" class="gmail_attr">On Wed, Aug 19, 2020 at 8:53 PM David Rector <<a href="mailto:davrecthreads@gmail.com">davrecthreads@gmail.com</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 style="overflow-wrap: break-word;"><div style="margin:0px;font-stretch:normal;line-height:normal">For that matter "preferred alignment" is also confusing.  Who prefers it?  When might that preference not be met?</div><div style="margin:0px;font-stretch:normal;line-height:normal;min-height:14px"><br></div><div style="margin:0px;font-stretch:normal;line-height:normal">Here is my full understanding so far, expressed in the hope that pointing out my confusions here might better help answer Xiangling’s questions below:</div>
<ul>
<li style="margin:0px;font-stretch:normal;line-height:normal"> The "ABI" alignment of a type = the ABI-guaranteed minimum alignment of any object of that type built by any standard-conformant compiler.</li></ul></div></blockquote><div>Correct, this is the guaranteed alignment of any(***) object of that type (either top-level complete object or a subobject).</div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex"><div style="overflow-wrap: break-word;"><ul>
<li style="margin:0px;font-stretch:normal;line-height:normal">The "preferred" alignment of a type = the actual alignment, at least as large as the ABI alignment, that <i>our</i> compiler will <i>always</i> use in constructing an object of that type.</li></ul></div></blockquote><div>When constructing a global or local variable, yes, we do. </div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex"><div style="overflow-wrap: break-word;"><ul><li style="margin:0px;font-stretch:normal;line-height:normal">Therefore, whenever we are dealing with an object we know <i>our</i> compiler has constructed, we can use the "preferred" alignment.</li><ul><li style="margin:0px;font-stretch:normal;line-height:normal">And thereby take advantage of target-specific optimizations like AIX.</li></ul>
<li style="margin:0px;font-stretch:normal;line-height:normal">However, whenever we are dealing with e.g. a pointer to an object we are not sure we built — i.e. which may have been constructed within a function not compiled by our compiler (e.g. a function compiled without AIX in some library might have constructed that object and passed a pointer to it to some function our compiler is presently building) -- we are forced to use only the ABI-guaranteed minimum alignment.</li><ul><li style="margin:0px;font-stretch:normal;line-height:normal">`-And thereby miss out on AIX.</li></ul></ul></div></blockquote><div>The most critical difference isn't really our compiler vs someone else's compiler. The "preferred" alignment is actually part of the ABI, too (yet another reason why "ABI alignment" is an awful name, and "guaranteed alignment" is better).</div><div><br></div><div>The preferred alignment applies only to creation of a full object, not, for example, to fields within a struct. E.g. on x86-32:</div><div>   struct Foo { int i; double d; }; </div><div>the double is placed at offset 4 within the struct, not 8. So, on x86-32, when we have a pointer than could be pointing to such an object, we must only assume the pointee has alignment of 4, not 8.</div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex"><div style="overflow-wrap: break-word;"><ul><li style="margin:0px;font-stretch:normal;line-height:normal">Whenever a type has <font face="Menlo">alignas(N)</font>, that alignment will be returned for…both the ABI and the preferred alignments?  It will override both?  Is that right?  I recall it overrides the preferred alignment in Xiangling’s implementation, at least, not sure about the ABI case.</li></ul></div></blockquote><div>Yes. Except that alignas can never reduce the alignment below the guaranteed alignment according to the C/C++ language rules, so in effect it can only override preferred alignment. (The nonstandard `__attribute__((aligned))` and  `__attribute__((packed))` can reduce alignment below the default abi alignment, and in that case, you do override both.)</div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex"><div style="overflow-wrap: break-word;"><ul><li style="margin:0px;font-stretch:normal;line-height:normal"><font face="Menlo">alignof(T)</font> / <font face="Menlo">__alignof(T)</font> must return the "ABI" and "preferred" alignments of <font face="Menlo">T</font>, respectively.</li></ul></div></blockquote><div>Yep.</div><div> </div><div>***: Of course, with the nonstandard packed/aligned attributes, you can create objects which violate these alignment guarantees. If you do so, you must be extremely careful to only access such an object through the appropriately attributed type, and not through a normal unadorned pointer.</div><div><br></div></div></div>