]> git.ipfire.org Git - thirdparty/gcc.git/commitdiff
aarch64: Don't trust TYPE_ALIGN for pointers [PR108910]
authorRichard Sandiford <richard.sandiford@arm.com>
Mon, 17 Apr 2023 13:41:00 +0000 (14:41 +0100)
committerRichard Sandiford <richard.sandiford@arm.com>
Mon, 17 Apr 2023 13:41:00 +0000 (14:41 +0100)
The aarch64 PCS rules ignore user alignment for scalars and
vectors and use the "natural" alignment of the type.  GCC tried
to calculate that natural alignment using:

  TYPE_ALIGN (TYPE_MAIN_VARIANT (type))

But as discussed in the PR, it's possible that the main variant
of a pointer type is an overaligned type (although that's usually
accidental).

This isn't known to be a problem for other types, so this patch
changes the bare minimum.  It might be that we need to ignore
TYPE_ALIGN in other cases too.

gcc/
PR target/108910
* config/aarch64/aarch64.cc (aarch64_function_arg_alignment): Do
not trust TYPE_ALIGN for pointer types; use POINTER_SIZE instead.

gcc/testsuite/
PR target/108910
* gcc.dg/torture/pr108910.c: New test.

(cherry picked from commit 66946624b96b762985de56444d726a0ebd4e0df5)

gcc/config/aarch64/aarch64.cc
gcc/testsuite/gcc.dg/torture/pr108910.c [new file with mode: 0644]

index ce3dcdcd754ce327094af2b30ae1b766f50240c4..465cd97818268e71542d86b7d3cdf40ad4b4eac3 100644 (file)
@@ -7319,7 +7319,19 @@ aarch64_function_arg_alignment (machine_mode mode, const_tree type,
   gcc_assert (TYPE_MODE (type) == mode);
 
   if (!AGGREGATE_TYPE_P (type))
-    return TYPE_ALIGN (TYPE_MAIN_VARIANT (type));
+    {
+      /* The ABI alignment is the natural alignment of the type, without
+        any attributes applied.  Normally this is the alignment of the
+        TYPE_MAIN_VARIANT, but not always; see PR108910 for a counterexample.
+        For now we just handle the known exceptions explicitly.  */
+      type = TYPE_MAIN_VARIANT (type);
+      if (POINTER_TYPE_P (type))
+       {
+         gcc_assert (known_eq (POINTER_SIZE, GET_MODE_BITSIZE (mode)));
+         return POINTER_SIZE;
+       }
+      return TYPE_ALIGN (type);
+    }
 
   if (TREE_CODE (type) == ARRAY_TYPE)
     return TYPE_ALIGN (TREE_TYPE (type));
diff --git a/gcc/testsuite/gcc.dg/torture/pr108910.c b/gcc/testsuite/gcc.dg/torture/pr108910.c
new file mode 100644 (file)
index 0000000..5973548
--- /dev/null
@@ -0,0 +1,8 @@
+extern void foo (float, float *, float *);
+
+void
+bar (void *p)
+{
+  float *__attribute__((aligned (64))) q = __builtin_assume_aligned (p, 64);
+  foo (0.0f, q, q);
+}