public inbox for gcc-patches@gcc.gnu.org
 help / color / mirror / Atom feed
From: Jovan Dmitrovic <Jovan.Dmitrovic@Syrmia.com>
To: YunQiang Su <wzssyqa@gmail.com>
Cc: "gcc-patches@gcc.gnu.org" <gcc-patches@gcc.gnu.org>,
	Djordje Todorovic <Djordje.Todorovic@syrmia.com>
Subject: Re: [PATCH v2] mips: Fix overaligned function arguments [PR109435]
Date: Tue, 27 Jun 2023 08:54:26 +0000	[thread overview]
Message-ID: <DB9PR03MB7163504135EEEE9011A4C8348F27A@DB9PR03MB7163.eurprd03.prod.outlook.com> (raw)
In-Reply-To: <CAKcpw6XRsqUGWz7WVmBXBossQ0cex1d7EnJa8YDbCW2F7o7oHQ@mail.gmail.com>

[-- Attachment #1: Type: text/plain, Size: 425 bytes --]

Hi,
I am sending a revised patch, now with different tests for N64/N32 and O32 ABIs.
For the O32 ABI, I've skipped the -O0 and -Os pipelines, considering there is a
difference between exact offsets for store instructions (the registers used remain
the same).

Skipping -flto isn't really necessary, so I've removed that part.

I've fixed the Changelog, hopefully I've corrected the mistakes I made.

Regards,
Jovan

[-- Warning: decoded text below may be mangled, UTF-8 assumed --]
[-- Attachment #2: 0001-mips-Fix-overaligned-function-arguments-PR109435.patch --]
[-- Type: text/x-patch; name="0001-mips-Fix-overaligned-function-arguments-PR109435.patch", Size: 4414 bytes --]

From 05e4ff4d2fbb91ea8040fb10d8d6a130ad24bba7 Mon Sep 17 00:00:00 2001
From: Jovan Dmitrovic <Jovan.Dmitrovic@Syrmia.com>
Date: Mon, 26 Jun 2023 17:00:20 +0200
Subject: [PATCH] mips: Fix overaligned function arguments [PR109435]
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

This patch changes alignment for typedef types when passed as
arguments, making the alignment equal to the alignment of
original (aliased) types.

This change makes it impossible for a typedef type to have
alignment that is less than its size.

2023-06-27  Jovan Dmitrović  <jovan.dmitrovic@syrmia.com>

gcc/ChangeLog:
        PR target/109435
	* config/mips/mips.cc (mips_function_arg_alignment): Returns
    the alignment of function argument. In case of typedef type,
    it returns the aligment of the aliased type.
	(mips_function_arg_boundary): Relocated calculation of the
    aligment of function arguments.

gcc/testsuite/ChangeLog:

	* gcc.target/mips/align-1-n64.c: New test.
	* gcc.target/mips/align-1-o32.c: New test.
---
 gcc/config/mips/mips.cc                     | 19 ++++++++++++++++++-
 gcc/testsuite/gcc.target/mips/align-1-n64.c | 19 +++++++++++++++++++
 gcc/testsuite/gcc.target/mips/align-1-o32.c | 20 ++++++++++++++++++++
 3 files changed, 57 insertions(+), 1 deletion(-)
 create mode 100644 gcc/testsuite/gcc.target/mips/align-1-n64.c
 create mode 100644 gcc/testsuite/gcc.target/mips/align-1-o32.c

diff --git a/gcc/config/mips/mips.cc b/gcc/config/mips/mips.cc
index c1d1691306e..20ba35f754c 100644
--- a/gcc/config/mips/mips.cc
+++ b/gcc/config/mips/mips.cc
@@ -6190,6 +6190,23 @@ mips_arg_partial_bytes (cumulative_args_t cum, const function_arg_info &arg)
   return info.stack_words > 0 ? info.reg_words * UNITS_PER_WORD : 0;
 }
 
+/* Given MODE and TYPE of a function argument, return the alignment in
+   bits.
+   In case of typedef, alignment of its original type is
+   used.  */
+
+static unsigned int
+mips_function_arg_alignment (machine_mode mode, const_tree type)
+{
+  if (!type)
+    return GET_MODE_ALIGNMENT (mode);
+
+  if (is_typedef_decl (TYPE_NAME (type)))
+    type = DECL_ORIGINAL_TYPE (TYPE_NAME (type));
+
+  return TYPE_ALIGN (type);
+}
+
 /* Implement TARGET_FUNCTION_ARG_BOUNDARY.  Every parameter gets at
    least PARM_BOUNDARY bits of alignment, but will be given anything up
    to STACK_BOUNDARY bits if the type requires it.  */
@@ -6198,8 +6215,8 @@ static unsigned int
 mips_function_arg_boundary (machine_mode mode, const_tree type)
 {
   unsigned int alignment;
+  alignment = mips_function_arg_alignment (mode, type);
 
-  alignment = type ? TYPE_ALIGN (type) : GET_MODE_ALIGNMENT (mode);
   if (alignment < PARM_BOUNDARY)
     alignment = PARM_BOUNDARY;
   if (alignment > STACK_BOUNDARY)
diff --git a/gcc/testsuite/gcc.target/mips/align-1-n64.c b/gcc/testsuite/gcc.target/mips/align-1-n64.c
new file mode 100644
index 00000000000..46e718d548d
--- /dev/null
+++ b/gcc/testsuite/gcc.target/mips/align-1-n64.c
@@ -0,0 +1,19 @@
+/* Check that typedef alignment does not affect passing of function
+   parameters for N64/N32 ABIs.  */
+/* { dg-do compile { target { "mips*-*-*" } } } */
+/* { dg-options "-mabi=64"  } */
+
+typedef struct ui8
+{
+  unsigned v[8];
+} uint8 __attribute__ ((aligned(64)));
+
+unsigned
+callee (int x, uint8 a)
+{
+  return a.v[0];
+}
+
+/* { dg-final { scan-assembler "\tsd\t\\\$5,0\\(\\\$\[0-9\]\\)" } } */
+/* { dg-final { scan-assembler "\tsd\t\\\$6,8\\(\\\$\[0-9\]\\)" } } */
+/* { dg-final { scan-assembler "\tsd\t\\\$7,16\\(\\\$\[0-9\]\\)" } } */
diff --git a/gcc/testsuite/gcc.target/mips/align-1-o32.c b/gcc/testsuite/gcc.target/mips/align-1-o32.c
new file mode 100644
index 00000000000..a548632b7f6
--- /dev/null
+++ b/gcc/testsuite/gcc.target/mips/align-1-o32.c
@@ -0,0 +1,20 @@
+/* Check that typedef alignment does not affect passing of function
+   parameters for O32 ABI.  */
+/* { dg-do compile { target { "mips*-*-*" } } } */
+/* { dg-options "-mabi=32"  } */
+/* { dg-skip-if "" { *-*-* } { "-O0" "-Os" } { "" } } */
+
+typedef struct ui8
+{
+  unsigned v[8];
+} uint8 __attribute__ ((aligned(64)));
+
+unsigned
+callee (int x, uint8 a)
+{
+  return a.v[0];
+}
+
+/* { dg-final { scan-assembler "\tsw\t\\\$5,100\\(\\\$sp\\)" } } */
+/* { dg-final { scan-assembler "\tsw\t\\\$6,104\\(\\\$sp\\)" } } */
+/* { dg-final { scan-assembler "\tsw\t\\\$7,108\\(\\\$sp\\)" } } */
-- 
2.34.1


  reply	other threads:[~2023-06-27  8:54 UTC|newest]

Thread overview: 10+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2023-05-29 10:32 [PATCH] " Jovan Dmitrovic
2023-05-31 10:05 ` YunQiang Su
2023-06-06 10:29   ` Jovan Dmitrovic
2023-06-06 10:50     ` YunQiang Su
2023-06-07 10:28       ` Jovan Dmitrovic
2023-06-16  8:07         ` YunQiang Su
2023-06-27  8:54           ` Jovan Dmitrovic [this message]
2023-06-29  6:04             ` [PATCH v2] " YunQiang Su
2023-06-29 10:04               ` YunQiang Su
2023-06-29 10:46                 ` Jovan Dmitrovic

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=DB9PR03MB7163504135EEEE9011A4C8348F27A@DB9PR03MB7163.eurprd03.prod.outlook.com \
    --to=jovan.dmitrovic@syrmia.com \
    --cc=Djordje.Todorovic@syrmia.com \
    --cc=gcc-patches@gcc.gnu.org \
    --cc=wzssyqa@gmail.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).