[Issue 11883] New: [feature request] align attribute without value defaults to largest supported alignment.

d-bugmail at puremagic.com d-bugmail at puremagic.com
Wed Jan 8 08:55:23 PST 2014


https://d.puremagic.com/issues/show_bug.cgi?id=11883

           Summary: [feature request] align attribute without value
                    defaults to largest supported alignment.
           Product: D
           Version: D2
          Platform: All
        OS/Version: All
            Status: NEW
          Severity: normal
          Priority: P2
         Component: DMD
        AssignedTo: nobody at puremagic.com
        ReportedBy: ibuclaw at ubuntu.com


--- Comment #0 from Iain Buclaw <ibuclaw at ubuntu.com> 2014-01-08 08:55:15 PST ---
Currently there are only two behaviours with align in struct fields/decls.

// Foo.bar.alignsize = STRUCTALIGN_DEFAULT
// Foo.sizeof = 6
struct Foo { short[3] bar; }

// Foo.bar.alignsize = 8
// Foo.sizeof = 8
struct Foo { align(8) short[3] bar; }

// Foo.bar.alignsize = STRUCTALIGN_DEFAULT
// Foo.sizeof = 6
struct Foo { align short[3] bar; }


This means that 'align' without a alignment value is redundant, so I propose an
change to give it a more meaningful (and useful) semantic.  That is to tell the
compiler to align a type to the maximum useful alignment for the target machine
you are compiling for.  This matches the behaviour of gcc
__attribute__((aligned)).


---
Whenever you leave out the alignment factor in an 'align' attribute
specification, the compiler automatically sets the alignment for the type to
the largest alignment that is ever used for any data type on the target machine
you are compiling for. Doing this can often make copy operations more
efficient, because the compiler can use whatever instructions copy the biggest
chunks of memory when performing copies to or from the variables that have
types that you have aligned this way.

Note that although you can ask the compiler to select a time-efficient
alignment for a given type and then declare only individual stand-alone objects
of that type, the compiler's ability to select a time-efficient alignment is
primarily useful only when you plan to create arrays of variables having the
relevant (efficiently aligned) type. If you declare or use arrays of variables
of an efficiently-aligned type, then it is likely that your program also does
pointer arithmetic (or subscripting, which amounts to the same thing) on
pointers to the relevant type, and the code that the compiler generates for
these pointer arithmetic operations is often more efficient for
efficiently-aligned types than for other types.
--


In the example above, the 'align' would have this effect on the struct:

// Foo.bar.alignsize = ALIGNATTR_DEFAULT
// Foo.sizeof = 8
struct Foo { align short[3] bar; }

Without 'align' the size of the entire struct type is 6 bytes. The smallest
power of two that is greater than or equal to that is 8, so the compiler sets
the alignment for the entire struct type to 8 bytes.

-- 
Configure issuemail: https://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------


More information about the Digitalmars-d-bugs mailing list