Hi Andrea,

Thanks for the review.

> On Wed, Nov 18, 2026 at 6:34 PM Jin Ma <[email protected]> wrote:
> >
> > Small switches can pay for a bounds check, a table load and an
> > indirect jump.  Raise the default speed threshold to 11 so that
> > CPython 3.11.15 uses conditional branches for its eight-way character
> > set dispatch.  Keep the generic threshold when optimizing for size.
> >
> > Use a threshold of 5 in pr126236.c so its eight-way switch 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.
>
> Most likely you will get an improvement in SPEC CPU 2026's cypthon too.

Thanks for the reminder. I'll also test 714.cpython_r from SPEC CPU 2026
to see whether it benefits from this change.

>
> >
> > Assisted-by: qwenwork
> >
> > gcc/ChangeLog:
> >
> >         * config/riscv/riscv.cc (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/jump-table-large-code-model.c: Keep the jump 
> > table.
> >         * gcc.target/riscv/switch-qi.c: Likewise.
> >         * gcc.target/riscv/switch-si.c: Likewise.
> >         * gcc.target/riscv/rvv/vsetvl/pr126236.c: Keep the shared jump 
> > table.
> >         * gcc.target/riscv/rvv/vsetvl/vlmax_back_prop-4.c: Keep the jump
> >         table and disable loop unrolling.
> > ---
> >  gcc/config/riscv/riscv.cc                     | 13 ++++++
> >  .../riscv/case-values-threshold-1.c           | 40 +++++++++++++++++++
> >  .../riscv/case-values-threshold-2.c           |  6 +++
> >  .../riscv/case-values-threshold-3.c           |  6 +++
> >  .../riscv/case-values-threshold-4.c           |  8 ++++
> >  .../riscv/case-values-threshold-5.c           |  8 ++++
> >  .../riscv/jump-table-large-code-model.c       |  1 +
> >  .../gcc.target/riscv/rvv/vsetvl/pr126236.c    |  4 ++
> >  .../riscv/rvv/vsetvl/vlmax_back_prop-4.c      |  4 ++
> >  gcc/testsuite/gcc.target/riscv/switch-qi.c    |  2 +-
> >  gcc/testsuite/gcc.target/riscv/switch-si.c    |  2 +-
> >  11 files changed, 92 insertions(+), 2 deletions(-)
> >  create mode 100644 gcc/testsuite/gcc.target/riscv/case-values-threshold-1.c
> >  create mode 100644 gcc/testsuite/gcc.target/riscv/case-values-threshold-2.c
> >  create mode 100644 gcc/testsuite/gcc.target/riscv/case-values-threshold-3.c
> >  create mode 100644 gcc/testsuite/gcc.target/riscv/case-values-threshold-4.c
> >  create mode 100644 gcc/testsuite/gcc.target/riscv/case-values-threshold-5.c
> >
> > diff --git a/gcc/config/riscv/riscv.cc b/gcc/config/riscv/riscv.cc
> > index 3e0aa58d3f3..935402fdd67 100644
> > --- a/gcc/config/riscv/riscv.cc
> > +++ b/gcc/config/riscv/riscv.cc
> > @@ -2464,6 +2464,16 @@ riscv_cannot_force_const_mem (machine_mode mode 
> > ATTRIBUTE_UNUSED, rtx x)
> >    return false;
> >  }
> >
> > +/* Implement TARGET_CASE_VALUES_THRESHOLD.  A jump table needs a bounds
> > +   check, a table lookup and an indirect jump.  Prefer branches for small
> > +   switches when optimizing for speed.  */
> > +
> > +static unsigned int
> > +riscv_case_values_threshold (void)
> > +{
> > +  return optimize_size ? default_case_values_threshold () : 11;
>
> Looking at what aarch64 does, it allows the tuning cpu to also
> override this at -O3 (still using 11 at -O2; 8 at -Os). I wonder if
> you think there might be a tmicro-arch that might benefit from a
> higher value?

If I understand correctly, you're asking whether it's worth letting
-mtune select a different jump-table threshold at -O3 for some RISC-V
cores. I think that could help. For example, on a core where indirect
jumps are expensive, conditional branches may be faster even for
switches with more cases.

Of the thresholds I tried, 11 gave the best overall results across the
CPUs I tested. I haven't seen a clear regression at the suite level.
It's an empirical choice, and the best value may depend on the
microarchitecture, ISA extensions, and workload. I don't yet have
measurements that would justify a different threshold for a particular
core.

I'd use 11 as the default in the RISC-V hook for now. We can add an
override if measurements show that a particular core benefits from a
different value. If you'd prefer to support per-core thresholds in this
patch, I'm happy to send a v2 with the threshold in the RISC-V tuning
data.

BR,
Jin

Reply via email to