On Thu, Nov 10, 2022 at 7:46 PM Ramana Radhakrishnan <ramana....@googlemail.com> wrote: > > On Thu, Nov 10, 2022 at 6:03 PM Richard Earnshaw > <richard.earns...@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....@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.
commit c2ed018d10328c5cf93aa56b00ba4caf5dace539 Author: Ramana Radhakrishnan <ramana....@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....@gmail.com> Tested-by: Alex Coplan <alex.cop...@arm.com> Reviewed-by: Richard Earnshaw <rearn...@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; +}