Skip to contentSkip to conversation

Add support for vector

PATCH Superseded LIU Zhiwei 3 participants 34 messages Sep 4, 2024 – Sep 10, 2024 RISC-V

v3 of 5 Superseded Latest is v5 →

lore
Share & tools
lore

LIU Zhiwei

author
#0 lore raw

v3 cover Add support for vector

From: TANG Tiancheng <[email protected]>

This patch set introduces support for the RISC-V vector extension in TCG backend for RISC-V targets.

v3:
1. Use the .insn form in cpuinfo probing.

2. Use reserved_regs to constrain the register group index instead of using constrain.

3. Avoid using macros to implement functions whenever possible.

4. Rename vtypei to vtype.

5. Move the __thread prev_vtype variable to TCGContext.

6. Support fractional LMUL setting, but since MF2 has a minimum ELEN of 32, restrict fractional LMUL to cases where SEW < 64.

7. Handle vector load/store imm12 split in a different function.

8. Remove compare vx and implement INDEX_op_cmpsel_vec for INDEX_op_cmp_vec in a more concise way.

9. Move the implementation of shi_vec from tcg_expand_vec_op to tcg_out_vec_op.

10. Address some formatting issues.

v2:
https://lists.gnu.org/archive/html/qemu-riscv/2024-08/msg00679.html

v1:
https://lists.gnu.org/archive/html/qemu-riscv/2024-08/msg00205.html

Swung0x48 (1):
tcg/riscv: Add basic support for vector

TANG Tiancheng (13):
tcg/op-gvec: Fix iteration step in 32-bit operation
util: Add RISC-V vector extension probe in cpuinfo tcg/riscv: Add riscv vset{i}vli support tcg/riscv: Implement vector load/store tcg/riscv: Implement vector mov/dup{m/i} tcg/riscv: Add support for basic vector opcodes tcg/riscv: Implement vector cmp ops tcg/riscv: Implement vector neg ops tcg/riscv: Implement vector sat/mul ops tcg/riscv: Implement vector min/max ops tcg/riscv: Implement vector shs/v ops tcg/riscv: Implement vector roti/v/x shi ops tcg/riscv: Enable native vector support for TCG host

 host/include/riscv/host/cpuinfo.h |    3 +
 include/tcg/tcg.h                 |    3 +
 tcg/riscv/tcg-target-con-set.h    |    7 +
 tcg/riscv/tcg-target-con-str.h    |    3 +
 tcg/riscv/tcg-target.c.inc        | 1047 ++++++++++++++++++++++++++---
 tcg/riscv/tcg-target.h            |   80 ++-
 tcg/riscv/tcg-target.opc.h        |   12 +
 tcg/tcg-internal.h                |    2 +
 tcg/tcg-op-gvec.c                 |    2 +-
 tcg/tcg-op-vec.c                  |    2 +-
 util/cpuinfo-riscv.c              |   26 +-
 11 files changed, 1062 insertions(+), 125 deletions(-)
 create mode 100644 tcg/riscv/tcg-target.opc.h
Signature · 1 line
2.43.0
author
#1 lore raw

PATCH v3 1/14 tcg/op-gvec: Fix iteration step in 32-bit operation

From: TANG Tiancheng <[email protected]>

The loop in the 32-bit case of the vector compare operation was incorrectly incrementing by 8 bytes per iteration instead of 4 bytes. This caused the function to process only half of the intended elements.

Fixes 9622c697d1 (tcg: Add gvec compare with immediate and scalar operand)
---
 tcg/tcg-op-gvec.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
tcg/tcg-op-gvec.c +1 -1
index 0308732d9b..78ee1ced80 100644
@@ -3939,7 +3939,7 @@ void tcg_gen_gvec_cmps(TCGCond cond, unsigned vece, uint32_t dofs,
         uint32_t i;
 
         tcg_gen_extrl_i64_i32(t1, c);
-        for (i = 0; i < oprsz; i += 8) {
+        for (i = 0; i < oprsz; i += 4) {
             tcg_gen_ld_i32(t0, tcg_env, aofs + i);
             tcg_gen_negsetcond_i32(cond, t0, t0, t1);
             tcg_gen_st_i32(t0, tcg_env, dofs + i);
Signature · 1 line
2.43.0
author
#2 lore raw

PATCH v3 2/14 util: Add RISC-V vector extension probe in cpuinfo

In reply to LIU Zhiwei

From: TANG Tiancheng <[email protected]>

Add support for probing RISC-V vector extension availability in the backend. This information will be used when deciding whether to use vector instructions in code generation.

While the compiler doesn't support RISCV_HWPROBE_EXT_ZVE64X, we use RISCV_HWPROBE_IMA_V instead.

---
 host/include/riscv/host/cpuinfo.h |  3 +++
 util/cpuinfo-riscv.c              | 26 ++++++++++++++++++++++++--
 2 files changed, 27 insertions(+), 2 deletions(-)
host/include/riscv/host/cpuinfo.h +3 -0
index 2b00660e36..727cb3204b 100644
@@ -10,9 +10,12 @@
 #define CPUINFO_ZBA             (1u << 1)
 #define CPUINFO_ZBB             (1u << 2)
 #define CPUINFO_ZICOND          (1u << 3)
+#define CPUINFO_ZVE64X          (1u << 4)
 
 /* Initialized with a constructor. */
 extern unsigned cpuinfo;
+extern unsigned riscv_vlenb;
+#define riscv_vlen (riscv_vlenb * 8)
 
 /*
  * We cannot rely on constructor ordering, so other constructors must
util/cpuinfo-riscv.c +24 -2
index 497ce12680..05917c42d8 100644
@@ -12,6 +12,7 @@
 #endif
 
 unsigned cpuinfo;
+unsigned riscv_vlenb;
 static volatile sig_atomic_t got_sigill;
 
 static void sigill_handler(int signo, siginfo_t *si, void *data)
@@ -33,7 +34,7 @@ static void sigill_handler(int signo, siginfo_t *si, void *data)
 /* Called both as constructor and (possibly) via other constructors. */
 unsigned __attribute__((constructor)) cpuinfo_init(void)
 {
-    unsigned left = CPUINFO_ZBA | CPUINFO_ZBB | CPUINFO_ZICOND;
+    unsigned left = CPUINFO_ZBA | CPUINFO_ZBB | CPUINFO_ZICOND | CPUINFO_ZVE64X;
     unsigned info = cpuinfo;
 
     if (info) {
@@ -49,6 +50,9 @@ unsigned __attribute__((constructor)) cpuinfo_init(void)
 #endif
 #if defined(__riscv_arch_test) && defined(__riscv_zicond)
     info |= CPUINFO_ZICOND;
+#endif
+#if defined(__riscv_arch_test) && defined(__riscv_zve64x)
+    info |= CPUINFO_ZVE64X;
 #endif
     left &= ~info;
 
@@ -64,7 +68,8 @@ unsigned __attribute__((constructor)) cpuinfo_init(void)
             && pair.key >= 0) {
             info |= pair.value & RISCV_HWPROBE_EXT_ZBA ? CPUINFO_ZBA : 0;
             info |= pair.value & RISCV_HWPROBE_EXT_ZBB ? CPUINFO_ZBB : 0;
-            left &= ~(CPUINFO_ZBA | CPUINFO_ZBB);
+            info |= pair.value & RISCV_HWPROBE_IMA_V ? CPUINFO_ZVE64X : 0;
+            left &= ~(CPUINFO_ZBA | CPUINFO_ZBB | CPUINFO_ZVE64X);
 #ifdef RISCV_HWPROBE_EXT_ZICOND
             info |= pair.value & RISCV_HWPROBE_EXT_ZICOND ? CPUINFO_ZICOND : 0;
             left &= ~CPUINFO_ZICOND;
@@ -112,6 +117,23 @@ unsigned __attribute__((constructor)) cpuinfo_init(void)
         assert(left == 0);
     }
 
+    if (info & CPUINFO_ZVE64X) {
+        /*
+         * Get vlenb for Vector: vsetvli rd, x0, e64.
+         * VLMAX = LMUL * VLEN / SEW.
+         * The "vsetvli rd, x0, e64" means "LMUL = 1, SEW = 64, rd = VLMAX",
+         * so "vlenb = VLMAX * 64 / 8".
+         */
+        unsigned long vlmax = 0;
+        asm volatile(".insn i 0x57, 7, %0, zero, (3 << 3)" : "=r"(vlmax));
+        if (vlmax) {
+            riscv_vlenb = vlmax * 8;
+            assert(riscv_vlen >= 64 && !(riscv_vlen & (riscv_vlen - 1)));
+        } else {
+            info &= ~CPUINFO_ZVE64X;
+        }
+    }
+
     info |= CPUINFO_ALWAYS;
     cpuinfo = info;
     return info;
Signature · 1 line
2.43.0
#3 lore raw

Re: [PATCH v3 02/14] util: Add RISC-V vector extension probe in cpuinfo

Quoted 6 lines · LIU Zhiwei
util/cpuinfo-riscv.c +6 -0
+    if (info & CPUINFO_ZVE64X) {
+        /*
+         * Get vlenb for Vector: vsetvli rd, x0, e64.
+         * VLMAX = LMUL * VLEN / SEW.
+         * The "vsetvli rd, x0, e64" means "LMUL = 1, SEW = 64, rd = VLMAX",
+         * so "vlenb = VLMAX * 64 / 8".

util/cpuinfo-riscv.c unsigned __attribute__((constructor)) cpuinfo_init(void) · line 135

Quoted 10 lines
util/cpuinfo-riscv.c +10 -0
+         */
+        unsigned long vlmax = 0;
+        asm volatile(".insn i 0x57, 7, %0, zero, (3 << 3)" : "=r"(vlmax));
+        if (vlmax) {
+            riscv_vlenb = vlmax * 8;
+            assert(riscv_vlen >= 64 && !(riscv_vlen & (riscv_vlen - 1)));
+        } else {
+            info &= ~CPUINFO_ZVE64X;
+        }
+    }

Surely this does not compile, since the riscv_vlen referenced in the assert does not exist.

That said, I've done some experimentation and I believe there is a further simplification to be had in instead saving log2(vlenb).

    if (info & CPUINFO_ZVE64X) {
        /*
         * We are guaranteed by RVV-1.0 that VLEN is a power of 2.
         * We are guaranteed by Zve64x that VLEN >= 64, and that
         * EEW of {8,16,32,64} are supported.
         *
         * Cache VLEN in a convenient form.
         */
        unsigned long vlenb;
        asm("csrr %0, vlenb" : "=r"(vlenb));
        riscv_lg2_vlenb = ctz32(vlenb);
    }

I'll talk about how this can be used against the next patch with vsetvl.

r~

author
#4 lore raw

Re: [PATCH v3 02/14] util: Add RISC-V vector extension probe in cpuinfo

Quoted 21 lines · Richard Henderson
Quoted 11 lines · LIU Zhiwei

+    if (info & CPUINFO_ZVE64X) {
+        /*
+         * Get vlenb for Vector: vsetvli rd, x0, e64.
+         * VLMAX = LMUL * VLEN / SEW.
+         * The "vsetvli rd, x0, e64" means "LMUL = 1, SEW = 64, rd = VLMAX",
+         * so "vlenb = VLMAX * 64 / 8".
+         */
+        unsigned long vlmax = 0;
+        asm volatile(".insn i 0x57, 7, %0, zero, (3 << 3)" :
"=r"(vlmax));

Quoted 8 lines

+        if (vlmax) {
+            riscv_vlenb = vlmax * 8;
+            assert(riscv_vlen >= 64 && !(riscv_vlen & (riscv_vlen - 1)));
+        } else {
+            info &= ~CPUINFO_ZVE64X;
+        }
+    }

Quoted 1 line

Surely this does not compile, since the riscv_vlen referenced in the assert does not exist.

riscv_vlen is macro about riscv_vlenb. I think you miss it.

Quoted 13 lines

That said, I've done some experimentation and I believe there is a further simplification to be had in instead saving log2(vlenb).

    if (info & CPUINFO_ZVE64X) {
        /*
         * We are guaranteed by RVV-1.0 that VLEN is a power of 2.
         * We are guaranteed by Zve64x that VLEN >= 64, and that
         * EEW of {8,16,32,64} are supported.
         *
         * Cache VLEN in a convenient form.
         */
        unsigned long vlenb;
        asm("csrr %0, vlenb" : "=r"(vlenb));

Should we use the .insn format here? Maybe we are having a compiler doesn't support vector.

Quoted 3 lines

riscv_lg2_vlenb = ctz32(vlenb);
    }

OK.

Thanks,
Zhiwei

Quoted 5 lines

I'll talk about how this can be used against the next patch with vsetvl.

r~

#5 lore raw

Re: [PATCH v3 02/14] util: Add RISC-V vector extension probe in cpuinfo

Quoted 20 lines · LIU Zhiwei
Quoted 18 lines · Richard Henderson
Quoted 16 lines · LIU Zhiwei

+    if (info & CPUINFO_ZVE64X) {
+        /*
+         * Get vlenb for Vector: vsetvli rd, x0, e64.
+         * VLMAX = LMUL * VLEN / SEW.
+         * The "vsetvli rd, x0, e64" means "LMUL = 1, SEW = 64, rd = VLMAX", +         * so "vlenb = VLMAX * 64 / 8".
+         */
+        unsigned long vlmax = 0;
+        asm volatile(".insn i 0x57, 7, %0, zero, (3 << 3)" : "=r"(vlmax)); +        if (vlmax) {
+            riscv_vlenb = vlmax * 8;
+            assert(riscv_vlen >= 64 && !(riscv_vlen & (riscv_vlen - 1))); +        } else {
+            info &= ~CPUINFO_ZVE64X;
+        }
+    }

Quoted 2 lines
Quoted 1 line

Surely this does not compile, since the riscv_vlen referenced in the assert does not exist.

riscv_vlen is macro about riscv_vlenb. I think you miss it.

I did miss the macro. But there's also no need for it to exist.

Quoted 14 lines
Quoted 13 lines

That said, I've done some experimentation and I believe there is a further simplification to be had in instead saving log2(vlenb).

    if (info & CPUINFO_ZVE64X) {
        /*
         * We are guaranteed by RVV-1.0 that VLEN is a power of 2.
         * We are guaranteed by Zve64x that VLEN >= 64, and that
         * EEW of {8,16,32,64} are supported.
         *
         * Cache VLEN in a convenient form.
         */
        unsigned long vlenb;
        asm("csrr %0, vlenb" : "=r"(vlenb));

Quoted 1 line

Should we use the .insn format here? Maybe we are having a compiler doesn't support vector.

Neither gcc nor clang requires V be enabled at compile time in order to access the CSR. It does seem like a mistake, but I'm happy to use it.

r~

author
#6 lore raw

Re: [PATCH v3 02/14] util: Add RISC-V vector extension probe in cpuinfo

Quoted 43 lines · Richard Henderson
Quoted 23 lines · LIU Zhiwei
Quoted 21 lines · Richard Henderson
Quoted 19 lines · LIU Zhiwei

+    if (info & CPUINFO_ZVE64X) {
+        /*
+         * Get vlenb for Vector: vsetvli rd, x0, e64.
+         * VLMAX = LMUL * VLEN / SEW.
+         * The "vsetvli rd, x0, e64" means "LMUL = 1, SEW = 64, rd = VLMAX",
+         * so "vlenb = VLMAX * 64 / 8".
+         */
+        unsigned long vlmax = 0;
+        asm volatile(".insn i 0x57, 7, %0, zero, (3 << 3)" :
"=r"(vlmax));
+        if (vlmax) {
+            riscv_vlenb = vlmax * 8;
+            assert(riscv_vlen >= 64 && !(riscv_vlen & (riscv_vlen
- 1)));
+        } else {
+            info &= ~CPUINFO_ZVE64X;
+        }
+    }

Quoted 2 lines
Quoted 1 line

Surely this does not compile, since the riscv_vlen referenced in the assert does not exist.

riscv_vlen is macro about riscv_vlenb. I think you miss it.

I did miss the macro.  But there's also no need for it to exist.

Quoted 14 lines
Quoted 13 lines

That said, I've done some experimentation and I believe there is a further simplification to be had in instead saving log2(vlenb).

    if (info & CPUINFO_ZVE64X) {
        /*
         * We are guaranteed by RVV-1.0 that VLEN is a power of 2.
         * We are guaranteed by Zve64x that VLEN >= 64, and that
         * EEW of {8,16,32,64} are supported.
         *
         * Cache VLEN in a convenient form.
         */
        unsigned long vlenb;
        asm("csrr %0, vlenb" : "=r"(vlenb));

Quoted 4 lines
Quoted 1 line

Should we use the .insn format here? Maybe we are having a compiler doesn't support vector.

Neither gcc nor clang requires V be enabled at compile time in order to access the CSR. It does seem like a mistake, but I'm happy to use it.

Can we follow you here? 🙂

Zhiwei

Quoted 3 lines

r~

author
#7 lore raw

PATCH v3 3/14 tcg/riscv: Add basic support for vector

In reply to LIU Zhiwei

From: Swung0x48 <[email protected]>

The RISC-V vector instruction set utilizes the LMUL field to group multiple registers, enabling variable-length vector registers. This implementation uses only the first register number of each group while reserving the other register numbers within the group.

In TCG, each VEC_IR can have 3 types (TCG_TYPE_V64/128/256), and the host runtime needs to adjust LMUL based on the type to use different register groups.

This presents challenges for TCG's register allocation. Currently, we avoid modifying the register allocation part of TCG and only expose the minimum number of vector registers.

For example, when the host vlen is 64 bits and type is TCG_TYPE_V256, with LMUL equal to 4, we use 4 vector registers as one register group. We can use a maximum of 8 register groups, but the V0 register number is reserved as a mask register, so we can effectively use at most 7 register groups. Moreover, when type is smaller than TCG_TYPE_V256, only 7 registers are forced to be used. This is because TCG cannot yet dynamically constrain registers with type; likewise, when the host vlen is 128 bits and TCG_TYPE_V256, we can use at most 15 registers.

There is not much pressure on vector register allocation in TCG now, so using 7 registers is feasible and will not have a major impact on code generation.

This patch:
1. Reserves vector register 0 for use as a mask register.
2. When using register groups, reserves the additional registers within each group.

---
 tcg/riscv/tcg-target-con-str.h |   1 +
 tcg/riscv/tcg-target.c.inc     | 166 ++++++++++++++++++++++++++-------
 tcg/riscv/tcg-target.h         |  78 +++++++++-------
 tcg/riscv/tcg-target.opc.h     |  12 +++
 4 files changed, 191 insertions(+), 66 deletions(-)
 create mode 100644 tcg/riscv/tcg-target.opc.h
tcg/riscv/tcg-target-con-str.h +1 -0
index d5c419dff1..b2b3211bcb 100644
@@ -9,6 +9,7 @@
  * REGS(letter, register_mask)
  */
 REGS('r', ALL_GENERAL_REGS)
+REGS('v', ALL_VECTOR_REGS)
 
 /*
  * Define constraint letters for constants:
tcg/riscv/tcg-target.c.inc +133 -33

220 lines not on this page · raw message

tcg/riscv/tcg-target.h +45 -33

93 lines not on this page · raw message

tcg/riscv/tcg-target.opc.h new file +12 -0
new file mode 100644
index 0000000000..b80b39e1e5
@@ -0,0 +1,12 @@
+/*
+ * Copyright (c) C-SKY Microsystems Co., Ltd.
+ *
+ * This work is licensed under the terms of the GNU GPL, version 2 or
+ * (at your option) any later version.
+ *
+ * See the COPYING file in the top-level directory for details.
+ *
+ * Target-specific opcodes for host vector expansion.  These will be
+ * emitted by tcg_expand_vec_op.  For those familiar with GCC internals,
+ * consider these to be UNSPEC with names.
+ */
Signature · 1 line
2.43.0
#8 lore raw

Re: [PATCH v3 03/14] tcg/riscv: Add basic support for vector

Quoted 28 lines · LIU Zhiwei

From: Swung0x48 <[email protected]>

The RISC-V vector instruction set utilizes the LMUL field to group multiple registers, enabling variable-length vector registers. This implementation uses only the first register number of each group while reserving the other register numbers within the group.

In TCG, each VEC_IR can have 3 types (TCG_TYPE_V64/128/256), and the host runtime needs to adjust LMUL based on the type to use different register groups.

This presents challenges for TCG's register allocation. Currently, we avoid modifying the register allocation part of TCG and only expose the minimum number of vector registers.

For example, when the host vlen is 64 bits and type is TCG_TYPE_V256, with LMUL equal to 4, we use 4 vector registers as one register group. We can use a maximum of 8 register groups, but the V0 register number is reserved as a mask register, so we can effectively use at most 7 register groups. Moreover, when type is smaller than TCG_TYPE_V256, only 7 registers are forced to be used. This is because TCG cannot yet dynamically constrain registers with type; likewise, when the host vlen is 128 bits and TCG_TYPE_V256, we can use at most 15 registers.

There is not much pressure on vector register allocation in TCG now, so using 7 registers is feasible and will not have a major impact on code generation.

Quoted 8 lines

This patch:
1. Reserves vector register 0 for use as a mask register.
2. When using register groups, reserves the additional registers within each group.

This patch does not compile.

../src/tcg/tcg.c:135:13: error: 'tcg_out_dup_vec' used but never defined [-Werror]
135 | static bool tcg_out_dup_vec(TCGContext *s, TCGType type, unsigned vece,

      |             ^~~~~~~~~~~~~~~
../src/tcg/tcg.c:137:13: error: 'tcg_out_dupm_vec' used but never defined [-Werror]
  137 | static bool tcg_out_dupm_vec(TCGContext *s, TCGType type, unsigned vece,
      |             ^~~~~~~~~~~~~~~~
../src/tcg/tcg.c:139:13: error: 'tcg_out_dupi_vec' used but never defined [-Werror]
  139 | static void tcg_out_dupi_vec(TCGContext *s, TCGType type, unsigned vece,
      |             ^~~~~~~~~~~~~~~~
In file included from ../src/tcg/tcg.c:755:
/home/rth/qemu/src/tcg/riscv/tcg-target.c.inc:516:13: error: 'tcg_out_opc_ldst_vec' defined but not used [-Werror=unused-function]
  516 | static void tcg_out_opc_ldst_vec(TCGContext *s, RISCVInsn opc, TCGReg data,
      |             ^~~~~~~~~~~~~~~~~~~~
/home/rth/qemu/src/tcg/riscv/tcg-target.c.inc:507:13: error: 'tcg_out_opc_vi' defined but not used [-Werror=unused-function]
  507 | static void tcg_out_opc_vi(TCGContext *s, RISCVInsn opc, TCGReg vd,
      |             ^~~~~~~~~~~~~~
/home/rth/qemu/src/tcg/riscv/tcg-target.c.inc:501:13: error: 'tcg_out_opc_vx' defined but not used [-Werror=unused-function]
  501 | static void tcg_out_opc_vx(TCGContext *s, RISCVInsn opc, TCGReg vd,
      |             ^~~~~~~~~~~~~~
/home/rth/qemu/src/tcg/riscv/tcg-target.c.inc:495:13: error: 'tcg_out_opc_vv' defined but not used [-Werror=unused-function]
  495 | static void tcg_out_opc_vv(TCGContext *s, RISCVInsn opc, TCGReg vd,
      |             ^~~~~~~~~~~~~~

cc1: all warnings being treated as errors

Either:
(1) Provide stubs for the functions that are required, and delay implementation of the unused functions until the patch(es) that use them.
(2) Merge the dup patch so that these functions are defined and implemented, which will also provide uses for most of the tcg_out_opc_* functions.

tcg/riscv/tcg-target.c.inc static void tcg_target_init(TCGContext *s) · line 2185

Quoted 13 lines
tcg/riscv/tcg-target.c.inc +9 -0
@@ -2100,6 +2174,32 @@ static void tcg_target_init(TCGContext *s)
  {
      tcg_target_available_regs[TCG_TYPE_I32] = 0xffffffff;
      tcg_target_available_regs[TCG_TYPE_I64] = 0xffffffff;
+    s->reserved_regs = 0;
+
+    if (cpuinfo & CPUINFO_ZVE64X) {
+        switch (riscv_vlen) {
+        case 64:
+            tcg_target_available_regs[TCG_TYPE_V64] = ALL_VECTOR_REGS;
+            tcg_target_available_regs[TCG_TYPE_V128] = ALL_DVECTOR_REG_GROUPS;
+            tcg_target_available_regs[TCG_TYPE_V256] = ALL_QVECTOR_REG_GROUPS;
+            s->reserved_regs |= (~ALL_QVECTOR_REG_GROUPS & 0xffffffff00000000);

No need for ().
Use ALL_VECTOR_REGS instead of the immediate integer.

Quoted 4 lines
tcg/riscv/tcg-target.c.inc +4 -0
+            break;
+        case 128:
+            tcg_target_available_regs[TCG_TYPE_V64] = ALL_VECTOR_REGS;
+            tcg_target_available_regs[TCG_TYPE_V128] = ALL_VECTOR_REGS;

tcg/riscv/tcg-target.c.inc static void tcg_target_init(TCGContext *s) · line 2199

Quoted 10 lines
tcg/riscv/tcg-target.c.inc +10 -0
+            tcg_target_available_regs[TCG_TYPE_V256] = ALL_DVECTOR_REG_GROUPS;
+            s->reserved_regs |= (~ALL_DVECTOR_REG_GROUPS & 0xffffffff00000000);
+            break;
+        case 256:
+            tcg_target_available_regs[TCG_TYPE_V64] = ALL_VECTOR_REGS;
+            tcg_target_available_regs[TCG_TYPE_V128] = ALL_VECTOR_REGS;
+            tcg_target_available_regs[TCG_TYPE_V256] = ALL_VECTOR_REGS;
+            break;
+        default:
+            g_assert_not_reached();

The first host with 512-bit or larger vectors will trigger the assert.

With my suggestion against patch 2, this becomes

    switch (riscv_lg2_vlenb) {
    case TCG_TYPE_V64:
        ...
    case TCG_TYPE_V128:
        ...
    default:
        /* Guaranteed by Zve64x. */
        tcg_debug_assert(riscv_lg2_vlenb >= TCG_TYPE_V256);
    }

r~

author
#9 lore raw

Re: [PATCH v3 03/14] tcg/riscv: Add basic support for vector

Quoted 55 lines · Richard Henderson
Quoted 28 lines · LIU Zhiwei

From: Swung0x48 <[email protected]>

The RISC-V vector instruction set utilizes the LMUL field to group multiple registers, enabling variable-length vector registers. This implementation uses only the first register number of each group while reserving the other register numbers within the group.

In TCG, each VEC_IR can have 3 types (TCG_TYPE_V64/128/256), and the host runtime needs to adjust LMUL based on the type to use different register groups.

This presents challenges for TCG's register allocation. Currently, we avoid modifying the register allocation part of TCG and only expose the minimum number of vector registers.

For example, when the host vlen is 64 bits and type is TCG_TYPE_V256, with LMUL equal to 4, we use 4 vector registers as one register group. We can use a maximum of 8 register groups, but the V0 register number is reserved as a mask register, so we can effectively use at most 7 register groups. Moreover, when type is smaller than TCG_TYPE_V256, only 7 registers are forced to be used. This is because TCG cannot yet dynamically constrain registers with type; likewise, when the host vlen is 128 bits and TCG_TYPE_V256, we can use at most 15 registers.

There is not much pressure on vector register allocation in TCG now, so using 7 registers is feasible and will not have a major impact on code generation.

Quoted 8 lines

This patch:
1. Reserves vector register 0 for use as a mask register.
2. When using register groups, reserves the additional registers within each group.

This patch does not compile.

../src/tcg/tcg.c:135:13: error: 'tcg_out_dup_vec' used but never defined [-Werror]
  135 | static bool tcg_out_dup_vec(TCGContext *s, TCGType type, unsigned vece,
      |             ^~~~~~~~~~~~~~~
../src/tcg/tcg.c:137:13: error: 'tcg_out_dupm_vec' used but never defined [-Werror]
  137 | static bool tcg_out_dupm_vec(TCGContext *s, TCGType type, unsigned vece,
      |             ^~~~~~~~~~~~~~~~
../src/tcg/tcg.c:139:13: error: 'tcg_out_dupi_vec' used but never defined [-Werror]
  139 | static void tcg_out_dupi_vec(TCGContext *s, TCGType type, unsigned vece,
      |             ^~~~~~~~~~~~~~~~
In file included from ../src/tcg/tcg.c:755:
/home/rth/qemu/src/tcg/riscv/tcg-target.c.inc:516:13: error: 'tcg_out_opc_ldst_vec' defined but not used [-Werror=unused-function] 516 | static void tcg_out_opc_ldst_vec(TCGContext *s, RISCVInsn opc, TCGReg data,
      |             ^~~~~~~~~~~~~~~~~~~~
/home/rth/qemu/src/tcg/riscv/tcg-target.c.inc:507:13: error: 'tcg_out_opc_vi' defined but not used [-Werror=unused-function] 507 | static void tcg_out_opc_vi(TCGContext *s, RISCVInsn opc, TCGReg vd,

Quoted 8 lines

      |             ^~~~~~~~~~~~~~
/home/rth/qemu/src/tcg/riscv/tcg-target.c.inc:501:13: error: 'tcg_out_opc_vx' defined but not used [-Werror=unused-function] 501 | static void tcg_out_opc_vx(TCGContext *s, RISCVInsn opc, TCGReg vd,
      |             ^~~~~~~~~~~~~~
/home/rth/qemu/src/tcg/riscv/tcg-target.c.inc:495:13: error: 'tcg_out_opc_vv' defined but not used [-Werror=unused-function] 495 | static void tcg_out_opc_vv(TCGContext *s, RISCVInsn opc, TCGReg vd,
      |             ^~~~~~~~~~~~~~
cc1: all warnings being treated as errors

Oops. We miss compiling each patch one by one.

Quoted 4 lines

Either:
(1) Provide stubs for the functions that are required, and delay implementation of the unused functions until the patch(es) that use them.

We will take this way.

Quoted 15 lines

(2) Merge the dup patch so that these functions are defined and implemented, which will also provide uses for most of the tcg_out_opc_* functions.

Quoted 11 lines
tcg/riscv/tcg-target.c.inc +7 -0
@@ -2100,6 +2174,32 @@ static void tcg_target_init(TCGContext *s)
   {
       tcg_target_available_regs[TCG_TYPE_I32] = 0xffffffff;
       tcg_target_available_regs[TCG_TYPE_I64] = 0xffffffff;
+    s->reserved_regs = 0;
+
+    if (cpuinfo & CPUINFO_ZVE64X) {
+        switch (riscv_vlen) {
+        case 64:
+            tcg_target_available_regs[TCG_TYPE_V64] = ALL_VECTOR_REGS;
+            tcg_target_available_regs[TCG_TYPE_V128] = 
Quoted 8 lines

Review static void tcg_target_init(TCGContext *s) · line 2188

Quoted 5 lines
(quoted diff) +2 -0
 ALL_DVECTOR_REG_GROUPS;
+            tcg_target_available_regs[TCG_TYPE_V256] = 
 ALL_QVECTOR_REG_GROUPS;
+            s->reserved_regs |= (~ALL_QVECTOR_REG_GROUPS & 
 0xffffffff00000000);

No need for ().
Use ALL_VECTOR_REGS instead of the immediate integer.

OK.

Quoted 22 lines
Quoted 9 lines
tcg/riscv/tcg-target.c.inc +5 -0
+            break;
+        case 128:
+            tcg_target_available_regs[TCG_TYPE_V64] = ALL_VECTOR_REGS;
+            tcg_target_available_regs[TCG_TYPE_V128] = ALL_VECTOR_REGS;
+            tcg_target_available_regs[TCG_TYPE_V256] = 

ALL_DVECTOR_REG_GROUPS;
+            s->reserved_regs |= (~ALL_DVECTOR_REG_GROUPS & 0xffffffff00000000);

Quoted 8 lines

+            break;
+        case 256:
+            tcg_target_available_regs[TCG_TYPE_V64] = ALL_VECTOR_REGS; +            tcg_target_available_regs[TCG_TYPE_V128] = ALL_VECTOR_REGS; +            tcg_target_available_regs[TCG_TYPE_V256] = ALL_VECTOR_REGS; +            break;
+        default:
+            g_assert_not_reached();

The first host with 512-bit or larger vectors will trigger the assert.

With my suggestion against patch 2, this becomes

Quoted 10 lines

    switch (riscv_lg2_vlenb) {
    case TCG_TYPE_V64:
        ...
    case TCG_TYPE_V128:
        ...
    default:
        /* Guaranteed by Zve64x. */ tcg_debug_assert(riscv_lg2_vlenb >= TCG_TYPE_V256); }

Agree.

Thanks,

Zhiwei

Quoted 2 lines

r~

author
#10 lore raw

PATCH v3 4/14 tcg/riscv: Add riscv vset{i}vli support

In reply to LIU Zhiwei

From: TANG Tiancheng <[email protected]>

In RISC-V, vector operations require initial configuration using the vset{i}vl{i} instruction.

This instruction:
1. Sets the vector length (vl) in bytes
2. Configures the vtype register, which includes:
SEW (Single Element Width)
LMUL (vector register group multiplier) Other vector operation parameters

This configuration is crucial for defining subsequent vector operation behavior. To optimize performance, the configuration process is managed dynamically:
1. Reconfiguration using vset{i}vl{i} is necessary when SEW or vector register group width changes.
2. The vset instruction can be omitted when configuration remains unchanged.

This optimization is only effective within a single TB. Each TB requires reconfiguration at its start, as the current state cannot be obtained from hardware.

---
 include/tcg/tcg.h          |   3 +
 tcg/riscv/tcg-target.c.inc | 128 +++++++++++++++++++++++++++++++++++++
 2 files changed, 131 insertions(+)
include/tcg/tcg.h +3 -0
index 21d5884741..267e6ff95c 100644
@@ -566,6 +566,9 @@ struct TCGContext {
 
     /* Exit to translator on overflow. */
     sigjmp_buf jmp_trans;
+
+    /* For host-specific values. */
+    int riscv_host_vtype;
 };
 
 static inline bool temp_readonly(TCGTemp *ts)
tcg/riscv/tcg-target.c.inc +128 -0

158 lines not on this page · raw message

Signature · 1 line
2.43.0
#11 lore raw

Re: [PATCH v3 04/14] tcg/riscv: Add riscv vset{i}vli support

Quoted 37 lines · LIU Zhiwei

From: TANG Tiancheng <[email protected]>

In RISC-V, vector operations require initial configuration using the vset{i}vl{i} instruction.

This instruction:
1. Sets the vector length (vl) in bytes
2. Configures the vtype register, which includes:
SEW (Single Element Width)
LMUL (vector register group multiplier) Other vector operation parameters

This configuration is crucial for defining subsequent vector operation behavior. To optimize performance, the configuration process is managed dynamically:
1. Reconfiguration using vset{i}vl{i} is necessary when SEW or vector register group width changes.
2. The vset instruction can be omitted when configuration remains unchanged.

This optimization is only effective within a single TB. Each TB requires reconfiguration at its start, as the current state cannot be obtained from hardware.

---
  include/tcg/tcg.h          |   3 +
  tcg/riscv/tcg-target.c.inc | 128 +++++++++++++++++++++++++++++++++++++
  2 files changed, 131 insertions(+)
include/tcg/tcg.h +0 -0
index 21d5884741..267e6ff95c 100644

include/tcg/tcg.h struct TCGContext { · line 572

Quoted 7 lines
include/tcg/tcg.h +3 -0
@@ -566,6 +566,9 @@ struct TCGContext {
 
      /* Exit to translator on overflow. */
      sigjmp_buf jmp_trans;
+
+    /* For host-specific values. */
+    int riscv_host_vtype;
  };

(1) At minimum this needs #ifdef __riscv.
I planned to think of a cleaner way to do this,
but haven't gotten there yet.
I had also planned to place it higher in the structure, before the large temp arrays, so that the structure offset would be smaller.

(2) I have determined through experimentation that vtype alone is insufficient. While vtype + avl would be sufficient, it is inefficient.
Best to store the original inputs: TCGType and SEW, since that way there's no effort required when querying the current SEW for use in load/store/logicals.

The bug here appears as TCG swaps between TCGTypes for different operations. E.g. if the vtype computed for (V64, E8) is the same as the vtype computed for (V128, E8), with AVL differing, then we will incorrectly omit the vsetvl instruction.

My test case was tcg/tests/aarch64-linux-user/sha1-vector

The naming of these functions is varied and inconsistent. I suggest the following:

static void set_vtype(TCGContext *s, TCGType type, MemOp vsew)

{
    unsigned vtype, insn, avl;
    int lmul;
    RISCVVlmul vlmul;
    bool lmul_eq_avl;

    s->riscv_cur_type = type;
    s->riscv_cur_vsew = vsew;

    /* Match riscv_lg2_vlenb to TCG_TYPE_V64. */
    QEMU_BUILD_BUG_ON(TCG_TYPE_V64 != 3);

    lmul = type - riscv_lg2_vlenb;
    if (lmul < -3) {
        /* Host VLEN >= 1024 bits. */
        vlmul = VLMUL_M1;
        lmul_eq_avl = false;
    } else if (lmul < 3) {
        /* 1/8 ... 1 ... 8 */
        vlmul = lmul & 7;
        lmul_eq_avl = true;
    } else {
        /* Guaranteed by Zve64x. */
        g_assert_not_reached();
    }

    avl = tcg_type_size(type) >> vsew;
    vtype = encode_vtype(true, true, vsew, vlmul);

    if (avl < 32) {
        insn = encode_i(OPC_VSETIVLI, TCG_REG_ZERO, avl, vtype);
    } else if (lmul_eq_avl) {
        /* rd != 0 and rs1 == 0 uses vlmax */
        insn = encode_i(OPC_VSETVLI, TCG_REG_TMP0, TCG_REG_ZERO, vtype);
    } else {
        tcg_out_opc_imm(s, OPC_ADDI, TCG_REG_TMP0, TCG_REG_ZERO, avl);
        insn = encode_i(OPC_VSETVLI, TCG_REG_ZERO, TCG_REG_TMP0, vtype);
    }
    tcg_out32(s, insn);
}

static MemOp set_vtype_len(TCGContext *s, TCGType type)

{
    if (type != s->riscv_cur_type) {
        set_type(s, type, MO_64);
    }
    return s->riscv_cur_vsew;
}

static void set_vtype_len_sew(TCGContext *s, TCGType type, MemOp vsew)

{
    if (type != s->riscv_cur_type || vsew != s->riscv_cur_vsew) {
        set_type(s, type, vsew);
    }
}

(1) The storing of lg2(vlenb) means we can convert all of the division into subtraction.
(2) get_vec_type_bytes() already exists as tcg_type_size().
(3) Make use of the signed 3-bit encoding of vlmul.
(4) Make use of rd != 0, rs1 = 0 for the relatively common case of AVL = 32.

r~

author
#12 lore raw

Re: [PATCH v3 04/14] tcg/riscv: Add riscv vset{i}vli support

Quoted 57 lines · Richard Henderson
Quoted 37 lines · LIU Zhiwei

From: TANG Tiancheng <[email protected]>

In RISC-V, vector operations require initial configuration using the vset{i}vl{i} instruction.

This instruction:
   1. Sets the vector length (vl) in bytes
   2. Configures the vtype register, which includes:
     SEW (Single Element Width)
     LMUL (vector register group multiplier) Other vector operation parameters

This configuration is crucial for defining subsequent vector operation behavior. To optimize performance, the configuration process is managed dynamically:
   1. Reconfiguration using vset{i}vl{i} is necessary when SEW or vector register group width changes.
   2. The vset instruction can be omitted when configuration remains unchanged.

This optimization is only effective within a single TB. Each TB requires reconfiguration at its start, as the current state cannot be obtained from hardware.

---
  include/tcg/tcg.h          |   3 + tcg/riscv/tcg-target.c.inc | 128 +++++++++++++++++++++++++++++++++++++

  2 files changed, 131 insertions(+)
include/tcg/tcg.h +0 -0
index 21d5884741..267e6ff95c 100644

include/tcg/tcg.h struct TCGContext { · line 571

Quoted 6 lines
include/tcg/tcg.h +3 -0
@@ -566,6 +566,9 @@ struct TCGContext {
         /* Exit to translator on overflow. */
       sigjmp_buf jmp_trans;
+
+    /* For host-specific values. */
+    int riscv_host_vtype;
   };

(1) At minimum this needs #ifdef __riscv.
    I planned to think of a cleaner way to do this,
    but haven't gotten there yet.
    I had also planned to place it higher in the structure, before the large temp arrays, so that the structure offset would be smaller.

(2) I have determined through experimentation that vtype alone is insufficient. While vtype + avl would be sufficient, it is inefficient.
    Best to store the original inputs: TCGType and SEW, since that way there's no effort required when querying the current SEW for use in load/store/logicals.

Quoted 7 lines

    The bug here appears as TCG swaps between TCGTypes for different operations.  E.g. if the vtype computed for (V64, E8) is the same as the vtype computed for (V128, E8), with AVL differing, then we will incorrectly omit the vsetvl instruction.

    My test case was tcg/tests/aarch64-linux-user/sha1-vector

Agree.

Quoted 15 lines

The naming of these functions is varied and inconsistent. I suggest the following:

static void set_vtype(TCGContext *s, TCGType type, MemOp vsew) {
    unsigned vtype, insn, avl;
    int lmul;
    RISCVVlmul vlmul;
    bool lmul_eq_avl;

    s->riscv_cur_type = type;
    s->riscv_cur_vsew = vsew;

Quoted 7 lines

    /* Match riscv_lg2_vlenb to TCG_TYPE_V64. */
    QEMU_BUILD_BUG_ON(TCG_TYPE_V64 != 3);

    lmul = type - riscv_lg2_vlenb;
    if (lmul < -3) {
        /* Host VLEN >= 1024 bits. */
        vlmul = VLMUL_M1;

I am not sure if we should use VLMUL_MF8,

Quoted 10 lines

lmul_eq_avl = false;
    } else if (lmul < 3) {
        /* 1/8 ... 1 ... 8 */
        vlmul = lmul & 7;
        lmul_eq_avl = true;
    } else {
        /* Guaranteed by Zve64x. */
        g_assert_not_reached();
    }

Quoted 5 lines

    avl = tcg_type_size(type) >> vsew; vtype = encode_vtype(true, true, vsew, vlmul);

    if (avl < 32) {
        insn = encode_i(OPC_VSETIVLI, TCG_REG_ZERO, avl, vtype);

Which may benifit here? we usually use  lmul as smallest as we can for macro ops split.

Quoted 6 lines

    } else if (lmul_eq_avl) {
        /* rd != 0 and rs1 == 0 uses vlmax */
        insn = encode_i(OPC_VSETVLI, TCG_REG_TMP0, TCG_REG_ZERO, vtype); } else {
        tcg_out_opc_imm(s, OPC_ADDI, TCG_REG_TMP0, TCG_REG_ZERO, avl); insn = encode_i(OPC_VSETVLI, TCG_REG_ZERO, TCG_REG_TMP0, vtype);

And perhaps here.

Quoted 8 lines

    }
    tcg_out32(s, insn);
}

static MemOp set_vtype_len(TCGContext *s, TCGType type) {
    if (type != s->riscv_cur_type) { set_type(s, type, MO_64);

I think you mean set_vtype here.

Quoted 8 lines

    }
    return s->riscv_cur_vsew;
}

static void set_vtype_len_sew(TCGContext *s, TCGType type, MemOp vsew) {
    if (type != s->riscv_cur_type || vsew != s->riscv_cur_vsew) { set_type(s, type, vsew);

and set_vtype here.

Thanks,
Zhiwei

Quoted 11 lines

    }
}

(1) The storing of lg2(vlenb) means we can convert all of the division into subtraction.
(2) get_vec_type_bytes() already exists as tcg_type_size().
(3) Make use of the signed 3-bit encoding of vlmul.
(4) Make use of rd != 0, rs1 = 0 for the relatively common case of AVL = 32.

r~

#13 lore raw

Re: [PATCH v3 04/14] tcg/riscv: Add riscv vset{i}vli support

Quoted 5 lines · LIU Zhiwei
Quoted 4 lines

    lmul = type - riscv_lg2_vlenb;
    if (lmul < -3) {
        /* Host VLEN >= 1024 bits. */
        vlmul = VLMUL_M1;

I am not sure if we should use VLMUL_MF8,

Perhaps. See below.

Quoted 9 lines
Quoted 9 lines

    } else if (lmul < 3) {
        /* 1/8 ... 1 ... 8 */
        vlmul = lmul & 7;
        lmul_eq_avl = true;
    } else {
        /* Guaranteed by Zve64x. */
        g_assert_not_reached();
    }

Quoted 6 lines
Quoted 5 lines

    avl = tcg_type_size(type) >> vsew; vtype = encode_vtype(true, true, vsew, vlmul);

    if (avl < 32) {
        insn = encode_i(OPC_VSETIVLI, TCG_REG_ZERO, avl, vtype);

Which may benifit here? we usually use  lmul as smallest as we can for macro ops split.

lmul is unchanged, just explicitly setting AVL as well. The "benefit" is that AVL is visible in the disassembly, and that we are able to discard the result.

There doesn't appear to be a down side. Is there one?

Quoted 3 lines
Quoted 3 lines

    } else if (lmul_eq_avl) {
        /* rd != 0 and rs1 == 0 uses vlmax */
        insn = encode_i(OPC_VSETVLI, TCG_REG_TMP0, TCG_REG_ZERO, vtype);

As opposed to here, where we must clobber a register. It is a scratch reg, sure, and probably affects nothing in any microarch which does register renaming.

Quoted 4 lines
Quoted 3 lines

    } else {
        tcg_out_opc_imm(s, OPC_ADDI, TCG_REG_TMP0, TCG_REG_ZERO, avl); insn = encode_i(OPC_VSETVLI, TCG_REG_ZERO, TCG_REG_TMP0, vtype);

And perhaps here.

Here, lmul does *not* equal avl, and so we must set it, and because of non-use of VSETIVLI we also know that it does not fit in uimm5.

But here's a follow-up question regarding current micro-architectures:

How much benefit is there from adjusting LMUL < 1, or AVL < VLMAX?

For instance, on other hosts with 128-bit vectors, we also promise support for 64-bit registers, just so we can support guests which have 64-bit vector operations. In existing hosts (x86, ppc, s390x, loongarch) we accept that the host instruction will operate on all 128-bits; we simply ignore half of any result.

Thus the question becomes: can we minimize the number of vset* instructions by bounding minimal lmul to 1 (or whatever) and always leaving avl as the full register? If so, the only vset* changes are for SEW changes, or for load/store that are smaller than V*1REG64.

r~

author
#14 lore raw

Re: [PATCH v3 04/14] tcg/riscv: Add riscv vset{i}vli support

Quoted 42 lines · Richard Henderson
Quoted 5 lines · LIU Zhiwei
Quoted 4 lines

    lmul = type - riscv_lg2_vlenb;
    if (lmul < -3) {
        /* Host VLEN >= 1024 bits. */
        vlmul = VLMUL_M1;

I am not sure if we should use VLMUL_MF8,

Perhaps.  See below.

Quoted 9 lines
Quoted 9 lines

    } else if (lmul < 3) {
        /* 1/8 ... 1 ... 8 */
        vlmul = lmul & 7;
        lmul_eq_avl = true;
    } else {
        /* Guaranteed by Zve64x. */
        g_assert_not_reached();
    }

Quoted 6 lines
Quoted 5 lines

    avl = tcg_type_size(type) >> vsew; vtype = encode_vtype(true, true, vsew, vlmul);

    if (avl < 32) {
        insn = encode_i(OPC_VSETIVLI, TCG_REG_ZERO, avl, vtype);

Which may benifit here? we usually use  lmul as smallest as we can for macro ops split.

lmul is unchanged, just explicitly setting AVL as well. The "benefit" is that AVL is visible in the disassembly, and that we are able to discard the result.

There doesn't appear to be a down side.  Is there one?

Quoted 3 lines
Quoted 3 lines

    } else if (lmul_eq_avl) {
        /* rd != 0 and rs1 == 0 uses vlmax */
        insn = encode_i(OPC_VSETVLI, TCG_REG_TMP0, TCG_REG_ZERO, vtype);

As opposed to here, where we must clobber a register. It is a scratch reg, sure, and probably affects nothing in any microarch which does register renaming.

Quoted 3 lines
Quoted 3 lines

    } else {
        tcg_out_opc_imm(s, OPC_ADDI, TCG_REG_TMP0, TCG_REG_ZERO, avl); insn = encode_i(OPC_VSETVLI, TCG_REG_ZERO, TCG_REG_TMP0, vtype);

Quoted 7 lines
Quoted 1 line

And perhaps here.

Here, lmul does *not* equal avl, and so we must set it, and because of non-use of VSETIVLI we also know that it does not fit in uimm5.

But here's a follow-up question regarding current micro-architectures:

  How much benefit is there from adjusting LMUL < 1, or AVL < VLMAX?

It may reduce some macro ops for LMUL < 1 than LMUL = 1. For example, on host with 128-bit vector,

1) LMUL = 1/2, only one macro ops.

vsetivli x0, 8, e32, mf2
vadd.v.v  x2, x4, x5

2) LMUL = 1, two macro ops.

vsetivli x0, 8, e32, m1
vadd.v.v x2, x4, x5

Quoted 4 lines

For instance, on other hosts with 128-bit vectors, we also promise support for 64-bit registers, just so we can support guests which have 64-bit vector operations.  In existing hosts (x86, ppc, s390x, loongarch) we accept that the host instruction will operate on all 128-bits; we simply ignore half of any result.

Thus the question becomes: can we minimize the number of vset* instructions by bounding minimal lmul to 1 (or whatever) and always leaving avl as the full register?

I think the question we are talking about is when TCG_TYPE_V* is smaller than vlen, should we use fraction lmul?

1) Fraction lmul leads to less macro ops. (Depend on micro-architectures).

2) LMUL = 1 leads to less vset*.

I like to use the 1), because vset*vli we are using can be fusion-ed probably.

Thanks,
Zhiwei

Quoted 4 lines

If so, the only vset* changes are for SEW changes, or for load/store that are smaller than V*1REG64.

r~

author
#15 lore raw

PATCH v3 5/14 tcg/riscv: Implement vector load/store

In reply to LIU Zhiwei

From: TANG Tiancheng <[email protected]>

---
 tcg/riscv/tcg-target-con-set.h |   2 +
 tcg/riscv/tcg-target.c.inc     | 202 +++++++++++++++++++++++++++++++--
 2 files changed, 196 insertions(+), 8 deletions(-)
tcg/riscv/tcg-target-con-set.h +2 -0
index aac5ceee2b..d73a62b0f2 100644
@@ -21,3 +21,5 @@ C_O1_I2(r, rZ, rZ)
 C_N1_I2(r, r, rM)
 C_O1_I4(r, r, rI, rM, rM)
 C_O2_I4(r, r, rZ, rZ, rM, rM)
+C_O0_I2(v, r)
+C_O1_I1(v, r)
tcg/riscv/tcg-target.c.inc +194 -8

278 lines not on this page · raw message

Signature · 1 line
2.43.0
#16 lore raw

Re: [PATCH v3 05/14] tcg/riscv: Implement vector load/store

Quoted 81 lines · LIU Zhiwei
tcg/riscv/tcg-target.c.inc +43 -6
@@ -811,31 +849,52 @@ static void tcg_out_extrl_i64_i32(TCGContext *s, TCGReg ret, TCGReg arg)
      tcg_out_ext32s(s, ret, arg);
  }
 
-static void tcg_out_ldst(TCGContext *s, RISCVInsn opc, TCGReg data,
-                         TCGReg addr, intptr_t offset)
+static intptr_t split_offset_scalar(TCGContext *s, TCGReg *addr,
+                                    intptr_t offset)
  {
      intptr_t imm12 = sextreg(offset, 0, 12);
 
      if (offset != imm12) {
          intptr_t diff = tcg_pcrel_diff(s, (void *)offset);
 
-        if (addr == TCG_REG_ZERO && diff == (int32_t)diff) {
+        if (*addr == TCG_REG_ZERO && diff == (int32_t)diff) {
              imm12 = sextreg(diff, 0, 12);
              tcg_out_opc_upper(s, OPC_AUIPC, TCG_REG_TMP2, diff - imm12);
          } else {
              tcg_out_movi(s, TCG_TYPE_PTR, TCG_REG_TMP2, offset - imm12);
-            if (addr != TCG_REG_ZERO) {
-                tcg_out_opc_reg(s, OPC_ADD, TCG_REG_TMP2, TCG_REG_TMP2, addr);
+            if (*addr != TCG_REG_ZERO) {
+                tcg_out_opc_reg(s, OPC_ADD, TCG_REG_TMP2, TCG_REG_TMP2, *addr);
              }
          }
-        addr = TCG_REG_TMP2;
+        *addr = TCG_REG_TMP2;
+    }
+    return imm12;
+}
+
+static void split_offset_vector(TCGContext *s, TCGReg *addr, intptr_t offset)
+{
+    if (offset != 0) {
+        if (offset == sextreg(offset, 0, 12)) {
+            tcg_out_opc_imm(s, OPC_ADDI, TCG_REG_TMP0, *addr, offset);
+        } else {
+            tcg_out_movi(s, TCG_TYPE_PTR, TCG_REG_TMP0, offset);
+            tcg_out_opc_reg(s, OPC_ADD, TCG_REG_TMP0, TCG_REG_TMP0, *addr);
+        }
+        *addr = TCG_REG_TMP0;
      }
+}
+
+static void tcg_out_ldst(TCGContext *s, RISCVInsn opc, TCGReg data,
+                         TCGReg addr, intptr_t offset)
+{
+    intptr_t imm12;
 
      switch (opc) {
      case OPC_SB:
      case OPC_SH:
      case OPC_SW:
      case OPC_SD:
+        imm12 = split_offset_scalar(s, &addr, offset);
          tcg_out_opc_store(s, opc, addr, data, imm12);
          break;
      case OPC_LB:
@@ -845,8 +904,31 @@ static void tcg_out_ldst(TCGContext *s, RISCVInsn opc, TCGReg data,
      case OPC_LW:
      case OPC_LWU:
      case OPC_LD:
+        imm12 = split_offset_scalar(s, &addr, offset);
          tcg_out_opc_imm(s, opc, data, addr, imm12);
          break;
+    case OPC_VSE8_V:
+    case OPC_VSE16_V:
+    case OPC_VSE32_V:
+    case OPC_VSE64_V:
+    case OPC_VS1R_V:
+    case OPC_VS2R_V:
+    case OPC_VS4R_V:
+    case OPC_VS8R_V:
+        split_offset_vector(s, &addr, offset);
+        tcg_out_opc_ldst_vec(s, opc, data, addr, true);
+        break;
+    case OPC_VLE8_V:
+    case OPC_VLE16_V:
+    case OPC_VLE32_V:
+    case OPC_VLE64_V:

tcg/riscv/tcg-target.c.inc static void tcg_out_ldst(TCGContext *s, RISCVInsn opc, TCGReg data, · line 934

Quoted 10 lines
tcg/riscv/tcg-target.c.inc +7 -0
+    case OPC_VL1RE64_V:
+    case OPC_VL2RE64_V:
+    case OPC_VL4RE64_V:
+    case OPC_VL8RE64_V:
+        split_offset_vector(s, &addr, offset);
+        tcg_out_opc_ldst_vec(s, opc, data, addr, true);
+        break;
      default:
          g_assert_not_reached();
      }

This is more complicated than it needs to be, calling a combined function, then using a switch to separate, then calling separate functions. Calling separate functions in the first place is simpler. E.g.

static void tcg_out_vec_ldst(TCGContext *s, RISCVInsn opc, TCGReg data,
TCGReg addr, intptr_t offset)

{
    tcg_debug_assert(data >= TCG_REG_V0);
    tcg_debug_assert(addr < TCG_REG_V0);

    if (offset) {
        tcg_debug_assert(addr != TCG_REG_ZERO);
        if (offset == sextreg(offset, 0, 12)) {
            tcg_out_opc_imm(s, OPC_ADDI, TCG_REG_TMP0, addr, offset);
        } else {
            tcg_out_movi(s, TCG_TYPE_PTR, TCG_REG_TMP0, offset);
            tcg_out_opc_reg(s, OPC_ADD, TCG_REG_TMP0, TCG_REG_TMP0, addr);
        }
        addr = TCG_REG_TMP0;
    }

    tcg_out32(s, opc | ((data & 0x1f) << 7) | (addr << 15) | (1 << 25));
}
Quoted 36 lines

static void tcg_out_ld(TCGContext *s, TCGType type, TCGReg arg,
TCGReg arg1, intptr_t arg2)
{

tcg/riscv/tcg-target.c.inc +32 -1
-    RISCVInsn insn = type == TCG_TYPE_I32 ? OPC_LW : OPC_LD;
+    RISCVInsn insn;
+
+    if (type < TCG_TYPE_V64) {
+        insn = (type == TCG_TYPE_I32) ? OPC_LW : OPC_LD;
+    } else {
+        int nf = get_vec_type_bytes(type) / riscv_vlenb;
+
+        switch (nf) {
+        case 1:
+            insn = OPC_VL1RE64_V;
+            break;
+        case 2:
+            insn = OPC_VL2RE64_V;
+            break;
+        case 4:
+            insn = OPC_VL4RE64_V;
+            break;
+        case 8:
+            insn = OPC_VL8RE64_V;
+            break;
+        default:
+            {
+                int prev_vsew = riscv_set_vec_config_vl(s, type);
+
+                switch (prev_vsew) {
+                case MO_8:
+                    insn = OPC_VLE8_V;
+                    break;
+                case MO_16:
+                    insn = OPC_VLE16_V;
+                    break;
+                case MO_32:

tcg/riscv/tcg-target.c.inc static void tcg_out_ldst(TCGContext *s, RISCVInsn opc, TCGReg data, · line 981

Quoted 10 lines
tcg/riscv/tcg-target.c.inc +10 -0
+                    insn = OPC_VLE32_V;
+                    break;
+                case MO_64:
+                    insn = OPC_VLE64_V;
+                    break;
+                default:
+                    g_assert_not_reached();
+                }
+            }
+            break;

This can be simplified:

    switch (type) {
    case TCG_TYPE_I32:
        tcg_out_ldst(s, OPC_LW, data, base, offset);
        break;
    case TCG_TYPE_I64:
        tcg_out_ldst(s, OPC_LD, data, base, offset);
        break;
    case TCG_TYPE_V64:
    case TCG_TYPE_V128:
    case TCG_TYPE_V256:
        if (type >= riscv_lg2_vlenb) {
            static const RISCVInsn whole_reg_ld[] = {
                OPC_VL1RE64_V, OPC_VL2RE64_V, OPC_VL4RE64_V, OPC_VL8RE64_V
            };
            unsigned idx = type - riscv_lg2_vlenb;
            insn = whole_reg_ld[idx];
        } else {
            static const RISCVInsn unit_stride_ld[] = {
                OPC_VLE8_V, OPC_VLE16_V, OPC_VLE32_V, OPC_VLE64_V
            };
            MemOp prev_vsew = set_vtype_len(s, type);
            insn = unit_stride_ld[prev_vsew];
        }
        tcg_out_vec_ldst(s, insn, data, base, offset);
        break;
    default:
        g_assert_not_reached();
    }

and similar for store.

r~

author
#17 lore raw

Re: [PATCH v3 05/14] tcg/riscv: Implement vector load/store

Quoted 195 lines · Richard Henderson
Quoted 85 lines · LIU Zhiwei
tcg/riscv/tcg-target.c.inc +26 -4
@@ -811,31 +849,52 @@ static void tcg_out_extrl_i64_i32(TCGContext 
 *s, TCGReg ret, TCGReg arg)
       tcg_out_ext32s(s, ret, arg);
   }
   -static void tcg_out_ldst(TCGContext *s, RISCVInsn opc, TCGReg data,
-                         TCGReg addr, intptr_t offset)
+static intptr_t split_offset_scalar(TCGContext *s, TCGReg *addr,
+                                    intptr_t offset)
   {
       intptr_t imm12 = sextreg(offset, 0, 12);
         if (offset != imm12) {
           intptr_t diff = tcg_pcrel_diff(s, (void *)offset);
   -        if (addr == TCG_REG_ZERO && diff == (int32_t)diff) {
+        if (*addr == TCG_REG_ZERO && diff == (int32_t)diff) {
               imm12 = sextreg(diff, 0, 12);
               tcg_out_opc_upper(s, OPC_AUIPC, TCG_REG_TMP2, diff - imm12);
           } else {
               tcg_out_movi(s, TCG_TYPE_PTR, TCG_REG_TMP2, offset - imm12);
-            if (addr != TCG_REG_ZERO) {
-                tcg_out_opc_reg(s, OPC_ADD, TCG_REG_TMP2, 
 TCG_REG_TMP2, addr);
+            if (*addr != TCG_REG_ZERO) {
+                tcg_out_opc_reg(s, OPC_ADD, TCG_REG_TMP2, 
 TCG_REG_TMP2, *addr);
               }
           }
-        addr = TCG_REG_TMP2;
+        *addr = TCG_REG_TMP2;
+    }
+    return imm12;
+}
+
+static void split_offset_vector(TCGContext *s, TCGReg *addr, 
 intptr_t offset)
+{
+    if (offset != 0) {
+        if (offset == sextreg(offset, 0, 12)) {
+            tcg_out_opc_imm(s, OPC_ADDI, TCG_REG_TMP0, *addr, offset);
+        } else {
+            tcg_out_movi(s, TCG_TYPE_PTR, TCG_REG_TMP0, offset);
+            tcg_out_opc_reg(s, OPC_ADD, TCG_REG_TMP0, TCG_REG_TMP0, 
 *addr);
+        }
+        *addr = TCG_REG_TMP0;
       }
+}
+
+static void tcg_out_ldst(TCGContext *s, RISCVInsn opc, TCGReg data,
+                         TCGReg addr, intptr_t offset)
+{
+    intptr_t imm12;
         switch (opc) {

      case OPC_SB:
      case OPC_SH:
      case OPC_SW:
      case OPC_SD:
+        imm12 = split_offset_scalar(s, &addr, offset); tcg_out_opc_store(s, opc, addr, data, imm12); break;
      case OPC_LB:

(quoted diff) +0 -0
@@ -845,8 +904,31 @@ static void tcg_out_ldst(TCGContext *s, 
 RISCVInsn opc, TCGReg data,

      case OPC_LW:
      case OPC_LWU:
      case OPC_LD:
+        imm12 = split_offset_scalar(s, &addr, offset); tcg_out_opc_imm(s, opc, data, addr, imm12); break;
+    case OPC_VSE8_V:
+    case OPC_VSE16_V:
+    case OPC_VSE32_V:
+    case OPC_VSE64_V:
+    case OPC_VS1R_V:
+    case OPC_VS2R_V:
+    case OPC_VS4R_V:
+    case OPC_VS8R_V:
+        split_offset_vector(s, &addr, offset); +        tcg_out_opc_ldst_vec(s, opc, data, addr, true); +        break;
+    case OPC_VLE8_V:
+    case OPC_VLE16_V:
+    case OPC_VLE32_V:
+    case OPC_VLE64_V:
+    case OPC_VL1RE64_V:
+    case OPC_VL2RE64_V:

Quoted 8 lines

+    case OPC_VL4RE64_V:
+    case OPC_VL8RE64_V:
+        split_offset_vector(s, &addr, offset); +        tcg_out_opc_ldst_vec(s, opc, data, addr, true); +        break;
      default:
          g_assert_not_reached();
      }

This is more complicated than it needs to be, calling a combined function, then using a switch to separate, then calling separate functions.  Calling separate functions in the first place is simpler.  E.g.

static void tcg_out_vec_ldst(TCGContext *s, RISCVInsn opc, TCGReg data, TCGReg addr, intptr_t offset)
{
    tcg_debug_assert(data >= TCG_REG_V0); tcg_debug_assert(addr < TCG_REG_V0);

    if (offset) {
        tcg_debug_assert(addr != TCG_REG_ZERO);
        if (offset == sextreg(offset, 0, 12)) { tcg_out_opc_imm(s, OPC_ADDI, TCG_REG_TMP0, addr, offset); } else {
            tcg_out_movi(s, TCG_TYPE_PTR, TCG_REG_TMP0, offset); tcg_out_opc_reg(s, OPC_ADD, TCG_REG_TMP0, TCG_REG_TMP0, addr); }
        addr = TCG_REG_TMP0;
    }

    tcg_out32(s, opc | ((data & 0x1f) << 7) | (addr << 15) | (1 << 25)); }

Quoted 36 lines

  static void tcg_out_ld(TCGContext *s, TCGType type, TCGReg arg, TCGReg arg1, intptr_t arg2)
  {
-    RISCVInsn insn = type == TCG_TYPE_I32 ? OPC_LW : OPC_LD; +    RISCVInsn insn;

tcg/riscv/tcg-target.c.inc +31 -0
+
+    if (type < TCG_TYPE_V64) {
+        insn = (type == TCG_TYPE_I32) ? OPC_LW : OPC_LD;
+    } else {
+        int nf = get_vec_type_bytes(type) / riscv_vlenb;
+
+        switch (nf) {
+        case 1:
+            insn = OPC_VL1RE64_V;
+            break;
+        case 2:
+            insn = OPC_VL2RE64_V;
+            break;
+        case 4:
+            insn = OPC_VL4RE64_V;
+            break;
+        case 8:
+            insn = OPC_VL8RE64_V;
+            break;
+        default:
+            {
+                int prev_vsew = riscv_set_vec_config_vl(s, type);
+
+                switch (prev_vsew) {
+                case MO_8:
+                    insn = OPC_VLE8_V;
+                    break;
+                case MO_16:
+                    insn = OPC_VLE16_V;
+                    break;
+                case MO_32:

tcg/riscv/tcg-target.c.inc static void tcg_out_ldst(TCGContext *s, RISCVInsn opc, TCGReg data, · line 981

Quoted 10 lines
tcg/riscv/tcg-target.c.inc +10 -0
+                    insn = OPC_VLE32_V;
+                    break;
+                case MO_64:
+                    insn = OPC_VLE64_V;
+                    break;
+                default:
+                    g_assert_not_reached();
+                }
+            }
+            break;

This can be simplified:

    switch (type) {
    case TCG_TYPE_I32:
        tcg_out_ldst(s, OPC_LW, data, base, offset);
        break;
    case TCG_TYPE_I64:
        tcg_out_ldst(s, OPC_LD, data, base, offset);
        break;
    case TCG_TYPE_V64:
    case TCG_TYPE_V128:
    case TCG_TYPE_V256:
        if (type >= riscv_lg2_vlenb) {
            static const RISCVInsn whole_reg_ld[] = { OPC_VL1RE64_V, OPC_VL2RE64_V, OPC_VL4RE64_V, OPC_VL8RE64_V };
            unsigned idx = type - riscv_lg2_vlenb;
            insn = whole_reg_ld[idx];
        } else {
            static const RISCVInsn unit_stride_ld[] = { OPC_VLE8_V, OPC_VLE16_V, OPC_VLE32_V, OPC_VLE64_V };
            MemOp prev_vsew = set_vtype_len(s, type);
            insn = unit_stride_ld[prev_vsew];
        }
        tcg_out_vec_ldst(s, insn, data, base, offset);
        break;
    default:
        g_assert_not_reached();
    }

Quoted 1 line

and similar for store.

Great. We will take this way.

Zhiwei

Quoted 3 lines

r~

author
#18 lore raw

PATCH v3 6/14 tcg/riscv: Implement vector mov/dup{m/i}

In reply to LIU Zhiwei

From: TANG Tiancheng <[email protected]>

---
 tcg/riscv/tcg-target.c.inc | 53 ++++++++++++++++++++++++++++++++++++++
 1 file changed, 53 insertions(+)
tcg/riscv/tcg-target.c.inc +53 -0
index 4b1079fc6f..ddb0c8190c 100644
@@ -309,6 +309,12 @@ typedef enum {
     OPC_VS2R_V = 0x2000027 | V_UNIT_STRIDE_WHOLE_REG | V_NF(1),
     OPC_VS4R_V = 0x2000027 | V_UNIT_STRIDE_WHOLE_REG | V_NF(3),
     OPC_VS8R_V = 0x2000027 | V_UNIT_STRIDE_WHOLE_REG | V_NF(7),
+
+    OPC_VMV_V_V = 0x5e000057 | V_OPIVV,
+    OPC_VMV_V_I = 0x5e000057 | V_OPIVI,
+    OPC_VMV_V_X = 0x5e000057 | V_OPIVX,
+
+    OPC_VMVNR_V = 0x9e000057 | V_OPIVI,
 } RISCVInsn;
 
 /*
@@ -698,6 +704,21 @@ static bool tcg_out_mov(TCGContext *s, TCGType type, TCGReg ret, TCGReg arg)
     case TCG_TYPE_I64:
         tcg_out_opc_imm(s, OPC_ADDI, ret, arg, 0);
         break;
+    case TCG_TYPE_V64:
+    case TCG_TYPE_V128:
+    case TCG_TYPE_V256:
+        {
+            int nf = get_vec_type_bytes(type) / riscv_vlenb;
+
+            if (nf != 0) {
+                tcg_debug_assert(is_power_of_2(nf) && nf <= 8);
+                tcg_out_opc_vi(s, OPC_VMVNR_V, ret, arg, nf - 1, true);
+            } else {
+                riscv_set_vec_config_vl(s, type);
+                tcg_out_opc_vv(s, OPC_VMV_V_V, ret, TCG_REG_V0, arg, true);
+            }
+        }
+        break;
     default:
         g_assert_not_reached();
     }
@@ -1106,6 +1127,33 @@ static void tcg_out_addsub2(TCGContext *s,
     }
 }
 
+static bool tcg_out_dup_vec(TCGContext *s, TCGType type, unsigned vece,
+                                   TCGReg dst, TCGReg src)
+{
+    riscv_set_vec_config_vl_vece(s, type, vece);
+    tcg_out_opc_vx(s, OPC_VMV_V_X, dst, TCG_REG_V0, src, true);
+    return true;
+}
+
+static bool tcg_out_dupm_vec(TCGContext *s, TCGType type, unsigned vece,
+                                    TCGReg dst, TCGReg base, intptr_t offset)
+{
+    tcg_out_ld(s, TCG_TYPE_REG, TCG_REG_TMP0, base, offset);
+    return tcg_out_dup_vec(s, type, vece, dst, TCG_REG_TMP0);
+}
+
+static void tcg_out_dupi_vec(TCGContext *s, TCGType type, unsigned vece,
+                                    TCGReg dst, int64_t arg)
+{
+    if (arg < 16 && arg >= -16) {
+        riscv_set_vec_config_vl_vece(s, type, vece);
+        tcg_out_opc_vi(s, OPC_VMV_V_I, dst, TCG_REG_V0, arg, true);
+        return;
+    }
+    tcg_out_movi(s, TCG_TYPE_I64, TCG_REG_TMP0, arg);
+    tcg_out_dup_vec(s, type, vece, dst, TCG_REG_TMP0);
+}
+
 static const struct {
     RISCVInsn op;
     bool swap;
@@ -2234,6 +2282,9 @@ static void tcg_out_vec_op(TCGContext *s, TCGOpcode opc,
     a2 = args[2];
 
     switch (opc) {
+    case INDEX_op_dupm_vec:
+        tcg_out_dupm_vec(s, type, vece, a0, a1, a2);
+        break;
     case INDEX_op_ld_vec:
         tcg_out_ld(s, type, a0, a1, a2);
         break;
@@ -2405,6 +2456,8 @@ static TCGConstraintSetIndex tcg_target_op_def(TCGOpcode op)
 
     case INDEX_op_st_vec:
         return C_O0_I2(v, r);
+    case INDEX_op_dup_vec:
+    case INDEX_op_dupm_vec:
     case INDEX_op_ld_vec:
         return C_O1_I1(v, r);
     default:
Signature · 1 line
2.43.0
#19 lore raw

Re: [PATCH v3 06/14] tcg/riscv: Implement vector mov/dup{m/i}

Quoted 9 lines · LIU Zhiwei
tcg/riscv/tcg-target.c.inc +5 -0
@@ -698,6 +704,21 @@ static bool tcg_out_mov(TCGContext *s, TCGType type, TCGReg ret, TCGReg arg)
      case TCG_TYPE_I64:
          tcg_out_opc_imm(s, OPC_ADDI, ret, arg, 0);
          break;
+    case TCG_TYPE_V64:
+    case TCG_TYPE_V128:
+    case TCG_TYPE_V256:
+        {
+            int nf = get_vec_type_bytes(type) / riscv_vlenb;

tcg/riscv/tcg-target.c.inc static bool tcg_out_mov(TCGContext *s, TCGType type, TCGReg ret, TCGReg arg) · line 721

Quoted 10 lines
tcg/riscv/tcg-target.c.inc +10 -0
+
+            if (nf != 0) {
+                tcg_debug_assert(is_power_of_2(nf) && nf <= 8);
+                tcg_out_opc_vi(s, OPC_VMVNR_V, ret, arg, nf - 1, true);
+            } else {
+                riscv_set_vec_config_vl(s, type);
+                tcg_out_opc_vv(s, OPC_VMV_V_V, ret, TCG_REG_V0, arg, true);
+            }
+        }
+        break;

Perhaps

        int lmul = type - riscv_lg2_vlenb;
        int nf = 1 << MIN(lmul, 0);
        tcg_out_opc_vi(s, OPC_VMVNR_V, ret, arg, nf - 1);

Is there a reason to prefer vmv.v.v over vmvnr.v?
Seems like we can always move one vector reg...

tcg/riscv/tcg-target.c.inc static void tcg_out_addsub2(TCGContext *s, · line 1155

Quoted 11 lines
tcg/riscv/tcg-target.c.inc +11 -0
+static void tcg_out_dupi_vec(TCGContext *s, TCGType type, unsigned vece,
+                                    TCGReg dst, int64_t arg)
+{
+    if (arg < 16 && arg >= -16) {
+        riscv_set_vec_config_vl_vece(s, type, vece);
+        tcg_out_opc_vi(s, OPC_VMV_V_I, dst, TCG_REG_V0, arg, true);
+        return;
+    }
+    tcg_out_movi(s, TCG_TYPE_I64, TCG_REG_TMP0, arg);
+    tcg_out_dup_vec(s, type, vece, dst, TCG_REG_TMP0);
+}

I'll note that 0 and -1 do not require SEW change. I don't know how often that will come up, since in my testing with aarch64, we usually needed to swap to TCG_TYPE_V256 anyway.

r~

author
#20 lore raw

Re: [PATCH v3 06/14] tcg/riscv: Implement vector mov/dup{m/i}

Quoted 25 lines · Richard Henderson
Quoted 13 lines · LIU Zhiwei
(quoted diff) +0 -0
@@ -698,6 +704,21 @@ static bool tcg_out_mov(TCGContext *s, TCGType 
 type, TCGReg ret, TCGReg arg)

      case TCG_TYPE_I64:
          tcg_out_opc_imm(s, OPC_ADDI, ret, arg, 0); break;
+    case TCG_TYPE_V64:
+    case TCG_TYPE_V128:
+    case TCG_TYPE_V256:
+        {
+            int nf = get_vec_type_bytes(type) / riscv_vlenb; +
+            if (nf != 0) {
+                tcg_debug_assert(is_power_of_2(nf) && nf <= 8);

Quoted 8 lines

+                tcg_out_opc_vi(s, OPC_VMVNR_V, ret, arg, nf - 1, true); +            } else {
+                riscv_set_vec_config_vl(s, type);
+                tcg_out_opc_vv(s, OPC_VMV_V_V, ret, TCG_REG_V0, arg, true);
+            }
+        }
+        break;

Perhaps

Quoted 5 lines

        int lmul = type - riscv_lg2_vlenb;
        int nf = 1 << MIN(lmul, 0); tcg_out_opc_vi(s, OPC_VMVNR_V, ret, arg, nf - 1);

Is there a reason to prefer vmv.v.v over vmvnr.v?

I think it's a trade-off. For some CPUs,  instruction will be split internally. Thus the less the fraction lmul is, the less micro ops for execution. That's the benefit of using vmv.v.v. But here we also need a vsetivli. On some cpus, it can be fusion-ed to the next instruction.

Quoted 1 line

Seems like we can always move one vector reg...

OK. I will take this way.

Quoted 15 lines

tcg/riscv/tcg-target.c.inc static void tcg_out_addsub2(TCGContext *s, · line 1155

Quoted 13 lines

+static void tcg_out_dupi_vec(TCGContext *s, TCGType type, unsigned vece,
+                                    TCGReg dst, int64_t arg)

tcg/riscv/tcg-target.c.inc +9 -0
+{
+    if (arg < 16 && arg >= -16) {
+        riscv_set_vec_config_vl_vece(s, type, vece);
+        tcg_out_opc_vi(s, OPC_VMV_V_I, dst, TCG_REG_V0, arg, true);
+        return;
+    }
+    tcg_out_movi(s, TCG_TYPE_I64, TCG_REG_TMP0, arg);
+    tcg_out_dup_vec(s, type, vece, dst, TCG_REG_TMP0);
+}

I'll note that 0 and -1 do not require SEW change. I don't know how often that will come up

On our test on OpenCV, we get a rate of 99.7%. Thus we will optimize this next version.

Thanks,
Zhiwei

Quoted 4 lines

, since in my testing with aarch64, we usually needed to swap to TCG_TYPE_V256 anyway.

r~

author
#21 lore raw

PATCH v3 7/14 tcg/riscv: Add support for basic vector opcodes

In reply to LIU Zhiwei

From: TANG Tiancheng <[email protected]>

---
 tcg/riscv/tcg-target-con-set.h |  1 +
 tcg/riscv/tcg-target.c.inc     | 46 ++++++++++++++++++++++++++++++++++
 tcg/riscv/tcg-target.h         |  2 +-
 3 files changed, 48 insertions(+), 1 deletion(-)
tcg/riscv/tcg-target-con-set.h +1 -0
index d73a62b0f2..d4504122a2 100644
@@ -23,3 +23,4 @@ C_O1_I4(r, r, rI, rM, rM)
 C_O2_I4(r, r, rZ, rZ, rM, rM)
 C_O0_I2(v, r)
 C_O1_I1(v, r)
+C_O1_I1(v, v)
tcg/riscv/tcg-target.c.inc +46 -0

70 lines not on this page · raw message

tcg/riscv/tcg-target.h +1 -1
index 12a7a37aaa..acb8dfdf16 100644
@@ -151,7 +151,7 @@ typedef enum {
 #define TCG_TARGET_HAS_nand_vec         0
 #define TCG_TARGET_HAS_nor_vec          0
 #define TCG_TARGET_HAS_eqv_vec          0
-#define TCG_TARGET_HAS_not_vec          0
+#define TCG_TARGET_HAS_not_vec          1
 #define TCG_TARGET_HAS_neg_vec          0
 #define TCG_TARGET_HAS_abs_vec          0
 #define TCG_TARGET_HAS_roti_vec         0
Signature · 1 line
2.43.0
Reviewed-by on v3 7/14
#22 lore raw

Re: [PATCH v3 07/14] tcg/riscv: Add support for basic vector opcodes

Quoted 9 lines · LIU Zhiwei

From: TANG Tiancheng<[email protected]>

---
  tcg/riscv/tcg-target-con-set.h |  1 +
  tcg/riscv/tcg-target.c.inc     | 46 ++++++++++++++++++++++++++++++++++
  tcg/riscv/tcg-target.h         |  2 +-
  3 files changed, 48 insertions(+), 1 deletion(-)

r~

author
#23 lore raw

PATCH v3 8/14 tcg/riscv: Implement vector cmp ops

In reply to LIU Zhiwei

From: TANG Tiancheng <[email protected]>

1.Address immediate value constraints in RISC-V Vector Extension 1.0 for comparison instructions.

2.Extend comparison results from mask registers to SEW-width elements,
following recommendations in The RISC-V SPEC Volume I (Version 20240411).

This aligns with TCG's cmp_vec behavior by expanding compare results to full element width: all 1s for true, all 0s for false.

---
 tcg/riscv/tcg-target-con-set.h |   3 +
 tcg/riscv/tcg-target-con-str.h |   2 +
 tcg/riscv/tcg-target.c.inc     | 252 +++++++++++++++++++++++++++------
 tcg/riscv/tcg-target.h         |   2 +-
 tcg/tcg-internal.h             |   2 +
 tcg/tcg-op-vec.c               |   2 +-
 6 files changed, 214 insertions(+), 49 deletions(-)
tcg/riscv/tcg-target-con-set.h +3 -0
index d4504122a2..cc06102ccf 100644
@@ -24,3 +24,6 @@ C_O2_I4(r, r, rZ, rZ, rM, rM)
 C_O0_I2(v, r)
 C_O1_I1(v, r)
 C_O1_I1(v, v)
+C_O1_I2(v, v, v)
+C_O1_I2(v, v, vL)
+C_O1_I4(v, v, vL, vK, vK)
tcg/riscv/tcg-target-con-str.h +2 -0
index b2b3211bcb..089efe96ca 100644
@@ -17,6 +17,8 @@ REGS('v', ALL_VECTOR_REGS)
  */
 CONST('I', TCG_CT_CONST_S12)
 CONST('J', TCG_CT_CONST_J12)
+CONST('K', TCG_CT_CONST_S5)
+CONST('L', TCG_CT_CONST_CMP_VI)
 CONST('N', TCG_CT_CONST_N12)
 CONST('M', TCG_CT_CONST_M12)
 CONST('Z', TCG_CT_CONST_ZERO)
tcg/riscv/tcg-target.c.inc +205 -47

317 lines not on this page · raw message

tcg/riscv/tcg-target.h +1 -1
index acb8dfdf16..94034504b2 100644
@@ -164,7 +164,7 @@ typedef enum {
 #define TCG_TARGET_HAS_sat_vec          0
 #define TCG_TARGET_HAS_minmax_vec       0
 #define TCG_TARGET_HAS_bitsel_vec       0
-#define TCG_TARGET_HAS_cmpsel_vec       0
+#define TCG_TARGET_HAS_cmpsel_vec       1
 
 #define TCG_TARGET_HAS_tst_vec          0
 
tcg/tcg-internal.h +2 -0
index 9b0d982f65..512128e8a7 100644
@@ -102,5 +102,7 @@ void tcg_gen_op6(TCGOpcode, TCGArg, TCGArg, TCGArg, TCGArg, TCGArg, TCGArg);
 void vec_gen_2(TCGOpcode, TCGType, unsigned, TCGArg, TCGArg);
 void vec_gen_3(TCGOpcode, TCGType, unsigned, TCGArg, TCGArg, TCGArg);
 void vec_gen_4(TCGOpcode, TCGType, unsigned, TCGArg, TCGArg, TCGArg, TCGArg);
+void vec_gen_6(TCGOpcode opc, TCGType type, unsigned vece, TCGArg r,
+                    TCGArg a, TCGArg b, TCGArg c, TCGArg d, TCGArg e);
 
 #endif /* TCG_INTERNAL_H */
tcg/tcg-op-vec.c +1 -1
index 84af210bc0..851322878c 100644
@@ -172,7 +172,7 @@ void vec_gen_4(TCGOpcode opc, TCGType type, unsigned vece,
     op->args[3] = c;
 }
 
-static void vec_gen_6(TCGOpcode opc, TCGType type, unsigned vece, TCGArg r,
+void vec_gen_6(TCGOpcode opc, TCGType type, unsigned vece, TCGArg r,
                       TCGArg a, TCGArg b, TCGArg c, TCGArg d, TCGArg e)
 {
     TCGOp *op = tcg_emit_op(opc, 6);
Signature · 1 line
2.43.0
#24 lore raw

Re: [PATCH v3 08/14] tcg/riscv: Implement vector cmp ops

tcg/riscv/tcg-target.c.inc static void tcg_out_vec_op(TCGContext *s, TCGOpcode opc, · line 2417

Quoted 8 lines · LIU Zhiwei
tcg/riscv/tcg-target.c.inc +4 -0
@@ -2322,6 +2411,51 @@ static void tcg_out_vec_op(TCGContext *s, TCGOpcode opc,
          riscv_set_vec_config_vl(s, type);
          tcg_out_opc_vi(s, OPC_VXOR_VI, a0, a1, -1, true);
          break;
+    case INDEX_op_cmpsel_vec:
+        TCGArg a3, a4;
+        int c3, c4;
+        TCGCond cond;

While I suppose this compiles, it's not great to have new variables added randomly within a switch. At minimum, add { } around the block, but consider breaking out a separate tcg_out_cmpsel function, akin to tcg_out_movcond et al.

Quoted 16 lines
tcg/riscv/tcg-target.c.inc +11 -0
@@ -2332,10 +2466,27 @@ static void tcg_out_vec_op(TCGContext *s, TCGOpcode opc,
  void tcg_expand_vec_op(TCGOpcode opc, TCGType type, unsigned vece,
                         TCGArg a0, ...)
  {
+    va_list va;
+    TCGv_vec v0, v1;
+    TCGArg a2, a3;
+
+    va_start(va, a0);
+    v0 = temp_tcgv_vec(arg_temp(a0));
+    v1 = temp_tcgv_vec(arg_temp(va_arg(va, TCGArg)));
+    a2 = va_arg(va, TCGArg);
+
      switch (opc) {
+    case INDEX_op_cmp_vec:
+        a3 = va_arg(va, TCGArg);

tcg/riscv/tcg-target.c.inc static void tcg_out_vec_op(TCGContext *s, TCGOpcode opc, · line 2490

Quoted 10 lines
tcg/riscv/tcg-target.c.inc +6 -0
+        vec_gen_6(INDEX_op_cmpsel_vec, type, vece, tcgv_vec_arg(v0),
+                    tcgv_vec_arg(v1), a2,
+                    tcgv_i64_arg(tcg_constant_i64(-1)),
+                    tcgv_i64_arg(tcg_constant_i64(0)), a3);
+        break;
      default:
          g_assert_not_reached();
      }
+    va_end(va);
  }

Better to use "TCGArg a0, a1". Converting through arg_tmp + temp_tcgv_vec to v0/v1 and then undoing that with tcgv_vec_arg is confusing.

r~

author
#25 lore raw

Re: [PATCH v3 08/14] tcg/riscv: Implement vector cmp ops

Quoted 12 lines · Richard Henderson

tcg/riscv/tcg-target.c.inc static void tcg_out_vec_op(TCGContext *s, · line 2418

Quoted 9 lines · LIU Zhiwei
tcg/riscv/tcg-target.c.inc +4 -0
@@ -2322,6 +2411,51 @@ static void tcg_out_vec_op(TCGContext *s, 
 TCGOpcode opc,
           riscv_set_vec_config_vl(s, type);
           tcg_out_opc_vi(s, OPC_VXOR_VI, a0, a1, -1, true);
           break;
+    case INDEX_op_cmpsel_vec:
+        TCGArg a3, a4;
+        int c3, c4;
+        TCGCond cond;

While I suppose this compiles, it's not great to have new variables added randomly within a switch.  At minimum, add { } around the block, but consider breaking out a separate tcg_out_cmpsel function, akin to tcg_out_movcond et al.

OK.

Quoted 22 lines
Quoted 22 lines
tcg/riscv/tcg-target.c.inc +15 -0
@@ -2332,10 +2466,27 @@ static void tcg_out_vec_op(TCGContext *s, 
 TCGOpcode opc,
   void tcg_expand_vec_op(TCGOpcode opc, TCGType type, unsigned vece,
                          TCGArg a0, ...)
   {
+    va_list va;
+    TCGv_vec v0, v1;
+    TCGArg a2, a3;
+
+    va_start(va, a0);
+    v0 = temp_tcgv_vec(arg_temp(a0));
+    v1 = temp_tcgv_vec(arg_temp(va_arg(va, TCGArg)));
+    a2 = va_arg(va, TCGArg);
+
       switch (opc) {
+    case INDEX_op_cmp_vec:
+        a3 = va_arg(va, TCGArg);
+        vec_gen_6(INDEX_op_cmpsel_vec, type, vece, tcgv_vec_arg(v0),
+                    tcgv_vec_arg(v1), a2,
+                    tcgv_i64_arg(tcg_constant_i64(-1)),
+                    tcgv_i64_arg(tcg_constant_i64(0)), a3);
Quoted 8 lines
Quoted 6 lines
(quoted diff) +1 -0
+        break;

      default:
          g_assert_not_reached();
      }
+    va_end(va);
  }

Better to use "TCGArg a0, a1".  Converting through arg_tmp + temp_tcgv_vec to v0/v1 and then undoing that with tcgv_vec_arg is confusing.

OK.

Thanks,
Zhiwei

Quoted 3 lines

r~

author
#26 lore raw

PATCH v3 9/14 tcg/riscv: Implement vector neg ops

In reply to LIU Zhiwei

From: TANG Tiancheng <[email protected]>

---
 tcg/riscv/tcg-target.c.inc | 7 +++++++
 tcg/riscv/tcg-target.h     | 2 +-
 2 files changed, 8 insertions(+), 1 deletion(-)
tcg/riscv/tcg-target.c.inc +7 -0
index 37909e56fb..60a5a21ff5 100644
@@ -280,6 +280,7 @@ typedef enum {
     OPC_VXOR_VV = 0x2c000057 | V_OPIVV,
     OPC_VXOR_VI = 0x2c000057 | V_OPIVI,
 
+    OPC_VRSUB_VI = 0xc000057 | V_OPIVI,
     OPC_VMSEQ_VV = 0x60000057 | V_OPIVV,
     OPC_VMSEQ_VI = 0x60000057 | V_OPIVI,
     OPC_VMSEQ_VX = 0x60000057 | V_OPIVX,
@@ -2411,6 +2412,10 @@ static void tcg_out_vec_op(TCGContext *s, TCGOpcode opc,
         riscv_set_vec_config_vl(s, type);
         tcg_out_opc_vi(s, OPC_VXOR_VI, a0, a1, -1, true);
         break;
+    case INDEX_op_neg_vec:
+        riscv_set_vec_config_vl_vece(s, type, vece);
+        tcg_out_opc_vi(s, OPC_VRSUB_VI, a0, a1, 0, true);
+        break;
     case INDEX_op_cmpsel_vec:
         TCGArg a3, a4;
         int c3, c4;
@@ -2498,6 +2503,7 @@ int tcg_can_emit_vec_op(TCGOpcode opc, TCGType type, unsigned vece)
     case INDEX_op_or_vec:
     case INDEX_op_xor_vec:
     case INDEX_op_not_vec:
+    case INDEX_op_neg_vec:
     case INDEX_op_cmpsel_vec:
         return 1;
     case INDEX_op_cmp_vec:
@@ -2652,6 +2658,7 @@ static TCGConstraintSetIndex tcg_target_op_def(TCGOpcode op)
     case INDEX_op_dupm_vec:
     case INDEX_op_ld_vec:
         return C_O1_I1(v, r);
+    case INDEX_op_neg_vec:
     case INDEX_op_not_vec:
         return C_O1_I1(v, v);
     case INDEX_op_add_vec:
tcg/riscv/tcg-target.h +1 -1
index 94034504b2..ae10381e02 100644
@@ -152,7 +152,7 @@ typedef enum {
 #define TCG_TARGET_HAS_nor_vec          0
 #define TCG_TARGET_HAS_eqv_vec          0
 #define TCG_TARGET_HAS_not_vec          1
-#define TCG_TARGET_HAS_neg_vec          0
+#define TCG_TARGET_HAS_neg_vec          1
 #define TCG_TARGET_HAS_abs_vec          0
 #define TCG_TARGET_HAS_roti_vec         0
 #define TCG_TARGET_HAS_rots_vec         0
Signature · 1 line
2.43.0
author
#27 lore raw

PATCH v3 10/14 tcg/riscv: Implement vector sat/mul ops

In reply to LIU Zhiwei

From: TANG Tiancheng <[email protected]>

---
 tcg/riscv/tcg-target.c.inc | 36 ++++++++++++++++++++++++++++++++++++
 tcg/riscv/tcg-target.h     |  4 ++--
 2 files changed, 38 insertions(+), 2 deletions(-)
tcg/riscv/tcg-target.c.inc +36 -0
index 60a5a21ff5..c31f92731c 100644
@@ -281,6 +281,12 @@ typedef enum {
     OPC_VXOR_VI = 0x2c000057 | V_OPIVI,
 
     OPC_VRSUB_VI = 0xc000057 | V_OPIVI,
+    OPC_VMUL_VV = 0x94000057 | V_OPMVV,
+    OPC_VSADD_VV = 0x84000057 | V_OPIVV,
+    OPC_VSSUB_VV = 0x8c000057 | V_OPIVV,
+    OPC_VSADDU_VV = 0x80000057 | V_OPIVV,
+    OPC_VSSUBU_VV = 0x88000057 | V_OPIVV,
+
     OPC_VMSEQ_VV = 0x60000057 | V_OPIVV,
     OPC_VMSEQ_VI = 0x60000057 | V_OPIVI,
     OPC_VMSEQ_VX = 0x60000057 | V_OPIVX,
@@ -2416,6 +2422,26 @@ static void tcg_out_vec_op(TCGContext *s, TCGOpcode opc,
         riscv_set_vec_config_vl_vece(s, type, vece);
         tcg_out_opc_vi(s, OPC_VRSUB_VI, a0, a1, 0, true);
         break;
+    case INDEX_op_mul_vec:
+        riscv_set_vec_config_vl_vece(s, type, vece);
+        tcg_out_opc_vv(s, OPC_VMUL_VV, a0, a1, a2, true);
+        break;
+    case INDEX_op_ssadd_vec:
+        riscv_set_vec_config_vl_vece(s, type, vece);
+        tcg_out_opc_vv(s, OPC_VSADD_VV, a0, a1, a2, true);
+        break;
+    case INDEX_op_sssub_vec:
+        riscv_set_vec_config_vl_vece(s, type, vece);
+        tcg_out_opc_vv(s, OPC_VSSUB_VV, a0, a1, a2, true);
+        break;
+    case INDEX_op_usadd_vec:
+        riscv_set_vec_config_vl_vece(s, type, vece);
+        tcg_out_opc_vv(s, OPC_VSADDU_VV, a0, a1, a2, true);
+        break;
+    case INDEX_op_ussub_vec:
+        riscv_set_vec_config_vl_vece(s, type, vece);
+        tcg_out_opc_vv(s, OPC_VSSUBU_VV, a0, a1, a2, true);
+        break;
     case INDEX_op_cmpsel_vec:
         TCGArg a3, a4;
         int c3, c4;
@@ -2504,6 +2530,11 @@ int tcg_can_emit_vec_op(TCGOpcode opc, TCGType type, unsigned vece)
     case INDEX_op_xor_vec:
     case INDEX_op_not_vec:
     case INDEX_op_neg_vec:
+    case INDEX_op_mul_vec:
+    case INDEX_op_ssadd_vec:
+    case INDEX_op_sssub_vec:
+    case INDEX_op_usadd_vec:
+    case INDEX_op_ussub_vec:
     case INDEX_op_cmpsel_vec:
         return 1;
     case INDEX_op_cmp_vec:
@@ -2666,6 +2697,11 @@ static TCGConstraintSetIndex tcg_target_op_def(TCGOpcode op)
     case INDEX_op_and_vec:
     case INDEX_op_or_vec:
     case INDEX_op_xor_vec:
+    case INDEX_op_mul_vec:
+    case INDEX_op_ssadd_vec:
+    case INDEX_op_sssub_vec:
+    case INDEX_op_usadd_vec:
+    case INDEX_op_ussub_vec:
         return C_O1_I2(v, v, v);
     case INDEX_op_cmp_vec:
         return C_O1_I2(v, v, vL);
tcg/riscv/tcg-target.h +2 -2
index ae10381e02..1d4d8878ce 100644
@@ -160,8 +160,8 @@ typedef enum {
 #define TCG_TARGET_HAS_shi_vec          0
 #define TCG_TARGET_HAS_shs_vec          0
 #define TCG_TARGET_HAS_shv_vec          0
-#define TCG_TARGET_HAS_mul_vec          0
-#define TCG_TARGET_HAS_sat_vec          0
+#define TCG_TARGET_HAS_mul_vec          1
+#define TCG_TARGET_HAS_sat_vec          1
 #define TCG_TARGET_HAS_minmax_vec       0
 #define TCG_TARGET_HAS_bitsel_vec       0
 #define TCG_TARGET_HAS_cmpsel_vec       1
Signature · 1 line
2.43.0
author
#28 lore raw

PATCH v3 11/14 tcg/riscv: Implement vector min/max ops

In reply to LIU Zhiwei

From: TANG Tiancheng <[email protected]>

---
 tcg/riscv/tcg-target.c.inc | 29 +++++++++++++++++++++++++++++
 tcg/riscv/tcg-target.h     |  2 +-
 2 files changed, 30 insertions(+), 1 deletion(-)
tcg/riscv/tcg-target.c.inc +29 -0
index c31f92731c..507f659fd6 100644
@@ -287,6 +287,11 @@ typedef enum {
     OPC_VSADDU_VV = 0x80000057 | V_OPIVV,
     OPC_VSSUBU_VV = 0x88000057 | V_OPIVV,
 
+    OPC_VMAX_VV = 0x1c000057 | V_OPIVV,
+    OPC_VMAXU_VV = 0x18000057 | V_OPIVV,
+    OPC_VMIN_VV = 0x14000057 | V_OPIVV,
+    OPC_VMINU_VV = 0x10000057 | V_OPIVV,
+
     OPC_VMSEQ_VV = 0x60000057 | V_OPIVV,
     OPC_VMSEQ_VI = 0x60000057 | V_OPIVI,
     OPC_VMSEQ_VX = 0x60000057 | V_OPIVX,
@@ -2442,6 +2447,22 @@ static void tcg_out_vec_op(TCGContext *s, TCGOpcode opc,
         riscv_set_vec_config_vl_vece(s, type, vece);
         tcg_out_opc_vv(s, OPC_VSSUBU_VV, a0, a1, a2, true);
         break;
+    case INDEX_op_smax_vec:
+        riscv_set_vec_config_vl_vece(s, type, vece);
+        tcg_out_opc_vv(s, OPC_VMAX_VV, a0, a1, a2, true);
+        break;
+    case INDEX_op_smin_vec:
+        riscv_set_vec_config_vl_vece(s, type, vece);
+        tcg_out_opc_vv(s, OPC_VMIN_VV, a0, a1, a2, true);
+        break;
+    case INDEX_op_umax_vec:
+        riscv_set_vec_config_vl_vece(s, type, vece);
+        tcg_out_opc_vv(s, OPC_VMAXU_VV, a0, a1, a2, true);
+        break;
+    case INDEX_op_umin_vec:
+        riscv_set_vec_config_vl_vece(s, type, vece);
+        tcg_out_opc_vv(s, OPC_VMINU_VV, a0, a1, a2, true);
+        break;
     case INDEX_op_cmpsel_vec:
         TCGArg a3, a4;
         int c3, c4;
@@ -2535,6 +2556,10 @@ int tcg_can_emit_vec_op(TCGOpcode opc, TCGType type, unsigned vece)
     case INDEX_op_sssub_vec:
     case INDEX_op_usadd_vec:
     case INDEX_op_ussub_vec:
+    case INDEX_op_smax_vec:
+    case INDEX_op_smin_vec:
+    case INDEX_op_umax_vec:
+    case INDEX_op_umin_vec:
     case INDEX_op_cmpsel_vec:
         return 1;
     case INDEX_op_cmp_vec:
@@ -2702,6 +2727,10 @@ static TCGConstraintSetIndex tcg_target_op_def(TCGOpcode op)
     case INDEX_op_sssub_vec:
     case INDEX_op_usadd_vec:
     case INDEX_op_ussub_vec:
+    case INDEX_op_smax_vec:
+    case INDEX_op_smin_vec:
+    case INDEX_op_umax_vec:
+    case INDEX_op_umin_vec:
         return C_O1_I2(v, v, v);
     case INDEX_op_cmp_vec:
         return C_O1_I2(v, v, vL);
tcg/riscv/tcg-target.h +1 -1
index 1d4d8878ce..7005099810 100644
@@ -162,7 +162,7 @@ typedef enum {
 #define TCG_TARGET_HAS_shv_vec          0
 #define TCG_TARGET_HAS_mul_vec          1
 #define TCG_TARGET_HAS_sat_vec          1
-#define TCG_TARGET_HAS_minmax_vec       0
+#define TCG_TARGET_HAS_minmax_vec       1
 #define TCG_TARGET_HAS_bitsel_vec       0
 #define TCG_TARGET_HAS_cmpsel_vec       1
 
Signature · 1 line
2.43.0
author
#29 lore raw

PATCH v3 12/14 tcg/riscv: Implement vector shs/v ops

In reply to LIU Zhiwei

From: TANG Tiancheng <[email protected]>

---
 tcg/riscv/tcg-target.c.inc | 44 ++++++++++++++++++++++++++++++++++++++
 tcg/riscv/tcg-target.h     |  4 ++--
 2 files changed, 46 insertions(+), 2 deletions(-)
tcg/riscv/tcg-target.c.inc +44 -0
index 507f659fd6..2dc6befe09 100644
@@ -315,6 +315,13 @@ typedef enum {
     OPC_VMSGT_VI = 0x7c000057 | V_OPIVI,
     OPC_VMSGT_VX = 0x7c000057 | V_OPIVX,
 
+    OPC_VSLL_VV = 0x94000057 | V_OPIVV,
+    OPC_VSLL_VX = 0x94000057 | V_OPIVX,
+    OPC_VSRL_VV = 0xa0000057 | V_OPIVV,
+    OPC_VSRL_VX = 0xa0000057 | V_OPIVX,
+    OPC_VSRA_VV = 0xa4000057 | V_OPIVV,
+    OPC_VSRA_VX = 0xa4000057 | V_OPIVX,
+
     OPC_VMV_V_V = 0x5e000057 | V_OPIVV,
     OPC_VMV_V_I = 0x5e000057 | V_OPIVI,
     OPC_VMV_V_X = 0x5e000057 | V_OPIVX,
@@ -2463,6 +2470,30 @@ static void tcg_out_vec_op(TCGContext *s, TCGOpcode opc,
         riscv_set_vec_config_vl_vece(s, type, vece);
         tcg_out_opc_vv(s, OPC_VMINU_VV, a0, a1, a2, true);
         break;
+    case INDEX_op_shls_vec:
+        riscv_set_vec_config_vl_vece(s, type, vece);
+        tcg_out_opc_vx(s, OPC_VSLL_VX, a0, a1, a2, true);
+        break;
+    case INDEX_op_shrs_vec:
+        riscv_set_vec_config_vl_vece(s, type, vece);
+        tcg_out_opc_vx(s, OPC_VSRL_VX, a0, a1, a2, true);
+        break;
+    case INDEX_op_sars_vec:
+        riscv_set_vec_config_vl_vece(s, type, vece);
+        tcg_out_opc_vx(s, OPC_VSRA_VX, a0, a1, a2, true);
+        break;
+    case INDEX_op_shlv_vec:
+        riscv_set_vec_config_vl_vece(s, type, vece);
+        tcg_out_opc_vv(s, OPC_VSLL_VV, a0, a1, a2, true);
+        break;
+    case INDEX_op_shrv_vec:
+        riscv_set_vec_config_vl_vece(s, type, vece);
+        tcg_out_opc_vv(s, OPC_VSRL_VV, a0, a1, a2, true);
+        break;
+    case INDEX_op_sarv_vec:
+        riscv_set_vec_config_vl_vece(s, type, vece);
+        tcg_out_opc_vv(s, OPC_VSRA_VV, a0, a1, a2, true);
+        break;
     case INDEX_op_cmpsel_vec:
         TCGArg a3, a4;
         int c3, c4;
@@ -2560,6 +2591,12 @@ int tcg_can_emit_vec_op(TCGOpcode opc, TCGType type, unsigned vece)
     case INDEX_op_smin_vec:
     case INDEX_op_umax_vec:
     case INDEX_op_umin_vec:
+    case INDEX_op_shls_vec:
+    case INDEX_op_shrs_vec:
+    case INDEX_op_sars_vec:
+    case INDEX_op_shlv_vec:
+    case INDEX_op_shrv_vec:
+    case INDEX_op_sarv_vec:
     case INDEX_op_cmpsel_vec:
         return 1;
     case INDEX_op_cmp_vec:
@@ -2731,7 +2768,14 @@ static TCGConstraintSetIndex tcg_target_op_def(TCGOpcode op)
     case INDEX_op_smin_vec:
     case INDEX_op_umax_vec:
     case INDEX_op_umin_vec:
+    case INDEX_op_shlv_vec:
+    case INDEX_op_shrv_vec:
+    case INDEX_op_sarv_vec:
         return C_O1_I2(v, v, v);
+    case INDEX_op_shls_vec:
+    case INDEX_op_shrs_vec:
+    case INDEX_op_sars_vec:
+        return C_O1_I2(v, v, r);
     case INDEX_op_cmp_vec:
         return C_O1_I2(v, v, vL);
     case INDEX_op_cmpsel_vec:
tcg/riscv/tcg-target.h +2 -2
index 7005099810..3bd8b811ef 100644
@@ -158,8 +158,8 @@ typedef enum {
 #define TCG_TARGET_HAS_rots_vec         0
 #define TCG_TARGET_HAS_rotv_vec         0
 #define TCG_TARGET_HAS_shi_vec          0
-#define TCG_TARGET_HAS_shs_vec          0
-#define TCG_TARGET_HAS_shv_vec          0
+#define TCG_TARGET_HAS_shs_vec          1
+#define TCG_TARGET_HAS_shv_vec          1
 #define TCG_TARGET_HAS_mul_vec          1
 #define TCG_TARGET_HAS_sat_vec          1
 #define TCG_TARGET_HAS_minmax_vec       1
Signature · 1 line
2.43.0
author
#30 lore raw

PATCH v3 13/14 tcg/riscv: Implement vector roti/v/x shi ops

In reply to LIU Zhiwei

From: TANG Tiancheng <[email protected]>

---
 tcg/riscv/tcg-target-con-set.h |  1 +
 tcg/riscv/tcg-target.c.inc     | 86 +++++++++++++++++++++++++++++++++-
 tcg/riscv/tcg-target.h         |  8 ++--
 3 files changed, 90 insertions(+), 5 deletions(-)
tcg/riscv/tcg-target-con-set.h +1 -0
index cc06102ccf..53649f750c 100644
@@ -25,5 +25,6 @@ C_O0_I2(v, r)
 C_O1_I1(v, r)
 C_O1_I1(v, v)
 C_O1_I2(v, v, v)
+C_O1_I2(v, v, r)
 C_O1_I2(v, v, vL)
 C_O1_I4(v, v, vL, vK, vK)
tcg/riscv/tcg-target.c.inc +85 -1

138 lines not on this page · raw message

tcg/riscv/tcg-target.h +4 -4
index 3bd8b811ef..082942d858 100644
@@ -154,10 +154,10 @@ typedef enum {
 #define TCG_TARGET_HAS_not_vec          1
 #define TCG_TARGET_HAS_neg_vec          1
 #define TCG_TARGET_HAS_abs_vec          0
-#define TCG_TARGET_HAS_roti_vec         0
-#define TCG_TARGET_HAS_rots_vec         0
-#define TCG_TARGET_HAS_rotv_vec         0
-#define TCG_TARGET_HAS_shi_vec          0
+#define TCG_TARGET_HAS_roti_vec         -1
+#define TCG_TARGET_HAS_rots_vec         -1
+#define TCG_TARGET_HAS_rotv_vec         -1
+#define TCG_TARGET_HAS_shi_vec          1
 #define TCG_TARGET_HAS_shs_vec          1
 #define TCG_TARGET_HAS_shv_vec          1
 #define TCG_TARGET_HAS_mul_vec          1
Signature · 1 line
2.43.0
author
#31 lore raw

PATCH v3 14/14 tcg/riscv: Enable native vector support for TCG host

In reply to LIU Zhiwei

From: TANG Tiancheng <[email protected]>

---
 tcg/riscv/tcg-target.h | 8 +++++---
 1 file changed, 5 insertions(+), 3 deletions(-)
tcg/riscv/tcg-target.h +5 -3
index 082942d858..099b7aa705 100644
@@ -143,9 +143,11 @@ typedef enum {
 #define TCG_TARGET_HAS_tst              0
 
 /* vector instructions */
-#define TCG_TARGET_HAS_v64              0
-#define TCG_TARGET_HAS_v128             0
-#define TCG_TARGET_HAS_v256             0
+#define have_rvv    (cpuinfo & CPUINFO_ZVE64X)
+
+#define TCG_TARGET_HAS_v64              have_rvv
+#define TCG_TARGET_HAS_v128             have_rvv
+#define TCG_TARGET_HAS_v256             have_rvv
 #define TCG_TARGET_HAS_andc_vec         0
 #define TCG_TARGET_HAS_orc_vec          0
 #define TCG_TARGET_HAS_nand_vec         0
Signature · 1 line
2.43.0
#32 lore raw
In reply to LIU Zhiwei
Quoted 2 lines · LIU Zhiwei

From: TANG Tiancheng <[email protected]>

Can you please mention RISC-V in the cover letter title. Otherwise it's not obvious that this is RISC-V specific

Alistair

Quoted 68 lines

This patch set introduces support for the RISC-V vector extension in TCG backend for RISC-V targets.

v3:
1. Use the .insn form in cpuinfo probing.

2. Use reserved_regs to constrain the register group index instead of using constrain.

3. Avoid using macros to implement functions whenever possible.

4. Rename vtypei to vtype.

5. Move the __thread prev_vtype variable to TCGContext.

6. Support fractional LMUL setting, but since MF2 has a minimum ELEN of 32, restrict fractional LMUL to cases where SEW < 64.

7. Handle vector load/store imm12 split in a different function.

8. Remove compare vx and implement INDEX_op_cmpsel_vec for INDEX_op_cmp_vec in a more concise way.

9. Move the implementation of shi_vec from tcg_expand_vec_op to tcg_out_vec_op.

10. Address some formatting issues.

v2:
https://lists.gnu.org/archive/html/qemu-riscv/2024-08/msg00679.html

v1:
https://lists.gnu.org/archive/html/qemu-riscv/2024-08/msg00205.html

Swung0x48 (1):
tcg/riscv: Add basic support for vector

TANG Tiancheng (13):
tcg/op-gvec: Fix iteration step in 32-bit operation
util: Add RISC-V vector extension probe in cpuinfo tcg/riscv: Add riscv vset{i}vli support tcg/riscv: Implement vector load/store tcg/riscv: Implement vector mov/dup{m/i} tcg/riscv: Add support for basic vector opcodes tcg/riscv: Implement vector cmp ops tcg/riscv: Implement vector neg ops tcg/riscv: Implement vector sat/mul ops tcg/riscv: Implement vector min/max ops tcg/riscv: Implement vector shs/v ops tcg/riscv: Implement vector roti/v/x shi ops tcg/riscv: Enable native vector support for TCG host

 host/include/riscv/host/cpuinfo.h |    3 +
 include/tcg/tcg.h                 |    3 +
 tcg/riscv/tcg-target-con-set.h    |    7 +
 tcg/riscv/tcg-target-con-str.h    |    3 +
 tcg/riscv/tcg-target.c.inc        | 1047 ++++++++++++++++++++++++++---
 tcg/riscv/tcg-target.h            |   80 ++-
 tcg/riscv/tcg-target.opc.h        |   12 +
 tcg/tcg-internal.h                |    2 +
 tcg/tcg-op-gvec.c                 |    2 +-
 tcg/tcg-op-vec.c                  |    2 +-
 util/cpuinfo-riscv.c              |   26 +-
 11 files changed, 1062 insertions(+), 125 deletions(-)
 create mode 100644 tcg/riscv/tcg-target.opc.h

--
2.43.0

author
#33 lore raw
Quoted 4 lines · Alistair Francis
Quoted 1 line · LIU Zhiwei

From: TANG Tiancheng <[email protected]>

Can you please mention RISC-V in the cover letter title. Otherwise it's not obvious that this is RISC-V specific

Sorry. I miss it.

Thanks,
Zhiwei

Quoted 70 lines

Alistair

Quoted 67 lines

This patch set introduces support for the RISC-V vector extension in TCG backend for RISC-V targets.

v3:
1. Use the .insn form in cpuinfo probing.

2. Use reserved_regs to constrain the register group index instead of using constrain.

3. Avoid using macros to implement functions whenever possible.

4. Rename vtypei to vtype.

5. Move the __thread prev_vtype variable to TCGContext.

6. Support fractional LMUL setting, but since MF2 has a minimum ELEN of 32, restrict fractional LMUL to cases where SEW < 64.

7. Handle vector load/store imm12 split in a different function.

8. Remove compare vx and implement INDEX_op_cmpsel_vec for INDEX_op_cmp_vec in a more concise way.

9. Move the implementation of shi_vec from tcg_expand_vec_op to tcg_out_vec_op.

10. Address some formatting issues.

v2:

   https://lists.gnu.org/archive/html/qemu-riscv/2024-08/msg00679.html

v1:

   https://lists.gnu.org/archive/html/qemu-riscv/2024-08/msg00205.html

Swung0x48 (1):
tcg/riscv: Add basic support for vector

TANG Tiancheng (13):
tcg/op-gvec: Fix iteration step in 32-bit operation
util: Add RISC-V vector extension probe in cpuinfo tcg/riscv: Add riscv vset{i}vli support tcg/riscv: Implement vector load/store tcg/riscv: Implement vector mov/dup{m/i} tcg/riscv: Add support for basic vector opcodes tcg/riscv: Implement vector cmp ops tcg/riscv: Implement vector neg ops tcg/riscv: Implement vector sat/mul ops tcg/riscv: Implement vector min/max ops tcg/riscv: Implement vector shs/v ops tcg/riscv: Implement vector roti/v/x shi ops tcg/riscv: Enable native vector support for TCG host

  host/include/riscv/host/cpuinfo.h |    3 +
  include/tcg/tcg.h                 |    3 +
  tcg/riscv/tcg-target-con-set.h    |    7 +
  tcg/riscv/tcg-target-con-str.h    |    3 +
  tcg/riscv/tcg-target.c.inc        | 1047 ++++++++++++++++++++++++++---
  tcg/riscv/tcg-target.h            |   80 ++-
  tcg/riscv/tcg-target.opc.h        |   12 +
  tcg/tcg-internal.h                |    2 +
  tcg/tcg-op-gvec.c                 |    2 +-
  tcg/tcg-op-vec.c                  |    2 +-
  util/cpuinfo-riscv.c              |   26 +-
  11 files changed, 1062 insertions(+), 125 deletions(-)

create mode 100644 tcg/riscv/tcg-target.opc.h

--
2.43.0