]> git.ipfire.org Git - thirdparty/gcc.git/commitdiff
RISC-V: Support vfadd static rounding mode by mode switching
authorPan Li <pan2.li@intel.com>
Thu, 29 Jun 2023 01:25:36 +0000 (09:25 +0800)
committerPan Li <pan2.li@intel.com>
Thu, 29 Jun 2023 04:13:26 +0000 (12:13 +0800)
This patch would like to support the vfadd static round mode similar to
the fixed-point. Then the related fsrm instructions will be inserted
correlatively.

Please *NOTE* this PATCH doesn't cover anything about FRM dynamic mode,
it will be implemented in the underlying PATCH(s).

Signed-off-by: Pan Li <pan2.li@intel.com>
gcc/ChangeLog:

* config/riscv/riscv.cc (riscv_emit_mode_set): Add emit for FRM.
(riscv_mode_needed): Likewise.
(riscv_entity_mode_after): Likewise.
(riscv_mode_after): Likewise.
(riscv_mode_entry): Likewise.
(riscv_mode_exit): Likewise.
* config/riscv/riscv.h (NUM_MODES_FOR_MODE_SWITCHING): Add number
for FRM.
* config/riscv/riscv.md: Add FRM register.
* config/riscv/vector-iterators.md: Add FRM type.
* config/riscv/vector.md (frm_mode): Define new attr for FRM mode.
(fsrm): Define new insn for fsrm instruction.

gcc/testsuite/ChangeLog:

* gcc.target/riscv/rvv/base/float-point-frm-insert-1.c: New test.
* gcc.target/riscv/rvv/base/float-point-frm-insert-2.c: New test.
* gcc.target/riscv/rvv/base/float-point-frm-insert-3.c: New test.
* gcc.target/riscv/rvv/base/float-point-frm-insert-4.c: New test.
* gcc.target/riscv/rvv/base/float-point-frm-insert-5.c: New test.

gcc/config/riscv/riscv.cc
gcc/config/riscv/riscv.h
gcc/config/riscv/riscv.md
gcc/config/riscv/vector-iterators.md
gcc/config/riscv/vector.md
gcc/testsuite/gcc.target/riscv/rvv/base/float-point-frm-insert-1.c [new file with mode: 0644]
gcc/testsuite/gcc.target/riscv/rvv/base/float-point-frm-insert-2.c [new file with mode: 0644]
gcc/testsuite/gcc.target/riscv/rvv/base/float-point-frm-insert-3.c [new file with mode: 0644]
gcc/testsuite/gcc.target/riscv/rvv/base/float-point-frm-insert-4.c [new file with mode: 0644]
gcc/testsuite/gcc.target/riscv/rvv/base/float-point-frm-insert-5.c [new file with mode: 0644]

index 280aa0b33b988687e9c7800c487e687d172d7038..e4dc8115e696ed44affe6ee8b51d635fe0eaaa33 100644 (file)
@@ -7669,6 +7669,16 @@ riscv_emit_mode_set (int entity, int mode, int prev_mode,
       if (mode != VXRM_MODE_NONE && mode != prev_mode)
        emit_insn (gen_vxrmsi (gen_int_mode (mode, SImode)));
       break;
+    case RISCV_FRM:
+      if (mode != FRM_MODE_NONE && mode != prev_mode)
+       {
+         rtx scaler = gen_reg_rtx (SImode);
+         rtx imm = gen_int_mode (mode, SImode);
+
+         emit_insn (gen_movsi (scaler, imm));
+         emit_insn (gen_fsrm (scaler, scaler));
+       }
+      break;
     default:
       gcc_unreachable ();
     }
@@ -7680,11 +7690,14 @@ riscv_emit_mode_set (int entity, int mode, int prev_mode,
 static int
 riscv_mode_needed (int entity, rtx_insn *insn)
 {
+  int code = recog_memoized (insn);
+
   switch (entity)
     {
     case RISCV_VXRM:
-      return recog_memoized (insn) >= 0 ? get_attr_vxrm_mode (insn)
-                                       : VXRM_MODE_NONE;
+      return code >= 0 ? get_attr_vxrm_mode (insn) : VXRM_MODE_NONE;
+    case RISCV_FRM:
+      return code >= 0 ? get_attr_frm_mode (insn) : FRM_MODE_NONE;
     default:
       gcc_unreachable ();
     }
@@ -7715,6 +7728,21 @@ global_state_unknown_p (rtx_insn *insn, unsigned int regno)
   return false;
 }
 
+static int
+riscv_entity_mode_after (int regnum, rtx_insn *insn, int mode,
+                        int (*get_attr_mode) (rtx_insn *), int default_mode)
+{
+  if (global_state_unknown_p (insn, regnum))
+    return default_mode;
+  else if (recog_memoized (insn) < 0)
+    return mode;
+
+  rtx reg = gen_rtx_REG (SImode, regnum);
+  bool mentioned_p = reg_mentioned_p (reg, PATTERN (insn));
+
+  return mentioned_p ? get_attr_mode (insn): mode;
+}
+
 /* Return the mode that an insn results in.  */
 
 static int
@@ -7723,15 +7751,13 @@ riscv_mode_after (int entity, int mode, rtx_insn *insn)
   switch (entity)
     {
     case RISCV_VXRM:
-      if (global_state_unknown_p (insn, VXRM_REGNUM))
-       return VXRM_MODE_NONE;
-      else if (recog_memoized (insn) >= 0)
-       return reg_mentioned_p (gen_rtx_REG (SImode, VXRM_REGNUM),
-                               PATTERN (insn))
-                ? get_attr_vxrm_mode (insn)
-                : mode;
-      else
-       return mode;
+      return riscv_entity_mode_after (VXRM_REGNUM, insn, mode,
+                                     (int (*)(rtx_insn *)) get_attr_vxrm_mode,
+                                     VXRM_MODE_NONE);
+    case RISCV_FRM:
+      return riscv_entity_mode_after (FRM_REGNUM, insn, mode,
+                                     (int (*)(rtx_insn *)) get_attr_frm_mode,
+                                     FRM_MODE_NONE);
     default:
       gcc_unreachable ();
     }
@@ -7747,6 +7773,8 @@ riscv_mode_entry (int entity)
     {
     case RISCV_VXRM:
       return VXRM_MODE_NONE;
+    case RISCV_FRM:
+      return FRM_MODE_NONE;
     default:
       gcc_unreachable ();
     }
@@ -7762,6 +7790,8 @@ riscv_mode_exit (int entity)
     {
     case RISCV_VXRM:
       return VXRM_MODE_NONE;
+    case RISCV_FRM:
+      return FRM_MODE_NONE;
     default:
       gcc_unreachable ();
     }
index bfd9b7551bcdf79a19a49222b10a915ffb376928..83dcac165b5105ec8a424473888e3b89bb44d883 100644 (file)
@@ -849,7 +849,7 @@ typedef struct {
   "fs0", "fs1", "fa0", "fa1", "fa2", "fa3", "fa4", "fa5",      \
   "fa6", "fa7", "fs2", "fs3", "fs4", "fs5", "fs6", "fs7",      \
   "fs8", "fs9", "fs10","fs11","ft8", "ft9", "ft10","ft11",     \
-  "arg", "frame", "vl", "vtype", "vxrm", "N/A", "N/A", "N/A",   \
+  "arg", "frame", "vl", "vtype", "vxrm", "frm", "N/A", "N/A",   \
   "N/A", "N/A", "N/A", "N/A", "N/A", "N/A", "N/A", "N/A",      \
   "N/A", "N/A", "N/A", "N/A", "N/A", "N/A", "N/A", "N/A",      \
   "N/A", "N/A", "N/A", "N/A", "N/A", "N/A", "N/A", "N/A",      \
@@ -1113,6 +1113,6 @@ extern void riscv_remove_unneeded_save_restore_calls (void);
 
 /* Mode switching (Lazy code motion) for RVV rounding mode instructions.  */
 #define OPTIMIZE_MODE_SWITCHING(ENTITY) (TARGET_VECTOR)
-#define NUM_MODES_FOR_MODE_SWITCHING {VXRM_MODE_NONE}
+#define NUM_MODES_FOR_MODE_SWITCHING {VXRM_MODE_NONE, FRM_MODE_NONE}
 
 #endif /* ! GCC_RISCV_H */
index 565e8cd27cdd6bea6bea8ac58e908b17a58b6048..4cd8b9d7c779eb20827332fc94d3c7453bbcd46c 100644 (file)
 ;; rdvlenb     vector byte length vlenb csrr read
 ;; rdvl        vector length vl csrr read
 ;; wrvxrm      vector fixed-point rounding mode write
+;; wrfrm       vector floating-point rounding mode write
 ;; vsetvl      vector configuration-setting instrucions
 ;; 7. Vector Loads and Stores
 ;; vlde        vector unit-stride load instructions
    mtc,mfc,const,arith,logical,shift,slt,imul,idiv,move,fmove,fadd,fmul,
    fmadd,fdiv,fcmp,fcvt,fsqrt,multi,auipc,sfb_alu,nop,ghost,bitmanip,rotate,
    clmul,min,max,minu,maxu,clz,ctz,cpop,
-   atomic,condmove,crypto,rdvlenb,rdvl,wrvxrm,vsetvl,vlde,vste,vldm,vstm,vlds,vsts,
+   atomic,condmove,crypto,rdvlenb,rdvl,wrvxrm,wrfrm,vsetvl,
+   vlde,vste,vldm,vstm,vlds,vsts,
    vldux,vldox,vstux,vstox,vldff,vldr,vstr,
    vlsegde,vssegte,vlsegds,vssegts,vlsegdux,vlsegdox,vssegtux,vssegtox,vlsegdff,
    vialu,viwalu,vext,vicalu,vshift,vnshift,vicmp,viminmax,
index 0e6b614e50944b89baafd344ae0aa0dc2aa76e40..a79ea79abb6f135793f4c0be4f5207c7e835fc4a 100644 (file)
@@ -81,6 +81,8 @@
   UNSPEC_VCOMPRESS
   UNSPEC_VLEFF
   UNSPEC_MODIFY_VL
+
+  UNSPEC_FSRM
 ])
 
 (define_mode_iterator V [
index 406f96439ec36a577215de925c4eb57ac9d669fa..5e489dc91ba32cf336c17d9b9ee78aa1feee779b 100644 (file)
         (const_string "rod")]
         (const_string "none")))
 
+;; Defines rounding mode of an floating-point operation.
+(define_attr "frm_mode" "rne,rtz,rdn,rup,rmm,dyn,none"
+  (cond
+    [
+      (eq_attr "type" "vfalu")
+      (cond
+       [
+         (match_test "INTVAL (operands[9]) == riscv_vector::FRM_RNE")
+         (const_string "rne")
+
+         (match_test "INTVAL (operands[9]) == riscv_vector::FRM_RTZ")
+         (const_string "rtz")
+
+         (match_test "INTVAL (operands[9]) == riscv_vector::FRM_RDN")
+         (const_string "rdn")
+
+         (match_test "INTVAL (operands[9]) == riscv_vector::FRM_RUP")
+         (const_string "rup")
+
+         (match_test "INTVAL (operands[9]) == riscv_vector::FRM_RMM")
+         (const_string "rmm")
+
+         (match_test "INTVAL (operands[9]) == riscv_vector::FRM_RDN")
+         (const_string "rdn")
+       ]
+       (const_string "none")
+      )
+    ]
+    (const_string "none")
+  )
+)
+
 ;; -----------------------------------------------------------------
 ;; ---- Miscellaneous Operations
 ;; -----------------------------------------------------------------
   [(set_attr "type" "wrvxrm")
    (set_attr "mode" "SI")])
 
+;; Set FRM
+(define_insn "fsrm"
+  [
+    (set
+      (reg:SI FRM_REGNUM)
+      (unspec:SI
+       [
+         (match_operand:SI 0 "register_operand" "=&r")
+         (match_operand:SI 1 "register_operand" "r")
+       ] UNSPEC_FSRM
+      )
+    )
+  ]
+  "TARGET_VECTOR"
+  "fsrm\t%0,%1"
+  [
+    (set_attr "type" "wrfrm")
+    (set_attr "mode" "SI")
+  ]
+)
+
 ;; -----------------------------------------------------------------
 ;; ---- Moves Operations
 ;; -----------------------------------------------------------------
diff --git a/gcc/testsuite/gcc.target/riscv/rvv/base/float-point-frm-insert-1.c b/gcc/testsuite/gcc.target/riscv/rvv/base/float-point-frm-insert-1.c
new file mode 100644 (file)
index 0000000..732e030
--- /dev/null
@@ -0,0 +1,31 @@
+/* { dg-do compile } */
+/* { dg-options "-march=rv64gcv -mabi=lp64 -O3 -Wno-psabi" } */
+
+#include "riscv_vector.h"
+
+typedef float float32_t;
+
+vfloat32m1_t
+test_riscv_vfadd_vv_f32m1_rm (vfloat32m1_t op1, vfloat32m1_t op2, size_t vl) {
+  return __riscv_vfadd_vv_f32m1_rm (op1, op2, 0, vl);
+}
+
+vfloat32m1_t
+test_vfadd_vv_f32m1_m_rm(vbool32_t mask, vfloat32m1_t op1, vfloat32m1_t op2,
+                        size_t vl) {
+  return __riscv_vfadd_vv_f32m1_m_rm(mask, op1, op2, 1, vl);
+}
+
+vfloat32m1_t
+test_vfadd_vf_f32m1_rm(vfloat32m1_t op1, float32_t op2, size_t vl) {
+  return __riscv_vfadd_vf_f32m1_rm(op1, op2, 2, vl);
+}
+
+vfloat32m1_t
+test_vfadd_vf_f32m1_m_rm(vbool32_t mask, vfloat32m1_t op1, float32_t op2,
+                        size_t vl) {
+  return __riscv_vfadd_vf_f32m1_m_rm(mask, op1, op2, 3, vl);
+}
+
+/* { dg-final { scan-assembler-times {vfadd\.v[vf]\s+v[0-9]+,\s*v[0-9]+,\s*[fav]+[0-9]+} 4 } } */
+/* { dg-final { scan-assembler-times {fsrm\s+[ax][0-9]+,\s*[ax][0-9]+} 4 } } */
diff --git a/gcc/testsuite/gcc.target/riscv/rvv/base/float-point-frm-insert-2.c b/gcc/testsuite/gcc.target/riscv/rvv/base/float-point-frm-insert-2.c
new file mode 100644 (file)
index 0000000..72e5d20
--- /dev/null
@@ -0,0 +1,14 @@
+/* { dg-do compile } */
+/* { dg-options "-march=rv64gcv -mabi=lp64 -O3 -Wno-psabi" } */
+
+#include "riscv_vector.h"
+
+vfloat32m1_t
+test_float_point_frm_insert (vfloat32m1_t op1, vfloat32m1_t op2, size_t vl) {
+  vfloat32m1_t v1 = __riscv_vfadd_vv_f32m1_rm (op1, op2, 0, vl);
+  vfloat32m1_t v2 = __riscv_vfmin_vv_f32m1 (op1, v1, vl);
+  return __riscv_vfadd_vv_f32m1_rm (v1, v2, 0, vl);
+}
+
+/* { dg-final { scan-assembler-times {vfadd\.vv\s+v[0-9]+,\s*v[0-9]+,\s*v+[0-9]+} 2 } } */
+/* { dg-final { scan-assembler-times {fsrm\s+[ax][0-9]+,\s*[ax][0-9]+} 1 } } */
diff --git a/gcc/testsuite/gcc.target/riscv/rvv/base/float-point-frm-insert-3.c b/gcc/testsuite/gcc.target/riscv/rvv/base/float-point-frm-insert-3.c
new file mode 100644 (file)
index 0000000..c9e8d0a
--- /dev/null
@@ -0,0 +1,14 @@
+/* { dg-do compile } */
+/* { dg-options "-march=rv64gcv -mabi=lp64 -O3 -Wno-psabi" } */
+
+#include "riscv_vector.h"
+
+vfloat32m1_t
+test_float_point_frm_insert (vfloat32m1_t op1, vfloat32m1_t op2, size_t vl) {
+  vfloat32m1_t v1 = __riscv_vfadd_vv_f32m1_rm (op1, op2, 0, vl);
+  vfloat32m1_t v2 = __riscv_vfmin_vv_f32m1 (op1, v1, vl);
+  return __riscv_vfadd_vv_f32m1_rm (v1, v2, 1, vl);
+}
+
+/* { dg-final { scan-assembler-times {vfadd\.vv\s+v[0-9]+,\s*v[0-9]+,\s*v+[0-9]+} 2 } } */
+/* { dg-final { scan-assembler-times {fsrm\s+[ax][0-9]+,\s*[ax][0-9]+} 2 } } */
diff --git a/gcc/testsuite/gcc.target/riscv/rvv/base/float-point-frm-insert-4.c b/gcc/testsuite/gcc.target/riscv/rvv/base/float-point-frm-insert-4.c
new file mode 100644 (file)
index 0000000..a288e0b
--- /dev/null
@@ -0,0 +1,23 @@
+/* { dg-do compile } */
+/* { dg-options "-march=rv64gcv -mabi=lp64 -O3 -Wno-psabi" } */
+
+#include "riscv_vector.h"
+
+vfloat32m1_t
+test_float_point_frm_insert (vfloat32m1_t op1, vfloat32m1_t op2, size_t vl,
+                            size_t count) {
+  vfloat32m1_t tmp = op1, result;
+
+  result = __riscv_vfadd_vv_f32m1_rm (result, op1, 1, vl);
+  result = __riscv_vfadd_vv_f32m1_rm (result, op2, 1, vl);
+
+  for (int i = 0; i < count; i++) {
+    tmp = __riscv_vfadd_vv_f32m1_rm (op1, tmp, 1, vl + i);
+    result = __riscv_vfrsqrt7_v_f32m1 (tmp, vl + i);
+  }
+
+  return __riscv_vfadd_vv_f32m1_rm (result, op2, 1, vl);
+}
+
+/* { dg-final { scan-assembler-times {vfadd\.vv\s+v[0-9]+,\s*v[0-9]+,\s*v+[0-9]+} 4 } } */
+/* { dg-final { scan-assembler-times {fsrm\s+[ax][0-9]+,\s*[ax][0-9]+} 1 } } */
diff --git a/gcc/testsuite/gcc.target/riscv/rvv/base/float-point-frm-insert-5.c b/gcc/testsuite/gcc.target/riscv/rvv/base/float-point-frm-insert-5.c
new file mode 100644 (file)
index 0000000..bb77a6e
--- /dev/null
@@ -0,0 +1,23 @@
+/* { dg-do compile } */
+/* { dg-options "-march=rv64gcv -mabi=lp64 -O3 -Wno-psabi" } */
+
+#include "riscv_vector.h"
+
+vfloat32m1_t
+test_float_point_frm_insert (vfloat32m1_t op1, vfloat32m1_t op2, size_t vl,
+                            size_t count) {
+  vfloat32m1_t tmp = op1, result;
+
+  result = __riscv_vfadd_vv_f32m1_rm (result, op1, 1, vl);
+  result = __riscv_vfadd_vv_f32m1_rm (result, op2, 1, vl);
+
+  for (int i = 0; i < count; i++) {
+    tmp = __riscv_vfadd_vv_f32m1_rm (op1, tmp, 2, vl + i);
+    result = __riscv_vfrsqrt7_v_f32m1 (tmp, vl + i);
+  }
+
+  return __riscv_vfadd_vv_f32m1_rm (result, op2, 1, vl);
+}
+
+/* { dg-final { scan-assembler-times {vfadd\.vv\s+v[0-9]+,\s*v[0-9]+,\s*v+[0-9]+} 4 } } */
+/* { dg-final { scan-assembler-times {fsrm\s+[ax][0-9]+,\s*[ax][0-9]+} 3 } } */