[cfe-dev] [RFC] Use preferred alignment instead of ABI alignment for complete object when applicable

David Rector via cfe-dev cfe-dev at lists.llvm.org
Wed Aug 19 17:53:01 PDT 2020


For that matter "preferred alignment" is also confusing.  Who prefers it?  When might that preference not be met?

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:
The "ABI" alignment of a type = the ABI-guaranteed minimum alignment of any object of that type built by any standard-conformant compiler.
The "preferred" alignment of a type = the actual alignment, at least as large as the ABI alignment, that our compiler will always use in constructing an object of that type.
Therefore, whenever we are dealing with an object we know our compiler has constructed, we can use the "preferred" alignment.
And thereby take advantage of target-specific optimizations like AIX.
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.
`-And thereby miss out on AIX.
Whenever a type has alignas(N), 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.
alignof(T) / __alignof(T) must return the "ABI" and "preferred" alignments of T, respectively.
And here are Xiangling’s responses/new questions reproduced again, to get back to the specifics:

> 1. Given a pointer to some object of type T or a field of type T inside another object, what can I know for sure about the alignment of that pointer? (How many of the low bits are absolutely guaranteed to be 0, assuming the program doesn't involve Undefined Behavior?)
>  
> “what can I know for sure about the alignment of that pointer” I assume you mean “the alignment of the pointee”. So this means we should always use ABI alignment for the pointee, because it’s an unknown object for the compiler right? 
>  
> Besides that, any other scenarios you are aware of that we should always use ABI alignment?
>  
> 2. If I want to allocate a brand new complete object of type T, at what alignment should I use to place it? This must be at least the alignment from 1. But beyond that, it could differ by circumstance, e.g. stack, global, and function parameter allocations could make different choices. In some cases, increasing the alignment of an allocation above the required minimum may just be an optimization, but in other cases it has an ABI impact.
>  
> Does this mean when we “allocate a brand new complete object of type T”, we will prioritize/take “PreferredAlignment”?
>  
>  
> Regarding X86-32, can you show me an example of how X86-32 makes the choice between PreferredAlignment and ABI alignment for “double” type? 
>  
> Thank you,
> Xiangling



> On Aug 19, 2020, at 6:07 PM, James Y Knight via cfe-dev <cfe-dev at lists.llvm.org> wrote:
> 
> On Wed, Aug 19, 2020 at 5:57 PM Richard Smith <richard at metafoo.co.uk <mailto:richard at metafoo.co.uk>> wrote:
> On Wed, 19 Aug 2020 at 12:16, James Y Knight via cfe-dev <cfe-dev at lists.llvm.org <mailto:cfe-dev at lists.llvm.org>> wrote:
> I wasn't talking about the alignment of a pointer, but rather the alignment of a value of type 'double', which has a preferred alignment of 8.
> 
> Not all double values in memory are 8 byte aligned -- some are placed only at 4 byte aligned offsets. Therefore, the compiler can only assume an alignment of 4 for an unknown object of type 'double' -- this is the abi alignment.
> 
> It seems to me that the term "ABI alignment" is actively confusing matters here -- both alignment values are part of the ABI in the AIX case. Perhaps "minimum alignment" would be a better term for the alignment that all objects of the type are guaranteed to have?
> 
> I agree that "ABI alignment" is a poor name. I like calling it "guaranteed type alignment".
> _______________________________________________
> cfe-dev mailing list
> cfe-dev at lists.llvm.org
> https://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/20200819/50c26d1d/attachment.html>


More information about the cfe-dev mailing list