> This program should generate an error; it's illogical.  If the alignment 
> of the element is greater than the element size, then arrays of such a 
> type should be disallowed.  Otherwise, stuff in either the compiler or 
> the program itself could make the justified assumption that things of 
> that type are aligned more strictly than they actually are.
> 
> -- 
> Mark Mitchell

Interesting, I have created a patch (attached) that gives an error
whenever we try to create an array of elements and the alignment of the
elements is greater than the size of the elements.

The problem I have, and the reason I haven't sent it to gcc-patches, is
that it generates a bunch of regressions.  The regressions are all due
to bad tests but I am not sure how to fix the tests so that I can check
in the patch.

The regressions are in two places, gcc.dg/compat/struct-layout* and
gcc.dg/vect/*

Most of the gcc.dg/vect/* tests contain something like:

        typedef float afloat __attribute__ ((__aligned__(16)));
        afloat a[N];

The question is, since this is illegal, what should we use instead?
I don't know if the alignment is an integral part of what is being
tested or not since the tests have no comments in them. So I am not
sure if we should just delete the alignment attribute or make it
smaller.  If we make it smaller we need to know the size of float in
order to know if a particular alignment is legal or not.

The gcc.dg/compat/struct-layout problems seem to stem from
struct-layout-1_generate.c.  In generate_fields() it generates random
types, some of these are arrays of some base type.  Then based on
another random number we might add an attribute like alignment.  There
is no check to ensure that the alignment of the base type is less than or
equal to the size of the base type in those instances where we are
creating an array.

I would be interested in any advice on the best way to fix these tests
so that I can add my patch without causing regressions.

Steve Ellcey
[EMAIL PROTECTED]




Here is the patch that checks for the alignment of array elements and that
causes the regressions:


2005-03-15  Steve Ellcey  <[EMAIL PROTECTED]>

        PR 19893
        * stor-layout.c (layout_type): Add alignment check.


*** gcc.orig/gcc/stor-layout.c  Fri Mar 11 14:40:03 2005
--- gcc/gcc/stor-layout.c       Tue Mar 15 15:46:02 2005
*************** layout_type (tree type)
*** 1632,1637 ****
--- 1632,1643 ----
  
        build_pointer_type (element);
  
+       if (host_integerp (TYPE_SIZE_UNIT (element), 1)
+             && tree_low_cst (TYPE_SIZE_UNIT (element), 1) > 0
+             && (HOST_WIDE_INT) TYPE_ALIGN_UNIT (element)
+              > tree_low_cst (TYPE_SIZE_UNIT (element), 1))
+         error ("alignment of array elements is greater than element size");
+ 
        /* We need to know both bounds in order to compute the size.  */
        if (index && TYPE_MAX_VALUE (index) && TYPE_MIN_VALUE (index)
            && TYPE_SIZE (element))

Reply via email to