target/riscv/tcg/insn_trans/trans_rvv.c.inc | 25 ++++++++++++++++++++----- 1 file changed, 20 insertions(+), 5 deletions(-)
This patch uses gen_goto_tb() to reduce the overhead of the
unconditional lookup_and_goto_ptr() exit currently generated
for vsetvli.
The shared do_vsetvl() function handles both vsetvl and vsetvli.
The two instructions differ primarily in the source of their vtype
operand: vsetvl takes it from a register, while vsetvli provides it
as an immediate.
When translating vsetvli, the immediate vtype provides information
that can be used to optimize the exit generated by do_vsetvl().
To use gen_goto_tb(), each jump slot must correspond to a deterministic
successor-TB lookup key (pc, flags, cflags, cs_base) for a given source
TB. The relevant state changes are confined to the vector-related
translation state: VILL, SEW, ALTFMT, VSTART_EQ_ZERO, VS, LMUL, VTA,
VMA, and VL_EQ_VLMAX.
For the vsetvli forms optimized by this patch, given the CPU
configuration and source TB context, the immediate vtype and existing
state update flow determine the other relevant translation state.
Only VL_EQ_VLMAX can vary with AVL. This patch therefore uses separate
jump slots depending on whether the returned VL equals VLMAX,
enabling direct TB linking after vsetvli.
The register-vtype vsetvl instruction retains the existing lookup
path, as its vtype is not known at translation time.
Measured on an AMD Ryzen AI 9 H 365 (x86-64) host, using qemu-riscv64
with -cpu rv64,v=true,vlen=128,elen=64. A RISC-V GCC 14.2.0 cc1 built
for rv64gcv compiles the packages with
-O2 -g0 -march=rv64gcv -mabi=lp64d. Only cc1 runs under emulation;
make, the compiler driver and cross-binutils run on the host.
Each variant is warmed up once, followed by three paired clean builds
in AB/BA/AB order, using -j1 and fixed host CPU affinity (CPU 7).
The SQLite workload builds the amalgamation and command-line shell.
Median build wall times are:
Before (s) After (s) Wall time reduction
LZ4 1.10.0 125.120 123.867 1.00%
zlib 1.3.1 70.269 69.911 0.51%
SQLite 3.45.1 438.322 433.213 1.17%
In a separate untimed test, a loop executes two vsetvli instructions
per iteration for 37 iterations. Host-side GDB entry breakpoints show
that helper_lookup_tb_ptr() entries at the two sequential successor
PCs fall from 74 to 0.
Signed-off-by: WX Chen <wxchen0913@gmail.com>
---
target/riscv/tcg/insn_trans/trans_rvv.c.inc | 25 ++++++++++++++++++++-----
1 file changed, 20 insertions(+), 5 deletions(-)
diff --git a/target/riscv/tcg/insn_trans/trans_rvv.c.inc b/target/riscv/tcg/insn_trans/trans_rvv.c.inc
index 7e1295f33e7d4b7a2ff26cbf428f6b5d9eb682ff..5e9a861b6af1565485e9be89b393d0d64080a662 100644
--- a/target/riscv/tcg/insn_trans/trans_rvv.c.inc
+++ b/target/riscv/tcg/insn_trans/trans_rvv.c.inc
@@ -191,7 +191,8 @@ static bool require_noover(const int8_t dst, const int8_t dst_lmul,
return !is_overlapped(dst, dst_size, src, src_size);
}
-static bool do_vsetvl(DisasContext *s, int rd, int rs1, TCGv s2)
+/* A negative vtype denotes the register-supplied form. */
+static bool do_vsetvl(DisasContext *s, int rd, int rs1, TCGv s2, int vtype)
{
TCGv s1, dst;
@@ -215,8 +216,22 @@ static bool do_vsetvl(DisasContext *s, int rd, int rs1, TCGv s2)
gen_set_gpr(s, rd, dst);
finalize_rvv_inst(s);
- gen_update_pc(s, s->cur_insn_len);
- lookup_and_goto_ptr(s);
+ if (vtype >= 0 && (rd != 0 || rs1 != 0)) {
+ uint32_t vsew = FIELD_EX32(vtype, VTYPE, VSEW);
+ int8_t lmul = sextract32(FIELD_EX32(vtype, VTYPE, VLMUL), 0, 3);
+ uint32_t vlmax = vext_get_vlmax(s->cfg_ptr->vlenb, vsew, lmul);
+ target_ulong pc_save = s->pc_save;
+ TCGLabel *not_vlmax = gen_new_label();
+
+ tcg_gen_brcondi_tl(TCG_COND_NE, dst, vlmax, not_vlmax);
+ gen_goto_tb(s, 0, s->cur_insn_len);
+ s->pc_save = pc_save;
+ gen_set_label(not_vlmax);
+ gen_goto_tb(s, 1, s->cur_insn_len);
+ } else {
+ gen_update_pc(s, s->cur_insn_len);
+ lookup_and_goto_ptr(s);
+ }
s->base.is_jmp = DISAS_NORETURN;
return true;
}
@@ -244,13 +259,13 @@ static bool do_vsetivli(DisasContext *s, int rd, TCGv s1, TCGv s2)
static bool trans_vsetvl(DisasContext *s, arg_vsetvl *a)
{
TCGv s2 = get_gpr(s, a->rs2, EXT_ZERO);
- return do_vsetvl(s, a->rd, a->rs1, s2);
+ return do_vsetvl(s, a->rd, a->rs1, s2, -1);
}
static bool trans_vsetvli(DisasContext *s, arg_vsetvli *a)
{
TCGv s2 = tcg_constant_tl(a->zimm);
- return do_vsetvl(s, a->rd, a->rs1, s2);
+ return do_vsetvl(s, a->rd, a->rs1, s2, a->zimm);
}
static bool trans_vsetivli(DisasContext *s, arg_vsetivli *a)
---
base-commit: a3565937b39779d4e3704298095f566e7559ad53
change-id: 20261104-research-vsetvli-direct-chain-20261104-206bc2104ddb
Best regards,
--
WX Chen <wxchen0913@gmail.com>
Hi Wu Xing,
Thanks for the contribution! I have few comments for you.
On Wed, 04 Nov 2026 15:55:24 +0800 WX Chen <wxchen0913@gmail.com> wrote:
> This patch uses gen_goto_tb() to reduce the overhead of the
> unconditional lookup_and_goto_ptr() exit currently generated
> for vsetvli.
>
> The shared do_vsetvl() function handles both vsetvl and vsetvli.
> The two instructions differ primarily in the source of their vtype
> operand: vsetvl takes it from a register, while vsetvli provides it
> as an immediate.
>
> When translating vsetvli, the immediate vtype provides information
> that can be used to optimize the exit generated by do_vsetvl().
>
> To use gen_goto_tb(), each jump slot must correspond to a deterministic
> successor-TB lookup key (pc, flags, cflags, cs_base) for a given source
> TB. The relevant state changes are confined to the vector-related
> translation state: VILL, SEW, ALTFMT, VSTART_EQ_ZERO, VS, LMUL, VTA,
> VMA, and VL_EQ_VLMAX.
>
> For the vsetvli forms optimized by this patch, given the CPU
> configuration and source TB context, the immediate vtype and existing
> state update flow determine the other relevant translation state.
> Only VL_EQ_VLMAX can vary with AVL. This patch therefore uses separate
> jump slots depending on whether the returned VL equals VLMAX,
> enabling direct TB linking after vsetvli.
>
> The register-vtype vsetvl instruction retains the existing lookup
> path, as its vtype is not known at translation time.
>
> Measured on an AMD Ryzen AI 9 H 365 (x86-64) host, using qemu-riscv64
> with -cpu rv64,v=true,vlen=128,elen=64. A RISC-V GCC 14.2.0 cc1 built
> for rv64gcv compiles the packages with
> -O2 -g0 -march=rv64gcv -mabi=lp64d. Only cc1 runs under emulation;
> make, the compiler driver and cross-binutils run on the host.
>
> Each variant is warmed up once, followed by three paired clean builds
> in AB/BA/AB order, using -j1 and fixed host CPU affinity (CPU 7).
> The SQLite workload builds the amalgamation and command-line shell.
> Median build wall times are:
>
> Before (s) After (s) Wall time reduction
> LZ4 1.10.0 125.120 123.867 1.00%
> zlib 1.3.1 70.269 69.911 0.51%
> SQLite 3.45.1 438.322 433.213 1.17%
>
> In a separate untimed test, a loop executes two vsetvli instructions
> per iteration for 37 iterations. Host-side GDB entry breakpoints show
> that helper_lookup_tb_ptr() entries at the two sequential successor
> PCs fall from 74 to 0.
>
> Signed-off-by: WX Chen <wxchen0913@gmail.com>
> ---
> target/riscv/tcg/insn_trans/trans_rvv.c.inc | 25 ++++++++++++++++++++-----
> 1 file changed, 20 insertions(+), 5 deletions(-)
>
> diff --git a/target/riscv/tcg/insn_trans/trans_rvv.c.inc b/target/riscv/tcg/insn_trans/trans_rvv.c.inc
> index 7e1295f33e7d4b7a2ff26cbf428f6b5d9eb682ff..5e9a861b6af1565485e9be89b393d0d64080a662 100644
> --- a/target/riscv/tcg/insn_trans/trans_rvv.c.inc
> +++ b/target/riscv/tcg/insn_trans/trans_rvv.c.inc
> @@ -191,7 +191,8 @@ static bool require_noover(const int8_t dst, const int8_t dst_lmul,
> return !is_overlapped(dst, dst_size, src, src_size);
> }
>
> -static bool do_vsetvl(DisasContext *s, int rd, int rs1, TCGv s2)
> +/* A negative vtype denotes the register-supplied form. */
> +static bool do_vsetvl(DisasContext *s, int rd, int rs1, TCGv s2, int vtype)
I suggested that we replace `int vtype` with `uint32_t vtype` and `bool vtype_is_imm`:
static bool do_vsetvl(DisasContext *s, int rd, int rs1, TCGv s2,
uint32_t vtype, bool vtype_is_imm)
> {
> TCGv s1, dst;
>
> @@ -215,8 +216,22 @@ static bool do_vsetvl(DisasContext *s, int rd, int rs1, TCGv s2)
> gen_set_gpr(s, rd, dst);
> finalize_rvv_inst(s);
>
> - gen_update_pc(s, s->cur_insn_len);
> - lookup_and_goto_ptr(s);
> + if (vtype >= 0 && (rd != 0 || rs1 != 0)) {
I would like to discuss this a bit further. In fact, we can divide this into
five cases:
Case Condition Current implementation
------------------------------------------------------------------------------
1. vsetvl rd, rs1, rs2 vtype from rs2 register keep lookup_and_goto_ptr()
2. vsetvli x0, x0, imm rd == 0 && rs1 == 0 keep lookup_and_goto_ptr()
3. vsetvli rd, rs1, imm rd != 0 && rs1 != 0 new path
4. vsetvli x0, rs1, imm rd == 0 && rs1 != 0 new path
5. vsetvli rd, x0, imm rd != 0 && rs1 == 0 new path
Currently there are two TB exit forms:
- Case1 and Case2 is the existing lookup_and_goto_ptr() exit.
- Case3 ~ Case5 is the new split exit, which compares the returned VL against
VLMAX and uses two goto_tb slots.
The new path can be improved. For rs1 == 0 a single gen_goto_tb() call is
enough, because the successor TB lookup key cannot change between executions.
So, We give three exit forms:
1. lookup: keep lookup_and_goto_ptr()
2. single: one gen_goto_tb() slot
3. split: compare VL against VLMAX and use two gen_goto_tb() slots
The cases map to the exit forms as follows:
Case Exit form
--------------------------------------
1 lookup
2 lookup
3 split, or single when maxsz < 8
4 split, or single when maxsz < 8
5 single
For cases 3 and 4, we care about the case where VL is not equal to
VLMAX. To explain this, let us look at VL_EQ_VLMAX first. It is computed
in riscv_get_tb_cpu_state() in target/riscv/tcg/tcg-cpu.c:
vl_eq_vlmax = (env->vstart == 0) && (vlmax == env->vl) && (maxsz >= 8)
In two cases, the AVL comes from a GPR rather than an immediate, so
VL may or may not equal VLMAX. Using the split exit is fine in that
situation. When maxsz < 8, however, VL_EQ_VLMAX is forced to zero. In
that case, both VL values map to the same successor key during the TB
lookup, so a single slot is enough.
> + uint32_t vsew = FIELD_EX32(vtype, VTYPE, VSEW);
> + int8_t lmul = sextract32(FIELD_EX32(vtype, VTYPE, VLMUL), 0, 3);
> + uint32_t vlmax = vext_get_vlmax(s->cfg_ptr->vlenb, vsew, lmul);
> + target_ulong pc_save = s->pc_save;
> + TCGLabel *not_vlmax = gen_new_label();
> +
> + tcg_gen_brcondi_tl(TCG_COND_NE, dst, vlmax, not_vlmax);
> + gen_goto_tb(s, 0, s->cur_insn_len);
> + s->pc_save = pc_save;
> + gen_set_label(not_vlmax);
> + gen_goto_tb(s, 1, s->cur_insn_len);
> + } else {
> + gen_update_pc(s, s->cur_insn_len);
> + lookup_and_goto_ptr(s);
> + }
The following code is for your reference:
if (!vtype_is_imm || (rd == 0 && rs1 == 0)) {
gen_update_pc(s, s->cur_insn_len);
lookup_and_goto_ptr(s);
} else {
uint32_t vsew = FIELD_EX32(vtype, VTYPE, VSEW);
int8_t lmul = sextract32(FIELD_EX32(vtype, VTYPE, VLMUL), 0, 3);
uint32_t vlmax = vext_get_vlmax(s->cfg_ptr->vlenb, vsew, lmul);
if ((rs1 == 0) || ((vlmax << vsew) < 8)) {
gen_goto_tb(s, 0, s->cur_insn_len);
} else {
target_ulong pc_save = s->pc_save;
TCGLabel *not_vlmax = gen_new_label();
tcg_gen_brcondi_tl(TCG_COND_NE, dst, vlmax, not_vlmax);
gen_goto_tb(s, 0, s->cur_insn_len);
s->pc_save = pc_save;
gen_set_label(not_vlmax);
gen_goto_tb(s, 1, s->cur_insn_len);
}
}
Thanks,
Chao
> s->base.is_jmp = DISAS_NORETURN;
> return true;
> }
> @@ -244,13 +259,13 @@ static bool do_vsetivli(DisasContext *s, int rd, TCGv s1, TCGv s2)
> static bool trans_vsetvl(DisasContext *s, arg_vsetvl *a)
> {
> TCGv s2 = get_gpr(s, a->rs2, EXT_ZERO);
> - return do_vsetvl(s, a->rd, a->rs1, s2);
> + return do_vsetvl(s, a->rd, a->rs1, s2, -1);
> }
>
> static bool trans_vsetvli(DisasContext *s, arg_vsetvli *a)
> {
> TCGv s2 = tcg_constant_tl(a->zimm);
> - return do_vsetvl(s, a->rd, a->rs1, s2);
> + return do_vsetvl(s, a->rd, a->rs1, s2, a->zimm);
> }
>
> static bool trans_vsetivli(DisasContext *s, arg_vsetivli *a)
>
> ---
> base-commit: a3565937b39779d4e3704298095f566e7559ad53
> change-id: 20261104-research-vsetvli-direct-chain-20261104-206bc2104ddb
>
> Best regards,
> --
> WX Chen <wxchen0913@gmail.com>
>
>
>
Sent using hkml (https://github.com/sjp38/hackermail)
On 11/5/26 15:41, Chao Liu wrote: > I would like to discuss this a bit further. In fact, we can divide this into > five cases: > > Case Condition Current implementation > ------------------------------------------------------------------------------ > 1. vsetvl rd, rs1, rs2 vtype from rs2 register keep lookup_and_goto_ptr() > 2. vsetvli x0, x0, imm rd == 0 && rs1 == 0 keep lookup_and_goto_ptr() > 3. vsetvli rd, rs1, imm rd != 0 && rs1 != 0 new path > 4. vsetvli x0, rs1, imm rd == 0 && rs1 != 0 new path > 5. vsetvli rd, x0, imm rd != 0 && rs1 == 0 new path > > Currently there are two TB exit forms: > > - Case1 and Case2 is the existing lookup_and_goto_ptr() exit. > - Case3 ~ Case5 is the new split exit, which compares the returned VL against > VLMAX and uses two goto_tb slots. > > The new path can be improved. For rs1 == 0 a single gen_goto_tb() call is > enough, because the successor TB lookup key cannot change between executions. > > So, We give three exit forms: > > 1. lookup: keep lookup_and_goto_ptr() > 2. single: one gen_goto_tb() slot > 3. split: compare VL against VLMAX and use two gen_goto_tb() slots > > The cases map to the exit forms as follows: > > Case Exit form > -------------------------------------- > 1 lookup > 2 lookup > 3 split, or single when maxsz < 8 > 4 split, or single when maxsz < 8 > 5 single > > For cases 3 and 4, we care about the case where VL is not equal to > VLMAX. To explain this, let us look at VL_EQ_VLMAX first. It is computed > in riscv_get_tb_cpu_state() in target/riscv/tcg/tcg-cpu.c: > > vl_eq_vlmax = (env->vstart == 0) && (vlmax == env->vl) && (maxsz >= 8) > > In two cases, the AVL comes from a GPR rather than an immediate, so > VL may or may not equal VLMAX. Using the split exit is fine in that > situation. When maxsz < 8, however, VL_EQ_VLMAX is forced to zero. In > that case, both VL values map to the same successor key during the TB > lookup, so a single slot is enough. Hi Chao: Thanks for the detailed review! I hadn't noticed the maxsz >= 8 condition for VL_EQ_VLMAX. Thanks for pointing that out! I'll take this condition into account in v2. Best regards, WX Chen
© 2016 - 2026 Red Hat, Inc.