public inbox for gcc-patches@gcc.gnu.org
 help / color / mirror / Atom feed
From: Richard Sandiford <richard.sandiford@arm.com>
To: gcc-patches@gcc.gnu.org
Cc: jakub@redhat.com
Subject: [PATCH] aarch64: Don't trust TYPE_ALIGN for pointers [PR108910]
Date: Thu, 13 Apr 2023 17:00:05 +0100	[thread overview]
Message-ID: <mpta5zbhiwq.fsf@arm.com> (raw)

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.

Tested on aarch64-linux-gnu & pushed to trunk so far.  Will backport
to GCC 12 soon.

Richard


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.
---
 gcc/config/aarch64/aarch64.cc           | 15 ++++++++++++++-
 gcc/testsuite/gcc.dg/torture/pr108910.c |  8 ++++++++
 2 files changed, 22 insertions(+), 1 deletion(-)
 create mode 100644 gcc/testsuite/gcc.dg/torture/pr108910.c

diff --git a/gcc/config/aarch64/aarch64.cc b/gcc/config/aarch64/aarch64.cc
index 42617ced73a..f4ef22ce02f 100644
--- a/gcc/config/aarch64/aarch64.cc
+++ b/gcc/config/aarch64/aarch64.cc
@@ -7484,7 +7484,20 @@ 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;
+	}
+      gcc_assert (!TYPE_USER_ALIGN (type));
+      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
index 00000000000..59735488c2e
--- /dev/null
+++ b/gcc/testsuite/gcc.dg/torture/pr108910.c
@@ -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);
+}
-- 
2.25.1


                 reply	other threads:[~2023-04-13 16:00 UTC|newest]

Thread overview: [no followups] expand[flat|nested]  mbox.gz  Atom feed

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=mpta5zbhiwq.fsf@arm.com \
    --to=richard.sandiford@arm.com \
    --cc=gcc-patches@gcc.gnu.org \
    --cc=jakub@redhat.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).