https://gcc.gnu.org/g:0edfdff3040104bb95feebf4c83d4eed71564d5f

commit r17-6547-g0edfdff3040104bb95feebf4c83d4eed71564d5f
Author: Jin Ma <[email protected]>
Date:   Thu Nov 19 23:49:44 2026 +0800

    RISC-V: Tune the jump-table threshold for C9501 and SpacemiT X100
    
    Set the jump-table threshold to 11 when optimizing for speed on C9501
    and SpacemiT X100 (K3).  The eight-way character set dispatch in
    CPython 3.11.15 then uses conditional branches, avoiding a table load
    and an indirect jump.  Keep the generic threshold for other cores and
    for functions compiled with -Os or -Oz.
    
    Keep the eight-way switch in pr126236.c eligible for a jump table so
    the test continues to exercise vsetvl on shared jump-table edges.
    
    PyBench relative gains are positive when elapsed time decreases.
    
      Case                             K3    C9501
      --------------------------  -------  -------
      Geomean (58 cases)           +0.89%   +1.35%
    
    Cases that ran more than 5% faster or slower on C9501
    
      Case                             K3    C9501
      --------------------------  -------  -------
      CompareUnicode               +1.57%  +14.02%
      UnicodeSlicing               -1.53%  +12.21%
      CompareFloats               +17.54%  +10.84%
      CreateStringsWithConcat      -2.68%  +10.38%
      StringSlicing                -1.85%  +10.32%
      CreateUnicodeWithConcat      -3.17%   +9.97%
      TryExcept                   -13.45%   +9.76%
      SmallLists                   +1.33%   +5.34%
      CompareIntegers             +26.47%   +5.30%
      ComplexPythonFunctionCalls   +0.67%   -5.26%
    
    SPEC CPU2017 Integer testing on C9501 showed only a very small gain,
    mainly in perlbench_r, with no clear overall regression.
    
    Assisted-by: qwenwork
    
    gcc/ChangeLog:
    
            * config/riscv/riscv.cc (riscv_tune_param): Add 
case_values_threshold.
            (generic_ooo_tune_info): Use the generic threshold.
            (xt_c9501_tune_info, spacemit_x100_tune_info): Use 11.
            (riscv_case_values_threshold): New function.
            (TARGET_CASE_VALUES_THRESHOLD): Define.
    
    gcc/testsuite/ChangeLog:
    
            * gcc.target/riscv/case-values-threshold-1.c: New test.
            * gcc.target/riscv/case-values-threshold-2.c: New test.
            * gcc.target/riscv/case-values-threshold-3.c: New test.
            * gcc.target/riscv/case-values-threshold-4.c: New test.
            * gcc.target/riscv/case-values-threshold-5.c: New test.
            * gcc.target/riscv/case-values-threshold-6.c: New test.
            * gcc.target/riscv/rvv/vsetvl/pr126236.c: Keep the shared jump 
table.

Diff:
---
 gcc/config/riscv/riscv.cc                          | 17 +++++++++
 .../gcc.target/riscv/case-values-threshold-1.c     | 40 ++++++++++++++++++++++
 .../gcc.target/riscv/case-values-threshold-2.c     |  6 ++++
 .../gcc.target/riscv/case-values-threshold-3.c     |  6 ++++
 .../gcc.target/riscv/case-values-threshold-4.c     |  8 +++++
 .../gcc.target/riscv/case-values-threshold-5.c     |  8 +++++
 .../gcc.target/riscv/case-values-threshold-6.c     |  7 ++++
 .../gcc.target/riscv/rvv/vsetvl/pr126236.c         |  4 +++
 8 files changed, 96 insertions(+)

diff --git a/gcc/config/riscv/riscv.cc b/gcc/config/riscv/riscv.cc
index 5ab6c5bf85f9..55714826216d 100644
--- a/gcc/config/riscv/riscv.cc
+++ b/gcc/config/riscv/riscv.cc
@@ -300,6 +300,7 @@ struct riscv_tune_param
   const char *jump_align;
   const char *loop_align;
   bool prefer_agnostic;
+  unsigned short case_values_threshold = 0;
   unsigned short int_reassoc_width = 1;
   unsigned short fp_reassoc_width = 1;
   unsigned short vec_reassoc_width = 1;
@@ -727,6 +728,7 @@ static const struct riscv_tune_param generic_ooo_tune_info 
= {
   NULL,                                                /* jump_align */
   NULL,                                                /* loop_align */
   true,                                                /* prefer-agnostic.  */
+  0,                                           /* case_values_threshold.  */
   2,                                           /* int_reassoc_width.  */
   2,                                           /* fp_reassoc_width.  */
   1,                                           /* vec_reassoc_width.  */
@@ -815,6 +817,7 @@ static const struct riscv_tune_param xt_c9501_tune_info = {
   "8",                                         /* jump_align */
   "16",                                                /* loop_align */
   true,                                                /* prefer-agnostic.  */
+  11,                                          /* case_values_threshold.  */
   3,                                           /* int_reassoc_width.  */
   2,                                           /* fp_reassoc_width.  */
   1,                                           /* vec_reassoc_width.  */
@@ -1019,6 +1022,7 @@ static const struct riscv_tune_param 
spacemit_x100_tune_info = {
   NULL,                                                /* jump_align */
   NULL,                                                /* loop_align */
   true,                                                /* prefer-agnostic.  */
+  11,                                          /* case_values_threshold.  */
 };
 
 /* Costs to use when optimizing for Andes 23 series.  */
@@ -2464,6 +2468,16 @@ riscv_cannot_force_const_mem (machine_mode mode 
ATTRIBUTE_UNUSED, rtx x)
   return false;
 }
 
+/* Implement TARGET_CASE_VALUES_THRESHOLD.  */
+
+static unsigned int
+riscv_case_values_threshold (void)
+{
+  if (!optimize_size && tune_param->case_values_threshold)
+    return tune_param->case_values_threshold;
+  return default_case_values_threshold ();
+}
+
 /* Return true if register REGNO is a valid base register for mode MODE.
    STRICT_P is true if REG_OK_STRICT is in effect.  */
 
@@ -17456,6 +17470,9 @@ riscv_memtag_tag_bitsize ()
 #undef TARGET_CANNOT_FORCE_CONST_MEM
 #define TARGET_CANNOT_FORCE_CONST_MEM riscv_cannot_force_const_mem
 
+#undef TARGET_CASE_VALUES_THRESHOLD
+#define TARGET_CASE_VALUES_THRESHOLD riscv_case_values_threshold
+
 #undef TARGET_LEGITIMATE_CONSTANT_P
 #define TARGET_LEGITIMATE_CONSTANT_P riscv_legitimate_constant_p
 
diff --git a/gcc/testsuite/gcc.target/riscv/case-values-threshold-1.c 
b/gcc/testsuite/gcc.target/riscv/case-values-threshold-1.c
new file mode 100644
index 000000000000..a61d9605b66d
--- /dev/null
+++ b/gcc/testsuite/gcc.target/riscv/case-values-threshold-1.c
@@ -0,0 +1,40 @@
+/* { dg-do compile } */
+/* { dg-options "-O2 -mtune=xt-c9501fdvt -ffat-lto-objects 
-fdump-tree-switchlower1" } */
+
+#ifndef NUM_CASES
+#define NUM_CASES 8
+#endif
+
+/* Give each case a distinct destination so that switch conversion
+   cannot replace the dispatch with a table of return values.  */
+extern int f0 (void), f1 (void), f2 (void), f3 (void);
+extern int f4 (void), f5 (void), f6 (void), f7 (void);
+extern int f8 (void), f9 (void), f10 (void);
+
+int
+dispatch (int x)
+{
+  switch (x)
+    {
+    case 0: return f0 ();
+    case 1: return f1 ();
+    case 2: return f2 ();
+    case 3: return f3 ();
+    case 4: return f4 ();
+    case 5: return f5 ();
+    case 6: return f6 ();
+    case 7: return f7 ();
+#if NUM_CASES >= 9
+    case 8: return f8 ();
+#endif
+#if NUM_CASES >= 10
+    case 9: return f9 ();
+#endif
+#if NUM_CASES >= 11
+    case 10: return f10 ();
+#endif
+    default: return -1;
+    }
+}
+
+/* { dg-final { scan-tree-dump "case clusters: 0 1 2 3 4 5 6 7" "switchlower1" 
} } */
diff --git a/gcc/testsuite/gcc.target/riscv/case-values-threshold-2.c 
b/gcc/testsuite/gcc.target/riscv/case-values-threshold-2.c
new file mode 100644
index 000000000000..6e08579c333f
--- /dev/null
+++ b/gcc/testsuite/gcc.target/riscv/case-values-threshold-2.c
@@ -0,0 +1,6 @@
+/* { dg-do compile } */
+/* { dg-options "-O3 -mtune=spacemit-x100 -ffat-lto-objects 
-fdump-tree-switchlower1 --param=case-values-threshold=5" } */
+
+#include "case-values-threshold-1.c"
+
+/* { dg-final { scan-tree-dump "case clusters: JT:0-7" "switchlower1" } } */
diff --git a/gcc/testsuite/gcc.target/riscv/case-values-threshold-3.c 
b/gcc/testsuite/gcc.target/riscv/case-values-threshold-3.c
new file mode 100644
index 000000000000..2eed0b39cc44
--- /dev/null
+++ b/gcc/testsuite/gcc.target/riscv/case-values-threshold-3.c
@@ -0,0 +1,6 @@
+/* { dg-do compile } */
+/* { dg-options "-Os -mtune=xt-c9501fdvt -ffat-lto-objects 
-fdump-tree-switchlower1" } */
+
+#include "case-values-threshold-1.c"
+
+/* { dg-final { scan-tree-dump "case clusters: JT:0-7" "switchlower1" } } */
diff --git a/gcc/testsuite/gcc.target/riscv/case-values-threshold-4.c 
b/gcc/testsuite/gcc.target/riscv/case-values-threshold-4.c
new file mode 100644
index 000000000000..991294111a85
--- /dev/null
+++ b/gcc/testsuite/gcc.target/riscv/case-values-threshold-4.c
@@ -0,0 +1,8 @@
+/* { dg-do compile } */
+/* { dg-options "-O3 -mtune=spacemit-x100 -ffat-lto-objects 
-fdump-tree-switchlower1" } */
+
+/* Ten cases are just below the speed threshold.  */
+#define NUM_CASES 10
+#include "case-values-threshold-1.c"
+
+/* { dg-final { scan-tree-dump "case clusters: 0 1 2 3 4 5 6 7 8 9" 
"switchlower1" } } */
diff --git a/gcc/testsuite/gcc.target/riscv/case-values-threshold-5.c 
b/gcc/testsuite/gcc.target/riscv/case-values-threshold-5.c
new file mode 100644
index 000000000000..e360503e94eb
--- /dev/null
+++ b/gcc/testsuite/gcc.target/riscv/case-values-threshold-5.c
@@ -0,0 +1,8 @@
+/* { dg-do compile } */
+/* { dg-options "-O3 -mtune=spacemit-x100 -ffat-lto-objects 
-fdump-tree-switchlower1" } */
+
+/* Eleven cases meet the speed threshold.  */
+#define NUM_CASES 11
+#include "case-values-threshold-1.c"
+
+/* { dg-final { scan-tree-dump "case clusters: JT:0-10" "switchlower1" } } */
diff --git a/gcc/testsuite/gcc.target/riscv/case-values-threshold-6.c 
b/gcc/testsuite/gcc.target/riscv/case-values-threshold-6.c
new file mode 100644
index 000000000000..99f548205429
--- /dev/null
+++ b/gcc/testsuite/gcc.target/riscv/case-values-threshold-6.c
@@ -0,0 +1,7 @@
+/* { dg-do compile } */
+/* { dg-options "-O3 -mtune=rocket -ffat-lto-objects -fdump-tree-switchlower1" 
} */
+
+/* Rocket uses the generic threshold.  */
+#include "case-values-threshold-1.c"
+
+/* { dg-final { scan-tree-dump "case clusters: JT:0-7" "switchlower1" } } */
diff --git a/gcc/testsuite/gcc.target/riscv/rvv/vsetvl/pr126236.c 
b/gcc/testsuite/gcc.target/riscv/rvv/vsetvl/pr126236.c
index 9e84d5837af2..f6c2458d73af 100644
--- a/gcc/testsuite/gcc.target/riscv/rvv/vsetvl/pr126236.c
+++ b/gcc/testsuite/gcc.target/riscv/rvv/vsetvl/pr126236.c
@@ -2,6 +2,10 @@
 /* { dg-do run { target { riscv_v } } } */
 /* { dg-additional-options "-std=c99 -O2 -fduplicate-switch-dispatch 
-mno-explicit-relocs -fdump-rtl-compgotos" } */
 
+/* Keep the eight-way switch eligible for a jump table so vsetvl sees
+   incoming edges from dispatches that share the table.  */
+/* { dg-additional-options "--param=case-values-threshold=5" } */
+
 /* Execute each ordered pair of handlers with different vector configurations.
    The reference uses the same unsigned operations without vectorization.  */

Reply via email to