https://gcc.gnu.org/bugzilla/show_bug.cgi?id=43798

--- Comment #10 from Jakub Jelinek <jakub at gcc dot gnu.org> ---
We can't pad, that is against the ABI.
I think we've discussed this many times in the past, the fact is that people do
use this kind of mess when they want to say that the whole array has certain
alignment or when they are just careless, I think it is too late now for GCC9
to start rejecting it without an analysis how much code would be affected, not
to mention that it is not backportable.

The following patch does the adjustment and fixes the testcase:
--- gcc/stor-layout.c.jj        2019-01-01 12:37:17.296972670 +0100
+++ gcc/stor-layout.c   2019-03-20 09:56:15.576407411 +0100
@@ -2550,10 +2550,27 @@ layout_type (tree type)
            /* If TYPE_SIZE_UNIT overflowed, then it is certainly larger than
               TYPE_ALIGN_UNIT.  */
            && !TREE_OVERFLOW (TYPE_SIZE_UNIT (element))
-           && !integer_zerop (TYPE_SIZE_UNIT (element))
-           && compare_tree_int (TYPE_SIZE_UNIT (element),
-                                TYPE_ALIGN_UNIT (element)) < 0)
-         error ("alignment of array elements is greater than element size");
+           && !integer_zerop (TYPE_SIZE_UNIT (element)))
+         {
+           if (compare_tree_int (TYPE_SIZE_UNIT (element),
+                                 TYPE_ALIGN_UNIT (element)) < 0)
+             error ("alignment of array elements is greater than "
+                    "element size");
+           if (TYPE_ALIGN_UNIT (element))
+             {
+               unsigned align = (TREE_INT_CST_LOW (TYPE_SIZE_UNIT (element))
+                                 & (TYPE_ALIGN_UNIT (element) - 1));
+               align = least_bit_hwi (align);
+               if (align && align != TYPE_ALIGN_UNIT (element))
+                 {
+                   element = build_variant_type_copy (element);
+                   SET_TYPE_ALIGN (element, align * BITS_PER_UNIT);
+                   TYPE_USER_ALIGN (element) = 1;
+                   TREE_TYPE (type) = element;
+                   TYPE_USER_ALIGN (type) = 1;
+                 }
+             }
+         }
        break;
       }

but I haven't done any testing on it beyond that.
Perhaps for GCC 9 we could warn when we do this and depending on the amount of
warnings consider rejecting it later on if it doesn't affect significant amount
of code?

Reply via email to