public inbox for gcc-patches@gcc.gnu.org
 help / color / mirror / Atom feed
From: Ramana Radhakrishnan <ramana.gcc@googlemail.com>
To: Richard Earnshaw <Richard.Earnshaw@foss.arm.com>
Cc: gcc-patches <gcc-patches@gcc.gnu.org>,
	Richard Earnshaw <Richard.Earnshaw@arm.com>,
	 Alex Coplan <alex.coplan@arm.com>
Subject: Re: [Patch Arm] Fix PR 92999
Date: Fri, 11 Nov 2022 21:50:21 +0000	[thread overview]
Message-ID: <CAJA7tRYadtzpCG8ehT5DVvHR9_BPFh7ZURwftwdqzYhHS6XLtw@mail.gmail.com> (raw)
In-Reply-To: <CAJA7tRbpaQGt6D_=oF+VA_uuJMPgkLf6OmvCUJNJEp-TsSWLZg@mail.gmail.com>

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

On Thu, Nov 10, 2022 at 7:46 PM Ramana Radhakrishnan
<ramana.gcc@googlemail.com> wrote:
>
> On Thu, Nov 10, 2022 at 6:03 PM Richard Earnshaw
> <Richard.Earnshaw@foss.arm.com> wrote:
> >
> >
> >
> > On 10/11/2022 17:21, Richard Earnshaw via Gcc-patches wrote:
> > >
> > >
> > > On 08/11/2022 18:20, Ramana Radhakrishnan via Gcc-patches wrote:
> > >> PR92999 is a case where the VFP calling convention does not allocate
> > >> enough FP registers for a homogenous aggregate containing FP16 values.
> > >> I believe this is the complete fix but would appreciate another set of
> > >> eyes on this.
> > >>
> > >> Could I get a hand with a regression test run on an armhf environment
> > >> while I fix my environment ?
> > >>
> > >> gcc/ChangeLog:
> > >>
> > >> PR target/92999
> > >> *  config/arm/arm.c (aapcs_vfp_allocate_return_reg): Adjust to handle
> > >> aggregates with elements smaller than SFmode.
> > >>
> > >> gcc/testsuite/ChangeLog:
> > >>
> > >> * gcc.target/arm/pr92999.c: New test.
> > >>
> > >>
> > >> Thanks,
> > >> Ramana
> > >>
> > >> Signed-off-by: Ramana Radhakrishnan <ramana.gcc@gmail.com>
> > >
> > > I'm not sure about this.  The AAPCS does not mention a base type of a
> > > half-precision FP type as an appropriate homogeneous aggregate for using
> > > VFP registers for either calling or returning.
>
> Ooh interesting, thanks for taking a look and poking at the AAPCS and
> that's a good catch. BF16 should also have the same behaviour as FP16
> , I suspect ?

I suspect I got caught out by the definition of the Homogenous
aggregate from Section 5.3.5
((https://github.com/ARM-software/abi-aa/blob/2982a9f3b512a5bfdc9e3fea5d3b298f9165c36b/aapcs32/aapcs32.rst#homogeneous-aggregates)
which simply suggests it's an aggregate of fundamental types which
lists half precision floating point .

FTR, ideally I should have read 7.1.2.1
https://github.com/ARM-software/abi-aa/blob/2982a9f3b512a5bfdc9e3fea5d3b298f9165c36b/aapcs32/aapcs32.rst#procedure-calling)
:)



>
> > >
> > > So perhaps the bug is that we try to treat this as a homogeneous
> > > aggregate at all.
>
> Yep I agree - I'll take a look again tomorrow and see if I can get a fix.
>
> (And thanks Alex for the test run, I might trouble you again while I
> still (slowly) get some of my boards back up)


and as promised take 2. I'd really prefer another review on this one
to see if I've not missed anything in the cases below.

regards
Ramana


>
> regards,
> Ramana
>
>
> >
> > R.

[-- Attachment #2: p1.txt --]
[-- Type: text/plain, Size: 4142 bytes --]

commit c2ed018d10328c5cf93aa56b00ba4caf5dace539
Author: Ramana Radhakrishnan <ramana.gcc@gmail.com>
Date:   Fri Nov 11 21:39:22 2022 +0000

    [Patch Arm] Fix PR92999
    
    PR target/92999 is a case where the VFP PCS implementation is
    incorrectly considering homogenous floating point aggregates with FP16
    and BF16 values.
    
    Can someone help me with a bootstrap and regression test on an armhf
    environment ?
    
    Signed-off-by: Ramana Radhakrishnan <ramana.gcc@gmail.com>
    Tested-by: Alex Coplan  <alex.coplan@arm.com>
    Reviewed-by: Richard Earnshaw  <rearnsha@arm.com>
    
            PR target/92999
    
    gcc/ChangeLog:
    
            * config/arm/arm.cc (aapcs_vfp_is_invalid_scalar_in_ha): New
            (aapcs_vfp_sub_candidate): Adjust.
    
    gcc/testsuite/ChangeLog:
    
            * gcc.target/arm/pr92999.c: New test.

diff --git a/gcc/config/arm/arm.cc b/gcc/config/arm/arm.cc
index 2eb4d51e4a3..cd3e1ffe777 100644
--- a/gcc/config/arm/arm.cc
+++ b/gcc/config/arm/arm.cc
@@ -6281,6 +6281,31 @@ const unsigned int WARN_PSABI_EMPTY_CXX17_BASE = 1U << 0;
 const unsigned int WARN_PSABI_NO_UNIQUE_ADDRESS = 1U << 1;
 const unsigned int WARN_PSABI_ZERO_WIDTH_BITFIELD = 1U << 2;
 
+
+/* The AAPCS VFP ABI allows homogenous aggregates with scalar
+   FP32 and FP64 members.
+   Return
+     true if this is a scalar that is not a proper candidate
+     false if this is a scalar that is an acceptable scalar data
+     type in a homogenous aggregate or if this is not a scalar allowing
+     the tree walk in aapcs_vfp_sub_candidate to continue.
+ */
+static bool
+aapcs_vfp_is_invalid_scalar_in_ha (const_tree inner_type)
+{
+
+  machine_mode mode = TYPE_MODE (inner_type);
+  if (TREE_CODE (inner_type) == REAL_TYPE)
+    {
+      if (mode == DFmode && mode == SFmode)
+	return false;
+      else
+	return true;
+    }
+  else
+    return false;
+}
+
 /* Walk down the type tree of TYPE counting consecutive base elements.
    If *MODEP is VOIDmode, then set it to the first valid floating point
    type.  If a non-floating point type is found, or if a floating point
@@ -6372,6 +6397,10 @@ aapcs_vfp_sub_candidate (const_tree type, machine_mode *modep,
 	    || TREE_CODE (TYPE_SIZE (type)) != INTEGER_CST)
 	  return -1;
 
+	/* We ignore HFA's of FP16 and BF16.  */
+	if (aapcs_vfp_is_invalid_scalar_in_ha (TREE_TYPE (type)))
+	  return -1;
+
 	count = aapcs_vfp_sub_candidate (TREE_TYPE (type), modep,
 					 warn_psabi_flags);
 	if (count == -1
@@ -6455,6 +6484,10 @@ aapcs_vfp_sub_candidate (const_tree type, machine_mode *modep,
 		  }
 	      }
 
+	    /* We ignore HA's of FP16 and BF16.  */
+	    if (aapcs_vfp_is_invalid_scalar_in_ha (TREE_TYPE (field)))
+	      return -1;
+
 	    sub_count = aapcs_vfp_sub_candidate (TREE_TYPE (field), modep,
 						 warn_psabi_flags);
 	    if (sub_count < 0)
@@ -6489,6 +6522,10 @@ aapcs_vfp_sub_candidate (const_tree type, machine_mode *modep,
 	    if (TREE_CODE (field) != FIELD_DECL)
 	      continue;
 
+	    /* We ignore HA's of FP16 and BF16.  */
+	    if (aapcs_vfp_is_invalid_scalar_in_ha (TREE_TYPE (field)))
+		return -1;
+
 	    sub_count = aapcs_vfp_sub_candidate (TREE_TYPE (field), modep,
 						 warn_psabi_flags);
 	    if (sub_count < 0)
diff --git a/gcc/testsuite/gcc.target/arm/pr92999.c b/gcc/testsuite/gcc.target/arm/pr92999.c
new file mode 100644
index 00000000000..faa21fdb7d2
--- /dev/null
+++ b/gcc/testsuite/gcc.target/arm/pr92999.c
@@ -0,0 +1,31 @@
+/* { dg-do run } */
+/* { dg-options "-mfp16-format=ieee" } */
+
+//
+// Compile with gcc -mfp16-format=ieee
+// Any optimization level is fine.
+//
+// Correct output should be
+// "y.first = 1, y.second = -99"
+//
+// Buggy output is
+// "y.first = -99, y.second = -99"
+//
+#include <stdlib.h>
+struct phalf {
+    __fp16 first;
+    __fp16 second;
+};
+
+struct phalf phalf_copy(struct phalf* src) __attribute__((noinline));
+struct phalf phalf_copy(struct phalf* src) {
+    return *src;
+}
+
+int main() {
+    struct phalf x = { 1.0, -99.0};
+    struct phalf y = phalf_copy(&x);
+    if (y.first != 1.0 && y.second != -99.0)
+	abort();
+    return 0;
+}

  reply	other threads:[~2022-11-11 21:50 UTC|newest]

Thread overview: 9+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2022-11-08 18:20 Ramana Radhakrishnan
2022-11-09 18:42 ` Alex Coplan
2022-11-10 17:21 ` Richard Earnshaw
2022-11-10 18:03   ` Richard Earnshaw
2022-11-10 19:46     ` Ramana Radhakrishnan
2022-11-11 21:50       ` Ramana Radhakrishnan [this message]
2022-11-17 20:15         ` Ramana Radhakrishnan
2022-11-24 12:32           ` Ramana Radhakrishnan
2022-11-24 16:12         ` Richard Earnshaw

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=CAJA7tRYadtzpCG8ehT5DVvHR9_BPFh7ZURwftwdqzYhHS6XLtw@mail.gmail.com \
    --to=ramana.gcc@googlemail.com \
    --cc=Richard.Earnshaw@arm.com \
    --cc=Richard.Earnshaw@foss.arm.com \
    --cc=alex.coplan@arm.com \
    --cc=gcc-patches@gcc.gnu.org \
    /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).