Skip to contentSkip to conversation

tcg/riscv: Add support for vector

PATCH In review LIU Zhiwei 2 participants 18 messages Oct 7, 2024 – Oct 9, 2024 RISC-V

lore
Share & tools
lore

LIU Zhiwei

author
#0 lore raw

v5 cover tcg/riscv: 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.

v5:
1. Improve the signature of co-authors

2. Add probe for fractional LMUL setting.

3. Fix bug of INDEX_op_rotlv_vec.

4. Modify the author's alias swung0x48 to his real name Huang Shiyuan
and add signed-off tag.

5. Add vtype initialization in tcg_out_call_int() and prepare_host_addr().

v4:
https://lists.gnu.org/archive/html/qemu-riscv/2024-09/msg00284.html

v3:
https://lists.gnu.org/archive/html/qemu-riscv/2024-09/msg00060.html

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

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

TANG Tiancheng (11):
util: Add RISC-V vector extension probe in cpuinfo tcg/riscv: Add vset{i}vli and ld/st vec ops tcg/riscv: Implement vector mov/dup{m/i} tcg/riscv: Add support for basic vector opcodes tcg/riscv: Implement vector cmp/cmpsel 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 shi/s/v ops tcg/riscv: Implement vector roti/v/x ops tcg/riscv: Enable native vector support for TCG host

 host/include/riscv/host/cpuinfo.h |    2 +
 include/tcg/tcg.h                 |    7 +
 tcg/riscv/tcg-target-con-set.h    |    7 +
 tcg/riscv/tcg-target-con-str.h    |    3 +
 tcg/riscv/tcg-target.c.inc        | 1016 ++++++++++++++++++++++++++---
 tcg/riscv/tcg-target.h            |   80 ++-
 tcg/riscv/tcg-target.opc.h        |   12 +
 util/cpuinfo-riscv.c              |   24 +-
 8 files changed, 1031 insertions(+), 120 deletions(-)
 create mode 100644 tcg/riscv/tcg-target.opc.h
Signature · 1 line
2.43.0
author
#1 lore raw

PATCH v5 1/12 util: Add RISC-V vector extension probe in cpuinfo

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.

Cache lg2(vlenb) for the backend. The storing of lg2(vlenb) means we can convert all of the division into subtraction.

While the compiler doesn't support RISCV_HWPROBE_EXT_ZVE64X, we use RISCV_HWPROBE_IMA_V instead. RISCV_HWPROBE_IMA_V is more strictly constrainted than RISCV_HWPROBE_EXT_ZVE64X. At least in current QEMU implemenation, the V vector extension depends on the zve64d extension.

---
 host/include/riscv/host/cpuinfo.h |  2 ++
 util/cpuinfo-riscv.c              | 24 ++++++++++++++++++++++--
 2 files changed, 24 insertions(+), 2 deletions(-)
host/include/riscv/host/cpuinfo.h +2 -0
index 2b00660e36..cdc784e7b6 100644
@@ -10,9 +10,11 @@
 #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_lg2_vlenb;
 
 /*
  * We cannot rely on constructor ordering, so other constructors must
util/cpuinfo-riscv.c +22 -2
index 497ce12680..bab782745b 100644
@@ -4,6 +4,7 @@
  */
 
 #include "qemu/osdep.h"
+#include "qemu/host-utils.h"
 #include "host/cpuinfo.h"
 
 #ifdef CONFIG_ASM_HWPROBE_H
@@ -12,6 +13,7 @@
 #endif
 
 unsigned cpuinfo;
+unsigned riscv_lg2_vlenb;
 static volatile sig_atomic_t got_sigill;
 
 static void sigill_handler(int signo, siginfo_t *si, void *data)
@@ -33,7 +35,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 +51,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 +69,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 +118,20 @@ unsigned __attribute__((constructor)) cpuinfo_init(void)
         assert(left == 0);
     }
 
+    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;
+        /* Read csr "vlenb" with "csrr %0, vlenb" : "=r"(vlenb) */
+        asm volatile(".insn i 0x73, 0x2, %0, zero, -990" : "=r"(vlenb));
+        riscv_lg2_vlenb = ctz32(vlenb);
+    }
+
     info |= CPUINFO_ALWAYS;
     cpuinfo = info;
     return info;
Signature · 1 line
2.43.0
author
#2 lore raw

PATCH v5 2/12 tcg/riscv: Add basic support for vector

In reply to LIU Zhiwei

From: Huang Shiyuan <[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     | 126 ++++++++++++++++++++++++---------
 tcg/riscv/tcg-target.h         |  78 +++++++++++---------
 tcg/riscv/tcg-target.opc.h     |  12 ++++
 4 files changed, 151 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 +93 -33

174 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
#3 lore raw

Re: [PATCH v5 02/12] tcg/riscv: Add basic support for vector

Quoted 18 lines · LIU Zhiwei
tcg/riscv/tcg-target.c.inc +14 -0
@@ -2100,6 +2136,30 @@ 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;
+
+    switch (riscv_lg2_vlenb) {
+    case TCG_TYPE_V64:
+        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 & ALL_VECTOR_REGS);
+        break;
+    case TCG_TYPE_V128:
+        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 & ALL_VECTOR_REGS);

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

Quoted 10 lines
tcg/riscv/tcg-target.c.inc +10 -0
+        break;
+    default:
+        /* Guaranteed by Zve64x. */
+        tcg_debug_assert(riscv_lg2_vlenb >= TCG_TYPE_V256);
+
+        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;
+    }

Missing a check for host vector support.

r~

#4 lore raw

Re: [PATCH v5 02/12] tcg/riscv: Add basic support for vector

In reply to LIU Zhiwei
Quoted 4 lines · LIU Zhiwei

+ }

      tcg_target_call_clobber_regs = -1u;
      tcg_regset_reset_reg(tcg_target_call_clobber_regs, TCG_REG_S0);

I believe all of the vector registers are call-clobbered? This needs

- tcg_target_call_clobber_regs = -1u;
+ tcg_target_call_clobber_regs = -1;

so that all 64 registers begin as call-clobbered.

r~

author
#5 lore raw

PATCH v5 3/12 tcg/riscv: Add vset{i}vli and ld/st vec ops

In reply to LIU Zhiwei

From: TANG Tiancheng <[email protected]>

In RISC-V, vector operations require initial vtype and vl 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 TCG_Type 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.

We save the TCGType and SEW in TCGContext, so that it matches the multi-threaded TCG.

---
 include/tcg/tcg.h              |   7 +
 tcg/riscv/tcg-target-con-set.h |   2 +
 tcg/riscv/tcg-target.c.inc     | 334 ++++++++++++++++++++++++++++++++-
 3 files changed, 338 insertions(+), 5 deletions(-)
include/tcg/tcg.h +7 -0
index 21d5884741..93aa9c30ee 100644
@@ -544,6 +544,12 @@ struct TCGContext {
     struct qemu_plugin_insn *plugin_insn;
 #endif
 
+    /* For host-specific values. */
+#ifdef __riscv
+    MemOp riscv_cur_vsew;
+    TCGType riscv_cur_type;
+#endif
+
     GHashTable *const_table[TCG_TYPE_COUNT];
     TCGTempSet free_temps[TCG_TYPE_COUNT];
     TCGTemp temps[TCG_MAX_TEMPS]; /* globals first, temps after */
@@ -566,6 +572,7 @@ struct TCGContext {
 
     /* Exit to translator on overflow. */
     sigjmp_buf jmp_trans;
+
 };
 
 static inline bool temp_readonly(TCGTemp *ts)
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 +329 -5

427 lines not on this page · raw message

Signature · 1 line
2.43.0
#6 lore raw

Re: [PATCH v5 03/12] tcg/riscv: Add vset{i}vli and ld/st vec ops

Quoted 36 lines · LIU Zhiwei
tcg/riscv/tcg-target.c.inc +36 -0
+static void probe_frac_lmul(void)
+{
+    unsigned long vlmax[3];
+
+    for (int i = MO_8; i <= MO_64; ++i) {
+        switch (i) {
+        case MO_8:
+            asm volatile(
+                "vsetvli %0, zero, e8, mf2\n\t"
+                "vsetvli %1, zero, e8, mf4\n\t"
+                "vsetvli %2, zero, e8, mf8"
+                : "=r"(vlmax[0]), "=r"(vlmax[1]), "=r"(vlmax[2])
+            );
+            break;
+        case MO_16:
+            asm volatile(
+                "vsetvli %0, zero, e16, mf2\n\t"
+                "vsetvli %1, zero, e16, mf4\n\t"
+                "vsetvli %2, zero, e16, mf8"
+                : "=r"(vlmax[0]), "=r"(vlmax[1]), "=r"(vlmax[2])
+            );
+            break;
+        case MO_32:
+            asm volatile(
+                "vsetvli %0, zero, e32, mf2\n\t"
+                "vsetvli %1, zero, e32, mf4\n\t"
+                "vsetvli %2, zero, e32, mf8"
+                : "=r"(vlmax[0]), "=r"(vlmax[1]), "=r"(vlmax[2])
+            );
+            break;
+        case MO_64:
+            asm volatile(
+                "vsetvli %0, zero, e64, mf2\n\t"
+                "vsetvli %1, zero, e64, mf4\n\t"
+                "vsetvli %2, zero, e64, mf8"
+                : "=r"(vlmax[0]), "=r"(vlmax[1]), "=r"(vlmax[2])

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

Quoted 10 lines
tcg/riscv/tcg-target.c.inc +9 -0
+            );
+            break;
+        default:
+            g_assert_not_reached();
+        }
+        vaild_frac_lmul[i][1] = vlmax[0] != 0;
+        vaild_frac_lmul[i][2] = vlmax[1] != 0;
+        vaild_frac_lmul[i][3] = vlmax[2] != 0;
+    }
  }

This fails to build on debian with default cflags:

/home/rth/qemu/src/tcg/riscv/tcg-target.c.inc: Assembler messages:
/home/rth/qemu/src/tcg/riscv/tcg-target.c.inc:2880: Error: unrecognized opcode vsetvli a3,zero,e8,mf2', extension v' or zve64x' or zve32x' required /home/rth/qemu/src/tcg/riscv/tcg-target.c.inc:2881: Error: unrecognized opcode vsetvli a4,zero,e8,mf4', extension v' or zve64x' or zve32x' required /home/rth/qemu/src/tcg/riscv/tcg-target.c.inc:2882: Error: unrecognized opcode vsetvli a5,zero,e8,mf8', extension v' or zve64x' or zve32x' required /home/rth/qemu/src/tcg/riscv/tcg-target.c.inc:2888: Error: unrecognized opcode vsetvli a3,zero,e16,mf2', extension v' or zve64x' or zve32x' required /home/rth/qemu/src/tcg/riscv/tcg-target.c.inc:2889: Error: unrecognized opcode vsetvli a4,zero,e16,mf4', extension v' or zve64x' or zve32x' required /home/rth/qemu/src/tcg/riscv/tcg-target.c.inc:2890: Error: unrecognized opcode vsetvli a5,zero,e16,mf8', extension v' or zve64x' or zve32x' required /home/rth/qemu/src/tcg/riscv/tcg-target.c.inc:2896: Error: unrecognized opcode vsetvli a3,zero,e32,mf2', extension v' or zve64x' or zve32x' required /home/rth/qemu/src/tcg/riscv/tcg-target.c.inc:2897: Error: unrecognized opcode vsetvli a4,zero,e32,mf4', extension v' or zve64x' or zve32x' required /home/rth/qemu/src/tcg/riscv/tcg-target.c.inc:2898: Error: unrecognized opcode vsetvli a5,zero,e32,mf8', extension v' or zve64x' or zve32x' required /home/rth/qemu/src/tcg/riscv/tcg-target.c.inc:2904: Error: unrecognized opcode vsetvli a3,zero,e64,mf2', extension v' or zve64x' or zve32x' required /home/rth/qemu/src/tcg/riscv/tcg-target.c.inc:2905: Error: unrecognized opcode vsetvli a4,zero,e64,mf4', extension v' or zve64x' or zve32x' required /home/rth/qemu/src/tcg/riscv/tcg-target.c.inc:2906: Error: unrecognized opcode vsetvli a5,zero,e64,mf8', extension v' or zve64x' or zve32x' required

Rather than expanding this with a switch over immediate encodings, perhaps better feed encode_vtype() to the .insn encoding of vsetvl.

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

Quoted 5 lines
tcg/riscv/tcg-target.c.inc +1 -0
@@ -2160,6 +2483,7 @@ static void tcg_target_init(TCGContext *s)
         tcg_target_available_regs[TCG_TYPE_V256] = ALL_VECTOR_REGS;
         break;
     }
+    probe_frac_lmul();

You need to avoid this call if the host does not support vectors.

It occurs to me that, rather than caching valid_frac_lmul[][], we can pre-compute encode_vtype and lmul_eq_avl. It's not much of a savings within set_vtype(), but perhaps it is clearer.

r~

author
#7 lore raw

Re: [PATCH v5 03/12] tcg/riscv: Add vset{i}vli and ld/st vec ops

Quoted 64 lines · Richard Henderson
Quoted 45 lines · LIU Zhiwei
tcg/riscv/tcg-target.c.inc +45 -0
+static void probe_frac_lmul(void)
+{
+    unsigned long vlmax[3];
+
+    for (int i = MO_8; i <= MO_64; ++i) {
+        switch (i) {
+        case MO_8:
+            asm volatile(
+                "vsetvli %0, zero, e8, mf2\n\t"
+                "vsetvli %1, zero, e8, mf4\n\t"
+                "vsetvli %2, zero, e8, mf8"
+                : "=r"(vlmax[0]), "=r"(vlmax[1]), "=r"(vlmax[2])
+            );
+            break;
+        case MO_16:
+            asm volatile(
+                "vsetvli %0, zero, e16, mf2\n\t"
+                "vsetvli %1, zero, e16, mf4\n\t"
+                "vsetvli %2, zero, e16, mf8"
+                : "=r"(vlmax[0]), "=r"(vlmax[1]), "=r"(vlmax[2])
+            );
+            break;
+        case MO_32:
+            asm volatile(
+                "vsetvli %0, zero, e32, mf2\n\t"
+                "vsetvli %1, zero, e32, mf4\n\t"
+                "vsetvli %2, zero, e32, mf8"
+                : "=r"(vlmax[0]), "=r"(vlmax[1]), "=r"(vlmax[2])
+            );
+            break;
+        case MO_64:
+            asm volatile(
+                "vsetvli %0, zero, e64, mf2\n\t"
+                "vsetvli %1, zero, e64, mf4\n\t"
+                "vsetvli %2, zero, e64, mf8"
+                : "=r"(vlmax[0]), "=r"(vlmax[1]), "=r"(vlmax[2])
+            );
+            break;
+        default:
+            g_assert_not_reached();
+        }
+        vaild_frac_lmul[i][1] = vlmax[0] != 0;
+        vaild_frac_lmul[i][2] = vlmax[1] != 0;
+        vaild_frac_lmul[i][3] = vlmax[2] != 0;
+    }
Quoted 1 line

  }

This fails to build on debian with default cflags:

/home/rth/qemu/src/tcg/riscv/tcg-target.c.inc: Assembler messages:
/home/rth/qemu/src/tcg/riscv/tcg-target.c.inc:2880: Error: unrecognized opcode vsetvli a3,zero,e8,mf2', extension v' or zve64x' or zve32x' required /home/rth/qemu/src/tcg/riscv/tcg-target.c.inc:2881: Error: unrecognized opcode vsetvli a4,zero,e8,mf4', extension v' or zve64x' or zve32x' required /home/rth/qemu/src/tcg/riscv/tcg-target.c.inc:2882: Error: unrecognized opcode vsetvli a5,zero,e8,mf8', extension v' or zve64x' or zve32x' required /home/rth/qemu/src/tcg/riscv/tcg-target.c.inc:2888: Error: unrecognized opcode vsetvli a3,zero,e16,mf2', extension v' or zve64x' or zve32x' required /home/rth/qemu/src/tcg/riscv/tcg-target.c.inc:2889: Error: unrecognized opcode vsetvli a4,zero,e16,mf4', extension v' or zve64x' or zve32x' required /home/rth/qemu/src/tcg/riscv/tcg-target.c.inc:2890: Error: unrecognized opcode vsetvli a5,zero,e16,mf8', extension v' or zve64x' or zve32x' required /home/rth/qemu/src/tcg/riscv/tcg-target.c.inc:2896: Error: unrecognized opcode vsetvli a3,zero,e32,mf2', extension v' or zve64x' or zve32x' required /home/rth/qemu/src/tcg/riscv/tcg-target.c.inc:2897: Error: unrecognized opcode vsetvli a4,zero,e32,mf4', extension v' or zve64x' or zve32x' required /home/rth/qemu/src/tcg/riscv/tcg-target.c.inc:2898: Error: unrecognized opcode vsetvli a5,zero,e32,mf8', extension v' or zve64x' or zve32x' required /home/rth/qemu/src/tcg/riscv/tcg-target.c.inc:2904: Error: unrecognized opcode vsetvli a3,zero,e64,mf2', extension v' or zve64x' or zve32x' required /home/rth/qemu/src/tcg/riscv/tcg-target.c.inc:2905: Error: unrecognized opcode vsetvli a4,zero,e64,mf4', extension v' or zve64x' or zve32x' required /home/rth/qemu/src/tcg/riscv/tcg-target.c.inc:2906: Error: unrecognized opcode vsetvli a5,zero,e64,mf8', extension v' or zve64x' or zve32x' required

Quoted 1 line

Rather than expanding this with a switch over immediate encodings, perhaps better feed encode_vtype() to the .insn encoding of vsetvl.

OK.

Quoted 9 lines

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

Quoted 7 lines
tcg/riscv/tcg-target.c.inc +1 -0
@@ -2160,6 +2483,7 @@ static void tcg_target_init(TCGContext *s)
          tcg_target_available_regs[TCG_TYPE_V256] = ALL_VECTOR_REGS;
          break;
      }
+    probe_frac_lmul();

You need to avoid this call if the host does not support vectors.

Agree.

Quoted 2 lines

It occurs to me that, rather than caching valid_frac_lmul[][], we can pre-compute encode_vtype and lmul_eq_avl.

Do you mean cache vtype and lmul_eq_avl for different (lmul, sew) pairs instead of valid_frac_lmul?

Thanks,
Zhiwei

Quoted 4 lines

  It's not much of a savings within set_vtype(), but perhaps it is clearer.

r~

#8 lore raw

Re: [PATCH v5 03/12] tcg/riscv: Add vset{i}vli and ld/st vec ops

Quoted 3 lines · LIU Zhiwei
Quoted 1 line

It occurs to me that, rather than caching valid_frac_lmul[][], we can pre-compute encode_vtype and lmul_eq_avl.

Do you mean cache vtype and lmul_eq_avl for different (lmul, sew) pairs instead of valid_frac_lmul?

Or even one step further:

typedef struct VsetCache {
    unsigned movi_insn;
    unsigned vset_insn;
} VsetCache;

static VsetCache riscv_vset_cache[3][4];

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

{
    const VsetCache *p = &riscv_vset_cache[type - TCG_TYPE_V64][vsew];

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

    if (p->movi_insn) {
        tcg_out32(s->p->movi_insn);
    }
    tcg_out32(s, p->vset_insn);
}

static bool vtype_check(unsigned vtype)

{
    unsigned long tmp;
    asm("vsetvl %0, zero, %1" : "=r"(tmp) : "r"(vtype));     /* in .inst form */
    return tmp != 0;
}

static void probe_frac_lmul_1(TCGType type, MemOp vsew)

{
    VsetCache *p = &riscv_vset_cache[type - TCG_TYPE_V64][vsew];
    unsigned avl = tcg_type_size(type) >> vsew;
    int lmul = type - riscv_lg2_vlenb;
    unsigned vtype = encode_vtype(true, true, vsew, lmul & 7);
    bool lmul_eq_avl = true;

    /* Guaranteed by Zve64x. */
    assert(lmul < 3);

    /*
     * For LMUL < -3, the host vector size is so large that TYPE
     * is smaller than the minimum 1/8 fraction.
     *
     * For other fractional LMUL settings, implementations must
     * support SEW settings between SEW_MIN and LMUL * ELEN, inclusive.
     * So if ELEN = 64, LMUL = 1/2, then SEW will support e8, e16, e32,
     * but e64 may not be supported. In other words, the hardware only
     * guarantees SEW_MIN <= SEW <= LMUL * ELEN.  Check.
     */
    if (lmul < 0 && (lmul < -3 || !vtype_check(vtype)) {
        vtype = encode_vtype(true, true, vsew, VLMUL_M1);
        lmul_eq_avl = false;
    }

    if (avl < 32) {
        p->vset_insn = encode_vseti(OPC_VSETIVLI, TCG_REG_ZERO, avl, vtype);
    } else if (lmul_eq_avl) {
        /* rd != 0 and rs1 == 0 uses vlmax */
        p->vset_insn = encode_vset(OPC_VSETVLI, TCG_REG_TMP0, TCG_REG_ZERO, vtype);
    } else {
        p->movi_insn = encode_i(OPC_ADDI, TCG_REG_TMP0, TCG_REG_ZERO, avl);
        p->vset_insn = encode_vset(OPC_VSETVLI, TCG_REG_ZERO, TCG_REG_TMP0, vtype);
    }
}

static void probe_frac_lmul(void)

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

    for (TCGType t = TCG_TYPE_V64; t <= TCG_TYPE_V256; t++) {
        for (MemOp e = MO_8; e <= MO_64; e++) {
            probe_frac_lmul_1(t, e);
        }
    }
}

So that everything is pre-computed at startup.

r~

author
#9 lore raw

PATCH v5 4/12 tcg/riscv: Implement vector mov/dup{m/i}

In reply to LIU Zhiwei

From: TANG Tiancheng <[email protected]>

---
 tcg/riscv/tcg-target.c.inc | 73 ++++++++++++++++++++++++++++++++++++--
 1 file changed, 71 insertions(+), 2 deletions(-)
tcg/riscv/tcg-target.c.inc +71 -2
index aacb1ae28e..5187cdb6ac 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;
 
 /*
@@ -401,6 +407,16 @@ static int32_t encode_uj(RISCVInsn opc, TCGReg rd, uint32_t imm)
     return opc | (rd & 0x1f) << 7 | encode_ujimm20(imm);
 }
 
+
+/* Type-OPIVI */
+
+static int32_t encode_vi(RISCVInsn opc, TCGReg rd, int32_t imm,
+                         TCGReg vs2, bool vm)
+{
+    return opc | (rd & 0x1f) << 7 | (imm & 0x1f) << 15 |
+           (vs2 & 0x1f) << 20 | (vm << 25);
+}
+
 /* Type-OPIVV/OPMVV/OPIVX/OPMVX, Vector load and store */
 
 static int32_t encode_v(RISCVInsn opc, TCGReg d, TCGReg s1,
@@ -546,6 +562,24 @@ static bool patch_reloc(tcg_insn_unit *code_ptr, int type,
  * RISC-V vector instruction emitters
  */
 
+/*
+ * Vector registers uses the same 5 lower bits as GPR registers,
+ * and vm=0 (vm = false) means vector masking ENABLED.
+ * With RVV 1.0, vs2 is the first operand, while rs1/imm is the
+ * second operand.
+ */
+static void tcg_out_opc_vx(TCGContext *s, RISCVInsn opc, TCGReg vd,
+                           TCGReg vs2, TCGReg rs1, bool vm)
+{
+    tcg_out32(s, encode_v(opc, vd, rs1, vs2, vm));
+}
+
+static void tcg_out_opc_vi(TCGContext *s, RISCVInsn opc, TCGReg vd,
+                           TCGReg vs2, int32_t imm, bool vm)
+{
+    tcg_out32(s, encode_vi(opc, vd, imm, vs2, vm));
+}
+
 /*
  * Only unit-stride addressing implemented; may extend in future.
  */
@@ -628,6 +662,13 @@ static MemOp set_vtype_len(TCGContext *s, TCGType type)
     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_vtype(s, type, vsew);
+    }
+}
+
 /*
  * TCG intrinsics
  */
@@ -642,6 +683,15 @@ 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 lmul = type - riscv_lg2_vlenb;
+            int nf = 1 << MAX(lmul, 0);
+            tcg_out_opc_vi(s, OPC_VMVNR_V, ret, arg, nf - 1, true);
+        }
+        break;
     default:
         g_assert_not_reached();
     }
@@ -1005,18 +1055,32 @@ static void tcg_out_addsub2(TCGContext *s,
 static bool tcg_out_dup_vec(TCGContext *s, TCGType type, unsigned vece,
                                    TCGReg dst, TCGReg src)
 {
-    return false;
+    set_vtype_len_sew(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)
 {
-    return false;
+    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) {
+        if (arg == 0 || arg == -1) {
+            set_vtype_len(s, type);
+        } else {
+            set_vtype_len_sew(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 {
@@ -2159,6 +2223,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;
@@ -2330,6 +2397,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
author
#10 lore raw

PATCH v5 5/12 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 |  2 ++
 tcg/riscv/tcg-target.c.inc     | 52 ++++++++++++++++++++++++++++++++++
 tcg/riscv/tcg-target.h         |  2 +-
 3 files changed, 55 insertions(+), 1 deletion(-)
tcg/riscv/tcg-target-con-set.h +2 -0
index d73a62b0f2..4c4bc99355 100644
@@ -23,3 +23,5 @@ 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)
+C_O1_I2(v, v, v)
tcg/riscv/tcg-target.c.inc +52 -0

82 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
author
#11 lore raw

PATCH v5 6/12 tcg/riscv: Implement vector cmp/cmpsel 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.

Expand cmp with cmpsel.

---
 tcg/riscv/tcg-target-con-set.h |   2 +
 tcg/riscv/tcg-target-con-str.h |   2 +
 tcg/riscv/tcg-target.c.inc     | 251 +++++++++++++++++++++++++++------
 tcg/riscv/tcg-target.h         |   2 +-
 4 files changed, 209 insertions(+), 48 deletions(-)
tcg/riscv/tcg-target-con-set.h +2 -0
index 4c4bc99355..cc06102ccf 100644
@@ -25,3 +25,5 @@ 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 +204 -47

319 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
 
Signature · 1 line
2.43.0
author
#12 lore raw

PATCH v5 7/12 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 faa1958b67..e7d7796793 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,
@@ -2401,6 +2402,10 @@ static void tcg_out_vec_op(TCGContext *s, TCGOpcode opc,
         set_vtype_len(s, type);
         tcg_out_opc_vi(s, OPC_VXOR_VI, a0, a1, -1, true);
         break;
+    case INDEX_op_neg_vec:
+        set_vtype_len_sew(s, type, vece);
+        tcg_out_opc_vi(s, OPC_VRSUB_VI, a0, a1, 0, true);
+        break;
     case INDEX_op_cmpsel_vec:
         tcg_out_cmpsel(s, type, vece, args[5], a0, a1, a2, const_args[2],
                        args[3], const_args[3], args[4], const_args[4]);
@@ -2444,6 +2449,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:
@@ -2598,6 +2604,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
#13 lore raw

PATCH v5 8/12 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 e7d7796793..0b2541692a 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,
@@ -2406,6 +2412,26 @@ static void tcg_out_vec_op(TCGContext *s, TCGOpcode opc,
         set_vtype_len_sew(s, type, vece);
         tcg_out_opc_vi(s, OPC_VRSUB_VI, a0, a1, 0, true);
         break;
+    case INDEX_op_mul_vec:
+        set_vtype_len_sew(s, type, vece);
+        tcg_out_opc_vv(s, OPC_VMUL_VV, a0, a1, a2, true);
+        break;
+    case INDEX_op_ssadd_vec:
+        set_vtype_len_sew(s, type, vece);
+        tcg_out_opc_vv(s, OPC_VSADD_VV, a0, a1, a2, true);
+        break;
+    case INDEX_op_sssub_vec:
+        set_vtype_len_sew(s, type, vece);
+        tcg_out_opc_vv(s, OPC_VSSUB_VV, a0, a1, a2, true);
+        break;
+    case INDEX_op_usadd_vec:
+        set_vtype_len_sew(s, type, vece);
+        tcg_out_opc_vv(s, OPC_VSADDU_VV, a0, a1, a2, true);
+        break;
+    case INDEX_op_ussub_vec:
+        set_vtype_len_sew(s, type, vece);
+        tcg_out_opc_vv(s, OPC_VSSUBU_VV, a0, a1, a2, true);
+        break;
     case INDEX_op_cmpsel_vec:
         tcg_out_cmpsel(s, type, vece, args[5], a0, a1, a2, const_args[2],
                        args[3], const_args[3], args[4], const_args[4]);
@@ -2450,6 +2476,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:
@@ -2612,6 +2643,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
#14 lore raw

PATCH v5 9/12 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 0b2541692a..fd9a079d53 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,
@@ -2432,6 +2437,22 @@ static void tcg_out_vec_op(TCGContext *s, TCGOpcode opc,
         set_vtype_len_sew(s, type, vece);
         tcg_out_opc_vv(s, OPC_VSSUBU_VV, a0, a1, a2, true);
         break;
+    case INDEX_op_smax_vec:
+        set_vtype_len_sew(s, type, vece);
+        tcg_out_opc_vv(s, OPC_VMAX_VV, a0, a1, a2, true);
+        break;
+    case INDEX_op_smin_vec:
+        set_vtype_len_sew(s, type, vece);
+        tcg_out_opc_vv(s, OPC_VMIN_VV, a0, a1, a2, true);
+        break;
+    case INDEX_op_umax_vec:
+        set_vtype_len_sew(s, type, vece);
+        tcg_out_opc_vv(s, OPC_VMAXU_VV, a0, a1, a2, true);
+        break;
+    case INDEX_op_umin_vec:
+        set_vtype_len_sew(s, type, vece);
+        tcg_out_opc_vv(s, OPC_VMINU_VV, a0, a1, a2, true);
+        break;
     case INDEX_op_cmpsel_vec:
         tcg_out_cmpsel(s, type, vece, args[5], a0, a1, a2, const_args[2],
                        args[3], const_args[3], args[4], const_args[4]);
@@ -2481,6 +2502,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:
@@ -2648,6 +2673,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
#15 lore raw

PATCH v5 10/12 tcg/riscv: Implement vector shi/s/v 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     | 76 ++++++++++++++++++++++++++++++++++
 tcg/riscv/tcg-target.h         |  6 +--
 3 files changed, 80 insertions(+), 3 deletions(-)
tcg/riscv/tcg-target-con-set.h +1 -0
index cc06102ccf..f40de70001 100644
@@ -24,6 +24,7 @@ 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, r)
 C_O1_I2(v, v, v)
 C_O1_I2(v, v, vL)
 C_O1_I4(v, v, vL, vK, vK)
tcg/riscv/tcg-target.c.inc +76 -0

113 lines not on this page · raw message

tcg/riscv/tcg-target.h +3 -3
index 7005099810..76d30e789b 100644
@@ -157,9 +157,9 @@ typedef enum {
 #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_shs_vec          0
-#define TCG_TARGET_HAS_shv_vec          0
+#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
 #define TCG_TARGET_HAS_sat_vec          1
 #define TCG_TARGET_HAS_minmax_vec       1
Signature · 1 line
2.43.0
author
#16 lore raw

PATCH v5 11/12 tcg/riscv: Implement vector roti/v/x ops

In reply to LIU Zhiwei

From: TANG Tiancheng <[email protected]>

---
 tcg/riscv/tcg-target.c.inc | 36 ++++++++++++++++++++++++++++++++++++
 tcg/riscv/tcg-target.h     |  6 +++---
 2 files changed, 39 insertions(+), 3 deletions(-)
tcg/riscv/tcg-target.c.inc +36 -0
index d43d268597..738e40359a 100644
@@ -2510,6 +2510,34 @@ static void tcg_out_vec_op(TCGContext *s, TCGOpcode opc,
         set_vtype_len_sew(s, type, vece);
         tcg_out_vshifti(s, OPC_VSRA_VI, OPC_VSRA_VX, a0, a1, a2);
         break;
+    case INDEX_op_rotli_vec:
+        set_vtype_len_sew(s, type, vece);
+        tcg_out_vshifti(s, OPC_VSLL_VI, OPC_VSLL_VX, TCG_REG_V0, a1, a2);
+        tcg_out_vshifti(s, OPC_VSRL_VI, OPC_VSRL_VX, a0, a1,
+                        -a2 & ((8 << vece) - 1));
+        tcg_out_opc_vv(s, OPC_VOR_VV, a0, a0, TCG_REG_V0, true);
+        break;
+    case INDEX_op_rotls_vec:
+        set_vtype_len_sew(s, type, vece);
+        tcg_out_opc_vx(s, OPC_VSLL_VX, TCG_REG_V0, a1, a2, true);
+        tcg_out_opc_reg(s, OPC_SUBW, a2, TCG_REG_ZERO, a2);
+        tcg_out_opc_vx(s, OPC_VSRL_VX, a0, a1, a2, true);
+        tcg_out_opc_vv(s, OPC_VOR_VV, a0, a0, TCG_REG_V0, true);
+        break;
+    case INDEX_op_rotlv_vec:
+        set_vtype_len_sew(s, type, vece);
+        tcg_out_opc_vi(s, OPC_VRSUB_VI, TCG_REG_V0, a2, 0, true);
+        tcg_out_opc_vv(s, OPC_VSRL_VV, TCG_REG_V0, a1, TCG_REG_V0, true);
+        tcg_out_opc_vv(s, OPC_VSLL_VV, a0, a1, a2, true);
+        tcg_out_opc_vv(s, OPC_VOR_VV, a0, a0, TCG_REG_V0, true);
+        break;
+    case INDEX_op_rotrv_vec:
+        set_vtype_len_sew(s, type, vece);
+        tcg_out_opc_vi(s, OPC_VRSUB_VI, TCG_REG_V0, a2, 0, true);
+        tcg_out_opc_vv(s, OPC_VSLL_VV, TCG_REG_V0, a1, TCG_REG_V0, true);
+        tcg_out_opc_vv(s, OPC_VSRL_VV, a0, a1, a2, true);
+        tcg_out_opc_vv(s, OPC_VOR_VV, a0, a0, TCG_REG_V0, true);
+        break;
     case INDEX_op_cmpsel_vec:
         tcg_out_cmpsel(s, type, vece, args[5], a0, a1, a2, const_args[2],
                        args[3], const_args[3], args[4], const_args[4]);
@@ -2572,6 +2600,10 @@ int tcg_can_emit_vec_op(TCGOpcode opc, TCGType type, unsigned vece)
     case INDEX_op_shri_vec:
     case INDEX_op_shli_vec:
     case INDEX_op_sari_vec:
+    case INDEX_op_rotls_vec:
+    case INDEX_op_rotlv_vec:
+    case INDEX_op_rotrv_vec:
+    case INDEX_op_rotli_vec:
     case INDEX_op_cmpsel_vec:
         return 1;
     case INDEX_op_cmp_vec:
@@ -2731,6 +2763,7 @@ static TCGConstraintSetIndex tcg_target_op_def(TCGOpcode op)
     case INDEX_op_shli_vec:
     case INDEX_op_shri_vec:
     case INDEX_op_sari_vec:
+    case INDEX_op_rotli_vec:
         return C_O1_I1(v, v);
     case INDEX_op_add_vec:
     case INDEX_op_sub_vec:
@@ -2749,10 +2782,13 @@ static TCGConstraintSetIndex tcg_target_op_def(TCGOpcode op)
     case INDEX_op_shlv_vec:
     case INDEX_op_shrv_vec:
     case INDEX_op_sarv_vec:
+    case INDEX_op_rotlv_vec:
+    case INDEX_op_rotrv_vec:
         return C_O1_I2(v, v, v);
     case INDEX_op_shls_vec:
     case INDEX_op_shrs_vec:
     case INDEX_op_sars_vec:
+    case INDEX_op_rotls_vec:
         return C_O1_I2(v, v, r);
     case INDEX_op_cmp_vec:
         return C_O1_I2(v, v, vL);
tcg/riscv/tcg-target.h +3 -3
index 76d30e789b..e6d66cd1b9 100644
@@ -154,9 +154,9 @@ 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_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
Signature · 1 line
2.43.0
author
#17 lore raw

PATCH v5 12/12 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 e6d66cd1b9..d27007f2e6 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