diff mbox series

[1/2] RISC-V: Add per-CPU errata framework

Message ID 20261105042032.1545001-2-kito.cheng@sifive.com
State New
Headers show
Series RISC-V: Add per-CPU errata and work around SiFive MAL-9174 | expand

Commit Message

Kito Cheng Nov. 5, 2026, 4:20 a.m. UTC
This patch adds the framework to fix errata for specific cores.

- riscv-errata.def lists the errata.  Each one has a -mfix-NAME option.
- RISCV_CORE_ERRATUM in riscv-cores.def lists the errata of a core.
- --with-errata=LIST turns on workarounds by default.

A workaround is on if -mfix-NAME is given.  If neither -mfix-NAME nor
-mno-fix-NAME is given, it is on if the -mcpu core has the erratum or the
erratum is in --with-errata.  -mtune has no effect.

Functions with different errata are not inlined into each other.

gcc/ChangeLog:

	* config.gcc (riscv*-*-*): Handle --with-errata.
	* config/riscv/riscv-cores.def (RISCV_CORE_ERRATUM): New.
	* config/riscv/riscv-errata.def: New file.
	* config/riscv/riscv-opts.h (enum riscv_erratum): New.
	(RISCV_ERRATUM_MASK, RISCV_ERRATUM_P): New.
	* config/riscv/riscv.cc (riscv_errata): New variable.
	(riscv_cpu_errata): New function.
	(riscv_default_errata): New function.
	(riscv_resolve_errata): New function.
	(riscv_can_inline_p): Do not inline functions with different errata.
	(riscv_override_options_internal): Set riscv_errata.
	* config/riscv/riscv.h (riscv_errata): Declare.
	* doc/install.texi (--with-errata): Document.
---
 gcc/config.gcc                    | 19 +++++++++
 gcc/config/riscv/riscv-cores.def  | 14 +++++-
 gcc/config/riscv/riscv-errata.def | 33 ++++++++++++++
 gcc/config/riscv/riscv-opts.h     | 13 ++++++
 gcc/config/riscv/riscv.cc         | 71 +++++++++++++++++++++++++++++++
 gcc/config/riscv/riscv.h          |  1 +
 gcc/doc/install.texi              |  7 +++
 7 files changed, 157 insertions(+), 1 deletion(-)
 create mode 100644 gcc/config/riscv/riscv-errata.def

Comments

Jeff Law Nov. 10, 2026, 5:25 p.m. UTC | #1
On 11/4/26 9:20 PM, Kito Cheng wrote:
> This patch adds the framework to fix errata for specific cores.
>
> - riscv-errata.def lists the errata.  Each one has a -mfix-NAME option.
> - RISCV_CORE_ERRATUM in riscv-cores.def lists the errata of a core.
> - --with-errata=LIST turns on workarounds by default.
>
> A workaround is on if -mfix-NAME is given.  If neither -mfix-NAME nor
> -mno-fix-NAME is given, it is on if the -mcpu core has the erratum or the
> erratum is in --with-errata.  -mtune has no effect.
>
> Functions with different errata are not inlined into each other.
>
> gcc/ChangeLog:
>
> 	* config.gcc (riscv*-*-*): Handle --with-errata.
> 	* config/riscv/riscv-cores.def (RISCV_CORE_ERRATUM): New.
> 	* config/riscv/riscv-errata.def: New file.
> 	* config/riscv/riscv-opts.h (enum riscv_erratum): New.
> 	(RISCV_ERRATUM_MASK, RISCV_ERRATUM_P): New.
> 	* config/riscv/riscv.cc (riscv_errata): New variable.
> 	(riscv_cpu_errata): New function.
> 	(riscv_default_errata): New function.
> 	(riscv_resolve_errata): New function.
> 	(riscv_can_inline_p): Do not inline functions with different errata.
> 	(riscv_override_options_internal): Set riscv_errata.
> 	* config/riscv/riscv.h (riscv_errata): Declare.
> 	* doc/install.texi (--with-errata): Document.
Sashiko noted that if you use -mcpu=native and happen to be on a design 
with an errata in this table that workaround won't take effect.  The 
claim is opts->x_riscv_cpu_string will be NULL in that case.

Sashiko also complained about an LTO issue.

https://gcc.sashiko.dev/#/patchset/20261105042032.1545001-1-kito.cheng%40sifive.com

So the series is OK once you review those findings from Sashiko and take 
the action you think is most appropriate (which can include declaring 
the Sashiko concerns false positives).

Jeff
diff mbox series

Patch

diff --git a/gcc/config.gcc b/gcc/config.gcc
index d5fdbd0b864..75ff79f6e6d 100644
--- a/gcc/config.gcc
+++ b/gcc/config.gcc
@@ -4944,6 +4944,25 @@  case "${target}" in
 			;;
 		esac
 
+		# Handle --with-errata.
+		case "${with_errata}" in
+		"" | none)
+			;;
+		*)
+			errata_def=${srcdir}/config/riscv/riscv-errata.def
+			for erratum in `echo "${with_errata}" | tr ',' ' '`; do
+				if test "x${erratum}" != xall \
+				   && ! grep "^RISCV_ERRATUM(.*\"${erratum}\"" \
+					${errata_def} > /dev/null; then
+					echo "Unknown erratum ${erratum}" \
+					     "in --with-errata" 1>&2
+					exit 1
+				fi
+			done
+			tm_defines="${tm_defines} TARGET_DEFAULT_ERRATA=\\\"${with_errata}\\\""
+			;;
+		esac
+
 		# Handle --with-multilib-list.
 		if test "x${with_multilib_list}" != xdefault; then
 			tmake_file="${tmake_file} riscv/t-withmultilib"
diff --git a/gcc/config/riscv/riscv-cores.def b/gcc/config/riscv/riscv-cores.def
index e8416498d66..725b55dbb37 100644
--- a/gcc/config/riscv/riscv-cores.def
+++ b/gcc/config/riscv/riscv-cores.def
@@ -95,7 +95,15 @@  RISCV_TUNE("arc-v-rmx-100-series", arcv_rmx100, arcv_rmx100_tune_info)
       RISCV_CORE_ALIAS(CANONICAL_NAME, ALIAS_NAME)
 
    CANONICAL_NAME is the normal name of the core, represented as a string.
-   ALIAS_NAME is the alternate name for the core, represented as a string.  */
+   ALIAS_NAME is the alternate name for the core, represented as a string.
+
+   If the core has a hardware erratum that GCC can work around, list it
+   with this macro, once for each erratum:
+
+      RISCV_CORE_ERRATUM(CORE_NAME, ERRATUM)
+
+   CORE_NAME is the canonical name of the core, represented as a string.
+   ERRATUM is the ID of the erratum in riscv-errata.def.  */
 
 #ifndef UNKNOWN_ID
 #define UNKNOWN_ID ((riscv_core_id_t) -1)
@@ -106,6 +114,9 @@  RISCV_TUNE("arc-v-rmx-100-series", arcv_rmx100, arcv_rmx100_tune_info)
 #ifndef RISCV_CORE_ALIAS
 #define RISCV_CORE_ALIAS(CANONICAL_NAME, ALIAS_NAME)
 #endif
+#ifndef RISCV_CORE_ERRATUM
+#define RISCV_CORE_ERRATUM(CORE_NAME, ERRATUM)
+#endif
 
 RISCV_CORE("sifive-e20",      "rv32imc",    "rocket",
 			      /* MVENDORID */ UNKNOWN_ID,
@@ -414,3 +425,4 @@  RISCV_CORE("spacemit-a100",   "rva23u64_zfh_zbc_zbkc_zfbfmin_smepmp_smnpm_"
 
 #undef RISCV_CORE
 #undef RISCV_CORE_ALIAS
+#undef RISCV_CORE_ERRATUM
diff --git a/gcc/config/riscv/riscv-errata.def b/gcc/config/riscv/riscv-errata.def
new file mode 100644
index 00000000000..bcf74faf495
--- /dev/null
+++ b/gcc/config/riscv/riscv-errata.def
@@ -0,0 +1,33 @@ 
+/* List of hardware errata that GCC can work around for RISC-V.
+   Copyright (C) 2026 Free Software Foundation, Inc.
+
+   This file is part of GCC.
+
+   GCC is free software; you can redistribute it and/or modify it
+   under the terms of the GNU General Public License as published by
+   the Free Software Foundation; either version 3, or (at your option)
+   any later version.
+
+   GCC is distributed in the hope that it will be useful, but
+   WITHOUT ANY WARRANTY; without even the implied warranty of
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+   General Public License for more details.
+
+   You should have received a copy of the GNU General Public License
+   along with GCC; see the file COPYING3.  If not see
+   <http://www.gnu.org/licenses/>.  */
+
+/* Before using #include to read this file, define a macro:
+
+      RISCV_ERRATUM(ID, NAME, VAR)
+
+   ID is the identifier of the erratum, RISCV_ERRATUM_<ID> is its index.
+   NAME is the name of the erratum, represented as a string, as used by
+   -mfix-NAME and --with-errata.
+   VAR is the variable of the -mfix-NAME option, which is -1 if the option
+   is not given.
+
+   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.  */
+
+#undef RISCV_ERRATUM
diff --git a/gcc/config/riscv/riscv-opts.h b/gcc/config/riscv/riscv-opts.h
index 8711031d709..7de4102f254 100644
--- a/gcc/config/riscv/riscv-opts.h
+++ b/gcc/config/riscv/riscv-opts.h
@@ -203,4 +203,17 @@  enum
 
 #define BITMASK_NOT_YET_ALLOCATED -1
 
+/* Hardware errata that GCC can work around.  */
+enum riscv_erratum
+{
+#define RISCV_ERRATUM(ID, NAME, VAR) RISCV_ERRATUM_##ID,
+#include "riscv-errata.def"
+  RISCV_ERRATUM_MAX
+};
+
+#define RISCV_ERRATUM_MASK(ID) (HOST_WIDE_INT_1U << RISCV_ERRATUM_##ID)
+
+/* True if we work around erratum ID.  */
+#define RISCV_ERRATUM_P(ID) ((riscv_errata & RISCV_ERRATUM_MASK (ID)) != 0)
+
 #endif /* ! GCC_RISCV_OPTS_H */
diff --git a/gcc/config/riscv/riscv.cc b/gcc/config/riscv/riscv.cc
index 7aca11d4daa..cb40a2004cf 100644
--- a/gcc/config/riscv/riscv.cc
+++ b/gcc/config/riscv/riscv.cc
@@ -329,6 +329,9 @@  bool riscv_vector_unaligned_access_p;
 /* Whether user explicitly passed -mstrict-align.  */
 bool riscv_user_wants_strict_align;
 
+/* Mask of the errata to work around, see RISCV_ERRATUM_P.  */
+unsigned HOST_WIDE_INT riscv_errata;
+
 /* Stack alignment to assume/maintain.  */
 unsigned riscv_stack_boundary;
 
@@ -1345,6 +1348,69 @@  riscv_parse_tune (const char *tune_string, bool null_p)
   return riscv_tune_info_table;
 }
 
+/* Return the mask of errata of the processor named by -mcpu=CPU_STRING.
+   CPU_STRING may be NULL.  */
+
+static unsigned HOST_WIDE_INT
+riscv_cpu_errata (const char *cpu_string)
+{
+  const riscv_cpu_info *cpu = cpu_string ? riscv_find_cpu (cpu_string) : NULL;
+  if (!cpu)
+    return 0;
+
+  unsigned HOST_WIDE_INT errata = 0;
+#define RISCV_CORE_ERRATUM(CORE_NAME, ERRATUM)		\
+  if (strcmp (cpu->name, CORE_NAME) == 0)		\
+    errata |= RISCV_ERRATUM_MASK (ERRATUM);
+#include "riscv-cores.def"
+  return errata;
+}
+
+/* Return the mask of errata given by --with-errata.  */
+
+static unsigned HOST_WIDE_INT
+riscv_default_errata (void)
+{
+  unsigned HOST_WIDE_INT errata = 0;
+#ifdef TARGET_DEFAULT_ERRATA
+  /* config.gcc has checked the names.  */
+  const char *p = TARGET_DEFAULT_ERRATA;
+  while (*p)
+    {
+      const char *end = strchr (p, ',');
+      size_t len = end ? (size_t) (end - p) : strlen (p);
+#define RISCV_ERRATUM(ID, NAME, VAR)				\
+      if ((len == 3 && strncmp (p, "all", 3) == 0)		\
+	  || (len == strlen (NAME) && strncmp (p, NAME, len) == 0))	\
+	errata |= RISCV_ERRATUM_MASK (ID);
+#include "riscv-errata.def"
+      p += len;
+      if (*p == ',')
+	p++;
+    }
+#endif
+  return errata;
+}
+
+/* Return the mask of errata to work around for OPTS, which is a
+   gcc_options or a cl_target_option.  An explicit -m[no-]fix-NAME wins,
+   otherwise use the errata of -mcpu and of --with-errata.  */
+
+template <typename T>
+static unsigned HOST_WIDE_INT
+riscv_resolve_errata (const T *opts)
+{
+  unsigned HOST_WIDE_INT errata
+    = riscv_cpu_errata (opts->x_riscv_cpu_string) | riscv_default_errata ();
+#define RISCV_ERRATUM(ID, NAME, VAR)		\
+  if (opts->x_##VAR == 0)			\
+    errata &= ~RISCV_ERRATUM_MASK (ID);		\
+  else if (opts->x_##VAR == 1)			\
+    errata |= RISCV_ERRATUM_MASK (ID);
+#include "riscv-errata.def"
+  return errata;
+}
+
 /* Helper function for riscv_build_integer; arguments are as for
    riscv_build_integer.  */
 
@@ -9782,6 +9848,9 @@  riscv_can_inline_p (tree caller, tree callee)
       != callee_opts->x_rvv_vector_strict_align)
     return false;
 
+  if (riscv_resolve_errata (caller_opts) != riscv_resolve_errata (callee_opts))
+    return false;
+
   if (riscv_zilsd_required_align (caller_opts)
       != riscv_zilsd_required_align (callee_opts))
     return false;
@@ -12558,6 +12627,8 @@  riscv_override_options_internal (struct gcc_options *opts)
       && cpu->tune_param->slow_unaligned_access)
     opts->x_target_flags |= MASK_STRICT_ALIGN;
 
+  riscv_errata = riscv_resolve_errata (opts);
+
   /* If the user hasn't specified a branch cost, use the processor's
      default.  */
   if (opts->x_riscv_branch_cost == 0)
diff --git a/gcc/config/riscv/riscv.h b/gcc/config/riscv/riscv.h
index 7ba2a726ac4..8da6d437c91 100644
--- a/gcc/config/riscv/riscv.h
+++ b/gcc/config/riscv/riscv.h
@@ -1240,6 +1240,7 @@  extern const enum reg_class riscv_regno_to_class[];
 extern bool riscv_slow_unaligned_access_p;
 extern bool riscv_vector_unaligned_access_p;
 extern bool riscv_user_wants_strict_align;
+extern unsigned HOST_WIDE_INT riscv_errata;
 extern unsigned riscv_stack_boundary;
 extern unsigned riscv_bytes_per_vector_chunk;
 extern poly_uint16 riscv_vector_chunks;
diff --git a/gcc/doc/install.texi b/gcc/doc/install.texi
index 840e6553db6..87490157fd6 100644
--- a/gcc/doc/install.texi
+++ b/gcc/doc/install.texi
@@ -1923,6 +1923,13 @@  Produce code conforming to version 20191213.
 @end table
 In the absence of this configuration option the default version is 20191213.
 
+@item --with-errata=@var{list}
+On RISC-V targets, enable the workarounds for the hardware errata in the
+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.
+
 @item --enable-__cxa_atexit
 Define if you want to use @code{__cxa_atexit}, rather than @code{atexit},
 to register C++ destructors for local statics and global objects.