RISC-V: Fix riscv_modes_tieable_p

Message ID 8fd4328940034d8778cca67eaad54e5a2c2b1a6c.fcd608b5.af7d.410c.935b.211e61191843@feishu.cn
State Deferred
Headers
Series RISC-V: Fix riscv_modes_tieable_p |

Checks

Context Check Description
rivoscibot/toolchain-ci-rivos-apply-patch fail Patch failed to apply
rivoscibot/toolchain-ci-rivos-lint warning Lint failed
linaro-tcwg-bot/tcwg_gcc_build--master-arm fail Patch failed to apply
linaro-tcwg-bot/tcwg_gcc_build--master-aarch64 fail Patch failed to apply

Commit Message

Zhijin Zeng Jan. 10, 2025, 4:48 p.m. UTC
  Integer values and floating-point values need to be converted
by fmv series instructions. So if mode1 is MODE_INT and mode2
is MODE_FLOAT, we should return false in riscv_modes_tieable_p,
and vice versa.

gcc/ChangeLog:

        * config/riscv/riscv.cc (riscv_modes_tieable_p):

gcc/testsuite/ChangeLog:

        * gcc.target/riscv/fwprop1-modes-tieable.c: New test.
---
 gcc/config/riscv/riscv.cc                     |  5 ++
 .../gcc.target/riscv/fwprop1-modes-tieable.c  | 80 +++++++++++++++++++
 2 files changed, 85 insertions(+)
 create mode 100644 gcc/testsuite/gcc.target/riscv/fwprop1-modes-tieable.c

--
2.25.1


This message and any attachment are confidential and may be privileged or otherwise protected from disclosure. If you are not an intended recipient of this message, please delete it and any attachment from your system and notify the sender immediately by reply e-mail. Unintended recipients should not use, copy, disclose or take any action based on this message or any information contained in this message. Emails cannot be guaranteed to be secure or error free as they can be intercepted, amended, lost or destroyed, and you should take full responsibility for security checking. 
 
本邮件及其任何附件具有保密性质,并可能受其他保护或不允许被披露给第三方。如阁下误收到本邮件,敬请立即以回复电子邮件的方式通知发件人,并将本邮件及其任何附件从阁下系统中予以删除。如阁下并非本邮件写明之收件人,敬请切勿使用、复制、披露本邮件或其任何内容,亦请切勿依本邮件或其任何内容而采取任何行动。电子邮件无法保证是一种安全和不会出现任何差错的通信方式,可能会被拦截、修改、丢失或损坏,收件人需自行负责做好安全检查。
  

Comments

Robin Dapp Jan. 10, 2025, 7:11 p.m. UTC | #1
> Integer values and floating-point values need to be converted
> by fmv series instructions. So if mode1 is MODE_INT and mode2
> is MODE_FLOAT, we should return false in riscv_modes_tieable_p,
> and vice versa.

I think that's on purpose because we can read and write float values
from/to integer registers.  Maybe it's a cost problem that we spill
at some point rather than access directly?

If I compile your test case I do see converting moves in the final
assembly - is there something you're concerned about in particular?
  
Jeff Law Jan. 10, 2025, 8:21 p.m. UTC | #2
On 1/10/25 12:11 PM, Robin Dapp wrote:
>> Integer values and floating-point values need to be converted
>> by fmv series instructions. So if mode1 is MODE_INT and mode2
>> is MODE_FLOAT, we should return false in riscv_modes_tieable_p,
>> and vice versa.
> 
> I think that's on purpose because we can read and write float values
> from/to integer registers.  Maybe it's a cost problem that we spill
> at some point rather than access directly?
But even if you spill, as long as loads/stores don't modify the value 
then I think we're OK from a correctness standpoint.


> 
> If I compile your test case I do see converting moves in the final
> assembly - is there something you're concerned about in particular?
Which was my general question as well.  Under precisely what 
circumstances is this causing a problem?  The secondary question would 
be how does this change interact with the finx and related extensions?

Jeff
  
Palmer Dabbelt Jan. 10, 2025, 11:59 p.m. UTC | #3
On Fri, 10 Jan 2025 12:21:15 PST (-0800), jeffreyalaw@gmail.com wrote:
>
>
> On 1/10/25 12:11 PM, Robin Dapp wrote:
>>> Integer values and floating-point values need to be converted
>>> by fmv series instructions. So if mode1 is MODE_INT and mode2
>>> is MODE_FLOAT, we should return false in riscv_modes_tieable_p,
>>> and vice versa.
>>
>> I think that's on purpose because we can read and write float values
>> from/to integer registers.  Maybe it's a cost problem that we spill
>> at some point rather than access directly?
> But even if you spill, as long as loads/stores don't modify the value
> then I think we're OK from a correctness standpoint.
>
>
>>
>> If I compile your test case I do see converting moves in the final
>> assembly - is there something you're concerned about in particular?

Which appears to be the glibc code (or very similar to it), and I don't 
think we've had users reporting incorrect results there.

> Which was my general question as well.  Under precisely what
> circumstances is this causing a problem?  The secondary question would
> be how does this change interact with the finx and related extensions?

FWIW I'm also a bit lost here: I'd expect riscv_hard_regno_mode_ok() to 
be sufficient to handle these X/F register mixing cases, and thus us not 
to need any more special handling in riscv_modes_tieable_p().

(I think we're safe for finx with the current code, as we can access the 
registers safely there.)

So maybe there's something else also needed to trigger this?

>
> Jeff
  
Jeff Law Jan. 11, 2025, 12:06 a.m. UTC | #4
On 1/10/25 4:59 PM, Palmer Dabbelt wrote:
> On Fri, 10 Jan 2025 12:21:15 PST (-0800), jeffreyalaw@gmail.com wrote:
>>
>>
>> On 1/10/25 12:11 PM, Robin Dapp wrote:
>>>> Integer values and floating-point values need to be converted
>>>> by fmv series instructions. So if mode1 is MODE_INT and mode2
>>>> is MODE_FLOAT, we should return false in riscv_modes_tieable_p,
>>>> and vice versa.
>>>
>>> I think that's on purpose because we can read and write float values
>>> from/to integer registers.  Maybe it's a cost problem that we spill
>>> at some point rather than access directly?
>> But even if you spill, as long as loads/stores don't modify the value
>> then I think we're OK from a correctness standpoint.
>>
>>
>>>
>>> If I compile your test case I do see converting moves in the final
>>> assembly - is there something you're concerned about in particular?
> 
> Which appears to be the glibc code (or very similar to it), and I don't 
> think we've had users reporting incorrect results there.
There's certainly cases in glibc that very much want to just move a blob 
of data from an FP register into a GPR without any kind of interpretation.

> 
>> Which was my general question as well.  Under precisely what
>> circumstances is this causing a problem?  The secondary question would
>> be how does this change interact with the finx and related extensions?
> 
> FWIW I'm also a bit lost here: I'd expect riscv_hard_regno_mode_ok() to 
> be sufficient to handle these X/F register mixing cases, and thus us not 
> to need any more special handling in riscv_modes_tieable_p().
> 
> (I think we're safe for finx with the current code, as we can access the 
> registers safely there.)
> 
> So maybe there's something else also needed to trigger this?
I don't think you're lost, Robin and I have similar concerns as yours. 
At this point I don't think the patch is right/correct, but I'm also 
open to the possibility there's something more complex going on that 
hasn't been fully explained.  Hence my request for an explanation of the 
precise circumstances when Zhijin thinks this is necessary.


Jeff
  

Patch

diff --git a/gcc/config/riscv/riscv.cc b/gcc/config/riscv/riscv.cc
index 65e09842fde..58b3b8c726c 100644
--- a/gcc/config/riscv/riscv.cc
+++ b/gcc/config/riscv/riscv.cc
@@ -9753,6 +9753,11 @@  riscv_modes_tieable_p (machine_mode mode1, machine_mode mode2)
      E.g. V2SI and DI are not tieable.  */
   if (riscv_v_ext_mode_p (mode1) != riscv_v_ext_mode_p (mode2))
     return false;
+  if ((GET_MODE_CLASS (mode1) == MODE_FLOAT
+       && GET_MODE_CLASS (mode2) == MODE_INT)
+       || (GET_MODE_CLASS (mode2) == MODE_FLOAT
+       && GET_MODE_CLASS (mode1) == MODE_INT))
+    return false;
   return (mode1 == mode2
          || !(GET_MODE_CLASS (mode1) == MODE_FLOAT
               && GET_MODE_CLASS (mode2) == MODE_FLOAT));
diff --git a/gcc/testsuite/gcc.target/riscv/fwprop1-modes-tieable.c b/gcc/testsuite/gcc.target/riscv/fwprop1-modes-tieable.c
new file mode 100644
index 00000000000..05d775c31d2
--- /dev/null
+++ b/gcc/testsuite/gcc.target/riscv/fwprop1-modes-tieable.c
@@ -0,0 +1,80 @@ 
+/* { dg-do compile } */
+/* { dg-options "-march=rv64gc -mabi=lp64d -fdump-rtl-fwprop1" } */
+/* { dg-skip-if "" { *-*-* } {"-Os" "-O1" "-O0" "-Og" "-Oz" "-flto"} } */
+/* { dg-final { scan-rtl-dump-not "\\(and:DI^(\s|\n)?$\\(subreg:DI^(\s|\n)?$\\(reg/v:DF" "fwprop1" } } */
+
+#include <stdint.h>
+
+#define EXP_TABLE_BITS 7
+#define EXP_POLY_ORDER 5
+#define EXP2_POLY_ORDER 5
+struct exp_data
+{
+  double invln2N;
+  double shift;
+  double negln2hiN;
+  double negln2loN;
+  double poly[4]; /* Last four coefficients.  */
+  double exp2_shift;
+  double exp2_poly[EXP2_POLY_ORDER];
+  uint64_t tab[2*(1 << EXP_TABLE_BITS)];
+};
+
+extern struct exp_data __exp_data;
+
+#define N (1 << EXP_TABLE_BITS)
+#define InvLn2N __exp_data.invln2N
+#define NegLn2hiN __exp_data.negln2hiN
+#define NegLn2loN __exp_data.negln2loN
+#define Shift __exp_data.shift
+#define T __exp_data.tab
+#define C2 __exp_data.poly[5 - EXP_POLY_ORDER]
+#define C3 __exp_data.poly[6 - EXP_POLY_ORDER]
+#define C4 __exp_data.poly[7 - EXP_POLY_ORDER]
+#define C5 __exp_data.poly[8 - EXP_POLY_ORDER]
+
+static inline uint64_t
+asuint64 (double f)
+{
+  union
+  {
+    double f;
+    uint64_t i;
+  } u = {f};
+  return u.i;
+}
+
+static inline double
+asdouble (uint64_t i)
+{
+  union
+  {
+    uint64_t i;
+    double f;
+  } u = {i};
+  return u.f;
+}
+
+double
+__testexp (double x)
+{
+  uint64_t ki, idx, sbits;
+  double kd, z, r, scale, tmp;
+
+  z = InvLn2N * x;
+
+  kd = z + Shift;
+  ki = asuint64 (kd);
+  kd -= Shift;
+
+  r = kd * NegLn2hiN + kd * NegLn2loN;
+
+  idx = (ki % N);
+
+  sbits = T[idx];
+
+  tmp = (r * C3);
+
+  scale = asdouble (sbits);
+  return scale * tmp;
+}