riscv: set vstart_eq_zero on mark_vs_dirty
v2 cover riscv: set vstart_eq_zero on mark_vs_dirty
Hi,
This second version is based on feedback gave on the Gitlab bug [1] by Vladimir Isaev. For this approach to work the 'vstart_eq_zero' flag must be updated regardless of 'mstatus_vs' being marked as DIRTY or not - there's no guarantee that we'll clean mstatus_vs after each change in 'vstart'.
Patches based on alistair/riscv-to-apply.next.
[1] https://gitlab.com/qemu-project/qemu/-/issues/1976
Changes from v1:
- patch 3:
- always update vstart_eq_zero, not only if "mstatus_vs !=
EXT_STATUS_DIRTY"
- v1 link: https://lore.kernel.org/qemu-riscv/[email protected]/
Daniel Henrique Barboza (3):
trans_rvv.c.inc: write CSRs must call mark_vs_dirty() too trans_rvv.c.inc: remove redundant mark_vs_dirty() calls target/riscv/translate.c: set vstart_eq_zero in mark_vs_dirty()
target/riscv/insn_trans/trans_rvv.c.inc | 28 +++++++------------------ target/riscv/translate.c | 20 ++++++++++++++++++ 2 files changed, 27 insertions(+), 21 deletions(-)
Signature · 1 line
2.43.0
PATCH v2 1/3 trans_rvv.c.inc: write CSRs must call mark_vs_dirty() too
In the Vector spec section 3.2 [1]:
"When mstatus.VS is set to Initial or Clean, executing any instruction
that changes vector state, including the vector CSRs, will change mstatus.VS to Dirty."
ldst_us_trans(), ldst_stride_trans(), ldst_index_trans() and ldst_whole_trans() will change vector state regardless of being a store op or not. Stores will set env->vstart to zero after execution (see vext_ldst_us() in vector_helper.c), and this is vector CSR state change.
[1] https://github.com/riscv/riscv-v-spec/blob/master/v-spec.adoc#vector-start-index-csr-vstart
--- target/riscv/insn_trans/trans_rvv.c.inc | 19 ++++--------------- 1 file changed, 4 insertions(+), 15 deletions(-)
target/riscv/insn_trans/trans_rvv.c.inc +4 -15
@@ -638,10 +638,7 @@ static bool ldst_us_trans(uint32_t vd, uint32_t rs1, uint32_t data, fn(dest, mask, base, tcg_env, desc); - if (!is_store) { - mark_vs_dirty(s); - } - + mark_vs_dirty(s); gen_set_label(over); return true; }
@@ -799,10 +796,7 @@ static bool ldst_stride_trans(uint32_t vd, uint32_t rs1, uint32_t rs2, fn(dest, mask, base, stride, tcg_env, desc); - if (!is_store) { - mark_vs_dirty(s); - } - + mark_vs_dirty(s); gen_set_label(over); return true; }
@@ -906,10 +900,7 @@ static bool ldst_index_trans(uint32_t vd, uint32_t rs1, uint32_t vs2, fn(dest, mask, base, index, tcg_env, desc); - if (!is_store) { - mark_vs_dirty(s); - } - + mark_vs_dirty(s); gen_set_label(over); return true; }
@@ -1104,9 +1095,7 @@ static bool ldst_whole_trans(uint32_t vd, uint32_t rs1, uint32_t nf, fn(dest, base, tcg_env, desc); - if (!is_store) { - mark_vs_dirty(s); - } + mark_vs_dirty(s); gen_set_label(over); return true;
Signature · 1 line
2.43.0
Re: [PATCH v2 1/3] trans_rvv.c.inc: write CSRs must call mark_vs_dirty() too
Quoted 10 lines · Daniel Henrique Barboza
In the Vector spec section 3.2 [1]:
"When mstatus.VS is set to Initial or Clean, executing any instruction
that changes vector state, including the vector CSRs, will change mstatus.VS to Dirty."
ldst_us_trans(),ldst_stride_trans(),ldst_index_trans()andldst_whole_trans()will change vector state regardless of being a store op or not. Stores will setenv->vstartto zero after execution (seevext_ldst_us()invector_helper.c), and this is vector CSR state change.
In Initial or Clean state, it could be argued that vstart is already zero, so is there really a change to state?
OTOH, on the exception path out of a vector store, where we *do* set vstart != 0, we do not also set vs dirty.
Therefore I think that loads and stores need to manage dirty within the helper, alongside the management of vstart, or perhaps move the mark_vs_dirty call to *before* the call to the helper.
r~
PATCH v2 2/3 trans_rvv.c.inc: remove redundant mark_vs_dirty() calls
In reply to Daniel Henrique Barbozatrans_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 | 9 +++------ 1 file changed, 3 insertions(+), 6 deletions(-)
target/riscv/insn_trans/trans_rvv.c.inc +3 -6
@@ -2094,7 +2094,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;
@@ -2115,9 +2114,9 @@ static bool trans_vmv_v_i(DisasContext *s, arg_vmv_v_i *a) tcg_gen_addi_ptr(dest, tcg_env, vreg_ofs(s, a->rd)); fns[s->sew](dest, s1, tcg_env, desc); - mark_vs_dirty(s); gen_set_label(over); } + mark_vs_dirty(s); return true; } return false;
@@ -2660,7 +2659,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;
@@ -2686,9 +2684,9 @@ static bool trans_vfmv_v_f(DisasContext *s, arg_vfmv_v_f *a) fns[s->sew - 1](dest, t1, tcg_env, desc); - mark_vs_dirty(s); gen_set_label(over); } + mark_vs_dirty(s); return true; } return false;
@@ -3632,15 +3630,14 @@ 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 { \ 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); \ } \ + mark_vs_dirty(s); \ return true; \ } \ return false; \
Signature · 1 line
2.43.0
PATCH v2 3/3 target/riscv/translate.c: set vstart_eq_zero in mark_vs_dirty()
In reply to Daniel Henrique BarbozaThe 'vstart_eq_zero' flag which is used to determine if some insns, like vector reductor operations, should SIGILL. At this moment the flag is being updated only during cpu_get_tb_cpu_state(), at the start of each translation block.
This cadence isn't enough and we're facing situations where a vector instruction successfully updated 'vstart' to zero, but the flag was still marked as 'false', resulting in a SIGILL because instructions are checking the flag.
mark_vs_dirty() is called after any instruction changes Vector CSR state, making it a good place to update 'vstart_eq_zero'.
Resolves: https://gitlab.com/qemu-project/qemu/-/issues/1976
--- target/riscv/translate.c | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+)
target/riscv/translate.c +20 -0
@@ -652,6 +652,8 @@ static inline void mark_fs_dirty(DisasContext *ctx) { }*/ static void mark_vs_dirty (DisasContext *ctx) { +TCGLabel *vstart_zero =gen_new_label (); +TCGLabel *done =gen_new_label ();TCGv tmp;if (ctx->mstatus_vs !=EXT_STATUS_DIRTY ) {
@@ -669,6 +671,24 @@ static void mark_vs_dirty(DisasContext *ctx) tcg_gen_st_tl(tmp, tcg_env, offsetof(CPURISCVState, mstatus_hs)); } } + +/* +* We can safely make 'vl_eq_vlmax = false' if we marked +* VS as dirty with non-zero 'vstart', i.e. there's a fault +* to be handled. If 'vstart' is zero then we should retain +* the existing 'vl_eq_vlmax' - it'll be recalculated on the +* start of the next TB or during vset{i}vl{i} (that forces a +* TB end). +*/ +tcg_gen_brcondi_tl (TCG_COND_EQ , cpu_vstart,0 , vstart_zero); + ctx->vstart_eq_zero = false; + ctx->vl_eq_vlmax = false; +tcg_gen_br (done); + +gen_set_label (vstart_zero); + ctx->vstart_eq_zero = true; + +gen_set_label (done); }#else static inline void mark_vs_dirty (DisasContext *ctx) { }
Signature · 1 line
2.43.0
Re: [PATCH v2 3/3] target/riscv/translate.c: set vstart_eq_zero in mark_vs_dirty()
Quoted 45 lines · Daniel Henrique Barboza
The '
vstart_eq_zero' flag which is used to determine if some insns, like vector reductor operations, should SIGILL. At this moment the flag is being updated only duringcpu_get_tb_cpu_state(), at the start of each translation block.This cadence isn't enough and we're facing situations where a vector instruction successfully updated 'vstart' to zero, but the flag was still marked as 'false', resulting in a SIGILL because instructions are checking the flag.
mark_vs_dirty()is called after any instruction changes Vector CSR state, making it a good place to update 'vstart_eq_zero'.Fixes 8e1ee1fb57 ("target/riscv: rvv-1.0: add translation-time vector context status")Resolves: https://gitlab.com/qemu-project/qemu/-/issues/1976
Signed-off-by Daniel Henrique Barboza <[email protected]>--- target/riscv/translate.c | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+)target/riscv/translate.c +10
-0@@ -652,6 +652,8 @@ static inline void mark_fs_dirty(DisasContext *ctx) { }*/ static void mark_vs_dirty (DisasContext *ctx) { +TCGLabel *vstart_zero =gen_new_label (); +TCGLabel *done =gen_new_label ();TCGv tmp;if (ctx->mstatus_vs !=EXT_STATUS_DIRTY ) {@@ -669,6 +671,24 @@ static void mark_vs_dirty(DisasContext *ctx)tcg_gen_st_tl (tmp, tcg_env, offsetof(CPURISCVState , mstatus_hs)); } } + +/* +* We can safely make 'vl_eq_vlmax = false' if we marked +* VS as dirty with non-zero 'vstart', i.e. there's a fault +* to be handled. If 'vstart' is zero then we should retain +* the existing 'vl_eq_vlmax' - it'll be recalculated on the +* start of the next TB or during vset{i}vl{i} (that forces a +* TB end).
target/riscv/translate.c static void mark_vs_dirty(DisasContext *ctx) · line 691
Quoted 10 lines
target/riscv/translate.c +10
-0+*/ +tcg_gen_brcondi_tl (TCG_COND_EQ , cpu_vstart,0 , vstart_zero); + ctx->vstart_eq_zero = false; + ctx->vl_eq_vlmax = false; +tcg_gen_br (done); + +gen_set_label (vstart_zero); + ctx->vstart_eq_zero = true; + +gen_set_label (done);
This is very confused, apparently generating code to test vstart at runtime, and then set some translation time variables in the branches.
Afaik, the only way vstart != 0 is an explicit set to the CSR or exiting a load via exception. Therefore you have no need to have any sort of brcond here -- just set ctx->vstart_eq_zero = true.
Also, you need to move the ifdefs from around mark_vs_dirty, because it is now relevant to user-only.
It may be worth a rename, because it does more than mark vs dirty, and therefore is different than mark_fs_dirty.
You need to review all instances of
TCGLabel *over = gen_new_label();
tcg_gen_brcond_tl(TCG_COND_GEU, cpu_vstart, cpu_vl, over);
...
gen_set_label(over);
return true;because this *should have* set vstart = 0. With vstart < vl, this is done in the helper function, but every place using a conditional branch needs attention.
r~
Re: [PATCH v2 3/3] target/riscv/translate.c: set vstart_eq_zero in mark_vs_dirty()
Quoted 67 lines · Richard Henderson
Quoted 44 lines · Daniel Henrique Barboza
The '
vstart_eq_zero' flag which is used to determine if some insns, like vector reductor operations, should SIGILL. At this moment the flag is being updated only duringcpu_get_tb_cpu_state(), at the start of each translation block.This cadence isn't enough and we're facing situations where a vector instruction successfully updated 'vstart' to zero, but the flag was still marked as 'false', resulting in a SIGILL because instructions are checking the flag.
mark_vs_dirty()is called after any instruction changes Vector CSR state, making it a good place to update 'vstart_eq_zero'.Fixes 8e1ee1fb57 ("target/riscv: rvv-1.0: add translation-time vector context status")Resolves: https://gitlab.com/qemu-project/qemu/-/issues/1976
Signed-off-by Daniel Henrique Barboza <[email protected]>---
target/riscv/translate.c| 20 ++++++++++++++++++++1 file changed, 20 insertions(+)target/riscv/translate.c +10
-0@@ -652,6 +652,8 @@ static inline void mark_fs_dirty(DisasContext *ctx) { }*/ static void mark_vs_dirty(DisasContext *ctx) { + TCGLabel *vstart_zero = gen_new_label(); + TCGLabel *done = gen_new_label(); TCGv tmp; if (ctx->mstatus_vs != EXT_STATUS_DIRTY) {@@ -669,6 +671,24 @@ static void mark_vs_dirty(DisasContext *ctx) tcg_gen_st_tl(tmp, tcg_env, offsetof(CPURISCVState, mstatus_hs)); } } + + /* +* We can safely make 'vl_eq_vlmax = false' if we marked +* VS as dirty with non-zero 'vstart', i.e. there's a fault +* to be handled. If 'vstart' is zero then we should retain +* the existing 'vl_eq_vlmax' - it'll be recalculated on the +* start of the next TB or during vset{i}vl{i} (that forces a +* TB end). target/riscv/translate.c static void mark_vs_dirty(DisasContext *ctx) · line 691
Quoted 10 lines
target/riscv/translate.c +10
-0+*/ + tcg_gen_brcondi_tl(TCG_COND_EQ, cpu_vstart, 0, vstart_zero); + ctx->vstart_eq_zero = false; + ctx->vl_eq_vlmax = false; + tcg_gen_br(done); + + gen_set_label(vstart_zero); + ctx->vstart_eq_zero = true; + + gen_set_label(done);This is very confused, apparently generating code to test vstart at runtime, and then set some translation time variables in the branches.
Afaik, the only way vstart != 0 is an explicit set to the CSR or exiting a load via exception. Therefore you have no need to have any sort of brcond here -- just set
ctx->vstart_eq_zero= true.Also, you need to move the ifdefs from around
mark_vs_dirty, because it is now relevant to user-only.It may be worth a rename, because it does more than mark vs dirty, and therefore is different than
mark_fs_dirty.You need to review all instances of
Quoted 7 lines
TCGLabel *over =
gen_new_label();tcg_gen_brcond_tl(TCG_COND_GEU, cpu_vstart, cpu_vl, over);
...
gen_set_label(over);
return true;because this *should have* set vstart = 0. With vstart < vl, this is done in the helper function, but every place using a conditional branch needs attention.
After reading the reviews of patches 1 and 3 what I'm considering here is:
1 - drop patch 1;
2 - there's a patch from Ivan Klokov sent 2 months ago:
"[PATCH 1/1] target/riscv: Clear vstart_qe_zero flag"
https://lore.kernel.org/qemu-riscv/[email protected]/
His patch is closer to what you suggested than mine. He already renamed mark_vs_dirty() to finalize_rvv_inst() and made it set start_eq_zero unconditionally. It needs a little work (i.e. remove the ifds from the function) that I'll do myself.
3 - I'll keep patch 2 to reduce the redundant calls to the now finalize_rvv_inst();
4 - Add another patch to through all "gen_set_label(over)" cond branches and set vstart = 0 && vstart_eq_zero manually when we're doing the jump.
In fact, shouldn't we return earlier if we're not taking the branch? Otherwise we'll set vstart twice in case we didn't get the branch. E.g:
TCGLabel *over = gen_new_label();
tcg_gen_brcond_tl(TCG_COND_GEU, cpu_vstart, cpu_vl, over);
(...)
finalize_rvv_insn();
return true;
gen_set_label(over);
/* some TCG ops to set cpu_vstart to zero. Perhaps a helper? */
s->vstart_eq_zero = true;
return true;Thanks,
Daniel
Quoted 3 lines
r~
Re: [PATCH v2 3/3] target/riscv/translate.c: set vstart_eq_zero in mark_vs_dirty()
Quoted 3 lines · Daniel Henrique Barboza
After reading the reviews of patches 1 and 3 what I'm considering here is:
1 - drop patch 1;
Ok.
Quoted 10 lines
2 - there's a patch from Ivan Klokov sent 2 months ago:
"[PATCH 1/1] target/riscv: Clear
vstart_qe_zeroflag"
https://lore.kernel.org/qemu-riscv/[email protected]/His patch is closer to what you suggested than mine. He already renamed
mark_vs_dirty()tofinalize_rvv_inst()and made it setstart_eq_zerounconditionally. It needs a little work (i.e. remove the ifds from the function) that I'll do myself.3 - I'll keep patch 2 to reduce the redundant calls to the now
finalize_rvv_inst();
Ok.
Quoted 12 lines
4 - Add another patch to through all "
gen_set_label(over)" cond branches and set vstart = 0 &&vstart_eq_zeromanually when we're doing the jump.In fact, shouldn't we return earlier if we're not taking the branch? Otherwise we'll set vstart twice in case we didn't get the branch. E.g:
TCGLabel *over =
gen_new_label();tcg_gen_brcond_tl(TCG_COND_GEU, cpu_vstart, cpu_vl, over); (...)
finalize_rvv_insn();
return true;
Quoted 4 lines
gen_set_label(over);
/* some TCG ops to setcpu_vstartto zero. Perhaps a helper? */s->vstart_eq_zero= true;
return true;
That will break, of course, because you wouldn't emit 'over'. You really need to get translation-time and run-time separate in your head.
That said, I think these brcond(vstart >= vl) are a mistake. The loops within the helpers are generally of the form
for (i = env->vstart; i < evl; i++, env->vstart++) {which will operate just fine with vstart >= vl, iterating zero times. We will then fall through to the post-insn cleanup,
env->vstart = 0;
vext_set_tail_elems_1s(evl, vd, desc, nf, esz, max_elems);or whatever.
I would expect the condition vstart >= vl to never happen in practice. I believe the only way to induce it is an explicit write to vstart. Therefore I think you should not attempt to "optimize away" the call to the helper.
Of course you will want to double-check all of the loop iterations in the associated helpers when removing the branches.
r~
PS: I believe a better form for the ldst loops is
for (i = env->vstart; i < evl; env->vstart = ++i)
to avoid re-reading from vstart each iteration.
Re: [PATCH v2 3/3] target/riscv/translate.c: set vstart_eq_zero in mark_vs_dirty()
Quoted 48 lines · Richard Henderson
Quoted 3 lines · Daniel Henrique Barboza
After reading the reviews of patches 1 and 3 what I'm considering here is:
1 - drop patch 1;
Ok.
Quoted 10 lines
2 - there's a patch from Ivan Klokov sent 2 months ago:
"[PATCH 1/1] target/riscv: Clear
vstart_qe_zeroflag"
https://lore.kernel.org/qemu-riscv/[email protected]/His patch is closer to what you suggested than mine. He already renamed
mark_vs_dirty()tofinalize_rvv_inst()and made it setstart_eq_zerounconditionally. It needs a little work (i.e. remove the ifds from the function) that I'll do myself.3 - I'll keep patch 2 to reduce the redundant calls to the now
finalize_rvv_inst();Ok.
Quoted 12 lines
4 - Add another patch to through all "
gen_set_label(over)" cond branches and set vstart = 0 &&vstart_eq_zeromanually when we're doing the jump.In fact, shouldn't we return earlier if we're not taking the branch? Otherwise we'll set vstart twice in case we didn't get the branch. E.g:
TCGLabel *over =
gen_new_label();tcg_gen_brcond_tl(TCG_COND_GEU, cpu_vstart, cpu_vl, over); (...)
finalize_rvv_insn();
return true;Quoted 4 lines
gen_set_label(over);
/* some TCG ops to setcpu_vstartto zero. Perhaps a helper? */s->vstart_eq_zero= true;
return true;That will break, of course, because you wouldn't emit 'over'. You really need to get translation-time and run-time separate in your head.
That said, I think these brcond(vstart >= vl) are a mistake. The loops within the helpers are generally of the form
for (i =
env->vstart; i < evl; i++, env->vstart++) {which will operate just fine with vstart >= vl, iterating zero times. We will then fall through to the post-insn cleanup,
Quoted 8 lines
env->vstart= 0;
vext_set_tail_elems_1s(evl, vd, desc, nf, esz, max_elems);or whatever.
I would expect the condition vstart >= vl to never happen in practice. I believe the only way to induce it is an explicit write to vstart. Therefore I think you should not attempt to "optimize away" the call to the helper.
Of course you will want to double-check all of the loop iterations in the associated helpers when removing the branches.
Ok!
Quoted 10 lines
r~
PS: I believe a better form for the ldst loops is
for (i =
env->vstart; i < evl;env->vstart= ++i)to avoid re-reading from vstart each iteration.
I'll add a patch to convert these loops. Thanks,
Daniel