https://gcc.gnu.org/g:2ce8902057a9a0ee863c5d5e17e35fddf5aaf198
commit r17-6627-g2ce8902057a9a0ee863c5d5e17e35fddf5aaf198 Author: Kito Cheng <[email protected]> Date: Sat Oct 24 22:27:41 2026 +0800 RISC-V: Add per-CPU errata framework 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_ERRATUM_ALIAS gives an erratum a second name. - 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. Diff: --- gcc/config.gcc | 19 ++++++++++ gcc/config/riscv/riscv-cores.def | 14 +++++++- gcc/config/riscv/riscv-errata.def | 46 ++++++++++++++++++++++++ gcc/config/riscv/riscv-opts.h | 13 +++++++ gcc/config/riscv/riscv.cc | 74 +++++++++++++++++++++++++++++++++++++++ gcc/config/riscv/riscv.h | 1 + gcc/doc/install.texi | 7 ++++ 7 files changed, 173 insertions(+), 1 deletion(-) diff --git a/gcc/config.gcc b/gcc/config.gcc index 5821df3eb1e7..cf281fb1b05d 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\(_ALIAS\)\{0,1\}(.*\"${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 85c88a738c68..7d9995d92787 100644 --- a/gcc/config/riscv/riscv-cores.def +++ b/gcc/config/riscv/riscv-cores.def @@ -97,7 +97,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) @@ -108,6 +116,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, @@ -416,3 +427,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 000000000000..66787ab9f7d0 --- /dev/null +++ b/gcc/config/riscv/riscv-errata.def @@ -0,0 +1,46 @@ +/* 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. + + An erratum that has more than one name can also use: + + RISCV_ERRATUM_ALIAS(ALIAS, ID) + + ALIAS is the other name, represented as a string, as used by + --with-errata. Its -mfix-ALIAS option is an Alias of -mfix-NAME in + riscv.opt. */ + +#ifndef RISCV_ERRATUM_ALIAS +#define RISCV_ERRATUM_ALIAS(ALIAS, ID) +#endif + +#undef RISCV_ERRATUM +#undef RISCV_ERRATUM_ALIAS diff --git a/gcc/config/riscv/riscv-opts.h b/gcc/config/riscv/riscv-opts.h index 87faf5bc9f80..18d58d7fdc7c 100644 --- a/gcc/config/riscv/riscv-opts.h +++ b/gcc/config/riscv/riscv-opts.h @@ -205,4 +205,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 961ed75a41da..0bec5efc94b1 100644 --- a/gcc/config/riscv/riscv.cc +++ b/gcc/config/riscv/riscv.cc @@ -330,6 +330,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; @@ -1383,6 +1386,72 @@ 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); +#define RISCV_ERRATUM_ALIAS(ALIAS, ID) \ + if (len == strlen (ALIAS) && strncmp (p, ALIAS, 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. */ @@ -9901,6 +9970,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; @@ -12681,6 +12753,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 7dc39764ec88..f14311cdc224 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 f24968af10d6..71272395fc1f 100644 --- a/gcc/doc/install.texi +++ b/gcc/doc/install.texi @@ -1928,6 +1928,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.
