[gcc r16-7983] [PATCH] RISC-V: Fix TARGET_CHECK_TARGET_CLONE_VERSION implementation

Jeff Law law@gcc.gnu.org
Tue Mar 10 14:01:13 GMT 2026


https://gcc.gnu.org/g:5a0016576f04ffbde19a4895063da4798fe0dd5c

commit r16-7983-g5a0016576f04ffbde19a4895063da4798fe0dd5c
Author: Yangyu Chen <cyy@cyyself.name>
Date:   Tue Mar 10 08:00:10 2026 -0600

    [PATCH] RISC-V: Fix TARGET_CHECK_TARGET_CLONE_VERSION implementation
    
    Currently, the implementation of TARGET_CHECK_TARGET_CLONE_VERSION for
    RISC-V is incomplete and will emit an error for invalid target_clones
    versions. This can be problematic during transitions to new extensions,
    as it may break builds that have not yet updated their target_clones
    versions.
    
    This commit implements TARGET_CHECK_TARGET_CLONE_VERSION for RISC-V, by
    using the riscv_process_target_version_str with loc set to NULL to
    prevent emitting errors for invalid versions. This allows for a smoother
    transition to new extensions, while still providing a mechanism for
    checking target_clones versions when needed.
    
    Signed-off-by: Yangyu Chen <cyy@cyyself.name>
    
    ---
    Note: I think it should be safe to push to GCC-16 with the new test, to align
    FMV with aarch64 for invalid version string. Future ISA extensions may cause
    build breakage if the version string is not updated, and this test can help
    catch such issues early.
    
    Aarch64 in GCC-16 already merged this new feature. X86 [1] may also merge this
    feature in GCC-16, but it is not yet clear. If X86 also merges this feature in
    GCC-16, then it would be more consistent to also merge the RISC-V
    implementation in GCC-16.
    
    [1] https://patchwork.sourceware.org/project/gcc/patch/tencent_E83AEE534C5D8845A3371FAAA091B6832306@qq.com/
    
    gcc/ChangeLog:
    
            * config/riscv/riscv.cc (riscv_check_target_clone_version):
            Fix TARGET_CHECK_TARGET_CLONE_VERSION implementation.
    
    gcc/testsuite/ChangeLog:
    
            * gcc.target/riscv/mvc-warning1.c: New test.

Diff:
---
 gcc/config/riscv/riscv.cc                     | 29 +++++++++++++++++++--------
 gcc/testsuite/gcc.target/riscv/mvc-warning1.c |  9 +++++++++
 2 files changed, 30 insertions(+), 8 deletions(-)

diff --git a/gcc/config/riscv/riscv.cc b/gcc/config/riscv/riscv.cc
index 27fbe6029f53..282cf71ad61c 100644
--- a/gcc/config/riscv/riscv.cc
+++ b/gcc/config/riscv/riscv.cc
@@ -15045,16 +15045,29 @@ riscv_compare_version_priority (tree decl1, tree decl2)
 bool
 riscv_check_target_clone_version (string_slice str, location_t *loc_p)
 {
-  struct riscv_feature_bits mask;
-  int prio;
+  if (str == "default")
+    return true;
 
-  /* Currently it is not possible to parse without emitting errors on failure
-     so do not reject on a failed parse, as this would then emit two
-     diagnostics.  Instead let errors be emitted which will halt
-     compilation.  */
-  parse_features_for_version (str, loc_p, mask, prio);
+  struct cl_target_option cur_target;
+  cl_target_option_save (&cur_target, &global_options,
+			 &global_options_set);
 
-  return true;
+  struct cl_target_option *default_opts
+    = TREE_TARGET_OPTION (target_option_default_node);
+  cl_target_option_restore (&global_options, &global_options_set,
+			    default_opts);
+
+  bool ok = riscv_process_target_version_str (str, NULL);
+
+  cl_target_option_restore (&global_options, &global_options_set,
+			    &cur_target);
+
+  if (!ok && loc_p)
+    warning_at (*loc_p, OPT_Wattributes,
+		"invalid version %qB for %<target_clones%> attribute",
+		&str);
+
+  return ok;
 }
 
 /* Implement TARGET_MANGLE_DECL_ASSEMBLER_NAME, to add function multiversioning
diff --git a/gcc/testsuite/gcc.target/riscv/mvc-warning1.c b/gcc/testsuite/gcc.target/riscv/mvc-warning1.c
new file mode 100644
index 000000000000..0ba4b182070c
--- /dev/null
+++ b/gcc/testsuite/gcc.target/riscv/mvc-warning1.c
@@ -0,0 +1,9 @@
+/* { dg-do compile } */
+/* { dg-require-ifunc "" } */
+/* { dg-options "-O0" } */
+
+__attribute__((target_clones("default", "random-arch-string")))
+int foo () /* { dg-warning "invalid version .*random-arch-string.*target_clones" } */
+{
+	return 1;
+}


More information about the Gcc-cvs mailing list