Skip to contentSkip to conversation

riscv: set vstart_eq_zero on vector insns

PATCH Superseded Daniel Henrique Barboza 3 participants 16 messages Mar 13, 2024 – Mar 14, 2024 RISC-V

v14 of 15 Superseded Latest is v15 →

lore
Share & tools
lore
#2 lore raw

PATCH v14 2/8 trans_rvv.c.inc: set vstart = 0 in int scalar move insns

In reply to Daniel Henrique Barboza

trans_vmv_x_s, trans_vmv_s_x, trans_vfmv_f_s and trans_vfmv_s_f aren't setting vstart = 0 after execution. This is usually done by a helper in vector_helper.c but these functions don't use helpers.

We'll set vstart after any potential 'over' brconds, and that will also mandate a mark_vs_dirty() too.

Fixes dedc53cbc9 ("target/riscv: rvv-1.0: integer scalar move instructions")
---
 target/riscv/insn_trans/trans_rvv.c.inc | 10 ++++++++--
 1 file changed, 8 insertions(+), 2 deletions(-)
target/riscv/insn_trans/trans_rvv.c.inc +8 -2
index e42728990e..8c16a9f5b3 100644
@@ -3373,6 +3373,8 @@ static bool trans_vmv_x_s(DisasContext *s, arg_vmv_x_s *a)
         vec_element_loadi(s, t1, a->rs2, 0, true);
         tcg_gen_trunc_i64_tl(dest, t1);
         gen_set_gpr(s, a->rd, dest);
+        tcg_gen_movi_tl(cpu_vstart, 0);
+        mark_vs_dirty(s);
         return true;
     }
     return false;
@@ -3399,8 +3401,9 @@ static bool trans_vmv_s_x(DisasContext *s, arg_vmv_s_x *a)
         s1 = get_gpr(s, a->rs1, EXT_NONE);
         tcg_gen_ext_tl_i64(t1, s1);
         vec_element_storei(s, a->rd, 0, t1);
-        mark_vs_dirty(s);
         gen_set_label(over);
+        tcg_gen_movi_tl(cpu_vstart, 0);
+        mark_vs_dirty(s);
         return true;
     }
     return false;
@@ -3427,6 +3430,8 @@ static bool trans_vfmv_f_s(DisasContext *s, arg_vfmv_f_s *a)
         }
 
         mark_fs_dirty(s);
+        tcg_gen_movi_tl(cpu_vstart, 0);
+        mark_vs_dirty(s);
         return true;
     }
     return false;
@@ -3452,8 +3457,9 @@ static bool trans_vfmv_s_f(DisasContext *s, arg_vfmv_s_f *a)
         do_nanbox(s, t1, cpu_fpr[a->rs1]);
 
         vec_element_storei(s, a->rd, 0, t1);
-        mark_vs_dirty(s);
         gen_set_label(over);
+        tcg_gen_movi_tl(cpu_vstart, 0);
+        mark_vs_dirty(s);
         return true;
     }
     return false;
Signature · 1 line
2.43.2
#3 lore raw

PATCH v14 3/8 target/riscv: always clear vstart in whole vec move insns

In reply to Daniel Henrique Barboza

These insns have 2 paths: we'll either have vstart already cleared if vstart_eq_zero or we'll do a brcond to check if vstart >= maxsz to call the 'vmvr_v' helper. The helper will clear vstart if it executes until the end, or if vstart >= vl.

However, if vstart >= maxsz, the helper will be skipped, and vstart won't be cleared since the helper is being responsible from doing it.

We want to make the helpers responsible to manage vstart, including these corner cases, precisely to avoid these situations. Move the vstart

Quoted 1 line

= maxsz cond to the helper, and be sure to clear vstart if that

happens. This way we're now sure that vstart is being cleared in the end of the execution, regardless of the path taken.

Fixes f714361ed7 ("target/riscv: rvv-1.0: implement vstart CSR")
---
 target/riscv/insn_trans/trans_rvv.c.inc | 3 ---
 target/riscv/vector_helper.c            | 5 +++++
 2 files changed, 5 insertions(+), 3 deletions(-)
target/riscv/insn_trans/trans_rvv.c.inc +0 -3
index 8c16a9f5b3..52c26a7834 100644
@@ -3664,12 +3664,9 @@ static bool trans_##NAME(DisasContext *s, arg_##NAME * a)               \
                              vreg_ofs(s, a->rs2), maxsz, maxsz);        \
             mark_vs_dirty(s);                                           \
         } else {                                                        \
-            TCGLabel *over = gen_new_label();                           \
-            tcg_gen_brcondi_tl(TCG_COND_GEU, cpu_vstart, maxsz, over);  \
             tcg_gen_gvec_2_ptr(vreg_ofs(s, a->rd), vreg_ofs(s, a->rs2), \
                                tcg_env, maxsz, maxsz, 0, gen_helper_vmvr_v); \
             mark_vs_dirty(s);                                           \
-            gen_set_label(over);                                        \
         }                                                               \
         return true;                                                    \
     }                                                                   \
target/riscv/vector_helper.c +5 -0
index ca79571ae2..cd8235ea98 100644
@@ -5075,6 +5075,11 @@ void HELPER(vmvr_v)(void *vd, void *vs2, CPURISCVState *env, uint32_t desc)
     uint32_t startb = env->vstart * sewb;
     uint32_t i = startb;
 
+    if (env->vstart >= maxsz) {
+        env->vstart = 0;
+        return;
+    }
+
     memcpy((uint8_t *)vd + H1(i),
            (uint8_t *)vs2 + H1(i),
            maxsz - startb);
Signature · 1 line
2.43.2
#0 lore raw

v14 cover riscv: set vstart_eq_zero on vector insns

Hi,

In this version we're fixing a redundant check in the vmvr_v helper that was pointed out by in v13.

To make this change easier patches 3 and 4 switched places. A trivial change was made in patch 4 that don't warrant another review.

We're missing acks in patch 3 only.

Series based on master.

Changes from v13:
- patches 3 and 4: switched places
- patch 3:
- fixed commit msg: from
"(...) now sure that vstart is being clearer"
to
"(...) now sure that vstart is being cleared"
- patch 4:
- do not check for vstart >= vl (i.e. add VSTART_CHECK_EARLY_EXIT())
in the 'vmvr_v' helper
- v13 link: https://lore.kernel.org/qemu-riscv/[email protected]/

Daniel Henrique Barboza (7):
target/riscv/vector_helper.c: set vstart = 0 in GEN_VEXT_VSLIDEUP_VX() trans_rvv.c.inc: set vstart = 0 in int scalar move insns target/riscv: always clear vstart in whole vec move insns target/riscv/vector_helpers: do early exit when vstart >= vl target/riscv: remove 'over' brconds from vector trans trans_rvv.c.inc: remove redundant mark_vs_dirty() calls target/riscv/vector_helper.c: optimize loops in ldst helpers

Ivan Klokov (1):
target/riscv: enable 'vstart_eq_zero' in the end of insns

 target/riscv/insn_trans/trans_rvbf16.c.inc |  18 +-
 target/riscv/insn_trans/trans_rvv.c.inc    | 198 +++++----------------
 target/riscv/insn_trans/trans_rvvk.c.inc   |  30 +---
 target/riscv/translate.c                   |   6 +
 target/riscv/vcrypto_helper.c              |  32 ++++
 target/riscv/vector_helper.c               | 100 ++++++++++-
 target/riscv/vector_internals.c            |   4 +
 target/riscv/vector_internals.h            |   9 +
 8 files changed, 205 insertions(+), 192 deletions(-)
Signature · 1 line
2.43.2
#1 lore raw

PATCH v14 1/8 target/riscv/vector_helper.c: set vstart = 0 in GEN_VEXT_VSLIDEUP_VX()

The helper isn't setting env->vstart = 0 after its execution, as it is expected from every vector instruction that completes successfully.

---
 target/riscv/vector_helper.c | 1 +
 1 file changed, 1 insertion(+)
target/riscv/vector_helper.c +1 -0
index fe56c007d5..ca79571ae2 100644
@@ -4781,6 +4781,7 @@ void HELPER(NAME)(void *vd, void *v0, target_ulong s1, void *vs2,         \
         }                                                                 \
         *((ETYPE *)vd + H(i)) = *((ETYPE *)vs2 + H(i - offset));          \
     }                                                                     \
+    env->vstart = 0;                                                      \
     /* set tail elements to 1s */                                         \
     vext_set_elems_1s(vd, vta, vl * esz, total_elems * esz);              \
 }
Signature · 1 line
2.43.2
#12 lore raw

PATCH v14 5/8 target/riscv: remove 'over' brconds from vector trans

In reply to Daniel Henrique Barboza

The previous patch added an early vstart >= vl exit in all vector helpers, most of them using the VSTART_CHECK_EARLY_EXIT() macro, and now we're left with a lot of 'brcond' that has not use. The pattern goes like this:

    VSTART_CHECK_EARLY_EXIT(env);
    (...)
    tcg_gen_brcond_tl(TCG_COND_GEU, cpu_vstart, cpu_vl, over);
    (...)
    gen_set_label(over);
    return true;

The early exit makes the 'brcond' unneeded since it's already granted that vstart < vl. Remove all 'over' conditionals from the vector helpers.

Note that not all insns uses helpers, and for those cases the 'brcond' jump is the only way to filter vstart >= vl. This is the case of trans_vmv_s_x() and trans_vfmv_s_f(). We won't remove the 'brcond' conditionals from them.

While we're at it, remove the (vl == 0) brconds from trans_rvbf16.c.inc too since they're unneeded.

---
 target/riscv/insn_trans/trans_rvbf16.c.inc |  12 ---
 target/riscv/insn_trans/trans_rvv.c.inc    | 105 ---------------------
 target/riscv/insn_trans/trans_rvvk.c.inc   |  18 ----
 3 files changed, 135 deletions(-)
target/riscv/insn_trans/trans_rvbf16.c.inc +0 -12
index 8ee99df3f3..a842e76a6b 100644
@@ -71,11 +71,8 @@ static bool trans_vfncvtbf16_f_f_w(DisasContext *ctx, arg_vfncvtbf16_f_f_w *a)
 
     if (opfv_narrow_check(ctx, a) && (ctx->sew == MO_16)) {
         uint32_t data = 0;
-        TCGLabel *over = gen_new_label();
 
         gen_set_rm_chkfrm(ctx, RISCV_FRM_DYN);
-        tcg_gen_brcondi_tl(TCG_COND_EQ, cpu_vl, 0, over);
-        tcg_gen_brcond_tl(TCG_COND_GEU, cpu_vstart, cpu_vl, over);
 
         data = FIELD_DP32(data, VDATA, VM, a->vm);
         data = FIELD_DP32(data, VDATA, LMUL, ctx->lmul);
@@ -87,7 +84,6 @@ static bool trans_vfncvtbf16_f_f_w(DisasContext *ctx, arg_vfncvtbf16_f_f_w *a)
                            ctx->cfg_ptr->vlenb, data,
                            gen_helper_vfncvtbf16_f_f_w);
         mark_vs_dirty(ctx);
-        gen_set_label(over);
         return true;
     }
     return false;
@@ -100,11 +96,8 @@ static bool trans_vfwcvtbf16_f_f_v(DisasContext *ctx, arg_vfwcvtbf16_f_f_v *a)
 
     if (opfv_widen_check(ctx, a) && (ctx->sew == MO_16)) {
         uint32_t data = 0;
-        TCGLabel *over = gen_new_label();
 
         gen_set_rm_chkfrm(ctx, RISCV_FRM_DYN);
-        tcg_gen_brcondi_tl(TCG_COND_EQ, cpu_vl, 0, over);
-        tcg_gen_brcond_tl(TCG_COND_GEU, cpu_vstart, cpu_vl, over);
 
         data = FIELD_DP32(data, VDATA, VM, a->vm);
         data = FIELD_DP32(data, VDATA, LMUL, ctx->lmul);
@@ -116,7 +109,6 @@ static bool trans_vfwcvtbf16_f_f_v(DisasContext *ctx, arg_vfwcvtbf16_f_f_v *a)
                            ctx->cfg_ptr->vlenb, data,
                            gen_helper_vfwcvtbf16_f_f_v);
         mark_vs_dirty(ctx);
-        gen_set_label(over);
         return true;
     }
     return false;
@@ -130,11 +122,8 @@ static bool trans_vfwmaccbf16_vv(DisasContext *ctx, arg_vfwmaccbf16_vv *a)
     if (require_rvv(ctx) && vext_check_isa_ill(ctx) && (ctx->sew == MO_16) &&
         vext_check_dss(ctx, a->rd, a->rs1, a->rs2, a->vm)) {
         uint32_t data = 0;
-        TCGLabel *over = gen_new_label();
 
         gen_set_rm_chkfrm(ctx, RISCV_FRM_DYN);
-        tcg_gen_brcondi_tl(TCG_COND_EQ, cpu_vl, 0, over);
-        tcg_gen_brcond_tl(TCG_COND_GEU, cpu_vstart, cpu_vl, over);
 
         data = FIELD_DP32(data, VDATA, VM, a->vm);
         data = FIELD_DP32(data, VDATA, LMUL, ctx->lmul);
@@ -147,7 +136,6 @@ static bool trans_vfwmaccbf16_vv(DisasContext *ctx, arg_vfwmaccbf16_vv *a)
                            ctx->cfg_ptr->vlenb, data,
                            gen_helper_vfwmaccbf16_vv);
         mark_vs_dirty(ctx);
-        gen_set_label(over);
         return true;
     }
     return false;
target/riscv/insn_trans/trans_rvv.c.inc +0 -105

484 lines not on this page · raw message

target/riscv/insn_trans/trans_rvvk.c.inc +0 -18
index a5cdd1b67f..6d640e4596 100644
@@ -164,8 +164,6 @@ GEN_OPIVX_GVEC_TRANS_CHECK(vandn_vx, andcs, zvkb_vx_check)
                 gen_helper_##NAME##_w,                                     \
                 gen_helper_##NAME##_d,                                     \
             };                                                             \
-            TCGLabel *over = gen_new_label();                              \
-            tcg_gen_brcond_tl(TCG_COND_GEU, cpu_vstart, cpu_vl, over);     \
                                                                            \
             data = FIELD_DP32(data, VDATA, VM, a->vm);                     \
             data = FIELD_DP32(data, VDATA, LMUL, s->lmul);                 \
@@ -177,7 +175,6 @@ GEN_OPIVX_GVEC_TRANS_CHECK(vandn_vx, andcs, zvkb_vx_check)
                                s->cfg_ptr->vlenb, s->cfg_ptr->vlenb,       \
                                data, fns[s->sew]);                         \
             mark_vs_dirty(s);                                              \
-            gen_set_label(over);                                           \
             return true;                                                   \
         }                                                                  \
         return false;                                                      \
@@ -249,14 +246,12 @@ GEN_OPIVI_WIDEN_TRANS(vwsll_vi, IMM_ZX, vwsll_vx, vwsll_vx_check)
             TCGv_ptr rd_v, rs2_v;                                             \
             TCGv_i32 desc, egs;                                               \
             uint32_t data = 0;                                                \
-            TCGLabel *over = gen_new_label();                                 \
                                                                               \
             if (!s->vstart_eq_zero || !s->vl_eq_vlmax) {                      \
                 /* save opcode for unwinding in case we throw an exception */ \
                 decode_save_opc(s);                                           \
                 egs = tcg_constant_i32(EGS);                                  \
                 gen_helper_egs_check(egs, tcg_env);                           \
-                tcg_gen_brcond_tl(TCG_COND_GEU, cpu_vstart, cpu_vl, over);    \
             }                                                                 \
                                                                               \
             data = FIELD_DP32(data, VDATA, VM, a->vm);                        \
@@ -272,7 +267,6 @@ GEN_OPIVI_WIDEN_TRANS(vwsll_vi, IMM_ZX, vwsll_vx, vwsll_vx_check)
             tcg_gen_addi_ptr(rs2_v, tcg_env, vreg_ofs(s, a->rs2));            \
             gen_helper_##NAME(rd_v, rs2_v, tcg_env, desc);                    \
             mark_vs_dirty(s);                                                 \
-            gen_set_label(over);                                              \
             return true;                                                      \
         }                                                                     \
         return false;                                                         \
@@ -325,14 +319,12 @@ GEN_V_UNMASKED_TRANS(vaesem_vs, vaes_check_vs, ZVKNED_EGS)
             TCGv_ptr rd_v, rs2_v;                                             \
             TCGv_i32 uimm_v, desc, egs;                                       \
             uint32_t data = 0;                                                \
-            TCGLabel *over = gen_new_label();                                 \
                                                                               \
             if (!s->vstart_eq_zero || !s->vl_eq_vlmax) {                      \
                 /* save opcode for unwinding in case we throw an exception */ \
                 decode_save_opc(s);                                           \
                 egs = tcg_constant_i32(EGS);                                  \
                 gen_helper_egs_check(egs, tcg_env);                           \
-                tcg_gen_brcond_tl(TCG_COND_GEU, cpu_vstart, cpu_vl, over);    \
             }                                                                 \
                                                                               \
             data = FIELD_DP32(data, VDATA, VM, a->vm);                        \
@@ -350,7 +342,6 @@ GEN_V_UNMASKED_TRANS(vaesem_vs, vaes_check_vs, ZVKNED_EGS)
             tcg_gen_addi_ptr(rs2_v, tcg_env, vreg_ofs(s, a->rs2));            \
             gen_helper_##NAME(rd_v, rs2_v, uimm_v, tcg_env, desc);            \
             mark_vs_dirty(s);                                                 \
-            gen_set_label(over);                                              \
             return true;                                                      \
         }                                                                     \
         return false;                                                         \
@@ -394,7 +385,6 @@ GEN_VI_UNMASKED_TRANS(vaeskf2_vi, vaeskf2_check, ZVKNED_EGS)
     {                                                                         \
         if (CHECK(s, a)) {                                                    \
             uint32_t data = 0;                                                \
-            TCGLabel *over = gen_new_label();                                 \
             TCGv_i32 egs;                                                     \
                                                                               \
             if (!s->vstart_eq_zero || !s->vl_eq_vlmax) {                      \
@@ -402,7 +392,6 @@ GEN_VI_UNMASKED_TRANS(vaeskf2_vi, vaeskf2_check, ZVKNED_EGS)
                 decode_save_opc(s);                                           \
                 egs = tcg_constant_i32(EGS);                                  \
                 gen_helper_egs_check(egs, tcg_env);                           \
-                tcg_gen_brcond_tl(TCG_COND_GEU, cpu_vstart, cpu_vl, over);    \
             }                                                                 \
                                                                               \
             data = FIELD_DP32(data, VDATA, VM, a->vm);                        \
@@ -417,7 +406,6 @@ GEN_VI_UNMASKED_TRANS(vaeskf2_vi, vaeskf2_check, ZVKNED_EGS)
                                data, gen_helper_##NAME);                      \
                                                                               \
             mark_vs_dirty(s);                                                 \
-            gen_set_label(over);                                              \
             return true;                                                      \
         }                                                                     \
         return false;                                                         \
@@ -448,7 +436,6 @@ static bool trans_vsha2cl_vv(DisasContext *s, arg_rmrr *a)
 {
     if (vsha_check(s, a)) {
         uint32_t data = 0;
-        TCGLabel *over = gen_new_label();
         TCGv_i32 egs;
 
         if (!s->vstart_eq_zero || !s->vl_eq_vlmax) {
@@ -456,7 +443,6 @@ static bool trans_vsha2cl_vv(DisasContext *s, arg_rmrr *a)
             decode_save_opc(s);
             egs = tcg_constant_i32(ZVKNH_EGS);
             gen_helper_egs_check(egs, tcg_env);
-            tcg_gen_brcond_tl(TCG_COND_GEU, cpu_vstart, cpu_vl, over);
         }
 
         data = FIELD_DP32(data, VDATA, VM, a->vm);
@@ -472,7 +458,6 @@ static bool trans_vsha2cl_vv(DisasContext *s, arg_rmrr *a)
                 gen_helper_vsha2cl32_vv : gen_helper_vsha2cl64_vv);
 
         mark_vs_dirty(s);
-        gen_set_label(over);
         return true;
     }
     return false;
@@ -482,7 +467,6 @@ static bool trans_vsha2ch_vv(DisasContext *s, arg_rmrr *a)
 {
     if (vsha_check(s, a)) {
         uint32_t data = 0;
-        TCGLabel *over = gen_new_label();
         TCGv_i32 egs;
 
         if (!s->vstart_eq_zero || !s->vl_eq_vlmax) {
@@ -490,7 +474,6 @@ static bool trans_vsha2ch_vv(DisasContext *s, arg_rmrr *a)
             decode_save_opc(s);
             egs = tcg_constant_i32(ZVKNH_EGS);
             gen_helper_egs_check(egs, tcg_env);
-            tcg_gen_brcond_tl(TCG_COND_GEU, cpu_vstart, cpu_vl, over);
         }
 
         data = FIELD_DP32(data, VDATA, VM, a->vm);
@@ -506,7 +489,6 @@ static bool trans_vsha2ch_vv(DisasContext *s, arg_rmrr *a)
                 gen_helper_vsha2ch32_vv : gen_helper_vsha2ch64_vv);
 
         mark_vs_dirty(s);
-        gen_set_label(over);
         return true;
     }
     return false;
Signature · 1 line
2.43.2
#6 lore raw

PATCH v14 4/8 target/riscv/vector_helpers: do early exit when vstart >= vl

In reply to Daniel Henrique Barboza

We're going to make changes that will required each helper to be responsible for the 'vstart' management, i.e. we will relieve the 'vstart < vl' assumption that helpers have today.

Helpers are usually able to deal with vstart >= vl, i.e. doing nothing aside from setting vstart = 0 at the end, but the tail update functions will update the tail regardless of vstart being valid or not.

Unifying the tail update process in a single function that would handle the vstart >= vl case isn't trivial. We have 2 functions that are used to update tail: vext_set_tail_elems_1s() and vext_set_elems_1s(). The latter is a more generic function that is also used to mask elements. There's no easy way of making all callers using vext_set_tail_elems_1s() because we're not encoding NF properly in all cases [1].

This patch takes a blunt approach: do an early exit in every single vector helper if vstart >= vl. We can worry about unifying the tail update process later.

[1] https://lore.kernel.org/qemu-riscv/[email protected]/

---
 target/riscv/vcrypto_helper.c   | 32 ++++++++++++
 target/riscv/vector_helper.c    | 88 +++++++++++++++++++++++++++++++++
 target/riscv/vector_internals.c |  4 ++
 target/riscv/vector_internals.h |  9 ++++
 4 files changed, 133 insertions(+)
target/riscv/vcrypto_helper.c +32 -0
index e2d719b13b..f7423df226 100644
@@ -222,6 +222,8 @@ static inline void xor_round_key(AESState *round_state, AESState *round_key)
         uint32_t total_elems = vext_get_total_elems(env, desc, 4);        \
         uint32_t vta = vext_vta(desc);                                    \
                                                                           \
+        VSTART_CHECK_EARLY_EXIT(env);                                     \
+                                                                          \
         for (uint32_t i = env->vstart / 4; i < env->vl / 4; i++) {        \
             AESState round_key;                                           \
             round_key.d[0] = *((uint64_t *)vs2 + H8(i * 2 + 0));          \
@@ -246,6 +248,8 @@ static inline void xor_round_key(AESState *round_state, AESState *round_key)
         uint32_t total_elems = vext_get_total_elems(env, desc, 4);        \
         uint32_t vta = vext_vta(desc);                                    \
                                                                           \
+        VSTART_CHECK_EARLY_EXIT(env);                                     \
+                                                                          \
         for (uint32_t i = env->vstart / 4; i < env->vl / 4; i++) {        \
             AESState round_key;                                           \
             round_key.d[0] = *((uint64_t *)vs2 + H8(0));                  \
@@ -305,6 +309,8 @@ void HELPER(vaeskf1_vi)(void *vd_vptr, void *vs2_vptr, uint32_t uimm,
     uint32_t total_elems = vext_get_total_elems(env, desc, 4);
     uint32_t vta = vext_vta(desc);
 
+    VSTART_CHECK_EARLY_EXIT(env);
+
     uimm &= 0b1111;
     if (uimm > 10 || uimm == 0) {
         uimm ^= 0b1000;
@@ -351,6 +357,8 @@ void HELPER(vaeskf2_vi)(void *vd_vptr, void *vs2_vptr, uint32_t uimm,
     uint32_t total_elems = vext_get_total_elems(env, desc, 4);
     uint32_t vta = vext_vta(desc);
 
+    VSTART_CHECK_EARLY_EXIT(env);
+
     uimm &= 0b1111;
     if (uimm > 14 || uimm < 2) {
         uimm ^= 0b1000;
@@ -457,6 +465,8 @@ void HELPER(vsha2ms_vv)(void *vd, void *vs1, void *vs2, CPURISCVState *env,
     uint32_t total_elems;
     uint32_t vta = vext_vta(desc);
 
+    VSTART_CHECK_EARLY_EXIT(env);
+
     for (uint32_t i = env->vstart / 4; i < env->vl / 4; i++) {
         if (sew == MO_32) {
             vsha2ms_e32(((uint32_t *)vd) + i * 4, ((uint32_t *)vs1) + i * 4,
@@ -572,6 +582,8 @@ void HELPER(vsha2ch32_vv)(void *vd, void *vs1, void *vs2, CPURISCVState *env,
     uint32_t total_elems;
     uint32_t vta = vext_vta(desc);
 
+    VSTART_CHECK_EARLY_EXIT(env);
+
     for (uint32_t i = env->vstart / 4; i < env->vl / 4; i++) {
         vsha2c_32(((uint32_t *)vs2) + 4 * i, ((uint32_t *)vd) + 4 * i,
                   ((uint32_t *)vs1) + 4 * i + 2);
@@ -590,6 +602,8 @@ void HELPER(vsha2ch64_vv)(void *vd, void *vs1, void *vs2, CPURISCVState *env,
     uint32_t total_elems;
     uint32_t vta = vext_vta(desc);
 
+    VSTART_CHECK_EARLY_EXIT(env);
+
     for (uint32_t i = env->vstart / 4; i < env->vl / 4; i++) {
         vsha2c_64(((uint64_t *)vs2) + 4 * i, ((uint64_t *)vd) + 4 * i,
                   ((uint64_t *)vs1) + 4 * i + 2);
@@ -608,6 +622,8 @@ void HELPER(vsha2cl32_vv)(void *vd, void *vs1, void *vs2, CPURISCVState *env,
     uint32_t total_elems;
     uint32_t vta = vext_vta(desc);
 
+    VSTART_CHECK_EARLY_EXIT(env);
+
     for (uint32_t i = env->vstart / 4; i < env->vl / 4; i++) {
         vsha2c_32(((uint32_t *)vs2) + 4 * i, ((uint32_t *)vd) + 4 * i,
                   (((uint32_t *)vs1) + 4 * i));
@@ -626,6 +642,8 @@ void HELPER(vsha2cl64_vv)(void *vd, void *vs1, void *vs2, CPURISCVState *env,
     uint32_t total_elems;
     uint32_t vta = vext_vta(desc);
 
+    VSTART_CHECK_EARLY_EXIT(env);
+
     for (uint32_t i = env->vstart / 4; i < env->vl / 4; i++) {
         vsha2c_64(((uint64_t *)vs2) + 4 * i, ((uint64_t *)vd) + 4 * i,
                   (((uint64_t *)vs1) + 4 * i));
@@ -658,6 +676,8 @@ void HELPER(vsm3me_vv)(void *vd_vptr, void *vs1_vptr, void *vs2_vptr,
     uint32_t *vs1 = vs1_vptr;
     uint32_t *vs2 = vs2_vptr;
 
+    VSTART_CHECK_EARLY_EXIT(env);
+
     for (int i = env->vstart / 8; i < env->vl / 8; i++) {
         uint32_t w[24];
         for (int j = 0; j < 8; j++) {
@@ -757,6 +777,8 @@ void HELPER(vsm3c_vi)(void *vd_vptr, void *vs2_vptr, uint32_t uimm,
     uint32_t *vs2 = vs2_vptr;
     uint32_t v1[8], v2[8], v3[8];
 
+    VSTART_CHECK_EARLY_EXIT(env);
+
     for (int i = env->vstart / 8; i < env->vl / 8; i++) {
         for (int k = 0; k < 8; k++) {
             v2[k] = bswap32(vd[H4(i * 8 + k)]);
@@ -780,6 +802,8 @@ void HELPER(vghsh_vv)(void *vd_vptr, void *vs1_vptr, void *vs2_vptr,
     uint32_t vta = vext_vta(desc);
     uint32_t total_elems = vext_get_total_elems(env, desc, 4);
 
+    VSTART_CHECK_EARLY_EXIT(env);
+
     for (uint32_t i = env->vstart / 4; i < env->vl / 4; i++) {
         uint64_t Y[2] = {vd[i * 2 + 0], vd[i * 2 + 1]};
         uint64_t H[2] = {brev8(vs2[i * 2 + 0]), brev8(vs2[i * 2 + 1])};
@@ -817,6 +841,8 @@ void HELPER(vgmul_vv)(void *vd_vptr, void *vs2_vptr, CPURISCVState *env,
     uint32_t vta = vext_vta(desc);
     uint32_t total_elems = vext_get_total_elems(env, desc, 4);
 
+    VSTART_CHECK_EARLY_EXIT(env);
+
     for (uint32_t i = env->vstart / 4; i < env->vl / 4; i++) {
         uint64_t Y[2] = {brev8(vd[i * 2 + 0]), brev8(vd[i * 2 + 1])};
         uint64_t H[2] = {brev8(vs2[i * 2 + 0]), brev8(vs2[i * 2 + 1])};
@@ -853,6 +879,8 @@ void HELPER(vsm4k_vi)(void *vd, void *vs2, uint32_t uimm5, CPURISCVState *env,
     uint32_t esz = sizeof(uint32_t);
     uint32_t total_elems = vext_get_total_elems(env, desc, esz);
 
+    VSTART_CHECK_EARLY_EXIT(env);
+
     for (uint32_t i = group_start; i < group_end; ++i) {
         uint32_t vstart = i * egs;
         uint32_t vend = (i + 1) * egs;
@@ -909,6 +937,8 @@ void HELPER(vsm4r_vv)(void *vd, void *vs2, CPURISCVState *env, uint32_t desc)
     uint32_t esz = sizeof(uint32_t);
     uint32_t total_elems = vext_get_total_elems(env, desc, esz);
 
+    VSTART_CHECK_EARLY_EXIT(env);
+
     for (uint32_t i = group_start; i < group_end; ++i) {
         uint32_t vstart = i * egs;
         uint32_t vend = (i + 1) * egs;
@@ -943,6 +973,8 @@ void HELPER(vsm4r_vs)(void *vd, void *vs2, CPURISCVState *env, uint32_t desc)
     uint32_t esz = sizeof(uint32_t);
     uint32_t total_elems = vext_get_total_elems(env, desc, esz);
 
+    VSTART_CHECK_EARLY_EXIT(env);
+
     for (uint32_t i = group_start; i < group_end; ++i) {
         uint32_t vstart = i * egs;
         uint32_t vend = (i + 1) * egs;
target/riscv/vector_helper.c +88 -0

334 lines not on this page · raw message

target/riscv/vector_internals.c +4 -0
index 12f5964fbb..996c21eb31 100644
@@ -44,6 +44,8 @@ void do_vext_vv(void *vd, void *v0, void *vs1, void *vs2,
     uint32_t vma = vext_vma(desc);
     uint32_t i;
 
+    VSTART_CHECK_EARLY_EXIT(env);
+
     for (i = env->vstart; i < vl; i++) {
         if (!vm && !vext_elem_mask(v0, i)) {
             /* set masked-off elements to 1s */
@@ -68,6 +70,8 @@ void do_vext_vx(void *vd, void *v0, target_long s1, void *vs2,
     uint32_t vma = vext_vma(desc);
     uint32_t i;
 
+    VSTART_CHECK_EARLY_EXIT(env);
+
     for (i = env->vstart; i < vl; i++) {
         if (!vm && !vext_elem_mask(v0, i)) {
             /* set masked-off elements to 1s */
target/riscv/vector_internals.h +9 -0
index 842765f6c1..9e1e15b575 100644
@@ -24,6 +24,13 @@
 #include "tcg/tcg-gvec-desc.h"
 #include "internals.h"
 
+#define VSTART_CHECK_EARLY_EXIT(env) do { \
+    if (env->vstart >= env->vl) {         \
+        env->vstart = 0;                  \
+        return;                           \
+    }                                     \
+} while (0)
+
 static inline uint32_t vext_nf(uint32_t desc)
 {
     return FIELD_EX32(simd_data(desc), VDATA, NF);
@@ -151,6 +158,8 @@ void HELPER(NAME)(void *vd, void *v0, void *vs2,       \
     uint32_t vma = vext_vma(desc);                     \
     uint32_t i;                                        \
                                                        \
+    VSTART_CHECK_EARLY_EXIT(env);                      \
+                                                       \
     for (i = env->vstart; i < vl; i++) {               \
         if (!vm && !vext_elem_mask(v0, i)) {           \
             /* set masked-off elements to 1s */        \
Signature · 1 line
2.43.2
#13 lore raw

PATCH v14 6/8 trans_rvv.c.inc: remove redundant mark_vs_dirty() calls

In reply to Daniel Henrique Barboza

trans_vmv_v_i , trans_vfmv_v_f and the trans_##NAME macro from GEN_VMV_WHOLE_TRANS() are calling mark_vs_dirty() in both branches of their 'ifs'. conditionals.

Call it just once in the end like other functions are doing.

---
 target/riscv/insn_trans/trans_rvv.c.inc | 11 +++--------
 1 file changed, 3 insertions(+), 8 deletions(-)
target/riscv/insn_trans/trans_rvv.c.inc +3 -8
index 4c1a064cf6..b0f19dcd85 100644
@@ -2065,7 +2065,6 @@ static bool trans_vmv_v_i(DisasContext *s, arg_vmv_v_i *a)
         if (s->vl_eq_vlmax && !(s->vta && s->lmul < 0)) {
             tcg_gen_gvec_dup_imm(s->sew, vreg_ofs(s, a->rd),
                                  MAXSZ(s), MAXSZ(s), simm);
-            mark_vs_dirty(s);
         } else {
             TCGv_i32 desc;
             TCGv_i64 s1;
@@ -2083,9 +2082,8 @@ static bool trans_vmv_v_i(DisasContext *s, arg_vmv_v_i *a)
                                               s->cfg_ptr->vlenb, data));
             tcg_gen_addi_ptr(dest, tcg_env, vreg_ofs(s, a->rd));
             fns[s->sew](dest, s1, tcg_env, desc);
-
-            mark_vs_dirty(s);
         }
+        mark_vs_dirty(s);
         return true;
     }
     return false;
@@ -2612,7 +2610,6 @@ static bool trans_vfmv_v_f(DisasContext *s, arg_vfmv_v_f *a)
 
             tcg_gen_gvec_dup_i64(s->sew, vreg_ofs(s, a->rd),
                                  MAXSZ(s), MAXSZ(s), t1);
-            mark_vs_dirty(s);
         } else {
             TCGv_ptr dest;
             TCGv_i32 desc;
@@ -2635,9 +2632,8 @@ static bool trans_vfmv_v_f(DisasContext *s, arg_vfmv_v_f *a)
             tcg_gen_addi_ptr(dest, tcg_env, vreg_ofs(s, a->rd));
 
             fns[s->sew - 1](dest, t1, tcg_env, desc);
-
-            mark_vs_dirty(s);
         }
+        mark_vs_dirty(s);
         return true;
     }
     return false;
@@ -3560,12 +3556,11 @@ static bool trans_##NAME(DisasContext *s, arg_##NAME * a)               \
         if (s->vstart_eq_zero) {                                        \
             tcg_gen_gvec_mov(s->sew, vreg_ofs(s, a->rd),                \
                              vreg_ofs(s, a->rs2), maxsz, maxsz);        \
-            mark_vs_dirty(s);                                           \
         } else {                                                        \
             tcg_gen_gvec_2_ptr(vreg_ofs(s, a->rd), vreg_ofs(s, a->rs2), \
                                tcg_env, maxsz, maxsz, 0, gen_helper_vmvr_v); \
-            mark_vs_dirty(s);                                           \
         }                                                               \
+        mark_vs_dirty(s);                                               \
         return true;                                                    \
     }                                                                   \
     return false;                                                       \
Signature · 1 line
2.43.2
#14 lore raw

PATCH v14 7/8 target/riscv: enable 'vstart_eq_zero' in the end of insns

In reply to Daniel Henrique Barboza

From: Ivan Klokov <[email protected]>

The vstart_eq_zero flag is updated at the beginning of the translation phase from the env->vstart variable. During the execution phase all functions will set env->vstart = 0 after a successful execution, but the vstart_eq_zero flag remains the same as at the start of the block. This will wrongly cause SIGILLs in translations that requires env->vstart = 0 and might be reading vstart_eq_zero = false.

This patch adds a new finalize_rvv_inst() helper that is called at the end of each vector instruction that will both update vstart_eq_zero and do a mark_vs_dirty().

Resolves: https://gitlab.com/qemu-project/qemu/-/issues/1976

---
 target/riscv/insn_trans/trans_rvbf16.c.inc |  6 +-
 target/riscv/insn_trans/trans_rvv.c.inc    | 83 ++++++++++++----------
 target/riscv/insn_trans/trans_rvvk.c.inc   | 12 ++--
 target/riscv/translate.c                   |  6 ++
 4 files changed, 59 insertions(+), 48 deletions(-)
target/riscv/insn_trans/trans_rvbf16.c.inc +3 -3
index a842e76a6b..0a9cd1ec31 100644
@@ -83,7 +83,7 @@ static bool trans_vfncvtbf16_f_f_w(DisasContext *ctx, arg_vfncvtbf16_f_f_w *a)
                            ctx->cfg_ptr->vlenb,
                            ctx->cfg_ptr->vlenb, data,
                            gen_helper_vfncvtbf16_f_f_w);
-        mark_vs_dirty(ctx);
+        finalize_rvv_inst(ctx);
         return true;
     }
     return false;
@@ -108,7 +108,7 @@ static bool trans_vfwcvtbf16_f_f_v(DisasContext *ctx, arg_vfwcvtbf16_f_f_v *a)
                            ctx->cfg_ptr->vlenb,
                            ctx->cfg_ptr->vlenb, data,
                            gen_helper_vfwcvtbf16_f_f_v);
-        mark_vs_dirty(ctx);
+        finalize_rvv_inst(ctx);
         return true;
     }
     return false;
@@ -135,7 +135,7 @@ static bool trans_vfwmaccbf16_vv(DisasContext *ctx, arg_vfwmaccbf16_vv *a)
                            ctx->cfg_ptr->vlenb,
                            ctx->cfg_ptr->vlenb, data,
                            gen_helper_vfwmaccbf16_vv);
-        mark_vs_dirty(ctx);
+        finalize_rvv_inst(ctx);
         return true;
     }
     return false;
target/riscv/insn_trans/trans_rvv.c.inc +44 -39

343 lines not on this page · raw message

target/riscv/insn_trans/trans_rvvk.c.inc +6 -6
index 6d640e4596..ae1f40174a 100644
@@ -174,7 +174,7 @@ GEN_OPIVX_GVEC_TRANS_CHECK(vandn_vx, andcs, zvkb_vx_check)
                                vreg_ofs(s, a->rs2), tcg_env,               \
                                s->cfg_ptr->vlenb, s->cfg_ptr->vlenb,       \
                                data, fns[s->sew]);                         \
-            mark_vs_dirty(s);                                              \
+            finalize_rvv_inst(s);                                          \
             return true;                                                   \
         }                                                                  \
         return false;                                                      \
@@ -266,7 +266,7 @@ GEN_OPIVI_WIDEN_TRANS(vwsll_vi, IMM_ZX, vwsll_vx, vwsll_vx_check)
             tcg_gen_addi_ptr(rd_v, tcg_env, vreg_ofs(s, a->rd));              \
             tcg_gen_addi_ptr(rs2_v, tcg_env, vreg_ofs(s, a->rs2));            \
             gen_helper_##NAME(rd_v, rs2_v, tcg_env, desc);                    \
-            mark_vs_dirty(s);                                                 \
+            finalize_rvv_inst(s);                                             \
             return true;                                                      \
         }                                                                     \
         return false;                                                         \
@@ -341,7 +341,7 @@ GEN_V_UNMASKED_TRANS(vaesem_vs, vaes_check_vs, ZVKNED_EGS)
             tcg_gen_addi_ptr(rd_v, tcg_env, vreg_ofs(s, a->rd));              \
             tcg_gen_addi_ptr(rs2_v, tcg_env, vreg_ofs(s, a->rs2));            \
             gen_helper_##NAME(rd_v, rs2_v, uimm_v, tcg_env, desc);            \
-            mark_vs_dirty(s);                                                 \
+            finalize_rvv_inst(s);                                             \
             return true;                                                      \
         }                                                                     \
         return false;                                                         \
@@ -405,7 +405,7 @@ GEN_VI_UNMASKED_TRANS(vaeskf2_vi, vaeskf2_check, ZVKNED_EGS)
                                s->cfg_ptr->vlenb, s->cfg_ptr->vlenb,          \
                                data, gen_helper_##NAME);                      \
                                                                               \
-            mark_vs_dirty(s);                                                 \
+            finalize_rvv_inst(s);                                             \
             return true;                                                      \
         }                                                                     \
         return false;                                                         \
@@ -457,7 +457,7 @@ static bool trans_vsha2cl_vv(DisasContext *s, arg_rmrr *a)
             s->sew == MO_32 ?
                 gen_helper_vsha2cl32_vv : gen_helper_vsha2cl64_vv);
 
-        mark_vs_dirty(s);
+        finalize_rvv_inst(s);
         return true;
     }
     return false;
@@ -488,7 +488,7 @@ static bool trans_vsha2ch_vv(DisasContext *s, arg_rmrr *a)
             s->sew == MO_32 ?
                 gen_helper_vsha2ch32_vv : gen_helper_vsha2ch64_vv);
 
-        mark_vs_dirty(s);
+        finalize_rvv_inst(s);
         return true;
     }
     return false;
target/riscv/translate.c +6 -0
index ea5d52b2ef..9d57089fcc 100644
@@ -676,6 +676,12 @@ static void mark_vs_dirty(DisasContext *ctx)
 static inline void mark_vs_dirty(DisasContext *ctx) { }
 #endif
 
+static void finalize_rvv_inst(DisasContext *ctx)
+{
+    mark_vs_dirty(ctx);
+    ctx->vstart_eq_zero = true;
+}
+
 static void gen_set_rm(DisasContext *ctx, int rm)
 {
     if (ctx->frm == rm) {
Signature · 1 line
2.43.2
#15 lore raw

PATCH v14 8/8 target/riscv/vector_helper.c: optimize loops in ldst helpers

In reply to Daniel Henrique Barboza

Change the for loops in ldst helpers to do a single increment in the counter, and assign it env->vstart, to avoid re-reading from vstart every time.

---
 target/riscv/vector_helper.c | 6 +++---
 1 file changed, 3 insertions(+), 3 deletions(-)
target/riscv/vector_helper.c +3 -3
index 48d041dd4e..6ee5380d0e 100644
@@ -209,7 +209,7 @@ vext_ldst_stride(void *vd, void *v0, target_ulong base,
 
     VSTART_CHECK_EARLY_EXIT(env);
 
-    for (i = env->vstart; i < env->vl; i++, env->vstart++) {
+    for (i = env->vstart; i < env->vl; env->vstart = ++i) {
         k = 0;
         while (k < nf) {
             if (!vm && !vext_elem_mask(v0, i)) {
@@ -277,7 +277,7 @@ vext_ldst_us(void *vd, target_ulong base, CPURISCVState *env, uint32_t desc,
     VSTART_CHECK_EARLY_EXIT(env);
 
     /* load bytes from guest memory */
-    for (i = env->vstart; i < evl; i++, env->vstart++) {
+    for (i = env->vstart; i < evl; env->vstart = ++i) {
         k = 0;
         while (k < nf) {
             target_ulong addr = base + ((i * nf + k) << log2_esz);
@@ -393,7 +393,7 @@ vext_ldst_index(void *vd, void *v0, target_ulong base,
     VSTART_CHECK_EARLY_EXIT(env);
 
     /* load bytes from guest memory */
-    for (i = env->vstart; i < env->vl; i++, env->vstart++) {
+    for (i = env->vstart; i < env->vl; env->vstart = ++i) {
         k = 0;
         while (k < nf) {
             if (!vm && !vext_elem_mask(v0, i)) {
Signature · 1 line
2.43.2
#4 lore raw

Re: [PATCH for-9.0 v14 3/8] target/riscv: always clear vstart in whole vec move insns

In reply to Daniel Henrique Barboza
Quoted 43 lines · Daniel Henrique Barboza

These insns have 2 paths: we'll either have vstart already cleared if vstart_eq_zero or we'll do a brcond to check if vstart >= maxsz to call the 'vmvr_v' helper. The helper will clear vstart if it executes until the end, or if vstart >= vl.

However, if vstart >= maxsz, the helper will be skipped, and vstart won't be cleared since the helper is being responsible from doing it.

We want to make the helpers responsible to manage vstart, including these corner cases, precisely to avoid these situations. Move the vstart

Quoted 1 line

= maxsz cond to the helper, and be sure to clear vstart if that

happens. This way we're now sure that vstart is being cleared in the end of the execution, regardless of the path taken.

Fixes f714361ed7 ("target/riscv: rvv-1.0: implement vstart CSR")
---
  target/riscv/insn_trans/trans_rvv.c.inc | 3 ---
  target/riscv/vector_helper.c            | 5 +++++
  2 files changed, 5 insertions(+), 3 deletions(-)
target/riscv/insn_trans/trans_rvv.c.inc +0 -3
index 8c16a9f5b3..52c26a7834 100644
@@ -3664,12 +3664,9 @@ static bool trans_##NAME(DisasContext *s, arg_##NAME * a)               \
                               vreg_ofs(s, a->rs2), maxsz, maxsz);        \
              mark_vs_dirty(s);                                           \
          } else {                                                        \
-            TCGLabel *over = gen_new_label();                           \
-            tcg_gen_brcondi_tl(TCG_COND_GEU, cpu_vstart, maxsz, over);  \
              tcg_gen_gvec_2_ptr(vreg_ofs(s, a->rd), vreg_ofs(s, a->rs2), \
                                 tcg_env, maxsz, maxsz, 0, gen_helper_vmvr_v); \
              mark_vs_dirty(s);                                           \
-            gen_set_label(over);                                        \
          }                                                               \
          return true;                                                    \
      }                                                                   \
target/riscv/vector_helper.c +0 -0
index ca79571ae2..cd8235ea98 100644

target/riscv/vector_helper.c void HELPER(vmvr_v)(void *vd, void *vs2, CPURISCVState *env, uint32_t desc) · line 5081

Quoted 7 lines
target/riscv/vector_helper.c +4 -0
@@ -5075,6 +5075,11 @@ void HELPER(vmvr_v)(void *vd, void *vs2, CPURISCVState *env, uint32_t desc)
      uint32_t startb = env->vstart * sewb;
      uint32_t i = startb;
 
+    if (env->vstart >= maxsz) {
+        env->vstart = 0;
+        return;
+    }

I think you were right to be concerned about vlmax -- you need to use startb not vstart in this comparison, otherwise you've got mixed units on the comparison.

target/riscv/vector_helper.c void HELPER(vmvr_v)(void *vd, void *vs2, CPURISCVState *env, uint32_t desc) · line 5085

Quoted 3 lines
target/riscv/vector_helper.c +0 -0
      memcpy((uint8_t *)vd + H1(i),
             (uint8_t *)vs2 + H1(i),
             maxsz - startb);

Unrelated to this patch series, this has a big-endian host error. With big-endian, the bytes to be copied may not be sequential.

    if (HOST_BIG_ENDIAN && i % 8 != 0) {
        uint32_t j = ROUND_UP(i, 8);
        memcpy(vd + H(j - 1), vs2 + H1(j - 1), j - i);
        i = j;
    }
    memcpy(vd + i, vs2 + i, maxsz - i);

r~

#7 lore raw

Re: [PATCH for-9.0 v14 4/8] target/riscv/vector_helpers: do early exit when vstart >= vl

In reply to Daniel Henrique Barboza

Hi Daniel,

According the v spec section 15.2 & 15.3.

"The vcpop.m instruction writes x[rd] even if vl=0 (with the value 0, since no mask elements are active).
  Traps on vcpop.m are always reported with a vstart of 0. The vcpop.m instruction will raise an illegal instruction exception if vstart is non-zero."

"The vfirst.m instruction writes x[rd] even if vl=0 (with the value -1, since no mask elements are active).
 Traps on vfirst are always reported with a vstart of 0. The vfirst instruction will raise an illegal instruction exception if vstart is non-zero."

Both the vcpop.m and vfirst.m instructions will raise illegal instruction exception with non-zero vstart.

And currently both the trans_vcpop_m and trans_vfirst_m translate functions check the vstart_eq_zero flag. So I think the early exit checking in the vcpop.m and vfirstm helper functions may be redundant.

target/riscv/vector_helper.c target_ulong HELPER(vcpop_m)(void *v0, void *vs2, CPURISCVState *env, · line 4651

Quoted 12 lines
target/riscv/vector_helper.c +5 -0
@@ -4585,6 +4641,11 @@ target_ulong HELPER(vcpop_m)(void *v0, void *vs2, CPURISCVState *env,
      uint32_t vl = env->vl;
      int i;
 
+    if (env->vstart >= env->vl) {
+        env->vstart = 0;
+        return 0;
+    }
+
      for (i = env->vstart; i < vl; i++) {
          if (vm || vext_elem_mask(v0, i)) {
              if (vext_elem_mask(vs2, i)) {

According v spec section 15.3

""The vfirst.m instruction writes x[rd] even if vl=0 (with the value -1, since no mask elements are active)."

If both the vstart and vl are 0 here, the early exit checking will return the wrong value 0 (the return value should be -1) here.

Quoted 13 lines
target/riscv/vector_helper.c +5 -0
@@ -4604,6 +4665,11 @@ target_ulong HELPER(vfirst_m)(void *v0, void *vs2, CPURISCVState *env,
      uint32_t vl = env->vl;
      int i;
 
+    if (env->vstart >= env->vl) {
+        env->vstart = 0;
+        return 0;
+    }
+
      for (i = env->vstart; i < vl; i++) {
          if (vm || vext_elem_mask(v0, i)) {
              if (vext_elem_mask(vs2, i)) {
#5 lore raw

Re: [PATCH for-9.0 v14 3/8] target/riscv: always clear vstart in whole vec move insns

In reply to Richard Henderson
Quoted 44 lines · Richard Henderson
Quoted 43 lines · Daniel Henrique Barboza

These insns have 2 paths: we'll either have vstart already cleared if vstart_eq_zero or we'll do a brcond to check if vstart >= maxsz to call the 'vmvr_v' helper. The helper will clear vstart if it executes until the end, or if vstart >= vl.

However, if vstart >= maxsz, the helper will be skipped, and vstart won't be cleared since the helper is being responsible from doing it.

We want to make the helpers responsible to manage vstart, including these corner cases, precisely to avoid these situations. Move the vstart

Quoted 1 line

= maxsz cond to the helper, and be sure to clear vstart if that

happens. This way we're now sure that vstart is being cleared in the end of the execution, regardless of the path taken.

Fixes f714361ed7 ("target/riscv: rvv-1.0: implement vstart CSR")

---
  target/riscv/insn_trans/trans_rvv.c.inc | 3 --- target/riscv/vector_helper.c            | 5 +++++

  2 files changed, 5 insertions(+), 3 deletions(-)
target/riscv/insn_trans/trans_rvv.c.inc +0 -3
index 8c16a9f5b3..52c26a7834 100644
@@ -3664,12 +3664,9 @@ static bool trans_##NAME(DisasContext *s, arg_##NAME * a)               \
                                vreg_ofs(s, a->rs2), maxsz, maxsz);        \
               mark_vs_dirty(s);                                           \
           } else {                                                        \
-            TCGLabel *over = gen_new_label();                           \
-            tcg_gen_brcondi_tl(TCG_COND_GEU, cpu_vstart, maxsz, over);  \
               tcg_gen_gvec_2_ptr(vreg_ofs(s, a->rd), vreg_ofs(s, a->rs2), \
                                  tcg_env, maxsz, maxsz, 0, gen_helper_vmvr_v); \
               mark_vs_dirty(s);                                           \
-            gen_set_label(over);                                        \
           }                                                               \
           return true;                                                    \
       }                                                                   \
target/riscv/vector_helper.c +0 -0
index ca79571ae2..cd8235ea98 100644
@@ -5075,6 +5075,11 @@ void HELPER(vmvr_v)(void *vd, void *vs2, CPURISCVState *env, uint32_t desc)
Quoted 8 lines

target/riscv/vector_helper.c void HELPER(vmvr_v)(void *vd, void *vs2, CPURISCVState *env, uint32_t desc) · line 5081

Quoted 6 lines
target/riscv/vector_helper.c +4 -0
       uint32_t startb = env->vstart * sewb;
       uint32_t i = startb;
+    if (env->vstart >= maxsz) {
+        env->vstart = 0;
+        return;
+    }

I think you were right to be concerned about vlmax -- you need to use startb not vstart in this comparison, otherwise you've got mixed units on the comparison.

Hm, so it seems like the comparison is broken in the translation today as well ...

Quoted 7 lines

target/riscv/vector_helper.c void HELPER(vmvr_v)(void *vd, void *vs2, CPURISCVState *env, uint32_t desc) · line 5085

Quoted 4 lines
target/riscv/vector_helper.c +0 -0
       memcpy((uint8_t *)vd + H1(i),
              (uint8_t *)vs2 + H1(i),
              maxsz - startb);

Unrelated to this patch series, this has a big-endian host error. With big-endian, the bytes to be copied may not be sequential.

We're going for v15. Might as well fix this too.

At this point I'll rename the series to "riscv: vector fixes" since there's a lot of stuff going on.

Quoted 7 lines

    if (HOST_BIG_ENDIAN && i % 8 != 0) { uint32_t j = ROUND_UP(i, 8); memcpy(vd + H(j - 1), vs2 + H1(j - 1), j - i); i = j;
    }
    memcpy(vd + i, vs2 + i, maxsz - i);

I'll do an extra patch with it. Thanks,

Daniel

Quoted 3 lines

r~

#8 lore raw

Re: [PATCH for-9.0 v14 4/8] target/riscv/vector_helpers: do early exit when vstart >= vl

In reply to Max Chou
Quoted 30 lines · Max Chou

Hi Daniel,

According the v spec section 15.2 & 15.3.

"The vcpop.m instruction writes x[rd] even if vl=0 (with the value 0, since no mask elements are active).
  Traps on vcpop.m are always reported with a vstart of 0. The vcpop.m instruction will raise an illegal instruction exception if vstart is non-zero."

"The vfirst.m instruction writes x[rd] even if vl=0 (with the value -1, since no mask elements are active).
 Traps on vfirst are always reported with a vstart of 0. The vfirst instruction will raise an illegal instruction exception if vstart is non-zero."

Both the vcpop.m and vfirst.m instructions will raise illegal instruction exception with non-zero vstart.

And currently both the trans_vcpop_m and trans_vfirst_m translate functions check the vstart_eq_zero flag. So I think the early exit checking in the vcpop.m and vfirstm helper functions may be redundant.

target/riscv/vector_helper.c target_ulong HELPER(vcpop_m)(void *v0, void *vs2, CPURISCVState *env, · line 4651

Quoted 12 lines
target/riscv/vector_helper.c +5 -0
@@ -4585,6 +4641,11 @@ target_ulong HELPER(vcpop_m)(void *v0, void *vs2, CPURISCVState *env,
      uint32_t vl = env->vl;
      int i;
 
+    if (env->vstart >= env->vl) {
+        env->vstart = 0;
+        return 0;
+    }
+
      for (i = env->vstart; i < vl; i++) {
          if (vm || vext_elem_mask(v0, i)) {
              if (vext_elem_mask(vs2, i)) {

According v spec section 15.3

Quoted 3 lines

""The vfirst.m instruction writes x[rd] even if vl=0 (with the value -1, since no mask elements are active)."

If both the vstart and vl are 0 here, the early exit checking will return the wrong value 0 (the return value should be -1) here.

Let's just remove these early exits from both vcpop.m and vfirst.m functions then.

Thanks,

Daniel

Quoted 14 lines
Quoted 14 lines
target/riscv/vector_helper.c +5 -0
@@ -4604,6 +4665,11 @@ target_ulong HELPER(vfirst_m)(void *v0, void *vs2, CPURISCVState *env,
      uint32_t vl = env->vl;
      int i;
 
+    if (env->vstart >= env->vl) {
+        env->vstart = 0;
+        return 0;
+    }
+
      for (i = env->vstart; i < vl; i++) {
          if (vm || vext_elem_mask(v0, i)) {
              if (vext_elem_mask(vs2, i)) {
#9 lore raw

Re: [PATCH for-9.0 v14 4/8] target/riscv/vector_helpers: do early exit when vstart >= vl

In reply to Daniel Henrique Barboza

According v spec section 7.9. Vector Load/Store Whole Register Instructions

"The instructions operate with an effective vector length, evl=NFIELDS*VLEN/EEW, regardless of current settings in vtype and vl. The usual property that no elements are written if vstart ≥ vl does not apply to these instructions. Instead, no elements are written if vstart ≥ evl."

The VSTART_CHECK_EARLY_EXIT in vext_ldst_whole function may causes unexpected result. We may replace the VSTART_CHECK_EARLY_EXIT function by

- VSTART_CHECK_EARLY_EXIT(env); + if (env->vstart >= ((vlenb * nf) >> log2_esz)) { + env->vstart = 0; + return; + }

Quoted 388 lines
target/riscv/vector_helper.c +80 -0
@@ -572,6 +580,8 @@ vext_ldst_whole(void *vd, target_ulong base, CPURISCVState *env, uint32_t desc,
      uint32_t vlenb = riscv_cpu_cfg(env)->vlenb;
      uint32_t max_elems = vlenb >> log2_esz;
 
+    VSTART_CHECK_EARLY_EXIT(env);
+
      k = env->vstart / max_elems;
      off = env->vstart % max_elems;
 
@@ -877,6 +887,8 @@ void HELPER(NAME)(void *vd, void *v0, void *vs1, void *vs2,   \
      uint32_t vta = vext_vta(desc);                            \
      uint32_t i;                                               \
                                                                \
+    VSTART_CHECK_EARLY_EXIT(env);                             \
+                                                              \
      for (i = env->vstart; i < vl; i++) {                      \
          ETYPE s1 = *((ETYPE *)vs1 + H(i));                    \
          ETYPE s2 = *((ETYPE *)vs2 + H(i));                    \
@@ -909,6 +921,8 @@ void HELPER(NAME)(void *vd, void *v0, target_ulong s1, void *vs2,        \
      uint32_t vta = vext_vta(desc);                                       \
      uint32_t i;                                                          \
                                                                           \
+    VSTART_CHECK_EARLY_EXIT(env);                                        \
+                                                                         \
      for (i = env->vstart; i < vl; i++) {                                 \
          ETYPE s2 = *((ETYPE *)vs2 + H(i));                               \
          ETYPE carry = vext_elem_mask(v0, i);                             \
@@ -944,6 +958,8 @@ void HELPER(NAME)(void *vd, void *v0, void *vs1, void *vs2,   \
      uint32_t vta_all_1s = vext_vta_all_1s(desc);              \
      uint32_t i;                                               \
                                                                \
+    VSTART_CHECK_EARLY_EXIT(env);                             \
+                                                              \
      for (i = env->vstart; i < vl; i++) {                      \
          ETYPE s1 = *((ETYPE *)vs1 + H(i));                    \
          ETYPE s2 = *((ETYPE *)vs2 + H(i));                    \
@@ -982,6 +998,8 @@ void HELPER(NAME)(void *vd, void *v0, target_ulong s1,          \
      uint32_t vta_all_1s = vext_vta_all_1s(desc);                \
      uint32_t i;                                                 \
                                                                  \
+    VSTART_CHECK_EARLY_EXIT(env);                               \
+                                                                \
      for (i = env->vstart; i < vl; i++) {                        \
          ETYPE s2 = *((ETYPE *)vs2 + H(i));                      \
          ETYPE carry = !vm && vext_elem_mask(v0, i);             \
@@ -1078,6 +1096,8 @@ void HELPER(NAME)(void *vd, void *v0, void *vs1,                          \
      uint32_t vma = vext_vma(desc);                                        \
      uint32_t i;                                                           \
                                                                            \
+    VSTART_CHECK_EARLY_EXIT(env);                                         \
+                                                                          \
      for (i = env->vstart; i < vl; i++) {                                  \
          if (!vm && !vext_elem_mask(v0, i)) {                              \
              /* set masked-off elements to 1s */                           \
@@ -1125,6 +1145,8 @@ void HELPER(NAME)(void *vd, void *v0, target_ulong s1,      \
      uint32_t vma = vext_vma(desc);                          \
      uint32_t i;                                             \
                                                              \
+    VSTART_CHECK_EARLY_EXIT(env);                           \
+                                                            \
      for (i = env->vstart; i < vl; i++) {                    \
          if (!vm && !vext_elem_mask(v0, i)) {                \
              /* set masked-off elements to 1s */             \
@@ -1187,6 +1209,8 @@ void HELPER(NAME)(void *vd, void *v0, void *vs1, void *vs2,   \
      uint32_t vma = vext_vma(desc);                            \
      uint32_t i;                                               \
                                                                \
+    VSTART_CHECK_EARLY_EXIT(env);                             \
+                                                              \
      for (i = env->vstart; i < vl; i++) {                      \
          ETYPE s1 = *((ETYPE *)vs1 + H(i));                    \
          ETYPE s2 = *((ETYPE *)vs2 + H(i));                    \
@@ -1252,6 +1276,8 @@ void HELPER(NAME)(void *vd, void *v0, target_ulong s1, void *vs2,   \
      uint32_t vma = vext_vma(desc);                                  \
      uint32_t i;                                                     \
                                                                      \
+    VSTART_CHECK_EARLY_EXIT(env);                                   \
+                                                                    \
      for (i = env->vstart; i < vl; i++) {                            \
          ETYPE s2 = *((ETYPE *)vs2 + H(i));                          \
          if (!vm && !vext_elem_mask(v0, i)) {                        \
@@ -1799,6 +1825,8 @@ void HELPER(NAME)(void *vd, void *vs1, CPURISCVState *env,           \
      uint32_t vta = vext_vta(desc);                                   \
      uint32_t i;                                                      \
                                                                       \
+    VSTART_CHECK_EARLY_EXIT(env);                                    \
+                                                                     \
      for (i = env->vstart; i < vl; i++) {                             \
          ETYPE s1 = *((ETYPE *)vs1 + H(i));                           \
          *((ETYPE *)vd + H(i)) = s1;                                  \
@@ -1823,6 +1851,8 @@ void HELPER(NAME)(void *vd, uint64_t s1, CPURISCVState *env,         \
      uint32_t vta = vext_vta(desc);                                   \
      uint32_t i;                                                      \
                                                                       \
+    VSTART_CHECK_EARLY_EXIT(env);                                    \
+                                                                     \
      for (i = env->vstart; i < vl; i++) {                             \
          *((ETYPE *)vd + H(i)) = (ETYPE)s1;                           \
      }                                                                \
@@ -1846,6 +1876,8 @@ void HELPER(NAME)(void *vd, void *v0, void *vs1, void *vs2,          \
      uint32_t vta = vext_vta(desc);                                   \
      uint32_t i;                                                      \
                                                                       \
+    VSTART_CHECK_EARLY_EXIT(env);                                    \
+                                                                     \
      for (i = env->vstart; i < vl; i++) {                             \
          ETYPE *vt = (!vext_elem_mask(v0, i) ? vs2 : vs1);            \
          *((ETYPE *)vd + H(i)) = *(vt + H(i));                        \
@@ -1870,6 +1902,8 @@ void HELPER(NAME)(void *vd, void *v0, target_ulong s1,               \
      uint32_t vta = vext_vta(desc);                                   \
      uint32_t i;                                                      \
                                                                       \
+    VSTART_CHECK_EARLY_EXIT(env);                                    \
+                                                                     \
      for (i = env->vstart; i < vl; i++) {                             \
          ETYPE s2 = *((ETYPE *)vs2 + H(i));                           \
          ETYPE d = (!vext_elem_mask(v0, i) ? s2 :                     \
@@ -1915,6 +1949,8 @@ vext_vv_rm_1(void *vd, void *v0, void *vs1, void *vs2,
               uint32_t vl, uint32_t vm, int vxrm,
               opivv2_rm_fn *fn, uint32_t vma, uint32_t esz)
  {
+    VSTART_CHECK_EARLY_EXIT(env);
+
      for (uint32_t i = env->vstart; i < vl; i++) {
          if (!vm && !vext_elem_mask(v0, i)) {
              /* set masked-off elements to 1s */
@@ -2040,6 +2076,8 @@ vext_vx_rm_1(void *vd, void *v0, target_long s1, void *vs2,
               uint32_t vl, uint32_t vm, int vxrm,
               opivx2_rm_fn *fn, uint32_t vma, uint32_t esz)
  {
+    VSTART_CHECK_EARLY_EXIT(env);
+
      for (uint32_t i = env->vstart; i < vl; i++) {
          if (!vm && !vext_elem_mask(v0, i)) {
              /* set masked-off elements to 1s */
@@ -2837,6 +2875,8 @@ void HELPER(NAME)(void *vd, void *v0, void *vs1,          \
      uint32_t vma = vext_vma(desc);                        \
      uint32_t i;                                           \
                                                            \
+    VSTART_CHECK_EARLY_EXIT(env);                         \
+                                                          \
      for (i = env->vstart; i < vl; i++) {                  \
          if (!vm && !vext_elem_mask(v0, i)) {              \
              /* set masked-off elements to 1s */           \
@@ -2880,6 +2920,8 @@ void HELPER(NAME)(void *vd, void *v0, uint64_t s1,        \
      uint32_t vma = vext_vma(desc);                        \
      uint32_t i;                                           \
                                                            \
+    VSTART_CHECK_EARLY_EXIT(env);                         \
+                                                          \
      for (i = env->vstart; i < vl; i++) {                  \
          if (!vm && !vext_elem_mask(v0, i)) {              \
              /* set masked-off elements to 1s */           \
@@ -3466,6 +3508,8 @@ void HELPER(NAME)(void *vd, void *v0, void *vs2,       \
      uint32_t vma = vext_vma(desc);                     \
      uint32_t i;                                        \
                                                         \
+    VSTART_CHECK_EARLY_EXIT(env);                      \
+                                                       \
      if (vl == 0) {                                     \
          return;                                        \
      }                                                  \
@@ -3987,6 +4031,8 @@ void HELPER(NAME)(void *vd, void *v0, void *vs1, void *vs2,   \
      uint32_t vma = vext_vma(desc);                            \
      uint32_t i;                                               \
                                                                \
+    VSTART_CHECK_EARLY_EXIT(env);                             \
+                                                              \
      for (i = env->vstart; i < vl; i++) {                      \
          ETYPE s1 = *((ETYPE *)vs1 + H(i));                    \
          ETYPE s2 = *((ETYPE *)vs2 + H(i));                    \
@@ -4027,6 +4073,8 @@ void HELPER(NAME)(void *vd, void *v0, uint64_t s1, void *vs2,       \
      uint32_t vma = vext_vma(desc);                                  \
      uint32_t i;                                                     \
                                                                      \
+    VSTART_CHECK_EARLY_EXIT(env);                                   \
+                                                                    \
      for (i = env->vstart; i < vl; i++) {                            \
          ETYPE s2 = *((ETYPE *)vs2 + H(i));                          \
          if (!vm && !vext_elem_mask(v0, i)) {                        \
@@ -4220,6 +4268,8 @@ void HELPER(NAME)(void *vd, void *v0, uint64_t s1, void *vs2, \
      uint32_t vta = vext_vta(desc);                            \
      uint32_t i;                                               \
                                                                \
+    VSTART_CHECK_EARLY_EXIT(env);                             \
+                                                              \
      for (i = env->vstart; i < vl; i++) {                      \
          ETYPE s2 = *((ETYPE *)vs2 + H(i));                    \
          *((ETYPE *)vd + H(i)) =                               \
@@ -4386,6 +4436,8 @@ void HELPER(NAME)(void *vd, void *v0, void *vs1,          \
      uint32_t i;                                           \
      TD s1 =  *((TD *)vs1 + HD(0));                        \
                                                            \
+    VSTART_CHECK_EARLY_EXIT(env);                         \
+                                                          \
      for (i = env->vstart; i < vl; i++) {                  \
          TS2 s2 = *((TS2 *)vs2 + HS2(i));                  \
          if (!vm && !vext_elem_mask(v0, i)) {              \
@@ -4472,6 +4524,8 @@ void HELPER(NAME)(void *vd, void *v0, void *vs1,           \
      uint32_t i;                                            \
      TD s1 =  *((TD *)vs1 + HD(0));                         \
                                                             \
+    VSTART_CHECK_EARLY_EXIT(env);                          \
+                                                           \
      for (i = env->vstart; i < vl; i++) {                   \
          TS2 s2 = *((TS2 *)vs2 + HS2(i));                   \
          if (!vm && !vext_elem_mask(v0, i)) {               \
@@ -4544,6 +4598,8 @@ void HELPER(NAME)(void *vd, void *v0, void *vs1,          \
      uint32_t i;                                           \
      int a, b;                                             \
                                                            \
+    VSTART_CHECK_EARLY_EXIT(env);                         \
+                                                          \
      for (i = env->vstart; i < vl; i++) {                  \
          a = vext_elem_mask(vs1, i);                       \
          b = vext_elem_mask(vs2, i);                       \
@@ -4585,6 +4641,11 @@ target_ulong HELPER(vcpop_m)(void *v0, void *vs2, CPURISCVState *env,
      uint32_t vl = env->vl;
      int i;
 
+    if (env->vstart >= env->vl) {
+        env->vstart = 0;
+        return 0;
+    }
+
      for (i = env->vstart; i < vl; i++) {
          if (vm || vext_elem_mask(v0, i)) {
              if (vext_elem_mask(vs2, i)) {
@@ -4604,6 +4665,11 @@ target_ulong HELPER(vfirst_m)(void *v0, void *vs2, CPURISCVState *env,
      uint32_t vl = env->vl;
      int i;
 
+    if (env->vstart >= env->vl) {
+        env->vstart = 0;
+        return 0;
+    }
+
      for (i = env->vstart; i < vl; i++) {
          if (vm || vext_elem_mask(v0, i)) {
              if (vext_elem_mask(vs2, i)) {
@@ -4632,6 +4698,8 @@ static void vmsetm(void *vd, void *v0, void *vs2, CPURISCVState *env,
      int i;
      bool first_mask_bit = false;
 
+    VSTART_CHECK_EARLY_EXIT(env);
+
      for (i = env->vstart; i < vl; i++) {
          if (!vm && !vext_elem_mask(v0, i)) {
              /* set masked-off elements to 1s */
@@ -4704,6 +4772,8 @@ void HELPER(NAME)(void *vd, void *v0, void *vs2, CPURISCVState *env,      \
      uint32_t sum = 0;                                                     \
      int i;                                                                \
                                                                            \
+    VSTART_CHECK_EARLY_EXIT(env);                                         \
+                                                                          \
      for (i = env->vstart; i < vl; i++) {                                  \
          if (!vm && !vext_elem_mask(v0, i)) {                              \
              /* set masked-off elements to 1s */                           \
@@ -4737,6 +4807,8 @@ void HELPER(NAME)(void *vd, void *v0, CPURISCVState *env, uint32_t desc)  \
      uint32_t vma = vext_vma(desc);                                        \
      int i;                                                                \
                                                                            \
+    VSTART_CHECK_EARLY_EXIT(env);                                         \
+                                                                          \
      for (i = env->vstart; i < vl; i++) {                                  \
          if (!vm && !vext_elem_mask(v0, i)) {                              \
              /* set masked-off elements to 1s */                           \
@@ -4772,6 +4844,8 @@ void HELPER(NAME)(void *vd, void *v0, target_ulong s1, void *vs2,         \
      uint32_t vma = vext_vma(desc);                                        \
      target_ulong offset = s1, i_min, i;                                   \
                                                                            \
+    VSTART_CHECK_EARLY_EXIT(env);                                         \
+                                                                          \
      i_min = MAX(env->vstart, offset);                                     \
      for (i = i_min; i < vl; i++) {                                        \
          if (!vm && !vext_elem_mask(v0, i)) {                              \
@@ -4805,6 +4879,8 @@ void HELPER(NAME)(void *vd, void *v0, target_ulong s1, void *vs2,         \
      uint32_t vma = vext_vma(desc);                                        \
      target_ulong i_max, i_min, i;                                         \
                                                                            \
+    VSTART_CHECK_EARLY_EXIT(env);                                         \
+                                                                          \
      i_min = MIN(s1 < vlmax ? vlmax - s1 : 0, vl);                         \
      i_max = MAX(i_min, env->vstart);                                      \
      for (i = env->vstart; i < i_max; ++i) {                               \
@@ -4847,6 +4923,8 @@ static void vslide1up_##BITWIDTH(void *vd, void *v0, uint64_t s1,           \
      uint32_t vma = vext_vma(desc);                                          \
      uint32_t i;                                                             \
                                                                              \
+    VSTART_CHECK_EARLY_EXIT(env);                                           \
+                                                                            \
      for (i = env->vstart; i < vl; i++) {                                    \
          if (!vm && !vext_elem_mask(v0, i)) {                                \
              /* set masked-off elements to 1s */                             \
@@ -4896,6 +4974,8 @@ static void vslide1down_##BITWIDTH(void *vd, void *v0, uint64_t s1,           \
      uint32_t vma = vext_vma(desc);                                            \
      uint32_t i;                                                               \
                                                                                \
+    VSTART_CHECK_EARLY_EXIT(env);                                             \
+                                                                              \
      for (i = env->vstart; i < vl; i++) {                                      \
          if (!vm && !vext_elem_mask(v0, i)) {                                  \
              /* set masked-off elements to 1s */                               \
@@ -4971,6 +5051,8 @@ void HELPER(NAME)(void *vd, void *v0, void *vs1, void *vs2,               \
      uint64_t index;                                                       \
      uint32_t i;                                                           \
                                                                            \
+    VSTART_CHECK_EARLY_EXIT(env);                                         \
+                                                                          \
      for (i = env->vstart; i < vl; i++) {                                  \
          if (!vm && !vext_elem_mask(v0, i)) {                              \
              /* set masked-off elements to 1s */                           \
@@ -5014,6 +5096,8 @@ void HELPER(NAME)(void *vd, void *v0, target_ulong s1, void *vs2,         \
      uint64_t index = s1;                                                  \
      uint32_t i;                                                           \
                                                                            \
+    VSTART_CHECK_EARLY_EXIT(env);                                         \
+                                                                          \
      for (i = env->vstart; i < vl; i++) {                                  \
          if (!vm && !vext_elem_mask(v0, i)) {                              \
              /* set masked-off elements to 1s */                           \
@@ -5048,6 +5132,8 @@ void HELPER(NAME)(void *vd, void *v0, void *vs1, void *vs2,               \
      uint32_t vta = vext_vta(desc);                                        \
      uint32_t num = 0, i;                                                  \
                                                                            \
+    VSTART_CHECK_EARLY_EXIT(env);                                         \
+                                                                          \
      for (i = env->vstart; i < vl; i++) {                                  \
          if (!vext_elem_mask(vs1, i)) {                                    \
              continue;                                                     \
@@ -5100,6 +5186,8 @@ void HELPER(NAME)(void *vd, void *v0, void *vs2,                 \
      uint32_t vma = vext_vma(desc);                               \
      uint32_t i;                                                  \
                                                                   \
+    VSTART_CHECK_EARLY_EXIT(env);                                \
+                                                                 \
      for (i = env->vstart; i < vl; i++) {                         \
          if (!vm && !vext_elem_mask(v0, i)) {                     \
              /* set masked-off elements to 1s */                  \
target/riscv/vector_internals.c +4 -0
index 12f5964fbb..996c21eb31 100644
@@ -44,6 +44,8 @@ void do_vext_vv(void *vd, void *v0, void *vs1, void *vs2,
      uint32_t vma = vext_vma(desc);
      uint32_t i;
 
+    VSTART_CHECK_EARLY_EXIT(env);
+
      for (i = env->vstart; i < vl; i++) {
          if (!vm && !vext_elem_mask(v0, i)) {
              /* set masked-off elements to 1s */
@@ -68,6 +70,8 @@ void do_vext_vx(void *vd, void *v0, target_long s1, void *vs2,
      uint32_t vma = vext_vma(desc);
      uint32_t i;
 
+    VSTART_CHECK_EARLY_EXIT(env);
+
      for (i = env->vstart; i < vl; i++) {
          if (!vm && !vext_elem_mask(v0, i)) {
              /* set masked-off elements to 1s */
target/riscv/vector_internals.h +9 -0
index 842765f6c1..9e1e15b575 100644
@@ -24,6 +24,13 @@
  #include "tcg/tcg-gvec-desc.h"
  #include "internals.h"
 
+#define VSTART_CHECK_EARLY_EXIT(env) do { \
+    if (env->vstart >= env->vl) {         \
+        env->vstart = 0;                  \
+        return;                           \
+    }                                     \
+} while (0)
+
  static inline uint32_t vext_nf(uint32_t desc)
  {
      return FIELD_EX32(simd_data(desc), VDATA, NF);
@@ -151,6 +158,8 @@ void HELPER(NAME)(void *vd, void *v0, void *vs2,       \
      uint32_t vma = vext_vma(desc);                     \
      uint32_t i;                                        \
                                                         \
+    VSTART_CHECK_EARLY_EXIT(env);                      \
+                                                       \
      for (i = env->vstart; i < vl; i++) {               \
          if (!vm && !vext_elem_mask(v0, i)) {           \
              /* set masked-off elements to 1s */        \
#10 lore raw

Re: [PATCH for-9.0 v14 4/8] target/riscv/vector_helpers: do early exit when vstart >= vl

Quoted 7 lines · Max Chou

According v spec section 7.9. Vector Load/Store Whole Register Instructions

"The instructions operate with an effective vector length, evl=NFIELDS*VLEN/EEW, regardless of current settings in vtype and vl. The usual property that no elements are written if vstart ≥ vl does not apply to these instructions. Instead, no elements are written if vstart ≥ evl."

The VSTART_CHECK_EARLY_EXIT in vext_ldst_whole function may causes unexpected result. We may replace the VSTART_CHECK_EARLY_EXIT function by

- VSTART_CHECK_EARLY_EXIT(env); + if (env->vstart >= ((vlenb * nf) >> log2_esz)) { + env->vstart = 0; + return; + }

Do we need to do an early exit in this case? If the function is able to handle gracefully whatever env->vstart value it faces (it seems to be the case) then we should just remove the exit entirely.

In fact I removed all the early exits from all helpers that are guarded by vstart_eq_zero: vcpop_m(), vfirst_m(), vmsetm(), GEN_VEXT_VIOTA_M(), GEN_VEXT_VCOMPRESS_VM(), GEN_VEXT_RED() and GEN_VEXT_FRED(). For these case the helpers can either do nothing if vl = 0 or throw some exception like vcpop and first does.

Thanks,

Daniel

Quoted 389 lines
Quoted 389 lines
target/riscv/vector_helper.c +80 -0
@@ -572,6 +580,8 @@ vext_ldst_whole(void *vd, target_ulong base, CPURISCVState *env, uint32_t desc,
      uint32_t vlenb = riscv_cpu_cfg(env)->vlenb;
      uint32_t max_elems = vlenb >> log2_esz;
 
+    VSTART_CHECK_EARLY_EXIT(env);
+
      k = env->vstart / max_elems;
      off = env->vstart % max_elems;
 
@@ -877,6 +887,8 @@ void HELPER(NAME)(void *vd, void *v0, void *vs1, void *vs2,   \
      uint32_t vta = vext_vta(desc);                            \
      uint32_t i;                                               \
                                                                \
+    VSTART_CHECK_EARLY_EXIT(env);                             \
+                                                              \
      for (i = env->vstart; i < vl; i++) {                      \
          ETYPE s1 = *((ETYPE *)vs1 + H(i));                    \
          ETYPE s2 = *((ETYPE *)vs2 + H(i));                    \
@@ -909,6 +921,8 @@ void HELPER(NAME)(void *vd, void *v0, target_ulong s1, void *vs2,        \
      uint32_t vta = vext_vta(desc);                                       \
      uint32_t i;                                                          \
                                                                           \
+    VSTART_CHECK_EARLY_EXIT(env);                                        \
+                                                                         \
      for (i = env->vstart; i < vl; i++) {                                 \
          ETYPE s2 = *((ETYPE *)vs2 + H(i));                               \
          ETYPE carry = vext_elem_mask(v0, i);                             \
@@ -944,6 +958,8 @@ void HELPER(NAME)(void *vd, void *v0, void *vs1, void *vs2,   \
      uint32_t vta_all_1s = vext_vta_all_1s(desc);              \
      uint32_t i;                                               \
                                                                \
+    VSTART_CHECK_EARLY_EXIT(env);                             \
+                                                              \
      for (i = env->vstart; i < vl; i++) {                      \
          ETYPE s1 = *((ETYPE *)vs1 + H(i));                    \
          ETYPE s2 = *((ETYPE *)vs2 + H(i));                    \
@@ -982,6 +998,8 @@ void HELPER(NAME)(void *vd, void *v0, target_ulong s1,          \
      uint32_t vta_all_1s = vext_vta_all_1s(desc);                \
      uint32_t i;                                                 \
                                                                  \
+    VSTART_CHECK_EARLY_EXIT(env);                               \
+                                                                \
      for (i = env->vstart; i < vl; i++) {                        \
          ETYPE s2 = *((ETYPE *)vs2 + H(i));                      \
          ETYPE carry = !vm && vext_elem_mask(v0, i);             \
@@ -1078,6 +1096,8 @@ void HELPER(NAME)(void *vd, void *v0, void *vs1,                          \
      uint32_t vma = vext_vma(desc);                                        \
      uint32_t i;                                                           \
                                                                            \
+    VSTART_CHECK_EARLY_EXIT(env);                                         \
+                                                                          \
      for (i = env->vstart; i < vl; i++) {                                  \
          if (!vm && !vext_elem_mask(v0, i)) {                              \
              /* set masked-off elements to 1s */                           \
@@ -1125,6 +1145,8 @@ void HELPER(NAME)(void *vd, void *v0, target_ulong s1,      \
      uint32_t vma = vext_vma(desc);                          \
      uint32_t i;                                             \
                                                              \
+    VSTART_CHECK_EARLY_EXIT(env);                           \
+                                                            \
      for (i = env->vstart; i < vl; i++) {                    \
          if (!vm && !vext_elem_mask(v0, i)) {                \
              /* set masked-off elements to 1s */             \
@@ -1187,6 +1209,8 @@ void HELPER(NAME)(void *vd, void *v0, void *vs1, void *vs2,   \
      uint32_t vma = vext_vma(desc);                            \
      uint32_t i;                                               \
                                                                \
+    VSTART_CHECK_EARLY_EXIT(env);                             \
+                                                              \
      for (i = env->vstart; i < vl; i++) {                      \
          ETYPE s1 = *((ETYPE *)vs1 + H(i));                    \
          ETYPE s2 = *((ETYPE *)vs2 + H(i));                    \
@@ -1252,6 +1276,8 @@ void HELPER(NAME)(void *vd, void *v0, target_ulong s1, void *vs2,   \
      uint32_t vma = vext_vma(desc);                                  \
      uint32_t i;                                                     \
                                                                      \
+    VSTART_CHECK_EARLY_EXIT(env);                                   \
+                                                                    \
      for (i = env->vstart; i < vl; i++) {                            \
          ETYPE s2 = *((ETYPE *)vs2 + H(i));                          \
          if (!vm && !vext_elem_mask(v0, i)) {                        \
@@ -1799,6 +1825,8 @@ void HELPER(NAME)(void *vd, void *vs1, CPURISCVState *env,           \
      uint32_t vta = vext_vta(desc);                                   \
      uint32_t i;                                                      \
                                                                       \
+    VSTART_CHECK_EARLY_EXIT(env);                                    \
+                                                                     \
      for (i = env->vstart; i < vl; i++) {                             \
          ETYPE s1 = *((ETYPE *)vs1 + H(i));                           \
          *((ETYPE *)vd + H(i)) = s1;                                  \
@@ -1823,6 +1851,8 @@ void HELPER(NAME)(void *vd, uint64_t s1, CPURISCVState *env,         \
      uint32_t vta = vext_vta(desc);                                   \
      uint32_t i;                                                      \
                                                                       \
+    VSTART_CHECK_EARLY_EXIT(env);                                    \
+                                                                     \
      for (i = env->vstart; i < vl; i++) {                             \
          *((ETYPE *)vd + H(i)) = (ETYPE)s1;                           \
      }                                                                \
@@ -1846,6 +1876,8 @@ void HELPER(NAME)(void *vd, void *v0, void *vs1, void *vs2,          \
      uint32_t vta = vext_vta(desc);                                   \
      uint32_t i;                                                      \
                                                                       \
+    VSTART_CHECK_EARLY_EXIT(env);                                    \
+                                                                     \
      for (i = env->vstart; i < vl; i++) {                             \
          ETYPE *vt = (!vext_elem_mask(v0, i) ? vs2 : vs1);            \
          *((ETYPE *)vd + H(i)) = *(vt + H(i));                        \
@@ -1870,6 +1902,8 @@ void HELPER(NAME)(void *vd, void *v0, target_ulong s1,               \
      uint32_t vta = vext_vta(desc);                                   \
      uint32_t i;                                                      \
                                                                       \
+    VSTART_CHECK_EARLY_EXIT(env);                                    \
+                                                                     \
      for (i = env->vstart; i < vl; i++) {                             \
          ETYPE s2 = *((ETYPE *)vs2 + H(i));                           \
          ETYPE d = (!vext_elem_mask(v0, i) ? s2 :                     \
@@ -1915,6 +1949,8 @@ vext_vv_rm_1(void *vd, void *v0, void *vs1, void *vs2,
               uint32_t vl, uint32_t vm, int vxrm,
               opivv2_rm_fn *fn, uint32_t vma, uint32_t esz)
  {
+    VSTART_CHECK_EARLY_EXIT(env);
+
      for (uint32_t i = env->vstart; i < vl; i++) {
          if (!vm && !vext_elem_mask(v0, i)) {
              /* set masked-off elements to 1s */
@@ -2040,6 +2076,8 @@ vext_vx_rm_1(void *vd, void *v0, target_long s1, void *vs2,
               uint32_t vl, uint32_t vm, int vxrm,
               opivx2_rm_fn *fn, uint32_t vma, uint32_t esz)
  {
+    VSTART_CHECK_EARLY_EXIT(env);
+
      for (uint32_t i = env->vstart; i < vl; i++) {
          if (!vm && !vext_elem_mask(v0, i)) {
              /* set masked-off elements to 1s */
@@ -2837,6 +2875,8 @@ void HELPER(NAME)(void *vd, void *v0, void *vs1,          \
      uint32_t vma = vext_vma(desc);                        \
      uint32_t i;                                           \
                                                            \
+    VSTART_CHECK_EARLY_EXIT(env);                         \
+                                                          \
      for (i = env->vstart; i < vl; i++) {                  \
          if (!vm && !vext_elem_mask(v0, i)) {              \
              /* set masked-off elements to 1s */           \
@@ -2880,6 +2920,8 @@ void HELPER(NAME)(void *vd, void *v0, uint64_t s1,        \
      uint32_t vma = vext_vma(desc);                        \
      uint32_t i;                                           \
                                                            \
+    VSTART_CHECK_EARLY_EXIT(env);                         \
+                                                          \
      for (i = env->vstart; i < vl; i++) {                  \
          if (!vm && !vext_elem_mask(v0, i)) {              \
              /* set masked-off elements to 1s */           \
@@ -3466,6 +3508,8 @@ void HELPER(NAME)(void *vd, void *v0, void *vs2,       \
      uint32_t vma = vext_vma(desc);                     \
      uint32_t i;                                        \
                                                         \
+    VSTART_CHECK_EARLY_EXIT(env);                      \
+                                                       \
      if (vl == 0) {                                     \
          return;                                        \
      }                                                  \
@@ -3987,6 +4031,8 @@ void HELPER(NAME)(void *vd, void *v0, void *vs1, void *vs2,   \
      uint32_t vma = vext_vma(desc);                            \
      uint32_t i;                                               \
                                                                \
+    VSTART_CHECK_EARLY_EXIT(env);                             \
+                                                              \
      for (i = env->vstart; i < vl; i++) {                      \
          ETYPE s1 = *((ETYPE *)vs1 + H(i));                    \
          ETYPE s2 = *((ETYPE *)vs2 + H(i));                    \
@@ -4027,6 +4073,8 @@ void HELPER(NAME)(void *vd, void *v0, uint64_t s1, void *vs2,       \
      uint32_t vma = vext_vma(desc);                                  \
      uint32_t i;                                                     \
                                                                      \
+    VSTART_CHECK_EARLY_EXIT(env);                                   \
+                                                                    \
      for (i = env->vstart; i < vl; i++) {                            \
          ETYPE s2 = *((ETYPE *)vs2 + H(i));                          \
          if (!vm && !vext_elem_mask(v0, i)) {                        \
@@ -4220,6 +4268,8 @@ void HELPER(NAME)(void *vd, void *v0, uint64_t s1, void *vs2, \
      uint32_t vta = vext_vta(desc);                            \
      uint32_t i;                                               \
                                                                \
+    VSTART_CHECK_EARLY_EXIT(env);                             \
+                                                              \
      for (i = env->vstart; i < vl; i++) {                      \
          ETYPE s2 = *((ETYPE *)vs2 + H(i));                    \
          *((ETYPE *)vd + H(i)) =                               \
@@ -4386,6 +4436,8 @@ void HELPER(NAME)(void *vd, void *v0, void *vs1,          \
      uint32_t i;                                           \
      TD s1 =  *((TD *)vs1 + HD(0));                        \
                                                            \
+    VSTART_CHECK_EARLY_EXIT(env);                         \
+                                                          \
      for (i = env->vstart; i < vl; i++) {                  \
          TS2 s2 = *((TS2 *)vs2 + HS2(i));                  \
          if (!vm && !vext_elem_mask(v0, i)) {              \
@@ -4472,6 +4524,8 @@ void HELPER(NAME)(void *vd, void *v0, void *vs1,           \
      uint32_t i;                                            \
      TD s1 =  *((TD *)vs1 + HD(0));                         \
                                                             \
+    VSTART_CHECK_EARLY_EXIT(env);                          \
+                                                           \
      for (i = env->vstart; i < vl; i++) {                   \
          TS2 s2 = *((TS2 *)vs2 + HS2(i));                   \
          if (!vm && !vext_elem_mask(v0, i)) {               \
@@ -4544,6 +4598,8 @@ void HELPER(NAME)(void *vd, void *v0, void *vs1,          \
      uint32_t i;                                           \
      int a, b;                                             \
                                                            \
+    VSTART_CHECK_EARLY_EXIT(env);                         \
+                                                          \
      for (i = env->vstart; i < vl; i++) {                  \
          a = vext_elem_mask(vs1, i);                       \
          b = vext_elem_mask(vs2, i);                       \
@@ -4585,6 +4641,11 @@ target_ulong HELPER(vcpop_m)(void *v0, void *vs2, CPURISCVState *env,
      uint32_t vl = env->vl;
      int i;
 
+    if (env->vstart >= env->vl) {
+        env->vstart = 0;
+        return 0;
+    }
+
      for (i = env->vstart; i < vl; i++) {
          if (vm || vext_elem_mask(v0, i)) {
              if (vext_elem_mask(vs2, i)) {
@@ -4604,6 +4665,11 @@ target_ulong HELPER(vfirst_m)(void *v0, void *vs2, CPURISCVState *env,
      uint32_t vl = env->vl;
      int i;
 
+    if (env->vstart >= env->vl) {
+        env->vstart = 0;
+        return 0;
+    }
+
      for (i = env->vstart; i < vl; i++) {
          if (vm || vext_elem_mask(v0, i)) {
              if (vext_elem_mask(vs2, i)) {
@@ -4632,6 +4698,8 @@ static void vmsetm(void *vd, void *v0, void *vs2, CPURISCVState *env,
      int i;
      bool first_mask_bit = false;
 
+    VSTART_CHECK_EARLY_EXIT(env);
+
      for (i = env->vstart; i < vl; i++) {
          if (!vm && !vext_elem_mask(v0, i)) {
              /* set masked-off elements to 1s */
@@ -4704,6 +4772,8 @@ void HELPER(NAME)(void *vd, void *v0, void *vs2, CPURISCVState *env,      \
      uint32_t sum = 0;                                                     \
      int i;                                                                \
                                                                            \
+    VSTART_CHECK_EARLY_EXIT(env);                                         \
+                                                                          \
      for (i = env->vstart; i < vl; i++) {                                  \
          if (!vm && !vext_elem_mask(v0, i)) {                              \
              /* set masked-off elements to 1s */                           \
@@ -4737,6 +4807,8 @@ void HELPER(NAME)(void *vd, void *v0, CPURISCVState *env, uint32_t desc)  \
      uint32_t vma = vext_vma(desc);                                        \
      int i;                                                                \
                                                                            \
+    VSTART_CHECK_EARLY_EXIT(env);                                         \
+                                                                          \
      for (i = env->vstart; i < vl; i++) {                                  \
          if (!vm && !vext_elem_mask(v0, i)) {                              \
              /* set masked-off elements to 1s */                           \
@@ -4772,6 +4844,8 @@ void HELPER(NAME)(void *vd, void *v0, target_ulong s1, void *vs2,         \
      uint32_t vma = vext_vma(desc);                                        \
      target_ulong offset = s1, i_min, i;                                   \
                                                                            \
+    VSTART_CHECK_EARLY_EXIT(env);                                         \
+                                                                          \
      i_min = MAX(env->vstart, offset);                                     \
      for (i = i_min; i < vl; i++) {                                        \
          if (!vm && !vext_elem_mask(v0, i)) {                              \
@@ -4805,6 +4879,8 @@ void HELPER(NAME)(void *vd, void *v0, target_ulong s1, void *vs2,         \
      uint32_t vma = vext_vma(desc);                                        \
      target_ulong i_max, i_min, i;                                         \
                                                                            \
+    VSTART_CHECK_EARLY_EXIT(env);                                         \
+                                                                          \
      i_min = MIN(s1 < vlmax ? vlmax - s1 : 0, vl);                         \
      i_max = MAX(i_min, env->vstart);                                      \
      for (i = env->vstart; i < i_max; ++i) {                               \
@@ -4847,6 +4923,8 @@ static void vslide1up_##BITWIDTH(void *vd, void *v0, uint64_t s1,           \
      uint32_t vma = vext_vma(desc);                                          \
      uint32_t i;                                                             \
                                                                              \
+    VSTART_CHECK_EARLY_EXIT(env);                                           \
+                                                                            \
      for (i = env->vstart; i < vl; i++) {                                    \
          if (!vm && !vext_elem_mask(v0, i)) {                                \
              /* set masked-off elements to 1s */                             \
@@ -4896,6 +4974,8 @@ static void vslide1down_##BITWIDTH(void *vd, void *v0, uint64_t s1,           \
      uint32_t vma = vext_vma(desc);                                            \
      uint32_t i;                                                               \
                                                                                \
+    VSTART_CHECK_EARLY_EXIT(env);                                             \
+                                                                              \
      for (i = env->vstart; i < vl; i++) {                                      \
          if (!vm && !vext_elem_mask(v0, i)) {                                  \
              /* set masked-off elements to 1s */                               \
@@ -4971,6 +5051,8 @@ void HELPER(NAME)(void *vd, void *v0, void *vs1, void *vs2,               \
      uint64_t index;                                                       \
      uint32_t i;                                                           \
                                                                            \
+    VSTART_CHECK_EARLY_EXIT(env);                                         \
+                                                                          \
      for (i = env->vstart; i < vl; i++) {                                  \
          if (!vm && !vext_elem_mask(v0, i)) {                              \
              /* set masked-off elements to 1s */                           \
@@ -5014,6 +5096,8 @@ void HELPER(NAME)(void *vd, void *v0, target_ulong s1, void *vs2,         \
      uint64_t index = s1;                                                  \
      uint32_t i;                                                           \
                                                                            \
+    VSTART_CHECK_EARLY_EXIT(env);                                         \
+                                                                          \
      for (i = env->vstart; i < vl; i++) {                                  \
          if (!vm && !vext_elem_mask(v0, i)) {                              \
              /* set masked-off elements to 1s */                           \
@@ -5048,6 +5132,8 @@ void HELPER(NAME)(void *vd, void *v0, void *vs1, void *vs2,               \
      uint32_t vta = vext_vta(desc);                                        \
      uint32_t num = 0, i;                                                  \
                                                                            \
+    VSTART_CHECK_EARLY_EXIT(env);                                         \
+                                                                          \
      for (i = env->vstart; i < vl; i++) {                                  \
          if (!vext_elem_mask(vs1, i)) {                                    \
              continue;                                                     \
@@ -5100,6 +5186,8 @@ void HELPER(NAME)(void *vd, void *v0, void *vs2,                 \
      uint32_t vma = vext_vma(desc);                               \
      uint32_t i;                                                  \
                                                                   \
+    VSTART_CHECK_EARLY_EXIT(env);                                \
+                                                                 \
      for (i = env->vstart; i < vl; i++) {                         \
          if (!vm && !vext_elem_mask(v0, i)) {                     \
              /* set masked-off elements to 1s */                  \
target/riscv/vector_internals.c +4 -0
index 12f5964fbb..996c21eb31 100644
@@ -44,6 +44,8 @@ void do_vext_vv(void *vd, void *v0, void *vs1, void *vs2,
      uint32_t vma = vext_vma(desc);
      uint32_t i;
 
+    VSTART_CHECK_EARLY_EXIT(env);
+
      for (i = env->vstart; i < vl; i++) {
          if (!vm && !vext_elem_mask(v0, i)) {
              /* set masked-off elements to 1s */
@@ -68,6 +70,8 @@ void do_vext_vx(void *vd, void *v0, target_long s1, void *vs2,
      uint32_t vma = vext_vma(desc);
      uint32_t i;
 
+    VSTART_CHECK_EARLY_EXIT(env);
+
      for (i = env->vstart; i < vl; i++) {
          if (!vm && !vext_elem_mask(v0, i)) {
              /* set masked-off elements to 1s */
target/riscv/vector_internals.h +9 -0
index 842765f6c1..9e1e15b575 100644
@@ -24,6 +24,13 @@
  #include "tcg/tcg-gvec-desc.h"
  #include "internals.h"
 
+#define VSTART_CHECK_EARLY_EXIT(env) do { \
+    if (env->vstart >= env->vl) {         \
+        env->vstart = 0;                  \
+        return;                           \
+    }                                     \
+} while (0)
+
  static inline uint32_t vext_nf(uint32_t desc)
  {
      return FIELD_EX32(simd_data(desc), VDATA, NF);
@@ -151,6 +158,8 @@ void HELPER(NAME)(void *vd, void *v0, void *vs2,       \
      uint32_t vma = vext_vma(desc);                     \
      uint32_t i;                                        \
                                                         \
+    VSTART_CHECK_EARLY_EXIT(env);                      \
+                                                       \
      for (i = env->vstart; i < vl; i++) {               \
          if (!vm && !vext_elem_mask(v0, i)) {           \
              /* set masked-off elements to 1s */        \
#11 lore raw

Re: [PATCH for-9.0 v14 4/8] target/riscv/vector_helpers: do early exit when vstart >= vl

Quoted 7 lines · Daniel Henrique Barboza
Quoted 4 lines · Max Chou

According v spec section 7.9. Vector Load/Store Whole Register Instructions

"The instructions operate with an effective vector length, evl=NFIELDS*VLEN/EEW, regardless of current settings in vtype and vl. The usual property that no elements are written if vstart ≥ vl does not apply to these instructions. Instead, no elements are written if vstart ≥ evl."

Quoted 7 lines
Quoted 3 lines

The VSTART_CHECK_EARLY_EXIT in vext_ldst_whole function may causes unexpected result. We may replace the VSTART_CHECK_EARLY_EXIT function by

- VSTART_CHECK_EARLY_EXIT(env); + if (env->vstart >= ((vlenb * nf) >> log2_esz)) { + env->vstart = 0; + return; + }

Do we need to do an early exit in this case? If the function is able to handle gracefully whatever env->vstart value it faces (it seems to be the case) then we should just remove the exit entirely.

Nevermind. Here's the guard that I am removing in the next patch for vext_lsdt_whole:

-    uint32_t evl = s->cfg_ptr->vlenb * nf / width;
-    TCGLabel *over = gen_new_label();
-    tcg_gen_brcondi_tl(TCG_COND_GEU, cpu_vstart, evl, over);

If we just remove it and put nothing else in its place we'll end up breaking it.

To preserve the existing behavior we'll have to change the early exit to

if (vstart >= evl) {...}

'evl' is being calculated as

    uint32_t evl = s->cfg_ptr->vlenb * nf / width;

So yeah, your suggestion to exit the helper early with:

+ if (env->vstart >= ((vlenb * nf) >> log2_esz)) {
+ env->vstart = 0;
+ return;
+ }

Is correct. I'll change it in v15.

Thanks,

Daniel

Quoted 395 lines

In fact I removed all the early exits from all helpers that are guarded by vstart_eq_zero: vcpop_m(), vfirst_m(), vmsetm(), GEN_VEXT_VIOTA_M(), GEN_VEXT_VCOMPRESS_VM(), GEN_VEXT_RED() and GEN_VEXT_FRED(). For these case the helpers can either do nothing if vl = 0 or throw some exception like vcpop and first does.

Thanks,

Daniel

Quoted 381 lines
Quoted 381 lines
target/riscv/vector_helper.c +80 -0
@@ -572,6 +580,8 @@ vext_ldst_whole(void *vd, target_ulong base, CPURISCVState *env, uint32_t desc,
       uint32_t vlenb = riscv_cpu_cfg(env)->vlenb;
       uint32_t max_elems = vlenb >> log2_esz;
+    VSTART_CHECK_EARLY_EXIT(env);
+
       k = env->vstart / max_elems;
       off = env->vstart % max_elems;
@@ -877,6 +887,8 @@ void HELPER(NAME)(void *vd, void *v0, void *vs1, void *vs2,   \
       uint32_t vta = vext_vta(desc);                            \
       uint32_t i;                                               \
                                                                 \
+    VSTART_CHECK_EARLY_EXIT(env);                             \
+                                                              \
       for (i = env->vstart; i < vl; i++) {                      \
           ETYPE s1 = *((ETYPE *)vs1 + H(i));                    \
           ETYPE s2 = *((ETYPE *)vs2 + H(i));                    \
@@ -909,6 +921,8 @@ void HELPER(NAME)(void *vd, void *v0, target_ulong s1, void *vs2,        \
       uint32_t vta = vext_vta(desc);                                       \
       uint32_t i;                                                          \
                                                                            \
+    VSTART_CHECK_EARLY_EXIT(env);                                        \
+                                                                         \
       for (i = env->vstart; i < vl; i++) {                                 \
           ETYPE s2 = *((ETYPE *)vs2 + H(i));                               \
           ETYPE carry = vext_elem_mask(v0, i);                             \
@@ -944,6 +958,8 @@ void HELPER(NAME)(void *vd, void *v0, void *vs1, void *vs2,   \
       uint32_t vta_all_1s = vext_vta_all_1s(desc);              \
       uint32_t i;                                               \
                                                                 \
+    VSTART_CHECK_EARLY_EXIT(env);                             \
+                                                              \
       for (i = env->vstart; i < vl; i++) {                      \
           ETYPE s1 = *((ETYPE *)vs1 + H(i));                    \
           ETYPE s2 = *((ETYPE *)vs2 + H(i));                    \
@@ -982,6 +998,8 @@ void HELPER(NAME)(void *vd, void *v0, target_ulong s1,          \
       uint32_t vta_all_1s = vext_vta_all_1s(desc);                \
       uint32_t i;                                                 \
                                                                   \
+    VSTART_CHECK_EARLY_EXIT(env);                               \
+                                                                \
       for (i = env->vstart; i < vl; i++) {                        \
           ETYPE s2 = *((ETYPE *)vs2 + H(i));                      \
           ETYPE carry = !vm && vext_elem_mask(v0, i);             \
@@ -1078,6 +1096,8 @@ void HELPER(NAME)(void *vd, void *v0, void *vs1,                          \
       uint32_t vma = vext_vma(desc);                                        \
       uint32_t i;                                                           \
                                                                             \
+    VSTART_CHECK_EARLY_EXIT(env);                                         \
+                                                                          \
       for (i = env->vstart; i < vl; i++) {                                  \
           if (!vm && !vext_elem_mask(v0, i)) {                              \
               /* set masked-off elements to 1s */                           \
@@ -1125,6 +1145,8 @@ void HELPER(NAME)(void *vd, void *v0, target_ulong s1,      \
       uint32_t vma = vext_vma(desc);                          \
       uint32_t i;                                             \
                                                               \
+    VSTART_CHECK_EARLY_EXIT(env);                           \
+                                                            \
       for (i = env->vstart; i < vl; i++) {                    \
           if (!vm && !vext_elem_mask(v0, i)) {                \
               /* set masked-off elements to 1s */             \
@@ -1187,6 +1209,8 @@ void HELPER(NAME)(void *vd, void *v0, void *vs1, void *vs2,   \
       uint32_t vma = vext_vma(desc);                            \
       uint32_t i;                                               \
                                                                 \
+    VSTART_CHECK_EARLY_EXIT(env);                             \
+                                                              \
       for (i = env->vstart; i < vl; i++) {                      \
           ETYPE s1 = *((ETYPE *)vs1 + H(i));                    \
           ETYPE s2 = *((ETYPE *)vs2 + H(i));                    \
@@ -1252,6 +1276,8 @@ void HELPER(NAME)(void *vd, void *v0, target_ulong s1, void *vs2,   \
       uint32_t vma = vext_vma(desc);                                  \
       uint32_t i;                                                     \
                                                                       \
+    VSTART_CHECK_EARLY_EXIT(env);                                   \
+                                                                    \
       for (i = env->vstart; i < vl; i++) {                            \
           ETYPE s2 = *((ETYPE *)vs2 + H(i));                          \
           if (!vm && !vext_elem_mask(v0, i)) {                        \
@@ -1799,6 +1825,8 @@ void HELPER(NAME)(void *vd, void *vs1, CPURISCVState *env,           \
       uint32_t vta = vext_vta(desc);                                   \
       uint32_t i;                                                      \
                                                                        \
+    VSTART_CHECK_EARLY_EXIT(env);                                    \
+                                                                     \
       for (i = env->vstart; i < vl; i++) {                             \
           ETYPE s1 = *((ETYPE *)vs1 + H(i));                           \
           *((ETYPE *)vd + H(i)) = s1;                                  \
@@ -1823,6 +1851,8 @@ void HELPER(NAME)(void *vd, uint64_t s1, CPURISCVState *env,         \
       uint32_t vta = vext_vta(desc);                                   \
       uint32_t i;                                                      \
                                                                        \
+    VSTART_CHECK_EARLY_EXIT(env);                                    \
+                                                                     \
       for (i = env->vstart; i < vl; i++) {                             \
           *((ETYPE *)vd + H(i)) = (ETYPE)s1;                           \
       }                                                                \
@@ -1846,6 +1876,8 @@ void HELPER(NAME)(void *vd, void *v0, void *vs1, void *vs2,          \
       uint32_t vta = vext_vta(desc);                                   \
       uint32_t i;                                                      \
                                                                        \
+    VSTART_CHECK_EARLY_EXIT(env);                                    \
+                                                                     \
       for (i = env->vstart; i < vl; i++) {                             \
           ETYPE *vt = (!vext_elem_mask(v0, i) ? vs2 : vs1);            \
           *((ETYPE *)vd + H(i)) = *(vt + H(i));                        \
@@ -1870,6 +1902,8 @@ void HELPER(NAME)(void *vd, void *v0, target_ulong s1,               \
       uint32_t vta = vext_vta(desc);                                   \
       uint32_t i;                                                      \
                                                                        \
+    VSTART_CHECK_EARLY_EXIT(env);                                    \
+                                                                     \
       for (i = env->vstart; i < vl; i++) {                             \
           ETYPE s2 = *((ETYPE *)vs2 + H(i));                           \
           ETYPE d = (!vext_elem_mask(v0, i) ? s2 :                     \
@@ -1915,6 +1949,8 @@ vext_vv_rm_1(void *vd, void *v0, void *vs1, void *vs2,
                uint32_t vl, uint32_t vm, int vxrm,
                opivv2_rm_fn *fn, uint32_t vma, uint32_t esz)
   {
+    VSTART_CHECK_EARLY_EXIT(env);
+
       for (uint32_t i = env->vstart; i < vl; i++) {
           if (!vm && !vext_elem_mask(v0, i)) {
               /* set masked-off elements to 1s */
@@ -2040,6 +2076,8 @@ vext_vx_rm_1(void *vd, void *v0, target_long s1, void *vs2,
                uint32_t vl, uint32_t vm, int vxrm,
                opivx2_rm_fn *fn, uint32_t vma, uint32_t esz)
   {
+    VSTART_CHECK_EARLY_EXIT(env);
+
       for (uint32_t i = env->vstart; i < vl; i++) {
           if (!vm && !vext_elem_mask(v0, i)) {
               /* set masked-off elements to 1s */
@@ -2837,6 +2875,8 @@ void HELPER(NAME)(void *vd, void *v0, void *vs1,          \
       uint32_t vma = vext_vma(desc);                        \
       uint32_t i;                                           \
                                                             \
+    VSTART_CHECK_EARLY_EXIT(env);                         \
+                                                          \
       for (i = env->vstart; i < vl; i++) {                  \
           if (!vm && !vext_elem_mask(v0, i)) {              \
               /* set masked-off elements to 1s */           \
@@ -2880,6 +2920,8 @@ void HELPER(NAME)(void *vd, void *v0, uint64_t s1,        \
       uint32_t vma = vext_vma(desc);                        \
       uint32_t i;                                           \
                                                             \
+    VSTART_CHECK_EARLY_EXIT(env);                         \
+                                                          \
       for (i = env->vstart; i < vl; i++) {                  \
           if (!vm && !vext_elem_mask(v0, i)) {              \
               /* set masked-off elements to 1s */           \
@@ -3466,6 +3508,8 @@ void HELPER(NAME)(void *vd, void *v0, void *vs2,       \
       uint32_t vma = vext_vma(desc);                     \
       uint32_t i;                                        \
                                                          \
+    VSTART_CHECK_EARLY_EXIT(env);                      \
+                                                       \
       if (vl == 0) {                                     \
           return;                                        \
       }                                                  \
@@ -3987,6 +4031,8 @@ void HELPER(NAME)(void *vd, void *v0, void *vs1, void *vs2,   \
       uint32_t vma = vext_vma(desc);                            \
       uint32_t i;                                               \
                                                                 \
+    VSTART_CHECK_EARLY_EXIT(env);                             \
+                                                              \
       for (i = env->vstart; i < vl; i++) {                      \
           ETYPE s1 = *((ETYPE *)vs1 + H(i));                    \
           ETYPE s2 = *((ETYPE *)vs2 + H(i));                    \
@@ -4027,6 +4073,8 @@ void HELPER(NAME)(void *vd, void *v0, uint64_t s1, void *vs2,       \
       uint32_t vma = vext_vma(desc);                                  \
       uint32_t i;                                                     \
                                                                       \
+    VSTART_CHECK_EARLY_EXIT(env);                                   \
+                                                                    \
       for (i = env->vstart; i < vl; i++) {                            \
           ETYPE s2 = *((ETYPE *)vs2 + H(i));                          \
           if (!vm && !vext_elem_mask(v0, i)) {                        \
@@ -4220,6 +4268,8 @@ void HELPER(NAME)(void *vd, void *v0, uint64_t s1, void *vs2, \
       uint32_t vta = vext_vta(desc);                            \
       uint32_t i;                                               \
                                                                 \
+    VSTART_CHECK_EARLY_EXIT(env);                             \
+                                                              \
       for (i = env->vstart; i < vl; i++) {                      \
           ETYPE s2 = *((ETYPE *)vs2 + H(i));                    \
           *((ETYPE *)vd + H(i)) =                               \
@@ -4386,6 +4436,8 @@ void HELPER(NAME)(void *vd, void *v0, void *vs1,          \
       uint32_t i;                                           \
       TD s1 =  *((TD *)vs1 + HD(0));                        \
                                                             \
+    VSTART_CHECK_EARLY_EXIT(env);                         \
+                                                          \
       for (i = env->vstart; i < vl; i++) {                  \
           TS2 s2 = *((TS2 *)vs2 + HS2(i));                  \
           if (!vm && !vext_elem_mask(v0, i)) {              \
@@ -4472,6 +4524,8 @@ void HELPER(NAME)(void *vd, void *v0, void *vs1,           \
       uint32_t i;                                            \
       TD s1 =  *((TD *)vs1 + HD(0));                         \
                                                              \
+    VSTART_CHECK_EARLY_EXIT(env);                          \
+                                                           \
       for (i = env->vstart; i < vl; i++) {                   \
           TS2 s2 = *((TS2 *)vs2 + HS2(i));                   \
           if (!vm && !vext_elem_mask(v0, i)) {               \
@@ -4544,6 +4598,8 @@ void HELPER(NAME)(void *vd, void *v0, void *vs1,          \
       uint32_t i;                                           \
       int a, b;                                             \
                                                             \
+    VSTART_CHECK_EARLY_EXIT(env);                         \
+                                                          \
       for (i = env->vstart; i < vl; i++) {                  \
           a = vext_elem_mask(vs1, i);                       \
           b = vext_elem_mask(vs2, i);                       \
@@ -4585,6 +4641,11 @@ target_ulong HELPER(vcpop_m)(void *v0, void *vs2, CPURISCVState *env,
       uint32_t vl = env->vl;
       int i;
+    if (env->vstart >= env->vl) {
+        env->vstart = 0;
+        return 0;
+    }
+
       for (i = env->vstart; i < vl; i++) {
           if (vm || vext_elem_mask(v0, i)) {
               if (vext_elem_mask(vs2, i)) {
@@ -4604,6 +4665,11 @@ target_ulong HELPER(vfirst_m)(void *v0, void *vs2, CPURISCVState *env,
       uint32_t vl = env->vl;
       int i;
+    if (env->vstart >= env->vl) {
+        env->vstart = 0;
+        return 0;
+    }
+
       for (i = env->vstart; i < vl; i++) {
           if (vm || vext_elem_mask(v0, i)) {
               if (vext_elem_mask(vs2, i)) {
@@ -4632,6 +4698,8 @@ static void vmsetm(void *vd, void *v0, void *vs2, CPURISCVState *env,
       int i;
       bool first_mask_bit = false;
+    VSTART_CHECK_EARLY_EXIT(env);
+
       for (i = env->vstart; i < vl; i++) {
           if (!vm && !vext_elem_mask(v0, i)) {
               /* set masked-off elements to 1s */
@@ -4704,6 +4772,8 @@ void HELPER(NAME)(void *vd, void *v0, void *vs2, CPURISCVState *env,      \
       uint32_t sum = 0;                                                     \
       int i;                                                                \
                                                                             \
+    VSTART_CHECK_EARLY_EXIT(env);                                         \
+                                                                          \
       for (i = env->vstart; i < vl; i++) {                                  \
           if (!vm && !vext_elem_mask(v0, i)) {                              \
               /* set masked-off elements to 1s */                           \
@@ -4737,6 +4807,8 @@ void HELPER(NAME)(void *vd, void *v0, CPURISCVState *env, uint32_t desc)  \
       uint32_t vma = vext_vma(desc);                                        \
       int i;                                                                \
                                                                             \
+    VSTART_CHECK_EARLY_EXIT(env);                                         \
+                                                                          \
       for (i = env->vstart; i < vl; i++) {                                  \
           if (!vm && !vext_elem_mask(v0, i)) {                              \
               /* set masked-off elements to 1s */                           \
@@ -4772,6 +4844,8 @@ void HELPER(NAME)(void *vd, void *v0, target_ulong s1, void *vs2,         \
       uint32_t vma = vext_vma(desc);                                        \
       target_ulong offset = s1, i_min, i;                                   \
                                                                             \
+    VSTART_CHECK_EARLY_EXIT(env);                                         \
+                                                                          \
       i_min = MAX(env->vstart, offset);                                     \
       for (i = i_min; i < vl; i++) {                                        \
           if (!vm && !vext_elem_mask(v0, i)) {                              \
@@ -4805,6 +4879,8 @@ void HELPER(NAME)(void *vd, void *v0, target_ulong s1, void *vs2,         \
       uint32_t vma = vext_vma(desc);                                        \
       target_ulong i_max, i_min, i;                                         \
                                                                             \
+    VSTART_CHECK_EARLY_EXIT(env);                                         \
+                                                                          \
       i_min = MIN(s1 < vlmax ? vlmax - s1 : 0, vl);                         \
       i_max = MAX(i_min, env->vstart);                                      \
       for (i = env->vstart; i < i_max; ++i) {                               \
@@ -4847,6 +4923,8 @@ static void vslide1up_##BITWIDTH(void *vd, void *v0, uint64_t s1,           \
       uint32_t vma = vext_vma(desc);                                          \
       uint32_t i;                                                             \
                                                                               \
+    VSTART_CHECK_EARLY_EXIT(env);                                           \
+                                                                            \
       for (i = env->vstart; i < vl; i++) {                                    \
           if (!vm && !vext_elem_mask(v0, i)) {                                \
               /* set masked-off elements to 1s */                             \
@@ -4896,6 +4974,8 @@ static void vslide1down_##BITWIDTH(void *vd, void *v0, uint64_t s1,           \
       uint32_t vma = vext_vma(desc);                                            \
       uint32_t i;                                                               \
                                                                                 \
+    VSTART_CHECK_EARLY_EXIT(env);                                             \
+                                                                              \
       for (i = env->vstart; i < vl; i++) {                                      \
           if (!vm && !vext_elem_mask(v0, i)) {                                  \
               /* set masked-off elements to 1s */                               \
@@ -4971,6 +5051,8 @@ void HELPER(NAME)(void *vd, void *v0, void *vs1, void *vs2,               \
       uint64_t index;                                                       \
       uint32_t i;                                                           \
                                                                             \
+    VSTART_CHECK_EARLY_EXIT(env);                                         \
+                                                                          \
       for (i = env->vstart; i < vl; i++) {                                  \
           if (!vm && !vext_elem_mask(v0, i)) {                              \
               /* set masked-off elements to 1s */                           \
@@ -5014,6 +5096,8 @@ void HELPER(NAME)(void *vd, void *v0, target_ulong s1, void *vs2,         \
       uint64_t index = s1;                                                  \
       uint32_t i;                                                           \
                                                                             \
+    VSTART_CHECK_EARLY_EXIT(env);                                         \
+                                                                          \
       for (i = env->vstart; i < vl; i++) {                                  \
           if (!vm && !vext_elem_mask(v0, i)) {                              \
               /* set masked-off elements to 1s */                           \
@@ -5048,6 +5132,8 @@ void HELPER(NAME)(void *vd, void *v0, void *vs1, void *vs2,               \
       uint32_t vta = vext_vta(desc);                                        \
       uint32_t num = 0, i;                                                  \
                                                                             \
+    VSTART_CHECK_EARLY_EXIT(env);                                         \
+                                                                          \
       for (i = env->vstart; i < vl; i++) {                                  \
           if (!vext_elem_mask(vs1, i)) {                                    \
               continue;                                                     \
@@ -5100,6 +5186,8 @@ void HELPER(NAME)(void *vd, void *v0, void *vs2,                 \
       uint32_t vma = vext_vma(desc);                               \
       uint32_t i;                                                  \
                                                                    \
+    VSTART_CHECK_EARLY_EXIT(env);                                \
+                                                                 \
       for (i = env->vstart; i < vl; i++) {                         \
           if (!vm && !vext_elem_mask(v0, i)) {                     \
               /* set masked-off elements to 1s */                  \
target/riscv/vector_internals.c +4 -0
index 12f5964fbb..996c21eb31 100644
@@ -44,6 +44,8 @@ void do_vext_vv(void *vd, void *v0, void *vs1, void *vs2,
       uint32_t vma = vext_vma(desc);
       uint32_t i;
+    VSTART_CHECK_EARLY_EXIT(env);
+
       for (i = env->vstart; i < vl; i++) {
           if (!vm && !vext_elem_mask(v0, i)) {
               /* set masked-off elements to 1s */
@@ -68,6 +70,8 @@ void do_vext_vx(void *vd, void *v0, target_long s1, void *vs2,
       uint32_t vma = vext_vma(desc);
       uint32_t i;
+    VSTART_CHECK_EARLY_EXIT(env);
+
       for (i = env->vstart; i < vl; i++) {
           if (!vm && !vext_elem_mask(v0, i)) {
               /* set masked-off elements to 1s */
target/riscv/vector_internals.h +9 -0
index 842765f6c1..9e1e15b575 100644
@@ -24,6 +24,13 @@
   #include "tcg/tcg-gvec-desc.h"
   #include "internals.h"
+#define VSTART_CHECK_EARLY_EXIT(env) do { \
+    if (env->vstart >= env->vl) {         \
+        env->vstart = 0;                  \
+        return;                           \
+    }                                     \
+} while (0)
+
   static inline uint32_t vext_nf(uint32_t desc)
   {
       return FIELD_EX32(simd_data(desc), VDATA, NF);
@@ -151,6 +158,8 @@ void HELPER(NAME)(void *vd, void *v0, void *vs2,       \
       uint32_t vma = vext_vma(desc);                     \
       uint32_t i;                                        \
                                                          \
+    VSTART_CHECK_EARLY_EXIT(env);                      \
+                                                       \
       for (i = env->vstart; i < vl; i++) {               \
           if (!vm && !vext_elem_mask(v0, i)) {           \
               /* set masked-off elements to 1s */        \