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 <[email protected]>
---
 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 <[email protected]>


Reply via email to