On sifive-p870-d, a non-segment vector indexed load whose destination
overlaps its index cannot restart correctly after a trap.  The elements
from the faulting one onward are already written, so the index is clobbered
and the restarted load reads from the wrong address.

Add -mfix-sifive-mal-9174, which keeps the destination from overlapping the
index.  It is on by default for sifive-p870-d.

The new index_load_overlap attribute turns off the overlap alternatives when
the workaround is on.  The same-EEW pattern also gets early-clobber
alternatives.  They are marked with `?` so the code does not change when the
workaround is off (without `?`, IRA gets slightly different costs and gives
different results).

gcc/ChangeLog:

        * config/riscv/riscv-cores.def (sifive-p870-d): Add erratum
        SIFIVE_MAL_9174.
        * config/riscv/riscv-errata.def (SIFIVE_MAL_9174): New erratum.
        * config/riscv/riscv.md (index_load_overlap): New attribute.
        (enabled): Disable index_load_overlap alternatives for MAL-9174.
        * config/riscv/riscv.opt (mfix-sifive-mal-9174): New option.
        * config/riscv/vector.md
        (@pred_indexed_<order>load<mode>_same_eew): Add early-clobber
        alternatives and set index_load_overlap.
        (@pred_indexed_<order>load<mode>_x2_smaller_eew): Set
        index_load_overlap.
        (@pred_indexed_<order>load<mode>_x4_smaller_eew): Likewise.
        (@pred_indexed_<order>load<mode>_x8_smaller_eew): Likewise.
        * doc/install.texi (--with-errata): List sifive-mal-9174.
        * doc/invoke.texi (RISC-V Options): Document -mfix-sifive-mal-9174.

gcc/testsuite/ChangeLog:

        * gcc.target/riscv/rvv/base/sifive-mal-9174-1.c: New test.
        * gcc.target/riscv/rvv/base/sifive-mal-9174-2.c: New test.
        * gcc.target/riscv/rvv/base/sifive-mal-9174-3.c: New test.
        * gcc.target/riscv/rvv/base/sifive-mal-9174-4.c: New test.
---
 gcc/config/riscv/riscv-cores.def              |  1 +
 gcc/config/riscv/riscv-errata.def             |  2 +
 gcc/config/riscv/riscv.md                     |  9 ++++
 gcc/config/riscv/riscv.opt                    |  4 ++
 gcc/config/riscv/vector.md                    | 34 ++++++++------
 gcc/doc/install.texi                          |  5 ++
 gcc/doc/invoke.texi                           | 20 ++++++++
 .../riscv/rvv/base/sifive-mal-9174-1.c        | 46 +++++++++++++++++++
 .../riscv/rvv/base/sifive-mal-9174-2.c        | 46 +++++++++++++++++++
 .../riscv/rvv/base/sifive-mal-9174-3.c        | 46 +++++++++++++++++++
 .../riscv/rvv/base/sifive-mal-9174-4.c        | 46 +++++++++++++++++++
 11 files changed, 245 insertions(+), 14 deletions(-)
 create mode 100644 gcc/testsuite/gcc.target/riscv/rvv/base/sifive-mal-9174-1.c
 create mode 100644 gcc/testsuite/gcc.target/riscv/rvv/base/sifive-mal-9174-2.c
 create mode 100644 gcc/testsuite/gcc.target/riscv/rvv/base/sifive-mal-9174-3.c
 create mode 100644 gcc/testsuite/gcc.target/riscv/rvv/base/sifive-mal-9174-4.c

diff --git a/gcc/config/riscv/riscv-cores.def b/gcc/config/riscv/riscv-cores.def
index 725b55dbb37..e2357fe153a 100644
--- a/gcc/config/riscv/riscv-cores.def
+++ b/gcc/config/riscv/riscv-cores.def
@@ -202,6 +202,7 @@ RISCV_CORE("sifive-p870-d",   
"rv64imafdcbv_zic64b_zicbom_zicbop_zicboz_"
                              /* MVENDORID */ 0x489,
                              /* MARCHID */ 0x8000000000000109ULL,
                              /* MIMPID */ 0x14250606ULL)
+RISCV_CORE_ERRATUM("sifive-p870-d", SIFIVE_MAL_9174)
 
 RISCV_CORE("thead-c906",      
"rv64imafdc_xtheadba_xtheadbb_xtheadbs_xtheadcmo_"
                              "xtheadcondmov_xtheadfmemidx_xtheadmac_"
diff --git a/gcc/config/riscv/riscv-errata.def 
b/gcc/config/riscv/riscv-errata.def
index bcf74faf495..f3cd54b421c 100644
--- a/gcc/config/riscv/riscv-errata.def
+++ b/gcc/config/riscv/riscv-errata.def
@@ -30,4 +30,6 @@
    Every erratum needs a -mfix-NAME option in riscv.opt.  The cores that
    have the erratum are listed in riscv-cores.def with RISCV_CORE_ERRATUM.  */
 
+RISCV_ERRATUM(SIFIVE_MAL_9174, "sifive-mal-9174", riscv_fix_sifive_mal_9174)
+
 #undef RISCV_ERRATUM
diff --git a/gcc/config/riscv/riscv.md b/gcc/config/riscv/riscv.md
index 47e83e87630..abb1cc6cf38 100644
--- a/gcc/config/riscv/riscv.md
+++ b/gcc/config/riscv/riscv.md
@@ -596,6 +596,11 @@ (define_attr "spec_restriction_disabled" "no,yes"
        ]
        (const_string "no")))
 
+;; This attribute marks the vector indexed load alternatives that let the
+;; destination overlap the index.
+(define_attr "index_load_overlap" "no,yes"
+  (const_string "no"))
+
 ;; Attribute to control enable or disable instructions.
 (define_attr "enabled" "no,yes"
   (cond [
@@ -607,6 +612,10 @@ (define_attr "enabled" "no,yes"
 
     (eq_attr "spec_restriction_disabled" "yes")
     (const_string "no")
+
+    (and (eq_attr "index_load_overlap" "yes")
+        (match_test "RISCV_ERRATUM_P (SIFIVE_MAL_9174)"))
+    (const_string "no")
   ]
   (const_string "yes")))
 
diff --git a/gcc/config/riscv/riscv.opt b/gcc/config/riscv/riscv.opt
index 3226367bf8a..c78870267d0 100644
--- a/gcc/config/riscv/riscv.opt
+++ b/gcc/config/riscv/riscv.opt
@@ -144,6 +144,10 @@ mvector-strict-align
 Target Save Var(rvv_vector_strict_align) Init(1)
 Do not create element-misaligned vector memory accesses.
 
+mfix-sifive-mal-9174
+Target Save Var(riscv_fix_sifive_mal_9174) Init(-1)
+Work around SiFive erratum MAL-9174.
+
 Enum
 Name(code_model) Type(enum riscv_code_model)
 Known code models (for use with the -mcmodel= option):
diff --git a/gcc/config/riscv/vector.md b/gcc/config/riscv/vector.md
index 8a97f35967f..22b077adf82 100644
--- a/gcc/config/riscv/vector.md
+++ b/gcc/config/riscv/vector.md
@@ -2595,27 +2595,30 @@ (define_insn "@pred_strided_store<mode>"
 ;; - 7.6. Vector Indexed Instructions
 ;; 
-------------------------------------------------------------------------------
 
-;; DEST eew is same as SOURCE eew, DEST register can overlap SOURCE.
+;; DEST eew is same as SOURCE eew, DEST register can overlap SOURCE,
+;; except for the last four alternatives, which are for SiFive erratum
+;; MAL-9174.
 (define_insn "@pred_indexed_<order>load<mode>_same_eew"
-  [(set (match_operand:VINDEXED 0 "register_operand"        "=vd, vr,vd, vr")
+  [(set (match_operand:VINDEXED 0 "register_operand"        "=vd, vr,vd, 
vr,?&vd,?&vr,?&vd,?&vr")
        (if_then_else:VINDEXED
          (unspec:<VM>
-           [(match_operand:<VM> 1 "vector_mask_operand"    " vm,Wc1,vm,Wc1")
-            (match_operand 5 "vector_length_operand"       "rvl,rvl,rvl,rvl")
-            (match_operand 6 "const_int_operand"           "  i,  i, i,  i")
-            (match_operand 7 "const_int_operand"           "  i,  i, i,  i")
-            (match_operand 8 "const_int_operand"           "  i,  i, i,  i")
+           [(match_operand:<VM> 1 "vector_mask_operand"    " vm,Wc1,vm,Wc1, 
vm,Wc1, vm,Wc1")
+            (match_operand 5 "vector_length_operand"       
"rvl,rvl,rvl,rvl,rvl,rvl,rvl,rvl")
+            (match_operand 6 "const_int_operand"           "  i,  i, i,  i,  
i,  i,  i,  i")
+            (match_operand 7 "const_int_operand"           "  i,  i, i,  i,  
i,  i,  i,  i")
+            (match_operand 8 "const_int_operand"           "  i,  i, i,  i,  
i,  i,  i,  i")
             (reg:SI VL_REGNUM)
             (reg:SI VTYPE_REGNUM)] UNSPEC_VPREDICATE)
          (unspec:VINDEXED
-           [(match_operand 3 "pmode_reg_or_0_operand"      " rJ, rJ,rJ, rJ")
+           [(match_operand 3 "pmode_reg_or_0_operand"      " rJ, rJ,rJ, rJ, 
rJ, rJ, rJ, rJ")
             (mem:BLK (scratch))
-            (match_operand:<VINDEX> 4 "register_operand"   " vr, vr,vr, vr")] 
ORDER)
-         (match_operand:VINDEXED 2 "vector_merge_operand"  " vu, vu, 0,  0")))]
+            (match_operand:<VINDEX> 4 "register_operand"   " vr, vr,vr, vr, 
vr, vr, vr, vr")] ORDER)
+         (match_operand:VINDEXED 2 "vector_merge_operand"  " vu, vu, 0,  0, 
vu, vu,  0,  0")))]
   "TARGET_VECTOR"
   "vl<order>xei<sew>.v\t%0,(%z3),%4%p1"
   [(set_attr "type" "vld<order>x")
-   (set_attr "mode" "<MODE>")])
+   (set_attr "mode" "<MODE>")
+   (set_attr "index_load_overlap" "yes,yes,yes,yes,no,no,no,no")])
 
 ;; DEST eew is greater than SOURCE eew.
 (define_insn "@pred_indexed_<order>load<mode>_x2_greater_eew"
@@ -2701,7 +2704,8 @@ (define_insn 
"@pred_indexed_<order>load<mode>_x2_smaller_eew"
   "TARGET_VECTOR"
   "vl<order>xei<double_ext_sew>.v\t%0,(%z3),%4%p1"
   [(set_attr "type" "vld<order>x")
-   (set_attr "mode" "<MODE>")])
+   (set_attr "mode" "<MODE>")
+   (set_attr "index_load_overlap" "yes,yes,yes,yes,no,no")])
 
 (define_insn "@pred_indexed_<order>load<mode>_x4_smaller_eew"
   [(set (match_operand:VEEWTRUNC4 0 "register_operand"             "=vd, vd, 
vr, vr,  &vr,  &vr")
@@ -2722,7 +2726,8 @@ (define_insn 
"@pred_indexed_<order>load<mode>_x4_smaller_eew"
   "TARGET_VECTOR"
   "vl<order>xei<quad_ext_sew>.v\t%0,(%z3),%4%p1"
   [(set_attr "type" "vld<order>x")
-   (set_attr "mode" "<MODE>")])
+   (set_attr "mode" "<MODE>")
+   (set_attr "index_load_overlap" "yes,yes,yes,yes,no,no")])
 
 (define_insn "@pred_indexed_<order>load<mode>_x8_smaller_eew"
   [(set (match_operand:VEEWTRUNC8 0 "register_operand"            "=vd, vd, 
vr, vr,  &vr,  &vr")
@@ -2743,7 +2748,8 @@ (define_insn 
"@pred_indexed_<order>load<mode>_x8_smaller_eew"
   "TARGET_VECTOR"
   "vl<order>xei<oct_ext_sew>.v\t%0,(%z3),%4%p1"
   [(set_attr "type" "vld<order>x")
-   (set_attr "mode" "<MODE>")])
+   (set_attr "mode" "<MODE>")
+   (set_attr "index_load_overlap" "yes,yes,yes,yes,no,no")])
 
 (define_insn "@pred_indexed_<order>store<mode>_same_eew"
   [(set (mem:BLK (scratch))
diff --git a/gcc/doc/install.texi b/gcc/doc/install.texi
index 87490157fd6..833770b01eb 100644
--- a/gcc/doc/install.texi
+++ b/gcc/doc/install.texi
@@ -1929,6 +1929,11 @@ comma-separated @var{list} by default, as if the matching
 @option{-mfix-@var{erratum}} options were given.  @var{list} can also be
 @samp{all} for every erratum GCC knows about, or @samp{none}, which is the
 default.  The @option{-mno-fix-@var{erratum}} options still disable them.
+The errata GCC knows about are:
+@table @code
+@item sifive-mal-9174
+See @option{-mfix-sifive-mal-9174}.
+@end table
 
 @item --enable-__cxa_atexit
 Define if you want to use @code{__cxa_atexit}, rather than @code{atexit},
diff --git a/gcc/doc/invoke.texi b/gcc/doc/invoke.texi
index 8a2c65a861e..fc38c6e86a1 100644
--- a/gcc/doc/invoke.texi
+++ b/gcc/doc/invoke.texi
@@ -1342,6 +1342,7 @@ See RS/6000 and PowerPC Options.
 -msave-restore  -mno-shorten-memrefs
 -mstrict-align  -mscalar-strict-align  -mno-vector-strict-align
 -mzilsd-word-align  -mzilsd-strict-align
+-mfix-sifive-mal-9174
 -mcmodel=medlow  -mcmodel=medany  -mcmodel=large
 -mexplicit-relocs  -mrelax  -mriscv-attribute
 -malign-data=@var{type}
@@ -31669,6 +31670,25 @@ The precedence among @option{-mzilsd-word-align},
 @option{-mno-strict-align}, @option{-mscalar-strict-align}, and
 @option{-mno-scalar-strict-align} is determined by the last one specified.
 
+@opindex mfix-sifive-mal-9174
+@opindex mno-fix-sifive-mal-9174
+@item -mfix-sifive-mal-9174
+@itemx -mno-fix-sifive-mal-9174
+Enable or disable the workaround for SiFive erratum MAL-9174, which keeps
+the destination of a vector indexed load from overlapping its index.  On
+the affected cores, a trap in the middle of such a load can clobber the
+index, so the load reads from the wrong address when it is restarted.
+
+By default, the workarounds for the errata of the processor selected by
+@option{-mcpu} are enabled, along with the ones given by the
+@option{--with-errata} configure option.  @option{-mtune} has no effect
+on them.  The errata of each processor are:
+
+@table @samp
+@item sifive-p870-d
+@samp{sifive-mal-9174}
+@end table
+
 @opindex mmax-vectorization
 @opindex mno-max-vectorization
 @item -mmax-vectorization
diff --git a/gcc/testsuite/gcc.target/riscv/rvv/base/sifive-mal-9174-1.c 
b/gcc/testsuite/gcc.target/riscv/rvv/base/sifive-mal-9174-1.c
new file mode 100644
index 00000000000..9a19a32e8a0
--- /dev/null
+++ b/gcc/testsuite/gcc.target/riscv/rvv/base/sifive-mal-9174-1.c
@@ -0,0 +1,46 @@
+/* { dg-do compile } */
+/* { dg-options "-march=rv64gcv -mabi=lp64d -O2 -mfix-sifive-mal-9174" } */
+
+/* The destination of an indexed load must not overlap the index.  */
+
+#include <riscv_vector.h>
+
+vint32m1_t
+tama (int *base, vuint32m1_t index, size_t vl)
+{
+  return __riscv_vluxei32_v_i32m1 (base, index, vl);
+}
+
+vint32m1_t
+tamu (vbool32_t m, int *base, vuint32m1_t index, size_t vl)
+{
+  return __riscv_vluxei32_v_i32m1_m (m, base, index, vl);
+}
+
+vint32m1_t
+tu (int *base, vuint32m1_t index, size_t vl)
+{
+  vint32m1_t merge = __riscv_vreinterpret_v_u32m1_i32m1 (index);
+  return __riscv_vluxei32_v_i32m1_tu (merge, base, index, vl);
+}
+
+vint32m1_t
+tumu (vbool32_t m, int *base, vuint32m1_t index, size_t vl)
+{
+  vint32m1_t merge = __riscv_vreinterpret_v_u32m1_i32m1 (index);
+  return __riscv_vluxei32_v_i32m1_tumu (m, merge, base, index, vl);
+}
+
+vint8mf8_t
+se (void *base, vuint64m1_t index, size_t vl)
+{
+  return __riscv_vluxei64_v_i8mf8 (base, index, vl);
+}
+
+vint8mf8_t
+se_tu (void *base, vuint64m1_t index, vint8mf8_t merge, size_t vl)
+{
+  return __riscv_vluxei64_v_i8mf8_tu (merge, base, index, vl);
+}
+
+/* { dg-final { scan-assembler-not 
{vl[ou]xei[0-9]+\.v\s+v([0-9]+),\([^)]*\),v\1\M} } } */
diff --git a/gcc/testsuite/gcc.target/riscv/rvv/base/sifive-mal-9174-2.c 
b/gcc/testsuite/gcc.target/riscv/rvv/base/sifive-mal-9174-2.c
new file mode 100644
index 00000000000..ccd8c84477b
--- /dev/null
+++ b/gcc/testsuite/gcc.target/riscv/rvv/base/sifive-mal-9174-2.c
@@ -0,0 +1,46 @@
+/* { dg-do compile } */
+/* { dg-options "-march=rv64gcv -mabi=lp64d -O2 -mcpu=sifive-p870-d" } */
+
+/* -mcpu=sifive-p870-d enables the workaround.  */
+
+#include <riscv_vector.h>
+
+vint32m1_t
+tama (int *base, vuint32m1_t index, size_t vl)
+{
+  return __riscv_vluxei32_v_i32m1 (base, index, vl);
+}
+
+vint32m1_t
+tamu (vbool32_t m, int *base, vuint32m1_t index, size_t vl)
+{
+  return __riscv_vluxei32_v_i32m1_m (m, base, index, vl);
+}
+
+vint32m1_t
+tu (int *base, vuint32m1_t index, size_t vl)
+{
+  vint32m1_t merge = __riscv_vreinterpret_v_u32m1_i32m1 (index);
+  return __riscv_vluxei32_v_i32m1_tu (merge, base, index, vl);
+}
+
+vint32m1_t
+tumu (vbool32_t m, int *base, vuint32m1_t index, size_t vl)
+{
+  vint32m1_t merge = __riscv_vreinterpret_v_u32m1_i32m1 (index);
+  return __riscv_vluxei32_v_i32m1_tumu (m, merge, base, index, vl);
+}
+
+vint8mf8_t
+se (void *base, vuint64m1_t index, size_t vl)
+{
+  return __riscv_vluxei64_v_i8mf8 (base, index, vl);
+}
+
+vint8mf8_t
+se_tu (void *base, vuint64m1_t index, vint8mf8_t merge, size_t vl)
+{
+  return __riscv_vluxei64_v_i8mf8_tu (merge, base, index, vl);
+}
+
+/* { dg-final { scan-assembler-not 
{vl[ou]xei[0-9]+\.v\s+v([0-9]+),\([^)]*\),v\1\M} } } */
diff --git a/gcc/testsuite/gcc.target/riscv/rvv/base/sifive-mal-9174-3.c 
b/gcc/testsuite/gcc.target/riscv/rvv/base/sifive-mal-9174-3.c
new file mode 100644
index 00000000000..b70db59fc61
--- /dev/null
+++ b/gcc/testsuite/gcc.target/riscv/rvv/base/sifive-mal-9174-3.c
@@ -0,0 +1,46 @@
+/* { dg-do compile } */
+/* { dg-options "-march=rv64gcv -mabi=lp64d -O2 -mcpu=sifive-p870-d 
-mno-fix-sifive-mal-9174" } */
+
+/* -mno-fix-sifive-mal-9174 overrides -mcpu.  */
+
+#include <riscv_vector.h>
+
+vint32m1_t
+tama (int *base, vuint32m1_t index, size_t vl)
+{
+  return __riscv_vluxei32_v_i32m1 (base, index, vl);
+}
+
+vint32m1_t
+tamu (vbool32_t m, int *base, vuint32m1_t index, size_t vl)
+{
+  return __riscv_vluxei32_v_i32m1_m (m, base, index, vl);
+}
+
+vint32m1_t
+tu (int *base, vuint32m1_t index, size_t vl)
+{
+  vint32m1_t merge = __riscv_vreinterpret_v_u32m1_i32m1 (index);
+  return __riscv_vluxei32_v_i32m1_tu (merge, base, index, vl);
+}
+
+vint32m1_t
+tumu (vbool32_t m, int *base, vuint32m1_t index, size_t vl)
+{
+  vint32m1_t merge = __riscv_vreinterpret_v_u32m1_i32m1 (index);
+  return __riscv_vluxei32_v_i32m1_tumu (m, merge, base, index, vl);
+}
+
+vint8mf8_t
+se (void *base, vuint64m1_t index, size_t vl)
+{
+  return __riscv_vluxei64_v_i8mf8 (base, index, vl);
+}
+
+vint8mf8_t
+se_tu (void *base, vuint64m1_t index, vint8mf8_t merge, size_t vl)
+{
+  return __riscv_vluxei64_v_i8mf8_tu (merge, base, index, vl);
+}
+
+/* { dg-final { scan-assembler 
{vl[ou]xei[0-9]+\.v\s+v([0-9]+),\([^)]*\),v\1\M} } } */
diff --git a/gcc/testsuite/gcc.target/riscv/rvv/base/sifive-mal-9174-4.c 
b/gcc/testsuite/gcc.target/riscv/rvv/base/sifive-mal-9174-4.c
new file mode 100644
index 00000000000..ee4df93b424
--- /dev/null
+++ b/gcc/testsuite/gcc.target/riscv/rvv/base/sifive-mal-9174-4.c
@@ -0,0 +1,46 @@
+/* { dg-do compile } */
+/* { dg-options "-march=rv64gcv -mabi=lp64d -O2 -mtune=sifive-p870-d" } */
+
+/* -mtune does not enable the workaround.  */
+
+#include <riscv_vector.h>
+
+vint32m1_t
+tama (int *base, vuint32m1_t index, size_t vl)
+{
+  return __riscv_vluxei32_v_i32m1 (base, index, vl);
+}
+
+vint32m1_t
+tamu (vbool32_t m, int *base, vuint32m1_t index, size_t vl)
+{
+  return __riscv_vluxei32_v_i32m1_m (m, base, index, vl);
+}
+
+vint32m1_t
+tu (int *base, vuint32m1_t index, size_t vl)
+{
+  vint32m1_t merge = __riscv_vreinterpret_v_u32m1_i32m1 (index);
+  return __riscv_vluxei32_v_i32m1_tu (merge, base, index, vl);
+}
+
+vint32m1_t
+tumu (vbool32_t m, int *base, vuint32m1_t index, size_t vl)
+{
+  vint32m1_t merge = __riscv_vreinterpret_v_u32m1_i32m1 (index);
+  return __riscv_vluxei32_v_i32m1_tumu (m, merge, base, index, vl);
+}
+
+vint8mf8_t
+se (void *base, vuint64m1_t index, size_t vl)
+{
+  return __riscv_vluxei64_v_i8mf8 (base, index, vl);
+}
+
+vint8mf8_t
+se_tu (void *base, vuint64m1_t index, vint8mf8_t merge, size_t vl)
+{
+  return __riscv_vluxei64_v_i8mf8_tu (merge, base, index, vl);
+}
+
+/* { dg-final { scan-assembler 
{vl[ou]xei[0-9]+\.v\s+v([0-9]+),\([^)]*\),v\1\M} } } */
-- 
2.54.0

Reply via email to