Implement the vfwbdota.vv instruction from the Zvfwbdota16bf
extension.
This instruction performs a batched dot-product operation on BF16
vectors, accumulating into FP32
Signed-off-by: Max Chou <[email protected]>
---
target/riscv/helper.h | 2 +
target/riscv/insn32.decode | 3 ++
target/riscv/tcg/insn_trans/trans_rvv.c.inc | 52 +++++++++++++++++++
target/riscv/tcg/vector_helper.c | 55 +++++++++++++++++++++
4 files changed, 112 insertions(+)
diff --git a/target/riscv/helper.h b/target/riscv/helper.h
index 84571a999f4..1f2dfd6d557 100644
--- a/target/riscv/helper.h
+++ b/target/riscv/helper.h
@@ -1395,6 +1395,8 @@ DEF_HELPER_6(vqwdotas_vv_h, void, ptr, ptr, ptr, ptr,
env, i32)
/* Zvfwdota16bf - BF16 Dot Product */
DEF_HELPER_6(vfwdota_vv, void, ptr, ptr, ptr, ptr, env, i32)
+/* Zvfwbdota16bf - BF16 Batched Dot Product */
+DEF_HELPER_7(vfwbdota_vv, void, ptr, ptr, ptr, ptr, i32, env, i32)
/* Zvfqwdota8f - OCP FP8 Dot Product */
DEF_HELPER_6(vfqwdota_vv, void, ptr, ptr, ptr, ptr, env, i32)
diff --git a/target/riscv/insn32.decode b/target/riscv/insn32.decode
index 8a06547a570..00ed64b73fd 100644
--- a/target/riscv/insn32.decode
+++ b/target/riscv/insn32.decode
@@ -1105,6 +1105,9 @@ vqwdotas_vv 100111 . ..... ..... 000 ..... 1110111 @r_vm
# *** Zvfwdota16bf BF16 Dot-Product Extension ***
vfwdota_vv 100100 . ..... ..... 001 ..... 1110111 @r_vm
+# *** Zvfwbdota16bf BF16 Batched Dot-Product Extension ***
+vfwbdota_vv 101100 . ..... ..... 001 ..... 1110111 @r_vm
+
# *** Zvfqwdota8f OCP FP8 Dot-Product Extension ***
vfqwdota_vv 100110 . ..... ..... 001 ..... 1110111 @r_vm
vfqwdota_alt_vv 100111 . ..... ..... 001 ..... 1110111 @r_vm
diff --git a/target/riscv/tcg/insn_trans/trans_rvv.c.inc
b/target/riscv/tcg/insn_trans/trans_rvv.c.inc
index 650affc045b..d4678171ac5 100644
--- a/target/riscv/tcg/insn_trans/trans_rvv.c.inc
+++ b/target/riscv/tcg/insn_trans/trans_rvv.c.inc
@@ -4365,3 +4365,55 @@ static bool trans_##NAME(DisasContext *s, arg_rmrr *a)
\
GEN_ZVBDOTA_INT_TRANS(vqwbdotau_vv, vext_vqwbdota_check)
GEN_ZVBDOTA_INT_TRANS(vqwbdotas_vv, vext_vqwbdota_check)
+
+/* Zvfwbdota16bf BF16 Batched Dot-Product Extension */
+static bool vext_vfwbdota_check(DisasContext *s, arg_rmrr * a)
+{
+ int32_t sew = s->sew;
+ int32_t vd_eew = sew + 1;
+ return s->mstatus_fs != EXT_STATUS_DISABLED &&
+ vext_zvbdota_base_check(s, a, vd_eew) &&
+ (s->cfg_ptr->ext_zvfwbdota16bf && s->sew == MO_16 && s->altfmt ==
1);
+}
+
+#define GEN_ZVBDOTA_FP_TRANS(NAME, CHECK, EEW_VD) \
+static bool trans_##NAME(DisasContext *s, arg_rmrr * a) \
+{ \
+ if (CHECK(s, a)) { \
+ TCGv_ptr dest, src1, src2, mask; \
+ TCGv_i32 ci, desc; \
+ uint32_t data = 0; \
+ uint32_t vlenb = s->cfg_ptr->vlenb; \
+ int32_t vd_emul = MAX(0, EEW_VD + 3 - ctzl(vlenb)); \
+ \
+ gen_set_rm(s, RISCV_FRM_DYN); \
+ \
+ data = FIELD_DP32(data, VDATA, VM, a->vm); \
+ data = FIELD_DP32(data, VDATA, LMUL, vd_emul); \
+ data = FIELD_DP32(data, VDATA, VMA, s->vma); \
+ data = FIELD_DP32(data, VDATA, VTA, s->vta); \
+ data = FIELD_DP32(data, VDATA, VTA_ALL_1S, s->cfg_vta_all_1s); \
+ \
+ dest = tcg_temp_new_ptr(); \
+ mask = tcg_temp_new_ptr(); \
+ src1 = tcg_temp_new_ptr(); \
+ src2 = tcg_temp_new_ptr(); \
+ desc = tcg_constant_i32(simd_desc(s->cfg_ptr->vlenb, \
+ s->cfg_ptr->vlenb, data)); \
+ ci = tcg_constant_i32((a->rs2 & 0x7) << 3); \
+ \
+ tcg_gen_addi_ptr(dest, tcg_env, vreg_ofs(s, a->rd)); \
+ tcg_gen_addi_ptr(src1, tcg_env, vreg_ofs(s, a->rs1)); \
+ tcg_gen_addi_ptr(src2, tcg_env, vreg_ofs(s, a->rs2 & ~0x7)); \
+ tcg_gen_addi_ptr(mask, tcg_env, vreg_ofs(s, 0)); \
+ \
+ gen_helper_##NAME(dest, mask, src1, src2, ci, tcg_env, desc); \
+ \
+ finalize_rvv_inst(s); \
+ \
+ return true; \
+ } \
+ return false; \
+}
+
+GEN_ZVBDOTA_FP_TRANS(vfwbdota_vv, vext_vfwbdota_check, MO_32)
diff --git a/target/riscv/tcg/vector_helper.c b/target/riscv/tcg/vector_helper.c
index 873adf4d7a6..51170625aad 100644
--- a/target/riscv/tcg/vector_helper.c
+++ b/target/riscv/tcg/vector_helper.c
@@ -6201,3 +6201,58 @@ GEN_VEXT_ZVBDOTA_INT(vqwbdotau_vv_b, 32, 8, uint8_t, H4,
H1)
GEN_VEXT_ZVBDOTA_INT(vqwbdotas_vv_b, 32, 8, int8_t, H4, H1)
GEN_VEXT_ZVBDOTA_INT(vqwbdotau_vv_h, 64, 16, uint16_t, H8, H2)
GEN_VEXT_ZVBDOTA_INT(vqwbdotas_vv_h, 64, 16, int16_t, H8, H2)
+
+/*
+ * Zvfwbdota16bf: BF16 Batched Dot Product with Accumulation
+ */
+void HELPER(vfwbdota_vv)(void *vd, void *v0, void *vs1, void *vs2, uint32_t ci,
+ CPURISCVState *env, uint32_t desc)
+{
+ uint32_t vm = vext_vm(desc);
+ uint32_t vl = env->vl;
+ uint32_t vta = vext_vta(desc);
+ uint32_t vta_all_1s = vext_vta_all_1s(desc);
+ uint32_t vlenb = riscv_cpu_cfg(env)->vlenb;
+ uint32_t vd_emul = vext_lmul(desc);
+ uint32_t log_esz = 2;
+ uint32_t total_elems = (vlenb << vd_emul) >> log_esz;
+ uint32_t vlmax = vlenb >> MO_16;
+ uint32_t n, k;
+ bfloat16 *a_vec, *b_vec;
+ float32 acc, dot_result;
+
+ VSTART_CHECK_EARLY_EXIT(env, vl);
+
+ a_vec = g_new0(bfloat16, vlmax);
+ b_vec = g_new0(bfloat16, vlmax);