Support RISC-V Zvqdotq vector dot-product extension
cover Support RISC-V Zvqdotq vector dot-product extension
This patch series introduces support for Zvqdotq extension.
The Zvqdotq extension's ISA specification is not yet ratified, so this patch series is based on the latest draft (v0.0.2) and treats the Zvqdotq extension as an experimental extension.
The draft of the Zvqdotq ISA specification:
https://github.com/riscv/riscv-dot-product
Max Chou (3):
target/riscv: Add Zvqdotq cfg property target/riscv: rvv: Add Zvqdotq support target/riscv: Expose Zvqdotq extension as a cpu property
target/riscv/cpu.c | 2 + target/riscv/cpu_cfg_fields.h.inc | 1 + target/riscv/helper.h | 10 +++ target/riscv/insn32.decode | 9 +++ target/riscv/insn_trans/trans_rvzvqdotq.c.inc | 61 ++++++++++++++++++ target/riscv/tcg/tcg-cpu.c | 5 ++ target/riscv/translate.c | 1 + target/riscv/vector_helper.c | 63 +++++++++++++++++++ 8 files changed, 152 insertions(+) create mode 100644 target/riscv/insn_trans/trans_rvzvqdotq.c.inc
Signature · 1 line
2.43.0
RFC PATCH 1/3 target/riscv: Add Zvqdotq cfg property
The Zvqdotq extension is the vector dot-product extension of RISC-V.
--- target/riscv/cpu.c | 1 + target/riscv/cpu_cfg_fields.h.inc | 1 + target/riscv/tcg/tcg-cpu.c | 5 +++++ 3 files changed, 7 insertions(+)
target/riscv/cpu.c +1 -0
@@ -187,6 +187,7 @@ const RISCVIsaExtData isa_edata_arr[] = {ISA_EXT_DATA_ENTRY (zvksg,PRIV_VERSION_1_12_0 , ext_zvksg),ISA_EXT_DATA_ENTRY (zvksh,PRIV_VERSION_1_12_0 , ext_zvksh),ISA_EXT_DATA_ENTRY (zvkt,PRIV_VERSION_1_12_0 , ext_zvkt), +ISA_EXT_DATA_ENTRY (zvqdotq,PRIV_VERSION_1_12_0 , ext_zvqdotq),ISA_EXT_DATA_ENTRY (zhinx,PRIV_VERSION_1_12_0 , ext_zhinx),ISA_EXT_DATA_ENTRY (zhinxmin,PRIV_VERSION_1_12_0 , ext_zhinxmin), ISA_EXT_DATA_ENTRY(sdtrig, PRIV_VERSION_1_12_0, debug),
target/riscv/cpu_cfg_fields.h.inc +1 -0
@@ -100,6 +100,7 @@ BOOL_FIELD(ext_zvfbfmin) BOOL_FIELD(ext_zvfbfwma) BOOL_FIELD(ext_zvfh) BOOL_FIELD(ext_zvfhmin) +BOOL_FIELD(ext_zvqdotq) BOOL_FIELD(ext_smaia) BOOL_FIELD(ext_ssaia) BOOL_FIELD(ext_smctr)
target/riscv/tcg/tcg-cpu.c +5 -0
@@ -767,6 +767,11 @@ void riscv_cpu_validate_set_extensions(RISCVCPU *cpu, Error **errp)return ; } +if (cpu->cfg.ext_zvqdotq && !cpu->cfg.ext_zve32x) { +error_setg (errp,"Zvqdotq extension requires V or Zve* extensions" ); +return ; + } +if ((cpu->cfg.ext_zvbc || cpu->cfg.ext_zvknhb) && !cpu->cfg.ext_zve64x) { error_setg( errp,
Signature · 1 line
2.43.0
RFC PATCH 2/3 target/riscv: rvv: Add Zvqdotq support
In reply to Max ChouSupport instructions for vector dot-product extension (Zvqdotq)
- vqdot.[vv,vx]
- vqdotu.[vv,vx]
- vqdotsu.[vv,vx]
- vqdotus.vx
--- target/riscv/helper.h | 10 +++ target/riscv/insn32.decode | 9 +++ target/riscv/insn_trans/trans_rvzvqdotq.c.inc | 61 ++++++++++++++++++ target/riscv/translate.c | 1 + target/riscv/vector_helper.c | 63 +++++++++++++++++++ 5 files changed, 144 insertions(+) create mode 100644 target/riscv/insn_trans/trans_rvzvqdotq.c.inc
target/riscv/helper.h +10 -0
@@ -1284,3 +1284,13 @@ DEF_HELPER_4(vgmul_vv, void, ptr, ptr, env, i32) DEF_HELPER_5(vsm4k_vi, void, ptr, ptr, i32, env, i32) DEF_HELPER_4(vsm4r_vv, void, ptr, ptr, env, i32)DEF_HELPER_4 (vsm4r_vs, void, ptr, ptr, env, i32) + +/* Vector dot-product functions */ +DEF_HELPER_6(vqdot_vv, void, ptr, ptr, ptr, ptr, env, i32) +DEF_HELPER_6(vqdotu_vv, void, ptr, ptr, ptr, ptr, env, i32) +DEF_HELPER_6(vqdotsu_vv, void, ptr, ptr, ptr, ptr, env, i32) + +DEF_HELPER_6(vqdot_vx, void, ptr, ptr, tl, ptr, env, i32) +DEF_HELPER_6(vqdotu_vx, void, ptr, ptr, tl, ptr, env, i32) +DEF_HELPER_6(vqdotsu_vx, void, ptr, ptr, tl, ptr, env, i32) +DEF_HELPER_6 (vqdotus_vx, void, ptr, ptr, tl, ptr, env, i32)
target/riscv/insn32.decode +9 -0
@@ -1066,3 +1066,12 @@ amominu_h 11000 . . ..... ..... 001 ..... 0101111 @atom_st amomaxu_h 11100 . . ..... ..... 001 ..... 0101111 @atom_st amocas_b 00101 . . ..... ..... 000 ..... 0101111 @atom_st amocas_h 00101 . . ..... ..... 001 ..... 0101111 @atom_st + +# *** Zvqdotq Vector Dot-Product Extension *** +vqdot_vv 101100 . ..... ..... 010 ..... 1010111 @r_vm +vqdot_vx 101100 . ..... ..... 110 ..... 1010111 @r_vm +vqdotu_vv 101000 . ..... ..... 010 ..... 1010111 @r_vm +vqdotu_vx 101000 . ..... ..... 110 ..... 1010111 @r_vm +vqdotsu_vv 101010 . ..... ..... 010 ..... 1010111 @r_vm +vqdotsu_vx 101010 . ..... ..... 110 ..... 1010111 @r_vm +vqdotus_vx 101110 . ..... ..... 110 ..... 1010111 @r_vm
target/riscv/insn_trans/trans_rvzvqdotq.c.inc new file +61 -0
61 lines not on this page · raw message
target/riscv/translate.c +1 -0
@@ -1190,6 +1190,7 @@ static uint32_t opcode_at(DisasContextBase *dcbase, target_ulong pc)#include "insn_trans/trans_rvzfh.c.inc" #include "insn_trans/trans_rvk.c.inc" #include "insn_trans/trans_rvvk.c.inc" +#include "insn_trans/trans_rvzvqdotq.c.inc" #include "insn_trans/trans_privileged.c.inc" #include "insn_trans/trans_svinval.c.inc" #include "insn_trans/trans_rvbf16.c.inc"
target/riscv/vector_helper.c +63 -0
72 lines not on this page · raw message
Signature · 1 line
2.43.0
Re: [RFC PATCH 2/3] target/riscv: rvv: Add Zvqdotq support
target/riscv/vector_helper.c GEN_VEXT_INT_EXT(vsext_vf2_d, int64_t, int32_t, H8, H4) · line 5493
Quoted 10 lines · Max Chou
target/riscv/vector_helper.c +10
-0+#define OPMVV_VQDOTQ (NAME ,TD ,T1 ,T2 ,TX1 ,TX2 ,HD ,HS1 ,HS2 ) \ +static void do_##NAME(void *vd, void *vs1, void *vs2, int i) \ +{ \ + int idx; \ + T1 r1; \ + T2 r2; \ + TX1 *r1_buf = (TX1 *)vs1 + HD(i); \ + TX2 *r2_buf = (TX2 *)vs2 + HD(i); \ + TD acc = *((TD *)vd + HD(i)); \ + int64_t partial_sum = 0; \
I think it's clear partial_sum should be the 32-bit type TD. Indeed, I'm not sure why you don't just have
TD acc = ((TD *)vd)[HD(i)];
target/riscv/vector_helper.c GEN_VEXT_INT_EXT(vsext_vf2_d, int64_t, int32_t, H8, H4) · line 5498
Quoted 5 lines
target/riscv/vector_helper.c +5
-0+ \ +for (idx =0 ; idx <4 ; ++idx) { \ + r1 = *((T1 *)r1_buf +HS1 (idx)); \ + r2 = *((T2 *)r2_buf +HS2 (idx)); \ + partial_sum += (r1 * r2); \
acc += r1 * r2;
Review
Quoted 2 lines
(quoted diff) +2
-0+ } \ + *((TD *)vd + HD(i)) = (acc + partial_sum) & MAKE_64BIT_MASK(0, 32); \
((TD *)vd)[HD(i)] = acc;
because that final mask is bogus.
r~
Quoted 5 lines
target/riscv/vector_helper.c +5
-0+} + +RVVCALL(OPMVV_VQDOTQ, vqdot_vv, QOP_SSS_B, H4, H1, H1) +RVVCALL(OPMVV_VQDOTQ, vqdotu_vv, QOP_UUU_B, H4, H1, H1) +RVVCALL (OPMVV_VQDOTQ , vqdotsu_vv,QOP_SUS_B ,H4 ,H1 ,H1 )
Re: [RFC PATCH 2/3] target/riscv: rvv: Add Zvqdotq support
Quoted 18 lines · On Tue, Sep 2, 2025 at 22:38 Richard Henderson <[email protected]>…
Quoted 1 line · Max Chou
+#define
OPMVV_VQDOTQ(NAME, TD, T1, T2, TX1, TX2, HD, HS1, HS2)\
Quoted 1 line
+static void do_##NAME(void *vd, void *vs1, void *vs2, int i)
\
Quoted 1 line
+{
\
Quoted 1 line
+ int idx;
\
Quoted 1 line
+ T1 r1;
\
Quoted 1 line
+ T2 r2;
\
Quoted 1 line
+ TX1
*r1_buf= (TX1 *)vs1 + HD(i);\
Quoted 1 line
+ TX2
*r2_buf= (TX2 *)vs2 + HD(i);\
Quoted 1 line
+ TD acc = *((TD *)vd + HD(i));
Quoted 8 lines
\
Quoted 1 line
+
int64_tpartial_sum= 0;\
I think it's clear
partial_sumshould be the 32-bit type TD. Indeed, I'm not sure why you don't just haveTD acc = ((TD *)vd)[HD(i)];
Thanks for the suggestion. I’ll update version 2 for this part.
Quoted 14 lines
Quoted 2 lines
+
\
Quoted 1 line
+ for (idx = 0; idx < 4; ++idx) {
\
Quoted 1 line
+ r1 = *((T1 *)r1_buf + HS1(idx));
\
Quoted 1 line
+ r2 = *((T2 *)r2_buf + HS2(idx));
\
Quoted 1 line
+
partial_sum+= (r1 * r2);\
acc += r1 * r2;
Quoted 9 lines
Quoted 1 line
+ }
\
Quoted 1 line
+ *((TD *)vd + HD(i)) = (acc +
partial_sum) &MAKE_64BIT_MASK(0, 32);\
((TD *)vd)[HD(i)] = acc;because that final mask is bogus.
The partial_sum and the final mask are created to ensure the behavior described in the Zvqdotq isa spec section 3 as follows:
“Finally, the four products are accumulated into the corresponding element of vd, wrapping around signed overflow.”
Thanks,
Max.
Re: [RFC PATCH 2/3] target/riscv: rvv: Add Zvqdotq support
Quoted 3 lines · Max Chou
The
partial_sumand the final mask are created to ensure the behavior described in the Zvqdotq isa spec section 3 as follows:“Finally, the four products are accumulated into the corresponding element of vd, wrapping around signed overflow.”
This is accomplished by the -fwrapv argument to the compiler, with which all of qemu is compiled.
r~
Re: [RFC PATCH 2/3] target/riscv: rvv: Add Zvqdotq support
Quoted 5 lines · Richard Henderson
Quoted 1 line · Max Chou
The
partial_sumand the final mask are created to ensure the behaviordescribed in the
Quoted 2 lines
Zvqdotq isa spec section 3 as follows:
Quoted 8 lines
Quoted 1 line
“Finally, the four products are accumulated into the corresponding
element of vd, wrapping
Quoted 1 line
around signed overflow.”
This is accomplished by the -fwrapv argument to the compiler, with which all of qemu is
compiled.
Thanks for this information. It’s helpful.
I’ll update version 2 to include this part.
Thanks,
Max
RFC PATCH 3/3 target/riscv: Expose Zvqdotq extension as a cpu property
In reply to Max Chou--- target/riscv/cpu.c | 1 + 1 file changed, 1 insertion(+)
target/riscv/cpu.c +1 -0
@@ -1373,6 +1373,7 @@ const RISCVCPUMultiExtConfig riscv_cpu_vendor_exts[] = {/* These are experimental so mark with 'x-' */ const RISCVCPUMultiExtConfig riscv_cpu_experimental_exts[] = {MULTI_EXT_CFG_BOOL ("x-svukte" , ext_svukte, false), +MULTI_EXT_CFG_BOOL ("x-zvqdotq" , ext_zvqdotq, false), { }, };
Signature · 1 line
2.43.0