public inbox for gcc-patches@gcc.gnu.org
 help / color / mirror / Atom feed
From: Lulu Cheng <chenglulu@loongson.cn>
To: gcc-patches@gcc.gnu.org
Cc: xry111@xry111.site, i@xen0n.name, xuchenghua@loongson.cn,
	Lulu Cheng <chenglulu@loongson.cn>
Subject: [PATCH] LoongArch: Fix the problem of structure parameter passing in C++. This structure has empty structure members and less than three floating point members.
Date: Wed, 24 May 2023 14:04:08 +0800	[thread overview]
Message-ID: <20230524060407.19181-1-chenglulu@loongson.cn> (raw)

An empty struct type that is not non-trivial for the purposes of calls
will be treated as though it were the following C type:

struct {
  char c;
};

Before this patch was added, a structure parameter containing an empty structure and
less than three floating-point members was passed through one or two floating-point
registers, while nested empty structures are ignored. Which did not conform to the
calling convention.

After adding this patch, empty structs will always be passed.

gcc/ChangeLog:

	* config/loongarch/loongarch.cc (loongarch_flatten_aggregate_field):
	Mark out nested empty structs.
	(loongarch_pass_aggregate_in_fpr_and_gpr_p): If an empty structure exists,
	increment the number of fixed-point registers required.

gcc/testsuite/ChangeLog:

	* g++.target/loongarch/empty1.C: New test.
	* g++.target/loongarch/empty2.C: New test.
	* g++.target/loongarch/empty3.C: New test.
---
 gcc/config/loongarch/loongarch.cc           | 13 +++++++++++++
 gcc/testsuite/g++.target/loongarch/empty1.C | 18 ++++++++++++++++++
 gcc/testsuite/g++.target/loongarch/empty2.C | 20 ++++++++++++++++++++
 gcc/testsuite/g++.target/loongarch/empty3.C | 19 +++++++++++++++++++
 4 files changed, 70 insertions(+)
 create mode 100644 gcc/testsuite/g++.target/loongarch/empty1.C
 create mode 100644 gcc/testsuite/g++.target/loongarch/empty2.C
 create mode 100644 gcc/testsuite/g++.target/loongarch/empty3.C

diff --git a/gcc/config/loongarch/loongarch.cc b/gcc/config/loongarch/loongarch.cc
index 7f4e0e59573..77506410501 100644
--- a/gcc/config/loongarch/loongarch.cc
+++ b/gcc/config/loongarch/loongarch.cc
@@ -321,6 +321,18 @@ loongarch_flatten_aggregate_field (const_tree type,
 	  || !tree_fits_uhwi_p (TYPE_SIZE (type)))
 	return -1;
 
+      if (default_is_empty_record (type))
+	{
+	  if (n < 2)
+	    {
+	      fields[n].type = type;
+	      fields[n].offset = offset;
+	      return n + 1;
+	    }
+	  else
+	    return -1;
+	}
+
       for (tree f = TYPE_FIELDS (type); f; f = DECL_CHAIN (f))
 	if (TREE_CODE (f) == FIELD_DECL)
 	  {
@@ -458,6 +470,7 @@ loongarch_pass_aggregate_in_fpr_and_gpr_p (const_tree type,
     {
       num_float += SCALAR_FLOAT_TYPE_P (fields[i].type);
       num_int += INTEGRAL_TYPE_P (fields[i].type);
+      num_int += (TREE_CODE (fields[i].type) == RECORD_TYPE);
     }
 
   return num_int == 1 && num_float == 1;
diff --git a/gcc/testsuite/g++.target/loongarch/empty1.C b/gcc/testsuite/g++.target/loongarch/empty1.C
new file mode 100644
index 00000000000..059e6774158
--- /dev/null
+++ b/gcc/testsuite/g++.target/loongarch/empty1.C
@@ -0,0 +1,18 @@
+/* { dg-do compile } */
+/* { dg-options "-O2 -mabi=lp64d" } */
+/* { dg-final { scan-assembler-times "ld.bu\t\\\$r4,\\\$r12,0" 1 } } */
+
+struct E {};
+struct s1
+{
+  struct E {} e1;
+  float f0;
+};
+
+extern void fun (struct s1);
+
+struct s1 s1_1 = {{}, 1.0};
+void test (void)
+{
+  fun (s1_1);
+}
diff --git a/gcc/testsuite/g++.target/loongarch/empty2.C b/gcc/testsuite/g++.target/loongarch/empty2.C
new file mode 100644
index 00000000000..abf01de751a
--- /dev/null
+++ b/gcc/testsuite/g++.target/loongarch/empty2.C
@@ -0,0 +1,20 @@
+/* { dg-do compile } */
+/* { dg-options "-O2 -mabi=lp64d" } */
+/* { dg-final { scan-assembler-not "fld.s" } } */
+/* { dg-final { scan-assembler-not "fld.d" } } */
+
+struct E {};
+struct s1
+{
+  struct E {} e1;
+  float f0;
+  double f1;
+};
+
+extern void fun (struct s1);
+
+struct s1 s1_1 = {{}, 1.0, 2.0};
+void test (void)
+{
+  fun (s1_1);
+}
diff --git a/gcc/testsuite/g++.target/loongarch/empty3.C b/gcc/testsuite/g++.target/loongarch/empty3.C
new file mode 100644
index 00000000000..8c40963238b
--- /dev/null
+++ b/gcc/testsuite/g++.target/loongarch/empty3.C
@@ -0,0 +1,19 @@
+/* { dg-do compile } */
+/* { dg-options "-O2 -mabi=lp64d" } */
+/* { dg-final { scan-assembler-not "fld.d" } } */
+
+struct E {};
+struct s1
+{
+  struct E {} e1;
+  double f0;
+  double f1;
+};
+
+extern void fun (struct s1);
+
+struct s1 s1_1 = {{}, 1.0, 2.0};
+void test (void)
+{
+  fun (s1_1);
+}
-- 
2.31.1


             reply	other threads:[~2023-05-24  6:04 UTC|newest]

Thread overview: 13+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2023-05-24  6:04 Lulu Cheng [this message]
2023-05-24  6:45 ` Xi Ruoyao
2023-05-24  8:41   ` Xi Ruoyao
2023-05-24  8:59     ` Jonathan Wakely
2023-05-24 20:15       ` Jason Merrill
2023-05-25  2:46         ` Lulu Cheng
2023-05-25  2:52           ` WANG Xuerui
2023-05-25  3:41             ` Lulu Cheng
2023-05-25  8:23         ` Jonathan Wakely
2023-05-24  8:47   ` Lulu Cheng
2023-05-24  9:25     ` Xi Ruoyao
2023-05-24 10:07       ` Lulu Cheng
2023-05-24 14:55         ` Xi Ruoyao

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=20230524060407.19181-1-chenglulu@loongson.cn \
    --to=chenglulu@loongson.cn \
    --cc=gcc-patches@gcc.gnu.org \
    --cc=i@xen0n.name \
    --cc=xry111@xry111.site \
    --cc=xuchenghua@loongson.cn \
    /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).