Skip to contentSkip to conversation

Add Zvqdotq support

RFC PATCH Superseded Max Chou 2 participants 7 messages Sep 3, 2025 RISC-V

v2 of 5 Superseded Latest is v5 →

lore
Share & tools
lore

Max Chou

author
#0 lore raw

v2 cover Add Zvqdotq support

This patch series adds Zvqdotq support.

The isa spec of Zvqdotq extension is not ratified yet, so this patch series is based on the latest draft of the spec (v0.0.2) and make the Zvqdotq extension as an experimental extension.

The draft of the Zvqdotq isa spec:
https://github.com/riscv/riscv-dot-product

v2:
* Remove unnecessary variable and mask (Thanks for Richard Henderson's suggestion)

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                  | 61 +++++++++++++++++++
 8 files changed, 150 insertions(+)
 create mode 100644 target/riscv/insn_trans/trans_rvzvqdotq.c.inc

--
2.39.3

author
#1 lore raw

RFC PATCH v2 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
index d055ddf462..95edd02e68 100644
@@ -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
index e2d116f0df..5da59c22d6 100644
@@ -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
index 78fb279184..7015370ab0 100644
@@ -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.39.3
author
#2 lore raw

RFC PATCH v2 2/3 target/riscv: rvv: Add Zvqdotq support

In reply to Max Chou

Support 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                  | 61 +++++++++++++++++++
 5 files changed, 142 insertions(+)
 create mode 100644 target/riscv/insn_trans/trans_rvzvqdotq.c.inc
target/riscv/helper.h +10 -0
index f712b1c368..80274f1dad 100644
@@ -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
index cd23b1f3a9..50a6156667 100644
@@ -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
index 9ddef2d6e2..6f43ed1ffd 100644
@@ -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 +61 -0

70 lines not on this page · raw message

Signature · 1 line
2.39.3
#3 lore raw

Re: [RFC PATCH v2 2/3] target/riscv: rvv: Add Zvqdotq support

Quoted 4 lines · Max Chou
target/riscv/vector_helper.c +4 -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;                                                            \

target/riscv/vector_helper.c GEN_VEXT_INT_EXT(vsext_vf2_d, int64_t, int32_t, H8, H4) · line 5497

Quoted 10 lines
target/riscv/vector_helper.c +10 -0
+    T1 r1;                                                              \
+    T2 r2;                                                              \
+    TX1 *r1_buf = (TX1 *)vs1 + HD(i);                                   \
+    TX2 *r2_buf = (TX2 *)vs2 + HD(i);                                   \
+    TD acc = ((TD *)vd)[HD(i)];                                         \
+                                                                        \
+    for (idx = 0; idx < 4; ++idx) {                                     \
+        r1 = (TD)(*((T1 *)r1_buf + HS1(idx)));                          \
+        r2 = (TD)(*((T2 *)r2_buf + HS2(idx)));                          \
+        acc += r1 * r2;                                                 \

Incorrect typing or casting, take your pick.

I suggest

    for (int idx = 0; idx < 4; ++idx) {
        T1 r1 = ((T1 *)r1_buf)[HS1(idx)];
        T2 r2 = ((T2 *)r1_buf)[HS2(idx)];
        acc += (TD)r1 * (TD)r2;
    }

r~

#4 lore raw

Re: [RFC PATCH v2 2/3] target/riscv: rvv: Add Zvqdotq support

In reply to Max Chou
Quoted 4 lines · Max Chou
target/riscv/vector_helper.c +4 -0
+#define OPMVX_VQDOTQ(NAME, TD, T1, T2, TX1, TX2, HD, HS1, HS2)          \
+static void do_##NAME(void *vd, target_long s1, void *vs2, int i)       \
+{                                                                       \
+    int idx;                                                            \

target/riscv/vector_helper.c GEN_VEXT_INT_EXT(vsext_vf2_d, int64_t, int32_t, H8, H4) · line 5523

Quoted 10 lines
target/riscv/vector_helper.c +10 -0
+    T1 r1;                                                              \
+    T2 r2;                                                              \
+    TX1 *r1_buf = (TX1 *)&s1;                                           \
+    TX2 *r2_buf = (TX2 *)vs2 + HD(i);                                   \
+    TD acc = ((TD *)vd)[HD(i)];                                         \
+                                                                        \
+    for (idx = 0; idx < 4; ++idx) {                                     \
+        r1 = *((T1 *)r1_buf + HS1(idx));                                \
+        r2 = *((T2 *)r2_buf + HS2(idx));                                \
+        acc += r1 * r2;                                                 \

One could argue for a missing widening cast to TD here. You got away with it because the only uses happen to have small inputs and "int" sized outputs, so C arithmetic promotion worked for you.

You can move the variable declarations into the loop.

r~

author
#5 lore raw

Re: [RFC PATCH v2 2/3] target/riscv: rvv: Add Zvqdotq support

Quoted 30 lines · Richard Henderson
Quoted 1 line · Max Chou

+#define OPMVX_VQDOTQ(NAME, TD, T1, T2, TX1, TX2, HD, HS1, HS2)

\

Quoted 1 line

+static void do_##NAME(void *vd, target_long s1, 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 *)&s1;

\

Quoted 1 line

+ TX2 *r2_buf = (TX2 *)vs2 + HD(i);

\

Quoted 1 line

+ TD acc = ((TD *)vd)[HD(i)];

\

Quoted 1 line

+

\

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

+ acc += r1 * r2;

\

Quoted 8 lines

One could argue for a missing widening cast to TD here. You got away with it because the
only uses happen to have small inputs and "int" sized outputs, so C arithmetic promotion
worked for you.

You can move the variable declarations into the loop.

Thanks for the suggestions. I overlooked this part. I’ll update version 3 to include this part.

Thanks a lot,
Max

author
#6 lore raw

RFC PATCH v2 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
index 95edd02e68..ed486113ba 100644
@@ -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.39.3